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