Skip to content

Commit 9b3aaf8

Browse files
pooh22gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b7e776 commit 9b3aaf8

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

contrib/completion/git-prompt.sh

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@
5656
# setting the bash.showUpstream config variable.
5757
#
5858
# 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+
#
6462
# __gitdir accepts 0 or 1 arguments (i.e., location)
6563
# returns location of .git repo
6664
__gitdir ()
@@ -217,7 +215,7 @@ __git_ps1_show_upstream ()
217215
__git_ps1 ()
218216
{
219217
local pcmode=no
220-
#defaults/examples:
218+
local detached=no
221219
local ps1pc_start='\u@\h:\w '
222220
local ps1pc_end='\$ '
223221
local printf_format=' (%s)'
@@ -266,7 +264,7 @@ __git_ps1 ()
266264
fi
267265

268266
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
269-
267+
detached=yes
270268
b="$(
271269
case "${GIT_PS1_DESCRIBE_STYLE-}" in
272270
(contains)
@@ -326,35 +324,46 @@ __git_ps1 ()
326324

327325
local f="$w$i$s$u"
328326
if [ $pcmode = yes ]; then
329-
PS1="$ps1pc_start("
330-
if [ -n "${GIT_PS1_SHOWCOLORHINT-}" ]; then
327+
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
331328
local c_red='\e[31m'
332329
local c_green='\e[32m'
333-
local c_yellow='\e[33m'
334330
local c_lblue='\e[1;34m'
335-
local c_purple='\e[35m'
336-
local c_cyan='\e[36m'
337331
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"
338336
local branchstring="$c${b##refs/heads/}"
339-
local branch_color="$c_green"
340-
local flags_color="$c_cyan"
341337

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"
346342
fi
347343

348344
# Setting PS1 directly with \[ and \] around colors
349345
# 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"
353362
fi
363+
PS1="$PS1\[$c_clear\]$r$p)$ps1pc_end"
354364
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"
356366
fi
357-
PS1="$PS1)$ps1pc_end"
358367
else
359368
# NO color option unless in PROMPT_COMMAND mode
360369
printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"

0 commit comments

Comments
 (0)