git-prompt: fix expansion of branch colour codes
Because of the wrapping of the branch name variable $b, the colour codes in the variable don't get applied, but are instead printed directly in the output. Move the wrapping of $b to before colour codes are inserted to correct this. Revert move of branch name colour codes in tests, as the branch name is now coloured after the wrapping instead of before. Signed-off-by: Joakim Petersen <joak-pet@online.no> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Joakim Petersen committed
Jun 10, 2022 at 02:47 UTC
0e5d9ef395467619b621540a7fdefbfc8062f2ac
2 files changed
+15
-15
contrib/completion/git-prompt.sh
+6
-6
@@ -556,9 +556,14 @@ __git_ps1 ()
556
fi
557
fi
558
559
- b=${b##refs/heads/}
559
local z="${GIT_PS1_STATESEPARATOR-" "}"
560
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
+
567
# NO color option unless in PROMPT_COMMAND mode or it's Zsh
568
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
569
if [ $pcmode = yes ] || [ -n "${ZSH_VERSION-}" ]; then
@@ -566,11 +571,6 @@ __git_ps1 ()
571
fi
572
fi
573
569
- if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
570
- __git_ps1_branch_name=$b
571
- b="\${__git_ps1_branch_name}"
572
- fi
573
-
574
local f="$h$w$i$s$u$p"
575
local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"
576
t/t9903-bash-prompt.sh
+9
-9
@@ -541,7 +541,7 @@ test_expect_success 'prompt - pc mode' '
541
'
542
543
test_expect_success 'prompt - bash color pc mode - branch name' '
544
- printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\n${c_green}main${c_clear}" >expected &&
544
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmain" >expected &&
545
(
546
GIT_PS1_SHOWCOLORHINTS=y &&
547
__git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
@@ -551,7 +551,7 @@ test_expect_success 'prompt - bash color pc mode - branch name' '
551
'
552
553
test_expect_success 'prompt - bash color pc mode - detached head' '
554
- printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\n${c_red}(%s...)"${c_clear} $(git log -1 --format="%h" b1^) >expected &&
554
+ printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
555
git checkout b1^ &&
556
test_when_finished "git checkout main" &&
557
(
@@ -563,7 +563,7 @@ test_expect_success 'prompt - bash color pc mode - detached head' '
563
'
564
565
test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
566
- printf "BEFORE: (\${__git_ps1_branch_name} ${c_red}*${c_clear}):AFTER\\n${c_green}main${c_clear}" >expected &&
566
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmain" >expected &&
567
echo "dirty" >file &&
568
test_when_finished "git reset --hard" &&
569
(
@@ -576,7 +576,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirt
576
'
577
578
test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
579
- printf "BEFORE: (\${__git_ps1_branch_name} ${c_green}+${c_clear}):AFTER\\n${c_green}main${c_clear}" >expected &&
579
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmain" >expected &&
580
echo "dirty" >file &&
581
test_when_finished "git reset --hard" &&
582
git add -u &&
@@ -590,7 +590,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirt
590
'
591
592
test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
593
- printf "BEFORE: (\${__git_ps1_branch_name} ${c_red}*${c_clear}${c_green}+${c_clear}):AFTER\\n${c_green}main${c_clear}" >expected &&
593
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}${c_green}+${c_clear}):AFTER\\nmain" >expected &&
594
echo "dirty index" >file &&
595
test_when_finished "git reset --hard" &&
596
git add -u &&
@@ -605,7 +605,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirt
605
'
606
607
test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
608
- printf "BEFORE: (\${__git_ps1_branch_name} ${c_green}#${c_clear}):AFTER\\n${c_green}main${c_clear}" >expected &&
608
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmain" >expected &&
609
(
610
GIT_PS1_SHOWDIRTYSTATE=y &&
611
GIT_PS1_SHOWCOLORHINTS=y &&
@@ -617,7 +617,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - befo
617
'
618
619
test_expect_success 'prompt - bash color pc mode - inside .git directory' '
620
- printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\n${c_green}GIT_DIR!${c_clear}" >expected &&
620
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
621
echo "dirty" >file &&
622
test_when_finished "git reset --hard" &&
623
(
@@ -631,7 +631,7 @@ test_expect_success 'prompt - bash color pc mode - inside .git directory' '
631
'
632
633
test_expect_success 'prompt - bash color pc mode - stash status indicator' '
634
- printf "BEFORE: (\${__git_ps1_branch_name} ${c_lblue}\$${c_clear}):AFTER\\n${c_green}main${c_clear}" >expected &&
634
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmain" >expected &&
635
echo 2 >file &&
636
git stash &&
637
test_when_finished "git stash drop" &&
@@ -645,7 +645,7 @@ test_expect_success 'prompt - bash color pc mode - stash status indicator' '
645
'
646
647
test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
648
- printf "BEFORE: (\${__git_ps1_branch_name} ${c_red}%%${c_clear}):AFTER\\n${c_green}main${c_clear}" >expected &&
648
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmain" >expected &&
649
(
650
GIT_PS1_SHOWUNTRACKEDFILES=y &&
651
GIT_PS1_SHOWCOLORHINTS=y &&