Skip to content

Commit dadc91f

Browse files
committed
Merge branch 'js/range-diff-one-side-only'
The "git range-diff" command learned "--(left|right)-only" option to show only one side of the compared range. * js/range-diff-one-side-only: range-diff: offer --left-only/--right-only options range-diff: move the diffopt initialization down one layer range-diff: combine all options in a single data structure range-diff: simplify code spawning `git log` range-diff: libify the read_patches() function again range-diff: avoid leaking memory in two error code paths
2 parents 77348b0 + 1e79f97 commit dadc91f

File tree

7 files changed

+120
-61
lines changed

7 files changed

+120
-61
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
@@ -68,6 +69,14 @@ to revert to color all lines according to the outer diff markers
6869
See the ``Algorithm`` section below for an explanation why this is
6970
needed.
7071

72+
--left-only::
73+
Suppress commits that are missing from the first specified range
74+
(or the "left range" when using the `<rev1>...<rev2>` format).
75+
76+
--right-only::
77+
Suppress commits that are missing from the second specified range
78+
(or the "right range" when using the `<rev1>...<rev2>` format).
79+
7180
--[no-]notes[=<ref>]::
7281
This flag is passed to the `git log` program
7382
(see linkgit:git-log[1]) that generates the patches.

builtin/log.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,14 +1223,20 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
12231223
*/
12241224
struct diff_options opts;
12251225
struct strvec other_arg = STRVEC_INIT;
1226+
struct range_diff_options range_diff_opts = {
1227+
.creation_factor = rev->creation_factor,
1228+
.dual_color = 1,
1229+
.diffopt = &opts,
1230+
.other_arg = &other_arg
1231+
};
1232+
12261233
diff_setup(&opts);
12271234
opts.file = rev->diffopt.file;
12281235
opts.use_color = rev->diffopt.use_color;
12291236
diff_setup_done(&opts);
12301237
fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
12311238
get_notes_args(&other_arg, rev);
1232-
show_range_diff(rev->rdiff1, rev->rdiff2,
1233-
rev->creation_factor, 1, &opts, &other_arg);
1239+
show_range_diff(rev->rdiff1, rev->rdiff2, &range_diff_opts);
12341240
strvec_clear(&other_arg);
12351241
}
12361242
}

builtin/range-diff.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,27 @@ NULL
1414

1515
int cmd_range_diff(int argc, const char **argv, const char *prefix)
1616
{
17-
int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1817
struct diff_options diffopt = { NULL };
1918
struct strvec other_arg = STRVEC_INIT;
20-
int simple_color = -1;
19+
struct range_diff_options range_diff_opts = {
20+
.creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
21+
.diffopt = &diffopt,
22+
.other_arg = &other_arg
23+
};
24+
int simple_color = -1, left_only = 0, right_only = 0;
2125
struct option range_diff_options[] = {
22-
OPT_INTEGER(0, "creation-factor", &creation_factor,
26+
OPT_INTEGER(0, "creation-factor",
27+
&range_diff_opts.creation_factor,
2328
N_("Percentage by which creation is weighted")),
2429
OPT_BOOL(0, "no-dual-color", &simple_color,
2530
N_("use simple diff colors")),
2631
OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
2732
N_("notes"), N_("passed to 'git log'"),
2833
PARSE_OPT_OPTARG),
34+
OPT_BOOL(0, "left-only", &left_only,
35+
N_("only emit output related to the first range")),
36+
OPT_BOOL(0, "right-only", &right_only,
37+
N_("only emit output related to the second range")),
2938
OPT_END()
3039
};
3140
struct option *options;
@@ -82,8 +91,10 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
8291
}
8392
FREE_AND_NULL(options);
8493

85-
res = show_range_diff(range1.buf, range2.buf, creation_factor,
86-
simple_color < 1, &diffopt, &other_arg);
94+
range_diff_opts.dual_color = simple_color < 1;
95+
range_diff_opts.left_only = left_only;
96+
range_diff_opts.right_only = right_only;
97+
res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
8798

8899
strvec_clear(&other_arg);
89100
strbuf_release(&range1);

log-tree.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,11 @@ void show_log(struct rev_info *opt)
808808
if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
809809
struct diff_queue_struct dq;
810810
struct diff_options opts;
811+
struct range_diff_options range_diff_opts = {
812+
.creation_factor = opt->creation_factor,
813+
.dual_color = 1,
814+
.diffopt = &opts
815+
};
811816

812817
memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
813818
DIFF_QUEUE_CLEAR(&diff_queued_diff);
@@ -822,8 +827,7 @@ void show_log(struct rev_info *opt)
822827
opts.file = opt->diffopt.file;
823828
opts.use_color = opt->diffopt.use_color;
824829
diff_setup_done(&opts);
825-
show_range_diff(opt->rdiff1, opt->rdiff2,
826-
opt->creation_factor, 1, &opts, NULL);
830+
show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
827831

828832
memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
829833
}

range-diff.c

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ static int read_patches(const char *range, struct string_list *list,
8181
finish_command(&cp);
8282
return -1;
8383
}
84+
if (finish_command(&cp))
85+
return -1;
8486

8587
line = contents.buf;
8688
size = contents.len;
@@ -98,10 +100,10 @@ static int read_patches(const char *range, struct string_list *list,
98100
if (get_oid(p, &util->oid)) {
99101
error(_("could not parse commit '%s'"), p);
100102
free(util);
103+
free(current_filename);
101104
string_list_clear(list, 1);
102105
strbuf_release(&buf);
103106
strbuf_release(&contents);
104-
finish_command(&cp);
105107
return -1;
106108
}
107109
util->matching = -1;
@@ -113,10 +115,10 @@ static int read_patches(const char *range, struct string_list *list,
113115
error(_("could not parse first line of `log` output: "
114116
"did not start with 'commit ': '%s'"),
115117
line);
118+
free(current_filename);
116119
string_list_clear(list, 1);
117120
strbuf_release(&buf);
118121
strbuf_release(&contents);
119-
finish_command(&cp);
120122
return -1;
121123
}
122124

@@ -134,9 +136,16 @@ static int read_patches(const char *range, struct string_list *list,
134136
orig_len = len;
135137
len = parse_git_diff_header(&root, &linenr, 0, line,
136138
len, size, &patch);
137-
if (len < 0)
138-
die(_("could not parse git header '%.*s'"),
139-
orig_len, line);
139+
if (len < 0) {
140+
error(_("could not parse git header '%.*s'"),
141+
orig_len, line);
142+
free(util);
143+
free(current_filename);
144+
string_list_clear(list, 1);
145+
strbuf_release(&buf);
146+
strbuf_release(&contents);
147+
return -1;
148+
}
140149
strbuf_addstr(&buf, " ## ");
141150
if (patch.is_new > 0)
142151
strbuf_addf(&buf, "%s (new)", patch.new_name);
@@ -219,9 +228,6 @@ static int read_patches(const char *range, struct string_list *list,
219228
strbuf_release(&buf);
220229
free(current_filename);
221230

222-
if (finish_command(&cp))
223-
return -1;
224-
225231
return 0;
226232
}
227233

@@ -459,12 +465,35 @@ static void patch_diff(const char *a, const char *b,
459465
diff_flush(diffopt);
460466
}
461467

468+
static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
469+
{
470+
return data;
471+
}
472+
462473
static void output(struct string_list *a, struct string_list *b,
463-
struct diff_options *diffopt)
474+
struct range_diff_options *range_diff_opts)
464475
{
465476
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
466477
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
467478
int i = 0, j = 0;
479+
struct diff_options opts;
480+
struct strbuf indent = STRBUF_INIT;
481+
482+
if (range_diff_opts->diffopt)
483+
memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
484+
else
485+
diff_setup(&opts);
486+
487+
if (!opts.output_format)
488+
opts.output_format = DIFF_FORMAT_PATCH;
489+
opts.flags.suppress_diff_headers = 1;
490+
opts.flags.dual_color_diffed_diffs =
491+
range_diff_opts->dual_color;
492+
opts.flags.suppress_hunk_header_line_count = 1;
493+
opts.output_prefix = output_prefix_cb;
494+
strbuf_addstr(&indent, " ");
495+
opts.output_prefix_data = &indent;
496+
diff_setup_done(&opts);
468497

469498
/*
470499
* We assume the user is really more interested in the second argument
@@ -485,79 +514,59 @@ static void output(struct string_list *a, struct string_list *b,
485514

486515
/* Show unmatched LHS commit whose predecessors were shown. */
487516
if (i < a->nr && a_util->matching < 0) {
488-
output_pair_header(diffopt, patch_no_width,
517+
if (!range_diff_opts->right_only)
518+
output_pair_header(&opts, patch_no_width,
489519
&buf, &dashes, a_util, NULL);
490520
i++;
491521
continue;
492522
}
493523

494524
/* Show unmatched RHS commits. */
495525
while (j < b->nr && b_util->matching < 0) {
496-
output_pair_header(diffopt, patch_no_width,
526+
if (!range_diff_opts->left_only)
527+
output_pair_header(&opts, patch_no_width,
497528
&buf, &dashes, NULL, b_util);
498529
b_util = ++j < b->nr ? b->items[j].util : NULL;
499530
}
500531

501532
/* Show matching LHS/RHS pair. */
502533
if (j < b->nr) {
503534
a_util = a->items[b_util->matching].util;
504-
output_pair_header(diffopt, patch_no_width,
535+
output_pair_header(&opts, patch_no_width,
505536
&buf, &dashes, a_util, b_util);
506-
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
537+
if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
507538
patch_diff(a->items[b_util->matching].string,
508-
b->items[j].string, diffopt);
539+
b->items[j].string, &opts);
509540
a_util->shown = 1;
510541
j++;
511542
}
512543
}
513544
strbuf_release(&buf);
514545
strbuf_release(&dashes);
515-
}
516-
517-
static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
518-
{
519-
return data;
546+
strbuf_release(&indent);
520547
}
521548

522549
int show_range_diff(const char *range1, const char *range2,
523-
int creation_factor, int dual_color,
524-
const struct diff_options *diffopt,
525-
const struct strvec *other_arg)
550+
struct range_diff_options *range_diff_opts)
526551
{
527552
int res = 0;
528553

529554
struct string_list branch1 = STRING_LIST_INIT_DUP;
530555
struct string_list branch2 = STRING_LIST_INIT_DUP;
531556

532-
if (read_patches(range1, &branch1, other_arg))
557+
if (range_diff_opts->left_only && range_diff_opts->right_only)
558+
res = error(_("--left-only and --right-only are mutually exclusive"));
559+
560+
if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
533561
res = error(_("could not parse log for '%s'"), range1);
534-
if (!res && read_patches(range2, &branch2, other_arg))
562+
if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
535563
res = error(_("could not parse log for '%s'"), range2);
536564

537565
if (!res) {
538-
struct diff_options opts;
539-
struct strbuf indent = STRBUF_INIT;
540-
541-
if (diffopt)
542-
memcpy(&opts, diffopt, sizeof(opts));
543-
else
544-
diff_setup(&opts);
545-
546-
if (!opts.output_format)
547-
opts.output_format = DIFF_FORMAT_PATCH;
548-
opts.flags.suppress_diff_headers = 1;
549-
opts.flags.dual_color_diffed_diffs = dual_color;
550-
opts.flags.suppress_hunk_header_line_count = 1;
551-
opts.output_prefix = output_prefix_cb;
552-
strbuf_addstr(&indent, " ");
553-
opts.output_prefix_data = &indent;
554-
diff_setup_done(&opts);
555-
556566
find_exact_matches(&branch1, &branch2);
557-
get_correspondences(&branch1, &branch2, creation_factor);
558-
output(&branch1, &branch2, &opts);
559-
560-
strbuf_release(&indent);
567+
get_correspondences(&branch1, &branch2,
568+
range_diff_opts->creation_factor);
569+
output(&branch1, &branch2, range_diff_opts);
561570
}
562571

563572
string_list_clear(&branch1, 1);

range-diff.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66

77
#define RANGE_DIFF_CREATION_FACTOR_DEFAULT 60
88

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+
};
16+
917
/*
10-
* Compare series of commits in RANGE1 and RANGE2, and emit to the
11-
* standard output. NULL can be passed to DIFFOPT to use the built-in
12-
* default.
18+
* Compare series of commits in `range1` and `range2`, and emit to the
19+
* standard output.
1320
*/
1421
int show_range_diff(const char *range1, const char *range2,
15-
int creation_factor, int dual_color,
16-
const struct diff_options *diffopt,
17-
const struct strvec *other_arg);
22+
struct range_diff_options *opts);
1823

1924
/*
2025
* Determine whether the given argument is usable as a range argument of `git

t/t3206-range-diff.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,4 +733,19 @@ test_expect_success 'format-patch --range-diff with multiple notes' '
733733
test_cmp expect actual
734734
'
735735

736+
test_expect_success '--left-only/--right-only' '
737+
git switch --orphan left-right &&
738+
test_commit first &&
739+
test_commit unmatched &&
740+
test_commit common &&
741+
git switch -C left-right first &&
742+
git cherry-pick common &&
743+
744+
git range-diff -s --left-only ...common >actual &&
745+
head_oid=$(git rev-parse --short HEAD) &&
746+
common_oid=$(git rev-parse --short common) &&
747+
echo "1: $head_oid = 2: $common_oid common" >expect &&
748+
test_cmp expect actual
749+
'
750+
736751
test_done

0 commit comments

Comments
 (0)