range-diff: move the diffopt initialization down one layer
It is actually only the `output()` function that uses those diffopts. By moving the diffopt initialization down into that function, it is encapsulated better. Incidentally, it will also make it easier to implement the `--left-only` and `--right-only` options in `git range-diff` because the `output()` function is now receiving all range-diff options as a parameter, not just the diffopts. 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
3e6046edadf409537cc9e991d1df628fa96953ba
1 file changed
+31
-33
range-diff.c
+31
-33
@@ -464,12 +464,35 @@ static void patch_diff(const char *a, const char *b,
464
diff_flush(diffopt);
465
}
466
467
+static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
468
+{
469
+ return data;
470
+}
471
+
472
static void output(struct string_list *a, struct string_list *b,
468
- struct diff_options *diffopt)
473
+ struct range_diff_options *range_diff_opts)
474
{
475
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
476
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
477
int i = 0, j = 0;
478
+ struct diff_options opts;
479
+ struct strbuf indent = STRBUF_INIT;
480
+
481
+ if (range_diff_opts->diffopt)
482
+ memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
483
+ else
484
+ diff_setup(&opts);
485
+
486
+ if (!opts.output_format)
487
+ opts.output_format = DIFF_FORMAT_PATCH;
488
+ opts.flags.suppress_diff_headers = 1;
489
+ opts.flags.dual_color_diffed_diffs =
490
+ range_diff_opts->dual_color;
491
+ opts.flags.suppress_hunk_header_line_count = 1;
492
+ opts.output_prefix = output_prefix_cb;
493
+ strbuf_addstr(&indent, " ");
494
+ opts.output_prefix_data = &indent;
495
+ diff_setup_done(&opts);
496
497
/*
498
* We assume the user is really more interested in the second argument
@@ -490,7 +513,7 @@ 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) {
493
- output_pair_header(diffopt, patch_no_width,
516
+ output_pair_header(&opts, patch_no_width,
517
&buf, &dashes, a_util, NULL);
518
i++;
519
continue;
@@ -498,7 +521,7 @@ static void output(struct string_list *a, struct string_list *b,
521
522
/* Show unmatched RHS commits. */
523
while (j < b->nr && b_util->matching < 0) {
501
- output_pair_header(diffopt, patch_no_width,
524
+ output_pair_header(&opts, patch_no_width,
525
&buf, &dashes, NULL, b_util);
526
b_util = ++j < b->nr ? b->items[j].util : NULL;
527
}
@@ -506,22 +529,18 @@ static void output(struct string_list *a, struct string_list *b,
529
/* Show matching LHS/RHS pair. */
530
if (j < b->nr) {
531
a_util = a->items[b_util->matching].util;
509
- output_pair_header(diffopt, patch_no_width,
532
+ output_pair_header(&opts, patch_no_width,
533
&buf, &dashes, a_util, b_util);
511
- if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
534
+ if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
535
patch_diff(a->items[b_util->matching].string,
513
- b->items[j].string, diffopt);
536
+ b->items[j].string, &opts);
537
a_util->shown = 1;
538
j++;
539
}
540
}
541
strbuf_release(&buf);
542
strbuf_release(&dashes);
520
-}
521
-
522
-static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
523
-{
524
- return data;
543
+ strbuf_release(&indent);
544
}
545
546
int show_range_diff(const char *range1, const char *range2,
@@ -538,31 +557,10 @@ int show_range_diff(const char *range1, const char *range2,
557
res = error(_("could not parse log for '%s'"), range2);
558
559
if (!res) {
541
- struct diff_options opts;
542
- struct strbuf indent = STRBUF_INIT;
543
-
544
- if (range_diff_opts->diffopt)
545
- memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
546
- else
547
- diff_setup(&opts);
548
-
549
- if (!opts.output_format)
550
- opts.output_format = DIFF_FORMAT_PATCH;
551
- opts.flags.suppress_diff_headers = 1;
552
- opts.flags.dual_color_diffed_diffs =
553
- range_diff_opts->dual_color;
554
- opts.flags.suppress_hunk_header_line_count = 1;
555
- opts.output_prefix = output_prefix_cb;
556
- strbuf_addstr(&indent, " ");
557
- opts.output_prefix_data = &indent;
558
- diff_setup_done(&opts);
559
-
560
find_exact_matches(&branch1, &branch2);
561
get_correspondences(&branch1, &branch2,
562
range_diff_opts->creation_factor);
563
- output(&branch1, &branch2, &opts);
564
-
565
- strbuf_release(&indent);
563
+ output(&branch1, &branch2, range_diff_opts);
564
}
565
566
string_list_clear(&branch1, 1);