Skip to content

Commit e082631

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

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17091709
else
17101710
fprintf(stderr, _("Merge with strategy %s failed.\n"),
17111711
use_strategies[0]->name);
1712+
apply_autostash(git_path_merge_autostash(the_repository));
17121713
ret = 2;
17131714
goto done;
17141715
} else if (best_strategy == wt_strategy)

t/t7600-merge.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,14 @@ test_expect_success 'octopus merge with --autostash' '
732732
test_cmp result.1-3-5-9 file
733733
'
734734

735+
test_expect_success 'failed merge (exit 2) with --autostash' '
736+
git reset --hard c1 &&
737+
git merge-file file file.orig file.5 &&
738+
test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
739+
test_i18ngrep "Applied autostash." err &&
740+
test_cmp result.1-5 file
741+
'
742+
735743
test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
736744
git reset --hard c3 &&
737745
cp file.1 file &&

0 commit comments

Comments
 (0)