merge-tree: add -X strategy option
Add merge strategy option to produce more customizable merge result such as automatically resolving conflicts. Signed-off-by: Tang Yuyi <winglovet@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tang Yuyi committed
Sep 24, 2023 at 02:23 UTC
6a4c9e7b32c4154345785bd7b8d4afee5fddcead
2 files changed
+38
-3
builtin/merge-tree.c
+15
-3
@@ -18,6 +18,7 @@
18
#include "quote.h"
19
#include "tree.h"
20
#include "config.h"
21
+#include "strvec.h"
22
23
static int line_termination = '\n';
24
@@ -414,6 +415,7 @@ struct merge_tree_options {
415
int show_messages;
416
int name_only;
417
int use_stdin;
418
+ struct merge_options merge_options;
419
};
420
421
static int real_merge(struct merge_tree_options *o,
@@ -423,7 +425,7 @@ static int real_merge(struct merge_tree_options *o,
425
{
426
struct commit *parent1, *parent2;
427
struct commit_list *merge_bases = NULL;
426
- struct merge_options opt;
428
+ struct merge_options opt = o->merge_options;
429
struct merge_result result = { 0 };
430
int show_messages = o->show_messages;
431
@@ -437,8 +439,6 @@ static int real_merge(struct merge_tree_options *o,
439
help_unknown_ref(branch2, "merge-tree",
440
_("not something we can merge"));
441
440
- init_merge_options(&opt, the_repository);
441
-
442
opt.show_rename_progress = 0;
443
444
opt.branch1 = branch1;
@@ -513,6 +513,7 @@ static int real_merge(struct merge_tree_options *o,
513
int cmd_merge_tree(int argc, const char **argv, const char *prefix)
514
{
515
struct merge_tree_options o = { .show_messages = -1 };
516
+ struct strvec xopts = STRVEC_INIT;
517
int expected_remaining_argc;
518
int original_argc;
519
const char *merge_base = NULL;
@@ -548,14 +549,25 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
549
&merge_base,
550
N_("commit"),
551
N_("specify a merge-base for the merge")),
552
+ OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"),
553
+ N_("option for selected merge strategy")),
554
OPT_END()
555
};
556
557
+ /* Init merge options */
558
+ init_merge_options(&o.merge_options, the_repository);
559
+
560
/* Parse arguments */
561
original_argc = argc - 1; /* ignoring argv[0] */
562
argc = parse_options(argc, argv, prefix, mt_options,
563
merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
564
565
+ if (xopts.nr && o.mode == MODE_TRIVIAL)
566
+ die(_("--trivial-merge is incompatible with all other options"));
567
+ for (int x = 0; x < xopts.nr; x++)
568
+ if (parse_merge_opt(&o.merge_options, xopts.v[x]))
569
+ die(_("unknown strategy option: -X%s"), xopts.v[x]);
570
+
571
/* Handle --stdin */
572
if (o.use_stdin) {
573
struct strbuf buf = STRBUF_INIT;
t/t4301-merge-tree-write-tree.sh
+23
@@ -22,6 +22,7 @@ test_expect_success setup '
22
git branch side1 &&
23
git branch side2 &&
24
git branch side3 &&
25
+ git branch side4 &&
26
27
git checkout side1 &&
28
test_write_lines 1 2 3 4 5 6 >numbers &&
@@ -46,6 +47,13 @@ test_expect_success setup '
47
test_tick &&
48
git commit -m rename-numbers &&
49
50
+ git checkout side4 &&
51
+ test_write_lines 0 1 2 3 4 5 >numbers &&
52
+ echo yo >greeting &&
53
+ git add numbers greeting &&
54
+ test_tick &&
55
+ git commit -m other-content-modifications &&
56
+
57
git switch --orphan unrelated &&
58
>something-else &&
59
git add something-else &&
@@ -97,6 +105,21 @@ test_expect_success 'Content merge and a few conflicts' '
105
test_cmp expect actual
106
'
107
108
+test_expect_success 'Auto resolve conflicts by "ours" strategy option' '
109
+ git checkout side1^0 &&
110
+
111
+ # make sure merge conflict exists
112
+ test_must_fail git merge side4 &&
113
+ git merge --abort &&
114
+
115
+ git merge -X ours side4 &&
116
+ git rev-parse HEAD^{tree} >expected &&
117
+
118
+ git merge-tree -X ours side1 side4 >actual &&
119
+
120
+ test_cmp expected actual
121
+'
122
+
123
test_expect_success 'Barf on misspelled option, with exit code other than 0 or 1' '
124
# Mis-spell with single "s" instead of double "s"
125
test_expect_code 129 git merge-tree --write-tree --mesages FOOBAR side1 side2 2>expect &&