7 #include "reflog-walk.h"
9 #include "string-list.h"
11 #include "gpg-interface.h"
12 #include "sequencer.h"
15 static struct decoration name_decoration = { "object names" };
17 static char decoration_colors[][COLOR_MAXLEN] = {
19 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
20 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
21 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
22 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
23 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
24 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
27 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
29 if (want_color(decorate_use_color))
30 return decoration_colors[ix];
34 static int parse_decorate_color_slot(const char *slot)
37 * We're comparing with 'ignore-case' on
38 * (because config.c sets them all tolower),
39 * but let's match the letters in the literal
40 * string values here with how they are
41 * documented in Documentation/config.txt, for
44 * We love being consistent, don't we?
46 if (!strcasecmp(slot, "branch"))
47 return DECORATION_REF_LOCAL;
48 if (!strcasecmp(slot, "remoteBranch"))
49 return DECORATION_REF_REMOTE;
50 if (!strcasecmp(slot, "tag"))
51 return DECORATION_REF_TAG;
52 if (!strcasecmp(slot, "stash"))
53 return DECORATION_REF_STASH;
54 if (!strcasecmp(slot, "HEAD"))
55 return DECORATION_REF_HEAD;
59 int parse_decorate_color_config(const char *var, const int ofs, const char *value)
61 int slot = parse_decorate_color_slot(var + ofs);
65 return config_error_nonbool(var);
66 color_parse(value, var, decoration_colors[slot]);
71 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
72 * for showing the commit sha1, use the same check for --decorate
74 #define decorate_get_color_opt(o, ix) \
75 decorate_get_color((o)->use_color, ix)
77 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
79 int nlen = strlen(name);
80 struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
81 memcpy(res->name, name, nlen + 1);
83 res->next = add_decoration(&name_decoration, obj, res);
86 const struct name_decoration *get_name_decoration(const struct object *obj)
88 return lookup_decoration(&name_decoration, obj);
91 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
94 enum decoration_type type = DECORATION_NONE;
96 if (starts_with(refname, "refs/replace/")) {
97 unsigned char original_sha1[20];
98 if (!check_replace_refs)
100 if (get_sha1_hex(refname + 13, original_sha1)) {
101 warning("invalid replace ref %s", refname);
104 obj = parse_object(original_sha1);
106 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
110 obj = parse_object(sha1);
114 if (starts_with(refname, "refs/heads/"))
115 type = DECORATION_REF_LOCAL;
116 else if (starts_with(refname, "refs/remotes/"))
117 type = DECORATION_REF_REMOTE;
118 else if (starts_with(refname, "refs/tags/"))
119 type = DECORATION_REF_TAG;
120 else if (!strcmp(refname, "refs/stash"))
121 type = DECORATION_REF_STASH;
122 else if (!strcmp(refname, "HEAD"))
123 type = DECORATION_REF_HEAD;
125 if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
126 refname = prettify_refname(refname);
127 add_name_decoration(type, refname, obj);
128 while (obj->type == OBJ_TAG) {
129 obj = ((struct tag *)obj)->tagged;
133 parse_object(obj->sha1);
134 add_name_decoration(DECORATION_REF_TAG, refname, obj);
139 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
141 struct commit *commit = lookup_commit(graft->sha1);
144 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
148 void load_ref_decorations(int flags)
153 for_each_ref(add_ref_decoration, &flags);
154 head_ref(add_ref_decoration, &flags);
155 for_each_commit_graft(add_graft_decoration, NULL);
159 static void show_parents(struct commit *commit, int abbrev)
161 struct commit_list *p;
162 for (p = commit->parents; p ; p = p->next) {
163 struct commit *parent = p->item;
164 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
168 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
170 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
171 for ( ; p; p = p->next) {
172 printf(" %s", find_unique_abbrev(p->item->object.sha1, abbrev));
177 * The caller makes sure there is no funny color before calling.
178 * format_decorations_extended makes sure the same after return.
180 void format_decorations_extended(struct strbuf *sb,
181 const struct commit *commit,
184 const char *separator,
187 const struct name_decoration *decoration;
188 const char *color_commit =
189 diff_get_color(use_color, DIFF_COMMIT);
190 const char *color_reset =
191 decorate_get_color(use_color, DECORATION_NONE);
193 decoration = get_name_decoration(&commit->object);
197 strbuf_addstr(sb, color_commit);
198 strbuf_addstr(sb, prefix);
199 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
200 if (decoration->type == DECORATION_REF_TAG)
201 strbuf_addstr(sb, "tag: ");
202 strbuf_addstr(sb, decoration->name);
203 strbuf_addstr(sb, color_reset);
205 decoration = decoration->next;
207 strbuf_addstr(sb, color_commit);
208 strbuf_addstr(sb, suffix);
209 strbuf_addstr(sb, color_reset);
212 void show_decorations(struct rev_info *opt, struct commit *commit)
214 struct strbuf sb = STRBUF_INIT;
216 if (opt->show_source && commit->util)
217 printf("\t%s", (char *) commit->util);
218 if (!opt->show_decorations)
220 format_decorations(&sb, commit, opt->diffopt.use_color);
221 fputs(sb.buf, stdout);
225 static unsigned int digits_in_number(unsigned int number)
227 unsigned int i = 10, result = 1;
228 while (i <= number) {
235 void fmt_output_subject(struct strbuf *filename,
237 struct rev_info *info)
239 const char *suffix = info->patch_suffix;
241 int start_len = filename->len;
242 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
244 if (0 < info->reroll_count)
245 strbuf_addf(filename, "v%d-", info->reroll_count);
246 strbuf_addf(filename, "%04d-%s", nr, subject);
248 if (max_len < filename->len)
249 strbuf_setlen(filename, max_len);
250 strbuf_addstr(filename, suffix);
253 void fmt_output_commit(struct strbuf *filename,
254 struct commit *commit,
255 struct rev_info *info)
257 struct pretty_print_context ctx = {0};
258 struct strbuf subject = STRBUF_INIT;
260 format_commit_message(commit, "%f", &subject, &ctx);
261 fmt_output_subject(filename, subject.buf, info);
262 strbuf_release(&subject);
265 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
266 const char **subject_p,
267 const char **extra_headers_p,
268 int *need_8bit_cte_p)
270 const char *subject = NULL;
271 const char *extra_headers = opt->extra_headers;
272 const char *name = sha1_to_hex(commit->object.sha1);
274 *need_8bit_cte_p = 0; /* unknown */
275 if (opt->total > 0) {
276 static char buffer[64];
277 snprintf(buffer, sizeof(buffer),
278 "Subject: [%s%s%0*d/%d] ",
280 *opt->subject_prefix ? " " : "",
281 digits_in_number(opt->total),
282 opt->nr, opt->total);
284 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
285 static char buffer[256];
286 snprintf(buffer, sizeof(buffer),
288 opt->subject_prefix);
291 subject = "Subject: ";
294 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
295 graph_show_oneline(opt->graph);
296 if (opt->message_id) {
297 printf("Message-Id: <%s>\n", opt->message_id);
298 graph_show_oneline(opt->graph);
300 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
302 n = opt->ref_message_ids->nr;
303 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
304 for (i = 0; i < n; i++)
305 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
306 opt->ref_message_ids->items[i].string);
307 graph_show_oneline(opt->graph);
309 if (opt->mime_boundary) {
310 static char subject_buffer[1024];
311 static char buffer[1024];
312 struct strbuf filename = STRBUF_INIT;
313 *need_8bit_cte_p = -1; /* NEVER */
314 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
316 "MIME-Version: 1.0\n"
317 "Content-Type: multipart/mixed;"
318 " boundary=\"%s%s\"\n"
320 "This is a multi-part message in MIME "
323 "Content-Type: text/plain; "
324 "charset=UTF-8; format=fixed\n"
325 "Content-Transfer-Encoding: 8bit\n\n",
326 extra_headers ? extra_headers : "",
327 mime_boundary_leader, opt->mime_boundary,
328 mime_boundary_leader, opt->mime_boundary);
329 extra_headers = subject_buffer;
331 if (opt->numbered_files)
332 strbuf_addf(&filename, "%d", opt->nr);
334 fmt_output_commit(&filename, commit, opt);
335 snprintf(buffer, sizeof(buffer) - 1,
337 "Content-Type: text/x-patch;"
339 "Content-Transfer-Encoding: 8bit\n"
340 "Content-Disposition: %s;"
341 " filename=\"%s\"\n\n",
342 mime_boundary_leader, opt->mime_boundary,
344 opt->no_inline ? "attachment" : "inline",
346 opt->diffopt.stat_sep = buffer;
347 strbuf_release(&filename);
349 *subject_p = subject;
350 *extra_headers_p = extra_headers;
353 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
355 const char *color, *reset, *eol;
357 color = diff_get_color_opt(&opt->diffopt,
358 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
359 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
361 eol = strchrnul(bol, '\n');
362 printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
364 graph_show_oneline(opt->graph);
365 bol = (*eol) ? (eol + 1) : eol;
369 static void show_signature(struct rev_info *opt, struct commit *commit)
371 struct strbuf payload = STRBUF_INIT;
372 struct strbuf signature = STRBUF_INIT;
373 struct strbuf gpg_output = STRBUF_INIT;
376 if (parse_signed_commit(commit, &payload, &signature) <= 0)
379 status = verify_signed_buffer(payload.buf, payload.len,
380 signature.buf, signature.len,
382 if (status && !gpg_output.len)
383 strbuf_addstr(&gpg_output, "No signature\n");
385 show_sig_lines(opt, status, gpg_output.buf);
388 strbuf_release(&gpg_output);
389 strbuf_release(&payload);
390 strbuf_release(&signature);
393 static int which_parent(const unsigned char *sha1, const struct commit *commit)
396 const struct commit_list *parent;
398 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
399 if (!hashcmp(parent->item->object.sha1, sha1))
406 static int is_common_merge(const struct commit *commit)
408 return (commit->parents
409 && commit->parents->next
410 && !commit->parents->next->next);
413 static void show_one_mergetag(struct commit *commit,
414 struct commit_extra_header *extra,
417 struct rev_info *opt = (struct rev_info *)data;
418 unsigned char sha1[20];
420 struct strbuf verify_message;
422 size_t payload_size, gpg_message_offset;
424 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
425 tag = lookup_tag(sha1);
427 return; /* error message already given */
429 strbuf_init(&verify_message, 256);
430 if (parse_tag_buffer(tag, extra->value, extra->len))
431 strbuf_addstr(&verify_message, "malformed mergetag\n");
432 else if (is_common_merge(commit) &&
433 !hashcmp(tag->tagged->sha1,
434 commit->parents->next->item->object.sha1))
435 strbuf_addf(&verify_message,
436 "merged tag '%s'\n", tag->tag);
437 else if ((nth = which_parent(tag->tagged->sha1, commit)) < 0)
438 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
439 tag->tag, tag->tagged->sha1);
441 strbuf_addf(&verify_message,
442 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
443 gpg_message_offset = verify_message.len;
445 payload_size = parse_signature(extra->value, extra->len);
447 if (extra->len > payload_size) {
448 /* could have a good signature */
449 if (!verify_signed_buffer(extra->value, payload_size,
450 extra->value + payload_size,
451 extra->len - payload_size,
452 &verify_message, NULL))
453 status = 0; /* good */
454 else if (verify_message.len <= gpg_message_offset)
455 strbuf_addstr(&verify_message, "No signature\n");
456 /* otherwise we couldn't verify, which is shown as bad */
459 show_sig_lines(opt, status, verify_message.buf);
460 strbuf_release(&verify_message);
463 static void show_mergetag(struct rev_info *opt, struct commit *commit)
465 for_each_mergetag(show_one_mergetag, commit, opt);
468 void show_log(struct rev_info *opt)
470 struct strbuf msgbuf = STRBUF_INIT;
471 struct log_info *log = opt->loginfo;
472 struct commit *commit = log->commit, *parent = log->parent;
473 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
474 const char *extra_headers = opt->extra_headers;
475 struct pretty_print_context ctx = {0};
478 if (!opt->verbose_header) {
479 graph_show_commit(opt->graph);
482 put_revision_mark(opt, commit);
483 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
484 if (opt->print_parents)
485 show_parents(commit, abbrev_commit);
486 if (opt->children.name)
487 show_children(opt, commit, abbrev_commit);
488 show_decorations(opt, commit);
489 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
491 graph_show_remainder(opt->graph);
493 putchar(opt->diffopt.line_termination);
498 * If use_terminator is set, we already handled any record termination
499 * at the end of the last record.
500 * Otherwise, add a diffopt.line_termination character before all
501 * entries but the first. (IOW, as a separator between entries)
503 if (opt->shown_one && !opt->use_terminator) {
505 * If entries are separated by a newline, the output
506 * should look human-readable. If the last entry ended
507 * with a newline, print the graph output before this
508 * newline. Otherwise it will end up as a completely blank
509 * line and will look like a gap in the graph.
511 * If the entry separator is not a newline, the output is
512 * primarily intended for programmatic consumption, and we
513 * never want the extra graph output before the entry
516 if (opt->diffopt.line_termination == '\n' &&
517 !opt->missing_newline)
518 graph_show_padding(opt->graph);
519 putchar(opt->diffopt.line_termination);
524 * If the history graph was requested,
525 * print the graph, up to this commit's line
527 graph_show_commit(opt->graph);
530 * Print header line of header..
533 if (opt->commit_format == CMIT_FMT_EMAIL) {
534 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
536 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
537 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
538 if (opt->commit_format != CMIT_FMT_ONELINE)
539 fputs("commit ", stdout);
542 put_revision_mark(opt, commit);
543 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
545 if (opt->print_parents)
546 show_parents(commit, abbrev_commit);
547 if (opt->children.name)
548 show_children(opt, commit, abbrev_commit);
551 find_unique_abbrev(parent->object.sha1,
553 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
554 show_decorations(opt, commit);
555 if (opt->commit_format == CMIT_FMT_ONELINE) {
559 graph_show_oneline(opt->graph);
561 if (opt->reflog_info) {
563 * setup_revisions() ensures that opt->reflog_info
564 * and opt->graph cannot both be set,
565 * so we don't need to worry about printing the
568 show_reflog_message(opt->reflog_info,
569 opt->commit_format == CMIT_FMT_ONELINE,
571 opt->date_mode_explicit);
572 if (opt->commit_format == CMIT_FMT_ONELINE)
577 if (opt->show_signature) {
578 show_signature(opt, commit);
579 show_mergetag(opt, commit);
582 if (!get_cached_commit_buffer(commit, NULL))
585 if (opt->show_notes) {
587 struct strbuf notebuf = STRBUF_INIT;
589 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
590 format_display_notes(commit->object.sha1, ¬ebuf,
591 get_log_output_encoding(), raw);
592 ctx.notes_message = notebuf.len
593 ? strbuf_detach(¬ebuf, NULL)
598 * And then the pretty-printed message itself
600 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
602 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
603 getenv("GIT_COMMITTER_EMAIL")));
604 ctx.date_mode = opt->date_mode;
605 ctx.date_mode_explicit = opt->date_mode_explicit;
606 ctx.abbrev = opt->diffopt.abbrev;
607 ctx.after_subject = extra_headers;
608 ctx.preserve_subject = opt->preserve_subject;
609 ctx.reflog_info = opt->reflog_info;
610 ctx.fmt = opt->commit_format;
611 ctx.mailmap = opt->mailmap;
612 ctx.color = opt->diffopt.use_color;
613 ctx.output_encoding = get_log_output_encoding();
614 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
615 ctx.from_ident = &opt->from_ident;
616 pretty_print_commit(&ctx, commit, &msgbuf);
618 if (opt->add_signoff)
619 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
621 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
622 ctx.notes_message && *ctx.notes_message) {
623 if (ctx.fmt == CMIT_FMT_EMAIL) {
624 strbuf_addstr(&msgbuf, "---\n");
625 opt->shown_dashes = 1;
627 strbuf_addstr(&msgbuf, ctx.notes_message);
630 if (opt->show_log_size) {
631 printf("log size %i\n", (int)msgbuf.len);
632 graph_show_oneline(opt->graph);
636 * Set opt->missing_newline if msgbuf doesn't
637 * end in a newline (including if it is empty)
639 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
640 opt->missing_newline = 1;
642 opt->missing_newline = 0;
645 graph_show_commit_msg(opt->graph, &msgbuf);
647 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
648 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
649 if (!opt->missing_newline)
650 graph_show_padding(opt->graph);
651 putchar(opt->diffopt.line_termination);
654 strbuf_release(&msgbuf);
655 free(ctx.notes_message);
658 int log_tree_diff_flush(struct rev_info *opt)
660 opt->shown_dashes = 0;
661 diffcore_std(&opt->diffopt);
663 if (diff_queue_is_empty()) {
664 int saved_fmt = opt->diffopt.output_format;
665 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
666 diff_flush(&opt->diffopt);
667 opt->diffopt.output_format = saved_fmt;
671 if (opt->loginfo && !opt->no_commit_id) {
673 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
674 opt->verbose_header &&
675 opt->commit_format != CMIT_FMT_ONELINE &&
676 !commit_format_is_empty(opt->commit_format)) {
678 * When showing a verbose header (i.e. log message),
679 * and not in --pretty=oneline format, we would want
680 * an extra newline between the end of log and the
681 * diff/diffstat output for readability.
683 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
684 if (opt->diffopt.output_prefix) {
685 struct strbuf *msg = NULL;
686 msg = opt->diffopt.output_prefix(&opt->diffopt,
687 opt->diffopt.output_prefix_data);
688 fwrite(msg->buf, msg->len, 1, stdout);
692 * We may have shown three-dashes line early
693 * between notes and the log message, in which
694 * case we only want a blank line after the
695 * notes without (an extra) three-dashes line.
696 * Otherwise, we show the three-dashes line if
697 * we are showing the patch with diffstat, but
698 * in that case, there is no extra blank line
699 * after the three-dashes line.
701 if (!opt->shown_dashes &&
702 (pch & opt->diffopt.output_format) == pch)
707 diff_flush(&opt->diffopt);
711 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
713 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
714 return !opt->loginfo;
718 * Show the diff of a commit.
720 * Return true if we printed any log info messages
722 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
725 struct commit_list *parents;
726 unsigned const char *sha1;
728 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
731 parse_commit_or_die(commit);
732 sha1 = commit->tree->object.sha1;
735 parents = get_saved_parents(opt, commit);
737 if (opt->show_root_diff) {
738 diff_root_tree_sha1(sha1, "", &opt->diffopt);
739 log_tree_diff_flush(opt);
741 return !opt->loginfo;
744 /* More than one parent? */
745 if (parents && parents->next) {
746 if (opt->ignore_merges)
748 else if (opt->combine_merges)
749 return do_diff_combined(opt, commit);
750 else if (opt->first_parent_only) {
752 * Generate merge log entry only for the first
753 * parent, showing summary diff of the others
756 parse_commit_or_die(parents->item);
757 diff_tree_sha1(parents->item->tree->object.sha1,
758 sha1, "", &opt->diffopt);
759 log_tree_diff_flush(opt);
760 return !opt->loginfo;
763 /* If we show individual diffs, show the parent info */
764 log->parent = parents->item;
769 struct commit *parent = parents->item;
771 parse_commit_or_die(parent);
772 diff_tree_sha1(parent->tree->object.sha1,
773 sha1, "", &opt->diffopt);
774 log_tree_diff_flush(opt);
776 showed_log |= !opt->loginfo;
778 /* Set up the log info for the next parent, if any.. */
779 parents = parents->next;
782 log->parent = parents->item;
788 int log_tree_commit(struct rev_info *opt, struct commit *commit)
797 if (opt->line_level_traverse)
798 return line_log_print(opt, commit);
800 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
801 printf("\n%s\n", opt->break_bar);
802 shown = log_tree_diff(opt, commit, &log);
803 if (!shown && opt->loginfo && opt->always_show_header) {
808 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
809 printf("\n%s\n", opt->break_bar);
811 maybe_flush_or_die(stdout, "stdout");