Skip to content

Commit fcdeb7e

Browse files
committed
Merge branch 'range-diff'
This takes an early version of the `range-diff` branch, for the benefit of GitGitGadget. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents a60968c + 7b16bd3 commit fcdeb7e

18 files changed

+1862
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
/git-pull
113113
/git-push
114114
/git-quiltimport
115+
/git-range-diff
115116
/git-read-tree
116117
/git-rebase
117118
/git-rebase--am

Documentation/git-range-diff.txt

+235
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
git-range-diff(1)
2+
==================
3+
4+
NAME
5+
----
6+
git-range-diff - Compare two commit ranges (e.g. two versions of a branch)
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'git range-diff' [--color=[<when>]] [--no-color] [<diff-options>]
12+
[--dual-color] [--creation-factor=<factor>]
13+
( <range1> <range2> | <rev1>...<rev2> | <base> <rev1> <rev2> )
14+
15+
DESCRIPTION
16+
-----------
17+
18+
This command shows the differences between two versions of a patch
19+
series, or more generally, two commit ranges (ignoring merges).
20+
21+
To that end, it first finds pairs of commits from both commit ranges
22+
that correspond with each other. Two commits are said to correspond when
23+
the diff between their patches (i.e. the author information, the commit
24+
message and the commit diff) is reasonably small compared to the
25+
patches' size. See ``Algorithm` below for details.
26+
27+
Finally, the list of matching commits is shown in the order of the
28+
second commit range, with unmatched commits being inserted just after
29+
all of their ancestors have been shown.
30+
31+
32+
OPTIONS
33+
-------
34+
--dual-color::
35+
When the commit diffs differ, recreate the original diffs'
36+
coloring, and add outer -/+ diff markers with the *background*
37+
being red/green to make it easier to see e.g. when there was a
38+
change in what exact lines were added.
39+
40+
--creation-factor=<percent>::
41+
Set the creation/deletion cost fudge factor to `<percent>`.
42+
Defaults to 60. Try a larger value if `git range-diff` erroneously
43+
considers a large change a total rewrite (deletion of one commit
44+
and addition of another), and a smaller one in the reverse case.
45+
See the ``Algorithm`` section below for an explanation why this is
46+
needed.
47+
48+
<range1> <range2>::
49+
Compare the commits specified by the two ranges, where
50+
`<range1>` is considered an older version of `<range2>`.
51+
52+
<rev1>...<rev2>::
53+
Equivalent to passing `<rev2>..<rev1>` and `<rev1>..<rev2>`.
54+
55+
<base> <rev1> <rev2>::
56+
Equivalent to passing `<base>..<rev1>` and `<base>..<rev2>`.
57+
Note that `<base>` does not need to be the exact branch point
58+
of the branches. Example: after rebasing a branch `my-topic`,
59+
`git range-diff my-topic@{u} my-topic@{1} my-topic` would
60+
show the differences introduced by the rebase.
61+
62+
`git range-diff` also accepts the regular diff options (see
63+
linkgit:git-diff[1]), most notably the `--color=[<when>]` and
64+
`--no-color` options. These options are used when generating the "diff
65+
between patches", i.e. to compare the author, commit message and diff of
66+
corresponding old/new commits. There is currently no means to tweak the
67+
diff options passed to `git log` when generating those patches.
68+
69+
70+
CONFIGURATION
71+
-------------
72+
This command uses the `diff.color.*` and `pager.range-diff` settings
73+
(the latter is on by default).
74+
See linkgit:git-config[1].
75+
76+
77+
EXAMPLES
78+
--------
79+
80+
When a rebase required merge conflicts to be resolved, compare the changes
81+
introduced by the rebase directly afterwards using:
82+
83+
------------
84+
$ git range-diff @{u} @{1} @
85+
------------
86+
87+
88+
A typical output of `git range-diff` would look like this:
89+
90+
------------
91+
-: ------- > 1: 0ddba11 Prepare for the inevitable!
92+
1: c0debee = 2: cab005e Add a helpful message at the start
93+
2: f00dbal ! 3: decafe1 Describe a bug
94+
@@ -1,3 +1,3 @@
95+
Author: A U Thor <[email protected]>
96+
97+
-TODO: Describe a bug
98+
+Describe a bug
99+
@@ -324,5 +324,6
100+
This is expected.
101+
102+
-+What is unexpected is that it will also crash.
103+
++Unexpectedly, it also crashes. This is a bug, and the jury is
104+
++still out there how to fix it best. See ticket #314 for details.
105+
106+
Contact
107+
3: bedead < -: ------- TO-UNDO
108+
------------
109+
110+
In this example, there are 3 old and 3 new commits, where the developer
111+
removed the 3rd, added a new one before the first two, and modified the
112+
commit message of the 2nd commit as well its diff.
113+
114+
When the output goes to a terminal, it is color-coded by default, just
115+
like regular `git diff`'s output. In addition, the first line (adding a
116+
commit) is green, the last line (deleting a commit) is red, the second
117+
line (with a perfect match) is yellow like the commit header of `git
118+
show`'s output, and the third line colors the old commit red, the new
119+
one green and the rest like `git show`'s commit header.
120+
121+
The color-coded diff is actually a bit hard to read, though, as it
122+
colors the entire lines red or green. The line that added "What is
123+
unexpected" in the old commit, for example, is completely red, even if
124+
the intent of the old commit was to add something.
125+
126+
To help with that, use the `--dual-color` mode. In this mode, the diff
127+
of diffs will retain the original diff colors, and prefix the lines with
128+
-/+ markers that have their *background* red or green, to make it more
129+
obvious that they describe how the diff itself changed.
130+
131+
132+
Algorithm
133+
---------
134+
135+
The general idea is this: we generate a cost matrix between the commits
136+
in both commit ranges, then solve the least-cost assignment.
137+
138+
To avoid false positives (e.g. when a patch has been removed, and an
139+
unrelated patch has been added between two iterations of the same patch
140+
series), the cost matrix is extended to allow for that, by adding
141+
fixed-cost entries for wholesale deletes/adds.
142+
143+
Example: Let commits `1--2` be the first iteration of a patch series and
144+
`A--C` the second iteration. Let's assume that `A` is a cherry-pick of
145+
`2,` and `C` is a cherry-pick of `1` but with a small modification (say,
146+
a fixed typo). Visualize the commits as a bipartite graph:
147+
148+
------------
149+
1 A
150+
151+
2 B
152+
153+
C
154+
------------
155+
156+
We are looking for a "best" explanation of the new series in terms of
157+
the old one. We can represent an "explanation" as an edge in the graph:
158+
159+
160+
------------
161+
1 A
162+
/
163+
2 --------' B
164+
165+
C
166+
------------
167+
168+
This explanation comes for "free" because there was no change. Similarly
169+
`C` could be explained using `1`, but that comes at some cost c>0
170+
because of the modification:
171+
172+
------------
173+
1 ----. A
174+
| /
175+
2 ----+---' B
176+
|
177+
`----- C
178+
c>0
179+
------------
180+
181+
In mathematical terms, what we are looking for is some sort of a minimum
182+
cost bipartite matching; `1` is matched to `C` at some cost, etc. The
183+
underlying graph is in fact a complete bipartite graph; the cost we
184+
associate with every edge is the size of the diff between the two
185+
commits' patches. To explain also new commits, we introduce dummy nodes
186+
on both sides:
187+
188+
------------
189+
1 ----. A
190+
| /
191+
2 ----+---' B
192+
|
193+
o `----- C
194+
c>0
195+
o o
196+
197+
o o
198+
------------
199+
200+
The cost of an edge `o--C` is the size of `C`'s diff, modified by a
201+
fudge factor that should be smaller than 100%. The cost of an edge
202+
`o--o` is free. The fudge factor is necessary because even if `1` and
203+
`C` have nothing in common, they may still share a few empty lines and
204+
such, possibly making the assignment `1--C`, `o--o` slightly cheaper
205+
than `1--o`, `o--C` even if `1` and `C` have nothing in common. With the
206+
fudge factor we require a much larger common part to consider patches as
207+
corresponding.
208+
209+
The overall time needed to compute this algorithm is the time needed to
210+
compute n+m commit diffs and then n*m diffs of patches, plus the time
211+
needed to compute the least-cost assigment between n and m diffs. Git
212+
uses an implementation of the Jonker-Volgenant algorithm to solve the
213+
assignment problem, which has cubic runtime complexity. The matching
214+
found in this case will look like this:
215+
216+
------------
217+
1 ----. A
218+
| /
219+
2 ----+---' B
220+
.--+-----'
221+
o -' `----- C
222+
c>0
223+
o ---------- o
224+
225+
o ---------- o
226+
------------
227+
228+
229+
SEE ALSO
230+
--------
231+
linkgit:git-log[1]
232+
233+
GIT
234+
---
235+
Part of the linkgit:git[1] suite

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ LIB_OBJS += gpg-interface.o
853853
LIB_OBJS += graph.o
854854
LIB_OBJS += grep.o
855855
LIB_OBJS += hashmap.o
856+
LIB_OBJS += linear-assignment.o
856857
LIB_OBJS += help.o
857858
LIB_OBJS += hex.o
858859
LIB_OBJS += ident.o
@@ -903,6 +904,7 @@ LIB_OBJS += progress.o
903904
LIB_OBJS += prompt.o
904905
LIB_OBJS += protocol.o
905906
LIB_OBJS += quote.o
907+
LIB_OBJS += range-diff.o
906908
LIB_OBJS += reachable.o
907909
LIB_OBJS += read-cache.o
908910
LIB_OBJS += reflog-walk.o
@@ -1037,6 +1039,7 @@ BUILTIN_OBJS += builtin/prune-packed.o
10371039
BUILTIN_OBJS += builtin/prune.o
10381040
BUILTIN_OBJS += builtin/pull.o
10391041
BUILTIN_OBJS += builtin/push.o
1042+
BUILTIN_OBJS += builtin/range-diff.o
10401043
BUILTIN_OBJS += builtin/read-tree.o
10411044
BUILTIN_OBJS += builtin/rebase--helper.o
10421045
BUILTIN_OBJS += builtin/receive-pack.o

builtin.h

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ extern int cmd_prune(int argc, const char **argv, const char *prefix);
200200
extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
201201
extern int cmd_pull(int argc, const char **argv, const char *prefix);
202202
extern int cmd_push(int argc, const char **argv, const char *prefix);
203+
extern int cmd_range_diff(int argc, const char **argv, const char *prefix);
203204
extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
204205
extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix);
205206
extern int cmd_receive_pack(int argc, const char **argv, const char *prefix);

builtin/range-diff.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "cache.h"
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>"),
9+
N_("git range-diff [<options>] <old-tip>...<new-tip>"),
10+
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+
19+
int cmd_range_diff(int argc, const char **argv, const char *prefix)
20+
{
21+
int creation_factor = 60;
22+
struct diff_options diffopt = { NULL };
23+
int dual_color = 0;
24+
struct option options[] = {
25+
OPT_INTEGER(0, "creation-factor", &creation_factor,
26+
N_("Percentage by which creation is weighted")),
27+
OPT_BOOL(0, "dual-color", &dual_color,
28+
N_("color both diff and diff-between-diffs")),
29+
OPT_END()
30+
};
31+
int i, j, res = 0;
32+
struct strbuf four_spaces = STRBUF_INIT;
33+
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
34+
35+
git_config(git_diff_ui_config, NULL);
36+
37+
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;
43+
44+
argc = parse_options(argc, argv, NULL, options,
45+
builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN);
46+
47+
for (i = j = 0; i < argc; i++) {
48+
int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
49+
50+
if (!c)
51+
argv[j++] = argv[i];
52+
}
53+
argc = j;
54+
diff_setup_done(&diffopt);
55+
56+
if (dual_color) {
57+
diffopt.use_color = 1;
58+
diffopt.flags.dual_color_diffed_diffs = 1;
59+
}
60+
61+
if (argc == 2) {
62+
if (!strstr(argv[0], ".."))
63+
warning(_("no .. in range: '%s'"), argv[0]);
64+
strbuf_addstr(&range1, argv[0]);
65+
66+
if (!strstr(argv[1], ".."))
67+
warning(_("no .. in range: '%s'"), argv[1]);
68+
strbuf_addstr(&range2, argv[1]);
69+
} else if (argc == 3) {
70+
strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
71+
strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
72+
} else if (argc == 1) {
73+
const char *b = strstr(argv[0], "..."), *a = argv[0];
74+
int a_len;
75+
76+
if (!b)
77+
die(_("single arg format requires a symmetric range"));
78+
79+
a_len = (int)(b - a);
80+
if (!a_len) {
81+
a = "HEAD";
82+
a_len = strlen(a);
83+
}
84+
b += 3;
85+
if (!*b)
86+
b = "HEAD";
87+
strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
88+
strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
89+
} else {
90+
error(_("need two commit ranges"));
91+
usage_with_options(builtin_range_diff_usage, options);
92+
}
93+
94+
res = show_range_diff(range1.buf, range2.buf, creation_factor,
95+
&diffopt);
96+
97+
strbuf_release(&range1);
98+
strbuf_release(&range2);
99+
strbuf_release(&four_spaces);
100+
101+
return res;
102+
}

color.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct strbuf;
4444
#define GIT_COLOR_BG_CYAN "\033[46m"
4545
#define GIT_COLOR_FAINT "\033[2m"
4646
#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"
47+
#define GIT_COLOR_REVERSE "\033[7m"
4748

4849
/* A special value meaning "no color selected" */
4950
#define GIT_COLOR_NIL "NIL"

command-list.txt

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ git-prune-packed plumbingmanipulators
103103
git-pull mainporcelain remote
104104
git-push mainporcelain remote
105105
git-quiltimport foreignscminterface
106+
git-range-diff mainporcelain
106107
git-read-tree plumbingmanipulators
107108
git-rebase mainporcelain history
108109
git-receive-pack synchelpers

0 commit comments

Comments
 (0)