Skip to content

Commit 556895d

Browse files
jarmargitster
authored andcommitted
stash: avoid recursive hard reset on submodules
git stash push does not recursively stash submodules, but if submodule.recurse is set, it may recursively reset --hard them. Having only the destructive action recurse is likely to be surprising behaviour, and unlikely to be desirable, so the easiest fix should be to ensure that the call to git reset --hard never recurses into submodules. This matches the behavior of check_changes_tracked_files, which ignores submodules. Signed-off-by: Jakob Jarmar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5fa0f52 commit 556895d

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

builtin/stash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
13831383
struct child_process cp = CHILD_PROCESS_INIT;
13841384
cp.git_cmd = 1;
13851385
argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1386-
NULL);
1386+
"--no-recurse-submodules", NULL);
13871387
if (run_command(&cp)) {
13881388
ret = -1;
13891389
goto done;

git-legacy-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ push_stash () {
370370
git diff-index -p --cached --binary HEAD -- "$@" |
371371
git apply --index -R
372372
else
373-
git reset --hard -q
373+
git reset --hard -q --no-recurse-submodules
374374
fi
375375

376376
if test "$keep_index" = "t" && test -n "$i_tree"

t/t3906-stash-submodule.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
test_description='stash apply can handle submodules'
3+
test_description='stash can handle submodules'
44

55
. ./test-lib.sh
66
. "$TEST_DIRECTORY"/lib-submodule-update.sh
@@ -21,4 +21,44 @@ KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
2121
KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
2222
test_submodule_switch "git_stash"
2323

24+
setup_basic () {
25+
test_when_finished "rm -rf main sub" &&
26+
git init sub &&
27+
(
28+
cd sub &&
29+
test_commit sub_file
30+
) &&
31+
git init main &&
32+
(
33+
cd main &&
34+
git submodule add ../sub &&
35+
test_commit main_file
36+
)
37+
}
38+
39+
test_expect_success 'stash push with submodule.recurse=true preserves dirty submodule worktree' '
40+
setup_basic &&
41+
(
42+
cd main &&
43+
git config submodule.recurse true &&
44+
echo "x" >main_file.t &&
45+
echo "y" >sub/sub_file.t &&
46+
git stash push &&
47+
test_must_fail git -C sub diff --quiet
48+
)
49+
'
50+
51+
test_expect_success 'stash push and pop with submodule.recurse=true preserves dirty submodule worktree' '
52+
setup_basic &&
53+
(
54+
cd main &&
55+
git config submodule.recurse true &&
56+
echo "x" >main_file.t &&
57+
echo "y" >sub/sub_file.t &&
58+
git stash push &&
59+
git stash pop &&
60+
test_must_fail git -C sub diff --quiet
61+
)
62+
'
63+
2464
test_done

0 commit comments

Comments
 (0)