Thread (18 messages) 18 messages, 2 authors, 2016-06-15

[PATCHv3 15/16] bash prompt: avoid command substitution when finalizing gitstring

flat view

From: SZEDER Gábor <hidden>
Date: 2016-06-15 22:57:54
Subsystem: the rest · Maintainer: Linus Torvalds

Before setting $PS1, __git_ps1() uses a command substitution to
redirect the output from a printf into a variable.  Spare the overhead
of fork()ing a subshell by using 'printf -v <var>' to directly assign
the output to that variable.

zsh's printf doesn't support the '-v <var>' option, so stick with the
command substitution when under zsh.

Signed-off-by: SZEDER Gábor <redacted>
---
 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 5ea6a68b..7152ae49 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -461,7 +461,11 @@ __git_ps1 ()
 		else
 			gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
 		fi
-		gitstring=$(printf -- "$printf_format" "$gitstring")
+		if [[ -n ${ZSH_VERSION-} ]]; then
+			gitstring=$(printf -- "$printf_format" "$gitstring")
+		else
+			printf -v gitstring -- "$printf_format" "$gitstring"
+		fi
 		PS1="$ps1pc_start$gitstring$ps1pc_end"
 	else
 		# NO color option unless in PROMPT_COMMAND mode
-- 
1.8.3.1.599.g4459181
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help