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