Skip to content

Commit 1e79f97

Browse files
dschogitster
authored andcommitted
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 gitgitgadget#206 Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3e6046e commit 1e79f97

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

Documentation/git-range-diff.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git range-diff' [--color=[<when>]] [--no-color] [<diff-options>]
1212
[--no-dual-color] [--creation-factor=<factor>]
13+
[--left-only | --right-only]
1314
( <range1> <range2> | <rev1>...<rev2> | <base> <rev1> <rev2> )
1415

1516
DESCRIPTION
@@ -57,6 +58,14 @@ to revert to color all lines according to the outer diff markers
5758
See the ``Algorithm`` section below for an explanation why this is
5859
needed.
5960

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+
6069
--[no-]notes[=<ref>]::
6170
This flag is passed to the `git log` program
6271
(see linkgit:git-log[1]) that generates the patches.

builtin/range-diff.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
2020
.diffopt = &diffopt,
2121
.other_arg = &other_arg
2222
};
23-
int simple_color = -1;
23+
int simple_color = -1, left_only = 0, right_only = 0;
2424
struct option range_diff_options[] = {
2525
OPT_INTEGER(0, "creation-factor",
2626
&range_diff_opts.creation_factor,
@@ -30,6 +30,10 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
3030
OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
3131
N_("notes"), N_("passed to 'git log'"),
3232
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")),
3337
OPT_END()
3438
};
3539
struct option *options;
@@ -87,6 +91,8 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
8791
FREE_AND_NULL(options);
8892

8993
range_diff_opts.dual_color = simple_color < 1;
94+
range_diff_opts.left_only = left_only;
95+
range_diff_opts.right_only = right_only;
9096
res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
9197

9298
strvec_clear(&other_arg);

range-diff.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,17 @@ static void output(struct string_list *a, struct string_list *b,
513513

514514
/* Show unmatched LHS commit whose predecessors were shown. */
515515
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,
517518
&buf, &dashes, a_util, NULL);
518519
i++;
519520
continue;
520521
}
521522

522523
/* Show unmatched RHS commits. */
523524
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,
525527
&buf, &dashes, NULL, b_util);
526528
b_util = ++j < b->nr ? b->items[j].util : NULL;
527529
}
@@ -551,7 +553,10 @@ int show_range_diff(const char *range1, const char *range2,
551553
struct string_list branch1 = STRING_LIST_INIT_DUP;
552554
struct string_list branch2 = STRING_LIST_INIT_DUP;
553555

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))
555560
res = error(_("could not parse log for '%s'"), range1);
556561
if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
557562
res = error(_("could not parse log for '%s'"), range2);

range-diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
struct range_diff_options {
1010
int creation_factor;
1111
unsigned dual_color:1;
12+
unsigned left_only:1, right_only:1;
1213
const struct diff_options *diffopt; /* may be NULL */
1314
const struct strvec *other_arg; /* may be NULL */
1415
};

t/t3206-range-diff.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,4 +717,19 @@ test_expect_success 'format-patch --range-diff with multiple notes' '
717717
test_cmp expect actual
718718
'
719719

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+
720735
test_done

0 commit comments

Comments
 (0)