Skip to content

Commit b487b74

Browse files
committed
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 often, 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` option that is automatically supported via our use of diff_opt_parse(). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a559f79 commit b487b74

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

builtin/range-diff.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "builtin.h"
33
#include "parse-options.h"
44
#include "range-diff.h"
5+
#include "config.h"
56

67
static const char * const builtin_range_diff_usage[] = {
78
N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
@@ -13,16 +14,31 @@ NULL
1314
int cmd_range_diff(int argc, const char **argv, const char *prefix)
1415
{
1516
int creation_factor = 60;
17+
struct diff_options diffopt = { NULL };
1618
struct option options[] = {
1719
OPT_INTEGER(0, "creation-factor", &creation_factor,
1820
N_("Percentage by which creation is weighted")),
1921
OPT_END()
2022
};
21-
int res = 0;
23+
int i, j, res = 0;
2224
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
2325

24-
argc = parse_options(argc, argv, NULL, options, builtin_range_diff_usage,
25-
0);
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+
34+
for (i = j = 0; i < argc; i++) {
35+
int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
36+
37+
if (!c)
38+
argv[j++] = argv[i];
39+
}
40+
argc = j;
41+
diff_setup_done(&diffopt);
2642

2743
if (argc == 2) {
2844
if (!strstr(argv[0], ".."))
@@ -57,7 +73,8 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
5773
usage_with_options(builtin_range_diff_usage, options);
5874
}
5975

60-
res = show_range_diff(range1.buf, range2.buf, creation_factor);
76+
res = show_range_diff(range1.buf, range2.buf, creation_factor,
77+
&diffopt);
6178

6279
strbuf_release(&range1);
6380
strbuf_release(&range2);

range-diff.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "hashmap.h"
77
#include "xdiff-interface.h"
88
#include "linear-assignment.h"
9+
#include "diffcore.h"
910

1011
struct patch_util {
1112
/* For the search for an exact match */
@@ -254,7 +255,31 @@ static const char *short_oid(struct patch_util *util)
254255
return find_unique_abbrev(util->oid.hash, DEFAULT_ABBREV);
255256
}
256257

257-
static void output(struct string_list *a, struct string_list *b)
258+
static struct diff_filespec *get_filespec(const char *name, const char *p)
259+
{
260+
struct diff_filespec *spec = alloc_filespec(name);
261+
262+
fill_filespec(spec, &null_oid, 0, 0644);
263+
spec->data = (char *)p;
264+
spec->size = strlen(p);
265+
spec->should_munmap = 0;
266+
spec->is_stdin = 1;
267+
268+
return spec;
269+
}
270+
271+
static void patch_diff(const char *a, const char *b,
272+
struct diff_options *diffopt)
273+
{
274+
diff_queue(&diff_queued_diff,
275+
get_filespec("a", a), get_filespec("b", b));
276+
277+
diffcore_std(diffopt);
278+
diff_flush(diffopt);
279+
}
280+
281+
static void output(struct string_list *a, struct string_list *b,
282+
struct diff_options *diffopt)
258283
{
259284
int i = 0, j = 0;
260285

@@ -296,14 +321,17 @@ static void output(struct string_list *a, struct string_list *b)
296321
printf("%d: %s ! %d: %s\n",
297322
b_util->matching + 1, short_oid(a_util),
298323
j + 1, short_oid(b_util));
324+
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
325+
patch_diff(a->items[b_util->matching].string,
326+
b->items[j].string, diffopt);
299327
a_util->shown = 1;
300328
j++;
301329
}
302330
}
303331
}
304332

305333
int show_range_diff(const char *range1, const char *range2,
306-
int creation_factor)
334+
int creation_factor, struct diff_options *diffopt)
307335
{
308336
int res = 0;
309337

@@ -318,7 +346,7 @@ int show_range_diff(const char *range1, const char *range2,
318346
if (!res) {
319347
find_exact_matches(&branch1, &branch2);
320348
get_correspondences(&branch1, &branch2, creation_factor);
321-
output(&branch1, &branch2);
349+
output(&branch1, &branch2, diffopt);
322350
}
323351

324352
string_list_clear(&branch1, 1);

range-diff.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef BRANCH_DIFF_H
22
#define BRANCH_DIFF_H
33

4+
#include "diff.h"
5+
46
int show_range_diff(const char *range1, const char *range2,
5-
int creation_factor);
7+
int creation_factor, struct diff_options *diffopt);
68

79
#endif

0 commit comments

Comments
 (0)