range-diff: relieve callers of low-level configuration burden

There are a number of very low-level configuration details which need to be managed precisely to generate a proper range-diff. In particular, 'diff_options' output format, header suppression, indentation, and dual-color mode must all be set appropriately to ensure proper behavior. Handle these details locally in the libified range-diff back-end rather than forcing each caller to have specialized knowledge of these implementation details, and to avoid duplication as new callers are added. While at it, localize these tweaks to be active only while generating the range-diff, so they don't clobber the caller-provided 'diff_options', which might be used beyond range-diff generation. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 22, 2018 at 05:57 UTC 73a834e9e27906a76940f1ced5c132bce205d3f8
3 files changed +28 -22
builtin/range-diff.c
+4 -19
@@ -11,11 +11,6 @@ N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
11 NULL
12 };
13
14 -static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
15 -{
16 - return data;
17 -}
18 -
14 int cmd_range_diff(int argc, const char **argv, const char *prefix)
15 {
16 int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
@@ -29,17 +24,11 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
24 OPT_END()
25 };
26 int i, j, res = 0;
32 - struct strbuf four_spaces = STRBUF_INIT;
27 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
28
29 git_config(git_diff_ui_config, NULL);
30
31 diff_setup(&diffopt);
38 - diffopt.output_format = DIFF_FORMAT_PATCH;
39 - diffopt.flags.suppress_diff_headers = 1;
40 - diffopt.output_prefix = output_prefix_cb;
41 - strbuf_addstr(&four_spaces, " ");
42 - diffopt.output_prefix_data = &four_spaces;
32
33 argc = parse_options(argc, argv, NULL, options,
34 builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
@@ -63,12 +52,9 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
52 options + ARRAY_SIZE(options) - 1, /* OPT_END */
53 builtin_range_diff_usage, 0);
54
66 - if (simple_color < 1) {
67 - if (!simple_color)
68 - /* force color when --dual-color was used */
69 - diffopt.use_color = 1;
70 - diffopt.flags.dual_color_diffed_diffs = 1;
71 - }
55 + /* force color when --dual-color was used */
56 + if (!simple_color)
57 + diffopt.use_color = 1;
58
59 if (argc == 2) {
60 if (!strstr(argv[0], ".."))
@@ -106,11 +92,10 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
92 }
93
94 res = show_range_diff(range1.buf, range2.buf, creation_factor,
109 - &diffopt);
95 + simple_color < 1, &diffopt);
96
97 strbuf_release(&range1);
98 strbuf_release(&range2);
113 - strbuf_release(&four_spaces);
99
100 return res;
101 }
range-diff.c
+22 -2
@@ -409,8 +409,14 @@ static void output(struct string_list *a, struct string_list *b,
409 strbuf_release(&dashes);
410 }
411
412 +static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
413 +{
414 + return data;
415 +}
416 +
417 int show_range_diff(const char *range1, const char *range2,
413 - int creation_factor, struct diff_options *diffopt)
418 + int creation_factor, int dual_color,
419 + struct diff_options *diffopt)
420 {
421 int res = 0;
422
@@ -423,9 +429,23 @@ int show_range_diff(const char *range1, const char *range2,
429 res = error(_("could not parse log for '%s'"), range2);
430
431 if (!res) {
432 + struct diff_options opts;
433 + struct strbuf indent = STRBUF_INIT;
434 +
435 + memcpy(&opts, diffopt, sizeof(opts));
436 + opts.output_format = DIFF_FORMAT_PATCH;
437 + opts.flags.suppress_diff_headers = 1;
438 + opts.flags.dual_color_diffed_diffs = dual_color;
439 + opts.output_prefix = output_prefix_cb;
440 + strbuf_addstr(&indent, " ");
441 + opts.output_prefix_data = &indent;
442 + diff_setup_done(&opts);
443 +
444 find_exact_matches(&branch1, &branch2);
445 get_correspondences(&branch1, &branch2, creation_factor);
428 - output(&branch1, &branch2, diffopt);
446 + output(&branch1, &branch2, &opts);
447 +
448 + strbuf_release(&indent);
449 }
450
451 string_list_clear(&branch1, 1);
range-diff.h
+2 -1
@@ -6,6 +6,7 @@
6 #define RANGE_DIFF_CREATION_FACTOR_DEFAULT 60
7
8 int show_range_diff(const char *range1, const char *range2,
9 - int creation_factor, struct diff_options *diffopt);
9 + int creation_factor, int dual_color,
10 + struct diff_options *diffopt);
11
12 #endif