git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] merge-tree.c: add --merge-base=<commit> option
@ 2022-10-27 12:12 Kyle Zhao via GitGitGadget
  2022-10-27 18:09 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-10-27 12:12 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This option allows users to specify a merge-base commit for the merge.

It will give our callers more flexibility to use the `git merge-tree`.
For example:

    git merge-tree --merge-base=<sha1>^1 source-branch <sha1>

This allows us to implement `git cherry-pick` in bare repositories.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
    merge-tree.c: add --merge-base= option
    
    Thanks for Elijah's work. I'm very excited that merge-ort is integrated
    into the git merge-tree, which means that we can use merge-ort in bare
    repositories to optimize merge performance.
    
    In this patch, I introduce a new --merge-base=<commit> option to allow
    callers to specify a merge-base for the merge. This may allow users to
    implement git cherry-pick and git rebase in bare repositories with git
    merge-tree cmd.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 23 +++++++++++++----
 t/t4301-merge-tree-write-tree.sh | 44 ++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d6c356740ef..e762209b76d 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index ae5782917b9..adc461f00f3 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -402,6 +403,7 @@ struct merge_tree_options {
 	int allow_unrelated_histories;
 	int show_messages;
 	int name_only;
+	char* merge_base;
 };
 
 static int real_merge(struct merge_tree_options *o,
@@ -430,11 +432,18 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
+	if (o->merge_base) {
+		struct commit *c = lookup_commit_reference_by_name(o->merge_base);
+		if (!c)
+			die(_("could not lookup commit %s"), o->merge_base);
+		commit_list_insert(c, &merge_bases);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+	}
 	if (!merge_bases && !o->allow_unrelated_histories)
 		die(_("refusing to merge unrelated histories"));
 	merge_bases = reverse_commit_list(merge_bases);
@@ -505,6 +514,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.allow_unrelated_histories,
 			   N_("allow merging unrelated histories"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			 &o.merge_base,
+			 N_("commit"),
+			 N_("specify a merge-base commit for the merge")),
 		OPT_END()
 	};
 
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 013b77144bd..032ff4a1c3d 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -819,4 +819,48 @@ test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository
 	test_must_fail git -C read-only merge-tree side1 side2
 '
 
+# specify merge-base as parent of branch2.
+# git merge-tree --merge-base=A O B
+#   Commit O: foo, bar
+#   Commit A: modify foo after Commit O
+#   Commit B: modify bar after Commit A
+#   Expected: foo is unchanged, modify bar
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	git init base-b2-p && (
+		cd base-b2-p &&
+		echo foo >foo &&
+		echo bar >bar &&
+		git add foo bar &&
+		git commit -m O &&
+
+		git branch O &&
+		git branch A &&
+
+		git checkout A &&
+		echo "A" >foo &&
+		git add foo &&
+		git commit -m A &&
+
+		git checkout -b B &&
+		echo "B" >bar &&
+		git add bar &&
+		git commit -m B
+	) &&
+	# Testing
+	(
+		cd base-b2-p &&
+		TREE_OID=$(git merge-tree --merge-base=A O B) &&
+
+		q_to_tab <<-EOF >expect &&
+		100644 blob $(git rev-parse B:bar)Qbar
+		100644 blob $(git rev-parse O:foo)Qfoo
+		EOF
+
+		git ls-tree $TREE_OID >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done

base-commit: db29e6bbaed156ae9025ff0474fd788e58230367
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-27 12:12 [PATCH] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-10-27 18:09 ` Junio C Hamano
  2022-10-28  8:20   ` Elijah Newren
  2022-10-28 11:43   ` [Internet]Re: " kylezhao(赵柯宇)
  2022-10-28  8:13 ` Elijah Newren
  2022-10-28 11:55 ` [PATCH v2] " Kyle Zhao via GitGitGadget
  2 siblings, 2 replies; 43+ messages in thread
From: Junio C Hamano @ 2022-10-27 18:09 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Elijah Newren, Kyle Zhao

"Kyle Zhao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
> index d6c356740ef..e762209b76d 100644
> --- a/Documentation/git-merge-tree.txt
> +++ b/Documentation/git-merge-tree.txt
> @@ -64,6 +64,10 @@ OPTIONS
>  	share no common history.  This flag can be given to override that
>  	check and make the merge proceed anyway.
>  
> +--merge-base=<commit>::
> +	Instead of finding the merge-bases for <branch1> and <branch2>,
> +	specify a merge-base for the merge.

I like adding and exposing this feature to allow the end-user
specify which commit to use as the base (instead of allowing the
tool compute it from the two branches), but I wonder if a new option
is even needed.

In the original "trivial merge" mode, the command takes three trees
without having to have this new option.  In the new "write-tree"
mode, currently it is incapable of taking the base, but it does not
have to stay that way.  Wouldn't it be sufficient to update the UI
to

    git merge-tree [--write-tree] [<options>] [<base-commit>] <branch1> <branch2>
    git merge-tree [--trivial-merge] <base-commit> <branch1> <branch2>

IOW, when you want to supply the base, you'd be explicit and ask for
the new "write-tree" mode, i.e.

    $ git merge-tree --write-tree $(git merge-base branch^ branch) HEAD branch 

would be how you would use merge-tree to cherry-pick the commit at
the tip of the branch on top of the current commit.

> @@ -402,6 +403,7 @@ struct merge_tree_options {
>  	int allow_unrelated_histories;
>  	int show_messages;
>  	int name_only;
> +	char* merge_base;

Style.  We write in C, not in C++, and our asterisks stick to
variables and members of structs, not types.

> -	/*
> -	 * Get the merge bases, in reverse order; see comment above
> -	 * merge_incore_recursive in merge-ort.h
> -	 */
> -	merge_bases = get_merge_bases(parent1, parent2);
> +	if (o->merge_base) {
> +		struct commit *c = lookup_commit_reference_by_name(o->merge_base);
> +		if (!c)
> +			die(_("could not lookup commit %s"), o->merge_base);
> +		commit_list_insert(c, &merge_bases);

Curious.  The original code unconditionally assigned merge_bases, so
there wasn't a good reason to initialize the variable before this point,
but this new code assumes that merge_bases to be initialized to NULL.

Luckily, it is initialized in the original code, even though it
wasn't necessary at all.  So this new code can work correctly.
Good.

> +	} else {
> +		/*
> +		 * Get the merge bases, in reverse order; see comment above
> +		 * merge_incore_recursive in merge-ort.h
> +		 */
> +		merge_bases = get_merge_bases(parent1, parent2);
> +	}

Yes, this feature was very much lacking and is a welcome addition.

I also have to wonder how this should interact with a topic that is
in-flight to feed multiple merge-tree requests from the standard
input to have a single process perform multiple (not necessarily
related) merges.  Elijah knows much better, but my gut feeling is
that it shouldn't be hard to allow feeding an extra commit on the
same line to be used as the base.

Thanks.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-27 12:12 [PATCH] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-10-27 18:09 ` Junio C Hamano
@ 2022-10-28  8:13 ` Elijah Newren
  2022-10-28 11:54   ` [Internet]Re: " kylezhao(赵柯宇)
  2022-10-28 11:55 ` [PATCH v2] " Kyle Zhao via GitGitGadget
  2 siblings, 1 reply; 43+ messages in thread
From: Elijah Newren @ 2022-10-28  8:13 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Kyle Zhao

Hi Kyle!

Thanks for the interest in this area of the code.  There's lots of
interesting history you're probably not aware of here...

On Thu, Oct 27, 2022 at 5:12 AM Kyle Zhao via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Kyle Zhao <kylezhao@tencent.com>
>
> This option allows users to specify a merge-base commit for the merge.

Note that this has been implemented previously.  I may have
implemented it previously and ripped it out (about a year ago), or
maybe I just avoided it once I ran across the reasons below.  Dscho
also implemented it over at
https://lore.kernel.org/git/nycvar.QRO.7.76.6.2202210956430.26495@tvgsbejvaqbjf.bet/

You may want to take a look at his code for comparison, especially the
merge_incore_nonrecursive() stuff.

> It will give our callers more flexibility to use the `git merge-tree`.
> For example:
>
>     git merge-tree --merge-base=<sha1>^1 source-branch <sha1>
>
> This allows us to implement `git cherry-pick` in bare repositories.

This capability is something we may ultimately want for completeness,
but it definitely should not be marketed as a building block for
implementing cherry-pick or rebase.  There are several reasons for
that:

Performance reasons:
  * it's a design with a separate fork for every commit to be picked,
which is quite wasteful.  cherry-pick and rebase should do better.
  * it is explicitly single-commit replaying, which discards the
remembering renames optimization I placed into merge-ort.  We should
have something series-aware so it can take advantage of that.

Usability/Design reasons:
  * with cherry-picking or rebasing you often have a sequence of
commits.  Picking them one at a time not only has the performance
issues above, but also introduces usability and design questions
around the creation of commits from the toplevel trees created by
merge-tree.  For normal merges, we have no idea what the commit
message should be without explicit directives and user editing; this
is reflected in the fact that `git merge` has 9 different flags for
controlling how to specify the commit message or components of it or
how to tweak it or whatever.  I really don't want to copy all of that
logic from git-merge into git-merge-tree, and merge-tree is therefore
just a dry-run merge.  However, in the case of cherry-picking or
rebasing, we *do* have a commit message (and author) that we can just
re-use.  But if we make merge-tree handle the commit message and
create commits, you are fundamentally changing the output style of
merge-tree that we have carefully documented (and promised backward
compatibility on).  Figuring out how to extend the output and when
(and do we also allow that for non-specified merge bases) probably
requires some careful thought.
  * a cherry-pick or rebase tool actually might want to do something
clever with sequences of replayed commits.  For example, if later
commits in the series have commit messages that refer to earlier
commits in the series by their (abbreviated) commit hash, it would be
nice to update those (abbreviated) commit hashes to instead refer to
the replayed version of the earlier commit.  But that fundamentally
requires operating on a series rather than an individual commit.

Correctness/capability reason:
  * this patch is fundamentally limited to replaying regular commits.
Replaying merge-commits correctly doesn't fit within this design; it
needs a different framework.

Maintenance reason:
  * suggesting this code as a basis for cherry-pick or rebase is
likely to lead to yet another shell-scripted rebase; we've been trying
to generally nuke shell scripts from Git for a variety of reasons.
It'd be sad to regress here.

> Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
> ---
>     merge-tree.c: add --merge-base= option
>
>     Thanks for Elijah's work. I'm very excited that merge-ort is integrated
>     into the git merge-tree, which means that we can use merge-ort in bare
>     repositories to optimize merge performance.
>
>     In this patch, I introduce a new --merge-base=<commit> option to allow
>     callers to specify a merge-base for the merge. This may allow users to
>     implement git cherry-pick and git rebase in bare repositories with git
>     merge-tree cmd.

My apologies for having very limited git time (which is often entirely
used up just in reviewing/responding to patches and other queries on
the list instead of on new features like this, or maybe on making some
nice slides for a conference); if I had more time, I think git-replay
could have easily been done many months ago (or perhaps even last
year).  Then you'd have the high level tool you need for server side
cherry-picking and rebasing.  But, I haven't had much time.  So, it
makes sense that folks might want to use an interim hack while waiting
for the more robust tool to materialize.

> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/1397
>
>  Documentation/git-merge-tree.txt |  4 +++
>  builtin/merge-tree.c             | 23 +++++++++++++----
>  t/t4301-merge-tree-write-tree.sh | 44 ++++++++++++++++++++++++++++++++
>  3 files changed, 66 insertions(+), 5 deletions(-)
>
> diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
> index d6c356740ef..e762209b76d 100644
> --- a/Documentation/git-merge-tree.txt
> +++ b/Documentation/git-merge-tree.txt
> @@ -64,6 +64,10 @@ OPTIONS
>         share no common history.  This flag can be given to override that
>         check and make the merge proceed anyway.
>
> +--merge-base=<commit>::
> +       Instead of finding the merge-bases for <branch1> and <branch2>,
> +       specify a merge-base for the merge.

We could potentially just handle this as an additional positional
argument, which might be more in-keeping with merge-tree style.
However...

* it would force the --write-tree option to be specified when three
commits are listed...at least until we can declare the deprecation
period for --trivial-merge over and nuke that code and flag.
* if we ever want to allow octopus merges, the additional positional
argument does not fly well; the explicit flag for --merge-base might
be necessary.

So, this kind of raises questions on what the deprecation period for
--trivial-merge should be, and whether we ever want to support octopus
merges within git-merge-tree.

>  [[OUTPUT]]
>  OUTPUT

No output changes...makes sense, but kind of reinforces the fact that
this really isn't a basis for cherry-pick or rebase (as noted above).

>  ------
> diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
> index ae5782917b9..adc461f00f3 100644
> --- a/builtin/merge-tree.c
> +++ b/builtin/merge-tree.c
> @@ -3,6 +3,7 @@
>  #include "tree-walk.h"
>  #include "xdiff-interface.h"
>  #include "help.h"
> +#include "commit.h"
>  #include "commit-reach.h"
>  #include "merge-ort.h"
>  #include "object-store.h"
> @@ -402,6 +403,7 @@ struct merge_tree_options {
>         int allow_unrelated_histories;
>         int show_messages;
>         int name_only;
> +       char* merge_base;
>  };
>
>  static int real_merge(struct merge_tree_options *o,
> @@ -430,11 +432,18 @@ static int real_merge(struct merge_tree_options *o,
>         opt.branch1 = branch1;
>         opt.branch2 = branch2;
>
> -       /*
> -        * Get the merge bases, in reverse order; see comment above
> -        * merge_incore_recursive in merge-ort.h
> -        */
> -       merge_bases = get_merge_bases(parent1, parent2);
> +       if (o->merge_base) {
> +               struct commit *c = lookup_commit_reference_by_name(o->merge_base);
> +               if (!c)
> +                       die(_("could not lookup commit %s"), o->merge_base);

Would it make sense to have o->merge_base be a struct commit *, and
move this lookup/die logic out to the parsing logic in
cmd_merge_tree()?

Just a thought.

> +               commit_list_insert(c, &merge_bases);
> +       } else {
> +               /*
> +                * Get the merge bases, in reverse order; see comment above
> +                * merge_incore_recursive in merge-ort.h
> +                */
> +               merge_bases = get_merge_bases(parent1, parent2);
> +       }
>         if (!merge_bases && !o->allow_unrelated_histories)
>                 die(_("refusing to merge unrelated histories"));
>         merge_bases = reverse_commit_list(merge_bases);

and the next line of code, not showing in the context here, is a call
to merge_incore_recursive().  While that technically works, it doesn't
make logical sense.  You're not doing a recursive merge when you have
a specified merge base, so I think the code should instead call
merge_incore_nonrecursive() in such a case (see Dscho's code for a
comparison here).

> @@ -505,6 +514,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
>                            &o.allow_unrelated_histories,
>                            N_("allow merging unrelated histories"),
>                            PARSE_OPT_NONEG),
> +               OPT_STRING(0, "merge-base",
> +                        &o.merge_base,
> +                        N_("commit"),
> +                        N_("specify a merge-base commit for the merge")),
>                 OPT_END()
>         };
>
> diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
> index 013b77144bd..032ff4a1c3d 100755
> --- a/t/t4301-merge-tree-write-tree.sh
> +++ b/t/t4301-merge-tree-write-tree.sh
> @@ -819,4 +819,48 @@ test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository
>         test_must_fail git -C read-only merge-tree side1 side2
>  '
>
> +# specify merge-base as parent of branch2.
> +# git merge-tree --merge-base=A O B
> +#   Commit O: foo, bar
> +#   Commit A: modify foo after Commit O
> +#   Commit B: modify bar after Commit A
> +#   Expected: foo is unchanged, modify bar

So, the O-A-B thing thing was taken from t6423; it appears I didn't
copy the comment at the top of t6423 over to t4301 to explain this.
Sorry about that.  Anyway, for these types of comments in these files,
O is always the merge base of both A and B, and neither A nor B
contain each other in their history.  From that basis, your
description here makes no sense (A would be the tip of some branch,
not the parent of anything).  I'd call your commits something else
(maybe just C1, C2, and C3 or something?).

> +
> +test_expect_success 'specify merge-base as parent of branch2' '
> +       # Setup
> +       git init base-b2-p && (
> +               cd base-b2-p &&
> +               echo foo >foo &&
> +               echo bar >bar &&
> +               git add foo bar &&
> +               git commit -m O &&
> +
> +               git branch O &&
> +               git branch A &&
> +
> +               git checkout A &&
> +               echo "A" >foo &&
> +               git add foo &&
> +               git commit -m A &&
> +
> +               git checkout -b B &&
> +               echo "B" >bar &&
> +               git add bar &&
> +               git commit -m B

Would test_commit be useful here, given that you aren't worried about
the exact contents of files?

> +       ) &&
> +       # Testing
> +       (
> +               cd base-b2-p &&
> +               TREE_OID=$(git merge-tree --merge-base=A O B) &&
> +
> +               q_to_tab <<-EOF >expect &&
> +               100644 blob $(git rev-parse B:bar)Qbar
> +               100644 blob $(git rev-parse O:foo)Qfoo
> +               EOF
> +
> +               git ls-tree $TREE_OID >actual &&
> +               test_cmp expect actual
> +       )
> +'
> +
>  test_done
>
> base-commit: db29e6bbaed156ae9025ff0474fd788e58230367
> --
> gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-27 18:09 ` Junio C Hamano
@ 2022-10-28  8:20   ` Elijah Newren
  2022-10-28 16:03     ` Junio C Hamano
  2022-10-28 11:43   ` [Internet]Re: " kylezhao(赵柯宇)
  1 sibling, 1 reply; 43+ messages in thread
From: Elijah Newren @ 2022-10-28  8:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Kyle Zhao via GitGitGadget, git, Kyle Zhao

On Thu, Oct 27, 2022 at 11:09 AM Junio C Hamano <gitster@pobox.com> wrote:
>
[...]
> Wouldn't it be sufficient to update the UI to
>
>     git merge-tree [--write-tree] [<options>] [<base-commit>] <branch1> <branch2>
>     git merge-tree [--trivial-merge] <base-commit> <branch1> <branch2>
>
> IOW, when you want to supply the base, you'd be explicit and ask for
> the new "write-tree" mode, i.e.
>
>     $ git merge-tree --write-tree $(git merge-base branch^ branch) HEAD branch
>
> would be how you would use merge-tree to cherry-pick the commit at
> the tip of the branch on top of the current commit.

This was my thought too; but would we be painting ourselves into a
corner if we ever want to make merge-tree support octopus merges?

Also, why did you write $(git merge-base branch^ branch) rather than
just branch^ ?

> I also have to wonder how this should interact with a topic that is
> in-flight to feed multiple merge-tree requests from the standard
> input to have a single process perform multiple (not necessarily
> related) merges.  Elijah knows much better, but my gut feeling is
> that it shouldn't be hard to allow feeding an extra commit on the
> same line to be used as the base.

Yeah, I don't think that'd be too hard...if we could rule out ever
supporting octopus merges in merge-tree (which I'm not so sure is a
good assumption).  Otherwise, we might need to figure out the
appropriate backward-compatible input parsing (and output format
changes?)

^ permalink raw reply	[flat|nested] 43+ messages in thread

* RE: [Internet]Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-27 18:09 ` Junio C Hamano
  2022-10-28  8:20   ` Elijah Newren
@ 2022-10-28 11:43   ` kylezhao(赵柯宇)
  1 sibling, 0 replies; 43+ messages in thread
From: kylezhao(赵柯宇) @ 2022-10-28 11:43 UTC (permalink / raw)
  To: Junio C Hamano, Kyle Zhao via GitGitGadget; +Cc: git@vger.kernel.org

Thanks for your reviews.

> In the original "trivial merge" mode, the command takes three trees
> without having to have this new option.  In the new "write-tree"
> mode, currently it is incapable of taking the base, but it does not
> have to stay that way.  Wouldn't it be sufficient to update the UI
> to
>
>     git merge-tree [--write-tree] [<options>] [<base-commit>] <branch1> <branch2>
>     git merge-tree [--trivial-merge] <base-commit> <branch1> <branch2>
>
> IOW, when you want to supply the base, you'd be explicit and ask for
> the new "write-tree" mode, i.e.
>
>     $ git merge-tree --write-tree $(git merge-base branch^ branch) HEAD branch 
>
> would be how you would use merge-tree to cherry-pick the commit at
> the tip of the branch on top of the current commit.

Referring to Newren's reply, if we need to implement octopus merges for git-merge-tree in the future,  still need a new option, 
so I haven't modified it yet.

>> @@ -402,6 +403,7 @@ struct merge_tree_options {
>>  	int allow_unrelated_histories;
>>  	int show_messages;
>>  	int name_only;
>> +	char* merge_base;
>
> Style.  We write in C, not in C++, and our asterisks stick to variables and members of structs, not types.

Done.



^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [Internet]Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-28  8:13 ` Elijah Newren
@ 2022-10-28 11:54   ` kylezhao(赵柯宇)
  0 siblings, 0 replies; 43+ messages in thread
From: kylezhao(赵柯宇) @ 2022-10-28 11:54 UTC (permalink / raw)
  To: Elijah Newren, Kyle Zhao via GitGitGadget; +Cc: git@vger.kernel.org

> Note that this has been implemented previously.  I may have implemented it previously and ripped it out (about a year ago), or maybe I just avoided it once I ran across the reasons below.  Dscho also implemented it over at https://lore.kernel.org/git/nycvar.QRO.7.76.6.2202210956430.26495@tvgsbejvaqbjf.bet/
>
> You may want to take a look at his code for comparison, especially the
merge_incore_nonrecursive() stuff.

Thanks. I'll take a look later.

> This capability is something we may ultimately want for completeness,
> but it definitely should not be marketed as a building block for
> implementing cherry-pick or rebase.  There are several reasons for
> that:
> 
> Performance reasons:
>  * it's a design with a separate fork for every commit to be picked,
> ....
> Maintenance reason:
>   * suggesting this code as a basis for cherry-pick or rebase is
> likely to lead to yet another shell-scripted rebase; we've been trying
> to generally nuke shell scripts from Git for a variety of reasons.
> It'd be sad to regress here.

Thank you for your very detailed explanation, I feel like I have a deeper understanding of rebase and cherry-pick.

> My apologies for having very limited git time (which is often entirely used up just in reviewing/responding to patches and other queries on the list instead of on new features like this, or maybe on making some nice slides for a conference); if I had more time, I think git-replay could have easily been done many months ago (or perhaps even last year).  Then you'd have the high level tool you need for server side cherry-picking and rebasing.  But, I haven't had much time.  So, it makes sense that folks might want to use an interim hack while waiting for the more robust tool to materialize.

git-replay sounds great, looking forward to it one day.

> Would it make sense to have o->merge_base be a struct commit *, and move this lookup/die logic out to the parsing logic in cmd_merge_tree()?
Done.

> and the next line of code, not showing in the context here, is a call
> to merge_incore_recursive().  While that technically works, it doesn't
> make logical sense.  You're not doing a recursive merge when you have
> a specified merge base, so I think the code should instead call
> merge_incore_nonrecursive() in such a case (see Dscho's code for a
> comparison here).
Done. I've changed it to merge_incore_recursive(). It works,  but I don't know if it's written correctly, especially opt.ancestor.

> So, the O-A-B thing thing was taken from t6423; it appears I didn't copy the comment at the top of t6423 over to t4301 to explain this.
> Sorry about that.  Anyway, for these types of comments in these files, O is always the merge base of both A and B, and neither A nor B contain each other in their history.  From that basis, your description here makes no sense (A would be the tip of some branch, not the parent of anything).  I'd call your commits something else (maybe just C1, C2, and C3 or something?).

Done.

> Would test_commit be useful here, given that you aren't worried about
> the exact contents of files?

It's useful, which makes the test clearer.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v2] merge-tree.c: add --merge-base=<commit> option
  2022-10-27 12:12 [PATCH] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-10-27 18:09 ` Junio C Hamano
  2022-10-28  8:13 ` Elijah Newren
@ 2022-10-28 11:55 ` Kyle Zhao via GitGitGadget
  2022-10-29  1:32   ` Elijah Newren
  2022-10-29  3:42   ` [PATCH v3] " Kyle Zhao via GitGitGadget
  2 siblings, 2 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-10-28 11:55 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

It would cherry-pick the commit at the tip of the branch on top of the
current commit even if the repository is bare.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
    merge-tree: allow specifying a base commit when --write-tree is passed
    
    Thanks for Elijah's work. I'm very excited that merge-ort is integrated
    into the git merge-tree, which means that we can use merge-ort in bare
    repositories to optimize merge performance.
    
    In this patch, I introduce a new --merge-base=<commit> option to allow
    callers to specify a merge-base for the merge. This may allow users to
    implement git cherry-pick and git rebase in bare repositories with git
    merge-tree cmd.
    
    Changes since v1:
    
     * Changed merge_incore_nonrecursive() to merge_incore_recursive() when
       merge-base is specified.
     * Fixed c style problem.
     * Moved commit lookup/die logic out to the parsing logic in
       cmd_merge_tree().
     * use test_commit for test

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v1:

 1:  965e544c849 ! 1:  ab4e5d5ad08 merge-tree.c: add --merge-base=<commit> option
     @@ Metadata
       ## Commit message ##
          merge-tree.c: add --merge-base=<commit> option
      
     -    This option allows users to specify a merge-base commit for the merge.
     +    This patch will give our callers more flexibility to use `git merge-tree`,
     +    such as:
      
     -    It will give our callers more flexibility to use the `git merge-tree`.
     -    For example:
     +        git merge-tree --write-tree --merge-base=branch^ HEAD branch
      
     -        git merge-tree --merge-base=<sha1>^1 source-branch <sha1>
     -
     -    This allows us to implement `git cherry-pick` in bare repositories.
     +    It would cherry-pick the commit at the tip of the branch on top of the
     +    current commit even if the repository is bare.
      
          Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
      
     @@ builtin/merge-tree.c: struct merge_tree_options {
       	int allow_unrelated_histories;
       	int show_messages;
       	int name_only;
     -+	char* merge_base;
     ++	const struct commit *base_commit;
       };
       
       static int real_merge(struct merge_tree_options *o,
     @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
      -	 * merge_incore_recursive in merge-ort.h
      -	 */
      -	merge_bases = get_merge_bases(parent1, parent2);
     -+	if (o->merge_base) {
     -+		struct commit *c = lookup_commit_reference_by_name(o->merge_base);
     -+		if (!c)
     -+			die(_("could not lookup commit %s"), o->merge_base);
     -+		commit_list_insert(c, &merge_bases);
     +-	if (!merge_bases && !o->allow_unrelated_histories)
     +-		die(_("refusing to merge unrelated histories"));
     +-	merge_bases = reverse_commit_list(merge_bases);
     ++	if (o->base_commit) {
     ++		struct tree *base_tree, *parent1_tree, *parent2_tree;
     ++
     ++		opt.ancestor = "specified merge base";
     ++		base_tree = get_commit_tree(o->base_commit);
     ++		parent1_tree = get_commit_tree(parent1);
     ++		parent2_tree = get_commit_tree(parent2);
     ++		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
      +	} else {
      +		/*
      +		 * Get the merge bases, in reverse order; see comment above
      +		 * merge_incore_recursive in merge-ort.h
      +		 */
      +		merge_bases = get_merge_bases(parent1, parent2);
     ++		if (!merge_bases && !o->allow_unrelated_histories)
     ++			die(_("refusing to merge unrelated histories"));
     ++		merge_bases = reverse_commit_list(merge_bases);
     ++		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
      +	}
     - 	if (!merge_bases && !o->allow_unrelated_histories)
     - 		die(_("refusing to merge unrelated histories"));
     - 	merge_bases = reverse_commit_list(merge_bases);
     + 
     +-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
     + 	if (result.clean < 0)
     + 		die(_("failure to merge"));
     + 
     +@@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
     + 
     + int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     + {
     ++	const char *merge_base = NULL;
     + 	struct merge_tree_options o = { .show_messages = -1 };
     + 	int expected_remaining_argc;
     + 	int original_argc;
      @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
       			   &o.allow_unrelated_histories,
       			   N_("allow merging unrelated histories"),
       			   PARSE_OPT_NONEG),
      +		OPT_STRING(0, "merge-base",
     -+			 &o.merge_base,
     ++			 &merge_base,
      +			 N_("commit"),
     -+			 N_("specify a merge-base commit for the merge")),
     ++			 N_("specify a merge-base for the merge")),
       		OPT_END()
       	};
       
     +@@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     + 		usage_with_options(merge_tree_usage, mt_options);
     + 
     + 	/* Do the relevant type of merge */
     +-	if (o.mode == MODE_REAL)
     ++	if (o.mode == MODE_REAL) {
     ++		if (merge_base) {
     ++			o.base_commit = lookup_commit_reference_by_name(merge_base);
     ++			if (!o.base_commit)
     ++				die(_("could not lookup commit %s"), merge_base);
     ++		}
     + 		return real_merge(&o, argv[0], argv[1], prefix);
     ++	}
     + 	else
     + 		return trivial_merge(argv[0], argv[1], argv[2]);
     + }
      
       ## t/t4301-merge-tree-write-tree.sh ##
      @@ t/t4301-merge-tree-write-tree.sh: test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository
       	test_must_fail git -C read-only merge-tree side1 side2
       '
       
     -+# specify merge-base as parent of branch2.
     -+# git merge-tree --merge-base=A O B
     -+#   Commit O: foo, bar
     -+#   Commit A: modify foo after Commit O
     -+#   Commit B: modify bar after Commit A
     -+#   Expected: foo is unchanged, modify bar
     -+
      +test_expect_success 'specify merge-base as parent of branch2' '
      +	# Setup
      +	git init base-b2-p && (
      +		cd base-b2-p &&
     -+		echo foo >foo &&
     -+		echo bar >bar &&
     -+		git add foo bar &&
     -+		git commit -m O &&
     -+
     -+		git branch O &&
     -+		git branch A &&
     -+
     -+		git checkout A &&
     -+		echo "A" >foo &&
     -+		git add foo &&
     -+		git commit -m A &&
     -+
     -+		git checkout -b B &&
     -+		echo "B" >bar &&
     -+		git add bar &&
     -+		git commit -m B
     ++		test_commit c1 file1 &&
     ++		test_commit c2 file2 &&
     ++		test_commit c3 file3
      +	) &&
      +	# Testing
      +	(
      +		cd base-b2-p &&
     -+		TREE_OID=$(git merge-tree --merge-base=A O B) &&
     ++		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
      +
      +		q_to_tab <<-EOF >expect &&
     -+		100644 blob $(git rev-parse B:bar)Qbar
     -+		100644 blob $(git rev-parse O:foo)Qfoo
     ++		100644 blob $(git rev-parse c1:file1)Qfile1
     ++		100644 blob $(git rev-parse c3:file3)Qfile3
      +		EOF
      +
      +		git ls-tree $TREE_OID >actual &&


 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 43 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 23 +++++++++++++++++
 3 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d6c356740ef..e762209b76d 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index ae5782917b9..089ea8fac81 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -402,6 +403,7 @@ struct merge_tree_options {
 	int allow_unrelated_histories;
 	int show_messages;
 	int name_only;
+	const struct commit *base_commit;
 };
 
 static int real_merge(struct merge_tree_options *o,
@@ -430,16 +432,26 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (o->base_commit) {
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		opt.ancestor = "specified merge base";
+		base_tree = get_commit_tree(o->base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -478,6 +490,7 @@ static int real_merge(struct merge_tree_options *o,
 
 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 {
+	const char *merge_base = NULL;
 	struct merge_tree_options o = { .show_messages = -1 };
 	int expected_remaining_argc;
 	int original_argc;
@@ -505,6 +518,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.allow_unrelated_histories,
 			   N_("allow merging unrelated histories"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			 &merge_base,
+			 N_("commit"),
+			 N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
@@ -544,8 +561,14 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 		usage_with_options(merge_tree_usage, mt_options);
 
 	/* Do the relevant type of merge */
-	if (o.mode == MODE_REAL)
+	if (o.mode == MODE_REAL) {
+		if (merge_base) {
+			o.base_commit = lookup_commit_reference_by_name(merge_base);
+			if (!o.base_commit)
+				die(_("could not lookup commit %s"), merge_base);
+		}
 		return real_merge(&o, argv[0], argv[1], prefix);
+	}
 	else
 		return trivial_merge(argv[0], argv[1], argv[2]);
 }
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 013b77144bd..64bfe6f4a41 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -819,4 +819,27 @@ test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository
 	test_must_fail git -C read-only merge-tree side1 side2
 '
 
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	git init base-b2-p && (
+		cd base-b2-p &&
+		test_commit c1 file1 &&
+		test_commit c2 file2 &&
+		test_commit c3 file3
+	) &&
+	# Testing
+	(
+		cd base-b2-p &&
+		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+		q_to_tab <<-EOF >expect &&
+		100644 blob $(git rev-parse c1:file1)Qfile1
+		100644 blob $(git rev-parse c3:file3)Qfile3
+		EOF
+
+		git ls-tree $TREE_OID >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done

base-commit: 5af5e54106e20f65c913550c80aec3186b859e9b
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-28  8:20   ` Elijah Newren
@ 2022-10-28 16:03     ` Junio C Hamano
  2022-10-29  1:52       ` Elijah Newren
  0 siblings, 1 reply; 43+ messages in thread
From: Junio C Hamano @ 2022-10-28 16:03 UTC (permalink / raw)
  To: Elijah Newren; +Cc: Kyle Zhao via GitGitGadget, git, Kyle Zhao

Elijah Newren <newren@gmail.com> writes:

> On Thu, Oct 27, 2022 at 11:09 AM Junio C Hamano <gitster@pobox.com> wrote:
>>
> [...]
>> Wouldn't it be sufficient to update the UI to
>>
>>     git merge-tree [--write-tree] [<options>] [<base-commit>] <branch1> <branch2>
>>     git merge-tree [--trivial-merge] <base-commit> <branch1> <branch2>
>>
>> IOW, when you want to supply the base, you'd be explicit and ask for
>> the new "write-tree" mode, i.e.
>>
>>     $ git merge-tree --write-tree $(git merge-base branch^ branch) HEAD branch
>>
>> would be how you would use merge-tree to cherry-pick the commit at
>> the tip of the branch on top of the current commit.
>
> This was my thought too; but would we be painting ourselves into a
> corner if we ever want to make merge-tree support octopus merges?

True, the above suggestion was especially bad as it would not be
possible to extend to support multiple bases *and* octopus at the
same time.  Consider it scrapped.  Taking zero or more --base-commit
options explicitly from the command line would be a better
interface.

> Also, why did you write $(git merge-base branch^ branch) rather than
> just branch^ ?

Just to be explicit which one is doing what.

> Yeah, I don't think that'd be too hard...if we could rule out ever
> supporting octopus merges in merge-tree (which I'm not so sure is a
> good assumption).  Otherwise, we might need to figure out the
> appropriate backward-compatible input parsing (and output format
> changes?)

I'd prefer an approach to tackle one thing at a time while making
sure we leave the door open for the future ones.  I think the
backend interface from "merge" to external strategies use a
separator to signal the boundary between the heads being merged
(i.e. branchN above) and the bases being used, which is easy to
parse and support multiple bases and octopus at the same time.

As to making it easy to implement "cherry-pick", I do not think you
should dogmatically forbid it on the basis for merge-tree from the
command line is inherently one mergy operation per invocation.  You
will have the --stdin interface, and a way forward may be a notation
to refer to previous results in the --stdin input stream.  That way,
a single invocation of merge-tree "server" can be fed a sequence of
3-way merges that are actually steps of multi-commit cherry-pick,
that merge the differences of multiple commits on top of the
previous result, one step at a time.

IOW, as long as you make sure --stdin interface is rich enough to
allow you do what people would do a sequence of "git merge-tree"
single shot invocations (perhaps even driven by a script), you would
be OK, I would think.


^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH v2] merge-tree.c: add --merge-base=<commit> option
  2022-10-28 11:55 ` [PATCH v2] " Kyle Zhao via GitGitGadget
@ 2022-10-29  1:32   ` Elijah Newren
  2022-10-29  3:42   ` [PATCH v3] " Kyle Zhao via GitGitGadget
  1 sibling, 0 replies; 43+ messages in thread
From: Elijah Newren @ 2022-10-29  1:32 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Kyle Zhao

Hi,

On Fri, Oct 28, 2022 at 4:55 AM Kyle Zhao via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Kyle Zhao <kylezhao@tencent.com>
>
> This patch will give our callers more flexibility to use `git merge-tree`,
> such as:
>
>     git merge-tree --write-tree --merge-base=branch^ HEAD branch
>
> It would cherry-pick the commit at the tip of the branch on top of the
> current commit even if the repository is bare.

Perhaps just "This does a merge of HEAD and branch, but uses branch^
as the merge-base."

Also, given that both Junio and I thought a positional argument might
be better, it might be worth calling out that the reason for using an
option flag instead of a positional argument is to allow additional
commits passed to merge-tree to be handled via an octopus merge in the
future.

> Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
> ---
>     merge-tree: allow specifying a base commit when --write-tree is passed
>
>     Thanks for Elijah's work. I'm very excited that merge-ort is integrated
>     into the git merge-tree, which means that we can use merge-ort in bare
>     repositories to optimize merge performance.
>
>     In this patch, I introduce a new --merge-base=<commit> option to allow
>     callers to specify a merge-base for the merge. This may allow users to
>     implement git cherry-pick and git rebase in bare repositories with git
>     merge-tree cmd.
>
>     Changes since v1:
>
>      * Changed merge_incore_nonrecursive() to merge_incore_recursive() when
>        merge-base is specified.

I think you mean the other way around (using
merge_incore_nonrecursive() instead of merge_incore_recursive() when
merge-base is specified).

> +       if (o->base_commit) {
> +               struct tree *base_tree, *parent1_tree, *parent2_tree;
> +
> +               opt.ancestor = "specified merge base";

It is a specified merge base, but that won't help the user much when
they get conflict markers if they attempt to investigate them.  Since
the specified merge base is a commit the user will know, and in fact
one they named on the command line, we should use that name.  So, I'd
expect this to be set to o->merge_base.

> @@ -544,8 +561,14 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
>                 usage_with_options(merge_tree_usage, mt_options);
>
>         /* Do the relevant type of merge */
> -       if (o.mode == MODE_REAL)
> +       if (o.mode == MODE_REAL) {
> +               if (merge_base) {
> +                       o.base_commit = lookup_commit_reference_by_name(merge_base);
> +                       if (!o.base_commit)
> +                               die(_("could not lookup commit %s"), merge_base);
> +               }

Personally, I think of the code before "/* Do the relevant type of
merge */" as a continuation of option parsing (i.e. sanity checking
arguments and determining defaults and whatnot), and I think the last
five lines above are more option parsing.  From that angle, I think
it'd make sense to move these lines out above this block (before or
after determining o.mode).

But this may well just be personal preference; what you have certainly works.

>                 return real_merge(&o, argv[0], argv[1], prefix);
> +       }
>         else
>                 return trivial_merge(argv[0], argv[1], argv[2]);
>  }
> diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
> index 013b77144bd..64bfe6f4a41 100755
> --- a/t/t4301-merge-tree-write-tree.sh
> +++ b/t/t4301-merge-tree-write-tree.sh
> @@ -819,4 +819,27 @@ test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository
>         test_must_fail git -C read-only merge-tree side1 side2
>  '
>
> +test_expect_success 'specify merge-base as parent of branch2' '
> +       # Setup
> +       git init base-b2-p && (
> +               cd base-b2-p &&
> +               test_commit c1 file1 &&
> +               test_commit c2 file2 &&
> +               test_commit c3 file3

Much simpler.  :-)

> +       ) &&
> +       # Testing
> +       (
> +               cd base-b2-p &&
> +               TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
> +
> +               q_to_tab <<-EOF >expect &&
> +               100644 blob $(git rev-parse c1:file1)Qfile1
> +               100644 blob $(git rev-parse c3:file3)Qfile3
> +               EOF
> +
> +               git ls-tree $TREE_OID >actual &&
> +               test_cmp expect actual

In particular, you are testing here that file2 does NOT appear despite
the fact that it was part of c3.  That makes sense, but I'm not sure
casual readers of this code would catch that.  It might be worth
adding a comment to make it easier to follow for future test readers.

> +       )
> +'
> +
>  test_done
>
> base-commit: 5af5e54106e20f65c913550c80aec3186b859e9b

This should be rebased on top of en/merge-tree-sequence.  Then, you
need to either figure out how to modify the new --stdin flag to also
accept a specified merge base in a way that permits future handling of
octopus merges (it looks like Junio might have a good suggestion
elsewhere in this thread), or else explicitly call out in the commit
message why specified merge base support is only being added when
--stdin is not specified.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH] merge-tree.c: add --merge-base=<commit> option
  2022-10-28 16:03     ` Junio C Hamano
@ 2022-10-29  1:52       ` Elijah Newren
  0 siblings, 0 replies; 43+ messages in thread
From: Elijah Newren @ 2022-10-29  1:52 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Kyle Zhao via GitGitGadget, git, Kyle Zhao

On Fri, Oct 28, 2022 at 9:03 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> > Yeah, I don't think that'd be too hard...if we could rule out ever
> > supporting octopus merges in merge-tree (which I'm not so sure is a
> > good assumption).  Otherwise, we might need to figure out the
> > appropriate backward-compatible input parsing (and output format
> > changes?)
>
> I'd prefer an approach to tackle one thing at a time while making
> sure we leave the door open for the future ones.  I think the
> backend interface from "merge" to external strategies use a
> separator to signal the boundary between the heads being merged
> (i.e. branchN above) and the bases being used, which is easy to
> parse and support multiple bases and octopus at the same time.

Ooh, the separator idea sounds like a good solution.

> As to making it easy to implement "cherry-pick", I do not think you
> should dogmatically forbid it on the basis for merge-tree from the
> command line is inherently one mergy operation per invocation.  You
> will have the --stdin interface, and a way forward may be a notation
> to refer to previous results in the --stdin input stream.  That way,
> a single invocation of merge-tree "server" can be fed a sequence of
> 3-way merges that are actually steps of multi-commit cherry-pick,
> that merge the differences of multiple commits on top of the
> previous result, one step at a time.
>
> IOW, as long as you make sure --stdin interface is rich enough to
> allow you do what people would do a sequence of "git merge-tree"
> single shot invocations (perhaps even driven by a script), you would
> be OK, I would think.

I agree that this kind of functionality probably makes sense for
inclusion from a completeness perspective, but I still maintain that
if its sole purpose is implementing a cherry-pick command then it's at
best an ill-advised or interim hack and we should instead do something
better.  While combining this patch with --stdin does solve the
"single operation per invocation" issue, that's only one of six issues
I listed in my email to Kyle about why this is the wrong base for
building a cherry-pick alternative.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v3] merge-tree.c: add --merge-base=<commit> option
  2022-10-28 11:55 ` [PATCH v2] " Kyle Zhao via GitGitGadget
  2022-10-29  1:32   ` Elijah Newren
@ 2022-10-29  3:42   ` Kyle Zhao via GitGitGadget
  2022-11-01  1:08     ` Elijah Newren
  2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
  1 sibling, 2 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-10-29  3:42 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

This does a merge of HEAD and branch, but uses branch^ as the merge-base.

And the reason why using an option flag instead of a positional argument
is to allow additional commits passed to merge-tree to be handled via an
octopus merge in the future.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
    merge-tree: allow specifying a base commit when --write-tree is passed
    
    Thanks for Elijah's work. I'm very excited that merge-ort is integrated
    into the git merge-tree, which means that we can use merge-ort in bare
    repositories to optimize merge performance.
    
    In this patch, I introduce a new --merge-base=<commit> option to allow
    callers to specify a merge-base for the merge. This may allow users to
    implement git cherry-pick and git rebase in bare repositories with git
    merge-tree cmd.
    
    Changes since v1:
    
     * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
       merge-base is specified.
     * Fixed c style problem.
     * Moved commit lookup/die logic out to the parsing logic in
       cmd_merge_tree().
     * use test_commit for test
    
    Changes since v2:
    
     * commit message
     * Rebased on top of en/merge-tree-sequence.
     * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char.
       To make it easier to pass parameters, I moved
       lookup_commit_reference_by_name() to real_ merge() again.
     * Added test comment.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v2:

 1:  ab4e5d5ad08 ! 1:  554cbde49e8 merge-tree.c: add --merge-base=<commit> option
     @@ Commit message
      
              git merge-tree --write-tree --merge-base=branch^ HEAD branch
      
     -    It would cherry-pick the commit at the tip of the branch on top of the
     -    current commit even if the repository is bare.
     +    This does a merge of HEAD and branch, but uses branch^ as the merge-base.
     +
     +    And the reason why using an option flag instead of a positional argument
     +    is to allow additional commits passed to merge-tree to be handled via an
     +    octopus merge in the future.
      
          Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
      
     @@ builtin/merge-tree.c
       #include "merge-ort.h"
       #include "object-store.h"
      @@ builtin/merge-tree.c: struct merge_tree_options {
     - 	int allow_unrelated_histories;
       	int show_messages;
       	int name_only;
     -+	const struct commit *base_commit;
     + 	int use_stdin;
     ++	const char *merge_base;
       };
       
       static int real_merge(struct merge_tree_options *o,
     @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
      -	if (!merge_bases && !o->allow_unrelated_histories)
      -		die(_("refusing to merge unrelated histories"));
      -	merge_bases = reverse_commit_list(merge_bases);
     -+	if (o->base_commit) {
     ++	if (o->merge_base) {
     ++		struct commit *base_commit;
      +		struct tree *base_tree, *parent1_tree, *parent2_tree;
      +
     -+		opt.ancestor = "specified merge base";
     -+		base_tree = get_commit_tree(o->base_commit);
     ++		base_commit = lookup_commit_reference_by_name(o->merge_base);
     ++		if (!base_commit)
     ++			die(_("could not lookup commit %s"), o->merge_base);
     ++
     ++		opt.ancestor = o->merge_base;
     ++		base_tree = get_commit_tree(base_commit);
      +		parent1_tree = get_commit_tree(parent1);
      +		parent2_tree = get_commit_tree(parent2);
      +		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
     @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
       	if (result.clean < 0)
       		die(_("failure to merge"));
       
     -@@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
     - 
     - int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     - {
     -+	const char *merge_base = NULL;
     - 	struct merge_tree_options o = { .show_messages = -1 };
     - 	int expected_remaining_argc;
     - 	int original_argc;
      @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     - 			   &o.allow_unrelated_histories,
     - 			   N_("allow merging unrelated histories"),
     + 			   &o.use_stdin,
     + 			   N_("perform multiple merges, one per line of input"),
       			   PARSE_OPT_NONEG),
      +		OPT_STRING(0, "merge-base",
     -+			 &merge_base,
     -+			 N_("commit"),
     -+			 N_("specify a merge-base for the merge")),
     ++			   &o.merge_base,
     ++			   N_("commit"),
     ++			   N_("specify a merge-base for the merge")),
       		OPT_END()
       	};
       
     -@@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     - 		usage_with_options(merge_tree_usage, mt_options);
     - 
     - 	/* Do the relevant type of merge */
     --	if (o.mode == MODE_REAL)
     -+	if (o.mode == MODE_REAL) {
     -+		if (merge_base) {
     -+			o.base_commit = lookup_commit_reference_by_name(merge_base);
     -+			if (!o.base_commit)
     -+				die(_("could not lookup commit %s"), merge_base);
     -+		}
     - 		return real_merge(&o, argv[0], argv[1], prefix);
     -+	}
     - 	else
     - 		return trivial_merge(argv[0], argv[1], argv[2]);
     - }
      
       ## t/t4301-merge-tree-write-tree.sh ##
     -@@ t/t4301-merge-tree-write-tree.sh: test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository
     - 	test_must_fail git -C read-only merge-tree side1 side2
     +@@ t/t4301-merge-tree-write-tree.sh: test_expect_success '--stdin with both a successful and a conflicted merge' '
     + 	test_cmp expect actual
       '
       
     ++# specify merge-base as parent of branch2
     ++# git merge-tree --write-tree --merge-base=c2 c1 c3
     ++#   Commit c1: add file1
     ++#   Commit c2: add file2 after c1
     ++#   Commit c3: add file3 after c2
     ++#   Expected: add file3, and file2 does NOT appear
     ++
      +test_expect_success 'specify merge-base as parent of branch2' '
      +	# Setup
      +	git init base-b2-p && (


 Documentation/git-merge-tree.txt |  4 ++++
 builtin/merge-tree.c             | 39 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 30 ++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 04bcc416e6e..d9dacb2ce54 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..cd25aac6ba6 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -403,6 +404,7 @@ struct merge_tree_options {
 	int show_messages;
 	int name_only;
 	int use_stdin;
+	const char *merge_base;
 };
 
 static int real_merge(struct merge_tree_options *o,
@@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (o->merge_base) {
+		struct commit *base_commit;
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		base_commit = lookup_commit_reference_by_name(o->merge_base);
+		if (!base_commit)
+			die(_("could not lookup commit %s"), o->merge_base);
+
+		opt.ancestor = o->merge_base;
+		base_tree = get_commit_tree(base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -515,6 +532,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.use_stdin,
 			   N_("perform multiple merges, one per line of input"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			   &o.merge_base,
+			   N_("commit"),
+			   N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index cac85591b52..5b0073d3dcd 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,4 +860,34 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+#   Commit c1: add file1
+#   Commit c2: add file2 after c1
+#   Commit c3: add file3 after c2
+#   Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	git init base-b2-p && (
+		cd base-b2-p &&
+		test_commit c1 file1 &&
+		test_commit c2 file2 &&
+		test_commit c3 file3
+	) &&
+	# Testing
+	(
+		cd base-b2-p &&
+		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+		q_to_tab <<-EOF >expect &&
+		100644 blob $(git rev-parse c1:file1)Qfile1
+		100644 blob $(git rev-parse c3:file3)Qfile3
+		EOF
+
+		git ls-tree $TREE_OID >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done

base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH v3] merge-tree.c: add --merge-base=<commit> option
  2022-10-29  3:42   ` [PATCH v3] " Kyle Zhao via GitGitGadget
@ 2022-11-01  1:08     ` Elijah Newren
  2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
  1 sibling, 0 replies; 43+ messages in thread
From: Elijah Newren @ 2022-11-01  1:08 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Kyle Zhao

Hi,

On Fri, Oct 28, 2022 at 8:42 PM Kyle Zhao via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Kyle Zhao <kylezhao@tencent.com>
>
> This patch will give our callers more flexibility to use `git merge-tree`,
> such as:
>
>     git merge-tree --write-tree --merge-base=branch^ HEAD branch
>
> This does a merge of HEAD and branch, but uses branch^ as the merge-base.
>
> And the reason why using an option flag instead of a positional argument
> is to allow additional commits passed to merge-tree to be handled via an
> octopus merge in the future.
>
> Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
> ---
>     merge-tree: allow specifying a base commit when --write-tree is passed
>
>     Thanks for Elijah's work. I'm very excited that merge-ort is integrated
>     into the git merge-tree, which means that we can use merge-ort in bare
>     repositories to optimize merge performance.
>
>     In this patch, I introduce a new --merge-base=<commit> option to allow
>     callers to specify a merge-base for the merge. This may allow users to
>     implement git cherry-pick and git rebase in bare repositories with git
>     merge-tree cmd.
>
>     Changes since v1:
>
>      * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
>        merge-base is specified.
>      * Fixed c style problem.
>      * Moved commit lookup/die logic out to the parsing logic in
>        cmd_merge_tree().
>      * use test_commit for test
>
>     Changes since v2:
>
>      * commit message
>      * Rebased on top of en/merge-tree-sequence.
>      * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char.
>        To make it easier to pass parameters, I moved
>        lookup_commit_reference_by_name() to real_ merge() again.
>      * Added test comment.
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v3
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v3
> Pull-Request: https://github.com/gitgitgadget/git/pull/1397
>
[...]
>  Documentation/git-merge-tree.txt |  4 ++++
>  builtin/merge-tree.c             | 39 ++++++++++++++++++++++++--------
>  t/t4301-merge-tree-write-tree.sh | 30 ++++++++++++++++++++++++
>  3 files changed, 64 insertions(+), 9 deletions(-)
>
> diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
> index 04bcc416e6e..d9dacb2ce54 100644
> --- a/Documentation/git-merge-tree.txt
> +++ b/Documentation/git-merge-tree.txt
> @@ -64,6 +64,10 @@ OPTIONS
>         share no common history.  This flag can be given to override that
>         check and make the merge proceed anyway.
>
> +--merge-base=<commit>::
> +       Instead of finding the merge-bases for <branch1> and <branch2>,
> +       specify a merge-base for the merge.
> +
>  [[OUTPUT]]
>  OUTPUT
>  ------
> diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
> index fe853aa8f91..cd25aac6ba6 100644
> --- a/builtin/merge-tree.c
> +++ b/builtin/merge-tree.c
> @@ -3,6 +3,7 @@
>  #include "tree-walk.h"
>  #include "xdiff-interface.h"
>  #include "help.h"
> +#include "commit.h"
>  #include "commit-reach.h"
>  #include "merge-ort.h"
>  #include "object-store.h"
> @@ -403,6 +404,7 @@ struct merge_tree_options {
>         int show_messages;
>         int name_only;
>         int use_stdin;
> +       const char *merge_base;
>  };
>
>  static int real_merge(struct merge_tree_options *o,
> @@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
>         opt.branch1 = branch1;
>         opt.branch2 = branch2;
>
> -       /*
> -        * Get the merge bases, in reverse order; see comment above
> -        * merge_incore_recursive in merge-ort.h
> -        */
> -       merge_bases = get_merge_bases(parent1, parent2);
> -       if (!merge_bases && !o->allow_unrelated_histories)
> -               die(_("refusing to merge unrelated histories"));
> -       merge_bases = reverse_commit_list(merge_bases);
> +       if (o->merge_base) {
> +               struct commit *base_commit;
> +               struct tree *base_tree, *parent1_tree, *parent2_tree;
> +
> +               base_commit = lookup_commit_reference_by_name(o->merge_base);
> +               if (!base_commit)
> +                       die(_("could not lookup commit %s"), o->merge_base);
> +
> +               opt.ancestor = o->merge_base;
> +               base_tree = get_commit_tree(base_commit);
> +               parent1_tree = get_commit_tree(parent1);
> +               parent2_tree = get_commit_tree(parent2);
> +               merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
> +       } else {
> +               /*
> +                * Get the merge bases, in reverse order; see comment above
> +                * merge_incore_recursive in merge-ort.h
> +                */
> +               merge_bases = get_merge_bases(parent1, parent2);
> +               if (!merge_bases && !o->allow_unrelated_histories)
> +                       die(_("refusing to merge unrelated histories"));
> +               merge_bases = reverse_commit_list(merge_bases);
> +               merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
> +       }
>
> -       merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
>         if (result.clean < 0)
>                 die(_("failure to merge"));
>
> @@ -515,6 +532,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
>                            &o.use_stdin,
>                            N_("perform multiple merges, one per line of input"),
>                            PARSE_OPT_NONEG),
> +               OPT_STRING(0, "merge-base",
> +                          &o.merge_base,
> +                          N_("commit"),
> +                          N_("specify a merge-base for the merge")),
>                 OPT_END()
>         };
>
> diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
> index cac85591b52..5b0073d3dcd 100755
> --- a/t/t4301-merge-tree-write-tree.sh
> +++ b/t/t4301-merge-tree-write-tree.sh
> @@ -860,4 +860,34 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
>         test_cmp expect actual
>  '
>
> +# specify merge-base as parent of branch2
> +# git merge-tree --write-tree --merge-base=c2 c1 c3
> +#   Commit c1: add file1
> +#   Commit c2: add file2 after c1
> +#   Commit c3: add file3 after c2
> +#   Expected: add file3, and file2 does NOT appear
> +
> +test_expect_success 'specify merge-base as parent of branch2' '
> +       # Setup
> +       git init base-b2-p && (
> +               cd base-b2-p &&
> +               test_commit c1 file1 &&
> +               test_commit c2 file2 &&
> +               test_commit c3 file3
> +       ) &&
> +       # Testing
> +       (
> +               cd base-b2-p &&
> +               TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
> +
> +               q_to_tab <<-EOF >expect &&
> +               100644 blob $(git rev-parse c1:file1)Qfile1
> +               100644 blob $(git rev-parse c3:file3)Qfile3
> +               EOF
> +
> +               git ls-tree $TREE_OID >actual &&
> +               test_cmp expect actual
> +       )
> +'
> +
>  test_done
>
> base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4

This looks pretty good; thanks for the many changes.  One thing is
still missing: you are now building on a commit that adds a --stdin
option to merge-tree, which allows the user to specify what to merge
on stdin rather than via other arguments on the command line.  Since
you're making merge-tree support specified merge base(s), it should
also support that in conjunction with --stdin.  So, see the "/* Handle
stdin */" block and adjust it appropriately.

Once you do that, I think your series will be good to go.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-10-29  3:42   ` [PATCH v3] " Kyle Zhao via GitGitGadget
  2022-11-01  1:08     ` Elijah Newren
@ 2022-11-01  8:55     ` Kyle Zhao via GitGitGadget
  2022-11-01  8:55       ` [PATCH v4 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
                         ` (3 more replies)
  1 sibling, 4 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-01  8:55 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Kyle Zhao

Thanks for Elijah's work. I'm very excited that merge-ort is integrated into
the git merge-tree, which means that we can use merge-ort in bare
repositories to optimize merge performance.

In this patch, I introduce a new --merge-base=<commit> option to allow
callers to specify a merge-base for the merge. This may allow users to
implement git cherry-pick and git rebase in bare repositories with git
merge-tree cmd.

Changes since v1:

 * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
   merge-base is specified.
 * Fixed c style problem.
 * Moved commit lookup/die logic out to the parsing logic in
   cmd_merge_tree().
 * use test_commit for test

Changes since v2:

 * commit message
 * Rebased on top of en/merge-tree-sequence.
 * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char. To
   make it easier to pass parameters, I moved
   lookup_commit_reference_by_name() to real_ merge() again.
 * Added test comment.

Changes since v3:

 * support --merge-base in conjunction with --stdin

Kyle Zhao (2):
  merge-tree.c: add --merge-base=<commit> option
  merge-tree.c: support --merge-base in conjunction with --stdin

 Documentation/git-merge-tree.txt |  5 +++
 builtin/merge-tree.c             | 64 ++++++++++++++++++++++++++------
 t/t4301-merge-tree-write-tree.sh | 51 +++++++++++++++++++++++++
 3 files changed, 108 insertions(+), 12 deletions(-)


base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v3:

 1:  554cbde49e8 ! 1:  bba854fc8fa merge-tree.c: add --merge-base=<commit> option
     @@ builtin/merge-tree.c
       #include "merge-ort.h"
       #include "object-store.h"
      @@ builtin/merge-tree.c: struct merge_tree_options {
     - 	int show_messages;
     - 	int name_only;
     - 	int use_stdin;
     -+	const char *merge_base;
       };
       
       static int real_merge(struct merge_tree_options *o,
     ++		      const char *merge_base,
     + 		      const char *branch1, const char *branch2,
     + 		      const char *prefix)
     + {
      @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
       	opt.branch1 = branch1;
       	opt.branch2 = branch2;
     @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
      -	if (!merge_bases && !o->allow_unrelated_histories)
      -		die(_("refusing to merge unrelated histories"));
      -	merge_bases = reverse_commit_list(merge_bases);
     -+	if (o->merge_base) {
     ++	if (merge_base) {
      +		struct commit *base_commit;
      +		struct tree *base_tree, *parent1_tree, *parent2_tree;
      +
     -+		base_commit = lookup_commit_reference_by_name(o->merge_base);
     ++		base_commit = lookup_commit_reference_by_name(merge_base);
      +		if (!base_commit)
     -+			die(_("could not lookup commit %s"), o->merge_base);
     ++			die(_("could not lookup commit %s"), merge_base);
      +
     -+		opt.ancestor = o->merge_base;
     ++		opt.ancestor = merge_base;
      +		base_tree = get_commit_tree(base_commit);
      +		parent1_tree = get_commit_tree(parent1);
      +		parent2_tree = get_commit_tree(parent2);
     @@ builtin/merge-tree.c: static int real_merge(struct merge_tree_options *o,
       	if (result.clean < 0)
       		die(_("failure to merge"));
       
     +@@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     + 	struct merge_tree_options o = { .show_messages = -1 };
     + 	int expected_remaining_argc;
     + 	int original_argc;
     ++	const char *merge_base = NULL;
     + 
     + 	const char * const merge_tree_usage[] = {
     + 		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
      @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
       			   &o.use_stdin,
       			   N_("perform multiple merges, one per line of input"),
       			   PARSE_OPT_NONEG),
      +		OPT_STRING(0, "merge-base",
     -+			   &o.merge_base,
     ++			   &merge_base,
      +			   N_("commit"),
      +			   N_("specify a merge-base for the merge")),
       		OPT_END()
       	};
       
     +@@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     + 			if (!split[0] || !split[1] || split[2])
     + 				die(_("malformed input line: '%s'."), buf.buf);
     + 			strbuf_rtrim(split[0]);
     +-			result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
     ++			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
     + 			if (result < 0)
     + 				die(_("merging cannot continue; got unclean result of %d"), result);
     + 			strbuf_list_free(split);
     +@@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     + 
     + 	/* Do the relevant type of merge */
     + 	if (o.mode == MODE_REAL)
     +-		return real_merge(&o, argv[0], argv[1], prefix);
     ++		return real_merge(&o, merge_base, argv[0], argv[1], prefix);
     + 	else
     + 		return trivial_merge(argv[0], argv[1], argv[2]);
     + }
      
       ## t/t4301-merge-tree-write-tree.sh ##
      @@ t/t4301-merge-tree-write-tree.sh: test_expect_success '--stdin with both a successful and a conflicted merge' '
 -:  ----------- > 2:  db47fbc663e merge-tree.c: support --merge-base in conjunction with --stdin

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v4 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
@ 2022-11-01  8:55       ` Kyle Zhao via GitGitGadget
  2022-11-01  8:55       ` [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin Kyle Zhao via GitGitGadget
                         ` (2 subsequent siblings)
  3 siblings, 0 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-01  8:55 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

This does a merge of HEAD and branch, but uses branch^ as the merge-base.

And the reason why using an option flag instead of a positional argument
is to allow additional commits passed to merge-tree to be handled via an
octopus merge in the future.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 44 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 30 ++++++++++++++++++++++
 3 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 04bcc416e6e..d9dacb2ce54 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..f402b807c0f 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -406,6 +407,7 @@ struct merge_tree_options {
 };
 
 static int real_merge(struct merge_tree_options *o,
+		      const char *merge_base,
 		      const char *branch1, const char *branch2,
 		      const char *prefix)
 {
@@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (merge_base) {
+		struct commit *base_commit;
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		base_commit = lookup_commit_reference_by_name(merge_base);
+		if (!base_commit)
+			die(_("could not lookup commit %s"), merge_base);
+
+		opt.ancestor = merge_base;
+		base_tree = get_commit_tree(base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -487,6 +504,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 	struct merge_tree_options o = { .show_messages = -1 };
 	int expected_remaining_argc;
 	int original_argc;
+	const char *merge_base = NULL;
 
 	const char * const merge_tree_usage[] = {
 		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
@@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.use_stdin,
 			   N_("perform multiple merges, one per line of input"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			   &merge_base,
+			   N_("commit"),
+			   N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
@@ -538,7 +560,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			if (!split[0] || !split[1] || split[2])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
+			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
@@ -581,7 +603,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 	/* Do the relevant type of merge */
 	if (o.mode == MODE_REAL)
-		return real_merge(&o, argv[0], argv[1], prefix);
+		return real_merge(&o, merge_base, argv[0], argv[1], prefix);
 	else
 		return trivial_merge(argv[0], argv[1], argv[2]);
 }
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index cac85591b52..5b0073d3dcd 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,4 +860,34 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+#   Commit c1: add file1
+#   Commit c2: add file2 after c1
+#   Commit c3: add file3 after c2
+#   Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	git init base-b2-p && (
+		cd base-b2-p &&
+		test_commit c1 file1 &&
+		test_commit c2 file2 &&
+		test_commit c3 file3
+	) &&
+	# Testing
+	(
+		cd base-b2-p &&
+		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+		q_to_tab <<-EOF >expect &&
+		100644 blob $(git rev-parse c1:file1)Qfile1
+		100644 blob $(git rev-parse c3:file3)Qfile3
+		EOF
+
+		git ls-tree $TREE_OID >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 43+ messages in thread

* [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin
  2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
  2022-11-01  8:55       ` [PATCH v4 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-01  8:55       ` Kyle Zhao via GitGitGadget
  2022-11-03  1:13         ` Elijah Newren
  2022-11-01 21:19       ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Taylor Blau
  2022-11-03  3:29       ` [PATCH v5 " Kyle Zhao via GitGitGadget
  3 siblings, 1 reply; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-01  8:55 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

The previous change add "--merge-base" option in order to allow users to
specify merge-base for the merge. But it doesn't compatible well with
--stdin, because multiple batched merges can only have the same specified
base.

This patch allows users to pass --merge-base option into the input line,
such as:

    printf "--merge-base=<b3> <b1> <b2>" | git merge-tree --stdin

This does a merge of b1 and b2, and uses b3 as the merge-base.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt |  3 ++-
 builtin/merge-tree.c             | 22 ++++++++++++++++++++--
 t/t4301-merge-tree-write-tree.sh | 21 +++++++++++++++++++++
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d9dacb2ce54..be6a11bbaec 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -66,7 +66,8 @@ OPTIONS
 
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge.
+	specify a merge-base for the merge. When --stdin is passed, this
+	option should be passed into the input line.
 
 [[OUTPUT]]
 OUTPUT
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index f402b807c0f..7a8049e7b0c 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -551,16 +551,34 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 		if (o.mode == MODE_TRIVIAL)
 			die(_("--trivial-merge is incompatible with all other options"));
+		if (merge_base)
+			die(_("--merge-base should be passed into the input line"));
 		line_termination = '\0';
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
 			int result;
+			const char *input_merge_base = NULL;
+			const char *arg;
 
 			split = strbuf_split(&buf, ' ');
-			if (!split[0] || !split[1] || split[2])
+			if (!split[0] || !split[1])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
+
+			/* parse --merge-base=<commit> option */
+			arg = split[0]->buf;
+			if (skip_prefix(arg, "--merge-base=", &arg))
+				input_merge_base = arg;
+
+			if (input_merge_base && split[2] && !split[3]) {
+				strbuf_rtrim(split[1]);
+				result = real_merge(&o, input_merge_base, split[1]->buf, split[2]->buf, prefix);
+			} else if (!input_merge_base && !split[2]) {
+				result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
+			} else {
+				die(_("malformed input line: '%s'."), buf.buf);
+			}
+
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 5b0073d3dcd..aec2c00b91f 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,6 +860,13 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+
+test_expect_success '--merge-base is incompatible with --stdin' '
+	test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
+
+	grep "^fatal: --merge-base should be passed into the input line" expect
+'
+
 # specify merge-base as parent of branch2
 # git merge-tree --write-tree --merge-base=c2 c1 c3
 #   Commit c1: add file1
@@ -890,4 +897,18 @@ test_expect_success 'specify merge-base as parent of branch2' '
 	)
 '
 
+test_expect_success '--stdin with both a normal merge and a merge-base specified merge' '
+	cd base-b2-p &&
+	printf "c1 c3\n--merge-base=c2 c1 c3" | git merge-tree --stdin >actual &&
+
+	printf "1\0" >expect &&
+	git merge-tree --write-tree -z c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
+	printf "\0" >>expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
  2022-11-01  8:55       ` [PATCH v4 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-11-01  8:55       ` [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin Kyle Zhao via GitGitGadget
@ 2022-11-01 21:19       ` Taylor Blau
  2022-11-03  3:29       ` [PATCH v5 " Kyle Zhao via GitGitGadget
  3 siblings, 0 replies; 43+ messages in thread
From: Taylor Blau @ 2022-11-01 21:19 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Elijah Newren, kylezhao

On Tue, Nov 01, 2022 at 08:55:02AM +0000, Kyle Zhao via GitGitGadget wrote:
> Thanks for Elijah's work. I'm very excited that merge-ort is integrated into
> the git merge-tree, which means that we can use merge-ort in bare
> repositories to optimize merge performance.
>
> In this patch, I introduce a new --merge-base=<commit> option to allow
> callers to specify a merge-base for the merge. This may allow users to
> implement git cherry-pick and git rebase in bare repositories with git
> merge-tree cmd.

Thanks for the update. I haven't taken a close look, but queued what you
have as a replacement on top of en/merge-tree-sequence.

Let's see what Elijah thinks...

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin
  2022-11-01  8:55       ` [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin Kyle Zhao via GitGitGadget
@ 2022-11-03  1:13         ` Elijah Newren
  2022-11-03  3:28           ` [Internet]Re: " kylezhao(赵柯宇)
  0 siblings, 1 reply; 43+ messages in thread
From: Elijah Newren @ 2022-11-03  1:13 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Kyle Zhao

On Tue, Nov 1, 2022 at 1:55 AM Kyle Zhao via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Kyle Zhao <kylezhao@tencent.com>
>
> The previous change add "--merge-base" option in order to allow users to

s/add/added/ ?

> specify merge-base for the merge. But it doesn't compatible well with
> --stdin, because multiple batched merges can only have the same specified
> base.

"it doesn't compatible well" doesn't parse for me.

> This patch allows users to pass --merge-base option into the input line,
> such as:

Quoting from Documentation/SubmittingPatches:

"""
Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
to do frotz", as if you are giving orders to the codebase to change
its behavior.
"""

>
>     printf "--merge-base=<b3> <b1> <b2>" | git merge-tree --stdin
>
> This does a merge of b1 and b2, and uses b3 as the merge-base.

Perhaps something like:

"""
The previous commit added a `--merge-base` option in order to allow
using a specified merge-base for the merge.  Extend the input accepted
by `--stdin` to also allow a specified merge-base with each merge
requested.  For example:

    printf "--merge-base=<b3> <b1> <b2>" | git merge-tree --stdin

does a merge of b1 and b2, and uses b3 as the merge-base.
"""

However, I'm a bit curious about using `--merge-base=` on the input
line as opposed to just using a simpler marker; something like

    printf "<b3> -- <b1> <b2>" | git merge-tree --stdin

(which follows a precedent set by git-merge-recursive).  Your version
is a bit more self-documenting, but what if we want to allow users to
specify multiple merge bases in the future (for use in passing to
merge_incore_recursive())?  Is it annoying to need to prefix each one?

> Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
> ---
>  Documentation/git-merge-tree.txt |  3 ++-
>  builtin/merge-tree.c             | 22 ++++++++++++++++++++--
>  t/t4301-merge-tree-write-tree.sh | 21 +++++++++++++++++++++
>  3 files changed, 43 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
> index d9dacb2ce54..be6a11bbaec 100644
> --- a/Documentation/git-merge-tree.txt
> +++ b/Documentation/git-merge-tree.txt
> @@ -66,7 +66,8 @@ OPTIONS
>
>  --merge-base=<commit>::
>         Instead of finding the merge-bases for <branch1> and <branch2>,
> -       specify a merge-base for the merge.
> +       specify a merge-base for the merge. When --stdin is passed, this
> +       option should be passed into the input line.

I'm not sure "passed into the input line" will be clear to readers.
Perhaps we want to have --stdin documented (looks like I forgot to do
that in my series; oops), mentioning the input format.

>  [[OUTPUT]]
>  OUTPUT
> diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
> index f402b807c0f..7a8049e7b0c 100644
> --- a/builtin/merge-tree.c
> +++ b/builtin/merge-tree.c
> @@ -551,16 +551,34 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
>
>                 if (o.mode == MODE_TRIVIAL)
>                         die(_("--trivial-merge is incompatible with all other options"));
> +               if (merge_base)
> +                       die(_("--merge-base should be passed into the input line"));

If we change the input as noted above, the wording here may need to change too.

>                 line_termination = '\0';
>                 while (strbuf_getline_lf(&buf, stdin) != EOF) {
>                         struct strbuf **split;
>                         int result;
> +                       const char *input_merge_base = NULL;
> +                       const char *arg;
>
>                         split = strbuf_split(&buf, ' ');
> -                       if (!split[0] || !split[1] || split[2])
> +                       if (!split[0] || !split[1])
>                                 die(_("malformed input line: '%s'."), buf.buf);
>                         strbuf_rtrim(split[0]);
> -                       result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
> +
> +                       /* parse --merge-base=<commit> option */
> +                       arg = split[0]->buf;
> +                       if (skip_prefix(arg, "--merge-base=", &arg))
> +                               input_merge_base = arg;
> +
> +                       if (input_merge_base && split[2] && !split[3]) {
> +                               strbuf_rtrim(split[1]);
> +                               result = real_merge(&o, input_merge_base, split[1]->buf, split[2]->buf, prefix);
> +                       } else if (!input_merge_base && !split[2]) {
> +                               result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
> +                       } else {
> +                               die(_("malformed input line: '%s'."), buf.buf);
> +                       }
> +
>                         if (result < 0)
>                                 die(_("merging cannot continue; got unclean result of %d"), result);
>                         strbuf_list_free(split);
> diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
> index 5b0073d3dcd..aec2c00b91f 100755
> --- a/t/t4301-merge-tree-write-tree.sh
> +++ b/t/t4301-merge-tree-write-tree.sh
> @@ -860,6 +860,13 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
>         test_cmp expect actual
>  '
>
> +
> +test_expect_success '--merge-base is incompatible with --stdin' '
> +       test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
> +
> +       grep "^fatal: --merge-base should be passed into the input line" expect
> +'

Yeah, most merge-tree options are for controlling the output, and as
such they are not incompatible with --stdin.  This option is, so it
makes sense you need a special check for it.  Looks good.

> +
>  # specify merge-base as parent of branch2
>  # git merge-tree --write-tree --merge-base=c2 c1 c3
>  #   Commit c1: add file1
> @@ -890,4 +897,18 @@ test_expect_success 'specify merge-base as parent of branch2' '
>         )
>  '
>
> +test_expect_success '--stdin with both a normal merge and a merge-base specified merge' '
> +       cd base-b2-p &&
> +       printf "c1 c3\n--merge-base=c2 c1 c3" | git merge-tree --stdin >actual &&
> +
> +       printf "1\0" >expect &&
> +       git merge-tree --write-tree -z c1 c3 >>expect &&
> +       printf "\0" >>expect &&
> +
> +       printf "1\0" >>expect &&
> +       git merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
> +       printf "\0" >>expect &&
> +       test_cmp expect actual
> +'

This last test seems odd.  You are merely testing that the output of
"git merge-tree --stdin" matches the output of repeated calls to "git
merge-tree", not that the merges involved produce correct results?
Maybe that's fine since earlier tests verify that individual
merge-tree calls are doing the right thing, I was just a bit
surprised.  Maybe a comment in the code that this was your intent
would be helpful?

^ permalink raw reply	[flat|nested] 43+ messages in thread

* RE: [Internet]Re: [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin
  2022-11-03  1:13         ` Elijah Newren
@ 2022-11-03  3:28           ` kylezhao(赵柯宇)
  0 siblings, 0 replies; 43+ messages in thread
From: kylezhao(赵柯宇) @ 2022-11-03  3:28 UTC (permalink / raw)
  To: Elijah Newren, Kyle Zhao via GitGitGadget; +Cc: git@vger.kernel.org

Thanks for your review.

>>
>> The previous change add "--merge-base" option in order to allow users 
>> to
>
> s/add/added/ ?

Done.


> Perhaps something like:
> 
> """
> The previous commit added a `--merge-base` option in order to allow using a specified merge-base for the merge.  Extend the input accepted by `--stdin` to also allow a specified merge-base with each merge requested.  For example:
> 
 >    printf "--merge-base=<b3> <b1> <b2>" | git merge-tree --stdin
>

Done.


> However, I'm a bit curious about using `--merge-base=` on the input line as opposed to just using a simpler marker; something like
> 
>     printf "<b3> -- <b1> <b2>" | git merge-tree --stdin
> 
> (which follows a precedent set by git-merge-recursive).  Your version is a bit more self-documenting, but what if we want to allow users to specify multiple merge bases in the future (for use in passing to merge_incore_recursive())?  Is it annoying to need to prefix each one?

Actually, I thought about it at the time, but didn't think of any good input format. Your idea is great, I think I'll use it.

> I'm not sure "passed into the input line" will be clear to readers.
> Perhaps we want to have --stdin documented (looks like I forgot to do that in my series; oops), mentioning the input format.

I just added the documentation for --input, please see what else needs to be improved.

> > +       printf "1\0" >>expect &&
> > +       git merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
> > +       printf "\0" >>expect &&
> > +       test_cmp expect actual
> > +'
>
> This last test seems odd.  You are merely testing that the output of "git merge-tree --stdin" matches the output of repeated calls to "git merge-tree", not that the merges involved produce correct results?
> Maybe that's fine since earlier tests verify that individual merge-tree calls are doing the right thing, I was just a bit surprised.  Maybe a comment in the code that this was your intent would be helpful?

Yeah, I have added the comment.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v5 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
                         ` (2 preceding siblings ...)
  2022-11-01 21:19       ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Taylor Blau
@ 2022-11-03  3:29       ` Kyle Zhao via GitGitGadget
  2022-11-03  3:29         ` [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
                           ` (2 more replies)
  3 siblings, 3 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-03  3:29 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Taylor Blau, Kyle Zhao

Thanks for Elijah's work. I'm very excited that merge-ort is integrated into
the git merge-tree, which means that we can use merge-ort in bare
repositories to optimize merge performance.

In this patch, I introduce a new --merge-base=<commit> option to allow
callers to specify a merge-base for the merge. This may allow users to
implement git cherry-pick and git rebase in bare repositories with git
merge-tree cmd.

Changes since v1:

 * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
   merge-base is specified.
 * Fixed c style problem.
 * Moved commit lookup/die logic out to the parsing logic in
   cmd_merge_tree().
 * use test_commit for test

Changes since v2:

 * commit message
 * Rebased on top of en/merge-tree-sequence.
 * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char. To
   make it easier to pass parameters, I moved
   lookup_commit_reference_by_name() to real_ merge() again.
 * Added test comment.

Changes since v3:

 * support --merge-base in conjunction with --stdin

Kyle Zhao (2):
  merge-tree.c: add --merge-base=<commit> option
  merge-tree.c: allow specifying the merge-base when --stdin is passed

 Documentation/git-merge-tree.txt | 16 ++++++++
 builtin/merge-tree.c             | 65 ++++++++++++++++++++++++++------
 t/t4301-merge-tree-write-tree.sh | 55 +++++++++++++++++++++++++++
 3 files changed, 124 insertions(+), 12 deletions(-)


base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v4:

 1:  bba854fc8fa ! 1:  01df0d1a6a7 merge-tree.c: add --merge-base=<commit> option
     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char
       		OPT_END()
       	};
       
     +@@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     + 
     + 		if (o.mode == MODE_TRIVIAL)
     + 			die(_("--trivial-merge is incompatible with all other options"));
     ++		if (merge_base)
     ++			die(_("--merge-base is incompatible with --stdin"));
     + 		line_termination = '\0';
     + 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
     + 			struct strbuf **split;
      @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
       			if (!split[0] || !split[1] || split[2])
       				die(_("malformed input line: '%s'."), buf.buf);
 2:  db47fbc663e ! 2:  3bdfad03cca merge-tree.c: support --merge-base in conjunction with --stdin
     @@ Metadata
      Author: Kyle Zhao <kylezhao@tencent.com>
      
       ## Commit message ##
     -    merge-tree.c: support --merge-base in conjunction with --stdin
     +    merge-tree.c: allow specifying the merge-base when --stdin is passed
      
     -    The previous change add "--merge-base" option in order to allow users to
     -    specify merge-base for the merge. But it doesn't compatible well with
     -    --stdin, because multiple batched merges can only have the same specified
     -    base.
     +    The previous commit added a `--merge-base` option in order to allow
     +    using a specified merge-base for the merge.  Extend the input accepted
     +    by `--stdin` to also allow a specified merge-base with each merge
     +    requested.  For example:
      
     -    This patch allows users to pass --merge-base option into the input line,
     -    such as:
     +        printf "<b3> -- <b1> <b2>" | git merge-tree --stdin
      
     -        printf "--merge-base=<b3> <b1> <b2>" | git merge-tree --stdin
     -
     -    This does a merge of b1 and b2, and uses b3 as the merge-base.
     +    does a merge of b1 and b2, and uses b3 as the merge-base.
      
          Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
      
     @@ Documentation/git-merge-tree.txt: OPTIONS
       --merge-base=<commit>::
       	Instead of finding the merge-bases for <branch1> and <branch2>,
      -	specify a merge-base for the merge.
     -+	specify a merge-base for the merge. When --stdin is passed, this
     -+	option should be passed into the input line.
     ++	specify a merge-base for the merge. This option is incompatible
     ++	with `--stdin`.
       
       [[OUTPUT]]
       OUTPUT
     +@@ Documentation/git-merge-tree.txt: with linkgit:git-merge[1]:
     +   * any messages that would have been printed to stdout (the
     +     <<IM,Informational messages>>)
     + 
     ++INPUT FORMAT
     ++------------
     ++'git merge-tree --stdin' input format is fully text based. Each line
     ++has this format:
     ++
     ++	[<base-commit> -- ]<branch1> <branch2>
     ++
     ++If one line is separated by `--`, the string before the separator is
     ++used for specifying a merge-base for the merge and the string after
     ++the separator describes the branches to be merged.
     ++
     + MISTAKES TO AVOID
     + -----------------
     + 
      
       ## builtin/merge-tree.c ##
      @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char *prefix)
     - 
     - 		if (o.mode == MODE_TRIVIAL)
     - 			die(_("--trivial-merge is incompatible with all other options"));
     -+		if (merge_base)
     -+			die(_("--merge-base should be passed into the input line"));
     - 		line_termination = '\0';
       		while (strbuf_getline_lf(&buf, stdin) != EOF) {
       			struct strbuf **split;
       			int result;
      +			const char *input_merge_base = NULL;
     -+			const char *arg;
       
       			split = strbuf_split(&buf, ' ');
      -			if (!split[0] || !split[1] || split[2])
     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc, const char **argv, const char
       				die(_("malformed input line: '%s'."), buf.buf);
       			strbuf_rtrim(split[0]);
      -			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
     ++			strbuf_rtrim(split[1]);
      +
     -+			/* parse --merge-base=<commit> option */
     -+			arg = split[0]->buf;
     -+			if (skip_prefix(arg, "--merge-base=", &arg))
     -+				input_merge_base = arg;
     ++			/* parse the merge-base */
     ++			if (!strcmp(split[1]->buf, "--")) {
     ++				input_merge_base = split[0]->buf;
     ++			}
      +
     -+			if (input_merge_base && split[2] && !split[3]) {
     -+				strbuf_rtrim(split[1]);
     -+				result = real_merge(&o, input_merge_base, split[1]->buf, split[2]->buf, prefix);
     ++			if (input_merge_base && split[2] && split[3] && !split[4]) {
     ++				strbuf_rtrim(split[2]);
     ++				strbuf_rtrim(split[3]);
     ++				result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
      +			} else if (!input_merge_base && !split[2]) {
      +				result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
      +			} else {
     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success '--stdin with both a succe
      +test_expect_success '--merge-base is incompatible with --stdin' '
      +	test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
      +
     -+	grep "^fatal: --merge-base should be passed into the input line" expect
     ++	grep "^fatal: --merge-base is incompatible with --stdin" expect
      +'
      +
       # specify merge-base as parent of branch2
     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'specify merge-base as par
       	)
       '
       
     ++# Since the earlier tests have verified that individual merge-tree calls
     ++# are doing the right thing, this test case is only used to test whether
     ++# the input format is available.
     ++
      +test_expect_success '--stdin with both a normal merge and a merge-base specified merge' '
      +	cd base-b2-p &&
     -+	printf "c1 c3\n--merge-base=c2 c1 c3" | git merge-tree --stdin >actual &&
     ++	printf "c1 c3\nc2 -- c1 c3" | git merge-tree --stdin >actual &&
      +
      +	printf "1\0" >expect &&
      +	git merge-tree --write-tree -z c1 c3 >>expect &&

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-03  3:29       ` [PATCH v5 " Kyle Zhao via GitGitGadget
@ 2022-11-03  3:29         ` Kyle Zhao via GitGitGadget
  2022-11-03  8:36           ` Ævar Arnfjörð Bjarmason
  2022-11-03  3:29         ` [PATCH v5 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
  2022-11-03 10:50         ` [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2 siblings, 1 reply; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-03  3:29 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Taylor Blau, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

This does a merge of HEAD and branch, but uses branch^ as the merge-base.

And the reason why using an option flag instead of a positional argument
is to allow additional commits passed to merge-tree to be handled via an
octopus merge in the future.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 46 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 30 +++++++++++++++++++++
 3 files changed, 69 insertions(+), 11 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 04bcc416e6e..d9dacb2ce54 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..62c6d43cdb9 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -406,6 +407,7 @@ struct merge_tree_options {
 };
 
 static int real_merge(struct merge_tree_options *o,
+		      const char *merge_base,
 		      const char *branch1, const char *branch2,
 		      const char *prefix)
 {
@@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (merge_base) {
+		struct commit *base_commit;
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		base_commit = lookup_commit_reference_by_name(merge_base);
+		if (!base_commit)
+			die(_("could not lookup commit %s"), merge_base);
+
+		opt.ancestor = merge_base;
+		base_tree = get_commit_tree(base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -487,6 +504,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 	struct merge_tree_options o = { .show_messages = -1 };
 	int expected_remaining_argc;
 	int original_argc;
+	const char *merge_base = NULL;
 
 	const char * const merge_tree_usage[] = {
 		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
@@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.use_stdin,
 			   N_("perform multiple merges, one per line of input"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			   &merge_base,
+			   N_("commit"),
+			   N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
@@ -529,6 +551,8 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 		if (o.mode == MODE_TRIVIAL)
 			die(_("--trivial-merge is incompatible with all other options"));
+		if (merge_base)
+			die(_("--merge-base is incompatible with --stdin"));
 		line_termination = '\0';
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
@@ -538,7 +562,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			if (!split[0] || !split[1] || split[2])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
+			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
@@ -581,7 +605,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 	/* Do the relevant type of merge */
 	if (o.mode == MODE_REAL)
-		return real_merge(&o, argv[0], argv[1], prefix);
+		return real_merge(&o, merge_base, argv[0], argv[1], prefix);
 	else
 		return trivial_merge(argv[0], argv[1], argv[2]);
 }
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index cac85591b52..5b0073d3dcd 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,4 +860,34 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+#   Commit c1: add file1
+#   Commit c2: add file2 after c1
+#   Commit c3: add file3 after c2
+#   Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	git init base-b2-p && (
+		cd base-b2-p &&
+		test_commit c1 file1 &&
+		test_commit c2 file2 &&
+		test_commit c3 file3
+	) &&
+	# Testing
+	(
+		cd base-b2-p &&
+		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+		q_to_tab <<-EOF >expect &&
+		100644 blob $(git rev-parse c1:file1)Qfile1
+		100644 blob $(git rev-parse c3:file3)Qfile3
+		EOF
+
+		git ls-tree $TREE_OID >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 43+ messages in thread

* [PATCH v5 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed
  2022-11-03  3:29       ` [PATCH v5 " Kyle Zhao via GitGitGadget
  2022-11-03  3:29         ` [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-03  3:29         ` Kyle Zhao via GitGitGadget
  2022-11-03 10:50         ` [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2 siblings, 0 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-03  3:29 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren, kylezhao, Taylor Blau, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

The previous commit added a `--merge-base` option in order to allow
using a specified merge-base for the merge.  Extend the input accepted
by `--stdin` to also allow a specified merge-base with each merge
requested.  For example:

    printf "<b3> -- <b1> <b2>" | git merge-tree --stdin

does a merge of b1 and b2, and uses b3 as the merge-base.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt | 14 +++++++++++++-
 builtin/merge-tree.c             | 21 +++++++++++++++++++--
 t/t4301-merge-tree-write-tree.sh | 25 +++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d9dacb2ce54..298c133fdb6 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -66,7 +66,8 @@ OPTIONS
 
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge.
+	specify a merge-base for the merge. This option is incompatible
+	with `--stdin`.
 
 [[OUTPUT]]
 OUTPUT
@@ -220,6 +221,17 @@ with linkgit:git-merge[1]:
   * any messages that would have been printed to stdout (the
     <<IM,Informational messages>>)
 
+INPUT FORMAT
+------------
+'git merge-tree --stdin' input format is fully text based. Each line
+has this format:
+
+	[<base-commit> -- ]<branch1> <branch2>
+
+If one line is separated by `--`, the string before the separator is
+used for specifying a merge-base for the merge and the string after
+the separator describes the branches to be merged.
+
 MISTAKES TO AVOID
 -----------------
 
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index 62c6d43cdb9..330f779e8bc 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -557,12 +557,29 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
 			int result;
+			const char *input_merge_base = NULL;
 
 			split = strbuf_split(&buf, ' ');
-			if (!split[0] || !split[1] || split[2])
+			if (!split[0] || !split[1])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
+			strbuf_rtrim(split[1]);
+
+			/* parse the merge-base */
+			if (!strcmp(split[1]->buf, "--")) {
+				input_merge_base = split[0]->buf;
+			}
+
+			if (input_merge_base && split[2] && split[3] && !split[4]) {
+				strbuf_rtrim(split[2]);
+				strbuf_rtrim(split[3]);
+				result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
+			} else if (!input_merge_base && !split[2]) {
+				result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
+			} else {
+				die(_("malformed input line: '%s'."), buf.buf);
+			}
+
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 5b0073d3dcd..03bb1cb8685 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,6 +860,13 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+
+test_expect_success '--merge-base is incompatible with --stdin' '
+	test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
+
+	grep "^fatal: --merge-base is incompatible with --stdin" expect
+'
+
 # specify merge-base as parent of branch2
 # git merge-tree --write-tree --merge-base=c2 c1 c3
 #   Commit c1: add file1
@@ -890,4 +897,22 @@ test_expect_success 'specify merge-base as parent of branch2' '
 	)
 '
 
+# Since the earlier tests have verified that individual merge-tree calls
+# are doing the right thing, this test case is only used to test whether
+# the input format is available.
+
+test_expect_success '--stdin with both a normal merge and a merge-base specified merge' '
+	cd base-b2-p &&
+	printf "c1 c3\nc2 -- c1 c3" | git merge-tree --stdin >actual &&
+
+	printf "1\0" >expect &&
+	git merge-tree --write-tree -z c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
+	printf "\0" >>expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-03  3:29         ` [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-03  8:36           ` Ævar Arnfjörð Bjarmason
  2022-11-03  9:43             ` [Internet]Re: " kylezhao(赵柯宇)
  0 siblings, 1 reply; 43+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-03  8:36 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget; +Cc: git, Elijah Newren, Taylor Blau, Kyle Zhao


On Thu, Nov 03 2022, Kyle Zhao via GitGitGadget wrote:

> From: Kyle Zhao <kylezhao@tencent.com>

> [...]
> +test_expect_success 'specify merge-base as parent of branch2' '
> +	# Setup
> +	git init base-b2-p && (

It's good practice to nuke it after the test:

	test_when_finished "rm -rf base-b2-p" &&
	git init base-b2-p



> +		cd base-b2-p &&
> +		test_commit c1 file1 &&
> +		test_commit c2 file2 &&
> +		test_commit c3 file3
> +	) &&

Could avoid the subshell-ing with:

	test_commit -C base-b2-p c1 file1 &&
	[...]

	

> +	# Testing
> +	(
> +		cd base-b2-p &&
> +		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&

Ditto sub-shell.

> +
> +		q_to_tab <<-EOF >expect &&
> +		100644 blob $(git rev-parse c1:file1)Qfile1
> +		100644 blob $(git rev-parse c3:file3)Qfile3
> +		EOF
> +
> +		git ls-tree $TREE_OID >actual &&
> +		test_cmp expect actual
> +	)
> +'
> +
>  test_done


^ permalink raw reply	[flat|nested] 43+ messages in thread

* RE: [Internet]Re: [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-03  8:36           ` Ævar Arnfjörð Bjarmason
@ 2022-11-03  9:43             ` kylezhao(赵柯宇)
  0 siblings, 0 replies; 43+ messages in thread
From: kylezhao(赵柯宇) @ 2022-11-03  9:43 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason,
	Kyle Zhao via GitGitGadget
  Cc: git@vger.kernel.org, Elijah Newren, Taylor Blau

> 
> > [...]
> > +test_expect_success 'specify merge-base as parent of branch2' '
> > +	# Setup
> > +	git init base-b2-p && (
> 
> It's good practice to nuke it after the test:
> 
> 	test_when_finished "rm -rf base-b2-p" &&
> 	git init base-b2-p
> 
> 

Thanks for the reviews, I'll fix it in the next patch.

> 
> Could avoid the subshell-ing with:
> 
> 	test_commit -C base-b2-p c1 file1 &&
> 	[...]

Nice.



^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-03  3:29       ` [PATCH v5 " Kyle Zhao via GitGitGadget
  2022-11-03  3:29         ` [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-11-03  3:29         ` [PATCH v5 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
@ 2022-11-03 10:50         ` Kyle Zhao via GitGitGadget
  2022-11-03 10:50           ` [PATCH v6 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
                             ` (2 more replies)
  2 siblings, 3 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-03 10:50 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao

Thanks for Elijah's work. I'm very excited that merge-ort is integrated into
the git merge-tree, which means that we can use merge-ort in bare
repositories to optimize merge performance.

In this patch, I introduce a new --merge-base=<commit> option to allow
callers to specify a merge-base for the merge. This may allow users to
implement git cherry-pick and git rebase in bare repositories with git
merge-tree cmd.

Changes since v1:

 * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
   merge-base is specified.
 * Fixed c style problem.
 * Moved commit lookup/die logic out to the parsing logic in
   cmd_merge_tree().
 * use test_commit for test

Changes since v2:

 * commit message
 * Rebased on top of en/merge-tree-sequence.
 * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char. To
   make it easier to pass parameters, I moved
   lookup_commit_reference_by_name() to real_ merge() again.
 * Added test comment.

Changes since v3:

 * support --merge-base in conjunction with --stdin

Changes since v4:

 * commit message
 * added input format document
 * changed the input format for specifying the merge-base when --stdin is
   passed
 * changed the output when --stdin and --merge-base are used at the same
   time
 * add comment for test

Changes since v5:

 * improved test: remove the test repo after the test; avoid sub-shell.

Kyle Zhao (2):
  merge-tree.c: add --merge-base=<commit> option
  merge-tree.c: allow specifying the merge-base when --stdin is passed

 Documentation/git-merge-tree.txt | 16 ++++++++
 builtin/merge-tree.c             | 65 ++++++++++++++++++++++++++------
 t/t4301-merge-tree-write-tree.sh | 61 ++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 12 deletions(-)


base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v6
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v5:

 1:  01df0d1a6a7 ! 1:  1cf1c69b8e8 merge-tree.c: add --merge-base=<commit> option
     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success '--stdin with both a succe
      +
      +test_expect_success 'specify merge-base as parent of branch2' '
      +	# Setup
     -+	git init base-b2-p && (
     -+		cd base-b2-p &&
     -+		test_commit c1 file1 &&
     -+		test_commit c2 file2 &&
     -+		test_commit c3 file3
     -+	) &&
     ++	test_when_finished "rm -rf base-b2-p" &&
     ++	git init base-b2-p &&
     ++	test_commit -C base-b2-p c1 file1 &&
     ++	test_commit -C base-b2-p c2 file2 &&
     ++	test_commit -C base-b2-p c3 file3 &&
     ++
      +	# Testing
     -+	(
     -+		cd base-b2-p &&
     -+		TREE_OID=$(git merge-tree --write-tree --merge-base=c2 c1 c3) &&
     ++	TREE_OID=$(git -C base-b2-p merge-tree --write-tree --merge-base=c2 c1 c3) &&
      +
     -+		q_to_tab <<-EOF >expect &&
     -+		100644 blob $(git rev-parse c1:file1)Qfile1
     -+		100644 blob $(git rev-parse c3:file3)Qfile3
     -+		EOF
     ++	q_to_tab <<-EOF >expect &&
     ++	100644 blob $(git -C base-b2-p rev-parse c1:file1)Qfile1
     ++	100644 blob $(git -C base-b2-p rev-parse c3:file3)Qfile3
     ++	EOF
      +
     -+		git ls-tree $TREE_OID >actual &&
     -+		test_cmp expect actual
     -+	)
     ++	git -C base-b2-p ls-tree $TREE_OID >actual &&
     ++	test_cmp expect actual
      +'
      +
       test_done
 2:  3bdfad03cca ! 2:  40d56544e6e merge-tree.c: allow specifying the merge-base when --stdin is passed
     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success '--stdin with both a succe
       # git merge-tree --write-tree --merge-base=c2 c1 c3
       #   Commit c1: add file1
      @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'specify merge-base as parent of branch2' '
     - 	)
     + 	test_cmp expect actual
       '
       
      +# Since the earlier tests have verified that individual merge-tree calls
      +# are doing the right thing, this test case is only used to test whether
      +# the input format is available.
      +
     -+test_expect_success '--stdin with both a normal merge and a merge-base specified merge' '
     -+	cd base-b2-p &&
     -+	printf "c1 c3\nc2 -- c1 c3" | git merge-tree --stdin >actual &&
     ++test_expect_success 'check the input format when --stdin is passed' '
     ++	test_when_finished "rm -rf repo" &&
     ++	git init repo &&
     ++	test_commit -C repo c1 &&
     ++	test_commit -C repo c2 &&
     ++	test_commit -C repo c3 &&
     ++	printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
      +
      +	printf "1\0" >expect &&
     -+	git merge-tree --write-tree -z c1 c3 >>expect &&
     ++	git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
      +	printf "\0" >>expect &&
      +
      +	printf "1\0" >>expect &&
     -+	git merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
     ++	git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
      +	printf "\0" >>expect &&
     ++
     ++	printf "1\0" >>expect &&
     ++	git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
     ++	printf "\0" >>expect &&
     ++
      +	test_cmp expect actual
      +'
      +

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v6 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-03 10:50         ` [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
@ 2022-11-03 10:50           ` Kyle Zhao via GitGitGadget
  2022-11-03 10:50           ` [PATCH v6 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2 siblings, 0 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-03 10:50 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

This does a merge of HEAD and branch, but uses branch^ as the merge-base.

And the reason why using an option flag instead of a positional argument
is to allow additional commits passed to merge-tree to be handled via an
octopus merge in the future.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 46 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 27 +++++++++++++++++++
 3 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 04bcc416e6e..d9dacb2ce54 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..62c6d43cdb9 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -406,6 +407,7 @@ struct merge_tree_options {
 };
 
 static int real_merge(struct merge_tree_options *o,
+		      const char *merge_base,
 		      const char *branch1, const char *branch2,
 		      const char *prefix)
 {
@@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (merge_base) {
+		struct commit *base_commit;
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		base_commit = lookup_commit_reference_by_name(merge_base);
+		if (!base_commit)
+			die(_("could not lookup commit %s"), merge_base);
+
+		opt.ancestor = merge_base;
+		base_tree = get_commit_tree(base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -487,6 +504,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 	struct merge_tree_options o = { .show_messages = -1 };
 	int expected_remaining_argc;
 	int original_argc;
+	const char *merge_base = NULL;
 
 	const char * const merge_tree_usage[] = {
 		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
@@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.use_stdin,
 			   N_("perform multiple merges, one per line of input"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			   &merge_base,
+			   N_("commit"),
+			   N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
@@ -529,6 +551,8 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 		if (o.mode == MODE_TRIVIAL)
 			die(_("--trivial-merge is incompatible with all other options"));
+		if (merge_base)
+			die(_("--merge-base is incompatible with --stdin"));
 		line_termination = '\0';
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
@@ -538,7 +562,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			if (!split[0] || !split[1] || split[2])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
+			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
@@ -581,7 +605,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 	/* Do the relevant type of merge */
 	if (o.mode == MODE_REAL)
-		return real_merge(&o, argv[0], argv[1], prefix);
+		return real_merge(&o, merge_base, argv[0], argv[1], prefix);
 	else
 		return trivial_merge(argv[0], argv[1], argv[2]);
 }
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index cac85591b52..6db96ccbaae 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,4 +860,31 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+#   Commit c1: add file1
+#   Commit c2: add file2 after c1
+#   Commit c3: add file3 after c2
+#   Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	test_when_finished "rm -rf base-b2-p" &&
+	git init base-b2-p &&
+	test_commit -C base-b2-p c1 file1 &&
+	test_commit -C base-b2-p c2 file2 &&
+	test_commit -C base-b2-p c3 file3 &&
+
+	# Testing
+	TREE_OID=$(git -C base-b2-p merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+	q_to_tab <<-EOF >expect &&
+	100644 blob $(git -C base-b2-p rev-parse c1:file1)Qfile1
+	100644 blob $(git -C base-b2-p rev-parse c3:file3)Qfile3
+	EOF
+
+	git -C base-b2-p ls-tree $TREE_OID >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 43+ messages in thread

* [PATCH v6 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed
  2022-11-03 10:50         ` [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2022-11-03 10:50           ` [PATCH v6 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-03 10:50           ` Kyle Zhao via GitGitGadget
  2022-11-11 19:44             ` Elijah Newren
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2 siblings, 1 reply; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-03 10:50 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

The previous commit added a `--merge-base` option in order to allow
using a specified merge-base for the merge.  Extend the input accepted
by `--stdin` to also allow a specified merge-base with each merge
requested.  For example:

    printf "<b3> -- <b1> <b2>" | git merge-tree --stdin

does a merge of b1 and b2, and uses b3 as the merge-base.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt | 14 ++++++++++++-
 builtin/merge-tree.c             | 21 ++++++++++++++++++--
 t/t4301-merge-tree-write-tree.sh | 34 ++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d9dacb2ce54..298c133fdb6 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -66,7 +66,8 @@ OPTIONS
 
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge.
+	specify a merge-base for the merge. This option is incompatible
+	with `--stdin`.
 
 [[OUTPUT]]
 OUTPUT
@@ -220,6 +221,17 @@ with linkgit:git-merge[1]:
   * any messages that would have been printed to stdout (the
     <<IM,Informational messages>>)
 
+INPUT FORMAT
+------------
+'git merge-tree --stdin' input format is fully text based. Each line
+has this format:
+
+	[<base-commit> -- ]<branch1> <branch2>
+
+If one line is separated by `--`, the string before the separator is
+used for specifying a merge-base for the merge and the string after
+the separator describes the branches to be merged.
+
 MISTAKES TO AVOID
 -----------------
 
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index 62c6d43cdb9..330f779e8bc 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -557,12 +557,29 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
 			int result;
+			const char *input_merge_base = NULL;
 
 			split = strbuf_split(&buf, ' ');
-			if (!split[0] || !split[1] || split[2])
+			if (!split[0] || !split[1])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
+			strbuf_rtrim(split[1]);
+
+			/* parse the merge-base */
+			if (!strcmp(split[1]->buf, "--")) {
+				input_merge_base = split[0]->buf;
+			}
+
+			if (input_merge_base && split[2] && split[3] && !split[4]) {
+				strbuf_rtrim(split[2]);
+				strbuf_rtrim(split[3]);
+				result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
+			} else if (!input_merge_base && !split[2]) {
+				result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
+			} else {
+				die(_("malformed input line: '%s'."), buf.buf);
+			}
+
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 6db96ccbaae..22b43f0003e 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,6 +860,13 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+
+test_expect_success '--merge-base is incompatible with --stdin' '
+	test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
+
+	grep "^fatal: --merge-base is incompatible with --stdin" expect
+'
+
 # specify merge-base as parent of branch2
 # git merge-tree --write-tree --merge-base=c2 c1 c3
 #   Commit c1: add file1
@@ -887,4 +894,31 @@ test_expect_success 'specify merge-base as parent of branch2' '
 	test_cmp expect actual
 '
 
+# Since the earlier tests have verified that individual merge-tree calls
+# are doing the right thing, this test case is only used to test whether
+# the input format is available.
+
+test_expect_success 'check the input format when --stdin is passed' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	test_commit -C repo c1 &&
+	test_commit -C repo c2 &&
+	test_commit -C repo c3 &&
+	printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
+
+	printf "1\0" >expect &&
+	git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH v6 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed
  2022-11-03 10:50           ` [PATCH v6 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
@ 2022-11-11 19:44             ` Elijah Newren
  0 siblings, 0 replies; 43+ messages in thread
From: Elijah Newren @ 2022-11-11 19:44 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget
  Cc: git, Taylor Blau, Ævar Arnfjörð Bjarmason,
	Kyle Zhao

Sorry for the delay; my Git time has sadly been quite limited.  :-(

On Thu, Nov 3, 2022 at 3:50 AM Kyle Zhao via GitGitGadget
<gitgitgadget@gmail.com> wrote:
> +# Since the earlier tests have verified that individual merge-tree calls
> +# are doing the right thing, this test case is only used to test whether
> +# the input format is available.

"the input format is available"?  I'm not sure exactly what that
means, but it seems almost certainly to not be the only thing it is
testing.  Perhaps you meant something like:

# Since the earlier tests have verified that individual merge-tree calls
# are doing the right thing, this test case is only used to verify that
# we can also trigger merges via --stdin, and that when we do we get
# the same answer as running a bunch of separate merges.

> +
> +test_expect_success 'check the input format when --stdin is passed' '
> +       test_when_finished "rm -rf repo" &&
> +       git init repo &&
> +       test_commit -C repo c1 &&
> +       test_commit -C repo c2 &&
> +       test_commit -C repo c3 &&
> +       printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
> +
> +       printf "1\0" >expect &&
> +       git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
> +       printf "\0" >>expect &&
> +
> +       printf "1\0" >>expect &&
> +       git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
> +       printf "\0" >>expect &&
> +
> +       printf "1\0" >>expect &&
> +       git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
> +       printf "\0" >>expect &&
> +
> +       test_cmp expect actual
> +'
> +
>  test_done

My above nit on your comment is my only remaining issue with your
implementation.  Looks good.

As an aside, I am still a little disappointed that the sole reason for
this series is limited to a usecase where this solution is at best an
interim hack[1][2]...but since I have had very limited time to work on
Git stuff including providing a proper solution for that usecase (in
the form of git-replay), and since it makes sense to include this
capability from a completeness perspective.

Anyway, thanks for patiently fixing everything up.  I think this
series should be ready to merge down once the comment is fixed up.

[1] https://lore.kernel.org/git/CABPp-BGBFyoY7m+KCA9MTifKmpZ0ccLgsYHahMCgPxuTpuUGPg@mail.gmail.com/
[2] https://lore.kernel.org/git/CABPp-BGXM=iRAgjNbZ0o3FSjj583GpkuFB6emUYwWjdFWb9-jQ@mail.gmail.com/

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-03 10:50         ` [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2022-11-03 10:50           ` [PATCH v6 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-11-03 10:50           ` [PATCH v6 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
@ 2022-11-11 23:45           ` Kyle Zhao via GitGitGadget
  2022-11-11 23:45             ` [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
                               ` (4 more replies)
  2 siblings, 5 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-11 23:45 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao

Thanks for everyone's careful reviews.

In this patch, I introduce a new --merge-base=<commit> option to allow
callers to specify a merge-base for the merge and extend the input accepted
by --stdin to also allow a specified merge-base with each merge requested.

Regards, Kyle

Changes since v1:

 * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
   merge-base is specified.
 * Fixed c style problem.
 * Moved commit lookup/die logic out to the parsing logic in
   cmd_merge_tree().
 * use test_commit for test

Changes since v2:

 * commit message
 * Rebased on top of en/merge-tree-sequence.
 * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char. To
   make it easier to pass parameters, I moved
   lookup_commit_reference_by_name() to real_ merge() again.
 * Added test comment.

Changes since v3:

 * support --merge-base in conjunction with --stdin

Changes since v4:

 * commit message
 * added input format document
 * changed the input format for specifying the merge-base when --stdin is
   passed
 * changed the output when --stdin and --merge-base are used at the same
   time
 * add comment for test

Changes since v5:

 * improved test: remove the test repo after the test; avoid sub-shell.

Changes since v6:

 * fixed comment of test

Kyle Zhao (2):
  merge-tree.c: add --merge-base=<commit> option
  merge-tree.c: allow specifying the merge-base when --stdin is passed

 Documentation/git-merge-tree.txt | 16 ++++++++
 builtin/merge-tree.c             | 65 ++++++++++++++++++++++++++------
 t/t4301-merge-tree-write-tree.sh | 62 ++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 12 deletions(-)


base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v7
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v7
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v6:

 1:  1cf1c69b8e8 = 1:  1cf1c69b8e8 merge-tree.c: add --merge-base=<commit> option
 2:  40d56544e6e ! 2:  48e55d4e97c merge-tree.c: allow specifying the merge-base when --stdin is passed
     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'specify merge-base as par
       '
       
      +# Since the earlier tests have verified that individual merge-tree calls
     -+# are doing the right thing, this test case is only used to test whether
     -+# the input format is available.
     ++# are doing the right thing, this test case is only used to verify that
     ++# we can also trigger merges via --stdin, and that when we do we get
     ++# the same answer as running a bunch of separate merges.
      +
      +test_expect_success 'check the input format when --stdin is passed' '
      +	test_when_finished "rm -rf repo" &&

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
@ 2022-11-11 23:45             ` Kyle Zhao via GitGitGadget
  2022-11-22  3:04               ` Junio C Hamano
  2022-11-11 23:45             ` [PATCH v7 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
                               ` (3 subsequent siblings)
  4 siblings, 1 reply; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-11 23:45 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

This does a merge of HEAD and branch, but uses branch^ as the merge-base.

And the reason why using an option flag instead of a positional argument
is to allow additional commits passed to merge-tree to be handled via an
octopus merge in the future.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 46 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 27 +++++++++++++++++++
 3 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 04bcc416e6e..d9dacb2ce54 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..62c6d43cdb9 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -406,6 +407,7 @@ struct merge_tree_options {
 };
 
 static int real_merge(struct merge_tree_options *o,
+		      const char *merge_base,
 		      const char *branch1, const char *branch2,
 		      const char *prefix)
 {
@@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (merge_base) {
+		struct commit *base_commit;
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		base_commit = lookup_commit_reference_by_name(merge_base);
+		if (!base_commit)
+			die(_("could not lookup commit %s"), merge_base);
+
+		opt.ancestor = merge_base;
+		base_tree = get_commit_tree(base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -487,6 +504,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 	struct merge_tree_options o = { .show_messages = -1 };
 	int expected_remaining_argc;
 	int original_argc;
+	const char *merge_base = NULL;
 
 	const char * const merge_tree_usage[] = {
 		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
@@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.use_stdin,
 			   N_("perform multiple merges, one per line of input"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			   &merge_base,
+			   N_("commit"),
+			   N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
@@ -529,6 +551,8 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 		if (o.mode == MODE_TRIVIAL)
 			die(_("--trivial-merge is incompatible with all other options"));
+		if (merge_base)
+			die(_("--merge-base is incompatible with --stdin"));
 		line_termination = '\0';
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
@@ -538,7 +562,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			if (!split[0] || !split[1] || split[2])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
+			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
@@ -581,7 +605,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 	/* Do the relevant type of merge */
 	if (o.mode == MODE_REAL)
-		return real_merge(&o, argv[0], argv[1], prefix);
+		return real_merge(&o, merge_base, argv[0], argv[1], prefix);
 	else
 		return trivial_merge(argv[0], argv[1], argv[2]);
 }
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index cac85591b52..6db96ccbaae 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,4 +860,31 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+#   Commit c1: add file1
+#   Commit c2: add file2 after c1
+#   Commit c3: add file3 after c2
+#   Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	test_when_finished "rm -rf base-b2-p" &&
+	git init base-b2-p &&
+	test_commit -C base-b2-p c1 file1 &&
+	test_commit -C base-b2-p c2 file2 &&
+	test_commit -C base-b2-p c3 file3 &&
+
+	# Testing
+	TREE_OID=$(git -C base-b2-p merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+	q_to_tab <<-EOF >expect &&
+	100644 blob $(git -C base-b2-p rev-parse c1:file1)Qfile1
+	100644 blob $(git -C base-b2-p rev-parse c3:file3)Qfile3
+	EOF
+
+	git -C base-b2-p ls-tree $TREE_OID >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 43+ messages in thread

* [PATCH v7 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2022-11-11 23:45             ` [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-11 23:45             ` Kyle Zhao via GitGitGadget
  2022-11-12  0:32             ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Elijah Newren
                               ` (2 subsequent siblings)
  4 siblings, 0 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-11 23:45 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

The previous commit added a `--merge-base` option in order to allow
using a specified merge-base for the merge.  Extend the input accepted
by `--stdin` to also allow a specified merge-base with each merge
requested.  For example:

    printf "<b3> -- <b1> <b2>" | git merge-tree --stdin

does a merge of b1 and b2, and uses b3 as the merge-base.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt | 14 ++++++++++++-
 builtin/merge-tree.c             | 21 +++++++++++++++++--
 t/t4301-merge-tree-write-tree.sh | 35 ++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d9dacb2ce54..298c133fdb6 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -66,7 +66,8 @@ OPTIONS
 
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge.
+	specify a merge-base for the merge. This option is incompatible
+	with `--stdin`.
 
 [[OUTPUT]]
 OUTPUT
@@ -220,6 +221,17 @@ with linkgit:git-merge[1]:
   * any messages that would have been printed to stdout (the
     <<IM,Informational messages>>)
 
+INPUT FORMAT
+------------
+'git merge-tree --stdin' input format is fully text based. Each line
+has this format:
+
+	[<base-commit> -- ]<branch1> <branch2>
+
+If one line is separated by `--`, the string before the separator is
+used for specifying a merge-base for the merge and the string after
+the separator describes the branches to be merged.
+
 MISTAKES TO AVOID
 -----------------
 
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index 62c6d43cdb9..330f779e8bc 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -557,12 +557,29 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
 			int result;
+			const char *input_merge_base = NULL;
 
 			split = strbuf_split(&buf, ' ');
-			if (!split[0] || !split[1] || split[2])
+			if (!split[0] || !split[1])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
+			strbuf_rtrim(split[1]);
+
+			/* parse the merge-base */
+			if (!strcmp(split[1]->buf, "--")) {
+				input_merge_base = split[0]->buf;
+			}
+
+			if (input_merge_base && split[2] && split[3] && !split[4]) {
+				strbuf_rtrim(split[2]);
+				strbuf_rtrim(split[3]);
+				result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
+			} else if (!input_merge_base && !split[2]) {
+				result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
+			} else {
+				die(_("malformed input line: '%s'."), buf.buf);
+			}
+
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 6db96ccbaae..a8983a0edcb 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,6 +860,13 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+
+test_expect_success '--merge-base is incompatible with --stdin' '
+	test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
+
+	grep "^fatal: --merge-base is incompatible with --stdin" expect
+'
+
 # specify merge-base as parent of branch2
 # git merge-tree --write-tree --merge-base=c2 c1 c3
 #   Commit c1: add file1
@@ -887,4 +894,32 @@ test_expect_success 'specify merge-base as parent of branch2' '
 	test_cmp expect actual
 '
 
+# Since the earlier tests have verified that individual merge-tree calls
+# are doing the right thing, this test case is only used to verify that
+# we can also trigger merges via --stdin, and that when we do we get
+# the same answer as running a bunch of separate merges.
+
+test_expect_success 'check the input format when --stdin is passed' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	test_commit -C repo c1 &&
+	test_commit -C repo c2 &&
+	test_commit -C repo c3 &&
+	printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
+
+	printf "1\0" >expect &&
+	git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
  2022-11-11 23:45             ` [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-11-11 23:45             ` [PATCH v7 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
@ 2022-11-12  0:32             ` Elijah Newren
  2022-11-13  4:53             ` Taylor Blau
  2022-11-24  3:37             ` [PATCH v8 0/3] " Kyle Zhao via GitGitGadget
  4 siblings, 0 replies; 43+ messages in thread
From: Elijah Newren @ 2022-11-12  0:32 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget
  Cc: git, Taylor Blau, Ævar Arnfjörð Bjarmason,
	Kyle Zhao

On Fri, Nov 11, 2022 at 3:45 PM Kyle Zhao via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> Thanks for everyone's careful reviews.
>
> In this patch, I introduce a new --merge-base=<commit> option to allow
> callers to specify a merge-base for the merge and extend the input accepted
> by --stdin to also allow a specified merge-base with each merge requested.
>
> Regards, Kyle
>
> Changes since v1:
>
>  * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
>    merge-base is specified.
>  * Fixed c style problem.
>  * Moved commit lookup/die logic out to the parsing logic in
>    cmd_merge_tree().
>  * use test_commit for test
>
> Changes since v2:
>
>  * commit message
>  * Rebased on top of en/merge-tree-sequence.
>  * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char. To
>    make it easier to pass parameters, I moved
>    lookup_commit_reference_by_name() to real_ merge() again.
>  * Added test comment.
>
> Changes since v3:
>
>  * support --merge-base in conjunction with --stdin
>
> Changes since v4:
>
>  * commit message
>  * added input format document
>  * changed the input format for specifying the merge-base when --stdin is
>    passed
>  * changed the output when --stdin and --merge-base are used at the same
>    time
>  * add comment for test
>
> Changes since v5:
>
>  * improved test: remove the test repo after the test; avoid sub-shell.
>
> Changes since v6:
>
>  * fixed comment of test

Thanks; this version looks good to me and is ready to merge down.

> Kyle Zhao (2):
>   merge-tree.c: add --merge-base=<commit> option
>   merge-tree.c: allow specifying the merge-base when --stdin is passed
>
>  Documentation/git-merge-tree.txt | 16 ++++++++
>  builtin/merge-tree.c             | 65 ++++++++++++++++++++++++++------
>  t/t4301-merge-tree-write-tree.sh | 62 ++++++++++++++++++++++++++++++
>  3 files changed, 131 insertions(+), 12 deletions(-)
>
>
> base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v7
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v7
> Pull-Request: https://github.com/gitgitgadget/git/pull/1397
>
> Range-diff vs v6:
>
>  1:  1cf1c69b8e8 = 1:  1cf1c69b8e8 merge-tree.c: add --merge-base=<commit> option
>  2:  40d56544e6e ! 2:  48e55d4e97c merge-tree.c: allow specifying the merge-base when --stdin is passed
>      @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'specify merge-base as par
>        '
>
>       +# Since the earlier tests have verified that individual merge-tree calls
>      -+# are doing the right thing, this test case is only used to test whether
>      -+# the input format is available.
>      ++# are doing the right thing, this test case is only used to verify that
>      ++# we can also trigger merges via --stdin, and that when we do we get
>      ++# the same answer as running a bunch of separate merges.
>       +
>       +test_expect_success 'check the input format when --stdin is passed' '
>       + test_when_finished "rm -rf repo" &&
>
> --
> gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
                               ` (2 preceding siblings ...)
  2022-11-12  0:32             ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Elijah Newren
@ 2022-11-13  4:53             ` Taylor Blau
  2022-11-13  4:54               ` Taylor Blau
  2022-11-24  3:37             ` [PATCH v8 0/3] " Kyle Zhao via GitGitGadget
  4 siblings, 1 reply; 43+ messages in thread
From: Taylor Blau @ 2022-11-13  4:53 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget
  Cc: git, Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason

On Fri, Nov 11, 2022 at 11:45:12PM +0000, Kyle Zhao via GitGitGadget wrote:
> Thanks for everyone's careful reviews.
>
> In this patch, I introduce a new --merge-base=<commit> option to allow
> callers to specify a merge-base for the merge and extend the input accepted
> by --stdin to also allow a specified merge-base with each merge requested.

Thanks for the updated round. I applied it locally, now let's see what
reviewers think of it...

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-13  4:53             ` Taylor Blau
@ 2022-11-13  4:54               ` Taylor Blau
  0 siblings, 0 replies; 43+ messages in thread
From: Taylor Blau @ 2022-11-13  4:54 UTC (permalink / raw)
  To: Taylor Blau
  Cc: Kyle Zhao via GitGitGadget, git, Elijah Newren, kylezhao,
	Ævar Arnfjörð Bjarmason

On Sat, Nov 12, 2022 at 11:53:34PM -0500, Taylor Blau wrote:
> On Fri, Nov 11, 2022 at 11:45:12PM +0000, Kyle Zhao via GitGitGadget wrote:
> > Thanks for everyone's careful reviews.
> >
> > In this patch, I introduce a new --merge-base=<commit> option to allow
> > callers to specify a merge-base for the merge and extend the input accepted
> > by --stdin to also allow a specified merge-base with each merge requested.
>
> Thanks for the updated round. I applied it locally, now let's see what
> reviewers think of it...

Oops ;-). I should have looked further down in my inbox before replying.
Looks like this is ready to merge down according to Elijah, so let's
start cooking this in 'next'.

Thanks again.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-11 23:45             ` [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-22  3:04               ` Junio C Hamano
  2022-11-22  3:52                 ` [Internet]Re: " kylezhao(赵柯宇)
  0 siblings, 1 reply; 43+ messages in thread
From: Junio C Hamano @ 2022-11-22  3:04 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget
  Cc: git, Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason

"Kyle Zhao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> +--merge-base=<commit>::
> +	Instead of finding the merge-bases for <branch1> and <branch2>,
> +	specify a merge-base for the merge.

OK.

> +	const char *merge_base = NULL;
>  
>  	const char * const merge_tree_usage[] = {
>  		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
> @@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
>  			   &o.use_stdin,
>  			   N_("perform multiple merges, one per line of input"),
>  			   PARSE_OPT_NONEG),
> +		OPT_STRING(0, "merge-base",
> +			   &merge_base,
> +			   N_("commit"),
> +			   N_("specify a merge-base for the merge")),
>  		OPT_END()
>  	};

This looks wrong, though.

Shouldn't "git merge-tree --merge-base=X --merge-base=Y A B"
allow you to compute the merge between A and B in a history
where there are two merge bases?

Unfortunately this is already in 'next', so let's see an incremental
fix on top.

Thanks.

^ permalink raw reply	[flat|nested] 43+ messages in thread

* RE: [Internet]Re: [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-22  3:04               ` Junio C Hamano
@ 2022-11-22  3:52                 ` kylezhao(赵柯宇)
  2022-11-22  4:28                   ` Junio C Hamano
  0 siblings, 1 reply; 43+ messages in thread
From: kylezhao(赵柯宇) @ 2022-11-22  3:52 UTC (permalink / raw)
  To: Junio C Hamano, Kyle Zhao via GitGitGadget
  Cc: git@vger.kernel.org, Elijah Newren, Taylor Blau,
	Ævar Arnfjörð Bjarmason

> > +	const char *merge_base = NULL;
> >
> >  	const char * const merge_tree_usage[] = {
> >  		N_("git merge-tree [--write-tree] [<options>] <branch1>
> > <branch2>"), @@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const
> char **argv, const char *prefix)
> >  			   &o.use_stdin,
> >  			   N_("perform multiple merges, one per line of input"),
> >  			   PARSE_OPT_NONEG),
> > +		OPT_STRING(0, "merge-base",
> > +			   &merge_base,
> > +			   N_("commit"),
> > +			   N_("specify a merge-base for the merge")),
> >  		OPT_END()
> >  	};
> 
> This looks wrong, though.
> 
> Shouldn't "git merge-tree --merge-base=X --merge-base=Y A B"
> allow you to compute the merge between A and B in a history where there are
> two merge bases?
> 
> Unfortunately this is already in 'next', so let's see an incremental fix on top.
> 
> Thanks.

I agree.

OPT_STRING only use the last value of "--merge-base".
It will mislead users, they may specify the merge-base multiple times, but found it doesn't work.

I went to check the api-parse-option.txt, but I didn't found an elegant solution to stop when the users uses the
second "--merge-base".  Did I miss it? Or we just need to mention this in the git-merge-tree.txt,
such as:
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge.
+            specify a merge-base for the merge. This option doesn't support
+            being specified multiple times, only the last value you provide will be used.


Thanks,
Kyle


^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [Internet]Re: [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-22  3:52                 ` [Internet]Re: " kylezhao(赵柯宇)
@ 2022-11-22  4:28                   ` Junio C Hamano
  2022-11-22  5:42                     ` Junio C Hamano
  0 siblings, 1 reply; 43+ messages in thread
From: Junio C Hamano @ 2022-11-22  4:28 UTC (permalink / raw)
  To: kylezhao(赵柯宇)
  Cc: Kyle Zhao via GitGitGadget, git@vger.kernel.org, Elijah Newren,
	Taylor Blau, Ævar Arnfjörð Bjarmason

"kylezhao(赵柯宇)" <kylezhao@tencent.com> writes:

> I went to check the api-parse-option.txt, but I didn't found an
> elegant solution to stop when the users uses the second
> "--merge-base".

That's not even a fix, as it does not allow specifying more than one
merge bases, is it?

Just like how builtin/merge-tree.c::real_merge() is prepared to
handle parent1 and parent2 that have multiple merge bases and pass
all of them to the underlying merge machinery, you can accumulate
multiple strings using OPT_STRING_LIST() and use all of them as
merge_bases, no?

^ permalink raw reply	[flat|nested] 43+ messages in thread

* Re: [Internet]Re: [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-22  4:28                   ` Junio C Hamano
@ 2022-11-22  5:42                     ` Junio C Hamano
  2022-11-22  7:47                       ` [Internet]Re: " kylezhao(赵柯宇)
  0 siblings, 1 reply; 43+ messages in thread
From: Junio C Hamano @ 2022-11-22  5:42 UTC (permalink / raw)
  To: kylezhao(赵柯宇)
  Cc: Kyle Zhao via GitGitGadget, git@vger.kernel.org, Elijah Newren,
	Taylor Blau, Ævar Arnfjörð Bjarmason

Junio C Hamano <gitster@pobox.com> writes:

> "kylezhao(赵柯宇)" <kylezhao@tencent.com> writes:
>
>> I went to check the api-parse-option.txt, but I didn't found an
>> elegant solution to stop when the users uses the second
>> "--merge-base".
>
> That's not even a fix, as it does not allow specifying more than one
> merge bases, is it?
>
> Just like how builtin/merge-tree.c::real_merge() is prepared to
> handle parent1 and parent2 that have multiple merge bases and pass
> all of them to the underlying merge machinery, you can accumulate
> multiple strings using OPT_STRING_LIST() and use all of them as
> merge_bases, no?

I am a bit torn on this, though.  

Because it uses lookup_commit_reference_by_name() to obtain
base_commit in addition to parent1 and parent2, and then
get_commit_tree() on them to get their trees, the real_merge()
function in the posted patch is incapable of accepting a single
"pretend as if this tree object is the common ancestor tree" and
merging the two tree objects.  But once that flaw is fixed, using
merge_incore_nonrecursive() with an explicitly given "base", we can
merge totally trees regardless of how the commits they are found in
are related in the history, which is a very logical thing to do.
And while operating in that mode, there is no way to accept more
than one "base".

So, I would be PERFECTLY HAPPY if this new mode of operation can
take only one "base" tree object, if it allows BASE, PARENT1,
and PARENT2 to be all tree objects, not necessarily commit objects.

But if we insist that PARENT1 and PARENT2 must be commits, then I
would find it very unsatisfying if we only took a single BASE that
also must be a commit.  If the merge-base has to be a tree or trees,
then there is no way to perform recursive merge (because you cannot
compute common ancestors across tree objects) , so it is perfectly
justifiable to take only a single base (and error out upon seeing a
second --merge-base=X option).

But it has to be a commit, then there is no justification to forbid
recursive operation, and I do not see a good reason to take only one
COMMON thing.

So, it is easy to say "let's take the current patch as a good first
step", but it is unclear what the good second step would be.

We could correct the code to allow PARENT1, PARENT2 and BASE to be
tree objects, not necessarily commits, when only a single merge-base
is specified.  That corrects the current flaw that tree objects
cannot be used.  And then when more than one BASE is given (or no
BASE is given---which is the original code), we could correct the
code to call merge_incore_recursive() instead.

But the amount of the effort to get there from the current codebase
(without your patch) feels more or less the same ballpark as the
patch in question, so...

^ permalink raw reply	[flat|nested] 43+ messages in thread

* RE: [Internet]Re: Re: [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option
  2022-11-22  5:42                     ` Junio C Hamano
@ 2022-11-22  7:47                       ` kylezhao(赵柯宇)
  0 siblings, 0 replies; 43+ messages in thread
From: kylezhao(赵柯宇) @ 2022-11-22  7:47 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Kyle Zhao via GitGitGadget, git@vger.kernel.org, Elijah Newren,
	Taylor Blau, Ævar Arnfjörð Bjarmason

> I am a bit torn on this, though.
> 
> Because it uses lookup_commit_reference_by_name() to obtain base_commit
> in addition to parent1 and parent2, and then
> get_commit_tree() on them to get their trees, the real_merge() function in the
> posted patch is incapable of accepting a single "pretend as if this tree object is
> the common ancestor tree" and merging the two tree objects.  But once that
> flaw is fixed, using
> merge_incore_nonrecursive() with an explicitly given "base", we can merge
> totally trees regardless of how the commits they are found in are related in the
> history, which is a very logical thing to do.
> And while operating in that mode, there is no way to accept more than one
> "base".
> 
> So, I would be PERFECTLY HAPPY if this new mode of operation can take only
> one "base" tree object, if it allows BASE, PARENT1, and PARENT2 to be all tree
> objects, not necessarily commit objects.
> 
> But if we insist that PARENT1 and PARENT2 must be commits, then I would find
> it very unsatisfying if we only took a single BASE that also must be a commit.  If
> the merge-base has to be a tree or trees, then there is no way to perform
> recursive merge (because you cannot compute common ancestors across tree
> objects) , so it is perfectly justifiable to take only a single base (and error out
> upon seeing a second --merge-base=X option).
> 
> But it has to be a commit, then there is no justification to forbid recursive
> operation, and I do not see a good reason to take only one COMMON thing.
> 
> So, it is easy to say "let's take the current patch as a good first step", but it is
> unclear what the good second step would be.

Yeah, I think so.  
In fact, I planned to implement a version that specifies *only one* merge-base
at the beginning and made the format of this option would be possible to
extend to support multiple bases and octopus at the same time.

> 
> We could correct the code to allow PARENT1, PARENT2 and BASE to be tree
> objects, not necessarily commits, when only a single merge-base is specified.
> That corrects the current flaw that tree objects cannot be used.  And then when
> more than one BASE is given (or no BASE is given---which is the original code),
> we could correct the code to call merge_incore_recursive() instead.

I prefer this solution.

> 
> But the amount of the effort to get there from the current codebase (without
> your patch) feels more or less the same ballpark as the patch in question, so...

It means we need to support specifying multiple bases in the first version, which makes
merge-tree command more complete. Since individual merge-tree support multiple bases, 
--stdin mode seems to need to support this too.

However, I can't think of when the user needs to manually specify multiple bases for a merge ;).
In other words, do we really need to support multiple bases in the first version?

Thanks,
Kyle




^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v8 0/3] merge-tree: allow specifying a base commit when --write-tree is passed
  2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
                               ` (3 preceding siblings ...)
  2022-11-13  4:53             ` Taylor Blau
@ 2022-11-24  3:37             ` Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 1/3] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
                                 ` (2 more replies)
  4 siblings, 3 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-24  3:37 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao

Users may specify the merge-base multiple times, such as "git merge-tree
--merge-base=X --merge-base=Y A B", but found it doesn't work.

This patch I fix the description of option "--merge-base".

By the way, the amount of the effort support specifying multiple bases from
the codebase without these patches feels more or less the same ballpark as
these patches.

One point worth discussing is whether manually specifying multiple bases is
a rarely used feature and do we need to support it in the first version.

If needed, It might take a while for me to implement it, and the feature
might be delayed to the version after 2.39.

Thanks, Kyle

Changes since v1:

 * Changed merge_incore_recursive() to merge_incore_nonrecursive() when
   merge-base is specified.
 * Fixed c style problem.
 * Moved commit lookup/die logic out to the parsing logic in
   cmd_merge_tree().
 * use test_commit for test

Changes since v2:

 * commit message
 * Rebased on top of en/merge-tree-sequence.
 * Set opt.ancestor to o->merge_base. Because opt.ancestor is a *char. To
   make it easier to pass parameters, I moved
   lookup_commit_reference_by_name() to real_ merge() again.
 * Added test comment.

Changes since v3:

 * support --merge-base in conjunction with --stdin

Changes since v4:

 * commit message
 * added input format document
 * changed the input format for specifying the merge-base when --stdin is
   passed
 * changed the output when --stdin and --merge-base are used at the same
   time
 * add comment for test

Changes since v5:

 * improved test: remove the test repo after the test; avoid sub-shell.

Changes since v6:

 * fixed comment of test

Changes since v7:

 * docs: fix description of the --merge-base option

Kyle Zhao (3):
  merge-tree.c: add --merge-base=<commit> option
  merge-tree.c: allow specifying the merge-base when --stdin is passed
  docs: fix description of the `--merge-base` option

 Documentation/git-merge-tree.txt | 16 ++++++++
 builtin/merge-tree.c             | 65 ++++++++++++++++++++++++++------
 t/t4301-merge-tree-write-tree.sh | 62 ++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 12 deletions(-)


base-commit: ec1edbcb56ac05e9980299b05924c5c1b51d68b4
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1397%2Fkeyu98%2Fkz%2Fmerge-tree-option-merge-base-v8
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1397/keyu98/kz/merge-tree-option-merge-base-v8
Pull-Request: https://github.com/gitgitgadget/git/pull/1397

Range-diff vs v7:

 1:  1cf1c69b8e8 = 1:  1cf1c69b8e8 merge-tree.c: add --merge-base=<commit> option
 2:  48e55d4e97c = 2:  48e55d4e97c merge-tree.c: allow specifying the merge-base when --stdin is passed
 -:  ----------- > 3:  c21466d1db0 docs: fix description of the `--merge-base` option

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 43+ messages in thread

* [PATCH v8 1/3] merge-tree.c: add --merge-base=<commit> option
  2022-11-24  3:37             ` [PATCH v8 0/3] " Kyle Zhao via GitGitGadget
@ 2022-11-24  3:37               ` Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 2/3] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 3/3] docs: fix description of the `--merge-base` option Kyle Zhao via GitGitGadget
  2 siblings, 0 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-24  3:37 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

This patch will give our callers more flexibility to use `git merge-tree`,
such as:

    git merge-tree --write-tree --merge-base=branch^ HEAD branch

This does a merge of HEAD and branch, but uses branch^ as the merge-base.

And the reason why using an option flag instead of a positional argument
is to allow additional commits passed to merge-tree to be handled via an
octopus merge in the future.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt |  4 +++
 builtin/merge-tree.c             | 46 ++++++++++++++++++++++++--------
 t/t4301-merge-tree-write-tree.sh | 27 +++++++++++++++++++
 3 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 04bcc416e6e..d9dacb2ce54 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -64,6 +64,10 @@ OPTIONS
 	share no common history.  This flag can be given to override that
 	check and make the merge proceed anyway.
 
+--merge-base=<commit>::
+	Instead of finding the merge-bases for <branch1> and <branch2>,
+	specify a merge-base for the merge.
+
 [[OUTPUT]]
 OUTPUT
 ------
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..62c6d43cdb9 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -3,6 +3,7 @@
 #include "tree-walk.h"
 #include "xdiff-interface.h"
 #include "help.h"
+#include "commit.h"
 #include "commit-reach.h"
 #include "merge-ort.h"
 #include "object-store.h"
@@ -406,6 +407,7 @@ struct merge_tree_options {
 };
 
 static int real_merge(struct merge_tree_options *o,
+		      const char *merge_base,
 		      const char *branch1, const char *branch2,
 		      const char *prefix)
 {
@@ -432,16 +434,31 @@ static int real_merge(struct merge_tree_options *o,
 	opt.branch1 = branch1;
 	opt.branch2 = branch2;
 
-	/*
-	 * Get the merge bases, in reverse order; see comment above
-	 * merge_incore_recursive in merge-ort.h
-	 */
-	merge_bases = get_merge_bases(parent1, parent2);
-	if (!merge_bases && !o->allow_unrelated_histories)
-		die(_("refusing to merge unrelated histories"));
-	merge_bases = reverse_commit_list(merge_bases);
+	if (merge_base) {
+		struct commit *base_commit;
+		struct tree *base_tree, *parent1_tree, *parent2_tree;
+
+		base_commit = lookup_commit_reference_by_name(merge_base);
+		if (!base_commit)
+			die(_("could not lookup commit %s"), merge_base);
+
+		opt.ancestor = merge_base;
+		base_tree = get_commit_tree(base_commit);
+		parent1_tree = get_commit_tree(parent1);
+		parent2_tree = get_commit_tree(parent2);
+		merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
+	} else {
+		/*
+		 * Get the merge bases, in reverse order; see comment above
+		 * merge_incore_recursive in merge-ort.h
+		 */
+		merge_bases = get_merge_bases(parent1, parent2);
+		if (!merge_bases && !o->allow_unrelated_histories)
+			die(_("refusing to merge unrelated histories"));
+		merge_bases = reverse_commit_list(merge_bases);
+		merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
+	}
 
-	merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
 	if (result.clean < 0)
 		die(_("failure to merge"));
 
@@ -487,6 +504,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 	struct merge_tree_options o = { .show_messages = -1 };
 	int expected_remaining_argc;
 	int original_argc;
+	const char *merge_base = NULL;
 
 	const char * const merge_tree_usage[] = {
 		N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
@@ -515,6 +533,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			   &o.use_stdin,
 			   N_("perform multiple merges, one per line of input"),
 			   PARSE_OPT_NONEG),
+		OPT_STRING(0, "merge-base",
+			   &merge_base,
+			   N_("commit"),
+			   N_("specify a merge-base for the merge")),
 		OPT_END()
 	};
 
@@ -529,6 +551,8 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 		if (o.mode == MODE_TRIVIAL)
 			die(_("--trivial-merge is incompatible with all other options"));
+		if (merge_base)
+			die(_("--merge-base is incompatible with --stdin"));
 		line_termination = '\0';
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
@@ -538,7 +562,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 			if (!split[0] || !split[1] || split[2])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
+			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
@@ -581,7 +605,7 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 
 	/* Do the relevant type of merge */
 	if (o.mode == MODE_REAL)
-		return real_merge(&o, argv[0], argv[1], prefix);
+		return real_merge(&o, merge_base, argv[0], argv[1], prefix);
 	else
 		return trivial_merge(argv[0], argv[1], argv[2]);
 }
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index cac85591b52..6db96ccbaae 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,4 +860,31 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+#   Commit c1: add file1
+#   Commit c2: add file2 after c1
+#   Commit c3: add file3 after c2
+#   Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+	# Setup
+	test_when_finished "rm -rf base-b2-p" &&
+	git init base-b2-p &&
+	test_commit -C base-b2-p c1 file1 &&
+	test_commit -C base-b2-p c2 file2 &&
+	test_commit -C base-b2-p c3 file3 &&
+
+	# Testing
+	TREE_OID=$(git -C base-b2-p merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+	q_to_tab <<-EOF >expect &&
+	100644 blob $(git -C base-b2-p rev-parse c1:file1)Qfile1
+	100644 blob $(git -C base-b2-p rev-parse c3:file3)Qfile3
+	EOF
+
+	git -C base-b2-p ls-tree $TREE_OID >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 43+ messages in thread

* [PATCH v8 2/3] merge-tree.c: allow specifying the merge-base when --stdin is passed
  2022-11-24  3:37             ` [PATCH v8 0/3] " Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 1/3] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
@ 2022-11-24  3:37               ` Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 3/3] docs: fix description of the `--merge-base` option Kyle Zhao via GitGitGadget
  2 siblings, 0 replies; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-24  3:37 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

The previous commit added a `--merge-base` option in order to allow
using a specified merge-base for the merge.  Extend the input accepted
by `--stdin` to also allow a specified merge-base with each merge
requested.  For example:

    printf "<b3> -- <b1> <b2>" | git merge-tree --stdin

does a merge of b1 and b2, and uses b3 as the merge-base.

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt | 14 ++++++++++++-
 builtin/merge-tree.c             | 21 +++++++++++++++++--
 t/t4301-merge-tree-write-tree.sh | 35 ++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index d9dacb2ce54..298c133fdb6 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -66,7 +66,8 @@ OPTIONS
 
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge.
+	specify a merge-base for the merge. This option is incompatible
+	with `--stdin`.
 
 [[OUTPUT]]
 OUTPUT
@@ -220,6 +221,17 @@ with linkgit:git-merge[1]:
   * any messages that would have been printed to stdout (the
     <<IM,Informational messages>>)
 
+INPUT FORMAT
+------------
+'git merge-tree --stdin' input format is fully text based. Each line
+has this format:
+
+	[<base-commit> -- ]<branch1> <branch2>
+
+If one line is separated by `--`, the string before the separator is
+used for specifying a merge-base for the merge and the string after
+the separator describes the branches to be merged.
+
 MISTAKES TO AVOID
 -----------------
 
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index 62c6d43cdb9..330f779e8bc 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -557,12 +557,29 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 		while (strbuf_getline_lf(&buf, stdin) != EOF) {
 			struct strbuf **split;
 			int result;
+			const char *input_merge_base = NULL;
 
 			split = strbuf_split(&buf, ' ');
-			if (!split[0] || !split[1] || split[2])
+			if (!split[0] || !split[1])
 				die(_("malformed input line: '%s'."), buf.buf);
 			strbuf_rtrim(split[0]);
-			result = real_merge(&o, merge_base, split[0]->buf, split[1]->buf, prefix);
+			strbuf_rtrim(split[1]);
+
+			/* parse the merge-base */
+			if (!strcmp(split[1]->buf, "--")) {
+				input_merge_base = split[0]->buf;
+			}
+
+			if (input_merge_base && split[2] && split[3] && !split[4]) {
+				strbuf_rtrim(split[2]);
+				strbuf_rtrim(split[3]);
+				result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
+			} else if (!input_merge_base && !split[2]) {
+				result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
+			} else {
+				die(_("malformed input line: '%s'."), buf.buf);
+			}
+
 			if (result < 0)
 				die(_("merging cannot continue; got unclean result of %d"), result);
 			strbuf_list_free(split);
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index 6db96ccbaae..a8983a0edcb 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -860,6 +860,13 @@ test_expect_success '--stdin with both a successful and a conflicted merge' '
 	test_cmp expect actual
 '
 
+
+test_expect_success '--merge-base is incompatible with --stdin' '
+	test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
+
+	grep "^fatal: --merge-base is incompatible with --stdin" expect
+'
+
 # specify merge-base as parent of branch2
 # git merge-tree --write-tree --merge-base=c2 c1 c3
 #   Commit c1: add file1
@@ -887,4 +894,32 @@ test_expect_success 'specify merge-base as parent of branch2' '
 	test_cmp expect actual
 '
 
+# Since the earlier tests have verified that individual merge-tree calls
+# are doing the right thing, this test case is only used to verify that
+# we can also trigger merges via --stdin, and that when we do we get
+# the same answer as running a bunch of separate merges.
+
+test_expect_success 'check the input format when --stdin is passed' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	test_commit -C repo c1 &&
+	test_commit -C repo c2 &&
+	test_commit -C repo c3 &&
+	printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
+
+	printf "1\0" >expect &&
+	git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	printf "1\0" >>expect &&
+	git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
+	printf "\0" >>expect &&
+
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 43+ messages in thread

* [PATCH v8 3/3] docs: fix description of the `--merge-base` option
  2022-11-24  3:37             ` [PATCH v8 0/3] " Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 1/3] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
  2022-11-24  3:37               ` [PATCH v8 2/3] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
@ 2022-11-24  3:37               ` Kyle Zhao via GitGitGadget
  2022-11-25  5:28                 ` Junio C Hamano
  2 siblings, 1 reply; 43+ messages in thread
From: Kyle Zhao via GitGitGadget @ 2022-11-24  3:37 UTC (permalink / raw)
  To: git
  Cc: Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason, Kyle Zhao, Kyle Zhao

From: Kyle Zhao <kylezhao@tencent.com>

Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
---
 Documentation/git-merge-tree.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
index 298c133fdb6..88ee942101a 100644
--- a/Documentation/git-merge-tree.txt
+++ b/Documentation/git-merge-tree.txt
@@ -66,8 +66,8 @@ OPTIONS
 
 --merge-base=<commit>::
 	Instead of finding the merge-bases for <branch1> and <branch2>,
-	specify a merge-base for the merge. This option is incompatible
-	with `--stdin`.
+	specify a merge-base for the merge, and specifying multiple bases is
+	currently not supported. This option is incompatible with `--stdin`.
 
 [[OUTPUT]]
 OUTPUT
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 43+ messages in thread

* Re: [PATCH v8 3/3] docs: fix description of the `--merge-base` option
  2022-11-24  3:37               ` [PATCH v8 3/3] docs: fix description of the `--merge-base` option Kyle Zhao via GitGitGadget
@ 2022-11-25  5:28                 ` Junio C Hamano
  0 siblings, 0 replies; 43+ messages in thread
From: Junio C Hamano @ 2022-11-25  5:28 UTC (permalink / raw)
  To: Kyle Zhao via GitGitGadget
  Cc: git, Elijah Newren, kylezhao, Taylor Blau,
	Ævar Arnfjörð Bjarmason

"Kyle Zhao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Kyle Zhao <kylezhao@tencent.com>
>
> Signed-off-by: Kyle Zhao <kylezhao@tencent.com>
> ---
>  Documentation/git-merge-tree.txt | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt
> index 298c133fdb6..88ee942101a 100644
> --- a/Documentation/git-merge-tree.txt
> +++ b/Documentation/git-merge-tree.txt
> @@ -66,8 +66,8 @@ OPTIONS
>  
>  --merge-base=<commit>::
>  	Instead of finding the merge-bases for <branch1> and <branch2>,
> -	specify a merge-base for the merge. This option is incompatible
> -	with `--stdin`.
> +	specify a merge-base for the merge, and specifying multiple bases is
> +	currently not supported. This option is incompatible with `--stdin`.

Makes sense.  Will queue.

Thanks.

^ permalink raw reply	[flat|nested] 43+ messages in thread

end of thread, other threads:[~2022-11-25  5:29 UTC | newest]

Thread overview: 43+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-27 12:12 [PATCH] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
2022-10-27 18:09 ` Junio C Hamano
2022-10-28  8:20   ` Elijah Newren
2022-10-28 16:03     ` Junio C Hamano
2022-10-29  1:52       ` Elijah Newren
2022-10-28 11:43   ` [Internet]Re: " kylezhao(赵柯宇)
2022-10-28  8:13 ` Elijah Newren
2022-10-28 11:54   ` [Internet]Re: " kylezhao(赵柯宇)
2022-10-28 11:55 ` [PATCH v2] " Kyle Zhao via GitGitGadget
2022-10-29  1:32   ` Elijah Newren
2022-10-29  3:42   ` [PATCH v3] " Kyle Zhao via GitGitGadget
2022-11-01  1:08     ` Elijah Newren
2022-11-01  8:55     ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Kyle Zhao via GitGitGadget
2022-11-01  8:55       ` [PATCH v4 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
2022-11-01  8:55       ` [PATCH v4 2/2] merge-tree.c: support --merge-base in conjunction with --stdin Kyle Zhao via GitGitGadget
2022-11-03  1:13         ` Elijah Newren
2022-11-03  3:28           ` [Internet]Re: " kylezhao(赵柯宇)
2022-11-01 21:19       ` [PATCH v4 0/2] merge-tree: allow specifying a base commit when --write-tree is passed Taylor Blau
2022-11-03  3:29       ` [PATCH v5 " Kyle Zhao via GitGitGadget
2022-11-03  3:29         ` [PATCH v5 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
2022-11-03  8:36           ` Ævar Arnfjörð Bjarmason
2022-11-03  9:43             ` [Internet]Re: " kylezhao(赵柯宇)
2022-11-03  3:29         ` [PATCH v5 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
2022-11-03 10:50         ` [PATCH v6 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
2022-11-03 10:50           ` [PATCH v6 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
2022-11-03 10:50           ` [PATCH v6 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
2022-11-11 19:44             ` Elijah Newren
2022-11-11 23:45           ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Kyle Zhao via GitGitGadget
2022-11-11 23:45             ` [PATCH v7 1/2] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
2022-11-22  3:04               ` Junio C Hamano
2022-11-22  3:52                 ` [Internet]Re: " kylezhao(赵柯宇)
2022-11-22  4:28                   ` Junio C Hamano
2022-11-22  5:42                     ` Junio C Hamano
2022-11-22  7:47                       ` [Internet]Re: " kylezhao(赵柯宇)
2022-11-11 23:45             ` [PATCH v7 2/2] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
2022-11-12  0:32             ` [PATCH v7 0/2] merge-tree: allow specifying a base commit when --write-tree " Elijah Newren
2022-11-13  4:53             ` Taylor Blau
2022-11-13  4:54               ` Taylor Blau
2022-11-24  3:37             ` [PATCH v8 0/3] " Kyle Zhao via GitGitGadget
2022-11-24  3:37               ` [PATCH v8 1/3] merge-tree.c: add --merge-base=<commit> option Kyle Zhao via GitGitGadget
2022-11-24  3:37               ` [PATCH v8 2/3] merge-tree.c: allow specifying the merge-base when --stdin is passed Kyle Zhao via GitGitGadget
2022-11-24  3:37               ` [PATCH v8 3/3] docs: fix description of the `--merge-base` option Kyle Zhao via GitGitGadget
2022-11-25  5:28                 ` Junio C Hamano

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).