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