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