git-prompt: make upstream state indicator location consistent

Make upstream state indicator location more consistent with similar
state indicators (e.g. sparse). Group the short upstream state indicator
(`=`, `<`, `>`, or `<>`) with other short state indicators immediately
after the branch name. Previously short and long upstream state
indicators appeared after all other state indicators.

Use a separator (`SP` or `GIT_PS1_STATESEPARATOR`) between branch name
and short upstream state indicator. Previously the short upstream state
indicator would sometimes appear directly adjacent to the branch name
instead of being separated.

For comparison, `__git_ps1` examples without upstream state indicator:
(main)
(main %)
(main *%)
(main|SPARSE)
(main %|SPARSE)
(main *%|SPARSE)
(main|SPARSE|REBASE 1/2)
(main %|SPARSE|REBASE 1/2)

Note that if there are short state indicators, they appear together
after the branch name and separated from it by `SP` or
`GIT_PS1_STATESEPARATOR`.

Before/after examples with short upstream state indicator:
| Before           | After            |
| ---------------- | ---------------- |
| (main=)          | (main =)         |
| (main|SPARSE=)   | (main =|SPARSE)  |
| (main %|SPARSE=) | (main %=|SPARSE) |

Signed-off-by: Justin Donnelly <justinrdonnelly@gmail.com>
Reviewed-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Justin Donnelly 2022-02-27 19:57:10 +00:00 committed by Junio C Hamano
parent 4d9dc2c57a
commit 0ec7c23cdc

View file

@ -109,7 +109,7 @@
__git_printf_supports_v= __git_printf_supports_v=
printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1 printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
# stores the divergence from upstream in $p # stores the divergence from upstream in $p (for short status) or $upstream (for verbose status)
# used by GIT_PS1_SHOWUPSTREAM # used by GIT_PS1_SHOWUPSTREAM
__git_ps1_show_upstream () __git_ps1_show_upstream ()
{ {
@ -214,26 +214,26 @@ __git_ps1_show_upstream ()
*) # diverged from upstream *) # diverged from upstream
p="<>" ;; p="<>" ;;
esac esac
else else # verbose, set upstream instead of p
case "$count" in case "$count" in
"") # no upstream "") # no upstream
p="" ;; upstream="" ;;
"0 0") # equal to upstream "0 0") # equal to upstream
p=" u=" ;; upstream=" u=" ;;
"0 "*) # ahead of upstream "0 "*) # ahead of upstream
p=" u+${count#0 }" ;; upstream=" u+${count#0 }" ;;
*" 0") # behind upstream *" 0") # behind upstream
p=" u-${count% 0}" ;; upstream=" u-${count% 0}" ;;
*) # diverged from upstream *) # diverged from upstream
p=" u+${count#* }-${count% *}" ;; upstream=" u+${count#* }-${count% *}" ;;
esac esac
if [[ -n "$count" && -n "$name" ]]; then if [[ -n "$count" && -n "$name" ]]; then
__git_ps1_upstream_name=$(git rev-parse \ __git_ps1_upstream_name=$(git rev-parse \
--abbrev-ref "$upstream_type" 2>/dev/null) --abbrev-ref "$upstream_type" 2>/dev/null)
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
p="$p \${__git_ps1_upstream_name}" upstream="$upstream \${__git_ps1_upstream_name}"
else else
p="$p ${__git_ps1_upstream_name}" upstream="$upstream ${__git_ps1_upstream_name}"
# not needed anymore; keep user's # not needed anymore; keep user's
# environment clean # environment clean
unset __git_ps1_upstream_name unset __git_ps1_upstream_name
@ -512,7 +512,8 @@ __git_ps1 ()
local u="" local u=""
local h="" local h=""
local c="" local c=""
local p="" local p="" # short version of upstream state indicator
local upstream="" # verbose version of upstream state indicator
if [ "true" = "$inside_gitdir" ]; then if [ "true" = "$inside_gitdir" ]; then
if [ "true" = "$bare_repo" ]; then if [ "true" = "$bare_repo" ]; then
@ -568,8 +569,8 @@ __git_ps1 ()
b="\${__git_ps1_branch_name}" b="\${__git_ps1_branch_name}"
fi fi
local f="$h$w$i$s$u" local f="$h$w$i$s$u$p"
local gitstring="$c$b${f:+$z$f}${sparse}$r$p" local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"
if [ $pcmode = yes ]; then if [ $pcmode = yes ]; then
if [ "${__git_printf_supports_v-}" != yes ]; then if [ "${__git_printf_supports_v-}" != yes ]; then