]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge: only apply autostash when appropriate
authorElijah Newren <newren@gmail.com>
Tue, 23 Aug 2022 02:42:19 +0000 (02:42 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Aug 2022 16:08:32 +0000 (09:08 -0700)
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 <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/merge.c
t/t7600-merge.sh

index f178f5a3ee18c71d2acda9455b020f9c03da9509..bca91b8a6b3fa545da8bb09feba2cff94b4d2f3c 100644 (file)
@@ -492,7 +492,8 @@ static void finish(struct commit *head_commit,
        /* Run a post-merge hook */
        run_hooks_l("post-merge", squash ? "1" : "0", NULL);
 
-       apply_autostash(git_path_merge_autostash(the_repository));
+       if (new_head)
+               apply_autostash(git_path_merge_autostash(the_repository));
        strbuf_release(&reflog_message);
 }
 
@@ -1754,6 +1755,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                        "stopped before committing as requested\n"));
        else
                ret = suggest_conflicts();
+       if (autostash)
+               printf(_("When finished, apply stashed changes with `git stash pop`\n"));
 
 done:
        if (!automerge_was_ok) {
index f0f6fda150bc29695dc8b56cb3c204de251e6e1d..7c3f6ed99431839abc42533d520cd342f25dafd1 100755 (executable)
@@ -255,6 +255,15 @@ test_expect_success 'merge --squash c3 with c7' '
        test_cmp expect actual
 '
 
+test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
+       git reset --hard c3 &&
+       >unrelated &&
+       git add unrelated &&
+       test_must_fail git merge --squash c7 --autostash >out 2>err &&
+       ! grep "Applying autostash resulted in conflicts." err &&
+       grep "When finished, apply stashed changes with \`git stash pop\`" out
+'
+
 test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
        git config commit.cleanup scissors &&
        git reset --hard c3 &&