git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Philippe Blain via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Denton Liu <liu.denton@gmail.com>,
	Felipe Contreras <felipe.contreras@gmail.com>,
	Philippe Blain <levraiphilippeblain@gmail.com>,
	Philippe Blain <levraiphilippeblain@gmail.com>
Subject: [PATCH 4/4] merge: apply autostash if merge strategy fails
Date: Fri, 23 Jul 2021 12:14:30 +0000	[thread overview]
Message-ID: <e02b945ce8595a8d7b1b4ad9c382a1beeb040ed4.1627042470.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1002.git.1627042470.gitgitgadget@gmail.com>

From: Philippe Blain <levraiphilippeblain@gmail.com>

Since 'git merge' learned '--autostash' in a03b55530a (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 a03b55530a, 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 <levraiphilippeblain@gmail.com>
---
 builtin/merge.c  | 1 +
 t/t7600-merge.sh | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/builtin/merge.c b/builtin/merge.c
index 788a6b0cd55..d44c14a21a3 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1709,6 +1709,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		else
 			fprintf(stderr, _("Merge with strategy %s failed.\n"),
 				use_strategies[0]->name);
+		apply_autostash(git_path_merge_autostash(the_repository));
 		ret = 2;
 		goto done;
 	} else if (best_strategy == wt_strategy)
diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh
index 216113d3537..2ef39d3088e 100755
--- a/t/t7600-merge.sh
+++ b/t/t7600-merge.sh
@@ -732,6 +732,14 @@ test_expect_success 'octopus merge with --autostash' '
 	test_cmp result.1-3-5-9 file
 '
 
+test_expect_success 'failed merge (exit 2) with --autostash' '
+	git reset --hard c1 &&
+	git merge-file file file.orig file.5 &&
+	test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
+	test_i18ngrep "Applied autostash." err &&
+	test_cmp result.1-5 file
+'
+
 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
 	git reset --hard c3 &&
 	cp file.1 file &&
-- 
gitgitgadget

  parent reply	other threads:[~2021-07-23 12:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-23 12:14 [PATCH 0/4] [RFC] merge --autostash: apply autostash in more cases Philippe Blain via GitGitGadget
2021-07-23 12:14 ` [PATCH 1/4] merge: add missing word "strategy" to a message Philippe Blain via GitGitGadget
2021-07-23 15:57   ` Felipe Contreras
2021-07-23 12:14 ` [PATCH 2/4] Documentation: define 'MERGE_AUTOSTASH' Philippe Blain via GitGitGadget
2021-07-23 16:00   ` Felipe Contreras
2021-07-23 12:14 ` [PATCH 3/4] merge: apply autostash if fast-forward fails Philippe Blain via GitGitGadget
2021-07-23 16:02   ` Felipe Contreras
2021-07-23 19:13   ` Junio C Hamano
2021-07-23 12:14 ` Philippe Blain via GitGitGadget [this message]
2021-07-23 19:22   ` [PATCH 4/4] merge: apply autostash if merge strategy fails Junio C Hamano
2021-07-23 16:10 ` [PATCH 0/4] [RFC] merge --autostash: apply autostash in more cases Felipe Contreras

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e02b945ce8595a8d7b1b4ad9c382a1beeb040ed4.1627042470.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=felipe.contreras@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=levraiphilippeblain@gmail.com \
    --cc=liu.denton@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).