Skip to content

Commit d3a9295

Browse files
newrengitster
authored andcommitted
merge: only apply autostash when appropriate
If a merge failed and we are leaving conflicts in the working directory for the user to resolve, we should not attempt to apply any autostash. Further, if we fail to apply the autostash (because either the merge failed, or the user requested --no-commit), then we should instruct the user how to apply it later. Add a testcase verifying we have corrected this behavior. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd59c5b commit d3a9295

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

builtin/merge.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ static void finish(struct commit *head_commit,
492492
/* Run a post-merge hook */
493493
run_hooks_l("post-merge", squash ? "1" : "0", NULL);
494494

495-
apply_autostash(git_path_merge_autostash(the_repository));
495+
if (new_head)
496+
apply_autostash(git_path_merge_autostash(the_repository));
496497
strbuf_release(&reflog_message);
497498
}
498499

@@ -1754,6 +1755,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17541755
"stopped before committing as requested\n"));
17551756
else
17561757
ret = suggest_conflicts();
1758+
if (autostash)
1759+
printf(_("When finished, apply stashed changes with `git stash pop`\n"));
17571760

17581761
done:
17591762
if (!automerge_was_ok) {

t/t7600-merge.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ test_expect_success 'merge --squash c3 with c7' '
255255
test_cmp expect actual
256256
'
257257

258+
test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
259+
git reset --hard c3 &&
260+
>unrelated &&
261+
git add unrelated &&
262+
test_must_fail git merge --squash c7 --autostash >out 2>err &&
263+
! grep "Applying autostash resulted in conflicts." err &&
264+
grep "When finished, apply stashed changes with \`git stash pop\`" out
265+
'
266+
258267
test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
259268
git config commit.cleanup scissors &&
260269
git reset --hard c3 &&

0 commit comments

Comments
 (0)