Skip to content

Commit c26f70c

Browse files
szederpeff
authored andcommitted
bash prompt: indicate dirty index even on orphan branches
__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 <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent 0af9f7e commit c26f70c

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

contrib/completion/git-prompt.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,8 @@ __git_ps1 ()
477477
[ "$(git config --bool bash.showDirtyState)" != "false" ]
478478
then
479479
git diff --no-ext-diff --quiet || w="*"
480-
if [ -n "$short_sha" ]; then
481-
git diff-index --cached --quiet HEAD -- || i="+"
482-
else
480+
git diff --no-ext-diff --cached --quiet || i="+"
481+
if [ -z "$short_sha" ] && [ -z "$i" ]; then
483482
i="#"
484483
fi
485484
fi

t/t9903-bash-prompt.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ test_expect_success 'prompt - dirty status indicator - orphan branch - clean' '
285285
test_cmp expected "$actual"
286286
'
287287

288-
test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index' '
288+
test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index' '
289289
printf " (orphan +)" >expected &&
290290
test_when_finished "git checkout master" &&
291291
git checkout --orphan orphan &&
@@ -296,7 +296,7 @@ test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty ind
296296
test_cmp expected "$actual"
297297
'
298298

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

0 commit comments

Comments
 (0)