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>

Justin Donnelly committed Feb 27, 2022 at 19:57 UTC 0ec7c23cdc6bde5af3039c59e21507adf7579a99
1 file changed +13 -12
contrib/completion/git-prompt.sh
+13 -12
@@ -109,7 +109,7 @@
109 __git_printf_supports_v=
110 printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
111
112 -# stores the divergence from upstream in $p
112 +# stores the divergence from upstream in $p (for short status) or $upstream (for verbose status)
113 # used by GIT_PS1_SHOWUPSTREAM
114 __git_ps1_show_upstream ()
115 {
@@ -214,26 +214,26 @@ __git_ps1_show_upstream ()
214 *) # diverged from upstream
215 p="<>" ;;
216 esac
217 - else
217 + else # verbose, set upstream instead of p
218 case "$count" in
219 "") # no upstream
220 - p="" ;;
220 + upstream="" ;;
221 "0 0") # equal to upstream
222 - p=" u=" ;;
222 + upstream=" u=" ;;
223 "0 "*) # ahead of upstream
224 - p=" u+${count#0 }" ;;
224 + upstream=" u+${count#0 }" ;;
225 *" 0") # behind upstream
226 - p=" u-${count% 0}" ;;
226 + upstream=" u-${count% 0}" ;;
227 *) # diverged from upstream
228 - p=" u+${count#* }-${count% *}" ;;
228 + upstream=" u+${count#* }-${count% *}" ;;
229 esac
230 if [[ -n "$count" && -n "$name" ]]; then
231 __git_ps1_upstream_name=$(git rev-parse \
232 --abbrev-ref "$upstream_type" 2>/dev/null)
233 if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
234 - p="$p \${__git_ps1_upstream_name}"
234 + upstream="$upstream \${__git_ps1_upstream_name}"
235 else
236 - p="$p ${__git_ps1_upstream_name}"
236 + upstream="$upstream ${__git_ps1_upstream_name}"
237 # not needed anymore; keep user's
238 # environment clean
239 unset __git_ps1_upstream_name
@@ -512,7 +512,8 @@ __git_ps1 ()
512 local u=""
513 local h=""
514 local c=""
515 - local p=""
515 + local p="" # short version of upstream state indicator
516 + local upstream="" # verbose version of upstream state indicator
517
518 if [ "true" = "$inside_gitdir" ]; then
519 if [ "true" = "$bare_repo" ]; then
@@ -568,8 +569,8 @@ __git_ps1 ()
569 b="\${__git_ps1_branch_name}"
570 fi
571
571 - local f="$h$w$i$s$u"
572 - local gitstring="$c$b${f:+$z$f}${sparse}$r$p"
572 + local f="$h$w$i$s$u$p"
573 + local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"
574
575 if [ $pcmode = yes ]; then
576 if [ "${__git_printf_supports_v-}" != yes ]; then