Skip to content

Commit bfc8b41

Browse files
committed
Merge branch 'pb/merge-autostash-more' into next
The local changes stashed by "git merge --autostash" were lost when the merge failed in certain ways, which has been corrected. * pb/merge-autostash-more: merge: apply autostash if merge strategy fails merge: apply autostash if fast-forward fails Documentation: define 'MERGE_AUTOSTASH' merge: add missing word "strategy" to a message
2 parents 89cfdc4 + e082631 commit bfc8b41

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Documentation/merge-options.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ endif::git-pull[]
154154
--autostash::
155155
--no-autostash::
156156
Automatically create a temporary stash entry before the operation
157-
begins, and apply it after the operation ends. This means
157+
begins, record it in the special ref `MERGE_AUTOSTASH`
158+
and apply it after the operation ends. This means
158159
that you can run the operation on a dirty worktree. However, use
159160
with care: the final stash application after a successful
160161
merge might result in non-trivial conflicts.

builtin/merge.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15611561
&head_commit->object.oid,
15621562
&commit->object.oid,
15631563
overwrite_ignore)) {
1564+
apply_autostash(git_path_merge_autostash(the_repository));
15641565
ret = 1;
15651566
goto done;
15661567
}
@@ -1709,14 +1710,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17091710
else
17101711
fprintf(stderr, _("Merge with strategy %s failed.\n"),
17111712
use_strategies[0]->name);
1713+
apply_autostash(git_path_merge_autostash(the_repository));
17121714
ret = 2;
17131715
goto done;
17141716
} else if (best_strategy == wt_strategy)
17151717
; /* We already have its result in the working tree. */
17161718
else {
17171719
printf(_("Rewinding the tree to pristine...\n"));
17181720
restore_state(&head_commit->object.oid, &stash);
1719-
printf(_("Using the %s to prepare resolving by hand.\n"),
1721+
printf(_("Using the %s strategy to prepare resolving by hand.\n"),
17201722
best_strategy);
17211723
try_merge_strategy(best_strategy, common, remoteheads,
17221724
head_commit);

t/t7600-merge.sh

Lines changed: 19 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 &&
@@ -721,6 +732,14 @@ test_expect_success 'octopus merge with --autostash' '
721732
test_cmp result.1-3-5-9 file
722733
'
723734

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+
724743
test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
725744
git reset --hard c3 &&
726745
cp file.1 file &&

0 commit comments

Comments
 (0)