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