Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 1 | Generating patch text with -p |
| 2 | ----------------------------- |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 3 | |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 4 | Running |
| 5 | linkgit:git-diff[1], |
| 6 | linkgit:git-log[1], |
| 7 | linkgit:git-show[1], |
| 8 | linkgit:git-diff-index[1], |
| 9 | linkgit:git-diff-tree[1], or |
| 10 | linkgit:git-diff-files[1] |
| 11 | with the `-p` option produces patch text. |
| 12 | You can customize the creation of patch text via the |
Junio C Hamano | 042f214 | 2016-06-27 18:05:05 | [diff] [blame] | 13 | `GIT_EXTERNAL_DIFF` and the `GIT_DIFF_OPTS` environment variables. |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 14 | |
| 15 | What the -p option produces is slightly different from the traditional |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 16 | diff format: |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 17 | |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 18 | 1. It is preceded with a "git diff" header that looks like this: |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 19 | |
| 20 | diff --git a/file1 b/file2 |
| 21 | + |
| 22 | The `a/` and `b/` filenames are the same unless rename/copy is |
| 23 | involved. Especially, even for a creation or a deletion, |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 24 | `/dev/null` is _not_ used in place of the `a/` or `b/` filenames. |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 25 | + |
| 26 | When rename/copy is involved, `file1` and `file2` show the |
| 27 | name of the source file of the rename/copy and the name of |
| 28 | the file that rename/copy produces, respectively. |
| 29 | |
| 30 | 2. It is followed by one or more extended header lines: |
| 31 | |
| 32 | old mode <mode> |
| 33 | new mode <mode> |
| 34 | deleted file mode <mode> |
| 35 | new file mode <mode> |
| 36 | copy from <path> |
| 37 | copy to <path> |
| 38 | rename from <path> |
| 39 | rename to <path> |
| 40 | similarity index <number> |
| 41 | dissimilarity index <number> |
| 42 | index <hash>..<hash> <mode> |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 43 | + |
| 44 | File modes are printed as 6-digit octal numbers including the file type |
| 45 | and file permission bits. |
| 46 | + |
| 47 | Path names in extended headers do not include the `a/` and `b/` prefixes. |
| 48 | + |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 49 | The similarity index is the percentage of unchanged lines, and |
| 50 | the dissimilarity index is the percentage of changed lines. It |
| 51 | is a rounded down integer, followed by a percent sign. The |
| 52 | similarity index value of 100% is thus reserved for two equal |
| 53 | files, while 100% dissimilarity means that no line from the old |
| 54 | file made it into the new one. |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 55 | + |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 56 | The index line includes the blob object names before and after the change. |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 57 | The <mode> is included if the file mode does not change; otherwise, |
| 58 | separate lines indicate the old and the new mode. |
| 59 | |
Junio C Hamano | 0f6f3bf | 2017-03-13 07:01:53 | [diff] [blame] | 60 | 3. Pathnames with "unusual" characters are quoted as explained for |
| 61 | the configuration variable `core.quotePath` (see |
| 62 | linkgit:git-config[1]). |
Junio C Hamano | 7f9fa86 | 2010-10-19 23:51:14 | [diff] [blame] | 63 | |
| 64 | 4. All the `file1` files in the output refer to files before the |
| 65 | commit, and all the `file2` files refer to files after the commit. |
| 66 | It is incorrect to apply each change to each file sequentially. For |
| 67 | example, this patch will swap a and b: |
| 68 | |
| 69 | diff --git a/a b/b |
| 70 | rename from a |
| 71 | rename to b |
| 72 | diff --git a/b b/a |
| 73 | rename from b |
| 74 | rename to a |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 75 | |
| 76 | |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 77 | Combined diff format |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 78 | -------------------- |
| 79 | |
Junio C Hamano | c5be4cc | 2011-03-09 07:20:43 | [diff] [blame] | 80 | Any diff-generating command can take the `-c` or `--cc` option to |
| 81 | produce a 'combined diff' when showing a merge. This is the default |
| 82 | format when showing merges with linkgit:git-diff[1] or |
Junio C Hamano | 1dbca52 | 2015-05-22 20:48:55 | [diff] [blame] | 83 | linkgit:git-show[1]. Note also that you can give the `-m` option to any |
Junio C Hamano | c5be4cc | 2011-03-09 07:20:43 | [diff] [blame] | 84 | of these commands to force generation of diffs with individual parents |
| 85 | of a merge. |
| 86 | |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 87 | A "combined diff" format looks like this: |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 88 | |
| 89 | ------------ |
| 90 | diff --combined describe.c |
| 91 | index fabadb8,cc95eb0..4866510 |
| 92 | --- a/describe.c |
| 93 | +++ b/describe.c |
| 94 | @@@ -98,20 -98,12 +98,20 @@@ |
| 95 | return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1; |
| 96 | } |
| 97 | |
| 98 | - static void describe(char *arg) |
| 99 | -static void describe(struct commit *cmit, int last_one) |
| 100 | ++static void describe(char *arg, int last_one) |
| 101 | { |
| 102 | + unsigned char sha1[20]; |
| 103 | + struct commit *cmit; |
| 104 | struct commit_list *list; |
| 105 | static int initialized = 0; |
| 106 | struct commit_name *n; |
| 107 | |
| 108 | + if (get_sha1(arg, sha1) < 0) |
| 109 | + usage(describe_usage); |
| 110 | + cmit = lookup_commit_reference(sha1); |
| 111 | + if (!cmit) |
| 112 | + usage(describe_usage); |
| 113 | + |
| 114 | if (!initialized) { |
| 115 | initialized = 1; |
| 116 | for_each_ref(get_name); |
| 117 | ------------ |
| 118 | |
| 119 | 1. It is preceded with a "git diff" header, that looks like |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 120 | this (when the `-c` option is used): |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 121 | |
| 122 | diff --combined file |
| 123 | + |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 124 | or like this (when the `--cc` option is used): |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 125 | |
Junio C Hamano | 0868a30 | 2008-07-22 09:20:44 | [diff] [blame] | 126 | diff --cc file |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 127 | |
| 128 | 2. It is followed by one or more extended header lines |
| 129 | (this example shows a merge with two parents): |
| 130 | |
| 131 | index <hash>,<hash>..<hash> |
| 132 | mode <mode>,<mode>..<mode> |
| 133 | new file mode <mode> |
| 134 | deleted file mode <mode>,<mode> |
| 135 | + |
| 136 | The `mode <mode>,<mode>..<mode>` line appears only if at least one of |
| 137 | the <mode> is different from the rest. Extended headers with |
| 138 | information about detected contents movement (renames and |
| 139 | copying detection) are designed to work with diff of two |
| 140 | <tree-ish> and are not used by combined diff format. |
| 141 | |
| 142 | 3. It is followed by two-line from-file/to-file header |
| 143 | |
| 144 | --- a/file |
| 145 | +++ b/file |
| 146 | + |
| 147 | Similar to two-line header for traditional 'unified' diff |
| 148 | format, `/dev/null` is used to signal created or deleted |
| 149 | files. |
Junio C Hamano | 11f1537 | 2019-03-07 03:17:00 | [diff] [blame] | 150 | + |
| 151 | However, if the --combined-all-paths option is provided, instead of a |
| 152 | two-line from-file/to-file you get a N+1 line from-file/to-file header, |
| 153 | where N is the number of parents in the merge commit |
| 154 | |
| 155 | --- a/file |
| 156 | --- a/file |
| 157 | --- a/file |
| 158 | +++ b/file |
| 159 | + |
| 160 | This extended format can be useful if rename or copy detection is |
| 161 | active, to allow you to see the original name of the file in different |
| 162 | parents. |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 163 | |
| 164 | 4. Chunk header format is modified to prevent people from |
| 165 | accidentally feeding it to `patch -p1`. Combined diff format |
| 166 | was created for review of merge commit changes, and was not |
Junio C Hamano | 48cd3f1 | 2019-10-09 05:55:30 | [diff] [blame] | 167 | meant to be applied. The change is similar to the change in the |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 168 | extended 'index' header: |
| 169 | |
| 170 | @@@ <from-file-range> <from-file-range> <to-file-range> @@@ |
| 171 | + |
| 172 | There are (number of parents + 1) `@` characters in the chunk |
| 173 | header for combined diff format. |
| 174 | |
| 175 | Unlike the traditional 'unified' diff format, which shows two |
| 176 | files A and B with a single column that has `-` (minus -- |
| 177 | appears in A but removed in B), `+` (plus -- missing in A but |
| 178 | added to B), or `" "` (space -- unchanged) prefix, this format |
| 179 | compares two or more files file1, file2,... with one file X, and |
| 180 | shows how X differs from each of fileN. One column for each of |
| 181 | fileN is prepended to the output line to note how X's line is |
| 182 | different from it. |
| 183 | |
| 184 | A `-` character in the column N means that the line appears in |
| 185 | fileN but it does not appear in the result. A `+` character |
Junio C Hamano | 54bf1e2 | 2008-12-20 06:30:11 | [diff] [blame] | 186 | in the column N means that the line appears in the result, |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 187 | and fileN does not have that line (in other words, the line was |
| 188 | added, from the point of view of that parent). |
| 189 | |
| 190 | In the above example output, the function signature was changed |
| 191 | from both files (hence two `-` removals from both file1 and |
| 192 | file2, plus `++` to mean one line that was added does not appear |
Junio C Hamano | d75148a | 2014-04-08 19:48:38 | [diff] [blame] | 193 | in either file1 or file2). Also eight other lines are the same |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 194 | from file1 but do not appear in file2 (hence prefixed with `+`). |
Junio C Hamano | 00b8b63 | 2007-12-07 09:50:49 | [diff] [blame] | 195 | |
| 196 | When shown by `git diff-tree -c`, it compares the parents of a |
| 197 | merge commit with the merge result (i.e. file1..fileN are the |
| 198 | parents). When shown by `git diff-files -c`, it compares the |
| 199 | two unresolved merge parents with the working tree file |
| 200 | (i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka |
| 201 | "their version"). |