range-diff: combine all options in a single data structure

This will make it easier to implement the `--left-only` and `--right-only` options. 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 f1ce6c191e9d15ce78041d8b6496c246b10d9b2d
5 files changed +42 -23
builtin/log.c
+8 -2
@@ -1231,14 +1231,20 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
1231 */
1232 struct diff_options opts;
1233 struct strvec other_arg = STRVEC_INIT;
1234 + struct range_diff_options range_diff_opts = {
1235 + .creation_factor = rev->creation_factor,
1236 + .dual_color = 1,
1237 + .diffopt = &opts,
1238 + .other_arg = &other_arg
1239 + };
1240 +
1241 diff_setup(&opts);
1242 opts.file = rev->diffopt.file;
1243 opts.use_color = rev->diffopt.use_color;
1244 diff_setup_done(&opts);
1245 fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title);
1246 get_notes_args(&other_arg, rev);
1240 - show_range_diff(rev->rdiff1, rev->rdiff2,
1241 - rev->creation_factor, 1, &opts, &other_arg);
1247 + show_range_diff(rev->rdiff1, rev->rdiff2, &range_diff_opts);
1248 strvec_clear(&other_arg);
1249 }
1250 }
builtin/range-diff.c
+9 -4
@@ -13,12 +13,17 @@ NULL
13
14 int cmd_range_diff(int argc, const char **argv, const char *prefix)
15 {
16 - int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
16 struct diff_options diffopt = { NULL };
17 struct strvec other_arg = STRVEC_INIT;
18 + struct range_diff_options range_diff_opts = {
19 + .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
20 + .diffopt = &diffopt,
21 + .other_arg = &other_arg
22 + };
23 int simple_color = -1;
24 struct option range_diff_options[] = {
21 - OPT_INTEGER(0, "creation-factor", &creation_factor,
25 + OPT_INTEGER(0, "creation-factor",
26 + &range_diff_opts.creation_factor,
27 N_("Percentage by which creation is weighted")),
28 OPT_BOOL(0, "no-dual-color", &simple_color,
29 N_("use simple diff colors")),
@@ -81,8 +86,8 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
86 }
87 FREE_AND_NULL(options);
88
84 - res = show_range_diff(range1.buf, range2.buf, creation_factor,
85 - simple_color < 1, &diffopt, &other_arg);
89 + range_diff_opts.dual_color = simple_color < 1;
90 + res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
91
92 strvec_clear(&other_arg);
93 strbuf_release(&range1);
log-tree.c
+6 -2
@@ -808,6 +808,11 @@ void show_log(struct rev_info *opt)
808 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
809 struct diff_queue_struct dq;
810 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 + };
816
817 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
818 DIFF_QUEUE_CLEAR(&diff_queued_diff);
@@ -822,8 +827,7 @@ void show_log(struct rev_info *opt)
827 opts.file = opt->diffopt.file;
828 opts.use_color = opt->diffopt.use_color;
829 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);
831
832 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
833 }
range-diff.c
+9 -9
@@ -525,33 +525,32 @@ static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
525 }
526
527 int show_range_diff(const char *range1, const char *range2,
528 - int creation_factor, int dual_color,
529 - const struct diff_options *diffopt,
530 - const struct strvec *other_arg)
528 + struct range_diff_options *range_diff_opts)
529 {
530 int res = 0;
531
532 struct string_list branch1 = STRING_LIST_INIT_DUP;
533 struct string_list branch2 = STRING_LIST_INIT_DUP;
534
537 - if (read_patches(range1, &branch1, other_arg))
535 + if (read_patches(range1, &branch1, range_diff_opts->other_arg))
536 res = error(_("could not parse log for '%s'"), range1);
539 - if (!res && read_patches(range2, &branch2, other_arg))
537 + if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
538 res = error(_("could not parse log for '%s'"), range2);
539
540 if (!res) {
541 struct diff_options opts;
542 struct strbuf indent = STRBUF_INIT;
543
546 - if (diffopt)
547 - memcpy(&opts, diffopt, sizeof(opts));
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;
554 - opts.flags.dual_color_diffed_diffs = dual_color;
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, " ");
@@ -559,7 +558,8 @@ int show_range_diff(const char *range1, const char *range2,
558 diff_setup_done(&opts);
559
560 find_exact_matches(&branch1, &branch2);
562 - get_correspondences(&branch1, &branch2, creation_factor);
561 + get_correspondences(&branch1, &branch2,
562 + range_diff_opts->creation_factor);
563 output(&branch1, &branch2, &opts);
564
565 strbuf_release(&indent);
range-diff.h
+10 -6
@@ -6,14 +6,18 @@
6
7 #define RANGE_DIFF_CREATION_FACTOR_DEFAULT 60
8
9 +struct range_diff_options {
10 + int creation_factor;
11 + unsigned dual_color:1;
12 + const struct diff_options *diffopt; /* may be NULL */
13 + const struct strvec *other_arg; /* may be NULL */
14 +};
15 +
16 /*
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.
17 + * Compare series of commits in `range1` and `range2`, and emit to the
18 + * standard output.
19 */
20 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);
21 + struct range_diff_options *opts);
22
23 #endif