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