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