git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] contrib/git-prompt.sh: handle missing 'printf -v' more gracefully

From
BCBrandon Casey <bcasey@nvidia.com>
Date
Aug 22, 2013, 01:39 UTC
Message-ID
<1377135543-19053-1-git-send-email-bcasey@nvidia.com>
In-Reply-To
<1377134242-15012-1-git-send-email-bcasey@nvidia.com>
From: Brandon Casey <drafnel@gmail.com>

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>
---
On 8/21/2013 6:27 PM, Junio C Hamano wrote:> Brandon Casey <drafnel@gmail.com> writes:
Show 5 quoted lines
>
>> Why is "${var-}" necessary?  Wouldn't that be equivalent to "${var}"
>> or "$var"?
>
> set -u

Ah. Thanks. Updated. Also minor tweak to use [ ] instead of test ... to conform with the rest of the script.

-Brandon
 contrib/completion/git-prompt.sh | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index a81ef5a..ca7fb35 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,10 +437,10 @@ __git_ps1 ()
 	local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
 
 	if [ $pcmode = yes ]; then
-		if [[ -n ${ZSH_VERSION-} ]]; then
-			gitstring=$(printf -- "$printf_format" "$gitstring")
-		else
+		if [ "${__git_printf_supports_v-}" = yes ]; then
 			printf -v gitstring -- "$printf_format" "$gitstring"
+		else
+			gitstring=$(printf -- "$printf_format" "$gitstring")
 		fi
 		PS1="$ps1pc_start$gitstring$ps1pc_end"
 	else
-- 
1.8.4.rc0.2.g6cf5c31


-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------
Previous: Brandon CaseyNext: Junio C Hamano
Message 9 of 10 in “git-completion.bash: use correct Bash/Zsh array length syntax”
  1. 1/3 git-completion.bash: use correct Bash/Zsh array length syntaxBrandon Casey, Aug 21, 2013
  2. 2/3 t9902-completion.sh: old Bash still does not support array+=('') notationBrandon Casey, Aug 21, 2013
  3. 3/3 Revert "bash prompt: avoid command substitution when finalizing gitstring"Brandon Casey, Aug 21, 2013
  4. Junio C HamanoAug 21, 2013
  5. Brandon CaseyAug 21, 2013
  6. Junio C HamanoAug 22, 2013
  7. Brandon CaseyAug 22, 2013
  8. contrib/git-prompt.sh: handle missing 'printf -v' more gracefullyBrandon Casey, Aug 22, 2013
  9. contrib/git-prompt.sh: handle missing 'printf -v' more gracefullyBrandon Casey, Aug 22, 2013
  10. Junio C HamanoAug 22, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.