Skip to content

Commit dd0b72c

Browse files
committed
bash prompt: use bash builtins to check stash state
When the environment variable $GIT_PS1_SHOWSTASHSTATE is set __git_ps1() checks the presence of stashes by running 'git rev-parse --verify refs/stash'. This command not only checks that the 'refs/stash' ref exists but also, well, verifies that it's a valid ref. However, we don't need to be that thorough for the bash prompt. We can omit that verification and only check whether 'refs/stash' exists or not. Since 'git pack-refs' never packs 'refs/stash', it's a matter of checking the existence of a ref file. Perform this check using only bash builtins to spare the overhead of fork()+exec()ing a git process. Also run 'git pack-refs --all' in the corresponding test to document that the prompt script depends on 'git pack-refs' not packing 'refs/stash' and to catch possible breakages should this behavior ever change. Signed-off-by: SZEDER Gábor <[email protected]>
1 parent 0f37c12 commit dd0b72c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

contrib/completion/git-prompt.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,9 @@ __git_ps1 ()
435435
i="#"
436436
fi
437437
fi
438-
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
439-
git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
438+
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] &&
439+
[ -r "$g/refs/stash" ]; then
440+
s="$"
440441
fi
441442

442443
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&

t/t9903-bash-prompt.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ test_expect_success 'prompt - stash status indicator - stash' '
328328
echo 2 >file &&
329329
git stash &&
330330
test_when_finished "git stash drop" &&
331+
git pack-refs --all &&
331332
(
332333
GIT_PS1_SHOWSTASHSTATE=y &&
333334
__git_ps1 >"$actual"

0 commit comments

Comments
 (0)