Merge branch 'ls/hint-rev-list-count' into maint
[git/mjg.git] / log-tree.c
blobc931615d9267fd774d886e5414e051a25895ffdf
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"
12 #include "sequencer.h"
13 #include "line-log.h"
15 static struct decoration name_decoration = { "object names" };
16 static int decoration_loaded;
17 static int decoration_flags;
19 static char decoration_colors[][COLOR_MAXLEN] = {
20 GIT_COLOR_RESET,
21 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
22 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
23 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
24 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
25 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
26 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
29 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
31 if (want_color(decorate_use_color))
32 return decoration_colors[ix];
33 return "";
36 static int parse_decorate_color_slot(const char *slot)
39 * We're comparing with 'ignore-case' on
40 * (because config.c sets them all tolower),
41 * but let's match the letters in the literal
42 * string values here with how they are
43 * documented in Documentation/config.txt, for
44 * consistency.
46 * We love being consistent, don't we?
48 if (!strcasecmp(slot, "branch"))
49 return DECORATION_REF_LOCAL;
50 if (!strcasecmp(slot, "remoteBranch"))
51 return DECORATION_REF_REMOTE;
52 if (!strcasecmp(slot, "tag"))
53 return DECORATION_REF_TAG;
54 if (!strcasecmp(slot, "stash"))
55 return DECORATION_REF_STASH;
56 if (!strcasecmp(slot, "HEAD"))
57 return DECORATION_REF_HEAD;
58 return -1;
61 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
63 int slot = parse_decorate_color_slot(slot_name);
64 if (slot < 0)
65 return 0;
66 if (!value)
67 return config_error_nonbool(var);
68 return color_parse(value, decoration_colors[slot]);
72 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
73 * for showing the commit sha1, use the same check for --decorate
75 #define decorate_get_color_opt(o, ix) \
76 decorate_get_color((o)->use_color, ix)
78 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
80 int nlen = strlen(name);
81 struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
82 memcpy(res->name, name, nlen + 1);
83 res->type = type;
84 res->next = add_decoration(&name_decoration, obj, res);
87 const struct name_decoration *get_name_decoration(const struct object *obj)
89 return lookup_decoration(&name_decoration, obj);
92 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
94 struct object *obj;
95 enum decoration_type type = DECORATION_NONE;
97 assert(cb_data == NULL);
99 if (starts_with(refname, "refs/replace/")) {
100 unsigned char original_sha1[20];
101 if (!check_replace_refs)
102 return 0;
103 if (get_sha1_hex(refname + 13, original_sha1)) {
104 warning("invalid replace ref %s", refname);
105 return 0;
107 obj = parse_object(original_sha1);
108 if (obj)
109 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
110 return 0;
113 obj = parse_object(sha1);
114 if (!obj)
115 return 0;
117 if (starts_with(refname, "refs/heads/"))
118 type = DECORATION_REF_LOCAL;
119 else if (starts_with(refname, "refs/remotes/"))
120 type = DECORATION_REF_REMOTE;
121 else if (starts_with(refname, "refs/tags/"))
122 type = DECORATION_REF_TAG;
123 else if (!strcmp(refname, "refs/stash"))
124 type = DECORATION_REF_STASH;
125 else if (!strcmp(refname, "HEAD"))
126 type = DECORATION_REF_HEAD;
128 add_name_decoration(type, refname, obj);
129 while (obj->type == OBJ_TAG) {
130 obj = ((struct tag *)obj)->tagged;
131 if (!obj)
132 break;
133 if (!obj->parsed)
134 parse_object(obj->sha1);
135 add_name_decoration(DECORATION_REF_TAG, refname, obj);
137 return 0;
140 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
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;
149 void load_ref_decorations(int flags)
151 if (!decoration_loaded) {
152 decoration_loaded = 1;
153 decoration_flags = flags;
154 for_each_ref(add_ref_decoration, NULL);
155 head_ref(add_ref_decoration, NULL);
156 for_each_commit_graft(add_graft_decoration, NULL);
160 static void show_parents(struct commit *commit, int abbrev)
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));
169 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
171 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
172 for ( ; p; p = p->next) {
173 printf(" %s", find_unique_abbrev(p->item->object.sha1, abbrev));
178 * Do we have HEAD in the output, and also the branch it points at?
179 * If so, find that decoration entry for that current branch.
181 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
183 const struct name_decoration *list, *head = NULL;
184 const char *branch_name = NULL;
185 unsigned char unused[20];
186 int rru_flags;
188 /* First find HEAD */
189 for (list = decoration; list; list = list->next)
190 if (list->type == DECORATION_REF_HEAD) {
191 head = list;
192 break;
194 if (!head)
195 return NULL;
197 /* Now resolve and find the matching current branch */
198 branch_name = resolve_ref_unsafe("HEAD", 0, unused, &rru_flags);
199 if (!(rru_flags & REF_ISSYMREF))
200 return NULL;
202 if (!starts_with(branch_name, "refs/"))
203 return NULL;
205 /* OK, do we have that ref in the list? */
206 for (list = decoration; list; list = list->next)
207 if ((list->type == DECORATION_REF_LOCAL) &&
208 !strcmp(branch_name, list->name)) {
209 return list;
212 return NULL;
215 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
217 if (decoration_flags == DECORATE_SHORT_REFS)
218 strbuf_addstr(sb, prettify_refname(decoration->name));
219 else
220 strbuf_addstr(sb, decoration->name);
224 * The caller makes sure there is no funny color before calling.
225 * format_decorations_extended makes sure the same after return.
227 void format_decorations_extended(struct strbuf *sb,
228 const struct commit *commit,
229 int use_color,
230 const char *prefix,
231 const char *separator,
232 const char *suffix)
234 const struct name_decoration *decoration;
235 const struct name_decoration *current_and_HEAD;
236 const char *color_commit =
237 diff_get_color(use_color, DIFF_COMMIT);
238 const char *color_reset =
239 decorate_get_color(use_color, DECORATION_NONE);
241 decoration = get_name_decoration(&commit->object);
242 if (!decoration)
243 return;
245 current_and_HEAD = current_pointed_by_HEAD(decoration);
246 while (decoration) {
248 * When both current and HEAD are there, only
249 * show HEAD->current where HEAD would have
250 * appeared, skipping the entry for current.
252 if (decoration != current_and_HEAD) {
253 strbuf_addstr(sb, color_commit);
254 strbuf_addstr(sb, prefix);
255 strbuf_addstr(sb, color_reset);
256 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
257 if (decoration->type == DECORATION_REF_TAG)
258 strbuf_addstr(sb, "tag: ");
260 show_name(sb, decoration);
262 if (current_and_HEAD &&
263 decoration->type == DECORATION_REF_HEAD) {
264 strbuf_addstr(sb, color_reset);
265 strbuf_addstr(sb, color_commit);
266 strbuf_addstr(sb, " -> ");
267 strbuf_addstr(sb, color_reset);
268 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
269 show_name(sb, current_and_HEAD);
271 strbuf_addstr(sb, color_reset);
273 prefix = separator;
275 decoration = decoration->next;
277 strbuf_addstr(sb, color_commit);
278 strbuf_addstr(sb, suffix);
279 strbuf_addstr(sb, color_reset);
282 void show_decorations(struct rev_info *opt, struct commit *commit)
284 struct strbuf sb = STRBUF_INIT;
286 if (opt->show_source && commit->util)
287 printf("\t%s", (char *) commit->util);
288 if (!opt->show_decorations)
289 return;
290 format_decorations(&sb, commit, opt->diffopt.use_color);
291 fputs(sb.buf, stdout);
292 strbuf_release(&sb);
295 static unsigned int digits_in_number(unsigned int number)
297 unsigned int i = 10, result = 1;
298 while (i <= number) {
299 i *= 10;
300 result++;
302 return result;
305 void fmt_output_subject(struct strbuf *filename,
306 const char *subject,
307 struct rev_info *info)
309 const char *suffix = info->patch_suffix;
310 int nr = info->nr;
311 int start_len = filename->len;
312 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
314 if (0 < info->reroll_count)
315 strbuf_addf(filename, "v%d-", info->reroll_count);
316 strbuf_addf(filename, "%04d-%s", nr, subject);
318 if (max_len < filename->len)
319 strbuf_setlen(filename, max_len);
320 strbuf_addstr(filename, suffix);
323 void fmt_output_commit(struct strbuf *filename,
324 struct commit *commit,
325 struct rev_info *info)
327 struct pretty_print_context ctx = {0};
328 struct strbuf subject = STRBUF_INIT;
330 format_commit_message(commit, "%f", &subject, &ctx);
331 fmt_output_subject(filename, subject.buf, info);
332 strbuf_release(&subject);
335 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
336 const char **subject_p,
337 const char **extra_headers_p,
338 int *need_8bit_cte_p)
340 const char *subject = NULL;
341 const char *extra_headers = opt->extra_headers;
342 const char *name = sha1_to_hex(commit->object.sha1);
344 *need_8bit_cte_p = 0; /* unknown */
345 if (opt->total > 0) {
346 static char buffer[64];
347 snprintf(buffer, sizeof(buffer),
348 "Subject: [%s%s%0*d/%d] ",
349 opt->subject_prefix,
350 *opt->subject_prefix ? " " : "",
351 digits_in_number(opt->total),
352 opt->nr, opt->total);
353 subject = buffer;
354 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
355 static char buffer[256];
356 snprintf(buffer, sizeof(buffer),
357 "Subject: [%s] ",
358 opt->subject_prefix);
359 subject = buffer;
360 } else {
361 subject = "Subject: ";
364 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
365 graph_show_oneline(opt->graph);
366 if (opt->message_id) {
367 printf("Message-Id: <%s>\n", opt->message_id);
368 graph_show_oneline(opt->graph);
370 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
371 int i, n;
372 n = opt->ref_message_ids->nr;
373 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
374 for (i = 0; i < n; i++)
375 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
376 opt->ref_message_ids->items[i].string);
377 graph_show_oneline(opt->graph);
379 if (opt->mime_boundary) {
380 static char subject_buffer[1024];
381 static char buffer[1024];
382 struct strbuf filename = STRBUF_INIT;
383 *need_8bit_cte_p = -1; /* NEVER */
384 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
385 "%s"
386 "MIME-Version: 1.0\n"
387 "Content-Type: multipart/mixed;"
388 " boundary=\"%s%s\"\n"
389 "\n"
390 "This is a multi-part message in MIME "
391 "format.\n"
392 "--%s%s\n"
393 "Content-Type: text/plain; "
394 "charset=UTF-8; format=fixed\n"
395 "Content-Transfer-Encoding: 8bit\n\n",
396 extra_headers ? extra_headers : "",
397 mime_boundary_leader, opt->mime_boundary,
398 mime_boundary_leader, opt->mime_boundary);
399 extra_headers = subject_buffer;
401 if (opt->numbered_files)
402 strbuf_addf(&filename, "%d", opt->nr);
403 else
404 fmt_output_commit(&filename, commit, opt);
405 snprintf(buffer, sizeof(buffer) - 1,
406 "\n--%s%s\n"
407 "Content-Type: text/x-patch;"
408 " name=\"%s\"\n"
409 "Content-Transfer-Encoding: 8bit\n"
410 "Content-Disposition: %s;"
411 " filename=\"%s\"\n\n",
412 mime_boundary_leader, opt->mime_boundary,
413 filename.buf,
414 opt->no_inline ? "attachment" : "inline",
415 filename.buf);
416 opt->diffopt.stat_sep = buffer;
417 strbuf_release(&filename);
419 *subject_p = subject;
420 *extra_headers_p = extra_headers;
423 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
425 const char *color, *reset, *eol;
427 color = diff_get_color_opt(&opt->diffopt,
428 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
429 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
430 while (*bol) {
431 eol = strchrnul(bol, '\n');
432 printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
433 *eol ? "\n" : "");
434 graph_show_oneline(opt->graph);
435 bol = (*eol) ? (eol + 1) : eol;
439 static void show_signature(struct rev_info *opt, struct commit *commit)
441 struct strbuf payload = STRBUF_INIT;
442 struct strbuf signature = STRBUF_INIT;
443 struct strbuf gpg_output = STRBUF_INIT;
444 int status;
446 if (parse_signed_commit(commit, &payload, &signature) <= 0)
447 goto out;
449 status = verify_signed_buffer(payload.buf, payload.len,
450 signature.buf, signature.len,
451 &gpg_output, NULL);
452 if (status && !gpg_output.len)
453 strbuf_addstr(&gpg_output, "No signature\n");
455 show_sig_lines(opt, status, gpg_output.buf);
457 out:
458 strbuf_release(&gpg_output);
459 strbuf_release(&payload);
460 strbuf_release(&signature);
463 static int which_parent(const unsigned char *sha1, const struct commit *commit)
465 int nth;
466 const struct commit_list *parent;
468 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
469 if (!hashcmp(parent->item->object.sha1, sha1))
470 return nth;
471 nth++;
473 return -1;
476 static int is_common_merge(const struct commit *commit)
478 return (commit->parents
479 && commit->parents->next
480 && !commit->parents->next->next);
483 static void show_one_mergetag(struct commit *commit,
484 struct commit_extra_header *extra,
485 void *data)
487 struct rev_info *opt = (struct rev_info *)data;
488 unsigned char sha1[20];
489 struct tag *tag;
490 struct strbuf verify_message;
491 int status, nth;
492 size_t payload_size, gpg_message_offset;
494 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
495 tag = lookup_tag(sha1);
496 if (!tag)
497 return; /* error message already given */
499 strbuf_init(&verify_message, 256);
500 if (parse_tag_buffer(tag, extra->value, extra->len))
501 strbuf_addstr(&verify_message, "malformed mergetag\n");
502 else if (is_common_merge(commit) &&
503 !hashcmp(tag->tagged->sha1,
504 commit->parents->next->item->object.sha1))
505 strbuf_addf(&verify_message,
506 "merged tag '%s'\n", tag->tag);
507 else if ((nth = which_parent(tag->tagged->sha1, commit)) < 0)
508 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
509 tag->tag, tag->tagged->sha1);
510 else
511 strbuf_addf(&verify_message,
512 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
513 gpg_message_offset = verify_message.len;
515 payload_size = parse_signature(extra->value, extra->len);
516 status = -1;
517 if (extra->len > payload_size) {
518 /* could have a good signature */
519 if (!verify_signed_buffer(extra->value, payload_size,
520 extra->value + payload_size,
521 extra->len - payload_size,
522 &verify_message, NULL))
523 status = 0; /* good */
524 else if (verify_message.len <= gpg_message_offset)
525 strbuf_addstr(&verify_message, "No signature\n");
526 /* otherwise we couldn't verify, which is shown as bad */
529 show_sig_lines(opt, status, verify_message.buf);
530 strbuf_release(&verify_message);
533 static void show_mergetag(struct rev_info *opt, struct commit *commit)
535 for_each_mergetag(show_one_mergetag, commit, opt);
538 void show_log(struct rev_info *opt)
540 struct strbuf msgbuf = STRBUF_INIT;
541 struct log_info *log = opt->loginfo;
542 struct commit *commit = log->commit, *parent = log->parent;
543 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
544 const char *extra_headers = opt->extra_headers;
545 struct pretty_print_context ctx = {0};
547 opt->loginfo = NULL;
548 if (!opt->verbose_header) {
549 graph_show_commit(opt->graph);
551 if (!opt->graph)
552 put_revision_mark(opt, commit);
553 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
554 if (opt->print_parents)
555 show_parents(commit, abbrev_commit);
556 if (opt->children.name)
557 show_children(opt, commit, abbrev_commit);
558 show_decorations(opt, commit);
559 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
560 putchar('\n');
561 graph_show_remainder(opt->graph);
563 putchar(opt->diffopt.line_termination);
564 return;
568 * If use_terminator is set, we already handled any record termination
569 * at the end of the last record.
570 * Otherwise, add a diffopt.line_termination character before all
571 * entries but the first. (IOW, as a separator between entries)
573 if (opt->shown_one && !opt->use_terminator) {
575 * If entries are separated by a newline, the output
576 * should look human-readable. If the last entry ended
577 * with a newline, print the graph output before this
578 * newline. Otherwise it will end up as a completely blank
579 * line and will look like a gap in the graph.
581 * If the entry separator is not a newline, the output is
582 * primarily intended for programmatic consumption, and we
583 * never want the extra graph output before the entry
584 * separator.
586 if (opt->diffopt.line_termination == '\n' &&
587 !opt->missing_newline)
588 graph_show_padding(opt->graph);
589 putchar(opt->diffopt.line_termination);
591 opt->shown_one = 1;
594 * If the history graph was requested,
595 * print the graph, up to this commit's line
597 graph_show_commit(opt->graph);
600 * Print header line of header..
603 if (opt->commit_format == CMIT_FMT_EMAIL) {
604 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
605 &ctx.need_8bit_cte);
606 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
607 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
608 if (opt->commit_format != CMIT_FMT_ONELINE)
609 fputs("commit ", stdout);
611 if (!opt->graph)
612 put_revision_mark(opt, commit);
613 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
614 stdout);
615 if (opt->print_parents)
616 show_parents(commit, abbrev_commit);
617 if (opt->children.name)
618 show_children(opt, commit, abbrev_commit);
619 if (parent)
620 printf(" (from %s)",
621 find_unique_abbrev(parent->object.sha1,
622 abbrev_commit));
623 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
624 show_decorations(opt, commit);
625 if (opt->commit_format == CMIT_FMT_ONELINE) {
626 putchar(' ');
627 } else {
628 putchar('\n');
629 graph_show_oneline(opt->graph);
631 if (opt->reflog_info) {
633 * setup_revisions() ensures that opt->reflog_info
634 * and opt->graph cannot both be set,
635 * so we don't need to worry about printing the
636 * graph info here.
638 show_reflog_message(opt->reflog_info,
639 opt->commit_format == CMIT_FMT_ONELINE,
640 opt->date_mode,
641 opt->date_mode_explicit);
642 if (opt->commit_format == CMIT_FMT_ONELINE)
643 return;
647 if (opt->show_signature) {
648 show_signature(opt, commit);
649 show_mergetag(opt, commit);
652 if (!get_cached_commit_buffer(commit, NULL))
653 return;
655 if (opt->show_notes) {
656 int raw;
657 struct strbuf notebuf = STRBUF_INIT;
659 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
660 format_display_notes(commit->object.sha1, &notebuf,
661 get_log_output_encoding(), raw);
662 ctx.notes_message = notebuf.len
663 ? strbuf_detach(&notebuf, NULL)
664 : xcalloc(1, 1);
668 * And then the pretty-printed message itself
670 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
671 ctx.need_8bit_cte =
672 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
673 getenv("GIT_COMMITTER_EMAIL")));
674 ctx.date_mode = opt->date_mode;
675 ctx.date_mode_explicit = opt->date_mode_explicit;
676 ctx.abbrev = opt->diffopt.abbrev;
677 ctx.after_subject = extra_headers;
678 ctx.preserve_subject = opt->preserve_subject;
679 ctx.reflog_info = opt->reflog_info;
680 ctx.fmt = opt->commit_format;
681 ctx.mailmap = opt->mailmap;
682 ctx.color = opt->diffopt.use_color;
683 ctx.output_encoding = get_log_output_encoding();
684 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
685 ctx.from_ident = &opt->from_ident;
686 pretty_print_commit(&ctx, commit, &msgbuf);
688 if (opt->add_signoff)
689 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
691 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
692 ctx.notes_message && *ctx.notes_message) {
693 if (ctx.fmt == CMIT_FMT_EMAIL) {
694 strbuf_addstr(&msgbuf, "---\n");
695 opt->shown_dashes = 1;
697 strbuf_addstr(&msgbuf, ctx.notes_message);
700 if (opt->show_log_size) {
701 printf("log size %i\n", (int)msgbuf.len);
702 graph_show_oneline(opt->graph);
706 * Set opt->missing_newline if msgbuf doesn't
707 * end in a newline (including if it is empty)
709 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
710 opt->missing_newline = 1;
711 else
712 opt->missing_newline = 0;
714 if (opt->graph)
715 graph_show_commit_msg(opt->graph, &msgbuf);
716 else
717 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
718 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
719 if (!opt->missing_newline)
720 graph_show_padding(opt->graph);
721 putchar(opt->diffopt.line_termination);
724 strbuf_release(&msgbuf);
725 free(ctx.notes_message);
728 int log_tree_diff_flush(struct rev_info *opt)
730 opt->shown_dashes = 0;
731 diffcore_std(&opt->diffopt);
733 if (diff_queue_is_empty()) {
734 int saved_fmt = opt->diffopt.output_format;
735 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
736 diff_flush(&opt->diffopt);
737 opt->diffopt.output_format = saved_fmt;
738 return 0;
741 if (opt->loginfo && !opt->no_commit_id) {
742 show_log(opt);
743 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
744 opt->verbose_header &&
745 opt->commit_format != CMIT_FMT_ONELINE &&
746 !commit_format_is_empty(opt->commit_format)) {
748 * When showing a verbose header (i.e. log message),
749 * and not in --pretty=oneline format, we would want
750 * an extra newline between the end of log and the
751 * diff/diffstat output for readability.
753 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
754 if (opt->diffopt.output_prefix) {
755 struct strbuf *msg = NULL;
756 msg = opt->diffopt.output_prefix(&opt->diffopt,
757 opt->diffopt.output_prefix_data);
758 fwrite(msg->buf, msg->len, 1, stdout);
762 * We may have shown three-dashes line early
763 * between notes and the log message, in which
764 * case we only want a blank line after the
765 * notes without (an extra) three-dashes line.
766 * Otherwise, we show the three-dashes line if
767 * we are showing the patch with diffstat, but
768 * in that case, there is no extra blank line
769 * after the three-dashes line.
771 if (!opt->shown_dashes &&
772 (pch & opt->diffopt.output_format) == pch)
773 printf("---");
774 putchar('\n');
777 diff_flush(&opt->diffopt);
778 return 1;
781 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
783 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
784 return !opt->loginfo;
788 * Show the diff of a commit.
790 * Return true if we printed any log info messages
792 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
794 int showed_log;
795 struct commit_list *parents;
796 unsigned const char *sha1;
798 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
799 return 0;
801 parse_commit_or_die(commit);
802 sha1 = commit->tree->object.sha1;
804 /* Root commit? */
805 parents = get_saved_parents(opt, commit);
806 if (!parents) {
807 if (opt->show_root_diff) {
808 diff_root_tree_sha1(sha1, "", &opt->diffopt);
809 log_tree_diff_flush(opt);
811 return !opt->loginfo;
814 /* More than one parent? */
815 if (parents && parents->next) {
816 if (opt->ignore_merges)
817 return 0;
818 else if (opt->combine_merges)
819 return do_diff_combined(opt, commit);
820 else if (opt->first_parent_only) {
822 * Generate merge log entry only for the first
823 * parent, showing summary diff of the others
824 * we merged _in_.
826 parse_commit_or_die(parents->item);
827 diff_tree_sha1(parents->item->tree->object.sha1,
828 sha1, "", &opt->diffopt);
829 log_tree_diff_flush(opt);
830 return !opt->loginfo;
833 /* If we show individual diffs, show the parent info */
834 log->parent = parents->item;
837 showed_log = 0;
838 for (;;) {
839 struct commit *parent = parents->item;
841 parse_commit_or_die(parent);
842 diff_tree_sha1(parent->tree->object.sha1,
843 sha1, "", &opt->diffopt);
844 log_tree_diff_flush(opt);
846 showed_log |= !opt->loginfo;
848 /* Set up the log info for the next parent, if any.. */
849 parents = parents->next;
850 if (!parents)
851 break;
852 log->parent = parents->item;
853 opt->loginfo = log;
855 return showed_log;
858 int log_tree_commit(struct rev_info *opt, struct commit *commit)
860 struct log_info log;
861 int shown;
863 log.commit = commit;
864 log.parent = NULL;
865 opt->loginfo = &log;
867 if (opt->line_level_traverse)
868 return line_log_print(opt, commit);
870 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
871 printf("\n%s\n", opt->break_bar);
872 shown = log_tree_diff(opt, commit, &log);
873 if (!shown && opt->loginfo && opt->always_show_header) {
874 log.parent = NULL;
875 show_log(opt);
876 shown = 1;
878 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
879 printf("\n%s\n", opt->break_bar);
880 opt->loginfo = NULL;
881 maybe_flush_or_die(stdout, "stdout");
882 return shown;