Skip to content

Commit 9b7e776

Browse files
pooh22gitster
authored andcommitted
show color hints based on state of the git tree
By setting GIT_PS1_SHOW_COLORHINTS when using __git_ps1 as PROMPT_COMMAND, you will get color hints in addition to a different character (*+% etc.) to indicate the state of the tree. Signed-off-by: Simon Oosthoek <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1bfc51a commit 9b7e776

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

contrib/completion/git-prompt.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
# find one, or @{upstream} otherwise. Once you have set
5555
# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
5656
# 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.
5763

5864
# __gitdir accepts 0 or 1 arguments (i.e., location)
5965
# returns location of .git repo
@@ -207,6 +213,7 @@ __git_ps1_show_upstream ()
207213
# in that case it _sets_ PS1. The arguments are parts of a PS1 string.
208214
# when both arguments are given, the first is prepended and the second appended
209215
# to the state string when assigned to PS1.
216+
# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
210217
__git_ps1 ()
211218
{
212219
local pcmode=no
@@ -320,9 +327,36 @@ __git_ps1 ()
320327
local f="$w$i$s$u"
321328
if [ $pcmode = yes ]; then
322329
PS1="$ps1pc_start("
323-
PS1="$PS1$c${b##refs/heads/}${f:+ $f}$r$p"
330+
if [ -n "${GIT_PS1_SHOWCOLORHINT-}" ]; then
331+
local c_red='\e[31m'
332+
local c_green='\e[32m'
333+
local c_yellow='\e[33m'
334+
local c_lblue='\e[1;34m'
335+
local c_purple='\e[35m'
336+
local c_cyan='\e[36m'
337+
local c_clear='\e[0m'
338+
local branchstring="$c${b##refs/heads/}"
339+
local branch_color="$c_green"
340+
local flags_color="$c_cyan"
341+
342+
if [ "$w" = "*" ]; then
343+
branch_color="$c_red"
344+
elif [ -n "$i" ]; then
345+
branch_color="$c_yellow"
346+
fi
347+
348+
# Setting PS1 directly with \[ and \] around colors
349+
# 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\]"
353+
fi
354+
else
355+
PS1="$PS1$c${b##refs/heads/}${f:+ $f}$r$p"
356+
fi
324357
PS1="$PS1)$ps1pc_end"
325358
else
359+
# NO color option unless in PROMPT_COMMAND mode
326360
printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"
327361
fi
328362
fi

0 commit comments

Comments
 (0)