Documentation / diff-format.txton commit git-apply: Documentation typo fix (f847c07)
   1The output format from "git-diff-index", "git-diff-tree" and
   2"git-diff-files" are very similar.
   3
   4These commands all compare two sets of things; what is 
   5compared differs:
   6
   7git-diff-index <tree-ish>::
   8        compares the <tree-ish> and the files on the filesystem.
   9
  10git-diff-index --cached <tree-ish>::
  11        compares the <tree-ish> and the index.
  12
  13git-diff-tree [-r] <tree-ish-1> <tree-ish-2> [<pattern>...]::
  14        compares the trees named by the two arguments.
  15
  16git-diff-files [<pattern>...]::
  17        compares the index and the files on the filesystem.
  18
  19
  20An output line is formatted this way:
  21
  22------------------------------------------------
  23in-place edit  :100644 100644 bcd1234... 0123456... M file0
  24copy-edit      :100644 100644 abcd123... 1234567... C68 file1 file2
  25rename-edit    :100644 100644 abcd123... 1234567... R86 file1 file3
  26create         :000000 100644 0000000... 1234567... A file4
  27delete         :100644 000000 1234567... 0000000... D file5
  28unmerged       :000000 000000 0000000... 0000000... U file6
  29------------------------------------------------
  30
  31That is, from the left to the right:
  32
  33. a colon.
  34. mode for "src"; 000000 if creation or unmerged.
  35. a space.
  36. mode for "dst"; 000000 if deletion or unmerged.
  37. a space.
  38. sha1 for "src"; 0\{40\} if creation or unmerged.
  39. a space.
  40. sha1 for "dst"; 0\{40\} if creation, unmerged or "look at work tree".
  41. a space.
  42. status, followed by optional "score" number.
  43. a tab or a NUL when '-z' option is used.
  44. path for "src"
  45. a tab or a NUL when '-z' option is used; only exists for C or R.
  46. path for "dst"; only exists for C or R.
  47. an LF or a NUL when '-z' option is used, to terminate the record.
  48
  49<sha1> is shown as all 0's if a file is new on the filesystem
  50and it is out of sync with the index.
  51
  52Example:
  53
  54------------------------------------------------
  55:100644 100644 5be4a4...... 000000...... M file.c
  56------------------------------------------------
  57
  58When `-z` option is not used, TAB, LF, and backslash characters
  59in pathnames are represented as `\t`, `\n`, and `\\`,
  60respectively.
  61
  62
  63Generating patches with -p
  64--------------------------
  65
  66When "git-diff-index", "git-diff-tree", or "git-diff-files" are run
  67with a '-p' option, they do not produce the output described above;
  68instead they produce a patch file.
  69
  70The patch generation can be customized at two levels.
  71
  721. When the environment variable 'GIT_EXTERNAL_DIFF' is not set,
  73   these commands internally invoke "diff" like this:
  74
  75      diff -L a/<path> -L b/<path> -pu <old> <new>
  76+
  77For added files, `/dev/null` is used for <old>.  For removed
  78files, `/dev/null` is used for <new>
  79+
  80The "diff" formatting options can be customized via the
  81environment variable 'GIT_DIFF_OPTS'.  For example, if you
  82prefer context diff:
  83
  84      GIT_DIFF_OPTS=-c git-diff-index -p HEAD
  85
  86
  872. When the environment variable 'GIT_EXTERNAL_DIFF' is set, the
  88   program named by it is called, instead of the diff invocation
  89   described above.
  90+
  91For a path that is added, removed, or modified,
  92'GIT_EXTERNAL_DIFF' is called with 7 parameters:
  93
  94     path old-file old-hex old-mode new-file new-hex new-mode
  95+
  96where:
  97
  98     <old|new>-file:: are files GIT_EXTERNAL_DIFF can use to read the
  99                      contents of <old|new>,
 100     <old|new>-hex:: are the 40-hexdigit SHA1 hashes,
 101     <old|new>-mode:: are the octal representation of the file modes.
 102
 103+ 
 104The file parameters can point at the user's working file
 105(e.g. `new-file` in "git-diff-files"), `/dev/null` (e.g. `old-file`
 106when a new file is added), or a temporary file (e.g. `old-file` in the
 107index).  'GIT_EXTERNAL_DIFF' should not worry about unlinking the
 108temporary file --- it is removed when 'GIT_EXTERNAL_DIFF' exits.
 109
 110For a path that is unmerged, 'GIT_EXTERNAL_DIFF' is called with 1
 111parameter, <path>.
 112
 113
 114git specific extension to diff format
 115-------------------------------------
 116
 117What -p option produces is slightly different from the
 118traditional diff format.
 119
 1201.   It is preceded with a "git diff" header, that looks like
 121     this:
 122
 123     diff --git a/file1 b/file2
 124+
 125The `a/` and `b/` filenames are the same unless rename/copy is
 126involved.  Especially, even for a creation or a deletion,
 127`/dev/null` is _not_ used in place of `a/` or `b/` filenames.
 128+
 129When rename/copy is involved, `file1` and `file2` show the
 130name of the source file of the rename/copy and the name of
 131the file that rename/copy produces, respectively.
 132
 1332.   It is followed by one or more extended header lines:
 134
 135       old mode <mode>
 136       new mode <mode>
 137       deleted file mode <mode>
 138       new file mode <mode>
 139       copy from <path>
 140       copy to <path>
 141       rename from <path>
 142       rename to <path>
 143       similarity index <number>
 144       dissimilarity index <number>
 145       index <hash>..<hash> <mode>
 146
 1473.  TAB, LF, double quote and backslash characters in pathnames
 148    are represented as `\t`, `\n`, `\"` and `\\`, respectively.
 149    If there is need for such substitution then the whole
 150    pathname is put in double quotes.
 151
 152
 153combined diff format
 154--------------------
 155
 156git-diff-tree and git-diff-files can take '-c' or '--cc' option
 157to produce 'combined diff', which looks like this:
 158
 159------------
 160diff --combined describe.c
 161index fabadb8,cc95eb0..4866510
 162--- a/describe.c
 163+++ b/describe.c
 164@@@ -98,20 -98,12 +98,20 @@@
 165        return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
 166  }
 167  
 168- static void describe(char *arg)
 169 -static void describe(struct commit *cmit, int last_one)
 170++static void describe(char *arg, int last_one)
 171  {
 172 +      unsigned char sha1[20];
 173 +      struct commit *cmit;
 174        struct commit_list *list;
 175        static int initialized = 0;
 176        struct commit_name *n;
 177  
 178 +      if (get_sha1(arg, sha1) < 0)
 179 +              usage(describe_usage);
 180 +      cmit = lookup_commit_reference(sha1);
 181 +      if (!cmit)
 182 +              usage(describe_usage);
 183 +
 184        if (!initialized) {
 185                initialized = 1;
 186                for_each_ref(get_name);
 187------------
 188
 1891.   It is preceded with a "git diff" header, that looks like
 190     this (when '-c' option is used):
 191
 192       diff --combined file
 193+
 194or like this (when '--cc' option is used):
 195
 196       diff --c file
 197
 1982.   It is followed by one or more extended header lines
 199     (this example shows a merge with two parents):
 200
 201       index <hash>,<hash>..<hash>
 202       mode <mode>,<mode>..<mode>
 203       new file mode <mode>
 204       deleted file mode <mode>,<mode>
 205+
 206The `mode <mode>,<mode>..<mode>` line appears only if at least one of
 207the <mode> is diferent from the rest. Extended headers with
 208information about detected contents movement (renames and
 209copying detection) are designed to work with diff of two
 210<tree-ish> and are not used by combined diff format.
 211
 2123.   It is followed by two-line from-file/to-file header
 213
 214       --- a/file
 215       +++ b/file
 216+
 217Similar to two-line header for traditional 'unified' diff
 218format, `/dev/null` is used to signal created or deleted
 219files.
 220
 2214.   Chunk header format is modified to prevent people from
 222     accidentally feeding it to `patch -p1`. Combined diff format
 223     was created for review of merge commit changes, and was not
 224     meant for apply. The change is similar to the change in the
 225     extended 'index' header:
 226
 227       @@@ <from-file-range> <from-file-range> <to-file-range> @@@
 228+
 229There are (number of parents + 1) `@` characters in the chunk
 230header for combined diff format.
 231
 232Unlike the traditional 'unified' diff format, which shows two
 233files A and B with a single column that has `-` (minus --
 234appears in A but removed in B), `+` (plus -- missing in A but
 235added to B), or `" "` (space -- unchanged) prefix, this format
 236compares two or more files file1, file2,... with one file X, and
 237shows how X differs from each of fileN.  One column for each of
 238fileN is prepended to the output line to note how X's line is
 239different from it.
 240
 241A `-` character in the column N means that the line appears in
 242fileN but it does not appear in the result.  A `+` character
 243in the column N means that the line appears in the last file,
 244and fileN does not have that line (in other words, the line was
 245added, from the point of view of that parent).
 246
 247In the above example output, the function signature was changed
 248from both files (hence two `-` removals from both file1 and
 249file2, plus `++` to mean one line that was added does not appear
 250in either file1 nor file2).  Also two other lines are the same
 251from file1 but do not appear in file2 (hence prefixed with ` +`).
 252
 253When shown by `git diff-tree -c`, it compares the parents of a
 254merge commit with the merge result (i.e. file1..fileN are the
 255parents).  When shown by `git diff-files -c`, it compares the
 256two unresolved merge parents with the working tree file
 257(i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka
 258"their version").
 259