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