range-diff(docs): explain how to specify commit ranges
[git] / Documentation / git-range-diff.txt
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         [--no-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 merge commits).
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 There are three ways to specify the commit ranges:
32
33 - `<range1> <range2>`: Either commit range can be of the form
34   `<base>..<rev>`, `<rev>^!` or `<rev>^-<n>`. See `SPECIFYING RANGES`
35   in linkgit:gitrevisions[7] for more details.
36
37 - `<rev1>...<rev2>`. This is equivalent to
38   `<rev2>..<rev1> <rev1>..<rev2>`.
39
40 - `<base> <rev1> <rev2>`: This is equivalent to `<base>..<rev1>
41   <base>..<rev2>`.
42
43 OPTIONS
44 -------
45 --no-dual-color::
46         When the commit diffs differ, `git range-diff` recreates the
47         original diffs' coloring, and adds outer -/+ diff markers with
48         the *background* being red/green to make it easier to see e.g.
49         when there was a change in what exact lines were added.
50 +
51 Additionally, the commit diff lines that are only present in the first commit
52 range are shown "dimmed" (this can be overridden using the `color.diff.<slot>`
53 config setting where `<slot>` is one of `contextDimmed`, `oldDimmed` and
54 `newDimmed`), and the commit diff lines that are only present in the second
55 commit range are shown in bold (which can be overridden using the config
56 settings `color.diff.<slot>` with `<slot>` being one of `contextBold`,
57 `oldBold` or `newBold`).
58 +
59 This is known to `range-diff` as "dual coloring". Use `--no-dual-color`
60 to revert to color all lines according to the outer diff markers
61 (and completely ignore the inner diff when it comes to color).
62
63 --creation-factor=<percent>::
64         Set the creation/deletion cost fudge factor to `<percent>`.
65         Defaults to 60. Try a larger value if `git range-diff` erroneously
66         considers a large change a total rewrite (deletion of one commit
67         and addition of another), and a smaller one in the reverse case.
68         See the ``Algorithm`` section below for an explanation why this is
69         needed.
70
71 --[no-]notes[=<ref>]::
72         This flag is passed to the `git log` program
73         (see linkgit:git-log[1]) that generates the patches.
74
75 <range1> <range2>::
76         Compare the commits specified by the two ranges, where
77         `<range1>` is considered an older version of `<range2>`.
78
79 <rev1>...<rev2>::
80         Equivalent to passing `<rev2>..<rev1>` and `<rev1>..<rev2>`.
81
82 <base> <rev1> <rev2>::
83         Equivalent to passing `<base>..<rev1>` and `<base>..<rev2>`.
84         Note that `<base>` does not need to be the exact branch point
85         of the branches. Example: after rebasing a branch `my-topic`,
86         `git range-diff my-topic@{u} my-topic@{1} my-topic` would
87         show the differences introduced by the rebase.
88
89 `git range-diff` also accepts the regular diff options (see
90 linkgit:git-diff[1]), most notably the `--color=[<when>]` and
91 `--no-color` options. These options are used when generating the "diff
92 between patches", i.e. to compare the author, commit message and diff of
93 corresponding old/new commits. There is currently no means to tweak most of the
94 diff options passed to `git log` when generating those patches.
95
96 OUTPUT STABILITY
97 ----------------
98
99 The output of the `range-diff` command is subject to change. It is
100 intended to be human-readable porcelain output, not something that can
101 be used across versions of Git to get a textually stable `range-diff`
102 (as opposed to something like the `--stable` option to
103 linkgit:git-patch-id[1]). There's also no equivalent of
104 linkgit:git-apply[1] for `range-diff`, the output is not intended to
105 be machine-readable.
106
107 This is particularly true when passing in diff options. Currently some
108 options like `--stat` can, as an emergent effect, produce output
109 that's quite useless in the context of `range-diff`. Future versions
110 of `range-diff` may learn to interpret such options in a manner
111 specific to `range-diff` (e.g. for `--stat` producing human-readable
112 output which summarizes how the diffstat changed).
113
114 CONFIGURATION
115 -------------
116 This command uses the `diff.color.*` and `pager.range-diff` settings
117 (the latter is on by default).
118 See linkgit:git-config[1].
119
120
121 EXAMPLES
122 --------
123
124 When a rebase required merge conflicts to be resolved, compare the changes
125 introduced by the rebase directly afterwards using:
126
127 ------------
128 $ git range-diff @{u} @{1} @
129 ------------
130
131
132 A typical output of `git range-diff` would look like this:
133
134 ------------
135 -:  ------- > 1:  0ddba11 Prepare for the inevitable!
136 1:  c0debee = 2:  cab005e Add a helpful message at the start
137 2:  f00dbal ! 3:  decafe1 Describe a bug
138     @@ -1,3 +1,3 @@
139      Author: A U Thor <author@example.com>
140
141     -TODO: Describe a bug
142     +Describe a bug
143     @@ -324,5 +324,6
144       This is expected.
145
146     -+What is unexpected is that it will also crash.
147     ++Unexpectedly, it also crashes. This is a bug, and the jury is
148     ++still out there how to fix it best. See ticket #314 for details.
149
150       Contact
151 3:  bedead < -:  ------- TO-UNDO
152 ------------
153
154 In this example, there are 3 old and 3 new commits, where the developer
155 removed the 3rd, added a new one before the first two, and modified the
156 commit message of the 2nd commit as well its diff.
157
158 When the output goes to a terminal, it is color-coded by default, just
159 like regular `git diff`'s output. In addition, the first line (adding a
160 commit) is green, the last line (deleting a commit) is red, the second
161 line (with a perfect match) is yellow like the commit header of `git
162 show`'s output, and the third line colors the old commit red, the new
163 one green and the rest like `git show`'s commit header.
164
165 A naive color-coded diff of diffs is actually a bit hard to read,
166 though, as it colors the entire lines red or green. The line that added
167 "What is unexpected" in the old commit, for example, is completely red,
168 even if the intent of the old commit was to add something.
169
170 To help with that, `range` uses the `--dual-color` mode by default. In
171 this mode, the diff of diffs will retain the original diff colors, and
172 prefix the lines with -/+ markers that have their *background* red or
173 green, to make it more obvious that they describe how the diff itself
174 changed.
175
176
177 Algorithm
178 ---------
179
180 The general idea is this: we generate a cost matrix between the commits
181 in both commit ranges, then solve the least-cost assignment.
182
183 The cost matrix is populated thusly: for each pair of commits, both
184 diffs are generated and the "diff of diffs" is generated, with 3 context
185 lines, then the number of lines in that diff is used as cost.
186
187 To avoid false positives (e.g. when a patch has been removed, and an
188 unrelated patch has been added between two iterations of the same patch
189 series), the cost matrix is extended to allow for that, by adding
190 fixed-cost entries for wholesale deletes/adds.
191
192 Example: Let commits `1--2` be the first iteration of a patch series and
193 `A--C` the second iteration. Let's assume that `A` is a cherry-pick of
194 `2,` and `C` is a cherry-pick of `1` but with a small modification (say,
195 a fixed typo). Visualize the commits as a bipartite graph:
196
197 ------------
198     1            A
199
200     2            B
201
202                  C
203 ------------
204
205 We are looking for a "best" explanation of the new series in terms of
206 the old one. We can represent an "explanation" as an edge in the graph:
207
208
209 ------------
210     1            A
211                /
212     2 --------'  B
213
214                  C
215 ------------
216
217 This explanation comes for "free" because there was no change. Similarly
218 `C` could be explained using `1`, but that comes at some cost c>0
219 because of the modification:
220
221 ------------
222     1 ----.      A
223           |    /
224     2 ----+---'  B
225           |
226           `----- C
227           c>0
228 ------------
229
230 In mathematical terms, what we are looking for is some sort of a minimum
231 cost bipartite matching; `1` is matched to `C` at some cost, etc. The
232 underlying graph is in fact a complete bipartite graph; the cost we
233 associate with every edge is the size of the diff between the two
234 commits' patches. To explain also new commits, we introduce dummy nodes
235 on both sides:
236
237 ------------
238     1 ----.      A
239           |    /
240     2 ----+---'  B
241           |
242     o     `----- C
243           c>0
244     o            o
245
246     o            o
247 ------------
248
249 The cost of an edge `o--C` is the size of `C`'s diff, modified by a
250 fudge factor that should be smaller than 100%. The cost of an edge
251 `o--o` is free. The fudge factor is necessary because even if `1` and
252 `C` have nothing in common, they may still share a few empty lines and
253 such, possibly making the assignment `1--C`, `o--o` slightly cheaper
254 than `1--o`, `o--C` even if `1` and `C` have nothing in common. With the
255 fudge factor we require a much larger common part to consider patches as
256 corresponding.
257
258 The overall time needed to compute this algorithm is the time needed to
259 compute n+m commit diffs and then n*m diffs of patches, plus the time
260 needed to compute the least-cost assignment between n and m diffs. Git
261 uses an implementation of the Jonker-Volgenant algorithm to solve the
262 assignment problem, which has cubic runtime complexity. The matching
263 found in this case will look like this:
264
265 ------------
266     1 ----.      A
267           |    /
268     2 ----+---'  B
269        .--+-----'
270     o -'  `----- C
271           c>0
272     o ---------- o
273
274     o ---------- o
275 ------------
276
277
278 SEE ALSO
279 --------
280 linkgit:git-log[1]
281
282 GIT
283 ---
284 Part of the linkgit:git[1] suite