]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge: apply autostash if fast-forward fails
authorPhilippe Blain <levraiphilippeblain@gmail.com>
Fri, 23 Jul 2021 12:14:29 +0000 (12:14 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 23 Jul 2021 22:45:38 +0000 (15:45 -0700)
Since 'git merge' learned '--autostash' in a03b55530a (merge: teach
--autostash option, 2020-04-07), 'cmd_merge', in the fast-forward case,
calls 'create_autostash' before calling 'checkout_fast_forward' if
'--autostash' is given.

However, if 'checkout_fast_forward' fails, the autostash is not applied
to the working tree, nor saved in the stash list, since the code simply
calls 'goto done'.

Be more helpful to the user by applying the autostash in that case.

An easy way to test a failing fast-forward is when we are merging a
branch that has a tracked file that conflicts with an untracked file in
the working tree.

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 74797b6c7a63aa896603e0670a375c5ae06ff0a3..788a6b0cd553343837a453042c0802a99809ab0e 100644 (file)
@@ -1560,6 +1560,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                                          &head_commit->object.oid,
                                          &commit->object.oid,
                                          overwrite_ignore)) {
+                       apply_autostash(git_path_merge_autostash(the_repository));
                        ret = 1;
                        goto done;
                }
index 1cbc9715a81bdc9cf13bbe6d1341388468d0d047..216113d3537de8e2d1500fac592c4339d3979135 100755 (executable)
@@ -122,6 +122,8 @@ test_expect_success 'setup' '
        c0=$(git rev-parse HEAD) &&
        cp file.1 file &&
        git add file &&
+       cp file.1 other &&
+       git add other &&
        test_tick &&
        git commit -m "commit 1" &&
        git tag c1 &&
@@ -711,6 +713,15 @@ test_expect_success 'fast-forward merge with --autostash' '
        test_cmp result.1-5 file
 '
 
+test_expect_success 'failed fast-forward merge with --autostash' '
+       git reset --hard c0 &&
+       git merge-file file file.orig file.5 &&
+       cp file.5 other &&
+       test_must_fail git merge --autostash c1 2>err &&
+       test_i18ngrep "Applied autostash." err &&
+       test_cmp file.5 file
+'
+
 test_expect_success 'octopus merge with --autostash' '
        git reset --hard c1 &&
        git merge-file file file.orig file.3 &&