gitdiffcore.txt 11 KB

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