]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge: apply autostash if merge strategy fails
authorPhilippe Blain <levraiphilippeblain@gmail.com>
Fri, 23 Jul 2021 12:14:30 +0000 (12:14 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 23 Jul 2021 22:45:40 +0000 (15:45 -0700)
Since 'git merge' learned '--autostash' in a03b55530a (merge: teach
--autostash option, 2020-04-07), 'cmd_merge', once it is determined that
we have to create a merge commit, calls 'create_autostash' if
'--autostash' is given.

As explained in a03b55530a, and made more abvious by the tests added in
that commit, the autostash is then applied if the merge succeeds, either
directly or by committing (after conflict resolution or if '--no-commit'
was given), or if the merge is aborted with 'git merge --abort'. In some
other cases, like the user calling 'git reset --merge' or 'git merge
--quit', the autostash is not applied, but saved in the stash list.

However, there exists a scenario that creates an autostash but does not
apply nor save it to the stash list: if the chosen merge strategy
completely fails to handle the merge, i.e. 'try_merge_strategy' returns
2.

Apply the autostash in that case also. An easy way to test that is to
try to merge more than two commits but explicitely ask for the 'recursive'
merge strategy.

Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/merge.c
t/t7600-merge.sh

index 788a6b0cd553343837a453042c0802a99809ab0e..d44c14a21a3b30a1187aa49c48bda0465a015572 100644 (file)
@@ -1709,6 +1709,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                else
                        fprintf(stderr, _("Merge with strategy %s failed.\n"),
                                use_strategies[0]->name);
+               apply_autostash(git_path_merge_autostash(the_repository));
                ret = 2;
                goto done;
        } else if (best_strategy == wt_strategy)
index 216113d3537de8e2d1500fac592c4339d3979135..2ef39d3088e80690781e43c671ab1de11beed55f 100755 (executable)
@@ -732,6 +732,14 @@ test_expect_success 'octopus merge with --autostash' '
        test_cmp result.1-3-5-9 file
 '
 
+test_expect_success 'failed merge (exit 2) with --autostash' '
+       git reset --hard c1 &&
+       git merge-file file file.orig file.5 &&
+       test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
+       test_i18ngrep "Applied autostash." err &&
+       test_cmp result.1-5 file
+'
+
 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
        git reset --hard c3 &&
        cp file.1 file &&