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 <s.oosthoek@xs4all.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Simon Oosthoek committed Oct 10, 2012 at 21:32 UTC 9b7e776c0a5e6e90601badf175ad034ada5a230e
1 file changed +35 -1
contrib/completion/git-prompt.sh
+35 -1
@@ -54,6 +54,12 @@
54 # find one, or @{upstream} otherwise. Once you have set
55 # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
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
64 # __gitdir accepts 0 or 1 arguments (i.e., location)
65 # returns location of .git repo
@@ -207,6 +213,7 @@ __git_ps1_show_upstream ()
213 # in that case it _sets_ PS1. The arguments are parts of a PS1 string.
214 # when both arguments are given, the first is prepended and the second appended
215 # to the state string when assigned to PS1.
216 +# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
217 __git_ps1 ()
218 {
219 local pcmode=no
@@ -320,9 +327,36 @@ __git_ps1 ()
327 local f="$w$i$s$u"
328 if [ $pcmode = yes ]; then
329 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
357 PS1="$PS1)$ps1pc_end"
358 else
359 + # NO color option unless in PROMPT_COMMAND mode
360 printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"
361 fi
362 fi