range-diff: also show the diff between patches

Just like tbdiff, we now show the diff between matching patches. This is a "diff of two diffs", so it can be a bit daunting to read for the beginner. An alternative would be to display an interdiff, i.e. the hypothetical diff which is the result of first reverting the old diff and then applying the new diff. Especially when rebasing frequently, an interdiff is often not feasible, though: if the old diff cannot be applied in reverse (due to a moving upstream), an interdiff can simply not be inferred. This commit brings `range-diff` closer to feature parity with regard to tbdiff. To make `git range-diff` respect e.g. color.diff.* settings, we have to adjust git_branch_config() accordingly. Note: while we now parse diff options such as --color, the effect is not yet the same as in tbdiff, where also the commit pairs would be colored. This is left for a later commit. Note also: while tbdiff accepts the `--no-patches` option to suppress these diffs between patches, we prefer the `-s` (or `--no-patch`) option that is automatically supported via our use of diff_opt_parse(). And finally note: to support diff options, we have to call `parse_options()` such that it keeps unknown options, and then loop over those and let `diff_opt_parse()` handle them. After that loop, we have to call `parse_options()` again, to make sure that no unknown options are left. Helped-by: Thomas Gummerer <t.gummerer@gmail.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 13, 2018 at 04:33 UTC c8c5e43ac3f9a5b785faf16f10bb8e2c493606a4
3 files changed +63 -6
builtin/range-diff.c
+29 -2
@@ -2,6 +2,7 @@
2 #include "builtin.h"
3 #include "parse-options.h"
4 #include "range-diff.h"
5 +#include "config.h"
6
7 static const char * const builtin_range_diff_usage[] = {
8 N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
@@ -13,15 +14,40 @@ NULL
14 int cmd_range_diff(int argc, const char **argv, const char *prefix)
15 {
16 int creation_factor = 60;
17 + struct diff_options diffopt = { NULL };
18 struct option options[] = {
19 OPT_INTEGER(0, "creation-factor", &creation_factor,
20 N_("Percentage by which creation is weighted")),
21 OPT_END()
22 };
21 - int res = 0;
23 + int i, j, res = 0;
24 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
25
26 + git_config(git_diff_ui_config, NULL);
27 +
28 + diff_setup(&diffopt);
29 + diffopt.output_format = DIFF_FORMAT_PATCH;
30 +
31 argc = parse_options(argc, argv, NULL, options,
32 + builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
33 + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
34 +
35 + for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
36 + int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
37 +
38 + if (!c)
39 + argv[j++] = argv[i++];
40 + else
41 + i += c;
42 + }
43 + while (i < argc)
44 + argv[j++] = argv[i++];
45 + argc = j;
46 + diff_setup_done(&diffopt);
47 +
48 + /* Make sure that there are no unparsed options */
49 + argc = parse_options(argc, argv, NULL,
50 + options + ARRAY_SIZE(options) - 1, /* OPT_END */
51 builtin_range_diff_usage, 0);
52
53 if (argc == 2) {
@@ -59,7 +85,8 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
85 usage_with_options(builtin_range_diff_usage, options);
86 }
87
62 - res = show_range_diff(range1.buf, range2.buf, creation_factor);
88 + res = show_range_diff(range1.buf, range2.buf, creation_factor,
89 + &diffopt);
90
91 strbuf_release(&range1);
92 strbuf_release(&range2);
range-diff.c
+31 -3
@@ -6,6 +6,7 @@
6 #include "hashmap.h"
7 #include "xdiff-interface.h"
8 #include "linear-assignment.h"
9 +#include "diffcore.h"
10
11 struct patch_util {
12 /* For the search for an exact match */
@@ -258,7 +259,31 @@ static const char *short_oid(struct patch_util *util)
259 return find_unique_abbrev(&util->oid, DEFAULT_ABBREV);
260 }
261
261 -static void output(struct string_list *a, struct string_list *b)
262 +static struct diff_filespec *get_filespec(const char *name, const char *p)
263 +{
264 + struct diff_filespec *spec = alloc_filespec(name);
265 +
266 + fill_filespec(spec, &null_oid, 0, 0644);
267 + spec->data = (char *)p;
268 + spec->size = strlen(p);
269 + spec->should_munmap = 0;
270 + spec->is_stdin = 1;
271 +
272 + return spec;
273 +}
274 +
275 +static void patch_diff(const char *a, const char *b,
276 + struct diff_options *diffopt)
277 +{
278 + diff_queue(&diff_queued_diff,
279 + get_filespec("a", a), get_filespec("b", b));
280 +
281 + diffcore_std(diffopt);
282 + diff_flush(diffopt);
283 +}
284 +
285 +static void output(struct string_list *a, struct string_list *b,
286 + struct diff_options *diffopt)
287 {
288 int i = 0, j = 0;
289
@@ -300,6 +325,9 @@ static void output(struct string_list *a, struct string_list *b)
325 printf("%d: %s ! %d: %s\n",
326 b_util->matching + 1, short_oid(a_util),
327 j + 1, short_oid(b_util));
328 + if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
329 + patch_diff(a->items[b_util->matching].string,
330 + b->items[j].string, diffopt);
331 a_util->shown = 1;
332 j++;
333 }
@@ -307,7 +335,7 @@ static void output(struct string_list *a, struct string_list *b)
335 }
336
337 int show_range_diff(const char *range1, const char *range2,
310 - int creation_factor)
338 + int creation_factor, struct diff_options *diffopt)
339 {
340 int res = 0;
341
@@ -322,7 +350,7 @@ int show_range_diff(const char *range1, const char *range2,
350 if (!res) {
351 find_exact_matches(&branch1, &branch2);
352 get_correspondences(&branch1, &branch2, creation_factor);
325 - output(&branch1, &branch2);
353 + output(&branch1, &branch2, diffopt);
354 }
355
356 string_list_clear(&branch1, 1);
range-diff.h
+3 -1
@@ -1,7 +1,9 @@
1 #ifndef RANGE_DIFF_H
2 #define RANGE_DIFF_H
3
4 +#include "diff.h"
5 +
6 int show_range_diff(const char *range1, const char *range2,
5 - int creation_factor);
7 + int creation_factor, struct diff_options *diffopt);
8
9 #endif