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 <drafnel@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Casey committed
Aug 21, 2013 at 18:39 UTC
a44aa6930c9f3ce914f135c00a62878f9027f583
1 file changed
+5
-1
contrib/completion/git-prompt.sh
+5
-1
@@ -84,6 +84,10 @@
84
# the colored output of "git status -sb" and are available only when
85
# using __git_ps1 for PROMPT_COMMAND or precmd.
86
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
+
91
# stores the divergence from upstream in $p
92
# used by GIT_PS1_SHOWUPSTREAM
93
__git_ps1_show_upstream ()
@@ -433,7 +437,7 @@ __git_ps1 ()
437
local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
438
439
if [ $pcmode = yes ]; then
436
- if [[ -n ${ZSH_VERSION-} ]]; then
440
+ if [ "${__git_printf_supports_v-}" != yes ]; then
441
gitstring=$(printf -- "$printf_format" "$gitstring")
442
else
443
printf -v gitstring -- "$printf_format" "$gitstring"