git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Paul Tan <pyokagan@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
	Stefan Beller <sbeller@google.com>, Paul Tan <pyokagan@gmail.com>
Subject: [PATCH v4 19/44] builtin-am: implement --3way, am.threeway
Date: Sun, 28 Jun 2015 22:05:41 +0800	[thread overview]
Message-ID: <1435500366-31700-20-git-send-email-pyokagan@gmail.com> (raw)
In-Reply-To: <1435500366-31700-1-git-send-email-pyokagan@gmail.com>

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh supported the --3way option, and if set, would attempt to do a
3-way merge if the initial patch application fails.

Since d96a275 (git-am: add am.threeWay config variable, 2015-06-04), the
setting am.threeWay configures if the --3way option is set by default.

Since 5d86861 (am -3: list the paths that needed 3-way fallback,
2012-03-28), in a 3-way merge git-am.sh would list the paths that needed
3-way fallback, so that the user can review them more carefully to spot
mismerges.

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
---
 builtin/am.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 153 insertions(+), 4 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 1ffbba1..e870a1b 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -19,6 +19,8 @@
 #include "unpack-trees.h"
 #include "branch.h"
 #include "sequencer.h"
+#include "revision.h"
+#include "merge-recursive.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -82,6 +84,8 @@ struct am_state {
 	/* number of digits in patch filename */
 	int prec;
 
+	int threeway;
+
 	int quiet;
 
 	int append_signoff;
@@ -105,6 +109,8 @@ static void am_state_init(struct am_state *state, const char *dir)
 
 	state->prec = 4;
 
+	git_config_get_bool("am.threeway", &state->threeway);
+
 	quiet = getenv("GIT_QUIET");
 	if (quiet && *quiet)
 		state->quiet = 1;
@@ -377,6 +383,9 @@ static void am_load(struct am_state *state)
 
 	read_commit_msg(state);
 
+	read_state_file(&sb, state, "threeway", 1);
+	state->threeway = !strcmp(sb.buf, "t");
+
 	read_state_file(&sb, state, "quiet", 1);
 	state->quiet = !strcmp(sb.buf, "t");
 
@@ -561,6 +570,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
 		die(_("Failed to split patches."));
 	}
 
+	write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
+
 	write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
 
 	write_file(am_path(state, "sign"), 1, state->append_signoff ? "t" : "f");
@@ -796,16 +807,34 @@ finish:
 }
 
 /**
- * Applies current patch with git-apply. Returns 0 on success, -1 otherwise.
+ * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
+ * `index_file` is not NULL, the patch will be applied to that index.
  */
-static int run_apply(const struct am_state *state)
+static int run_apply(const struct am_state *state, const char *index_file)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 
 	cp.git_cmd = 1;
 
+	if (index_file)
+		argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
+
+	/*
+	 * If we are allowed to fall back on 3-way merge, don't give false
+	 * errors during the initial attempt.
+	 */
+	if (state->threeway && !index_file) {
+		cp.no_stdout = 1;
+		cp.no_stderr = 1;
+	}
+
 	argv_array_push(&cp.args, "apply");
-	argv_array_push(&cp.args, "--index");
+
+	if (index_file)
+		argv_array_push(&cp.args, "--cached");
+	else
+		argv_array_push(&cp.args, "--index");
+
 	argv_array_push(&cp.args, am_path(state, "patch"));
 
 	if (run_command(&cp))
@@ -813,8 +842,106 @@ static int run_apply(const struct am_state *state)
 
 	/* Reload index as git-apply will have modified it. */
 	discard_cache();
+	read_cache_from(index_file ? index_file : get_index_file());
+
+	return 0;
+}
+
+/**
+ * Builds a index that contains just the blobs needed for a 3way merge.
+ */
+static int build_fake_ancestor(const struct am_state *state, const char *index_file)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+
+	cp.git_cmd = 1;
+	argv_array_push(&cp.args, "apply");
+	argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
+	argv_array_push(&cp.args, am_path(state, "patch"));
+
+	if (run_command(&cp))
+		return -1;
+
+	return 0;
+}
+
+/**
+ * Attempt a threeway merge, using index_path as the temporary index.
+ */
+static int fall_back_threeway(const struct am_state *state, const char *index_path)
+{
+	unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
+		      our_tree[GIT_SHA1_RAWSZ];
+	const unsigned char *bases[1] = {orig_tree};
+	struct merge_options o;
+	struct commit *result;
+	char *his_tree_name;
+
+	if (get_sha1("HEAD", our_tree) < 0)
+		hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
+
+	if (build_fake_ancestor(state, index_path))
+		return error("could not build fake ancestor");
+
+	discard_cache();
+	read_cache_from(index_path);
+
+	if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
+		return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
+
+	say(state, stdout, _("Using index info to reconstruct a base tree..."));
+
+	if (!state->quiet) {
+		/*
+		 * List paths that needed 3-way fallback, so that the user can
+		 * review them with extra care to spot mismerges.
+		 */
+		struct rev_info rev_info;
+		const char *diff_filter_str = "--diff-filter=AM";
+
+		init_revisions(&rev_info, NULL);
+		rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
+		diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
+		add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
+		diff_setup_done(&rev_info.diffopt);
+		run_diff_index(&rev_info, 1);
+	}
+
+	if (run_apply(state, index_path))
+		return error(_("Did you hand edit your patch?\n"
+				"It does not apply to blobs recorded in its index."));
+
+	if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
+		return error("could not write tree");
+
+	say(state, stdout, _("Falling back to patching base and 3-way merge..."));
+
+	discard_cache();
 	read_cache();
 
+	/*
+	 * This is not so wrong. Depending on which base we picked, orig_tree
+	 * may be wildly different from ours, but his_tree has the same set of
+	 * wildly different changes in parts the patch did not touch, so
+	 * recursive ends up canceling them, saying that we reverted all those
+	 * changes.
+	 */
+
+	init_merge_options(&o);
+
+	o.branch1 = "HEAD";
+	his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
+	o.branch2 = his_tree_name;
+
+	if (state->quiet)
+		o.verbosity = 0;
+
+	if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
+		free(his_tree_name);
+		return error(_("Failed to merge in the changes."));
+	}
+
+	free(his_tree_name);
 	return 0;
 }
 
@@ -883,6 +1010,7 @@ static void am_run(struct am_state *state)
 
 	while (state->cur <= state->last) {
 		const char *mail = am_path(state, msgnum(state));
+		int apply_status;
 
 		if (!file_exists(mail))
 			goto next;
@@ -895,7 +1023,26 @@ static void am_run(struct am_state *state)
 
 		say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
 
-		if (run_apply(state) < 0) {
+		apply_status = run_apply(state, NULL);
+
+		if (apply_status && state->threeway) {
+			struct strbuf sb = STRBUF_INIT;
+
+			strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
+			apply_status = fall_back_threeway(state, sb.buf);
+			strbuf_release(&sb);
+
+			/*
+			 * Applying the patch to an earlier tree and merging
+			 * the result may have produced the same tree as ours.
+			 */
+			if (!apply_status && !index_has_changes(NULL)) {
+				say(state, stdout, _("No changes -- Patch already applied."));
+				goto next;
+			}
+		}
+
+		if (apply_status) {
 			int advice_amworkdir = 1;
 
 			printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
@@ -1167,6 +1314,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
 	};
 
 	struct option options[] = {
+		OPT_BOOL('3', "3way", &state.threeway,
+			N_("allow fall back on 3way merging if needed")),
 		OPT__QUIET(&state.quiet, N_("be quiet")),
 		OPT_BOOL('s', "signoff", &state.append_signoff,
 			N_("add a Signed-off-by line to the commit message")),
-- 
2.5.0.rc0.76.gb2c6e93

  parent reply	other threads:[~2015-06-28 14:08 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-28 14:05 [PATCH v4 00/44] Make git-am a builtin Paul Tan
2015-06-28 14:05 ` [PATCH v4 01/44] wrapper: implement xopen() Paul Tan
2015-06-29  4:48   ` Torsten Bögershausen
2015-06-29  5:04     ` Stefan Beller
2015-06-29 17:18     ` Junio C Hamano
2015-07-01  6:05       ` Paul Tan
2015-07-01  6:04     ` Paul Tan
2015-06-28 14:05 ` [PATCH v4 02/44] wrapper: implement xfopen() Paul Tan
2015-06-28 14:05 ` [PATCH v4 03/44] builtin-am: implement skeletal builtin am Paul Tan
2015-06-28 14:05 ` [PATCH v4 04/44] builtin-am: implement patch queue mechanism Paul Tan
2015-06-29  5:08   ` Stefan Beller
2015-07-07 12:50     ` Paul Tan
2015-06-28 14:05 ` [PATCH v4 05/44] builtin-am: split out mbox/maildir patches with git-mailsplit Paul Tan
2015-06-28 14:05 ` [PATCH v4 06/44] builtin-am: auto-detect mbox patches Paul Tan
2015-06-28 14:05 ` [PATCH v4 07/44] builtin-am: extract patch and commit info with git-mailinfo Paul Tan
2015-06-28 14:05 ` [PATCH v4 08/44] builtin-am: apply patch with git-apply Paul Tan
2015-06-28 14:05 ` [PATCH v4 09/44] builtin-am: implement committing applied patch Paul Tan
2015-06-28 14:05 ` [PATCH v4 10/44] builtin-am: refuse to apply patches if index is dirty Paul Tan
2015-06-28 14:05 ` [PATCH v4 11/44] builtin-am: implement --resolved/--continue Paul Tan
2015-06-28 14:05 ` [PATCH v4 12/44] builtin-am: implement --skip Paul Tan
2015-06-28 14:05 ` [PATCH v4 13/44] builtin-am: implement --abort Paul Tan
2015-06-28 14:05 ` [PATCH v4 14/44] builtin-am: reject patches when there's a session in progress Paul Tan
2015-06-28 14:05 ` [PATCH v4 15/44] builtin-am: implement -q/--quiet, GIT_QUIET Paul Tan
2015-06-28 14:05 ` [PATCH v4 16/44] builtin-am: exit with user friendly message on failure Paul Tan
2015-06-28 14:05 ` [PATCH v4 17/44] builtin-am: implement -s/--signoff Paul Tan
2015-06-28 14:05 ` [PATCH v4 18/44] cache-tree: introduce write_index_as_tree() Paul Tan
2015-06-28 14:05 ` Paul Tan [this message]
2015-06-30  0:18   ` [PATCH v4 19/44] builtin-am: implement --3way, am.threeway Stefan Beller
2015-06-28 14:05 ` [PATCH v4 20/44] builtin-am: implement --rebasing mode Paul Tan
2015-06-28 14:05 ` [PATCH v4 21/44] builtin-am: bypass git-mailinfo when --rebasing Paul Tan
2015-06-28 14:05 ` [PATCH v4 22/44] builtin-am: handle stray state directory Paul Tan
2015-06-28 14:05 ` [PATCH v4 23/44] builtin-am: implement -u/--utf8 Paul Tan
2015-06-28 14:05 ` [PATCH v4 24/44] builtin-am: implement -k/--keep, --keep-non-patch Paul Tan
2015-06-28 14:05 ` [PATCH v4 25/44] builtin-am: implement --[no-]message-id, am.messageid Paul Tan
2015-06-28 14:05 ` [PATCH v4 26/44] builtin-am: support --keep-cr, am.keepcr Paul Tan
2015-06-28 14:05 ` [PATCH v4 27/44] builtin-am: implement --[no-]scissors Paul Tan
2015-07-07  8:23   ` Paul Tan
2015-06-28 14:05 ` [PATCH v4 28/44] builtin-am: pass git-apply's options to git-apply Paul Tan
2015-06-29 23:56   ` Stefan Beller
2015-07-01 10:22     ` Paul Tan
2015-07-01 17:01       ` Stefan Beller
2015-06-28 14:05 ` [PATCH v4 29/44] builtin-am: implement --ignore-date Paul Tan
2015-06-28 14:05 ` [PATCH v4 30/44] builtin-am: implement --committer-date-is-author-date Paul Tan
2015-06-28 14:05 ` [PATCH v4 31/44] builtin-am: implement -S/--gpg-sign, commit.gpgsign Paul Tan
2015-06-29 23:51   ` Stefan Beller
2015-07-01  8:01     ` Paul Tan
2015-07-01 16:43       ` Stefan Beller
2015-06-28 14:05 ` [PATCH v4 32/44] builtin-am: invoke post-rewrite hook Paul Tan
2015-06-28 14:05 ` [PATCH v4 33/44] builtin-am: support automatic notes copying Paul Tan
2015-06-28 14:05 ` [PATCH v4 34/44] builtin-am: invoke applypatch-msg hook Paul Tan
2015-06-28 14:05 ` [PATCH v4 35/44] builtin-am: invoke pre-applypatch hook Paul Tan
2015-06-28 14:05 ` [PATCH v4 36/44] builtin-am: invoke post-applypatch hook Paul Tan
2015-06-28 14:05 ` [PATCH v4 37/44] builtin-am: rerere support Paul Tan
2015-06-28 14:06 ` [PATCH v4 38/44] builtin-am: support and auto-detect StGit patches Paul Tan
2015-06-29 20:42   ` Stefan Beller
2015-06-29 20:51     ` Eric Sunshine
2015-06-29 21:39       ` Junio C Hamano
2015-07-01  7:25         ` Paul Tan
2015-06-28 14:06 ` [PATCH v4 39/44] builtin-am: support and auto-detect StGit series files Paul Tan
2015-06-28 14:06 ` [PATCH v4 40/44] builtin-am: support and auto-detect mercurial patches Paul Tan
2015-06-29 20:32   ` Stefan Beller
2015-06-29 20:32     ` Stefan Beller
2015-07-01  9:07       ` Paul Tan
2015-07-01  8:48     ` Paul Tan
2015-06-28 14:06 ` [PATCH v4 41/44] builtin-am: implement -i/--interactive Paul Tan
2015-06-28 14:06 ` [PATCH v4 42/44] builtin-am: implement legacy -b/--binary option Paul Tan
2015-06-29 20:05   ` Stefan Beller
     [not found]     ` <CABPQNSakaoyNRuNz=bcDYWdy4e2O3M4UuYoOT5JAV1mt-BiAOw@mail.gmail.com>
2015-07-14 10:09       ` Paul Tan
2015-06-28 14:06 ` [PATCH v4 43/44] builtin-am: check for valid committer ident Paul Tan
2015-06-29 20:02   ` Stefan Beller
2015-07-01  6:43     ` Paul Tan
2015-06-28 14:06 ` [PATCH v4 44/44] builtin-am: remove redirection to git-am.sh Paul Tan
2015-06-29  5:01 ` [PATCH v4 00/44] Make git-am a builtin Stefan Beller
2015-06-30  0:19   ` Stefan Beller

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=1435500366-31700-20-git-send-email-pyokagan@gmail.com \
    --to=pyokagan@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    --cc=sbeller@google.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).