]> git.ipfire.org Git - thirdparty/git.git/commitdiff
range-diff: offer --left-only/--right-only options
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 5 Feb 2021 14:46:13 +0000 (14:46 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 7 Feb 2021 05:14:31 +0000 (21:14 -0800)
When comparing commit ranges, one is frequently interested only in one
side, such as asking the question "Has this patch that I submitted to
the Git mailing list been applied?": one would only care about the part
of the output that corresponds to the commits in a local branch.

To make that possible, imitate the `git rev-list` options `--left-only`
and `--right-only`.

This addresses https://github.com/gitgitgadget/git/issues/206

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-range-diff.txt
builtin/range-diff.c
range-diff.c
range-diff.h
t/t3206-range-diff.sh

index 9701c1e5fdd5a016c360b8102f0193dfa0fb90fb..dd46bb7f506c61129939ffc867da6834d4a0d15d 100644 (file)
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git range-diff' [--color=[<when>]] [--no-color] [<diff-options>]
        [--no-dual-color] [--creation-factor=<factor>]
+       [--left-only | --right-only]
        ( <range1> <range2> | <rev1>...<rev2> | <base> <rev1> <rev2> )
 
 DESCRIPTION
@@ -57,6 +58,14 @@ to revert to color all lines according to the outer diff markers
        See the ``Algorithm`` section below for an explanation why this is
        needed.
 
+--left-only::
+       Suppress commits that are missing from the first specified range
+       (or the "left range" when using the `<rev1>...<rev2>` format).
+
+--right-only::
+       Suppress commits that are missing from the second specified range
+       (or the "right range" when using the `<rev1>...<rev2>` format).
+
 --[no-]notes[=<ref>]::
        This flag is passed to the `git log` program
        (see linkgit:git-log[1]) that generates the patches.
index 73fea7960168023642f7687a3011383a77074b03..930f273b85a679543b0545798ab8f9cd03073c67 100644 (file)
@@ -20,7 +20,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
                .diffopt = &diffopt,
                .other_arg = &other_arg
        };
-       int simple_color = -1;
+       int simple_color = -1, left_only = 0, right_only = 0;
        struct option range_diff_options[] = {
                OPT_INTEGER(0, "creation-factor",
                            &range_diff_opts.creation_factor,
@@ -30,6 +30,10 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
                OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
                                  N_("notes"), N_("passed to 'git log'"),
                                  PARSE_OPT_OPTARG),
+               OPT_BOOL(0, "left-only", &left_only,
+                        N_("only emit output related to the first range")),
+               OPT_BOOL(0, "right-only", &right_only,
+                        N_("only emit output related to the second range")),
                OPT_END()
        };
        struct option *options;
@@ -87,6 +91,8 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
        FREE_AND_NULL(options);
 
        range_diff_opts.dual_color = simple_color < 1;
+       range_diff_opts.left_only = left_only;
+       range_diff_opts.right_only = right_only;
        res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
 
        strvec_clear(&other_arg);
index 514125b90cce4ae12bd05371ae591586f0c8dcd2..0a0d9ed2f12f5b9203edf62b62c6e7a16db129cd 100644 (file)
@@ -513,7 +513,8 @@ static void output(struct string_list *a, struct string_list *b,
 
                /* Show unmatched LHS commit whose predecessors were shown. */
                if (i < a->nr && a_util->matching < 0) {
-                       output_pair_header(&opts, patch_no_width,
+                       if (!range_diff_opts->right_only)
+                               output_pair_header(&opts, patch_no_width,
                                           &buf, &dashes, a_util, NULL);
                        i++;
                        continue;
@@ -521,7 +522,8 @@ static void output(struct string_list *a, struct string_list *b,
 
                /* Show unmatched RHS commits. */
                while (j < b->nr && b_util->matching < 0) {
-                       output_pair_header(&opts, patch_no_width,
+                       if (!range_diff_opts->left_only)
+                               output_pair_header(&opts, patch_no_width,
                                           &buf, &dashes, NULL, b_util);
                        b_util = ++j < b->nr ? b->items[j].util : NULL;
                }
@@ -551,7 +553,10 @@ 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;
 
-       if (read_patches(range1, &branch1, range_diff_opts->other_arg))
+       if (range_diff_opts->left_only && range_diff_opts->right_only)
+               res = error(_("--left-only and --right-only are mutually exclusive"));
+
+       if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
                res = error(_("could not parse log for '%s'"), range1);
        if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
                res = error(_("could not parse log for '%s'"), range2);
index 0bae6b0cb15eb5831b9c072af1930d6edd67efd2..27c9adfd2be46b1267ef655d6a80a5433a31b1c0 100644 (file)
@@ -9,6 +9,7 @@
 struct range_diff_options {
        int creation_factor;
        unsigned dual_color:1;
+       unsigned left_only:1, right_only:1;
        const struct diff_options *diffopt; /* may be NULL */
        const struct strvec *other_arg; /* may be NULL */
 };
index 6eb344be0312a4e125a036d673b56f9b236a822c..323439d21112e968337513b0bb28d05553324f52 100755 (executable)
@@ -717,4 +717,19 @@ test_expect_success 'format-patch --range-diff with multiple notes' '
        test_cmp expect actual
 '
 
+test_expect_success '--left-only/--right-only' '
+       git switch --orphan left-right &&
+       test_commit first &&
+       test_commit unmatched &&
+       test_commit common &&
+       git switch -C left-right first &&
+       git cherry-pick common &&
+
+       git range-diff -s --left-only ...common >actual &&
+       head_oid=$(git rev-parse --short HEAD) &&
+       common_oid=$(git rev-parse --short common) &&
+       echo "1:  $head_oid = 2:  $common_oid common" >expect &&
+       test_cmp expect actual
+'
+
 test_done