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