4 #include "object-store.h"
5 #include "repository.h"
10 #include "reflog-walk.h"
12 #include "string-list.h"
14 #include "gpg-interface.h"
15 #include "sequencer.h"
18 #include "interdiff.h"
19 #include "range-diff.h"
21 static struct decoration name_decoration = { "object names" };
22 static int decoration_loaded;
23 static int decoration_flags;
25 static char decoration_colors[][COLOR_MAXLEN] = {
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 */
35 static const char *color_decorate_slots[] = {
36 [DECORATION_REF_LOCAL] = "branch",
37 [DECORATION_REF_REMOTE] = "remoteBranch",
38 [DECORATION_REF_TAG] = "tag",
39 [DECORATION_REF_STASH] = "stash",
40 [DECORATION_REF_HEAD] = "HEAD",
41 [DECORATION_GRAFTED] = "grafted",
44 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
46 if (want_color(decorate_use_color))
47 return decoration_colors[ix];
51 define_list_config_array(color_decorate_slots);
53 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
55 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
59 return config_error_nonbool(var);
60 return color_parse(value, decoration_colors[slot]);
64 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
65 * for showing the commit sha1, use the same check for --decorate
67 #define decorate_get_color_opt(o, ix) \
68 decorate_get_color((o)->use_color, ix)
70 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
72 struct name_decoration *res;
73 FLEX_ALLOC_STR(res, name, name);
75 res->next = add_decoration(&name_decoration, obj, res);
78 const struct name_decoration *get_name_decoration(const struct object *obj)
80 return lookup_decoration(&name_decoration, obj);
83 static int add_ref_decoration(const char *refname, const struct object_id *oid,
84 int flags, void *cb_data)
87 enum decoration_type type = DECORATION_NONE;
88 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
90 if (filter && !ref_filter_match(refname,
91 filter->include_ref_pattern,
92 filter->exclude_ref_pattern))
95 if (starts_with(refname, git_replace_ref_base)) {
96 struct object_id original_oid;
97 if (!read_replace_refs)
99 if (get_oid_hex(refname + strlen(git_replace_ref_base),
101 warning("invalid replace ref %s", refname);
104 obj = parse_object(the_repository, &original_oid);
106 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
110 obj = parse_object(the_repository, oid);
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 add_name_decoration(type, refname, obj);
126 while (obj->type == OBJ_TAG) {
127 obj = ((struct tag *)obj)->tagged;
131 parse_object(the_repository, &obj->oid);
132 add_name_decoration(DECORATION_REF_TAG, refname, obj);
137 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
139 struct commit *commit = lookup_commit(the_repository, &graft->oid);
142 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
146 void load_ref_decorations(struct decoration_filter *filter, int flags)
148 if (!decoration_loaded) {
150 struct string_list_item *item;
151 for_each_string_list_item(item, filter->exclude_ref_pattern) {
152 normalize_glob_ref(item, NULL, item->string);
154 for_each_string_list_item(item, filter->include_ref_pattern) {
155 normalize_glob_ref(item, NULL, item->string);
158 decoration_loaded = 1;
159 decoration_flags = flags;
160 for_each_ref(add_ref_decoration, filter);
161 head_ref(add_ref_decoration, filter);
162 for_each_commit_graft(add_graft_decoration, filter);
166 static void show_parents(struct commit *commit, int abbrev, FILE *file)
168 struct commit_list *p;
169 for (p = commit->parents; p ; p = p->next) {
170 struct commit *parent = p->item;
171 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
175 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
177 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
178 for ( ; p; p = p->next) {
179 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
184 * Do we have HEAD in the output, and also the branch it points at?
185 * If so, find that decoration entry for that current branch.
187 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
189 const struct name_decoration *list, *head = NULL;
190 const char *branch_name = NULL;
193 /* First find HEAD */
194 for (list = decoration; list; list = list->next)
195 if (list->type == DECORATION_REF_HEAD) {
202 /* Now resolve and find the matching current branch */
203 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
204 if (!branch_name || !(rru_flags & REF_ISSYMREF))
207 if (!starts_with(branch_name, "refs/"))
210 /* OK, do we have that ref in the list? */
211 for (list = decoration; list; list = list->next)
212 if ((list->type == DECORATION_REF_LOCAL) &&
213 !strcmp(branch_name, list->name)) {
220 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
222 if (decoration_flags == DECORATE_SHORT_REFS)
223 strbuf_addstr(sb, prettify_refname(decoration->name));
225 strbuf_addstr(sb, decoration->name);
229 * The caller makes sure there is no funny color before calling.
230 * format_decorations_extended makes sure the same after return.
232 void format_decorations_extended(struct strbuf *sb,
233 const struct commit *commit,
236 const char *separator,
239 const struct name_decoration *decoration;
240 const struct name_decoration *current_and_HEAD;
241 const char *color_commit =
242 diff_get_color(use_color, DIFF_COMMIT);
243 const char *color_reset =
244 decorate_get_color(use_color, DECORATION_NONE);
246 decoration = get_name_decoration(&commit->object);
250 current_and_HEAD = current_pointed_by_HEAD(decoration);
253 * When both current and HEAD are there, only
254 * show HEAD->current where HEAD would have
255 * appeared, skipping the entry for current.
257 if (decoration != current_and_HEAD) {
258 strbuf_addstr(sb, color_commit);
259 strbuf_addstr(sb, prefix);
260 strbuf_addstr(sb, color_reset);
261 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
262 if (decoration->type == DECORATION_REF_TAG)
263 strbuf_addstr(sb, "tag: ");
265 show_name(sb, decoration);
267 if (current_and_HEAD &&
268 decoration->type == DECORATION_REF_HEAD) {
269 strbuf_addstr(sb, " -> ");
270 strbuf_addstr(sb, color_reset);
271 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
272 show_name(sb, current_and_HEAD);
274 strbuf_addstr(sb, color_reset);
278 decoration = decoration->next;
280 strbuf_addstr(sb, color_commit);
281 strbuf_addstr(sb, suffix);
282 strbuf_addstr(sb, color_reset);
285 void show_decorations(struct rev_info *opt, struct commit *commit)
287 struct strbuf sb = STRBUF_INIT;
290 char **slot = revision_sources_peek(opt->sources, commit);
293 fprintf(opt->diffopt.file, "\t%s", *slot);
295 if (!opt->show_decorations)
297 format_decorations(&sb, commit, opt->diffopt.use_color);
298 fputs(sb.buf, opt->diffopt.file);
302 static unsigned int digits_in_number(unsigned int number)
304 unsigned int i = 10, result = 1;
305 while (i <= number) {
312 void fmt_output_subject(struct strbuf *filename,
314 struct rev_info *info)
316 const char *suffix = info->patch_suffix;
318 int start_len = filename->len;
319 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
321 if (0 < info->reroll_count)
322 strbuf_addf(filename, "v%d-", info->reroll_count);
323 strbuf_addf(filename, "%04d-%s", nr, subject);
325 if (max_len < filename->len)
326 strbuf_setlen(filename, max_len);
327 strbuf_addstr(filename, suffix);
330 void fmt_output_commit(struct strbuf *filename,
331 struct commit *commit,
332 struct rev_info *info)
334 struct pretty_print_context ctx = {0};
335 struct strbuf subject = STRBUF_INIT;
337 format_commit_message(commit, "%f", &subject, &ctx);
338 fmt_output_subject(filename, subject.buf, info);
339 strbuf_release(&subject);
342 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
344 if (opt->total > 0) {
345 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
347 *opt->subject_prefix ? " " : "",
348 digits_in_number(opt->total),
349 opt->nr, opt->total);
350 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
351 strbuf_addf(sb, "Subject: [%s] ",
352 opt->subject_prefix);
354 strbuf_addstr(sb, "Subject: ");
358 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
359 const char **extra_headers_p,
360 int *need_8bit_cte_p,
363 const char *extra_headers = opt->extra_headers;
364 const char *name = oid_to_hex(opt->zero_commit ?
365 &null_oid : &commit->object.oid);
367 *need_8bit_cte_p = 0; /* unknown */
369 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
370 graph_show_oneline(opt->graph);
371 if (opt->message_id) {
372 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
373 graph_show_oneline(opt->graph);
375 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
377 n = opt->ref_message_ids->nr;
378 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
379 for (i = 0; i < n; i++)
380 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
381 opt->ref_message_ids->items[i].string);
382 graph_show_oneline(opt->graph);
384 if (opt->mime_boundary && maybe_multipart) {
385 static struct strbuf subject_buffer = STRBUF_INIT;
386 static struct strbuf buffer = STRBUF_INIT;
387 struct strbuf filename = STRBUF_INIT;
388 *need_8bit_cte_p = -1; /* NEVER */
390 strbuf_reset(&subject_buffer);
391 strbuf_reset(&buffer);
393 strbuf_addf(&subject_buffer,
395 "MIME-Version: 1.0\n"
396 "Content-Type: multipart/mixed;"
397 " boundary=\"%s%s\"\n"
399 "This is a multi-part message in MIME "
402 "Content-Type: text/plain; "
403 "charset=UTF-8; format=fixed\n"
404 "Content-Transfer-Encoding: 8bit\n\n",
405 extra_headers ? extra_headers : "",
406 mime_boundary_leader, opt->mime_boundary,
407 mime_boundary_leader, opt->mime_boundary);
408 extra_headers = subject_buffer.buf;
410 if (opt->numbered_files)
411 strbuf_addf(&filename, "%d", opt->nr);
413 fmt_output_commit(&filename, commit, opt);
416 "Content-Type: text/x-patch;"
418 "Content-Transfer-Encoding: 8bit\n"
419 "Content-Disposition: %s;"
420 " filename=\"%s\"\n\n",
421 mime_boundary_leader, opt->mime_boundary,
423 opt->no_inline ? "attachment" : "inline",
425 opt->diffopt.stat_sep = buffer.buf;
426 strbuf_release(&filename);
428 *extra_headers_p = extra_headers;
431 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
433 const char *color, *reset, *eol;
435 color = diff_get_color_opt(&opt->diffopt,
436 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
437 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
439 eol = strchrnul(bol, '\n');
440 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
442 graph_show_oneline(opt->graph);
443 bol = (*eol) ? (eol + 1) : eol;
447 static void show_signature(struct rev_info *opt, struct commit *commit)
449 struct strbuf payload = STRBUF_INIT;
450 struct strbuf signature = STRBUF_INIT;
451 struct strbuf gpg_output = STRBUF_INIT;
454 if (parse_signed_commit(commit, &payload, &signature) <= 0)
457 status = verify_signed_buffer(payload.buf, payload.len,
458 signature.buf, signature.len,
460 if (status && !gpg_output.len)
461 strbuf_addstr(&gpg_output, "No signature\n");
463 show_sig_lines(opt, status, gpg_output.buf);
466 strbuf_release(&gpg_output);
467 strbuf_release(&payload);
468 strbuf_release(&signature);
471 static int which_parent(const struct object_id *oid, const struct commit *commit)
474 const struct commit_list *parent;
476 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
477 if (oideq(&parent->item->object.oid, oid))
484 static int is_common_merge(const struct commit *commit)
486 return (commit->parents
487 && commit->parents->next
488 && !commit->parents->next->next);
491 static int show_one_mergetag(struct commit *commit,
492 struct commit_extra_header *extra,
495 struct rev_info *opt = (struct rev_info *)data;
496 struct object_id oid;
498 struct strbuf verify_message;
500 size_t payload_size, gpg_message_offset;
502 hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
503 tag = lookup_tag(the_repository, &oid);
505 return -1; /* error message already given */
507 strbuf_init(&verify_message, 256);
508 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
509 strbuf_addstr(&verify_message, "malformed mergetag\n");
510 else if (is_common_merge(commit) &&
511 oideq(&tag->tagged->oid,
512 &commit->parents->next->item->object.oid))
513 strbuf_addf(&verify_message,
514 "merged tag '%s'\n", tag->tag);
515 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
516 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
517 tag->tag, tag->tagged->oid.hash);
519 strbuf_addf(&verify_message,
520 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
521 gpg_message_offset = verify_message.len;
523 payload_size = parse_signature(extra->value, extra->len);
525 if (extra->len > payload_size) {
526 /* could have a good signature */
527 if (!verify_signed_buffer(extra->value, payload_size,
528 extra->value + payload_size,
529 extra->len - payload_size,
530 &verify_message, NULL))
531 status = 0; /* good */
532 else if (verify_message.len <= gpg_message_offset)
533 strbuf_addstr(&verify_message, "No signature\n");
534 /* otherwise we couldn't verify, which is shown as bad */
537 show_sig_lines(opt, status, verify_message.buf);
538 strbuf_release(&verify_message);
542 static int show_mergetag(struct rev_info *opt, struct commit *commit)
544 return for_each_mergetag(show_one_mergetag, commit, opt);
547 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
549 const char *x = opt->shown_dashes ? "\n" : "---\n";
551 strbuf_addstr(sb, x);
553 fputs(x, opt->diffopt.file);
554 opt->shown_dashes = 1;
557 void show_log(struct rev_info *opt)
559 struct strbuf msgbuf = STRBUF_INIT;
560 struct log_info *log = opt->loginfo;
561 struct commit *commit = log->commit, *parent = log->parent;
562 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
563 const char *extra_headers = opt->extra_headers;
564 struct pretty_print_context ctx = {0};
567 if (!opt->verbose_header) {
568 graph_show_commit(opt->graph);
571 put_revision_mark(opt, commit);
572 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
573 if (opt->print_parents)
574 show_parents(commit, abbrev_commit, opt->diffopt.file);
575 if (opt->children.name)
576 show_children(opt, commit, abbrev_commit);
577 show_decorations(opt, commit);
578 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
579 putc('\n', opt->diffopt.file);
580 graph_show_remainder(opt->graph);
582 putc(opt->diffopt.line_termination, opt->diffopt.file);
587 * If use_terminator is set, we already handled any record termination
588 * at the end of the last record.
589 * Otherwise, add a diffopt.line_termination character before all
590 * entries but the first. (IOW, as a separator between entries)
592 if (opt->shown_one && !opt->use_terminator) {
594 * If entries are separated by a newline, the output
595 * should look human-readable. If the last entry ended
596 * with a newline, print the graph output before this
597 * newline. Otherwise it will end up as a completely blank
598 * line and will look like a gap in the graph.
600 * If the entry separator is not a newline, the output is
601 * primarily intended for programmatic consumption, and we
602 * never want the extra graph output before the entry
605 if (opt->diffopt.line_termination == '\n' &&
606 !opt->missing_newline)
607 graph_show_padding(opt->graph);
608 putc(opt->diffopt.line_termination, opt->diffopt.file);
613 * If the history graph was requested,
614 * print the graph, up to this commit's line
616 graph_show_commit(opt->graph);
619 * Print header line of header..
622 if (cmit_fmt_is_mail(opt->commit_format)) {
623 log_write_email_headers(opt, commit, &extra_headers,
624 &ctx.need_8bit_cte, 1);
626 ctx.print_email_subject = 1;
627 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
628 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
629 if (opt->commit_format != CMIT_FMT_ONELINE)
630 fputs("commit ", opt->diffopt.file);
633 put_revision_mark(opt, commit);
634 fputs(find_unique_abbrev(&commit->object.oid,
637 if (opt->print_parents)
638 show_parents(commit, abbrev_commit, opt->diffopt.file);
639 if (opt->children.name)
640 show_children(opt, commit, abbrev_commit);
642 fprintf(opt->diffopt.file, " (from %s)",
643 find_unique_abbrev(&parent->object.oid, abbrev_commit));
644 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
645 show_decorations(opt, commit);
646 if (opt->commit_format == CMIT_FMT_ONELINE) {
647 putc(' ', opt->diffopt.file);
649 putc('\n', opt->diffopt.file);
650 graph_show_oneline(opt->graph);
652 if (opt->reflog_info) {
654 * setup_revisions() ensures that opt->reflog_info
655 * and opt->graph cannot both be set,
656 * so we don't need to worry about printing the
659 show_reflog_message(opt->reflog_info,
660 opt->commit_format == CMIT_FMT_ONELINE,
662 opt->date_mode_explicit);
663 if (opt->commit_format == CMIT_FMT_ONELINE)
668 if (opt->show_signature) {
669 show_signature(opt, commit);
670 show_mergetag(opt, commit);
673 if (opt->show_notes) {
675 struct strbuf notebuf = STRBUF_INIT;
677 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
678 format_display_notes(&commit->object.oid, ¬ebuf,
679 get_log_output_encoding(), raw);
680 ctx.notes_message = notebuf.len
681 ? strbuf_detach(¬ebuf, NULL)
686 * And then the pretty-printed message itself
688 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
690 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
691 getenv("GIT_COMMITTER_EMAIL")));
692 ctx.date_mode = opt->date_mode;
693 ctx.date_mode_explicit = opt->date_mode_explicit;
694 ctx.abbrev = opt->diffopt.abbrev;
695 ctx.after_subject = extra_headers;
696 ctx.preserve_subject = opt->preserve_subject;
697 ctx.reflog_info = opt->reflog_info;
698 ctx.fmt = opt->commit_format;
699 ctx.mailmap = opt->mailmap;
700 ctx.color = opt->diffopt.use_color;
701 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
702 ctx.output_encoding = get_log_output_encoding();
703 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
704 ctx.from_ident = &opt->from_ident;
706 ctx.graph_width = graph_width(opt->graph);
707 pretty_print_commit(&ctx, commit, &msgbuf);
709 if (opt->add_signoff)
710 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
712 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
713 ctx.notes_message && *ctx.notes_message) {
714 if (cmit_fmt_is_mail(ctx.fmt))
715 next_commentary_block(opt, &msgbuf);
716 strbuf_addstr(&msgbuf, ctx.notes_message);
719 if (opt->show_log_size) {
720 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
721 graph_show_oneline(opt->graph);
725 * Set opt->missing_newline if msgbuf doesn't
726 * end in a newline (including if it is empty)
728 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
729 opt->missing_newline = 1;
731 opt->missing_newline = 0;
733 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
734 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
735 if (!opt->missing_newline)
736 graph_show_padding(opt->graph);
737 putc(opt->diffopt.line_termination, opt->diffopt.file);
740 strbuf_release(&msgbuf);
741 free(ctx.notes_message);
743 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
744 struct diff_queue_struct dq;
746 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
747 DIFF_QUEUE_CLEAR(&diff_queued_diff);
749 next_commentary_block(opt, NULL);
750 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
751 show_interdiff(opt, 2);
753 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
756 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
757 struct diff_queue_struct dq;
759 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
760 DIFF_QUEUE_CLEAR(&diff_queued_diff);
762 next_commentary_block(opt, NULL);
763 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
764 show_range_diff(opt->rdiff1, opt->rdiff2,
765 opt->creation_factor, 1, &opt->diffopt);
767 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
771 int log_tree_diff_flush(struct rev_info *opt)
773 opt->shown_dashes = 0;
774 diffcore_std(&opt->diffopt);
776 if (diff_queue_is_empty()) {
777 int saved_fmt = opt->diffopt.output_format;
778 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
779 diff_flush(&opt->diffopt);
780 opt->diffopt.output_format = saved_fmt;
784 if (opt->loginfo && !opt->no_commit_id) {
786 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
787 opt->verbose_header &&
788 opt->commit_format != CMIT_FMT_ONELINE &&
789 !commit_format_is_empty(opt->commit_format)) {
791 * When showing a verbose header (i.e. log message),
792 * and not in --pretty=oneline format, we would want
793 * an extra newline between the end of log and the
794 * diff/diffstat output for readability.
796 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
797 if (opt->diffopt.output_prefix) {
798 struct strbuf *msg = NULL;
799 msg = opt->diffopt.output_prefix(&opt->diffopt,
800 opt->diffopt.output_prefix_data);
801 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
805 * We may have shown three-dashes line early
806 * between generated commentary (notes, etc.)
807 * and the log message, in which case we only
808 * want a blank line after the commentary
809 * without (an extra) three-dashes line.
810 * Otherwise, we show the three-dashes line if
811 * we are showing the patch with diffstat, but
812 * in that case, there is no extra blank line
813 * after the three-dashes line.
815 if (!opt->shown_dashes &&
816 (pch & opt->diffopt.output_format) == pch)
817 fprintf(opt->diffopt.file, "---");
818 putc('\n', opt->diffopt.file);
821 diff_flush(&opt->diffopt);
825 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
827 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
828 return !opt->loginfo;
832 * Show the diff of a commit.
834 * Return true if we printed any log info messages
836 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
839 struct commit_list *parents;
840 struct object_id *oid;
842 if (!opt->diff && !opt->diffopt.flags.exit_with_status)
845 parse_commit_or_die(commit);
846 oid = get_commit_tree_oid(commit);
849 parents = get_saved_parents(opt, commit);
851 if (opt->show_root_diff) {
852 diff_root_tree_oid(oid, "", &opt->diffopt);
853 log_tree_diff_flush(opt);
855 return !opt->loginfo;
858 /* More than one parent? */
859 if (parents && parents->next) {
860 if (opt->ignore_merges)
862 else if (opt->combine_merges)
863 return do_diff_combined(opt, commit);
864 else if (opt->first_parent_only) {
866 * Generate merge log entry only for the first
867 * parent, showing summary diff of the others
870 parse_commit_or_die(parents->item);
871 diff_tree_oid(get_commit_tree_oid(parents->item),
872 oid, "", &opt->diffopt);
873 log_tree_diff_flush(opt);
874 return !opt->loginfo;
877 /* If we show individual diffs, show the parent info */
878 log->parent = parents->item;
883 struct commit *parent = parents->item;
885 parse_commit_or_die(parent);
886 diff_tree_oid(get_commit_tree_oid(parent),
887 oid, "", &opt->diffopt);
888 log_tree_diff_flush(opt);
890 showed_log |= !opt->loginfo;
892 /* Set up the log info for the next parent, if any.. */
893 parents = parents->next;
896 log->parent = parents->item;
902 int log_tree_commit(struct rev_info *opt, struct commit *commit)
905 int shown, close_file = opt->diffopt.close_file;
910 opt->diffopt.close_file = 0;
912 if (opt->line_level_traverse)
913 return line_log_print(opt, commit);
915 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
916 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
917 shown = log_tree_diff(opt, commit, &log);
918 if (!shown && opt->loginfo && opt->always_show_header) {
923 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
924 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
926 maybe_flush_or_die(opt->diffopt.file, "stdout");
928 fclose(opt->diffopt.file);