merge: apply autostash if fast-forward fails

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>

Philippe Blain committed Jul 23, 2021 at 12:14 UTC 12510bd5da6187690ae957d46b41f59276b0dadc
2 files changed +12
builtin/merge.c
+1
@@ -1560,6 +1560,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1560 &head_commit->object.oid,
1561 &commit->object.oid,
1562 overwrite_ignore)) {
1563 + apply_autostash(git_path_merge_autostash(the_repository));
1564 ret = 1;
1565 goto done;
1566 }
t/t7600-merge.sh
+11
@@ -122,6 +122,8 @@ test_expect_success 'setup' '
122 c0=$(git rev-parse HEAD) &&
123 cp file.1 file &&
124 git add file &&
125 + cp file.1 other &&
126 + git add other &&
127 test_tick &&
128 git commit -m "commit 1" &&
129 git tag c1 &&
@@ -711,6 +713,15 @@ test_expect_success 'fast-forward merge with --autostash' '
713 test_cmp result.1-5 file
714 '
715
716 +test_expect_success 'failed fast-forward merge with --autostash' '
717 + git reset --hard c0 &&
718 + git merge-file file file.orig file.5 &&
719 + cp file.5 other &&
720 + test_must_fail git merge --autostash c1 2>err &&
721 + test_i18ngrep "Applied autostash." err &&
722 + test_cmp file.5 file
723 +'
724 +
725 test_expect_success 'octopus merge with --autostash' '
726 git reset --hard c1 &&
727 git merge-file file file.orig file.3 &&