Skip to content

Commit 288c67c

Browse files
peffgitster
authored andcommitted
stash: default listing to working-tree diff
When you list stashes, you can provide arbitrary git-log options to change the display. However, adding just "-p" does nothing, because each stash is actually a merge commit. This implementation detail is easy to forget, leading to confused users who think "-p" is not working. We can make this easier by defaulting to "--first-parent -m", which will show the diff against the working tree. This omits the index portion of the stash entirely, but it's simple and it matches what "git stash show" provides. People who are more clueful about stash's true form can use "--cc" to override the "-m", and the "--first-parent" will then do nothing. For diffs, it only affects non-combined diffs, so "--cc" overrides it. And for the traversal, we are walking the linear reflog anyway, so we do not even care about the parents. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d31f3ad commit 288c67c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

git-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ have_stash () {
296296

297297
list_stash () {
298298
have_stash || return 0
299-
git log --format="%gd: %gs" -g "$@" $ref_stash --
299+
git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
300300
}
301301

302302
show_stash () {

t/t3903-stash.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,46 @@ test_expect_success 'handle stash specification with spaces' '
685685
grep pig file
686686
'
687687

688+
test_expect_success 'setup stash with index and worktree changes' '
689+
git stash clear &&
690+
git reset --hard &&
691+
echo index >file &&
692+
git add file &&
693+
echo working >file &&
694+
git stash
695+
'
696+
697+
test_expect_success 'stash list implies --first-parent -m' '
698+
cat >expect <<-\EOF &&
699+
stash@{0}: WIP on master: b27a2bc subdir
700+
701+
diff --git a/file b/file
702+
index 257cc56..d26b33d 100644
703+
--- a/file
704+
+++ b/file
705+
@@ -1 +1 @@
706+
-foo
707+
+working
708+
EOF
709+
git stash list -p >actual &&
710+
test_cmp expect actual
711+
'
712+
713+
test_expect_success 'stash list --cc shows combined diff' '
714+
cat >expect <<-\EOF &&
715+
stash@{0}: WIP on master: b27a2bc subdir
716+
717+
diff --cc file
718+
index 257cc56,9015a7a..d26b33d
719+
--- a/file
720+
+++ b/file
721+
@@@ -1,1 -1,1 +1,1 @@@
722+
- foo
723+
-index
724+
++working
725+
EOF
726+
git stash list -p --cc >actual &&
727+
test_cmp expect actual
728+
'
729+
688730
test_done

0 commit comments

Comments
 (0)