]>
Commit | Line | Data |
---|---|---|
5f1c3f07 JH |
1 | #include "cache.h" |
2 | #include "diff.h" | |
3 | #include "commit.h" | |
4 | #include "log-tree.h" | |
8860fd42 | 5 | #include "reflog-walk.h" |
5f1c3f07 | 6 | |
c8c893c6 LT |
7 | static void show_parents(struct commit *commit, int abbrev) |
8 | { | |
9 | struct commit_list *p; | |
10 | for (p = commit->parents; p ; p = p->next) { | |
11 | struct commit *parent = p->item; | |
12 | printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev)); | |
13 | } | |
14 | } | |
15 | ||
fc1c75ec FBH |
16 | /* |
17 | * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches | |
18 | * Signed-off-by: and Acked-by: lines. | |
19 | */ | |
20 | static int detect_any_signoff(char *letter, int size) | |
21 | { | |
22 | char ch, *cp; | |
23 | int seen_colon = 0; | |
24 | int seen_at = 0; | |
25 | int seen_name = 0; | |
26 | int seen_head = 0; | |
27 | ||
28 | cp = letter + size; | |
29 | while (letter <= --cp && (ch = *cp) == '\n') | |
30 | continue; | |
31 | ||
32 | while (letter <= cp) { | |
33 | ch = *cp--; | |
34 | if (ch == '\n') | |
35 | break; | |
36 | ||
37 | if (!seen_at) { | |
38 | if (ch == '@') | |
39 | seen_at = 1; | |
40 | continue; | |
41 | } | |
42 | if (!seen_colon) { | |
43 | if (ch == '@') | |
44 | return 0; | |
45 | else if (ch == ':') | |
46 | seen_colon = 1; | |
47 | else | |
48 | seen_name = 1; | |
49 | continue; | |
50 | } | |
51 | if (('A' <= ch && ch <= 'Z') || | |
52 | ('a' <= ch && ch <= 'z') || | |
53 | ch == '-') { | |
54 | seen_head = 1; | |
55 | continue; | |
56 | } | |
57 | /* no empty last line doesn't match */ | |
58 | return 0; | |
59 | } | |
60 | return seen_head && seen_name; | |
61 | } | |
62 | ||
cf2251b6 JH |
63 | static int append_signoff(char *buf, int buf_sz, int at, const char *signoff) |
64 | { | |
cf2251b6 | 65 | static const char signed_off_by[] = "Signed-off-by: "; |
fc1c75ec FBH |
66 | int signoff_len = strlen(signoff); |
67 | int has_signoff = 0; | |
cf2251b6 JH |
68 | char *cp = buf; |
69 | ||
70 | /* Do we have enough space to add it? */ | |
c35f4c37 | 71 | if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3) |
cf2251b6 JH |
72 | return at; |
73 | ||
74 | /* First see if we already have the sign-off by the signer */ | |
fc1c75ec FBH |
75 | while ((cp = strstr(cp, signed_off_by))) { |
76 | ||
77 | has_signoff = 1; | |
78 | ||
cf2251b6 | 79 | cp += strlen(signed_off_by); |
fc1c75ec FBH |
80 | if (cp + signoff_len >= buf + at) |
81 | break; | |
82 | if (strncmp(cp, signoff, signoff_len)) | |
83 | continue; | |
84 | if (!isspace(cp[signoff_len])) | |
85 | continue; | |
86 | /* we already have him */ | |
87 | return at; | |
cf2251b6 JH |
88 | } |
89 | ||
fc1c75ec FBH |
90 | if (!has_signoff) |
91 | has_signoff = detect_any_signoff(buf, at); | |
92 | ||
93 | if (!has_signoff) | |
94 | buf[at++] = '\n'; | |
c35f4c37 | 95 | |
cf2251b6 JH |
96 | strcpy(buf + at, signed_off_by); |
97 | at += strlen(signed_off_by); | |
98 | strcpy(buf + at, signoff); | |
99 | at += signoff_len; | |
100 | buf[at++] = '\n'; | |
101 | buf[at] = 0; | |
102 | return at; | |
103 | } | |
104 | ||
39bc9a6c | 105 | void show_log(struct rev_info *opt, const char *sep) |
91539833 LT |
106 | { |
107 | static char this_header[16384]; | |
39bc9a6c | 108 | struct log_info *log = opt->loginfo; |
91539833 LT |
109 | struct commit *commit = log->commit, *parent = log->parent; |
110 | int abbrev = opt->diffopt.abbrev; | |
111 | int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40; | |
a4d34e2d | 112 | const char *extra; |
91539833 | 113 | int len; |
20ff0680 | 114 | const char *subject = NULL, *extra_headers = opt->extra_headers; |
91539833 LT |
115 | |
116 | opt->loginfo = NULL; | |
117 | if (!opt->verbose_header) { | |
74bd9029 JH |
118 | if (opt->left_right) { |
119 | if (commit->object.flags & BOUNDARY) | |
120 | putchar('-'); | |
121 | else if (commit->object.flags & SYMMETRIC_LEFT) | |
122 | putchar('<'); | |
123 | else | |
124 | putchar('>'); | |
125 | } | |
c8c893c6 LT |
126 | fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout); |
127 | if (opt->parents) | |
128 | show_parents(commit, abbrev_commit); | |
1dcb6922 | 129 | putchar(opt->diffopt.line_termination); |
91539833 LT |
130 | return; |
131 | } | |
132 | ||
133 | /* | |
a4d34e2d LT |
134 | * The "oneline" format has several special cases: |
135 | * - The pretty-printed commit lacks a newline at the end | |
136 | * of the buffer, but we do want to make sure that we | |
137 | * have a newline there. If the separator isn't already | |
138 | * a newline, add an extra one. | |
139 | * - unlike other log messages, the one-line format does | |
140 | * not have an empty line between entries. | |
91539833 | 141 | */ |
a4d34e2d LT |
142 | extra = ""; |
143 | if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE) | |
144 | extra = "\n"; | |
91539833 LT |
145 | if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE) |
146 | putchar('\n'); | |
147 | opt->shown_one = 1; | |
148 | ||
149 | /* | |
150 | * Print header line of header.. | |
151 | */ | |
3eefc189 | 152 | |
596524b3 | 153 | if (opt->commit_format == CMIT_FMT_EMAIL) { |
698ce6f8 | 154 | char *sha1 = sha1_to_hex(commit->object.sha1); |
596524b3 JS |
155 | if (opt->total > 0) { |
156 | static char buffer[64]; | |
157 | snprintf(buffer, sizeof(buffer), | |
158 | "Subject: [PATCH %d/%d] ", | |
159 | opt->nr, opt->total); | |
160 | subject = buffer; | |
8ac80a57 | 161 | } else if (opt->total == 0) |
596524b3 | 162 | subject = "Subject: [PATCH] "; |
8ac80a57 JS |
163 | else |
164 | subject = "Subject: "; | |
165 | ||
698ce6f8 | 166 | printf("From %s Mon Sep 17 00:00:00 2001\n", sha1); |
d1566f78 JT |
167 | if (opt->message_id) |
168 | printf("Message-Id: <%s>\n", opt->message_id); | |
169 | if (opt->ref_message_id) | |
170 | printf("In-Reply-To: <%s>\nReferences: <%s>\n", | |
171 | opt->ref_message_id, opt->ref_message_id); | |
698ce6f8 JS |
172 | if (opt->mime_boundary) { |
173 | static char subject_buffer[1024]; | |
174 | static char buffer[1024]; | |
175 | snprintf(subject_buffer, sizeof(subject_buffer) - 1, | |
20ff0680 | 176 | "%s" |
698ce6f8 JS |
177 | "MIME-Version: 1.0\n" |
178 | "Content-Type: multipart/mixed;\n" | |
179 | " boundary=\"%s%s\"\n" | |
180 | "\n" | |
181 | "This is a multi-part message in MIME " | |
182 | "format.\n" | |
183 | "--%s%s\n" | |
184 | "Content-Type: text/plain; " | |
185 | "charset=UTF-8; format=fixed\n" | |
186 | "Content-Transfer-Encoding: 8bit\n\n", | |
20ff0680 | 187 | extra_headers ? extra_headers : "", |
698ce6f8 JS |
188 | mime_boundary_leader, opt->mime_boundary, |
189 | mime_boundary_leader, opt->mime_boundary); | |
20ff0680 | 190 | extra_headers = subject_buffer; |
698ce6f8 JS |
191 | |
192 | snprintf(buffer, sizeof(buffer) - 1, | |
193 | "--%s%s\n" | |
194 | "Content-Type: text/x-patch;\n" | |
195 | " name=\"%s.diff\"\n" | |
196 | "Content-Transfer-Encoding: 8bit\n" | |
197 | "Content-Disposition: inline;\n" | |
198 | " filename=\"%s.diff\"\n\n", | |
199 | mime_boundary_leader, opt->mime_boundary, | |
200 | sha1, sha1); | |
201 | opt->diffopt.stat_sep = buffer; | |
202 | } | |
596524b3 | 203 | } else { |
74bd9029 JH |
204 | fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT), |
205 | stdout); | |
206 | if (opt->commit_format != CMIT_FMT_ONELINE) | |
207 | fputs("commit ", stdout); | |
208 | if (opt->left_right) { | |
209 | if (commit->object.flags & BOUNDARY) | |
210 | putchar('-'); | |
211 | else if (commit->object.flags & SYMMETRIC_LEFT) | |
212 | putchar('<'); | |
213 | else | |
214 | putchar('>'); | |
215 | } | |
216 | fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), | |
217 | stdout); | |
c66b6c06 JH |
218 | if (opt->parents) |
219 | show_parents(commit, abbrev_commit); | |
73f0a157 | 220 | if (parent) |
3eefc189 JH |
221 | printf(" (from %s)", |
222 | diff_unique_abbrev(parent->object.sha1, | |
223 | abbrev_commit)); | |
ce436973 JK |
224 | printf("%s", |
225 | diff_get_color(opt->diffopt.color_diff, DIFF_RESET)); | |
e557667e | 226 | putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n'); |
8860fd42 JS |
227 | if (opt->reflog_info) |
228 | show_reflog_message(opt->reflog_info); | |
3eefc189 | 229 | } |
91539833 LT |
230 | |
231 | /* | |
232 | * And then the pretty-printed message itself | |
233 | */ | |
3dfb9278 JF |
234 | len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, |
235 | sizeof(this_header), abbrev, subject, | |
236 | extra_headers, opt->relative_date); | |
cf2251b6 JH |
237 | |
238 | if (opt->add_signoff) | |
239 | len = append_signoff(this_header, sizeof(this_header), len, | |
240 | opt->add_signoff); | |
a4d34e2d | 241 | printf("%s%s%s", this_header, extra, sep); |
91539833 LT |
242 | } |
243 | ||
cd2bdc53 | 244 | int log_tree_diff_flush(struct rev_info *opt) |
5f1c3f07 JH |
245 | { |
246 | diffcore_std(&opt->diffopt); | |
91539833 | 247 | |
5f1c3f07 JH |
248 | if (diff_queue_is_empty()) { |
249 | int saved_fmt = opt->diffopt.output_format; | |
250 | opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; | |
251 | diff_flush(&opt->diffopt); | |
252 | opt->diffopt.output_format = saved_fmt; | |
253 | return 0; | |
254 | } | |
91539833 | 255 | |
3969cf7d JH |
256 | if (opt->loginfo && !opt->no_commit_id) { |
257 | /* When showing a verbose header (i.e. log message), | |
258 | * and not in --pretty=oneline format, we would want | |
259 | * an extra newline between the end of log and the | |
260 | * output for readability. | |
261 | */ | |
39bc9a6c | 262 | show_log(opt, opt->diffopt.msg_sep); |
3969cf7d JH |
263 | if (opt->verbose_header && |
264 | opt->commit_format != CMIT_FMT_ONELINE) { | |
265 | int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH; | |
266 | if ((pch & opt->diffopt.output_format) == pch) | |
267 | printf("---%c", opt->diffopt.line_termination); | |
268 | else | |
269 | putchar(opt->diffopt.line_termination); | |
270 | } | |
271 | } | |
5f1c3f07 JH |
272 | diff_flush(&opt->diffopt); |
273 | return 1; | |
274 | } | |
275 | ||
cd2bdc53 | 276 | static int do_diff_combined(struct rev_info *opt, struct commit *commit) |
5f1c3f07 JH |
277 | { |
278 | unsigned const char *sha1 = commit->object.sha1; | |
279 | ||
91539833 LT |
280 | diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt); |
281 | return !opt->loginfo; | |
5f1c3f07 JH |
282 | } |
283 | ||
91539833 LT |
284 | /* |
285 | * Show the diff of a commit. | |
286 | * | |
287 | * Return true if we printed any log info messages | |
288 | */ | |
289 | static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) | |
5f1c3f07 | 290 | { |
91539833 | 291 | int showed_log; |
5f1c3f07 JH |
292 | struct commit_list *parents; |
293 | unsigned const char *sha1 = commit->object.sha1; | |
294 | ||
91539833 LT |
295 | if (!opt->diff) |
296 | return 0; | |
297 | ||
5f1c3f07 | 298 | /* Root commit? */ |
91539833 LT |
299 | parents = commit->parents; |
300 | if (!parents) { | |
2b60356d RS |
301 | if (opt->show_root_diff) { |
302 | diff_root_tree_sha1(sha1, "", &opt->diffopt); | |
303 | log_tree_diff_flush(opt); | |
304 | } | |
91539833 | 305 | return !opt->loginfo; |
5f1c3f07 JH |
306 | } |
307 | ||
308 | /* More than one parent? */ | |
91539833 | 309 | if (parents && parents->next) { |
5f1c3f07 JH |
310 | if (opt->ignore_merges) |
311 | return 0; | |
312 | else if (opt->combine_merges) | |
313 | return do_diff_combined(opt, commit); | |
91539833 LT |
314 | |
315 | /* If we show individual diffs, show the parent info */ | |
316 | log->parent = parents->item; | |
5f1c3f07 JH |
317 | } |
318 | ||
91539833 LT |
319 | showed_log = 0; |
320 | for (;;) { | |
5f1c3f07 | 321 | struct commit *parent = parents->item; |
5f1c3f07 | 322 | |
91539833 LT |
323 | diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt); |
324 | log_tree_diff_flush(opt); | |
325 | ||
326 | showed_log |= !opt->loginfo; | |
327 | ||
328 | /* Set up the log info for the next parent, if any.. */ | |
329 | parents = parents->next; | |
330 | if (!parents) | |
331 | break; | |
332 | log->parent = parents->item; | |
333 | opt->loginfo = log; | |
334 | } | |
335 | return showed_log; | |
336 | } | |
337 | ||
338 | int log_tree_commit(struct rev_info *opt, struct commit *commit) | |
339 | { | |
340 | struct log_info log; | |
3eefc189 | 341 | int shown; |
91539833 LT |
342 | |
343 | log.commit = commit; | |
344 | log.parent = NULL; | |
345 | opt->loginfo = &log; | |
346 | ||
3eefc189 JH |
347 | shown = log_tree_diff(opt, commit, &log); |
348 | if (!shown && opt->loginfo && opt->always_show_header) { | |
91539833 | 349 | log.parent = NULL; |
39bc9a6c | 350 | show_log(opt, ""); |
3eefc189 | 351 | shown = 1; |
5f1c3f07 | 352 | } |
91539833 | 353 | opt->loginfo = NULL; |
3eefc189 | 354 | return shown; |
5f1c3f07 | 355 | } |