blob: 642c51227b5a0b1990c374bac4f815dac0b04c22 [file] [log] [blame]
Christian Couder30eba7b2008-06-06 09:07:28 +02001gitdiffcore(7)
2==============
Junio C Hamano4a1332d2005-06-05 14:30:58 -07003
Christian Couder30eba7b2008-06-06 09:07:28 +02004NAME
5----
Michael J Gruberdddfb3f2010-03-21 18:30:17 +01006gitdiffcore - Tweaking diff output
Junio C Hamano4a1332d2005-06-05 14:30:58 -07007
Christian Couder30eba7b2008-06-06 09:07:28 +02008SYNOPSIS
9--------
Martin von Zweigbergk7791a1d2011-07-01 22:38:26 -040010[verse]
Jonathan Nieder0979c102008-07-03 00:37:18 -050011'git diff' *
Christian Couder30eba7b2008-06-06 09:07:28 +020012
13DESCRIPTION
14-----------
Junio C Hamano4a1332d2005-06-05 14:30:58 -070015
Thomas Rast0b444cd2010-01-10 00:33:00 +010016The diff commands 'git diff-index', 'git diff-files', and 'git diff-tree'
Junio C Hamano4cc41a12007-02-07 17:03:46 -080017can be told to manipulate differences they find in
Jonathan Nieder2fd02c92008-07-03 00:55:07 -050018unconventional ways before showing 'diff' output. The manipulation
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -040019is collectively called "diffcore transformation". This short note
Jonathan Nieder0979c102008-07-03 00:37:18 -050020describes what they are and how to use them to produce 'diff' output
Jonathan Nieder877276d2008-07-03 00:30:25 -050021that is easier to understand than the conventional kind.
Junio C Hamano4a1332d2005-06-05 14:30:58 -070022
23
24The chain of operation
25----------------------
26
Thomas Rast0b444cd2010-01-10 00:33:00 +010027The 'git diff-{asterisk}' family works by first comparing two sets of
Junio C Hamano4a1332d2005-06-05 14:30:58 -070028files:
29
Thomas Rast0b444cd2010-01-10 00:33:00 +010030 - 'git diff-index' compares contents of a "tree" object and the
Matthieu Moybcf96262016-06-28 13:40:11 +020031 working directory (when `--cached` flag is not used) or a
32 "tree" object and the index file (when `--cached` flag is
Junio C Hamano4a1332d2005-06-05 14:30:58 -070033 used);
34
Thomas Rast0b444cd2010-01-10 00:33:00 +010035 - 'git diff-files' compares contents of the index file and the
Junio C Hamano4a1332d2005-06-05 14:30:58 -070036 working directory;
37
Thomas Rast0b444cd2010-01-10 00:33:00 +010038 - 'git diff-tree' compares contents of two "tree" objects;
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -040039
Yann Dirson264e0b92008-09-19 22:12:53 +020040In all of these cases, the commands themselves first optionally limit
41the two sets of files by any pathspecs given on their command-lines,
42and compare corresponding paths in the two resulting sets of files.
43
44The pathspecs are used to limit the world diff operates in. They remove
45the filepairs outside the specified sets of pathnames. E.g. If the
46input set of filepairs included:
47
48------------------------------------------------
49:100644 100644 bcd1234... 0123456... M junkfile
50------------------------------------------------
51
52but the command invocation was `git diff-files myfile`, then the
53junkfile entry would be removed from the list because only "myfile"
54is under consideration.
55
56The result of comparison is passed from these commands to what is
57internally called "diffcore", in a format similar to what is output
58when the -p option is not used. E.g.
Junio C Hamano4a1332d2005-06-05 14:30:58 -070059
Junio C Hamano8db93072005-08-30 13:51:01 -070060------------------------------------------------
61in-place edit :100644 100644 bcd1234... 0123456... M file0
62create :000000 100644 0000000... 1234567... A file4
63delete :100644 000000 1234567... 0000000... D file5
64unmerged :000000 000000 0000000... 0000000... U file6
65------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -070066
67The diffcore mechanism is fed a list of such comparison results
68(each of which is called "filepair", although at this point each
69of them talks about a single file), and transforms such a list
Yann Dirson264e0b92008-09-19 22:12:53 +020070into another list. There are currently 5 such transformations:
Junio C Hamano4a1332d2005-06-05 14:30:58 -070071
Junio C Hamano8db93072005-08-30 13:51:01 -070072- diffcore-break
73- diffcore-rename
74- diffcore-merge-broken
75- diffcore-pickaxe
76- diffcore-order
Junio C Hamano1eb41362021-02-11 11:57:50 -080077- diffcore-rotate
Junio C Hamano4a1332d2005-06-05 14:30:58 -070078
Thomas Rast0b444cd2010-01-10 00:33:00 +010079These are applied in sequence. The set of filepairs 'git diff-{asterisk}'
Yann Dirson264e0b92008-09-19 22:12:53 +020080commands find are used as the input to diffcore-break, and
81the output from diffcore-break is used as the input to the
Junio C Hamano4a1332d2005-06-05 14:30:58 -070082next transformation. The final result is then passed to the
83output routine and generates either diff-raw format (see Output
Thomas Rast0b444cd2010-01-10 00:33:00 +010084format sections of the manual for 'git diff-{asterisk}' commands) or
Junio C Hamano4a1332d2005-06-05 14:30:58 -070085diff-patch format.
86
87
Patrick Steinhardtb803ae42017-02-28 09:59:05 +010088diffcore-break: For Splitting Up Complete Rewrites
89--------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -070090
91The second transformation in the chain is diffcore-break, and is
Thomas Rast0b444cd2010-01-10 00:33:00 +010092controlled by the -B option to the 'git diff-{asterisk}' commands. This is
Junio C Hamano4a1332d2005-06-05 14:30:58 -070093used to detect a filepair that represents "complete rewrite" and
94break such filepair into two filepairs that represent delete and
95create. E.g. If the input contained this filepair:
96
Junio C Hamano8db93072005-08-30 13:51:01 -070097------------------------------------------------
98:100644 100644 bcd1234... 0123456... M file0
99------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700100
101and if it detects that the file "file0" is completely rewritten,
102it changes it to:
103
Junio C Hamano8db93072005-08-30 13:51:01 -0700104------------------------------------------------
105:100644 000000 bcd1234... 0000000... D file0
106:000000 100644 0000000... 0123456... A file0
107------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700108
109For the purpose of breaking a filepair, diffcore-break examines
110the extent of changes between the contents of the files before
111and after modification (i.e. the contents that have "bcd1234..."
Thomas Ackermannd5fa1f12013-04-15 19:49:04 +0200112and "0123456..." as their SHA-1 content ID, in the above
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700113example). The amount of deletion of original contents and
114insertion of new material are added together, and if it exceeds
115the "break score", the filepair is broken into two. The break
116score defaults to 50% of the size of the smaller of the original
117and the result (i.e. if the edit shrinks the file, the size of
118the result is used; if the edit lengthens the file, the size of
119the original is used), and can be customized by giving a number
120after "-B" option (e.g. "-B75" to tell it to use 75%).
121
122
Patrick Steinhardt1aa38192017-02-28 09:59:04 +0100123diffcore-rename: For Detecting Renames and Copies
Junio C Hamanoa67c1d02005-10-29 00:50:42 -0700124-------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700125
126This transformation is used to detect renames and copies, and is
127controlled by the -M option (to detect renames) and the -C option
Thomas Rast0b444cd2010-01-10 00:33:00 +0100128(to detect copies as well) to the 'git diff-{asterisk}' commands. If the
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700129input contained these filepairs:
130
Junio C Hamano8db93072005-08-30 13:51:01 -0700131------------------------------------------------
132:100644 000000 0123456... 0000000... D fileX
133:000000 100644 0000000... 0123456... A file0
134------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700135
136and the contents of the deleted file fileX is similar enough to
137the contents of the created file file0, then rename detection
138merges these filepairs and creates:
139
Junio C Hamano8db93072005-08-30 13:51:01 -0700140------------------------------------------------
141:100644 100644 0123456... 0123456... R100 fileX file0
142------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700143
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400144When the "-C" option is used, the original contents of modified files,
145and deleted files (and also unmodified files, if the
Jeff King1c262bb2015-05-13 01:01:38 -0400146"--find-copies-harder" option is used) are considered as candidates
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400147of the source files in rename/copy operation. If the input were like
148these filepairs, that talk about a modified file fileY and a newly
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700149created file file0:
150
Junio C Hamano8db93072005-08-30 13:51:01 -0700151------------------------------------------------
152:100644 100644 0123456... 1234567... M fileY
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400153:000000 100644 0000000... bcd3456... A file0
Junio C Hamano8db93072005-08-30 13:51:01 -0700154------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700155
156the original contents of fileY and the resulting contents of
157file0 are compared, and if they are similar enough, they are
158changed to:
159
Junio C Hamano8db93072005-08-30 13:51:01 -0700160------------------------------------------------
161:100644 100644 0123456... 1234567... M fileY
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400162:100644 100644 0123456... bcd3456... C100 fileY file0
Junio C Hamano8db93072005-08-30 13:51:01 -0700163------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700164
165In both rename and copy detection, the same "extent of changes"
166algorithm used in diffcore-break is used to determine if two
167files are "similar enough", and can be customized to use
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400168a similarity score different from the default of 50% by giving a
169number after the "-M" or "-C" option (e.g. "-M8" to tell it to use
Junio C Hamano4a1332d2005-06-05 14:30:58 -07001708/10 = 80%).
171
Elijah Newren07c9a7f2021-02-14 07:51:50 +0000172Note that when rename detection is on but both copy and break
173detection are off, rename detection adds a preliminary step that first
174checks if files are moved across directories while keeping their
175filename the same. If there is a file added to a directory whose
Elijah Newrence14cc02023-10-08 06:45:09 +0000176contents are sufficiently similar to a file with the same name that got
Elijah Newren07c9a7f2021-02-14 07:51:50 +0000177deleted from a different directory, it will mark them as renames and
178exclude them from the later quadratic step (the one that pairwise
179compares all unmatched files to find the "best" matches, determined by
180the highest content similarity). So, for example, if a deleted
181docs/ext.txt and an added docs/config/ext.txt are similar enough, they
182will be marked as a rename and prevent an added docs/ext.md that may
183be even more similar to the deleted docs/ext.txt from being considered
184as the rename destination in the later step. For this reason, the
185preliminary "match same filename" step uses a bit higher threshold to
186mark a file pair as a rename and stop considering other candidates for
187better matches. At most, one comparison is done per file in this
188preliminary pass; so if there are several remaining ext.txt files
189throughout the directory hierarchy after exact rename detection, this
Elijah Newren37a25142021-02-27 00:30:39 +0000190preliminary step may be skipped for those files.
Elijah Newren07c9a7f2021-02-14 07:51:50 +0000191
Jeff King6cf378f2012-04-26 04:51:57 -0400192Note. When the "-C" option is used with `--find-copies-harder`
Thomas Rast0b444cd2010-01-10 00:33:00 +0100193option, 'git diff-{asterisk}' commands feed unmodified filepairs to
Junio C Hamano232b75a2005-06-19 13:14:53 -0700194diffcore mechanism as well as modified ones. This lets the copy
195detector consider unmodified files as copy source candidates at
Jeff King6cf378f2012-04-26 04:51:57 -0400196the expense of making it slower. Without `--find-copies-harder`,
Thomas Rast0b444cd2010-01-10 00:33:00 +0100197'git diff-{asterisk}' commands can detect copies only if the file that was
Junio C Hamano232b75a2005-06-19 13:14:53 -0700198copied happened to have been modified in the same changeset.
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700199
200
Patrick Steinhardtb803ae42017-02-28 09:59:05 +0100201diffcore-merge-broken: For Putting Complete Rewrites Back Together
202------------------------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700203
204This transformation is used to merge filepairs broken by
Christian Mederf73ae1f2005-10-05 15:08:26 -0700205diffcore-break, and not transformed into rename/copy by
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700206diffcore-rename, back into a single modification. This always
207runs when diffcore-break is used.
208
209For the purpose of merging broken filepairs back, it uses a
210different "extent of changes" computation from the ones used by
211diffcore-break and diffcore-rename. It counts only the deletion
212from the original, and does not count insertion. If you removed
213only 10 lines from a 100-line document, even if you added 910
214new lines to make a new 1000-line document, you did not do a
215complete rewrite. diffcore-break breaks such a case in order to
Elijah Newren0a4f0512023-10-08 06:45:17 +0000216help diffcore-rename to consider such filepairs as a candidate of
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700217rename/copy detection, but if filepairs broken that way were not
218matched with other filepairs to create rename/copy, then this
219transformation merges them back into the original
220"modification".
221
222The "extent of changes" parameter can be tweaked from the
223default 80% (that is, unless more than 80% of the original
224material is deleted, the broken pairs are merged back into a
225single modification) by giving a second number to -B option,
226like these:
227
Junio C Hamano8db93072005-08-30 13:51:01 -0700228* -B50/60 (give 50% "break score" to diffcore-break, use 60%
229 for diffcore-merge-broken).
230
231* -B/60 (the same as above, since diffcore-break defaults to 50%).
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700232
Elijah Newren6cc668c2023-10-08 06:45:14 +0000233Note that earlier implementation left a broken pair as separate
Elijah Newren4d542682023-10-08 06:45:24 +0000234creation and deletion patches. This was an unnecessary hack, and
Junio C Hamano366175e2005-06-19 13:17:50 -0700235the latest implementation always merges all the broken pairs
236back into modifications, but the resulting patch output is
Christian Mederf73ae1f2005-10-05 15:08:26 -0700237formatted differently for easier review in case of such
Elijah Newren0a4f0512023-10-08 06:45:17 +0000238a complete rewrite by showing the entire contents of the old version
239prefixed with '-', followed by the entire contents of the new
Junio C Hamano366175e2005-06-19 13:17:50 -0700240version prefixed with '+'.
241
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700242
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400243diffcore-pickaxe: For Detecting Addition/Deletion of Specified String
Junio C Hamanoa67c1d02005-10-29 00:50:42 -0700244---------------------------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700245
Ramkumar Ramachandra5bc3f0b2013-05-31 17:42:15 +0530246This transformation limits the set of filepairs to those that change
247specified strings between the preimage and the postimage in a certain
Jean-Noël Avila2162f9f2023-12-25 21:21:26 +0000248way. -S<block-of-text> and -G<regular-expression> options are used to
Ramkumar Ramachandra5bc3f0b2013-05-31 17:42:15 +0530249specify different ways these strings are sought.
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700250
Jean-Noël Avila2162f9f2023-12-25 21:21:26 +0000251"-S<block-of-text>" detects filepairs whose preimage and postimage
Ramkumar Ramachandra5bc3f0b2013-05-31 17:42:15 +0530252have different number of occurrences of the specified block of text.
253By definition, it will not detect in-file moves. Also, when a
254changeset moves a file wholesale without affecting the interesting
255string, diffcore-rename kicks in as usual, and `-S` omits the filepair
256(since the number of occurrences of that string didn't change in that
257rename-detected filepair). When used with `--pickaxe-regex`, treat
Jean-Noël Avila2162f9f2023-12-25 21:21:26 +0000258the <block-of-text> as an extended POSIX regular expression to match,
Ramkumar Ramachandra5bc3f0b2013-05-31 17:42:15 +0530259instead of a literal string.
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700260
Jean-Noël Avila2162f9f2023-12-25 21:21:26 +0000261"-G<regular-expression>" (mnemonic: grep) detects filepairs whose
Ramkumar Ramachandra5bc3f0b2013-05-31 17:42:15 +0530262textual diff has an added or a deleted line that matches the given
263regular expression. This means that it will detect in-file (or what
264rename-detection considers the same file) moves, which is noise. The
265implementation runs diff twice and greps, and this can be quite
Elijah Newren4d542682023-10-08 06:45:24 +0000266expensive. To speed things up, binary files without textconv filters
Thomas Braune0e7cb82018-12-14 19:49:12 +0100267will be ignored.
Ramkumar Ramachandra5bc3f0b2013-05-31 17:42:15 +0530268
269When `-S` or `-G` are used without `--pickaxe-all`, only filepairs
270that match their respective criterion are kept in the output. When
271`--pickaxe-all` is used, if even one filepair matches their respective
272criterion in a changeset, the entire changeset is kept. This behavior
273is designed to make reviewing changes in the context of the whole
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700274changeset easier.
275
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400276diffcore-order: For Sorting the Output Based on Filenames
Junio C Hamanoa67c1d02005-10-29 00:50:42 -0700277---------------------------------------------------------
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700278
279This is used to reorder the filepairs according to the user's
280(or project's) taste, and is controlled by the -O option to the
Thomas Rast0b444cd2010-01-10 00:33:00 +0100281'git diff-{asterisk}' commands.
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700282
c.shoemaker@cox.net59df2a12005-10-29 00:15:49 -0400283This takes a text file each of whose lines is a shell glob
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700284pattern. Filepairs that match a glob pattern on an earlier line
285in the file are output before ones that match a later line, and
286filepairs that do not match any glob pattern are output last.
287
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100288As an example, a typical orderfile for the core Git probably
Junio C Hamano8db93072005-08-30 13:51:01 -0700289would look like this:
Junio C Hamano4a1332d2005-06-05 14:30:58 -0700290
Junio C Hamano8db93072005-08-30 13:51:01 -0700291------------------------------------------------
Jonas Fonsecadf8baa42005-10-03 19:16:30 +0200292README
293Makefile
294Documentation
295*.h
296*.c
297t
Junio C Hamano8db93072005-08-30 13:51:01 -0700298------------------------------------------------
Christian Couder30eba7b2008-06-06 09:07:28 +0200299
Junio C Hamano1eb41362021-02-11 11:57:50 -0800300diffcore-rotate: For Changing At Which Path Output Starts
301---------------------------------------------------------
302
303This transformation takes one pathname, and rotates the set of
304filepairs so that the filepair for the given pathname comes first,
305optionally discarding the paths that come before it. This is used
306to implement the `--skip-to` and the `--rotate-to` options. It is
307an error when the specified pathname is not in the set of filepairs,
308but it is not useful to error out when used with "git log" family of
309commands, because it is unreasonable to expect that a given path
310would be modified by each and every commit shown by the "git log"
311command. For this reason, when used with "git log", the filepair
312that sorts the same as, or the first one that sorts after, the given
313pathname is where the output starts.
314
315Use of this transformation combined with diffcore-order will produce
316unexpected results, as the input to this transformation is likely
317not sorted when diffcore-order is in effect.
318
319
Christian Couder30eba7b2008-06-06 09:07:28 +0200320SEE ALSO
321--------
322linkgit:git-diff[1],
323linkgit:git-diff-files[1],
324linkgit:git-diff-index[1],
325linkgit:git-diff-tree[1],
326linkgit:git-format-patch[1],
327linkgit:git-log[1],
328linkgit:gitglossary[7],
329link:user-manual.html[The Git User's Manual]
330
331GIT
332---
Stefan Beller941b9c52017-02-08 17:29:30 -0800333Part of the linkgit:git[1] suite