Skip to content

Commit 73a834e

Browse files
sunshinecogitster
authored andcommitted
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>
1 parent 2566865 commit 73a834e

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

builtin/range-diff.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
1111
NULL
1212
};
1313

14-
static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
15-
{
16-
return data;
17-
}
18-
1914
int cmd_range_diff(int argc, const char **argv, const char *prefix)
2015
{
2116
int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
@@ -29,17 +24,11 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
2924
OPT_END()
3025
};
3126
int i, j, res = 0;
32-
struct strbuf four_spaces = STRBUF_INIT;
3327
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
3428

3529
git_config(git_diff_ui_config, NULL);
3630

3731
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;
4332

4433
argc = parse_options(argc, argv, NULL, options,
4534
builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
@@ -63,12 +52,9 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
6352
options + ARRAY_SIZE(options) - 1, /* OPT_END */
6453
builtin_range_diff_usage, 0);
6554

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;
7258

7359
if (argc == 2) {
7460
if (!strstr(argv[0], ".."))
@@ -106,11 +92,10 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
10692
}
10793

10894
res = show_range_diff(range1.buf, range2.buf, creation_factor,
109-
&diffopt);
95+
simple_color < 1, &diffopt);
11096

11197
strbuf_release(&range1);
11298
strbuf_release(&range2);
113-
strbuf_release(&four_spaces);
11499

115100
return res;
116101
}

range-diff.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,14 @@ static void output(struct string_list *a, struct string_list *b,
409409
strbuf_release(&dashes);
410410
}
411411

412+
static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
413+
{
414+
return data;
415+
}
416+
412417
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)
414420
{
415421
int res = 0;
416422

@@ -423,9 +429,23 @@ int show_range_diff(const char *range1, const char *range2,
423429
res = error(_("could not parse log for '%s'"), range2);
424430

425431
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+
426444
find_exact_matches(&branch1, &branch2);
427445
get_correspondences(&branch1, &branch2, creation_factor);
428-
output(&branch1, &branch2, diffopt);
446+
output(&branch1, &branch2, &opts);
447+
448+
strbuf_release(&indent);
429449
}
430450

431451
string_list_clear(&branch1, 1);

range-diff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define RANGE_DIFF_CREATION_FACTOR_DEFAULT 60
77

88
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);
1011

1112
#endif

0 commit comments

Comments
 (0)