Skip to content

Commit cb9ec8e

Browse files
committed
Merge branch 'jk/stash-require-clean-index' into maint
"git stash pop/apply" forgot to make sure that not just the working tree is clean but also the index is clean. The latter is important as a stash application can conflict and the index will be used for conflict resolution. * jk/stash-require-clean-index: stash: require a clean index to apply t3903: avoid applying onto dirty index t3903: stop hard-coding commit sha1s
2 parents af6d7a6 + ed178ef commit cb9ec8e

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

git-stash.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ apply_stash () {
442442
assert_stash_like "$@"
443443

444444
git update-index -q --refresh || die "$(gettext "unable to refresh index")"
445+
git diff-index --cached --quiet --ignore-submodules HEAD -- ||
446+
die "$(gettext "Cannot apply stash: Your index contains uncommitted changes.")"
445447

446448
# current index state
447449
c_tree=$(git write-tree) ||

t/t3903-stash.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ test_description='Test git stash'
1010
test_expect_success 'stash some dirty working directory' '
1111
echo 1 > file &&
1212
git add file &&
13+
echo unrelated >other-file &&
14+
git add other-file &&
1315
test_tick &&
1416
git commit -m initial &&
1517
echo 2 > file &&
@@ -43,10 +45,15 @@ test_expect_success 'applying bogus stash does nothing' '
4345
test_cmp expect file
4446
'
4547

48+
test_expect_success 'apply requires a clean index' '
49+
test_when_finished "git reset --hard" &&
50+
echo changed >other-file &&
51+
git add other-file &&
52+
test_must_fail git stash apply
53+
'
54+
4655
test_expect_success 'apply does not need clean working directory' '
4756
echo 4 >other-file &&
48-
git add other-file &&
49-
echo 5 >other-file &&
5057
git stash apply &&
5158
echo 3 >expect &&
5259
test_cmp expect file
@@ -695,8 +702,8 @@ test_expect_success 'setup stash with index and worktree changes' '
695702
'
696703

697704
test_expect_success 'stash list implies --first-parent -m' '
698-
cat >expect <<-\EOF &&
699-
stash@{0}: WIP on master: b27a2bc subdir
705+
cat >expect <<-EOF &&
706+
stash@{0}
700707
701708
diff --git a/file b/file
702709
index 257cc56..d26b33d 100644
@@ -706,13 +713,13 @@ test_expect_success 'stash list implies --first-parent -m' '
706713
-foo
707714
+working
708715
EOF
709-
git stash list -p >actual &&
716+
git stash list --format=%gd -p >actual &&
710717
test_cmp expect actual
711718
'
712719

713720
test_expect_success 'stash list --cc shows combined diff' '
714721
cat >expect <<-\EOF &&
715-
stash@{0}: WIP on master: b27a2bc subdir
722+
stash@{0}
716723
717724
diff --cc file
718725
index 257cc56,9015a7a..d26b33d
@@ -723,7 +730,7 @@ test_expect_success 'stash list --cc shows combined diff' '
723730
-index
724731
++working
725732
EOF
726-
git stash list -p --cc >actual &&
733+
git stash list --format=%gd -p --cc >actual &&
727734
test_cmp expect actual
728735
'
729736

0 commit comments

Comments
 (0)