1 gitdiffcore(7)
2 ==============
4 NAME
5 ----
6 gitdiffcore - Tweaking diff output
8 SYNOPSIS
9 --------
10 [verse]
11 'git diff' *
13 DESCRIPTION
14 -----------
16 The diff commands 'git diff-index', 'git diff-files', and 'git diff-tree'
17 can be told to manipulate differences they find in
18 unconventional ways before showing 'diff' output. The manipulation
19 is collectively called "diffcore transformation". This short note
20 describes what they are and how to use them to produce 'diff' output
21 that is easier to understand than the conventional kind.
24 The chain of operation
25 ----------------------
27 The 'git diff-{asterisk}' family works by first comparing two sets of
28 files:
30 - 'git diff-index' compares contents of a "tree" object and the
31 working directory (when '\--cached' flag is not used) or a
32 "tree" object and the index file (when '\--cached' flag is
33 used);
35 - 'git diff-files' compares contents of the index file and the
36 working directory;
38 - 'git diff-tree' compares contents of two "tree" objects;
40 In all of these cases, the commands themselves first optionally limit
41 the two sets of files by any pathspecs given on their command-lines,
42 and compare corresponding paths in the two resulting sets of files.
44 The pathspecs are used to limit the world diff operates in. They remove
45 the filepairs outside the specified sets of pathnames. E.g. If the
46 input set of filepairs included:
48 ------------------------------------------------
49 :100644 100644 bcd1234... 0123456... M junkfile
50 ------------------------------------------------
52 but the command invocation was `git diff-files myfile`, then the
53 junkfile entry would be removed from the list because only "myfile"
54 is under consideration.
56 The result of comparison is passed from these commands to what is
57 internally called "diffcore", in a format similar to what is output
58 when the -p option is not used. E.g.
60 ------------------------------------------------
61 in-place edit :100644 100644 bcd1234... 0123456... M file0
62 create :000000 100644 0000000... 1234567... A file4
63 delete :100644 000000 1234567... 0000000... D file5
64 unmerged :000000 000000 0000000... 0000000... U file6
65 ------------------------------------------------
67 The diffcore mechanism is fed a list of such comparison results
68 (each of which is called "filepair", although at this point each
69 of them talks about a single file), and transforms such a list
70 into another list. There are currently 5 such transformations:
72 - diffcore-break
73 - diffcore-rename
74 - diffcore-merge-broken
75 - diffcore-pickaxe
76 - diffcore-order
78 These are applied in sequence. The set of filepairs 'git diff-{asterisk}'
79 commands find are used as the input to diffcore-break, and
80 the output from diffcore-break is used as the input to the
81 next transformation. The final result is then passed to the
82 output routine and generates either diff-raw format (see Output
83 format sections of the manual for 'git diff-{asterisk}' commands) or
84 diff-patch format.
87 diffcore-break: For Splitting Up "Complete Rewrites"
88 ----------------------------------------------------
90 The second transformation in the chain is diffcore-break, and is
91 controlled by the -B option to the 'git diff-{asterisk}' commands. This is
92 used to detect a filepair that represents "complete rewrite" and
93 break such filepair into two filepairs that represent delete and
94 create. E.g. If the input contained this filepair:
96 ------------------------------------------------
97 :100644 100644 bcd1234... 0123456... M file0
98 ------------------------------------------------
100 and if it detects that the file "file0" is completely rewritten,
101 it changes it to:
103 ------------------------------------------------
104 :100644 000000 bcd1234... 0000000... D file0
105 :000000 100644 0000000... 0123456... A file0
106 ------------------------------------------------
108 For the purpose of breaking a filepair, diffcore-break examines
109 the extent of changes between the contents of the files before
110 and after modification (i.e. the contents that have "bcd1234..."
111 and "0123456..." as their SHA1 content ID, in the above
112 example). The amount of deletion of original contents and
113 insertion of new material are added together, and if it exceeds
114 the "break score", the filepair is broken into two. The break
115 score defaults to 50% of the size of the smaller of the original
116 and the result (i.e. if the edit shrinks the file, the size of
117 the result is used; if the edit lengthens the file, the size of
118 the original is used), and can be customized by giving a number
119 after "-B" option (e.g. "-B75" to tell it to use 75%).
122 diffcore-rename: For Detection Renames and Copies
123 -------------------------------------------------
125 This transformation is used to detect renames and copies, and is
126 controlled by the -M option (to detect renames) and the -C option
127 (to detect copies as well) to the 'git diff-{asterisk}' commands. If the
128 input contained these filepairs:
130 ------------------------------------------------
131 :100644 000000 0123456... 0000000... D fileX
132 :000000 100644 0000000... 0123456... A file0
133 ------------------------------------------------
135 and the contents of the deleted file fileX is similar enough to
136 the contents of the created file file0, then rename detection
137 merges these filepairs and creates:
139 ------------------------------------------------
140 :100644 100644 0123456... 0123456... R100 fileX file0
141 ------------------------------------------------
143 When the "-C" option is used, the original contents of modified files,
144 and deleted files (and also unmodified files, if the
145 "\--find-copies-harder" option is used) are considered as candidates
146 of the source files in rename/copy operation. If the input were like
147 these filepairs, that talk about a modified file fileY and a newly
148 created file file0:
150 ------------------------------------------------
151 :100644 100644 0123456... 1234567... M fileY
152 :000000 100644 0000000... bcd3456... A file0
153 ------------------------------------------------
155 the original contents of fileY and the resulting contents of
156 file0 are compared, and if they are similar enough, they are
157 changed to:
159 ------------------------------------------------
160 :100644 100644 0123456... 1234567... M fileY
161 :100644 100644 0123456... bcd3456... C100 fileY file0
162 ------------------------------------------------
164 In both rename and copy detection, the same "extent of changes"
165 algorithm used in diffcore-break is used to determine if two
166 files are "similar enough", and can be customized to use
167 a similarity score different from the default of 50% by giving a
168 number after the "-M" or "-C" option (e.g. "-M8" to tell it to use
169 8/10 = 80%).
171 Note. When the "-C" option is used with `\--find-copies-harder`
172 option, 'git diff-{asterisk}' commands feed unmodified filepairs to
173 diffcore mechanism as well as modified ones. This lets the copy
174 detector consider unmodified files as copy source candidates at
175 the expense of making it slower. Without `\--find-copies-harder`,
176 'git diff-{asterisk}' commands can detect copies only if the file that was
177 copied happened to have been modified in the same changeset.
180 diffcore-merge-broken: For Putting "Complete Rewrites" Back Together
181 --------------------------------------------------------------------
183 This transformation is used to merge filepairs broken by
184 diffcore-break, and not transformed into rename/copy by
185 diffcore-rename, back into a single modification. This always
186 runs when diffcore-break is used.
188 For the purpose of merging broken filepairs back, it uses a
189 different "extent of changes" computation from the ones used by
190 diffcore-break and diffcore-rename. It counts only the deletion
191 from the original, and does not count insertion. If you removed
192 only 10 lines from a 100-line document, even if you added 910
193 new lines to make a new 1000-line document, you did not do a
194 complete rewrite. diffcore-break breaks such a case in order to
195 help diffcore-rename to consider such filepairs as candidate of
196 rename/copy detection, but if filepairs broken that way were not
197 matched with other filepairs to create rename/copy, then this
198 transformation merges them back into the original
199 "modification".
201 The "extent of changes" parameter can be tweaked from the
202 default 80% (that is, unless more than 80% of the original
203 material is deleted, the broken pairs are merged back into a
204 single modification) by giving a second number to -B option,
205 like these:
207 * -B50/60 (give 50% "break score" to diffcore-break, use 60%
208 for diffcore-merge-broken).
210 * -B/60 (the same as above, since diffcore-break defaults to 50%).
212 Note that earlier implementation left a broken pair as a separate
213 creation and deletion patches. This was an unnecessary hack and
214 the latest implementation always merges all the broken pairs
215 back into modifications, but the resulting patch output is
216 formatted differently for easier review in case of such
217 a complete rewrite by showing the entire contents of old version
218 prefixed with '-', followed by the entire contents of new
219 version prefixed with '+'.
222 diffcore-pickaxe: For Detecting Addition/Deletion of Specified String
223 ---------------------------------------------------------------------
225 This transformation is used to find filepairs that represent
226 changes that touch a specified string, and is controlled by the
227 -S option and the `\--pickaxe-all` option to the 'git diff-{asterisk}'
228 commands.
230 When diffcore-pickaxe is in use, it checks if there are
231 filepairs whose "result" side and whose "origin" side have
232 different number of specified string. Such a filepair represents
233 "the string appeared in this changeset". It also checks for the
234 opposite case that loses the specified string.
236 When `\--pickaxe-all` is not in effect, diffcore-pickaxe leaves
237 only such filepairs that touch the specified string in its
238 output. When `\--pickaxe-all` is used, diffcore-pickaxe leaves all
239 filepairs intact if there is such a filepair, or makes the
240 output empty otherwise. The latter behaviour is designed to
241 make reviewing of the changes in the context of the whole
242 changeset easier.
245 diffcore-order: For Sorting the Output Based on Filenames
246 ---------------------------------------------------------
248 This is used to reorder the filepairs according to the user's
249 (or project's) taste, and is controlled by the -O option to the
250 'git diff-{asterisk}' commands.
252 This takes a text file each of whose lines is a shell glob
253 pattern. Filepairs that match a glob pattern on an earlier line
254 in the file are output before ones that match a later line, and
255 filepairs that do not match any glob pattern are output last.
257 As an example, a typical orderfile for the core git probably
258 would look like this:
260 ------------------------------------------------
261 README
262 Makefile
263 Documentation
264 *.h
265 *.c
266 t
267 ------------------------------------------------
269 SEE ALSO
270 --------
271 linkgit:git-diff[1],
272 linkgit:git-diff-files[1],
273 linkgit:git-diff-index[1],
274 linkgit:git-diff-tree[1],
275 linkgit:git-format-patch[1],
276 linkgit:git-log[1],
277 linkgit:gitglossary[7],
278 link:user-manual.html[The Git User's Manual]
280 GIT
281 ---
282 Part of the linkgit:git[1] suite.