1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
.TH GIT\-SONAR 1 git\-sonar
.SH NAME
git\-sonar \- a heads up display for git
.SH SYNOPSIS
.B git\-sonar
.RB [ options ]
.SH DESCRIPTION
git\-sonar is a tool you can add to your shell prompt to provide at\-a\-glance
information about your git repositories. This includes information such as the
name of the checked\-out branch, a summary of the
.B git status
for modified files, the number of commits which may be pushed or pulled, and
more. See the "PROMPT ELEMENTS" section for examples of various possible prompt
outputs.
.P
After installation, git\-sonar is activated by placing a call to the script in
your shell's
.B $PS1
environment variable (or the equivalent). See the "USAGE" section for
additional instructions on setting up some common shells.
.P
git\-sonar's appearance can be customized by setting various environment
variables. See the "CONFIGURATION" section for an exhaustive list of supported
variables and their descriptions.
.P
As an additional feature, git\-sonar may be configured to periodically and
automatically run
.B git fetch
in the repositories your shell visits. Opt in to this feature by providing
.B \-\-fetch
to the git\-sonar command line.
.SH USAGE
To use git\-sonar, you need to add it to your shell prompt. This is done in
different ways depending on your shell.
.SS bash
.nf
# Add to .bashrc
PS1="...\\$(git-sonar)..."
.fi
.SS zsh
.nf
# Add to .zshrc
setopt PROMPT_SUBST
export GIT_SONAR_COLOR_PREFIX='%{'
export GIT_SONAR_COLOR_SUFFIX='%}'
PROMPT='...$(git-sonar)...'
.fi
.SS fish
.nf
# Add to config.fish
function fish_prompt
# ...
echo -n (git-sonar)
# ...
end
.fi
.SS Ensuring prompt execution
When setting the prompt variable, it is crucial that git\-sonar executes each
time the prompt renders. This way, the prompt will be able to react to changes
in the repository. Some details of the above snippets are necessary to ensure
this happens:
.P
\- bash requires the use of double\-quotes around the value of
.B $PS1.
.br
\- bash requires escaping the call to
.B git\-sonar
(add the backslash character).
.br
\- zsh requires the use of single\-quotes around the value of
.B $PROMPT,
as well as the use of
.B PROMPT_SUBST.
.SH PROMPT ELEMENTS
All of the information output by the git\-sonar prompt is implemented by one of
many prompt elements. By default, each one is present in the rendered prompt,
unless it has no information to report. Each element is named in the form of
.B %{element},
as this is the syntax used by the prompt format customization string (see the
"CONFIGURATION" section for more details). Furthermore, most elements may be
disabled by using a command\-line option (see "OPTIONS").
.TP
.B %{branch}
Outputs the name of the checked\-out branch. If
.B HEAD
is detached, outputs the abbreviated commit hash.
.nf
git:(my-branch)
git:(detached@cb39e58e)
.fi
.TP
.B %{status}
Outputs a summary of
.B git status \-\-porcelain,
consisting of a count of changed files \- both by change type (addition,
modification, deletion, etc.) as well as stage (changes staged in the index,
unmerged, unstaged, or untracked files).
Changes from the same stage are grouped together and color\-coded (see
"CONFIGURATION" for setting colors).
.nf
git:(my-branch) 1\m[green]\fBA\fR\m[] 2\m[red]\fBM\fR\m[]1\m[red]\fBD\fR\m[] 3?
git:(my-branch) 2\m[yellow]\fBU\fR\m[]1\m[yellow]\fBD\fR\m[]
.fi
The first example shows 1 new file staged for commit, 2 unstaged modifications,
1 unstaged file deletion, as well as 3 untracked files which are unknown to the
repository.
The second example shows us in the middle of a failed merge, with 2 "both
modified" unmerged files and 1 file "deleted by them/us".
The change indicators displayed are the same ones used by
.BR git\-status (1)
in the
.B \-\-short
output format.
.TP
.B %{stash}
If any stashed changes are saved, this element outputs the number of stashes
available with a customizable icon.
.nf
git:(my-branch) 2\m[yellow]\fB≡\fR\m[]
.fi
The example shows 2 stashed changes previously saved in this repository.
.TP
.B %{local}
Outputs the number of commits that are different between
.B HEAD
and the configured upstream branch
.B @{u},
if present. If there is no upstream branch, or the two are pointing to the
same commit, nothing is output.
.nf
git:(my-branch \m[green]\fB↑\fR\m[]5)
git:(my-branch 6\m[red]\fB↓\fR\m[])
git:(my-branch 6\m[yellow]\fB⇵\fR\m[]5)
.fi
In the three examples, the upstream branch is
.B remotes/origin/my\-branch,
and we are: ahead by 5 commits, behind by 6 commits, and diverged by 5/6 commits
respectively.
.TP
.B %{remote}
This element functions the same as the
.B %{local}
element, except that the upstream branch
.B @{u}
is compared to the repository's default branch.
.nf
git:(\m[green]\fB↑\fR\m[]5 my-branch)
git:(6\m[red]\fB↓\fR\m[] my-branch)
git:(6\m[yellow]\fB⇵\fR\m[]5 my-branch)
.fi
In the three examples, the upstream branch is
.B remotes/origin/my\-branch,
the default remote branch is
.B remotes/origin/master,
and: my\-branch is ahead by 5 commits, master is ahead by 6 commits, and the two
are diverged by 5/6 commits respectively.
The default branch is determined automatically.
.TP
.B %{alert}
Outputs a customizable icon whenever the repository is in the middle of an
ongoing operation such as a merge, rebase, bisect, etc., or if the branch's
configured upstream no longer exists.
.nf
git:(\m[yellow]\fB⚡\fR\m[]detached@2fb1cc17)
.fi
The example shows us in the middle of a bisect session. Since we are bisecting,
.B HEAD
is detached, with the next revision to test checked out.
See also,
.BR git\-precheck (1).
.P
Below is a more complete example, showcasing many elements of the prompt
rendering at once in a fairly complex situation:
.nf
o\-\-\-o\-\-\-o\-\-\-o\-\-\-o origin/master
\e
o\-\-\-o origin/my\-branch
\e
o\-\-\-o HEAD, my\-branch
git:(\m[yellow]\fB⚡\fR\m[]3\m[yellow]\fB⇵\fR\m[]2 my-branch \m[green]\fB↑\fR\m[]2) 1\m[yellow]\fB≡\fR\m[] 2\m[green]\fBM\fR\m[] 1\m[yellow]\fBU\fR\m[]
.fi
.B my\-branch
is checked out, with the shown branch topology. A merge from some other commit
has started, but could not complete automatically. Two files from the other
side of the merge have no conflicts and are already staged, however 1 file is in
conflict and must be resolved. There is also 1 saved stash being shown.
.SH OPTIONS
.TP
.B \-v, \-\-version
Print the version of git\-sonar and exit.
.TP
.B \-h, \-\-help
Display help for git\-sonar (opens this man page).
.TP
.B \-\-fetch
git\-sonar will automatically fetch from remote repositories after a
configurable amount of time passes. A timestamp of the latest auto\-fetch is
stored in the
.B $GIT_DIR/lastupdatetime
file. The fetch operation is only triggered when the prompt renders, only runs
in the repository your shell is in, and executes in the background.
.P
The remaining options affect the rendered prompt and are provided as a simple
way to disable features without the need to use a custom prompt format string.
For more powerful customization, see the "CONFIGURATION" section.
.TP
.B \-\-no\-remote\-commits
Disable rendering the
.B %{remote}
prompt element, even if it is present in
.B $GIT_SONAR_PROMPT_FORMAT.
.TP
.B \-\-no\-local\-commits
Disable rendering the
.B %{local}
prompt element, even if it is present in
.B $GIT_SONAR_PROMPT_FORMAT.
.TP
.B \-\-no\-status
Disable rendering the
.B %{status}
prompt element, even if it is present in
.B $GIT_SONAR_PROMPT_FORMAT.
For simpler prompts, disabling this feature provides a decent speedup, but omits
a lot of information.
.TP
.B \-\-no\-stash
Disable rendering the
.B %{stash}
prompt element, even if it is present in
.B $GIT_SONAR_PROMPT_FORMAT.
.SH CONFIGURATION
Colors are customized by using ANSI color codes with the environment variables
listed below. However, note that these escape sequences (and any unprintable
characters used) need to be wrapped in markers that your shell uses to track the
length of the prompt and handle line wrapping. These markers will vary by shell
and it is the responsibility of the user to insert the correct ones when
exporting overrides for the variables below. The default colors use markers
that are compatible with bash. Overrides for zsh can be seen in the "USAGE"
section of this document.
.P
As a convention for this section, this syntax will be used to denote the use of
a default color in another variable:
.nf
${<color\-name>} := [prefix marker] \\033[XXXm [suffix marker]
.fi
In cases where a default color is used, the markers remain configurable using
a pair of variables below.
.B XXX
represents the color attributes required by
.B <color\-name>.
.TP
.B $GIT_SONAR_COLOR_PREFIX
The prefix escape code marker used by default colors. User provided variables
must insert their own markers and are unaffected by this value.
Default:
.B \\\001
(bash compliant)
.TP
.B $GIT_SONAR_COLOR_SUFFIX
The suffix escape code marker used by default colors. User provided variables
must insert their own markers are are unaffected by this value.
Default:
.B \\\002
(bash compliant)
.TP
.B $GIT_SONAR_DEFAULT_COLOR
The color which the terminal is reset to at the end of each prompt element.
This variable is mainly useful for preserving a background color inside the
git\-sonar prompt.
Default:
.B ${reset}
(all terminal attributes cleared)
.TP
.B $GIT_SONAR_PROMPT_COLOR
The text color used by the default prompt format string for the
.B 'git:(' ')'
characters surrounding the branch information. If the user provides their own
value for
.B $GIT_SONAR_PROMPT_FORMAT,
it is unaffected by this value.
Default:
.B ${bright black}
.TP
.B $GIT_SONAR_PROMPT_FORMAT
When inside a git repository, this string controls the format of the output
produced by git\-sonar. It functions similarly as a printf\-style format
string: most characters are output verbatim, and a format specifier syntax is
used to substitute output from the prompt elements:
.nf
%{element-name}
.fi
If
.B element\-name
is either prefixed or suffixed by a colon, the colon delimits a string that is
affixed to the element output if and only if the element has any output to
produce. For example,
.B %{[:stash:]}
produces [1\m[yellow]\fB≡\fR\m[]] if a stash is available, but the extra
brackets are omitted otherwise. The default format string utilizes this to add
optional spacing around elements which don't always appear active.
See the "PROMPT ELEMENTS" section for a description of all possible outputs.
.nf
Default: (space) git:(%{alert}%{remote: }%{branch}%{ :local})%{ :stash}%{ :status}
.fi
.TP
.B $GIT_SONAR_FETCH_TIME
The number of seconds between automatic remote fetches when using
.B \-\-fetch.
Default:
.B 300
(5 minutes)
.TP
.B $GIT_SONAR_ALERT_ICON
The symbol shown by the
.B %{alert}
prompt element.
Default:
.B ${yellow}⚡
.TP
.B $GIT_SONAR_STASH_ICON
The symbol shown by the
.B %{stash}
prompt element.
Default:
.B ${yellow}≡
.TP
.B $GIT_SONAR_BRANCH_COLOR
The text color used by the
.B %{branch}
prompt element when on a branch.
Default:
.B $GIT_SONAR_DEFAULT_COLOR
.TP
.B $GIT_SONAR_DETACHED_COLOR
The text color used by the
.B %{branch}
prompt element when
.B HEAD
is detached.
Default:
.B $GIT_SONAR_DEFAULT_COLOR
.TP
.B $GIT_SONAR_STATUS_SEPARATOR
The string used to separate the different change stages output by the
.B %{status}
prompt element.
Default:
.B (space)
.TP
.B $GIT_SONAR_AHEAD_ICON
The symbol shown by the
.B %{local}
and
.B %{remote}
prompt elements when the primary ref is strictly ahead of the secondary.
Default:
.B ${green}↑
.TP
.B $GIT_SONAR_BEHIND_ICON
The symbol shown by the
.B %{local}
and
.B %{remote}
prompt elements when the primary ref is strictly behind the secondary.
Default:
.B ${red}↓
.TP
.B $GIT_SONAR_DIVERGED_ICON
The symbol shown by the
.B %{local}
and
.B %{remote}
prompt elements when the primary and secondary refs each hold unique commits.
Default:
.B ${yellow}⇵
.TP
.B $GIT_SONAR_STAGED_COLOR
The text color used by the
.B %{status}
prompt element for change indicators of changes staged in the index.
Default:
.B ${green}
.TP
.B $GIT_SONAR_UNSTAGED_COLOR
The text color used by the
.B %{status}
prompt element for change indicators of unstaged changes.
Default:
.B ${red}
.TP
.B $GIT_SONAR_UNMERGED_COLOR
The text color used by the
.B %{status}
prompt element for change indicators of unmerged paths (conflicted files).
Default:
.B ${yellow}
.TP
.B $GIT_SONAR_UNTRACKED_COLOR
The text color used by the
.B %{status}
prompt element for the untracked file indicator.
Default:
.B ${white}
.SH SEE ALSO
.BR git\-fetch (1),
.BR git\-log (1),
.BR git\-precheck (1),
.BR git\-stash (1),
.BR git\-status (1)
.SH LICENSE
.B git\-sonar
is available under the terms of the
.B MIT
license.
|