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
This commit is contained in:
Junio C Hamano 2022-06-21 10:07:50 -07:00
commit ddbc07872e
2 changed files with 18 additions and 16 deletions

View file

@ -245,7 +245,8 @@ __git_ps1_show_upstream ()
# Helper function that is meant to be called from __git_ps1. It # Helper function that is meant to be called from __git_ps1. It
# injects color codes into the appropriate gitstring variables used # injects color codes into the appropriate gitstring variables used
# to build a gitstring. # to build a gitstring. Colored variables are responsible for clearing
# their own color.
__git_ps1_colorize_gitstring () __git_ps1_colorize_gitstring ()
{ {
if [[ -n ${ZSH_VERSION-} ]]; then if [[ -n ${ZSH_VERSION-} ]]; then
@ -271,22 +272,23 @@ __git_ps1_colorize_gitstring ()
else else
branch_color="$bad_color" branch_color="$bad_color"
fi fi
c="$branch_color$c" if [ -n "$c" ]; then
c="$branch_color$c$c_clear"
fi
b="$branch_color$b$c_clear"
z="$c_clear$z" if [ -n "$w" ]; then
if [ "$w" = "*" ]; then w="$bad_color$w$c_clear"
w="$bad_color$w"
fi fi
if [ -n "$i" ]; then if [ -n "$i" ]; then
i="$ok_color$i" i="$ok_color$i$c_clear"
fi fi
if [ -n "$s" ]; then if [ -n "$s" ]; then
s="$flags_color$s" s="$flags_color$s$c_clear"
fi fi
if [ -n "$u" ]; then if [ -n "$u" ]; then
u="$bad_color$u" u="$bad_color$u$c_clear"
fi fi
r="$c_clear$r"
} }
# Helper function to read the first line of a file into a variable. # Helper function to read the first line of a file into a variable.
@ -556,6 +558,12 @@ __git_ps1 ()
local z="${GIT_PS1_STATESEPARATOR-" "}" local z="${GIT_PS1_STATESEPARATOR-" "}"
b=${b##refs/heads/}
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
__git_ps1_branch_name=$b
b="\${__git_ps1_branch_name}"
fi
# NO color option unless in PROMPT_COMMAND mode or it's Zsh # NO color option unless in PROMPT_COMMAND mode or it's Zsh
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
if [ $pcmode = yes ] || [ -n "${ZSH_VERSION-}" ]; then if [ $pcmode = yes ] || [ -n "${ZSH_VERSION-}" ]; then
@ -563,12 +571,6 @@ __git_ps1 ()
fi fi
fi fi
b=${b##refs/heads/}
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
__git_ps1_branch_name=$b
b="\${__git_ps1_branch_name}"
fi
local f="$h$w$i$s$u$p" local f="$h$w$i$s$u$p"
local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}" local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"

View file

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