Skip to content

Commit e0e2a9c

Browse files
peffgitster
authored andcommitted
stash: drop dirty worktree check on apply
Before we apply a stash, we make sure there are no changes in the worktree that are not in the index. This check dates back to the original git-stash.sh, and is presumably intended to prevent changes in the working tree from being accidentally lost during the merge. However, this check has two problems: 1. It is overly restrictive. If my stash changes only file "foo", but "bar" is dirty in the working tree, it will prevent us from applying the stash. 2. It is redundant. We don't touch the working tree at all until we actually call merge-recursive. But it has its own (much more accurate) checks to avoid losing working tree data, and will abort the merge with a nicer message telling us which paths were problems. So we can simply drop the check entirely. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa38cfc commit e0e2a9c

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

git-stash.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ apply_stash () {
344344

345345
assert_stash_like "$@"
346346

347-
git update-index -q --refresh &&
348-
git diff-files --quiet --ignore-submodules ||
349-
die 'Cannot apply to a dirty working tree, please stage your changes'
347+
git update-index -q --refresh || die 'unable to refresh index'
350348

351349
# current index state
352350
c_tree=$(git write-tree) ||

t/t3903-stash.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,26 @@ test_expect_success 'parents of stash' '
3737
test_cmp output expect
3838
'
3939

40-
test_expect_success 'apply needs clean working directory' '
41-
echo 4 > other-file &&
40+
test_expect_success 'apply does not need clean working directory' '
41+
echo 4 >other-file &&
4242
git add other-file &&
43-
echo 5 > other-file &&
44-
test_must_fail git stash apply
43+
echo 5 >other-file &&
44+
git stash apply &&
45+
echo 3 >expect &&
46+
test_cmp expect file
47+
'
48+
49+
test_expect_success 'apply does not clobber working directory changes' '
50+
git reset --hard &&
51+
echo 4 >file &&
52+
test_must_fail git stash apply &&
53+
echo 4 >expect &&
54+
test_cmp expect file
4555
'
4656

4757
test_expect_success 'apply stashed changes' '
58+
git reset --hard &&
59+
echo 5 >other-file &&
4860
git add other-file &&
4961
test_tick &&
5062
git commit -m other-file &&

0 commit comments

Comments
 (0)