[PATCH 13/19] bash prompt: check whether inside the worktree only when necessary

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Besides the current branch name or detached head info __git_ps1() can
also display status indicators in the prompt for changes in the work
tree, stashes, untracked files, and divergence from upstream.  These
are only displayed when enabled by specific environment variables and
when we are in a work tree.  The latter condition is checked by the
'$(git rev-parse --is-inside-work-tree)' command substitution,
impossing the overhead of fork()ing a subshell and fork()+exec()ing a
git process.

However, the check for the work tree preceeds the check of the
environment variables enabling status indicators, so it's evaluated
even when all these indicators are disabled.

Check upfront whether any of these indicators are enabled, i.e. a
corresponding environment variable is set, to spare the unnecessary
fork()+exec() overhead when all of them are disabled.

(Ideally we could check whether we are in a work tree using only bash
builtins, like we did in the previous commit for .git directory, but
the path of the work tree can be specified by the 'core.worktree'
config variable, and running 'git config' to get its value would be
just as expensive.)

Signed-off-by: SZEDER Gábor <szeder@xxxxxxxxxx>
---
 contrib/completion/git-completion.bash | 42 +++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 72f7d0ed..64b96f13 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -333,29 +333,35 @@ __git_ps1 ()
 		else
 			b="GIT_DIR!"
 		fi
-	elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
-		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
-			if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
-				git diff --no-ext-diff --quiet --exit-code || w="*"
-				if git rev-parse --quiet --verify HEAD >/dev/null; then
-					git diff-index --cached --quiet HEAD -- || i="+"
-				else
-					i="#"
+	elif [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" \
+			-o -n "${GIT_PS1_SHOWSTASHSTATE-}" \
+			-o -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" \
+			-n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+		if [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+				if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
+					git diff --no-ext-diff --quiet --exit-code || w="*"
+					if git rev-parse --quiet --verify HEAD >/dev/null; then
+						git diff-index --cached --quiet HEAD -- || i="+"
+					else
+						i="#"
+					fi
 				fi
 			fi
-		fi
-		if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
-			git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
-		fi
 
-		if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
-			if [ -n "$(git ls-files --others --exclude-standard)" ]; then
-				u="%"
+			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
+			        git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 			fi
-		fi
 
-		if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
-			__git_ps1_show_upstream
+			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
+				if [ -n "$(git ls-files --others --exclude-standard)" ]; then
+					u="%"
+				fi
+			fi
+
+			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+				__git_ps1_show_upstream
+			fi
 		fi
 	fi
 
-- 
1.7.10.1.541.gb1be298

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]