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