1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-24 14:16:32 +02:00

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>
This commit is contained in:
Tang Yuyi 2023-09-24 02:23:42 +00:00 committed by Junio C Hamano
parent bcb6cae296
commit 6a4c9e7b32
2 changed files with 38 additions and 3 deletions

View File

@ -18,6 +18,7 @@
#include "quote.h"
#include "tree.h"
#include "config.h"
#include "strvec.h"
static int line_termination = '\n';
@ -414,6 +415,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,
@ -423,7 +425,7 @@ static int real_merge(struct merge_tree_options *o,
{
struct commit *parent1, *parent2;
struct commit_list *merge_bases = NULL;
struct merge_options opt;
struct merge_options opt = o->merge_options;
struct merge_result result = { 0 };
int show_messages = o->show_messages;
@ -437,8 +439,6 @@ static int real_merge(struct merge_tree_options *o,
help_unknown_ref(branch2, "merge-tree",
_("not something we can merge"));
init_merge_options(&opt, the_repository);
opt.show_rename_progress = 0;
opt.branch1 = branch1;
@ -513,6 +513,7 @@ static int real_merge(struct merge_tree_options *o,
int cmd_merge_tree(int argc, const char **argv, const char *prefix)
{
struct merge_tree_options o = { .show_messages = -1 };
struct strvec xopts = STRVEC_INIT;
int expected_remaining_argc;
int original_argc;
const char *merge_base = NULL;
@ -548,14 +549,25 @@ 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_STRVEC('X', "strategy-option", &xopts, N_("option=value"),
N_("option for selected merge strategy")),
OPT_END()
};
/* Init merge options */
init_merge_options(&o.merge_options, the_repository);
/* Parse arguments */
original_argc = argc - 1; /* ignoring argv[0] */
argc = parse_options(argc, argv, prefix, mt_options,
merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
if (xopts.nr && o.mode == MODE_TRIVIAL)
die(_("--trivial-merge is incompatible with all other options"));
for (int x = 0; x < xopts.nr; x++)
if (parse_merge_opt(&o.merge_options, xopts.v[x]))
die(_("unknown strategy option: -X%s"), xopts.v[x]);
/* Handle --stdin */
if (o.use_stdin) {
struct strbuf buf = STRBUF_INIT;

View File

@ -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 &&