git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Izzy via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Elijah Newren <newren@gmail.com>, Izzy <winglovet@gmail.com>,
	Tang Yuyi <winglovet@gmail.com>
Subject: [PATCH v3] merge-tree: add -X strategy option
Date: Sat, 16 Sep 2023 02:14:22 +0000	[thread overview]
Message-ID: <pull.1565.v3.git.1694830462463.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1565.v2.git.1691818386345.gitgitgadget@gmail.com>

From: Tang Yuyi <winglovet@gmail.com>

Add merge strategy option to produce more customizable merge result such
as automatically solve conflicts.

Signed-off-by: Tang Yuyi <winglovet@gmail.com>
---
    merge-tree: add -X strategy option
    
    Change-Id: I16be592262d13cebcff8726eb043f7ecdb313b76

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1565%2FWingT%2Fmerge_tree_allow_strategy_option-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1565/WingT/merge_tree_allow_strategy_option-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1565

Range-diff vs v2:

 1:  7d53d08381e ! 1:  d64a774fa7c merge-tree: add -X strategy option
     @@
       ## Metadata ##
     -Author: winglovet <winglovet@gmail.com>
     +Author: Tang Yuyi <winglovet@gmail.com>
      
       ## Commit message ##
          merge-tree: add -X strategy option
     @@ Commit message
          Add merge strategy option to produce more customizable merge result such
          as automatically solve conflicts.
      
     -    Signed-off-by: winglovet <winglovet@gmail.com>
     +    Signed-off-by: Tang Yuyi <winglovet@gmail.com>
      
       ## builtin/merge-tree.c ##
      @@
     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Content merge and a few c
       	test_cmp expect actual
       '
       
     -+test_expect_success 'Auto resolve conflicts by "ours" stragety option' '
     ++test_expect_success 'Auto resolve conflicts by "ours" strategy option' '
      +	git checkout side1^0 &&
      +
      +	# make sure merge conflict exists


 builtin/merge-tree.c             | 24 ++++++++++++++++++++++++
 t/t4301-merge-tree-write-tree.sh | 23 +++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index 0de42aecf4b..2ec6ec0d39a 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -19,6 +19,8 @@
 #include "tree.h"
 #include "config.h"
 
+static const char **xopts;
+static size_t xopts_nr, xopts_alloc;
 static int line_termination = '\n';
 
 struct merge_list {
@@ -414,6 +416,7 @@ struct merge_tree_options {
 	int show_messages;
 	int name_only;
 	int use_stdin;
+	struct merge_options merge_options;
 };
 
 static int real_merge(struct merge_tree_options *o,
@@ -439,6 +442,8 @@ static int real_merge(struct merge_tree_options *o,
 
 	init_merge_options(&opt, the_repository);
 
+	opt.recursive_variant = o->merge_options.recursive_variant;
+
 	opt.show_rename_progress = 0;
 
 	opt.branch1 = branch1;
@@ -510,6 +515,17 @@ static int real_merge(struct merge_tree_options *o,
 	return !result.clean; /* result.clean < 0 handled above */
 }
 
+static int option_parse_x(const struct option *opt,
+			  const char *arg, int unset)
+{
+	if (unset)
+		return 0;
+
+	ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
+	xopts[xopts_nr++] = xstrdup(arg);
+	return 0;
+}
+
 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 {
 	struct merge_tree_options o = { .show_messages = -1 };
@@ -548,6 +564,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &merge_base,
 			   N_("commit"),
 			   N_("specify a merge-base for the merge")),
+		OPT_CALLBACK('X', "strategy-option", &xopts,
+			N_("option=value"),
+			N_("option for selected merge strategy"),
+			option_parse_x),
 		OPT_END()
 	};
 
@@ -556,6 +576,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, mt_options,
 			     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
 
+	for (int x = 0; x < xopts_nr; x++)
+		if (parse_merge_opt(&o.merge_options, xopts[x]))
+			die(_("unknown strategy option: -X%s"), xopts[x]);
+
 	/* Handle --stdin */
 	if (o.use_stdin) {
 		struct strbuf buf = STRBUF_INIT;
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 250f721795b..4125bb101ec 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -22,6 +22,7 @@ test_expect_success setup '
 	git branch side1 &&
 	git branch side2 &&
 	git branch side3 &&
+	git branch side4 &&
 
 	git checkout side1 &&
 	test_write_lines 1 2 3 4 5 6 >numbers &&
@@ -46,6 +47,13 @@ test_expect_success setup '
 	test_tick &&
 	git commit -m rename-numbers &&
 
+	git checkout side4 &&
+	test_write_lines 0 1 2 3 4 5 >numbers &&
+	echo yo >greeting &&
+	git add numbers greeting &&
+	test_tick &&
+	git commit -m other-content-modifications &&
+
 	git switch --orphan unrelated &&
 	>something-else &&
 	git add something-else &&
@@ -97,6 +105,21 @@ test_expect_success 'Content merge and a few conflicts' '
 	test_cmp expect actual
 '
 
+test_expect_success 'Auto resolve conflicts by "ours" strategy option' '
+	git checkout side1^0 &&
+
+	# make sure merge conflict exists
+	test_must_fail git merge side4 &&
+	git merge --abort &&
+
+	git merge -X ours side4 &&
+	git rev-parse HEAD^{tree} > expected &&
+
+    git merge-tree -X ours side1 side4 > actual &&
+
+	test_cmp expected actual
+'
+
 test_expect_success 'Barf on misspelled option, with exit code other than 0 or 1' '
 	# Mis-spell with single "s" instead of double "s"
 	test_expect_code 129 git merge-tree --write-tree --mesages FOOBAR side1 side2 2>expect &&

base-commit: ac83bc5054c2ac489166072334b4147ce6d0fccb
-- 
gitgitgadget

  parent reply	other threads:[~2023-09-16  2:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-05 14:24 [PATCH] merge-tree: add -X strategy option Izzy via GitGitGadget
2023-08-07  2:10 ` Junio C Hamano
2023-08-12  5:33 ` [PATCH v2] " Izzy via GitGitGadget
2023-08-12  5:41   ` 唐宇奕
2023-09-03  1:31     ` 唐宇奕
2023-09-12 15:03   ` Elijah Newren
2023-09-16  2:14   ` Izzy via GitGitGadget [this message]
2023-09-16  2:26     ` [PATCH v3] " 唐宇奕
2023-09-16  3:21       ` Elijah Newren
2023-09-16  3:16     ` Elijah Newren
2023-09-16  3:47     ` [PATCH v4] " Izzy via GitGitGadget
2023-09-16  3:55       ` Elijah Newren
2023-09-16  4:04       ` 唐宇奕
2023-09-16  6:11       ` Jeff King
2023-09-16  8:37       ` [PATCH v5] " Izzy via GitGitGadget
2023-09-16  8:38         ` 唐宇奕
2023-09-18  9:53         ` Phillip Wood
2023-09-18 16:08           ` Junio C Hamano
2023-09-24  2:23         ` [PATCH v6] " Izzy via GitGitGadget
2023-09-24  2:26           ` 唐宇奕
2023-10-09  9:58           ` Phillip Wood
2023-10-09 15:53             ` Jeff King
2023-10-09 17:10               ` Junio C Hamano
2023-10-09 18:52                 ` Jeff King
2023-10-11 19:38                   ` Junio C Hamano
2023-10-11 21:43                     ` Jeff King
2023-10-11 22:19                       ` Junio C Hamano

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

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

  git send-email \
    --in-reply-to=pull.1565.v3.git.1694830462463.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=newren@gmail.com \
    --cc=winglovet@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.
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 NNTP newsgroup(s).