Message ID | pull.1565.v5.git.1694853437494.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [v5] merge-tree: add -X strategy option | expand |
Thank you for your advice! I've uploaded new patch. On Sat, Sep 16, 2023 at 4:37 PM Izzy via GitGitGadget <gitgitgadget@gmail.com> wrote: > > From: Tang Yuyi <winglovet@gmail.com> > > Add merge strategy option to produce more customizable merge result such > as automatically resolving 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-v5 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1565/WingT/merge_tree_allow_strategy_option-v5 > Pull-Request: https://github.com/gitgitgadget/git/pull/1565 > > Range-diff vs v4: > > 1: d2d8fcc2e9b ! 1: 28d4282e0d8 merge-tree: add -X strategy option > @@ Commit message > > ## builtin/merge-tree.c ## > @@ > + #include "quote.h" > #include "tree.h" > #include "config.h" > ++#include "strvec.h" > > -+static const char **xopts; > -+static size_t xopts_nr, xopts_alloc; > static int line_termination = '\n'; > > - struct merge_list { > @@ builtin/merge-tree.c: struct merge_tree_options { > int show_messages; > int name_only; > @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o, > > opt.branch1 = branch1; > @@ builtin/merge-tree.c: 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 }; > ++ struct strvec xopts = STRVEC_INIT; > + int expected_remaining_argc; > + int original_argc; > + const char *merge_base = NULL; > @@ builtin/merge-tree.c: 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_STRVEC('X', "strategy-option", &xopts, N_("option=value"), > ++ N_("option for selected merge strategy")), > OPT_END() > }; > > @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char > 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]); > ++ 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) { > @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Content merge and a few c > + git merge --abort && > + > + git merge -X ours side4 && > -+ git rev-parse HEAD^{tree} > expected && > ++ git rev-parse HEAD^{tree} >expected && > + > -+ git merge-tree -X ours side1 side4 > actual && > ++ git merge-tree -X ours side1 side4 >actual && > + > + test_cmp expected actual > +' > > > builtin/merge-tree.c | 11 +++++++++++ > t/t4301-merge-tree-write-tree.sh | 23 +++++++++++++++++++++++ > 2 files changed, 34 insertions(+) > > diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c > index 0de42aecf4b..97d0fe6c952 100644 > --- a/builtin/merge-tree.c > +++ b/builtin/merge-tree.c > @@ -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, > @@ -439,6 +441,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; > @@ -513,6 +517,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,6 +553,8 @@ 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() > }; > > @@ -556,6 +563,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.v[x])) > + die(_("unknown strategy option: -X%s"), xopts.v[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..b2c8a43fce3 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
On 16/09/2023 09:37, Izzy via GitGitGadget wrote: > From: Tang Yuyi <winglovet@gmail.com> > > Add merge strategy option to produce more customizable merge result such > as automatically resolving conflicts. I think adding a merge strategy option to merge-tree is a good idea, but have you tested this with anything apart from -Xours or -Xtheirs? It looks to me like those are the only two that this patch supports. If you look at parse_merge_opt() in merge-recursive.c you will see that there are many other options. In order to support all the merge options I think this patch needs a bit of refactoring. > diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c > index 0de42aecf4b..97d0fe6c952 100644 > --- a/builtin/merge-tree.c > +++ b/builtin/merge-tree.c > static int real_merge(struct merge_tree_options *o, > @@ -439,6 +441,8 @@ static int real_merge(struct merge_tree_options *o, > > init_merge_options(&opt, the_repository); > > + opt.recursive_variant = o->merge_options.recursive_variant; > + Rather that copying across individual members I think you should initialize o->merge_options properly in cmd_merge_tree() by calling init_merge_options() and then use o->merge_options instead of "opt" in this function. That way all the strategy options will be supported. > opt.show_rename_progress = 0; > > opt.branch1 = branch1; > @@ -513,6 +517,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,6 +553,8 @@ 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() > }; > > @@ -556,6 +563,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix) You should add init_merge_options(&o.merge_options); here to ensure it is properly initialized. > argc = parse_options(argc, argv, prefix, mt_options, > merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION); This is the right place to call parse_merge_opt() but I think we should first check that the user has requested a real merge rather than a trivial merge. if (xopts.nr && o.mode == MODE_TRIVIAL) die(_("--trivial-merge is incompatible with all other options")); Otherwise if the user passes in invalid strategy option to a trivial merge they'll get an error about an invalid strategy option rather than being told --strategy-option is not supported with --trivial-merge. Ideally there would be a preparatory patch that moves the switch statement that is below the "if(o.use_stdin)" block up to this point so we'd always have set o.mode before checking if it is a trivial merge. (I think you'd to change the code slightly when it is moved to add a check for o.use_stdin) Best Wishes Phillip > + 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; > diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh > index 250f721795b..b2c8a43fce3 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
Phillip Wood <phillip.wood123@gmail.com> writes: > ... > This is the right place to call parse_merge_opt() but I think we > should first check that the user has requested a real merge rather > than a trivial merge. > > if (xopts.nr && o.mode == MODE_TRIVIAL) > die(_("--trivial-merge is incompatible with all other options")); > > Otherwise if the user passes in invalid strategy option to a trivial > merge they'll get an error about an invalid strategy option rather > than being told --strategy-option is not supported with > --trivial-merge. I presume that another issue with not having that compatibility checking with "--trivial" would be when the user passes a valid strategy option and it would be silently ignored. > Ideally there would be a preparatory patch that moves the switch > statement that is below the "if(o.use_stdin)" block up to this point > so we'd always have set o.mode before checking if it is a trivial > merge. (I think you'd to change the code slightly when it is moved to > add a check for o.use_stdin) That sounds very good. Thanks for a good "stepping back a bit" review. Highly appreciated.
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c index 0de42aecf4b..97d0fe6c952 100644 --- a/builtin/merge-tree.c +++ b/builtin/merge-tree.c @@ -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, @@ -439,6 +441,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; @@ -513,6 +517,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,6 +553,8 @@ 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() }; @@ -556,6 +563,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.v[x])) + die(_("unknown strategy option: -X%s"), xopts.v[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..b2c8a43fce3 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 &&