Skip to content

Commit 12510bd

Browse files
phil-blaingitster
authored andcommitted
merge: apply autostash if fast-forward fails
Since 'git merge' learned '--autostash' in a03b555 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd441eb commit 12510bd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15601560
&head_commit->object.oid,
15611561
&commit->object.oid,
15621562
overwrite_ignore)) {
1563+
apply_autostash(git_path_merge_autostash(the_repository));
15631564
ret = 1;
15641565
goto done;
15651566
}

t/t7600-merge.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ test_expect_success 'setup' '
122122
c0=$(git rev-parse HEAD) &&
123123
cp file.1 file &&
124124
git add file &&
125+
cp file.1 other &&
126+
git add other &&
125127
test_tick &&
126128
git commit -m "commit 1" &&
127129
git tag c1 &&
@@ -711,6 +713,15 @@ test_expect_success 'fast-forward merge with --autostash' '
711713
test_cmp result.1-5 file
712714
'
713715

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+
714725
test_expect_success 'octopus merge with --autostash' '
715726
git reset --hard c1 &&
716727
git merge-file file file.orig file.3 &&

0 commit comments

Comments
 (0)