git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 09/10] builtin/diff-tree: learn --merge-base

From
Denton Liu <liu.denton@gmail.com>
Date
Sep 21, 2020, 17:27 UTC
Message-ID
<20200921172740.GA946178@generichostname>
In-Reply-To
<xmqqzh5jf73t.fsf@gitster.c.googlers.com>
Hi Junio,
On Mon, Sep 21, 2020 at 09:05:26AM -0700, Junio C Hamano wrote:
Show 17 quoted lines
> Denton Liu <liu.denton@gmail.com> writes:
> 
> > Hi Junio,
> >
> > On Fri, Sep 18, 2020 at 09:52:39AM -0700, Junio C Hamano wrote:
> >> I wonder if turning the builtin/diff-tree.c to match that structure
> >> make the result easier to understand (and I'll be perfectly happy if
> >> the answer to this question turns out to be "no, the result of the
> >> posted patch is the easiest to follow").
> >
> > git diff-tree does not even recognise ranges so as a result, the else
> > case does not even need to exist there, unlike in git diff.
> 
> (caution: before morning caffeine so what I say may be totally off)
> 
> Do you mean "git diff-tree HEAD^..HEAD" would fail, or something
> else?

Yes, that is what I meant but I can see that what I wrote is totally wrong. I was reading git-diff-tree.txt and I assumed that ranges were not supported at all.

Anyway, now that I've realised my mistake, I've rewritten the diff-tree part so that the structure matches what was written in diff and it should be easier to follow.

-- >8 --
From: Denton Liu <liu.denton@gmail.com>
Date: Mon, 14 Sep 2020 11:36:52 -0700
Subject: [PATCH] builtin/diff-tree: learn --merge-base

The previous commit introduced ---merge-base a way to take the diff between the working tree or index and the merge base between an arbitrary commit and HEAD. It makes sense to extend this option to support the case where two commits are given too and behave in a manner identical to `git diff A...B`.

Introduce the --merge-base flag as an alternative to triple-dot notation. Thus, we would be able to write the above as `git diff --merge-base A B`.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 Documentation/git-diff-tree.txt      |  7 ++++-
 Documentation/git-diff.txt           |  8 ++++--
 builtin/diff-tree.c                  | 17 +++++++++++-
 builtin/diff.c                       | 39 +++++++++++++++++++---------
 t/t4068-diff-symmetric-merge-base.sh | 34 ++++++++++++++++++++++++
 5 files changed, 89 insertions(+), 16 deletions(-)
diff --git a/Documentation/git-diff-tree.txt b/Documentation/git-diff-tree.txt
index 5c8a2a5e97..2fc24c542f 100644
--- a/Documentation/git-diff-tree.txt
+++ b/Documentation/git-diff-tree.txt
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git diff-tree' [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty]
-	      [-t] [-r] [-c | --cc] [--combined-all-paths] [--root]
+	      [-t] [-r] [-c | --cc] [--combined-all-paths] [--root] [--merge-base]
 	      [<common diff options>] <tree-ish> [<tree-ish>] [<path>...]
 
 DESCRIPTION
@@ -43,6 +43,11 @@ include::diff-options.txt[]
 	When `--root` is specified the initial commit will be shown as a big
 	creation event. This is equivalent to a diff against the NULL tree.
 
+--merge-base::
+	Instead of comparing the <tree-ish>s directly, use the merge
+	base between the two <tree-ish>s as the "before" side.  There
+	must be two <tree-ish>s given and they must both be commits.
+
 --stdin::
 	When `--stdin` is specified, the command does not take
 	<tree-ish> arguments from the command line.  Instead, it
diff --git a/Documentation/git-diff.txt b/Documentation/git-diff.txt
index 762ee6d074..7f4c8a8ce7 100644
--- a/Documentation/git-diff.txt
+++ b/Documentation/git-diff.txt
@@ -11,7 +11,7 @@ SYNOPSIS
 [verse]
 'git diff' [<options>] [<commit>] [--] [<path>...]
 'git diff' [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]
-'git diff' [<options>] <commit> [<commit>...] <commit> [--] [<path>...]
+'git diff' [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]
 'git diff' [<options>] <commit>...<commit> [--] [<path>...]
 'git diff' [<options>] <blob> <blob>
 'git diff' [<options>] --no-index [--] <path> <path>
@@ -62,10 +62,14 @@ of <commit> and HEAD.  `git diff --merge-base A` is equivalent to
 	branch name to compare with the tip of a different
 	branch.
 
-'git diff' [<options>] <commit> <commit> [--] [<path>...]::
+'git diff' [<options>] [--merge-base] <commit> <commit> [--] [<path>...]::
 
 	This is to view the changes between two arbitrary
 	<commit>.
++
+If --merge-base is given, use the merge base of the two commits for the
+"before" side.  `git diff --merge-base A B` is equivalent to
+`git diff $(git merge-base A B) B`.
 
 'git diff' [<options>] <commit> <commit>... <commit> [--] [<path>...]::
 
diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c
index 802363d0a2..9fc95e959f 100644
--- a/builtin/diff-tree.c
+++ b/builtin/diff-tree.c
@@ -111,6 +111,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 	struct setup_revision_opt s_r_opt;
 	struct userformat_want w;
 	int read_stdin = 0;
+	int merge_base = 0;
 
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage(diff_tree_usage);
@@ -143,9 +144,18 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 			read_stdin = 1;
 			continue;
 		}
+		if (!strcmp(arg, "--merge-base")) {
+			merge_base = 1;
+			continue;
+		}
 		usage(diff_tree_usage);
 	}
 
+	if (read_stdin && merge_base)
+		die(_("--stdin and --merge-base are mutually exclusive"));
+	if (merge_base && opt->pending.nr != 2)
+		die(_("--merge-base only works with two commits"));
+
 	/*
 	 * NOTE!  We expect "a..b" to expand to "^a b" but it is
 	 * perfectly valid for revision range parser to yield "b ^a",
@@ -165,7 +175,12 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 	case 2:
 		tree1 = opt->pending.objects[0].item;
 		tree2 = opt->pending.objects[1].item;
-		if (tree2->flags & UNINTERESTING) {
+		if (merge_base) {
+			struct object_id oid;
+
+			diff_get_merge_base(opt, &oid);
+			tree1 = lookup_object(the_repository, &oid);
+		} else if (tree2->flags & UNINTERESTING) {
 			SWAP(tree2, tree1);
 		}
 		diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
diff --git a/builtin/diff.c b/builtin/diff.c
index 1baea18ae0..b50fc68c2a 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -26,7 +26,7 @@
 static const char builtin_diff_usage[] =
 "git diff [<options>] [<commit>] [--] [<path>...]\n"
 "   or: git diff [<options>] --cached [<commit>] [--] [<path>...]\n"
-"   or: git diff [<options>] <commit> [<commit>...] <commit> [--] [<path>...]\n"
+"   or: git diff [<options>] <commit> [--merge-base] [<commit>...] <commit> [--] [<path>...]\n"
 "   or: git diff [<options>] <commit>...<commit>] [--] [<path>...]\n"
 "   or: git diff [<options>] <blob> <blob>]\n"
 "   or: git diff [<options>] --no-index [--] <path> <path>]\n"
@@ -172,19 +172,34 @@ static int builtin_diff_tree(struct rev_info *revs,
 			     struct object_array_entry *ent1)
 {
 	const struct object_id *(oid[2]);
-	int swap = 0;
+	struct object_id mb_oid;
+	int merge_base = 0;
 
-	if (argc > 1)
-		usage(builtin_diff_usage);
+	while (1 < argc) {
+		const char *arg = argv[1];
+		if (!strcmp(arg, "--merge-base"))
+			merge_base = 1;
+		else
+			usage(builtin_diff_usage);
+		argv++; argc--;
+	}
 
-	/*
-	 * We saw two trees, ent0 and ent1.  If ent1 is uninteresting,
-	 * swap them.
-	 */
-	if (ent1->item->flags & UNINTERESTING)
-		swap = 1;
-	oid[swap] = &ent0->item->oid;
-	oid[1 - swap] = &ent1->item->oid;
+	if (merge_base) {
+		diff_get_merge_base(revs, &mb_oid);
+		oid[0] = &mb_oid;
+		oid[1] = &revs->pending.objects[1].item->oid;
+	} else {
+		int swap = 0;
+
+		/*
+		 * We saw two trees, ent0 and ent1.  If ent1 is uninteresting,
+		 * swap them.
+		 */
+		if (ent1->item->flags & UNINTERESTING)
+			swap = 1;
+		oid[swap] = &ent0->item->oid;
+		oid[1 - swap] = &ent1->item->oid;
+	}
 	diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
 	log_tree_diff_flush(revs);
 	return 0;
diff --git a/t/t4068-diff-symmetric-merge-base.sh b/t/t4068-diff-symmetric-merge-base.sh
index 49432379cb..03487cc945 100755
--- a/t/t4068-diff-symmetric-merge-base.sh
+++ b/t/t4068-diff-symmetric-merge-base.sh
@@ -156,4 +156,38 @@ do
 	'
 done
 
+for cmd in diff-tree diff
+do
+	test_expect_success "$cmd --merge-base with two commits" '
+		git $cmd commit-C master >expect &&
+		git $cmd --merge-base br2 master >actual &&
+		test_cmp expect actual
+	'
+
+	test_expect_success "$cmd --merge-base commit and non-commit" '
+		test_must_fail git $cmd --merge-base br2 master^{tree} 2>err &&
+		test_i18ngrep "fatal: --merge-base only works with commits" err
+	'
+
+	test_expect_success "$cmd --merge-base with no merge bases and two commits" '
+		test_must_fail git $cmd --merge-base br2 br3 2>err &&
+		test_i18ngrep "fatal: no merge base found" err
+	'
+
+	test_expect_success "$cmd --merge-base with multiple merge bases and two commits" '
+		test_must_fail git $cmd --merge-base master br1 2>err &&
+		test_i18ngrep "fatal: multiple merge bases found" err
+	'
+done
+
+test_expect_success 'diff-tree --merge-base with one commit' '
+	test_must_fail git diff-tree --merge-base master 2>err &&
+	test_i18ngrep "fatal: --merge-base only works with two commits" err
+'
+
+test_expect_success 'diff --merge-base with range' '
+	test_must_fail git diff --merge-base br2..br3 2>err &&
+	test_i18ngrep "fatal: --merge-base does not work with ranges" err
+'
+
 test_done
-- 
2.28.0.760.g8d73e04208
Previous: Junio C HamanoNext: Junio C Hamano
Message 25 of 58 in “builtin/diff: learn --merge-base”
  1. 0/4 builtin/diff: learn --merge-baseDenton Liu, Sep 5, 2020
  2. 1/4 t4068: remove unnecessary >tmpDenton Liu, Sep 5, 2020
  3. 3/4 builtin/diff: parse --no-index using parse_options()Denton Liu, Sep 5, 2020
  4. 2/4 git-diff.txt: backtick quote command textDenton Liu, Sep 5, 2020
  5. 4/4 builtin/diff: learn --merge-baseDenton Liu, Sep 5, 2020
  6. Junio C HamanoSep 6, 2020
  7. 0/4 builtin/diff: learn --merge-baseDenton Liu, Sep 10, 2020
  8. 1/4 t4068: remove unnecessary >tmpDenton Liu, Sep 10, 2020
  9. 2/4 git-diff.txt: backtick quote command textDenton Liu, Sep 10, 2020
  10. 3/4 builtin/diff: parse --no-index using parse_options()Denton Liu, Sep 10, 2020
  11. Junio C HamanoSep 10, 2020
  12. Denton LiuSep 13, 2020
  13. Junio C HamanoSep 13, 2020
  14. 4/4 builtin/diff: learn --merge-baseDenton Liu, Sep 10, 2020
  15. 00/10 builtin/diff: learn --merge-baseDenton Liu, Sep 17, 2020
  16. 01/10 t4068: remove unnecessary >tmpDenton Liu, Sep 17, 2020
  17. 02/10 git-diff-index.txt: make --cached description a proper sentenceDenton Liu, Sep 17, 2020
  18. 10/10 contrib/completion: complete `git diff --merge-base`Denton Liu, Sep 17, 2020
  19. 09/10 builtin/diff-tree: learn --merge-baseDenton Liu, Sep 17, 2020
  20. Junio C HamanoSep 17, 2020
  21. Denton LiuSep 18, 2020
  22. Junio C HamanoSep 18, 2020
  23. Denton LiuSep 20, 2020
  24. Junio C HamanoSep 21, 2020
  25. Denton LiuSep 21, 2020
  26. Junio C HamanoSep 21, 2020
  27. Junio C HamanoSep 21, 2020
  28. Denton LiuSep 21, 2020
  29. Junio C HamanoSep 21, 2020
  30. Denton LiuSep 23, 2020
  31. Junio C HamanoSep 25, 2020
  32. Denton LiuSep 26, 2020
  33. 08/10 builtin/diff-index: learn --merge-baseDenton Liu, Sep 17, 2020
  34. Junio C HamanoSep 17, 2020
  35. Jeff KingSep 17, 2020
  36. Junio C HamanoSep 18, 2020
  37. Jeff KingSep 18, 2020
  38. 05/10 diff-lib: accept option flags in run_diff_index()Denton Liu, Sep 17, 2020
  39. Junio C HamanoSep 17, 2020
  40. 04/10 contrib/completion: extract common diff/difftool optionsDenton Liu, Sep 17, 2020
  41. 06/10 diff-lib: define diff_get_merge_base()Denton Liu, Sep 17, 2020
  42. Junio C HamanoSep 17, 2020
  43. Denton LiuSep 18, 2020
  44. Junio C HamanoSep 19, 2020
  45. 03/10 git-diff.txt: backtick quote command textDenton Liu, Sep 17, 2020
  46. 07/10 t4068: add --merge-base testsDenton Liu, Sep 17, 2020
  47. 00/10 builtin/diff: learn --merge-baseDenton Liu, Sep 20, 2020
  48. 01/10 t4068: remove unnecessary >tmpDenton Liu, Sep 20, 2020
  49. 02/10 git-diff-index.txt: make --cached description a proper sentenceDenton Liu, Sep 20, 2020
  50. 06/10 diff-lib: define diff_get_merge_base()Denton Liu, Sep 20, 2020
  51. 05/10 diff-lib: accept option flags in run_diff_index()Denton Liu, Sep 20, 2020
  52. 03/10 git-diff.txt: backtick quote command textDenton Liu, Sep 20, 2020
  53. 07/10 t4068: add --merge-base testsDenton Liu, Sep 20, 2020
  54. 08/10 builtin/diff-index: learn --merge-baseDenton Liu, Sep 20, 2020
  55. Martin ÅgrenSep 29, 2020
  56. 04/10 contrib/completion: extract common diff/difftool optionsDenton Liu, Sep 20, 2020
  57. 09/10 builtin/diff-tree: learn --merge-baseDenton Liu, Sep 20, 2020
  58. 10/10 contrib/completion: complete `git diff --merge-base`Denton Liu, Sep 20, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.