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