Skip to content

Commit

Permalink
bash prompt: indicate dirty index even on orphan branches
Browse files Browse the repository at this point in the history
__git_ps1() doesn't indicate dirty index while on an orphan branch.

To check the dirtiness of the index, __git_ps1() runs 'git diff-index
--cached ... HEAD', which doesn't work on an orphan branch,
because HEAD doesn't point to a valid commit.

Run 'git diff ... --cached' instead, as it does the right thing both
on valid and invalid HEAD, i.e. compares the index to the existing
HEAD in the former case and to the empty tree in the latter.  This
fixes the two failing tests added in the first commit of this series.

The dirtiness of the worktree is already checked with 'git diff' and
is displayed correctly even on an orphan branch.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
Signed-off-by: Jeff King <peff@peff.net>
  • Loading branch information
SZEDER Gábor authored and Jeff King committed Nov 24, 2015
1 parent 0af9f7e commit c26f70c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions contrib/completion/git-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,8 @@ __git_ps1 ()
[ "$(git config --bool bash.showDirtyState)" != "false" ]
then
git diff --no-ext-diff --quiet || w="*"
if [ -n "$short_sha" ]; then
git diff-index --cached --quiet HEAD -- || i="+"
else
git diff --no-ext-diff --cached --quiet || i="+"
if [ -z "$short_sha" ] && [ -z "$i" ]; then
i="#"
fi
fi
Expand Down
4 changes: 2 additions & 2 deletions t/t9903-bash-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ test_expect_success 'prompt - dirty status indicator - orphan branch - clean' '
test_cmp expected "$actual"
'

test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index' '
test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index' '
printf " (orphan +)" >expected &&
test_when_finished "git checkout master" &&
git checkout --orphan orphan &&
Expand All @@ -296,7 +296,7 @@ test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty ind
test_cmp expected "$actual"
'

test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
printf " (orphan *+)" >expected &&
test_when_finished "git checkout master" &&
git checkout --orphan orphan &&
Expand Down

0 comments on commit c26f70c

Please sign in to comment.