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 #include "color.h"
11 #include "gpg-interface.h"
13 struct decoration name_decoration = { "object names" };
15 enum decoration_type {
16 DECORATION_NONE = 0,
17 DECORATION_REF_LOCAL,
18 DECORATION_REF_REMOTE,
19 DECORATION_REF_TAG,
20 DECORATION_REF_STASH,
21 DECORATION_REF_HEAD,
22 DECORATION_GRAFTED,
23 };
25 static char decoration_colors[][COLOR_MAXLEN] = {
26 GIT_COLOR_RESET,
27 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
28 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
29 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
30 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
31 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
32 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
33 };
35 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
36 {
37 if (want_color(decorate_use_color))
38 return decoration_colors[ix];
39 return "";
40 }
42 static int parse_decorate_color_slot(const char *slot)
43 {
44 /*
45 * We're comparing with 'ignore-case' on
46 * (because config.c sets them all tolower),
47 * but let's match the letters in the literal
48 * string values here with how they are
49 * documented in Documentation/config.txt, for
50 * consistency.
51 *
52 * We love being consistent, don't we?
53 */
54 if (!strcasecmp(slot, "branch"))
55 return DECORATION_REF_LOCAL;
56 if (!strcasecmp(slot, "remoteBranch"))
57 return DECORATION_REF_REMOTE;
58 if (!strcasecmp(slot, "tag"))
59 return DECORATION_REF_TAG;
60 if (!strcasecmp(slot, "stash"))
61 return DECORATION_REF_STASH;
62 if (!strcasecmp(slot, "HEAD"))
63 return DECORATION_REF_HEAD;
64 return -1;
65 }
67 int parse_decorate_color_config(const char *var, const int ofs, const char *value)
68 {
69 int slot = parse_decorate_color_slot(var + ofs);
70 if (slot < 0)
71 return 0;
72 if (!value)
73 return config_error_nonbool(var);
74 color_parse(value, var, decoration_colors[slot]);
75 return 0;
76 }
78 /*
79 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
80 * for showing the commit sha1, use the same check for --decorate
81 */
82 #define decorate_get_color_opt(o, ix) \
83 decorate_get_color((o)->use_color, ix)
85 static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
86 {
87 int nlen = strlen(name);
88 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
89 memcpy(res->name, name, nlen + 1);
90 res->type = type;
91 res->next = add_decoration(&name_decoration, obj, res);
92 }
94 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
95 {
96 struct object *obj;
97 enum decoration_type type = DECORATION_NONE;
99 if (!prefixcmp(refname, "refs/replace/")) {
100 unsigned char original_sha1[20];
101 if (!read_replace_refs)
102 return 0;
103 if (get_sha1_hex(refname + 13, original_sha1)) {
104 warning("invalid replace ref %s", refname);
105 return 0;
106 }
107 obj = parse_object(original_sha1);
108 if (obj)
109 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
110 return 0;
111 }
113 obj = parse_object(sha1);
114 if (!obj)
115 return 0;
117 if (!prefixcmp(refname, "refs/heads/"))
118 type = DECORATION_REF_LOCAL;
119 else if (!prefixcmp(refname, "refs/remotes/"))
120 type = DECORATION_REF_REMOTE;
121 else if (!prefixcmp(refname, "refs/tags/"))
122 type = DECORATION_REF_TAG;
123 else if (!prefixcmp(refname, "refs/stash"))
124 type = DECORATION_REF_STASH;
125 else if (!prefixcmp(refname, "HEAD"))
126 type = DECORATION_REF_HEAD;
128 if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
129 refname = prettify_refname(refname);
130 add_name_decoration(type, refname, obj);
131 while (obj->type == OBJ_TAG) {
132 obj = ((struct tag *)obj)->tagged;
133 if (!obj)
134 break;
135 add_name_decoration(DECORATION_REF_TAG, refname, obj);
136 }
137 return 0;
138 }
140 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
141 {
142 struct commit *commit = lookup_commit(graft->sha1);
143 if (!commit)
144 return 0;
145 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
146 return 0;
147 }
149 void load_ref_decorations(int flags)
150 {
151 static int loaded;
152 if (!loaded) {
153 loaded = 1;
154 for_each_ref(add_ref_decoration, &flags);
155 head_ref(add_ref_decoration, &flags);
156 for_each_commit_graft(add_graft_decoration, NULL);
157 }
158 }
160 static void show_parents(struct commit *commit, int abbrev)
161 {
162 struct commit_list *p;
163 for (p = commit->parents; p ; p = p->next) {
164 struct commit *parent = p->item;
165 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
166 }
167 }
169 void show_decorations(struct rev_info *opt, struct commit *commit)
170 {
171 const char *prefix;
172 struct name_decoration *decoration;
173 const char *color_commit =
174 diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
175 const char *color_reset =
176 decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
178 if (opt->show_source && commit->util)
179 printf("\t%s", (char *) commit->util);
180 if (!opt->show_decorations)
181 return;
182 decoration = lookup_decoration(&name_decoration, &commit->object);
183 if (!decoration)
184 return;
185 prefix = " (";
186 while (decoration) {
187 printf("%s", prefix);
188 fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
189 stdout);
190 if (decoration->type == DECORATION_REF_TAG)
191 fputs("tag: ", stdout);
192 printf("%s", decoration->name);
193 fputs(color_reset, stdout);
194 fputs(color_commit, stdout);
195 prefix = ", ";
196 decoration = decoration->next;
197 }
198 putchar(')');
199 }
201 /*
202 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
203 * Signed-off-by: and Acked-by: lines.
204 */
205 static int detect_any_signoff(char *letter, int size)
206 {
207 char *cp;
208 int seen_colon = 0;
209 int seen_at = 0;
210 int seen_name = 0;
211 int seen_head = 0;
213 cp = letter + size;
214 while (letter <= --cp && *cp == '\n')
215 continue;
217 while (letter <= cp) {
218 char ch = *cp--;
219 if (ch == '\n')
220 break;
222 if (!seen_at) {
223 if (ch == '@')
224 seen_at = 1;
225 continue;
226 }
227 if (!seen_colon) {
228 if (ch == '@')
229 return 0;
230 else if (ch == ':')
231 seen_colon = 1;
232 else
233 seen_name = 1;
234 continue;
235 }
236 if (('A' <= ch && ch <= 'Z') ||
237 ('a' <= ch && ch <= 'z') ||
238 ch == '-') {
239 seen_head = 1;
240 continue;
241 }
242 /* no empty last line doesn't match */
243 return 0;
244 }
245 return seen_head && seen_name;
246 }
248 static void append_signoff(struct strbuf *sb, const char *signoff)
249 {
250 static const char signed_off_by[] = "Signed-off-by: ";
251 size_t signoff_len = strlen(signoff);
252 int has_signoff = 0;
253 char *cp;
255 cp = sb->buf;
257 /* First see if we already have the sign-off by the signer */
258 while ((cp = strstr(cp, signed_off_by))) {
260 has_signoff = 1;
262 cp += strlen(signed_off_by);
263 if (cp + signoff_len >= sb->buf + sb->len)
264 break;
265 if (strncmp(cp, signoff, signoff_len))
266 continue;
267 if (!isspace(cp[signoff_len]))
268 continue;
269 /* we already have him */
270 return;
271 }
273 if (!has_signoff)
274 has_signoff = detect_any_signoff(sb->buf, sb->len);
276 if (!has_signoff)
277 strbuf_addch(sb, '\n');
279 strbuf_addstr(sb, signed_off_by);
280 strbuf_add(sb, signoff, signoff_len);
281 strbuf_addch(sb, '\n');
282 }
284 static unsigned int digits_in_number(unsigned int number)
285 {
286 unsigned int i = 10, result = 1;
287 while (i <= number) {
288 i *= 10;
289 result++;
290 }
291 return result;
292 }
294 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
295 struct strbuf *buf)
296 {
297 int suffix_len = strlen(suffix) + 1;
298 int start_len = buf->len;
300 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
301 if (commit) {
302 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
303 struct pretty_print_context ctx = {0};
304 ctx.date_mode = DATE_NORMAL;
306 format_commit_message(commit, "%f", buf, &ctx);
307 if (max_len < buf->len)
308 strbuf_setlen(buf, max_len);
309 strbuf_addstr(buf, suffix);
310 }
311 }
313 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
314 const char **subject_p,
315 const char **extra_headers_p,
316 int *need_8bit_cte_p)
317 {
318 const char *subject = NULL;
319 const char *extra_headers = opt->extra_headers;
320 const char *name = sha1_to_hex(commit->object.sha1);
322 *need_8bit_cte_p = 0; /* unknown */
323 if (opt->total > 0) {
324 static char buffer[64];
325 snprintf(buffer, sizeof(buffer),
326 "Subject: [%s%s%0*d/%d] ",
327 opt->subject_prefix,
328 *opt->subject_prefix ? " " : "",
329 digits_in_number(opt->total),
330 opt->nr, opt->total);
331 subject = buffer;
332 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
333 static char buffer[256];
334 snprintf(buffer, sizeof(buffer),
335 "Subject: [%s] ",
336 opt->subject_prefix);
337 subject = buffer;
338 } else {
339 subject = "Subject: ";
340 }
342 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
343 graph_show_oneline(opt->graph);
344 if (opt->message_id) {
345 printf("Message-Id: <%s>\n", opt->message_id);
346 graph_show_oneline(opt->graph);
347 }
348 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
349 int i, n;
350 n = opt->ref_message_ids->nr;
351 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
352 for (i = 0; i < n; i++)
353 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
354 opt->ref_message_ids->items[i].string);
355 graph_show_oneline(opt->graph);
356 }
357 if (opt->mime_boundary) {
358 static char subject_buffer[1024];
359 static char buffer[1024];
360 struct strbuf filename = STRBUF_INIT;
361 *need_8bit_cte_p = -1; /* NEVER */
362 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
363 "%s"
364 "MIME-Version: 1.0\n"
365 "Content-Type: multipart/mixed;"
366 " boundary=\"%s%s\"\n"
367 "\n"
368 "This is a multi-part message in MIME "
369 "format.\n"
370 "--%s%s\n"
371 "Content-Type: text/plain; "
372 "charset=UTF-8; format=fixed\n"
373 "Content-Transfer-Encoding: 8bit\n\n",
374 extra_headers ? extra_headers : "",
375 mime_boundary_leader, opt->mime_boundary,
376 mime_boundary_leader, opt->mime_boundary);
377 extra_headers = subject_buffer;
379 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
380 opt->patch_suffix, &filename);
381 snprintf(buffer, sizeof(buffer) - 1,
382 "\n--%s%s\n"
383 "Content-Type: text/x-patch;"
384 " name=\"%s\"\n"
385 "Content-Transfer-Encoding: 8bit\n"
386 "Content-Disposition: %s;"
387 " filename=\"%s\"\n\n",
388 mime_boundary_leader, opt->mime_boundary,
389 filename.buf,
390 opt->no_inline ? "attachment" : "inline",
391 filename.buf);
392 opt->diffopt.stat_sep = buffer;
393 strbuf_release(&filename);
394 }
395 *subject_p = subject;
396 *extra_headers_p = extra_headers;
397 }
399 static void show_signature(struct rev_info *opt, struct commit *commit)
400 {
401 struct strbuf payload = STRBUF_INIT;
402 struct strbuf signature = STRBUF_INIT;
403 struct strbuf gpg_output = STRBUF_INIT;
404 int status;
405 const char *color, *reset, *bol, *eol;
407 if (parse_signed_commit(commit->object.sha1, &payload, &signature) <= 0)
408 goto out;
410 status = verify_signed_buffer(payload.buf, payload.len,
411 signature.buf, signature.len,
412 &gpg_output);
413 if (status && !gpg_output.len)
414 strbuf_addstr(&gpg_output, "No signature\n");
416 color = diff_get_color_opt(&opt->diffopt,
417 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
418 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
420 bol = gpg_output.buf;
421 while (*bol) {
422 eol = strchrnul(bol, '\n');
423 printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
424 *eol ? "\n" : "");
425 bol = (*eol) ? (eol + 1) : eol;
426 }
428 out:
429 strbuf_release(&gpg_output);
430 strbuf_release(&payload);
431 strbuf_release(&signature);
432 }
434 void show_log(struct rev_info *opt)
435 {
436 struct strbuf msgbuf = STRBUF_INIT;
437 struct log_info *log = opt->loginfo;
438 struct commit *commit = log->commit, *parent = log->parent;
439 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
440 const char *extra_headers = opt->extra_headers;
441 struct pretty_print_context ctx = {0};
443 opt->loginfo = NULL;
444 ctx.show_notes = opt->show_notes;
445 if (!opt->verbose_header) {
446 graph_show_commit(opt->graph);
448 if (!opt->graph)
449 put_revision_mark(opt, commit);
450 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
451 if (opt->print_parents)
452 show_parents(commit, abbrev_commit);
453 show_decorations(opt, commit);
454 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
455 putchar('\n');
456 graph_show_remainder(opt->graph);
457 }
458 putchar(opt->diffopt.line_termination);
459 return;
460 }
462 /*
463 * If use_terminator is set, we already handled any record termination
464 * at the end of the last record.
465 * Otherwise, add a diffopt.line_termination character before all
466 * entries but the first. (IOW, as a separator between entries)
467 */
468 if (opt->shown_one && !opt->use_terminator) {
469 /*
470 * If entries are separated by a newline, the output
471 * should look human-readable. If the last entry ended
472 * with a newline, print the graph output before this
473 * newline. Otherwise it will end up as a completely blank
474 * line and will look like a gap in the graph.
475 *
476 * If the entry separator is not a newline, the output is
477 * primarily intended for programmatic consumption, and we
478 * never want the extra graph output before the entry
479 * separator.
480 */
481 if (opt->diffopt.line_termination == '\n' &&
482 !opt->missing_newline)
483 graph_show_padding(opt->graph);
484 putchar(opt->diffopt.line_termination);
485 }
486 opt->shown_one = 1;
488 /*
489 * If the history graph was requested,
490 * print the graph, up to this commit's line
491 */
492 graph_show_commit(opt->graph);
494 /*
495 * Print header line of header..
496 */
498 if (opt->commit_format == CMIT_FMT_EMAIL) {
499 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
500 &ctx.need_8bit_cte);
501 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
502 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
503 if (opt->commit_format != CMIT_FMT_ONELINE)
504 fputs("commit ", stdout);
506 if (!opt->graph)
507 put_revision_mark(opt, commit);
508 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
509 stdout);
510 if (opt->print_parents)
511 show_parents(commit, abbrev_commit);
512 if (parent)
513 printf(" (from %s)",
514 find_unique_abbrev(parent->object.sha1,
515 abbrev_commit));
516 show_decorations(opt, commit);
517 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
518 if (opt->commit_format == CMIT_FMT_ONELINE) {
519 putchar(' ');
520 } else {
521 putchar('\n');
522 graph_show_oneline(opt->graph);
523 }
524 if (opt->reflog_info) {
525 /*
526 * setup_revisions() ensures that opt->reflog_info
527 * and opt->graph cannot both be set,
528 * so we don't need to worry about printing the
529 * graph info here.
530 */
531 show_reflog_message(opt->reflog_info,
532 opt->commit_format == CMIT_FMT_ONELINE,
533 opt->date_mode_explicit ?
534 opt->date_mode :
535 DATE_NORMAL);
536 if (opt->commit_format == CMIT_FMT_ONELINE)
537 return;
538 }
539 }
541 if (opt->show_signature)
542 show_signature(opt, commit);
544 if (!commit->buffer)
545 return;
547 /*
548 * And then the pretty-printed message itself
549 */
550 if (ctx.need_8bit_cte >= 0)
551 ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
552 ctx.date_mode = opt->date_mode;
553 ctx.abbrev = opt->diffopt.abbrev;
554 ctx.after_subject = extra_headers;
555 ctx.preserve_subject = opt->preserve_subject;
556 ctx.reflog_info = opt->reflog_info;
557 ctx.fmt = opt->commit_format;
558 pretty_print_commit(&ctx, commit, &msgbuf);
560 if (opt->add_signoff)
561 append_signoff(&msgbuf, opt->add_signoff);
562 if (opt->show_log_size) {
563 printf("log size %i\n", (int)msgbuf.len);
564 graph_show_oneline(opt->graph);
565 }
567 /*
568 * Set opt->missing_newline if msgbuf doesn't
569 * end in a newline (including if it is empty)
570 */
571 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
572 opt->missing_newline = 1;
573 else
574 opt->missing_newline = 0;
576 if (opt->graph)
577 graph_show_commit_msg(opt->graph, &msgbuf);
578 else
579 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
580 if (opt->use_terminator) {
581 if (!opt->missing_newline)
582 graph_show_padding(opt->graph);
583 putchar('\n');
584 }
586 strbuf_release(&msgbuf);
587 }
589 int log_tree_diff_flush(struct rev_info *opt)
590 {
591 diffcore_std(&opt->diffopt);
593 if (diff_queue_is_empty()) {
594 int saved_fmt = opt->diffopt.output_format;
595 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
596 diff_flush(&opt->diffopt);
597 opt->diffopt.output_format = saved_fmt;
598 return 0;
599 }
601 if (opt->loginfo && !opt->no_commit_id) {
602 /* When showing a verbose header (i.e. log message),
603 * and not in --pretty=oneline format, we would want
604 * an extra newline between the end of log and the
605 * output for readability.
606 */
607 show_log(opt);
608 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
609 opt->verbose_header &&
610 opt->commit_format != CMIT_FMT_ONELINE) {
611 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
612 if ((pch & opt->diffopt.output_format) == pch)
613 printf("---");
614 if (opt->diffopt.output_prefix) {
615 struct strbuf *msg = NULL;
616 msg = opt->diffopt.output_prefix(&opt->diffopt,
617 opt->diffopt.output_prefix_data);
618 fwrite(msg->buf, msg->len, 1, stdout);
619 }
620 putchar('\n');
621 }
622 }
623 diff_flush(&opt->diffopt);
624 return 1;
625 }
627 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
628 {
629 unsigned const char *sha1 = commit->object.sha1;
631 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
632 return !opt->loginfo;
633 }
635 /*
636 * Show the diff of a commit.
637 *
638 * Return true if we printed any log info messages
639 */
640 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
641 {
642 int showed_log;
643 struct commit_list *parents;
644 unsigned const char *sha1 = commit->object.sha1;
646 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
647 return 0;
649 /* Root commit? */
650 parents = commit->parents;
651 if (!parents) {
652 if (opt->show_root_diff) {
653 diff_root_tree_sha1(sha1, "", &opt->diffopt);
654 log_tree_diff_flush(opt);
655 }
656 return !opt->loginfo;
657 }
659 /* More than one parent? */
660 if (parents && parents->next) {
661 if (opt->ignore_merges)
662 return 0;
663 else if (opt->combine_merges)
664 return do_diff_combined(opt, commit);
665 else if (opt->first_parent_only) {
666 /*
667 * Generate merge log entry only for the first
668 * parent, showing summary diff of the others
669 * we merged _in_.
670 */
671 diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
672 log_tree_diff_flush(opt);
673 return !opt->loginfo;
674 }
676 /* If we show individual diffs, show the parent info */
677 log->parent = parents->item;
678 }
680 showed_log = 0;
681 for (;;) {
682 struct commit *parent = parents->item;
684 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
685 log_tree_diff_flush(opt);
687 showed_log |= !opt->loginfo;
689 /* Set up the log info for the next parent, if any.. */
690 parents = parents->next;
691 if (!parents)
692 break;
693 log->parent = parents->item;
694 opt->loginfo = log;
695 }
696 return showed_log;
697 }
699 int log_tree_commit(struct rev_info *opt, struct commit *commit)
700 {
701 struct log_info log;
702 int shown;
704 log.commit = commit;
705 log.parent = NULL;
706 opt->loginfo = &log;
708 shown = log_tree_diff(opt, commit, &log);
709 if (!shown && opt->loginfo && opt->always_show_header) {
710 log.parent = NULL;
711 show_log(opt);
712 shown = 1;
713 }
714 opt->loginfo = NULL;
715 maybe_flush_or_die(stdout, "stdout");
716 return shown;
717 }