@@ -245,7 +245,8 @@ __git_ps1_show_upstream ()
245245
246246# Helper function that is meant to be called from __git_ps1. It
247247# injects color codes into the appropriate gitstring variables used
248- # to build a gitstring.
248+ # to build a gitstring. Colored variables are responsible for clearing
249+ # their own color.
249250__git_ps1_colorize_gitstring ()
250251{
251252 if [[ -n ${ZSH_VERSION-} ]]; then
@@ -271,22 +272,23 @@ __git_ps1_colorize_gitstring ()
271272 else
272273 branch_color=" $bad_color "
273274 fi
274- c=" $branch_color$c "
275+ if [ -n " $c " ]; then
276+ c=" $branch_color$c$c_clear "
277+ fi
278+ b=" $branch_color$b$c_clear "
275279
276- z=" $c_clear$z "
277- if [ " $w " = " *" ]; then
278- w=" $bad_color$w "
280+ if [ -n " $w " ]; then
281+ w=" $bad_color$w$c_clear "
279282 fi
280283 if [ -n " $i " ]; then
281- i=" $ok_color$i "
284+ i=" $ok_color$i$c_clear "
282285 fi
283286 if [ -n " $s " ]; then
284- s=" $flags_color$s "
287+ s=" $flags_color$s$c_clear "
285288 fi
286289 if [ -n " $u " ]; then
287- u=" $bad_color$u "
290+ u=" $bad_color$u$c_clear "
288291 fi
289- r=" $c_clear$r "
290292}
291293
292294# Helper function to read the first line of a file into a variable.
@@ -556,19 +558,19 @@ __git_ps1 ()
556558
557559 local z=" ${GIT_PS1_STATESEPARATOR-" " } "
558560
561+ b=${b## refs/ heads/ }
562+ if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
563+ __git_ps1_branch_name=$b
564+ b=" \$ {__git_ps1_branch_name}"
565+ fi
566+
559567 # NO color option unless in PROMPT_COMMAND mode or it's Zsh
560568 if [ -n " ${GIT_PS1_SHOWCOLORHINTS-} " ]; then
561569 if [ $pcmode = yes ] || [ -n " ${ZSH_VERSION-} " ]; then
562570 __git_ps1_colorize_gitstring
563571 fi
564572 fi
565573
566- b=${b## refs/ heads/ }
567- if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
568- __git_ps1_branch_name=$b
569- b=" \$ {__git_ps1_branch_name}"
570- fi
571-
572574 local f=" $h$w$i$s$u$p "
573575 local gitstring=" $c$b ${f: +$z$f }${sparse} $r ${upstream} "
574576
0 commit comments