[PATCH 16/19] bash prompt: display stash and upstream state even inside the repository

[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 some status indicators in the prompt.  The first such
indicator was for changes in the work tree in 738a94a9 (bash: offer to
show (un)staged changes, 2009-02-03), and was only checked/displayed
when inside the work tree.  Later other indicators were added in
2414b45c (Show presence of stashed changes in bash prompt.,
2009-06-02), 397f7c63 (Show the presence of untracked files in the
bash prompt., 2009-07-22), and 6d158cba (bash completion: Support
"divergence from upstream" messages in __git_ps1, 2010-06-17).  All of
these just followed suit and were checked only when inside the work
tree, i.e. after checking the results of the '$(git rev-parse
--is-inside-work-tree)' command substitution, imposing the overhead of
fork()ing a subshell and fork()+exec()ing a git process.

However, the presence of stashes and the divergence from upstream is
not a property of the work tree but a property of the repository, and
the implementation of their indicators doesn't actually require a work
tree.  Therefore, we can display these two indicators even inside the
repository.  Not that it's very useful to see the stash status while
poking around deep inside the .git directory, but this way users
enabling only the stash indicator won't pay the additional performance
penalty of the check for the work tree.

Signed-off-by: SZEDER Gábor <szeder@xxxxxxxxxx>
---
 contrib/completion/git-completion.bash | 20 +++++++++-----------
 t/t9903-bash-prompt.sh                 |  4 ++--
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 2346962d..64207e3c 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -334,9 +334,7 @@ __git_ps1 ()
 			b="GIT_DIR!"
 		fi
 	elif [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" \
-			-o -n "${GIT_PS1_SHOWSTASHSTATE-}" \
-			-o -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" \
-			-n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+			-o -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; 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
@@ -349,22 +347,22 @@ __git_ps1 ()
 				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="%"
 				fi
 			fi
-
-			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
-				__git_ps1_show_upstream
-			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_SHOWUPSTREAM-}" ]; then
+		__git_ps1_show_upstream
+	fi
+
 	local f="$w$i$s$u"
 	printf -- "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
 }
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index ffa22d39..a43d402a 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -542,8 +542,8 @@ test_expect_success 'prompt - stash status indicator - stash' '
 	test_cmp expected "$actual"
 '
 
-test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
-	printf " (GIT_DIR!)" > expected &&
+test_expect_success 'prompt - stash status indicator - stash while inside .git directory' '
+	printf " (GIT_DIR! $)" > expected &&
 	echo 2 >file &&
 	git stash &&
 	test_when_finished "git stash drop" &&
-- 
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]