Skip to content

Commit a44aa69

Browse files
drafnelgitster
authored andcommitted
contrib/git-prompt.sh: handle missing 'printf -v' more gracefully
Old Bash (3.0) which is distributed with RHEL 4.X and other ancient platforms that are still in wide use, do not have a printf that supports -v. Neither does Zsh (which is already handled in the code). As suggested by Junio, let's test whether printf supports the -v option and store the result. Then later, we can use it to determine whether 'printf -v' can be used, or whether printf must be called in a subshell. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ef0970 commit a44aa69

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

contrib/completion/git-prompt.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
# the colored output of "git status -sb" and are available only when
8585
# using __git_ps1 for PROMPT_COMMAND or precmd.
8686

87+
# check whether printf supports -v
88+
__git_printf_supports_v=
89+
printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
90+
8791
# stores the divergence from upstream in $p
8892
# used by GIT_PS1_SHOWUPSTREAM
8993
__git_ps1_show_upstream ()
@@ -433,7 +437,7 @@ __git_ps1 ()
433437
local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
434438

435439
if [ $pcmode = yes ]; then
436-
if [[ -n ${ZSH_VERSION-} ]]; then
440+
if [ "${__git_printf_supports_v-}" != yes ]; then
437441
gitstring=$(printf -- "$printf_format" "$gitstring")
438442
else
439443
printf -v gitstring -- "$printf_format" "$gitstring"

0 commit comments

Comments
 (0)