Fix up colored git-prompt
The main point is to match the colors to be more close to the color output of "git status -sb". - the branchname is green, or in red when the HEAD is detached; - the flags are either red or green for unstaged/staged and the remaining flags get a different color or none at all. Signed-off-by: Simon Oosthoek <s.oosthoek@xs4all.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Simon Oosthoek committed
Oct 16, 2012 at 21:34 UTC
9b3aaf8bf19ce2bf5e73b972dce31430e43df8dc
1 file changed
+32
-23
contrib/completion/git-prompt.sh
+32
-23
@@ -56,11 +56,9 @@
56
# setting the bash.showUpstream config variable.
57
#
58
# If you would like a colored hint about the current dirty state, set
59
-# GIT_PS1_SHOWCOLORHINTS to a nonempty value. When tracked files are
60
-# modified, the branch name turns red, when all modifications are staged
61
-# the branch name turns yellow and when all changes are checked in, the
62
-# color changes to green. The colors are currently hardcoded in the function.
63
-
59
+# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
60
+# the colored output of "git status -sb".
61
+#
62
# __gitdir accepts 0 or 1 arguments (i.e., location)
63
# returns location of .git repo
64
__gitdir ()
@@ -217,7 +215,7 @@ __git_ps1_show_upstream ()
215
__git_ps1 ()
216
{
217
local pcmode=no
220
- #defaults/examples:
218
+ local detached=no
219
local ps1pc_start='\u@\h:\w '
220
local ps1pc_end='\$ '
221
local printf_format=' (%s)'
@@ -266,7 +264,7 @@ __git_ps1 ()
264
fi
265
266
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
269
-
267
+ detached=yes
268
b="$(
269
case "${GIT_PS1_DESCRIBE_STYLE-}" in
270
(contains)
@@ -326,35 +324,46 @@ __git_ps1 ()
324
325
local f="$w$i$s$u"
326
if [ $pcmode = yes ]; then
329
- PS1="$ps1pc_start("
330
- if [ -n "${GIT_PS1_SHOWCOLORHINT-}" ]; then
327
+ if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
328
local c_red='\e[31m'
329
local c_green='\e[32m'
333
- local c_yellow='\e[33m'
330
local c_lblue='\e[1;34m'
335
- local c_purple='\e[35m'
336
- local c_cyan='\e[36m'
331
local c_clear='\e[0m'
332
+ local bad_color=$c_red
333
+ local ok_color=$c_green
334
+ local branch_color="$c_clear"
335
+ local flags_color="$c_lblue"
336
local branchstring="$c${b##refs/heads/}"
339
- local branch_color="$c_green"
340
- local flags_color="$c_cyan"
337
342
- if [ "$w" = "*" ]; then
343
- branch_color="$c_red"
344
- elif [ -n "$i" ]; then
345
- branch_color="$c_yellow"
338
+ if [ $detached = yes ]; then
339
+ branch_color="$ok_color"
340
+ else
341
+ branch_color="$bad_color"
342
fi
343
344
# Setting PS1 directly with \[ and \] around colors
345
# is necessary to prevent wrapping issues!
350
- PS1="$PS1\[$branch_color\]$branchstring\[$c_clear\]"
351
- if [ -n "$f" ]; then
352
- PS1="$PS1 \[$flags_color\]$f\[$c_clear\]"
346
+ PS1="$ps1pc_start (\[$branch_color\]$branchstring\[$c_clear\]"
347
+
348
+ if [ -n "$w$i$s$u$r$p" ]; then
349
+ PS1="$PS1 "
350
+ fi
351
+ if [ "$w" = "*" ]; then
352
+ PS1="$PS1\[$bad_color\]$w"
353
+ fi
354
+ if [ -n "$i" ]; then
355
+ PS1="$PS1\[$ok_color\]$i"
356
+ fi
357
+ if [ -n "$s" ]; then
358
+ PS1="$PS1\[$flags_color\]$s"
359
+ fi
360
+ if [ -n "$u" ]; then
361
+ PS1="$PS1\[$bad_color\]$u"
362
fi
363
+ PS1="$PS1\[$c_clear\]$r$p)$ps1pc_end"
364
else
355
- PS1="$PS1$c${b##refs/heads/}${f:+ $f}$r$p"
365
+ PS1="$ps1pc_start ($c${b##refs/heads/}${f:+ $f}$r$p)$ps1pc_end"
366
fi
357
- PS1="$PS1)$ps1pc_end"
367
else
368
# NO color option unless in PROMPT_COMMAND mode
369
printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"