From a44aa6930c9f3ce914f135c00a62878f9027f583 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Wed, 21 Aug 2013 18:39:03 -0700 Subject: 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 Signed-off-by: Junio C Hamano --- contrib/completion/git-prompt.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index a81ef5a482..d6c61b2bde 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -84,6 +84,10 @@ # the colored output of "git status -sb" and are available only when # using __git_ps1 for PROMPT_COMMAND or precmd. +# check whether printf supports -v +__git_printf_supports_v= +printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1 + # stores the divergence from upstream in $p # used by GIT_PS1_SHOWUPSTREAM __git_ps1_show_upstream () @@ -433,7 +437,7 @@ __git_ps1 () local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p" if [ $pcmode = yes ]; then - if [[ -n ${ZSH_VERSION-} ]]; then + if [ "${__git_printf_supports_v-}" != yes ]; then gitstring=$(printf -- "$printf_format" "$gitstring") else printf -v gitstring -- "$printf_format" "$gitstring" -- cgit v1.2.1