1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "tag.h"
5#include "graph.h"
6#include "log-tree.h"
7#include "reflog-walk.h"
8#include "refs.h"
9#include "string-list.h"
10
11struct decoration name_decoration = { "object names" };
12
13static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
14{
15 int plen = strlen(prefix);
16 int nlen = strlen(name);
17 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
18 memcpy(res->name, prefix, plen);
19 memcpy(res->name + plen, name, nlen + 1);
20 res->next = add_decoration(&name_decoration, obj, res);
21}
22
23static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
24{
25 struct object *obj = parse_object(sha1);
26 if (!obj)
27 return 0;
28 refname = prettify_refname(refname);
29 add_name_decoration("", refname, obj);
30 while (obj->type == OBJ_TAG) {
31 obj = ((struct tag *)obj)->tagged;
32 if (!obj)
33 break;
34 add_name_decoration("tag: ", refname, obj);
35 }
36 return 0;
37}
38
39void load_ref_decorations(void)
40{
41 static int loaded;
42 if (!loaded) {
43 loaded = 1;
44 for_each_ref(add_ref_decoration, NULL);
45 }
46}
47
48static void show_parents(struct commit *commit, int abbrev)
49{
50 struct commit_list *p;
51 for (p = commit->parents; p ; p = p->next) {
52 struct commit *parent = p->item;
53 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
54 }
55}
56
57void show_decorations(struct rev_info *opt, struct commit *commit)
58{
59 const char *prefix;
60 struct name_decoration *decoration;
61
62 if (opt->show_source && commit->util)
63 printf("\t%s", (char *) commit->util);
64 if (!opt->show_decorations)
65 return;
66 decoration = lookup_decoration(&name_decoration, &commit->object);
67 if (!decoration)
68 return;
69 prefix = " (";
70 while (decoration) {
71 printf("%s%s", prefix, decoration->name);
72 prefix = ", ";
73 decoration = decoration->next;
74 }
75 putchar(')');
76}
77
78/*
79 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
80 * Signed-off-by: and Acked-by: lines.
81 */
82static int detect_any_signoff(char *letter, int size)
83{
84 char *cp;
85 int seen_colon = 0;
86 int seen_at = 0;
87 int seen_name = 0;
88 int seen_head = 0;
89
90 cp = letter + size;
91 while (letter <= --cp && *cp == '\n')
92 continue;
93
94 while (letter <= cp) {
95 char ch = *cp--;
96 if (ch == '\n')
97 break;
98
99 if (!seen_at) {
100 if (ch == '@')
101 seen_at = 1;
102 continue;
103 }
104 if (!seen_colon) {
105 if (ch == '@')
106 return 0;
107 else if (ch == ':')
108 seen_colon = 1;
109 else
110 seen_name = 1;
111 continue;
112 }
113 if (('A' <= ch && ch <= 'Z') ||
114 ('a' <= ch && ch <= 'z') ||
115 ch == '-') {
116 seen_head = 1;
117 continue;
118 }
119 /* no empty last line doesn't match */
120 return 0;
121 }
122 return seen_head && seen_name;
123}
124
125static void append_signoff(struct strbuf *sb, const char *signoff)
126{
127 static const char signed_off_by[] = "Signed-off-by: ";
128 size_t signoff_len = strlen(signoff);
129 int has_signoff = 0;
130 char *cp;
131
132 cp = sb->buf;
133
134 /* First see if we already have the sign-off by the signer */
135 while ((cp = strstr(cp, signed_off_by))) {
136
137 has_signoff = 1;
138
139 cp += strlen(signed_off_by);
140 if (cp + signoff_len >= sb->buf + sb->len)
141 break;
142 if (strncmp(cp, signoff, signoff_len))
143 continue;
144 if (!isspace(cp[signoff_len]))
145 continue;
146 /* we already have him */
147 return;
148 }
149
150 if (!has_signoff)
151 has_signoff = detect_any_signoff(sb->buf, sb->len);
152
153 if (!has_signoff)
154 strbuf_addch(sb, '\n');
155
156 strbuf_addstr(sb, signed_off_by);
157 strbuf_add(sb, signoff, signoff_len);
158 strbuf_addch(sb, '\n');
159}
160
161static unsigned int digits_in_number(unsigned int number)
162{
163 unsigned int i = 10, result = 1;
164 while (i <= number) {
165 i *= 10;
166 result++;
167 }
168 return result;
169}
170
171void get_patch_filename(struct commit *commit, int nr, const char *suffix,
172 struct strbuf *buf)
173{
174 int suffix_len = strlen(suffix) + 1;
175 int start_len = buf->len;
176
177 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
178 if (commit) {
179 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
180
181 format_commit_message(commit, "%f", buf, DATE_NORMAL);
182 if (max_len < buf->len)
183 strbuf_setlen(buf, max_len);
184 strbuf_addstr(buf, suffix);
185 }
186}
187
188void log_write_email_headers(struct rev_info *opt, struct commit *commit,
189 const char **subject_p,
190 const char **extra_headers_p,
191 int *need_8bit_cte_p)
192{
193 const char *subject = NULL;
194 const char *extra_headers = opt->extra_headers;
195 const char *name = sha1_to_hex(commit->object.sha1);
196
197 *need_8bit_cte_p = 0; /* unknown */
198 if (opt->total > 0) {
199 static char buffer[64];
200 snprintf(buffer, sizeof(buffer),
201 "Subject: [%s %0*d/%d] ",
202 opt->subject_prefix,
203 digits_in_number(opt->total),
204 opt->nr, opt->total);
205 subject = buffer;
206 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
207 static char buffer[256];
208 snprintf(buffer, sizeof(buffer),
209 "Subject: [%s] ",
210 opt->subject_prefix);
211 subject = buffer;
212 } else {
213 subject = "Subject: ";
214 }
215
216 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
217 graph_show_oneline(opt->graph);
218 if (opt->message_id) {
219 printf("Message-Id: <%s>\n", opt->message_id);
220 graph_show_oneline(opt->graph);
221 }
222 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
223 int i, n;
224 n = opt->ref_message_ids->nr;
225 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
226 for (i = 0; i < n; i++)
227 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
228 opt->ref_message_ids->items[i].string);
229 graph_show_oneline(opt->graph);
230 }
231 if (opt->mime_boundary) {
232 static char subject_buffer[1024];
233 static char buffer[1024];
234 struct strbuf filename = STRBUF_INIT;
235 *need_8bit_cte_p = -1; /* NEVER */
236 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
237 "%s"
238 "MIME-Version: 1.0\n"
239 "Content-Type: multipart/mixed;"
240 " boundary=\"%s%s\"\n"
241 "\n"
242 "This is a multi-part message in MIME "
243 "format.\n"
244 "--%s%s\n"
245 "Content-Type: text/plain; "
246 "charset=UTF-8; format=fixed\n"
247 "Content-Transfer-Encoding: 8bit\n\n",
248 extra_headers ? extra_headers : "",
249 mime_boundary_leader, opt->mime_boundary,
250 mime_boundary_leader, opt->mime_boundary);
251 extra_headers = subject_buffer;
252
253 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
254 opt->patch_suffix, &filename);
255 snprintf(buffer, sizeof(buffer) - 1,
256 "\n--%s%s\n"
257 "Content-Type: text/x-patch;"
258 " name=\"%s\"\n"
259 "Content-Transfer-Encoding: 8bit\n"
260 "Content-Disposition: %s;"
261 " filename=\"%s\"\n\n",
262 mime_boundary_leader, opt->mime_boundary,
263 filename.buf,
264 opt->no_inline ? "attachment" : "inline",
265 filename.buf);
266 opt->diffopt.stat_sep = buffer;
267 strbuf_release(&filename);
268 }
269 *subject_p = subject;
270 *extra_headers_p = extra_headers;
271}
272
273void show_log(struct rev_info *opt)
274{
275 struct strbuf msgbuf = STRBUF_INIT;
276 struct log_info *log = opt->loginfo;
277 struct commit *commit = log->commit, *parent = log->parent;
278 int abbrev = opt->diffopt.abbrev;
279 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
280 const char *subject = NULL, *extra_headers = opt->extra_headers;
281 int need_8bit_cte = 0;
282
283 opt->loginfo = NULL;
284 if (!opt->verbose_header) {
285 graph_show_commit(opt->graph);
286
287 if (!opt->graph) {
288 if (commit->object.flags & BOUNDARY)
289 putchar('-');
290 else if (commit->object.flags & UNINTERESTING)
291 putchar('^');
292 else if (opt->left_right) {
293 if (commit->object.flags & SYMMETRIC_LEFT)
294 putchar('<');
295 else
296 putchar('>');
297 }
298 }
299 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
300 if (opt->print_parents)
301 show_parents(commit, abbrev_commit);
302 show_decorations(opt, commit);
303 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
304 putchar('\n');
305 graph_show_remainder(opt->graph);
306 }
307 putchar(opt->diffopt.line_termination);
308 return;
309 }
310
311 /*
312 * If use_terminator is set, we already handled any record termination
313 * at the end of the last record.
314 * Otherwise, add a diffopt.line_termination character before all
315 * entries but the first. (IOW, as a separator between entries)
316 */
317 if (opt->shown_one && !opt->use_terminator) {
318 /*
319 * If entries are separated by a newline, the output
320 * should look human-readable. If the last entry ended
321 * with a newline, print the graph output before this
322 * newline. Otherwise it will end up as a completely blank
323 * line and will look like a gap in the graph.
324 *
325 * If the entry separator is not a newline, the output is
326 * primarily intended for programmatic consumption, and we
327 * never want the extra graph output before the entry
328 * separator.
329 */
330 if (opt->diffopt.line_termination == '\n' &&
331 !opt->missing_newline)
332 graph_show_padding(opt->graph);
333 putchar(opt->diffopt.line_termination);
334 }
335 opt->shown_one = 1;
336
337 /*
338 * If the history graph was requested,
339 * print the graph, up to this commit's line
340 */
341 graph_show_commit(opt->graph);
342
343 /*
344 * Print header line of header..
345 */
346
347 if (opt->commit_format == CMIT_FMT_EMAIL) {
348 log_write_email_headers(opt, commit, &subject, &extra_headers,
349 &need_8bit_cte);
350 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
351 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
352 if (opt->commit_format != CMIT_FMT_ONELINE)
353 fputs("commit ", stdout);
354
355 if (!opt->graph) {
356 if (commit->object.flags & BOUNDARY)
357 putchar('-');
358 else if (commit->object.flags & UNINTERESTING)
359 putchar('^');
360 else if (opt->left_right) {
361 if (commit->object.flags & SYMMETRIC_LEFT)
362 putchar('<');
363 else
364 putchar('>');
365 }
366 }
367 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
368 stdout);
369 if (opt->print_parents)
370 show_parents(commit, abbrev_commit);
371 if (parent)
372 printf(" (from %s)",
373 find_unique_abbrev(parent->object.sha1,
374 abbrev_commit));
375 show_decorations(opt, commit);
376 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
377 if (opt->commit_format == CMIT_FMT_ONELINE) {
378 putchar(' ');
379 } else {
380 putchar('\n');
381 graph_show_oneline(opt->graph);
382 }
383 if (opt->reflog_info) {
384 /*
385 * setup_revisions() ensures that opt->reflog_info
386 * and opt->graph cannot both be set,
387 * so we don't need to worry about printing the
388 * graph info here.
389 */
390 show_reflog_message(opt->reflog_info,
391 opt->commit_format == CMIT_FMT_ONELINE,
392 opt->date_mode);
393 if (opt->commit_format == CMIT_FMT_ONELINE)
394 return;
395 }
396 }
397
398 if (!commit->buffer)
399 return;
400
401 /*
402 * And then the pretty-printed message itself
403 */
404 if (need_8bit_cte >= 0)
405 need_8bit_cte = has_non_ascii(opt->add_signoff);
406 pretty_print_commit(opt->commit_format, commit, &msgbuf,
407 abbrev, subject, extra_headers, opt->date_mode,
408 need_8bit_cte);
409
410 if (opt->add_signoff)
411 append_signoff(&msgbuf, opt->add_signoff);
412 if (opt->show_log_size) {
413 printf("log size %i\n", (int)msgbuf.len);
414 graph_show_oneline(opt->graph);
415 }
416
417 /*
418 * Set opt->missing_newline if msgbuf doesn't
419 * end in a newline (including if it is empty)
420 */
421 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
422 opt->missing_newline = 1;
423 else
424 opt->missing_newline = 0;
425
426 if (opt->graph)
427 graph_show_commit_msg(opt->graph, &msgbuf);
428 else
429 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
430 if (opt->use_terminator) {
431 if (!opt->missing_newline)
432 graph_show_padding(opt->graph);
433 putchar('\n');
434 }
435
436 strbuf_release(&msgbuf);
437}
438
439int log_tree_diff_flush(struct rev_info *opt)
440{
441 diffcore_std(&opt->diffopt);
442
443 if (diff_queue_is_empty()) {
444 int saved_fmt = opt->diffopt.output_format;
445 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
446 diff_flush(&opt->diffopt);
447 opt->diffopt.output_format = saved_fmt;
448 return 0;
449 }
450
451 if (opt->loginfo && !opt->no_commit_id) {
452 /* When showing a verbose header (i.e. log message),
453 * and not in --pretty=oneline format, we would want
454 * an extra newline between the end of log and the
455 * output for readability.
456 */
457 show_log(opt);
458 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
459 opt->verbose_header &&
460 opt->commit_format != CMIT_FMT_ONELINE) {
461 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
462 if ((pch & opt->diffopt.output_format) == pch)
463 printf("---");
464 putchar('\n');
465 }
466 }
467 diff_flush(&opt->diffopt);
468 return 1;
469}
470
471static int do_diff_combined(struct rev_info *opt, struct commit *commit)
472{
473 unsigned const char *sha1 = commit->object.sha1;
474
475 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
476 return !opt->loginfo;
477}
478
479/*
480 * Show the diff of a commit.
481 *
482 * Return true if we printed any log info messages
483 */
484static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
485{
486 int showed_log;
487 struct commit_list *parents;
488 unsigned const char *sha1 = commit->object.sha1;
489
490 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
491 return 0;
492
493 /* Root commit? */
494 parents = commit->parents;
495 if (!parents) {
496 if (opt->show_root_diff) {
497 diff_root_tree_sha1(sha1, "", &opt->diffopt);
498 log_tree_diff_flush(opt);
499 }
500 return !opt->loginfo;
501 }
502
503 /* More than one parent? */
504 if (parents && parents->next) {
505 if (opt->ignore_merges)
506 return 0;
507 else if (opt->combine_merges)
508 return do_diff_combined(opt, commit);
509
510 /* If we show individual diffs, show the parent info */
511 log->parent = parents->item;
512 }
513
514 showed_log = 0;
515 for (;;) {
516 struct commit *parent = parents->item;
517
518 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
519 log_tree_diff_flush(opt);
520
521 showed_log |= !opt->loginfo;
522
523 /* Set up the log info for the next parent, if any.. */
524 parents = parents->next;
525 if (!parents)
526 break;
527 log->parent = parents->item;
528 opt->loginfo = log;
529 }
530 return showed_log;
531}
532
533int log_tree_commit(struct rev_info *opt, struct commit *commit)
534{
535 struct log_info log;
536 int shown;
537
538 log.commit = commit;
539 log.parent = NULL;
540 opt->loginfo = &log;
541
542 shown = log_tree_diff(opt, commit, &log);
543 if (!shown && opt->loginfo && opt->always_show_header) {
544 log.parent = NULL;
545 show_log(opt);
546 shown = 1;
547 }
548 opt->loginfo = NULL;
549 maybe_flush_or_die(stdout, "stdout");
550 return shown;
551}