range-diff: offer --left-only/--right-only options

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>

Johannes Schindelin committed Feb 5, 2021 at 14:46 UTC 1e79f973266cfe0e3bab0e26e869b682078e457d
5 files changed +40 -4
Documentation/git-range-diff.txt
+9
@@ -10,6 +10,7 @@ SYNOPSIS
10 [verse]
11 'git range-diff' [--color=[<when>]] [--no-color] [<diff-options>]
12 [--no-dual-color] [--creation-factor=<factor>]
13 + [--left-only | --right-only]
14 ( <range1> <range2> | <rev1>...<rev2> | <base> <rev1> <rev2> )
15
16 DESCRIPTION
@@ -57,6 +58,14 @@ to revert to color all lines according to the outer diff markers
58 See the ``Algorithm`` section below for an explanation why this is
59 needed.
60
61 +--left-only::
62 + Suppress commits that are missing from the first specified range
63 + (or the "left range" when using the `<rev1>...<rev2>` format).
64 +
65 +--right-only::
66 + Suppress commits that are missing from the second specified range
67 + (or the "right range" when using the `<rev1>...<rev2>` format).
68 +
69 --[no-]notes[=<ref>]::
70 This flag is passed to the `git log` program
71 (see linkgit:git-log[1]) that generates the patches.
builtin/range-diff.c
+7 -1
@@ -20,7 +20,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
20 .diffopt = &diffopt,
21 .other_arg = &other_arg
22 };
23 - int simple_color = -1;
23 + int simple_color = -1, left_only = 0, right_only = 0;
24 struct option range_diff_options[] = {
25 OPT_INTEGER(0, "creation-factor",
26 &range_diff_opts.creation_factor,
@@ -30,6 +30,10 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
30 OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
31 N_("notes"), N_("passed to 'git log'"),
32 PARSE_OPT_OPTARG),
33 + OPT_BOOL(0, "left-only", &left_only,
34 + N_("only emit output related to the first range")),
35 + OPT_BOOL(0, "right-only", &right_only,
36 + N_("only emit output related to the second range")),
37 OPT_END()
38 };
39 struct option *options;
@@ -87,6 +91,8 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
91 FREE_AND_NULL(options);
92
93 range_diff_opts.dual_color = simple_color < 1;
94 + range_diff_opts.left_only = left_only;
95 + range_diff_opts.right_only = right_only;
96 res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
97
98 strvec_clear(&other_arg);
range-diff.c
+8 -3
@@ -513,7 +513,8 @@ static void output(struct string_list *a, struct string_list *b,
513
514 /* Show unmatched LHS commit whose predecessors were shown. */
515 if (i < a->nr && a_util->matching < 0) {
516 - output_pair_header(&opts, patch_no_width,
516 + if (!range_diff_opts->right_only)
517 + output_pair_header(&opts, patch_no_width,
518 &buf, &dashes, a_util, NULL);
519 i++;
520 continue;
@@ -521,7 +522,8 @@ static void output(struct string_list *a, struct string_list *b,
522
523 /* Show unmatched RHS commits. */
524 while (j < b->nr && b_util->matching < 0) {
524 - output_pair_header(&opts, patch_no_width,
525 + if (!range_diff_opts->left_only)
526 + output_pair_header(&opts, patch_no_width,
527 &buf, &dashes, NULL, b_util);
528 b_util = ++j < b->nr ? b->items[j].util : NULL;
529 }
@@ -551,7 +553,10 @@ int show_range_diff(const char *range1, const char *range2,
553 struct string_list branch1 = STRING_LIST_INIT_DUP;
554 struct string_list branch2 = STRING_LIST_INIT_DUP;
555
554 - if (read_patches(range1, &branch1, range_diff_opts->other_arg))
556 + if (range_diff_opts->left_only && range_diff_opts->right_only)
557 + res = error(_("--left-only and --right-only are mutually exclusive"));
558 +
559 + if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
560 res = error(_("could not parse log for '%s'"), range1);
561 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
562 res = error(_("could not parse log for '%s'"), range2);
range-diff.h
+1
@@ -9,6 +9,7 @@
9 struct range_diff_options {
10 int creation_factor;
11 unsigned dual_color:1;
12 + unsigned left_only:1, right_only:1;
13 const struct diff_options *diffopt; /* may be NULL */
14 const struct strvec *other_arg; /* may be NULL */
15 };
t/t3206-range-diff.sh
+15
@@ -717,4 +717,19 @@ test_expect_success 'format-patch --range-diff with multiple notes' '
717 test_cmp expect actual
718 '
719
720 +test_expect_success '--left-only/--right-only' '
721 + git switch --orphan left-right &&
722 + test_commit first &&
723 + test_commit unmatched &&
724 + test_commit common &&
725 + git switch -C left-right first &&
726 + git cherry-pick common &&
727 +
728 + git range-diff -s --left-only ...common >actual &&
729 + head_oid=$(git rev-parse --short HEAD) &&
730 + common_oid=$(git rev-parse --short common) &&
731 + echo "1: $head_oid = 2: $common_oid common" >expect &&
732 + test_cmp expect actual
733 +'
734 +
735 test_done