Skip to content

Commit ddbc078

Browse files
committed
Merge branch 'jp/prompt-clear-before-upstream-mark'
Bash command line prompt (in contrib/) update. * jp/prompt-clear-before-upstream-mark: git-prompt: fix expansion of branch colour codes git-prompt: make colourization consistent
2 parents 325240d + 0e5d9ef commit ddbc078

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

contrib/completion/git-prompt.sh

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

t/t9903-bash-prompt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirt
590590
'
591591

592592
test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
593-
printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER\\nmain" >expected &&
593+
printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}${c_green}+${c_clear}):AFTER\\nmain" >expected &&
594594
echo "dirty index" >file &&
595595
test_when_finished "git reset --hard" &&
596596
git add -u &&

0 commit comments

Comments
 (0)