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