2424# will show username, at-sign, host, colon, cwd, then
2525# various status string, followed by dollar and SP, as
2626# your prompt.
27+ # Optionally, you can supply a third argument with a printf
28+ # format string to finetune the output of the branch status
2729#
2830# The argument to __git_ps1 will be displayed only if you are currently
2931# in a git repository. The %s token will be the name of the current
@@ -213,10 +215,12 @@ __git_ps1_show_upstream ()
213215# when called from PS1 using command substitution
214216# in this mode it prints text to add to bash PS1 prompt (includes branch name)
215217#
216- # __git_ps1 requires 2 arguments when called from PROMPT_COMMAND (pc)
218+ # __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
217219# in that case it _sets_ PS1. The arguments are parts of a PS1 string.
218- # when both arguments are given, the first is prepended and the second appended
220+ # when two arguments are given, the first is prepended and the second appended
219221# to the state string when assigned to PS1.
222+ # The optional third parameter will be used as printf format string to further
223+ # customize the output of the git-status string.
220224# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
221225__git_ps1 ()
222226{
@@ -227,9 +231,10 @@ __git_ps1 ()
227231 local printf_format=' (%s)'
228232
229233 case " $# " in
230- 2) pcmode=yes
234+ 2|3 ) pcmode=yes
231235 ps1pc_start=" $1 "
232236 ps1pc_end=" $2 "
237+ printf_format=" ${3:- $printf_format } "
233238 ;;
234239 0|1) printf_format=" ${1:- $printf_format } "
235240 ;;
@@ -330,6 +335,7 @@ __git_ps1 ()
330335
331336 local f=" $w$i$s$u "
332337 if [ $pcmode = yes ]; then
338+ local gitstring=
333339 if [ -n " ${GIT_PS1_SHOWCOLORHINTS-} " ]; then
334340 local c_red=' \e[31m'
335341 local c_green=' \e[32m'
@@ -347,29 +353,31 @@ __git_ps1 ()
347353 branch_color=" $bad_color "
348354 fi
349355
350- # Setting PS1 directly with \[ and \] around colors
356+ # Setting gitstring directly with \[ and \] around colors
351357 # is necessary to prevent wrapping issues!
352- PS1= " $ps1pc_start ( \[$branch_color \]$branchstring \[$c_clear \]"
358+ gitstring= " \[$branch_color \]$branchstring \[$c_clear \]"
353359
354360 if [ -n " $w$i$s$u$r$p " ]; then
355- PS1 =" $PS1 "
361+ gitstring =" $gitstring "
356362 fi
357363 if [ " $w " = " *" ]; then
358- PS1 =" $PS1 \[$bad_color \]$w "
364+ gitstring =" $gitstring \[$bad_color \]$w "
359365 fi
360366 if [ -n " $i " ]; then
361- PS1 =" $PS1 \[$ok_color \]$i "
367+ gitstring =" $gitstring \[$ok_color \]$i "
362368 fi
363369 if [ -n " $s " ]; then
364- PS1 =" $PS1 \[$flags_color \]$s "
370+ gitstring =" $gitstring \[$flags_color \]$s "
365371 fi
366372 if [ -n " $u " ]; then
367- PS1 =" $PS1 \[$bad_color \]$u "
373+ gitstring =" $gitstring \[$bad_color \]$u "
368374 fi
369- PS1 =" $PS1 \[$c_clear \]$r$p ) $ps1pc_end "
375+ gitstring =" $gitstring \[$c_clear \]$r$p "
370376 else
371- PS1 =" $ps1pc_start ( $ c${b## refs/ heads/ }${f: + $f } $r$p ) $ps1pc_end "
377+ gitstring =" $c ${b## refs/ heads/ }${f: + $f } $r$p "
372378 fi
379+ gitstring=$( printf -- " $printf_format " " $gitstring " )
380+ PS1=" $ps1pc_start$gitstring$ps1pc_end "
373381 else
374382 # NO color option unless in PROMPT_COMMAND mode
375383 printf -- " $printf_format " " $c ${b## refs/ heads/ }${f: + $f } $r$p "
0 commit comments