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

Re: [PATCH v2] range-diff: optionally include merge commits' diffs in the analysis

From
Elijah Newren <newren@gmail.com>
Date
Nov 8, 2024, 17:04 UTC
Message-ID
<CABPp-BGeSwDHNeDdEwf+mo5q33GO3f0UiFpVOpjiLF3msEvrfg@mail.gmail.com>
In-Reply-To
<pull.1734.v2.git.1731073383564.gitgitgadget@gmail.com>

On Fri, Nov 8, 2024 at 5:43 AM Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 105 quoted lines
>
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> The `git log` command already offers support for including diffs for
> merges, via the `--diff-merges=<format>` option.
>
> Let's add corresponding support for `git range-diff`, too. This makes it
> more convenient to spot differences between iterations of non-linear
> contributions, where so-called "evil merges" are sometimes necessary and
> need to be reviewed, too.
>
> In my code reviews, I found the `--diff-merges=first-parent` option
> particularly useful.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>     Support diff merges option in range diff
>
>     The git range-diff command does the same with merge commits as git
>     rebase: It ignores them.
>
>     However, when comparing branch thickets it can be quite illuminating to
>     watch out for inadvertent changes in merge commits, in particular when
>     some "evil" merges have been replayed, i.e. merges that needed to
>     introduce changes outside of the merge conflicts (e.g. when one branch
>     changed a function's signature and another branch introduced a caller of
>     said function), in case the replayed merge is no longer "evil" and
>     therefore potentially incorrect.
>
>     Let's introduce support for the --diff-merges option that is passed
>     through to those git log commands.
>
>     I had a need for this earlier this year and got it working, leaving the
>     GitGitGadget PR in a draft mode. Phil Blain found it and kindly
>     nerd-sniped me into readying it for submitting, so say thanks to Phil!
>
>     Changes since v1:
>
>      * Changed the documentation to recommend first-parent mode instead of
>        vaguely talking about various modes' merits.
>      * Disallowed the no-arg --diff-merges option (because --diff-merges
>        requires an argument).
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1734%2Fdscho%2Fsupport-diff-merges-option-in-range-diff-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1734/dscho/support-diff-merges-option-in-range-diff-v2
> Pull-Request: https://github.com/gitgitgadget/git/pull/1734
>
> Range-diff vs v1:
>
>  1:  11361e07af8 ! 1:  d01a395900b range-diff: optionally include merge commits' diffs in the analysis
>      @@ Documentation/git-range-diff.txt: to revert to color all lines according to the
>       + corresponding `--diff-merges=<format>` option of linkgit:git-log[1],
>       + and include them in the comparison.
>       ++
>      -+Note: Some of the formats supported by linkgit:git-log[1] make less sense in
>      -+the context of the `range-diff` command than other formats, so choose wisely!
>      ++Note: In the common case, the `first-parent` mode will be the most natural one
>      ++to use, as it is consistent with the idea that a merge is kind of a "meta
>      ++patch", comprising all the merged commits' patches into a single one.
>       +
>        --[no-]notes[=<ref>]::
>         This flag is passed to the `git log` program
>      @@ builtin/range-diff.c: int cmd_range_diff(int argc,
>                                   N_("notes"), N_("passed to 'git log'"),
>                                   PARSE_OPT_OPTARG),
>       +         OPT_PASSTHRU_ARGV(0, "diff-merges", &diff_merges_arg,
>      -+                           N_("style"), N_("passed to 'git log'"),
>      -+                           PARSE_OPT_OPTARG),
>      ++                           N_("style"), N_("passed to 'git log'"), 0),
>                 OPT_BOOL(0, "left-only", &left_only,
>                          N_("only emit output related to the first range")),
>                 OPT_BOOL(0, "right-only", &right_only,
>
>
>  Documentation/git-range-diff.txt | 11 ++++++++++-
>  builtin/range-diff.c             | 10 ++++++++++
>  range-diff.c                     | 15 +++++++++++----
>  range-diff.h                     |  1 +
>  t/t3206-range-diff.sh            | 16 ++++++++++++++++
>  5 files changed, 48 insertions(+), 5 deletions(-)
>
> diff --git a/Documentation/git-range-diff.txt b/Documentation/git-range-diff.txt
> index fbdbe0befeb..17a85957877 100644
> --- a/Documentation/git-range-diff.txt
> +++ b/Documentation/git-range-diff.txt
> @@ -10,7 +10,7 @@ SYNOPSIS
>  [verse]
>  'git range-diff' [--color=[<when>]] [--no-color] [<diff-options>]
>         [--no-dual-color] [--creation-factor=<factor>]
> -       [--left-only | --right-only]
> +       [--left-only | --right-only] [--diff-merges=<format>]
>         ( <range1> <range2> | <rev1>...<rev2> | <base> <rev1> <rev2> )
>         [[--] <path>...]
>
> @@ -81,6 +81,15 @@ to revert to color all lines according to the outer diff markers
>         Suppress commits that are missing from the second specified range
>         (or the "right range" when using the `<rev1>...<rev2>` format).
>
> +--diff-merges=<format>::
> +       Instead of ignoring merge commits, generate diffs for them using the
> +       corresponding `--diff-merges=<format>` option of linkgit:git-log[1],
> +       and include them in the comparison.
> ++
> +Note: In the common case, the `first-parent` mode will be the most natural one
> +to use,

I think we need more wording around "common case"; I believe this "common case" is when you are diffing against a merely transplanted series of commits (a series created by rebasing without inserting, removing, or minimally modifying those commits in the process) that `first-parent` makes sense. And it only makes sense in that case.

I think `remerge-diff` generally makes sense here, both in the cases when `first-parent` makes sense and when `first-parent` does not. It could be improved by suppressing the inclusion of short commit ids (and maybe also commit messages) in the labels of conflict markers. I suspect that issue might make `remerge-diff` less useful than `first-parent` in simple common cases currently, but I think it's the right thing to build upon for what you are trying to view.

If `remerge-diff` didn't exist, I think I'd always use `dense-combined` over `first-parent` because of this merely-transplanted limitation. I suspect dense-combined would probably be kind of ugly and hard to parse when there is an actual evil merge, but it'd at least limit what it shows to the evil merge content.

> as it is consistent with the idea that a merge is kind of a "meta
> +patch", comprising all the merged commits' patches into a single one.

Doesn't this wording of yours naturally lead to the idea that `first-parent` is a bad choice if patches leading to the merge have been inserted, removed, or more than minimally modified? It points out that first-parent is a diff over the whole range, and so range-diff shows you a diff of the diffs over the whole range. If you want to look at the "evilness" of merge commits, i.e. the user-generated portion of the diffs included in by merge commits, you need either remerge-diff or dense-combined.

Show 151 quoted lines
> +
>  --[no-]notes[=<ref>]::
>         This flag is passed to the `git log` program
>         (see linkgit:git-log[1]) that generates the patches.
> diff --git a/builtin/range-diff.c b/builtin/range-diff.c
> index 1b33ab66a7b..901de5d133d 100644
> --- a/builtin/range-diff.c
> +++ b/builtin/range-diff.c
> @@ -21,6 +21,7 @@ int cmd_range_diff(int argc,
>  {
>         struct diff_options diffopt = { NULL };
>         struct strvec other_arg = STRVEC_INIT;
> +       struct strvec diff_merges_arg = STRVEC_INIT;
>         struct range_diff_options range_diff_opts = {
>                 .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
>                 .diffopt = &diffopt,
> @@ -36,6 +37,8 @@ int cmd_range_diff(int argc,
>                 OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
>                                   N_("notes"), N_("passed to 'git log'"),
>                                   PARSE_OPT_OPTARG),
> +               OPT_PASSTHRU_ARGV(0, "diff-merges", &diff_merges_arg,
> +                                 N_("style"), N_("passed to 'git log'"), 0),
>                 OPT_BOOL(0, "left-only", &left_only,
>                          N_("only emit output related to the first range")),
>                 OPT_BOOL(0, "right-only", &right_only,
> @@ -62,6 +65,12 @@ int cmd_range_diff(int argc,
>         if (!simple_color)
>                 diffopt.use_color = 1;
>
> +       /* If `--diff-merges` was specified, imply `--merges` */
> +       if (diff_merges_arg.nr) {
> +               range_diff_opts.include_merges = 1;
> +               strvec_pushv(&other_arg, diff_merges_arg.v);
> +       }
> +
>         for (i = 0; i < argc; i++)
>                 if (!strcmp(argv[i], "--")) {
>                         dash_dash = i;
> @@ -155,6 +164,7 @@ int cmd_range_diff(int argc,
>         res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
>
>         strvec_clear(&other_arg);
> +       strvec_clear(&diff_merges_arg);
>         strbuf_release(&range1);
>         strbuf_release(&range2);
>
> diff --git a/range-diff.c b/range-diff.c
> index bbb0952264b..9e59733059b 100644
> --- a/range-diff.c
> +++ b/range-diff.c
> @@ -38,7 +38,8 @@ struct patch_util {
>   * as struct object_id (will need to be free()d).
>   */
>  static int read_patches(const char *range, struct string_list *list,
> -                       const struct strvec *other_arg)
> +                       const struct strvec *other_arg,
> +                       unsigned int include_merges)
>  {
>         struct child_process cp = CHILD_PROCESS_INIT;
>         struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
> @@ -49,7 +50,7 @@ static int read_patches(const char *range, struct string_list *list,
>         size_t size;
>         int ret = -1;
>
> -       strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
> +       strvec_pushl(&cp.args, "log", "--no-color", "-p",
>                      "--reverse", "--date-order", "--decorate=no",
>                      "--no-prefix", "--submodule=short",
>                      /*
> @@ -64,6 +65,8 @@ static int read_patches(const char *range, struct string_list *list,
>                      "--pretty=medium",
>                      "--show-notes-by-default",
>                      NULL);
> +       if (!include_merges)
> +               strvec_push(&cp.args, "--no-merges");
>         strvec_push(&cp.args, range);
>         if (other_arg)
>                 strvec_pushv(&cp.args, other_arg->v);
> @@ -96,11 +99,14 @@ static int read_patches(const char *range, struct string_list *list,
>                 }
>
>                 if (skip_prefix(line, "commit ", &p)) {
> +                       char *q;
>                         if (util) {
>                                 string_list_append(list, buf.buf)->util = util;
>                                 strbuf_reset(&buf);
>                         }
>                         CALLOC_ARRAY(util, 1);
> +                       if (include_merges && (q = strstr(p, " (from ")))
> +                               *q = '\0';
>                         if (repo_get_oid(the_repository, p, &util->oid)) {
>                                 error(_("could not parse commit '%s'"), p);
>                                 FREE_AND_NULL(util);
> @@ -571,13 +577,14 @@ int show_range_diff(const char *range1, const char *range2,
>
>         struct string_list branch1 = STRING_LIST_INIT_DUP;
>         struct string_list branch2 = STRING_LIST_INIT_DUP;
> +       unsigned int include_merges = range_diff_opts->include_merges;
>
>         if (range_diff_opts->left_only && range_diff_opts->right_only)
>                 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
>
> -       if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
> +       if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg, include_merges))
>                 res = error(_("could not parse log for '%s'"), range1);
> -       if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
> +       if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg, include_merges))
>                 res = error(_("could not parse log for '%s'"), range2);
>
>         if (!res) {
> diff --git a/range-diff.h b/range-diff.h
> index 2f69f6a434d..cd85000b5a0 100644
> --- a/range-diff.h
> +++ b/range-diff.h
> @@ -16,6 +16,7 @@ struct range_diff_options {
>         int creation_factor;
>         unsigned dual_color:1;
>         unsigned left_only:1, right_only:1;
> +       unsigned include_merges:1;
>         const struct diff_options *diffopt; /* may be NULL */
>         const struct strvec *other_arg; /* may be NULL */
>  };
> diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> index 86010931ab6..c18a3fdab83 100755
> --- a/t/t3206-range-diff.sh
> +++ b/t/t3206-range-diff.sh
> @@ -909,4 +909,20 @@ test_expect_success 'submodule changes are shown irrespective of diff.submodule'
>         test_cmp expect actual
>  '
>
> +test_expect_success '--diff-merges' '
> +       renamed_oid=$(git rev-parse --short renamed-file) &&
> +       tree=$(git merge-tree unmodified renamed-file) &&
> +       clean=$(git commit-tree -m merge -p unmodified -p renamed-file $tree) &&
> +       clean_oid=$(git rev-parse --short $clean) &&
> +       conflict=$(git commit-tree -m merge -p unmodified -p renamed-file^ $tree) &&
> +       conflict_oid=$(git rev-parse --short $conflict) &&
> +
> +       git range-diff --diff-merges=1 $clean...$conflict >actual &&
> +       cat >expect <<-EOF &&
> +       1:  $renamed_oid < -:  ------- s/12/B/
> +       2:  $clean_oid = 1:  $conflict_oid merge
> +       EOF
> +       test_cmp expect actual
> +'
> +
>  test_done
>
> base-commit: 777489f9e09c8d0dd6b12f9d90de6376330577a2
> --
> gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin
Message 10 of 21 in “range-diff: optionally include merge commits' diffs in the analysis”
  1. range-diff: optionally include merge commits' diffs in the analysisJohannes Schindelin via GitGitGadget, Nov 7, 2024
  2. Junio C HamanoNov 8, 2024
  3. Johannes SixtNov 8, 2024
  4. Johannes SchindelinNov 8, 2024
  5. Elijah NewrenNov 9, 2024
  6. Junio C HamanoNov 11, 2024
  7. Junio C HamanoNov 8, 2024
  8. Elijah NewrenNov 8, 2024
  9. range-diff: optionally include merge commits' diffs in the analysisJohannes Schindelin via GitGitGadget, Nov 8, 2024
  10. Elijah NewrenNov 8, 2024
  11. Johannes SchindelinNov 11, 2024
  12. Philippe BlainNov 10, 2024
  13. Johannes SchindelinNov 11, 2024
  14. Junio C HamanoNov 26, 2024
  15. Junio C HamanoNov 11, 2024
  16. Elijah NewrenNov 11, 2024
  17. Junio C HamanoNov 12, 2024
  18. 0/2 Support diff merges option in range diffJohannes Schindelin via GitGitGadget, Dec 16, 2024
  19. 1/2 range-diff: optionally include merge commits' diffs in the analysisJohannes Schindelin via GitGitGadget, Dec 16, 2024
  20. 2/2 range-diff: introduce the convenience option `--remerge-diff`Johannes Schindelin via GitGitGadget, Dec 16, 2024
  21. Elijah NewrenNov 8, 2024

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.