distinguish error versus short read from read_in_full()
[git.git] / log-tree.c
blob410ab4f02de5b532485c46b5cceffc3c677df214
1 #include "cache.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include "graph.h"
7 #include "log-tree.h"
8 #include "reflog-walk.h"
9 #include "refs.h"
10 #include "string-list.h"
11 #include "color.h"
12 #include "gpg-interface.h"
13 #include "sequencer.h"
14 #include "line-log.h"
16 static struct decoration name_decoration = { "object names" };
17 static int decoration_loaded;
18 static int decoration_flags;
20 static char decoration_colors[][COLOR_MAXLEN] = {
21 GIT_COLOR_RESET,
22 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
23 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
24 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
25 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
26 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
27 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
30 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
32 if (want_color(decorate_use_color))
33 return decoration_colors[ix];
34 return "";
37 static int parse_decorate_color_slot(const char *slot)
40 * We're comparing with 'ignore-case' on
41 * (because config.c sets them all tolower),
42 * but let's match the letters in the literal
43 * string values here with how they are
44 * documented in Documentation/config.txt, for
45 * consistency.
47 * We love being consistent, don't we?
49 if (!strcasecmp(slot, "branch"))
50 return DECORATION_REF_LOCAL;
51 if (!strcasecmp(slot, "remoteBranch"))
52 return DECORATION_REF_REMOTE;
53 if (!strcasecmp(slot, "tag"))
54 return DECORATION_REF_TAG;
55 if (!strcasecmp(slot, "stash"))
56 return DECORATION_REF_STASH;
57 if (!strcasecmp(slot, "HEAD"))
58 return DECORATION_REF_HEAD;
59 return -1;
62 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
64 int slot = parse_decorate_color_slot(slot_name);
65 if (slot < 0)
66 return 0;
67 if (!value)
68 return config_error_nonbool(var);
69 return color_parse(value, decoration_colors[slot]);
73 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
74 * for showing the commit sha1, use the same check for --decorate
76 #define decorate_get_color_opt(o, ix) \
77 decorate_get_color((o)->use_color, ix)
79 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
81 struct name_decoration *res;
82 FLEX_ALLOC_STR(res, name, name);
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 struct object_id *oid,
93 int flags, void *cb_data)
95 struct object *obj;
96 enum decoration_type type = DECORATION_NONE;
98 assert(cb_data == NULL);
100 if (starts_with(refname, git_replace_ref_base)) {
101 struct object_id original_oid;
102 if (!check_replace_refs)
103 return 0;
104 if (get_oid_hex(refname + strlen(git_replace_ref_base),
105 &original_oid)) {
106 warning("invalid replace ref %s", refname);
107 return 0;
109 obj = parse_object(&original_oid);
110 if (obj)
111 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
112 return 0;
115 obj = parse_object(oid);
116 if (!obj)
117 return 0;
119 if (starts_with(refname, "refs/heads/"))
120 type = DECORATION_REF_LOCAL;
121 else if (starts_with(refname, "refs/remotes/"))
122 type = DECORATION_REF_REMOTE;
123 else if (starts_with(refname, "refs/tags/"))
124 type = DECORATION_REF_TAG;
125 else if (!strcmp(refname, "refs/stash"))
126 type = DECORATION_REF_STASH;
127 else if (!strcmp(refname, "HEAD"))
128 type = DECORATION_REF_HEAD;
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 if (!obj->parsed)
136 parse_object(&obj->oid);
137 add_name_decoration(DECORATION_REF_TAG, refname, obj);
139 return 0;
142 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
144 struct commit *commit = lookup_commit(&graft->oid);
145 if (!commit)
146 return 0;
147 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
148 return 0;
151 void load_ref_decorations(int flags)
153 if (!decoration_loaded) {
155 decoration_loaded = 1;
156 decoration_flags = flags;
157 for_each_ref(add_ref_decoration, NULL);
158 head_ref(add_ref_decoration, NULL);
159 for_each_commit_graft(add_graft_decoration, NULL);
163 static void show_parents(struct commit *commit, int abbrev, FILE *file)
165 struct commit_list *p;
166 for (p = commit->parents; p ; p = p->next) {
167 struct commit *parent = p->item;
168 fprintf(file, " %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
172 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
174 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
175 for ( ; p; p = p->next) {
176 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
181 * Do we have HEAD in the output, and also the branch it points at?
182 * If so, find that decoration entry for that current branch.
184 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
186 const struct name_decoration *list, *head = NULL;
187 const char *branch_name = NULL;
188 struct object_id unused;
189 int rru_flags;
191 /* First find HEAD */
192 for (list = decoration; list; list = list->next)
193 if (list->type == DECORATION_REF_HEAD) {
194 head = list;
195 break;
197 if (!head)
198 return NULL;
200 /* Now resolve and find the matching current branch */
201 branch_name = resolve_ref_unsafe("HEAD", 0, unused.hash, &rru_flags);
202 if (!(rru_flags & REF_ISSYMREF))
203 return NULL;
205 if (!starts_with(branch_name, "refs/"))
206 return NULL;
208 /* OK, do we have that ref in the list? */
209 for (list = decoration; list; list = list->next)
210 if ((list->type == DECORATION_REF_LOCAL) &&
211 !strcmp(branch_name, list->name)) {
212 return list;
215 return NULL;
218 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
220 if (decoration_flags == DECORATE_SHORT_REFS)
221 strbuf_addstr(sb, prettify_refname(decoration->name));
222 else
223 strbuf_addstr(sb, decoration->name);
227 * The caller makes sure there is no funny color before calling.
228 * format_decorations_extended makes sure the same after return.
230 void format_decorations_extended(struct strbuf *sb,
231 const struct commit *commit,
232 int use_color,
233 const char *prefix,
234 const char *separator,
235 const char *suffix)
237 const struct name_decoration *decoration;
238 const struct name_decoration *current_and_HEAD;
239 const char *color_commit =
240 diff_get_color(use_color, DIFF_COMMIT);
241 const char *color_reset =
242 decorate_get_color(use_color, DECORATION_NONE);
244 decoration = get_name_decoration(&commit->object);
245 if (!decoration)
246 return;
248 current_and_HEAD = current_pointed_by_HEAD(decoration);
249 while (decoration) {
251 * When both current and HEAD are there, only
252 * show HEAD->current where HEAD would have
253 * appeared, skipping the entry for current.
255 if (decoration != current_and_HEAD) {
256 strbuf_addstr(sb, color_commit);
257 strbuf_addstr(sb, prefix);
258 strbuf_addstr(sb, color_reset);
259 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
260 if (decoration->type == DECORATION_REF_TAG)
261 strbuf_addstr(sb, "tag: ");
263 show_name(sb, decoration);
265 if (current_and_HEAD &&
266 decoration->type == DECORATION_REF_HEAD) {
267 strbuf_addstr(sb, " -> ");
268 strbuf_addstr(sb, color_reset);
269 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
270 show_name(sb, current_and_HEAD);
272 strbuf_addstr(sb, color_reset);
274 prefix = separator;
276 decoration = decoration->next;
278 strbuf_addstr(sb, color_commit);
279 strbuf_addstr(sb, suffix);
280 strbuf_addstr(sb, color_reset);
283 void show_decorations(struct rev_info *opt, struct commit *commit)
285 struct strbuf sb = STRBUF_INIT;
287 if (opt->show_source && commit->util)
288 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
289 if (!opt->show_decorations)
290 return;
291 format_decorations(&sb, commit, opt->diffopt.use_color);
292 fputs(sb.buf, opt->diffopt.file);
293 strbuf_release(&sb);
296 static unsigned int digits_in_number(unsigned int number)
298 unsigned int i = 10, result = 1;
299 while (i <= number) {
300 i *= 10;
301 result++;
303 return result;
306 void fmt_output_subject(struct strbuf *filename,
307 const char *subject,
308 struct rev_info *info)
310 const char *suffix = info->patch_suffix;
311 int nr = info->nr;
312 int start_len = filename->len;
313 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
315 if (0 < info->reroll_count)
316 strbuf_addf(filename, "v%d-", info->reroll_count);
317 strbuf_addf(filename, "%04d-%s", nr, subject);
319 if (max_len < filename->len)
320 strbuf_setlen(filename, max_len);
321 strbuf_addstr(filename, suffix);
324 void fmt_output_commit(struct strbuf *filename,
325 struct commit *commit,
326 struct rev_info *info)
328 struct pretty_print_context ctx = {0};
329 struct strbuf subject = STRBUF_INIT;
331 format_commit_message(commit, "%f", &subject, &ctx);
332 fmt_output_subject(filename, subject.buf, info);
333 strbuf_release(&subject);
336 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
338 if (opt->total > 0) {
339 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
340 opt->subject_prefix,
341 *opt->subject_prefix ? " " : "",
342 digits_in_number(opt->total),
343 opt->nr, opt->total);
344 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
345 strbuf_addf(sb, "Subject: [%s] ",
346 opt->subject_prefix);
347 } else {
348 strbuf_addstr(sb, "Subject: ");
352 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
353 const char **extra_headers_p,
354 int *need_8bit_cte_p)
356 const char *extra_headers = opt->extra_headers;
357 const char *name = oid_to_hex(opt->zero_commit ?
358 &null_oid : &commit->object.oid);
360 *need_8bit_cte_p = 0; /* unknown */
362 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
363 graph_show_oneline(opt->graph);
364 if (opt->message_id) {
365 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
366 graph_show_oneline(opt->graph);
368 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
369 int i, n;
370 n = opt->ref_message_ids->nr;
371 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
372 for (i = 0; i < n; i++)
373 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
374 opt->ref_message_ids->items[i].string);
375 graph_show_oneline(opt->graph);
377 if (opt->mime_boundary) {
378 static char subject_buffer[1024];
379 static char buffer[1024];
380 struct strbuf filename = STRBUF_INIT;
381 *need_8bit_cte_p = -1; /* NEVER */
382 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
383 "%s"
384 "MIME-Version: 1.0\n"
385 "Content-Type: multipart/mixed;"
386 " boundary=\"%s%s\"\n"
387 "\n"
388 "This is a multi-part message in MIME "
389 "format.\n"
390 "--%s%s\n"
391 "Content-Type: text/plain; "
392 "charset=UTF-8; format=fixed\n"
393 "Content-Transfer-Encoding: 8bit\n\n",
394 extra_headers ? extra_headers : "",
395 mime_boundary_leader, opt->mime_boundary,
396 mime_boundary_leader, opt->mime_boundary);
397 extra_headers = subject_buffer;
399 if (opt->numbered_files)
400 strbuf_addf(&filename, "%d", opt->nr);
401 else
402 fmt_output_commit(&filename, commit, opt);
403 snprintf(buffer, sizeof(buffer) - 1,
404 "\n--%s%s\n"
405 "Content-Type: text/x-patch;"
406 " name=\"%s\"\n"
407 "Content-Transfer-Encoding: 8bit\n"
408 "Content-Disposition: %s;"
409 " filename=\"%s\"\n\n",
410 mime_boundary_leader, opt->mime_boundary,
411 filename.buf,
412 opt->no_inline ? "attachment" : "inline",
413 filename.buf);
414 opt->diffopt.stat_sep = buffer;
415 strbuf_release(&filename);
417 *extra_headers_p = extra_headers;
420 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
422 const char *color, *reset, *eol;
424 color = diff_get_color_opt(&opt->diffopt,
425 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
426 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
427 while (*bol) {
428 eol = strchrnul(bol, '\n');
429 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
430 *eol ? "\n" : "");
431 graph_show_oneline(opt->graph);
432 bol = (*eol) ? (eol + 1) : eol;
436 static void show_signature(struct rev_info *opt, struct commit *commit)
438 struct strbuf payload = STRBUF_INIT;
439 struct strbuf signature = STRBUF_INIT;
440 struct strbuf gpg_output = STRBUF_INIT;
441 int status;
443 if (parse_signed_commit(commit, &payload, &signature) <= 0)
444 goto out;
446 status = verify_signed_buffer(payload.buf, payload.len,
447 signature.buf, signature.len,
448 &gpg_output, NULL);
449 if (status && !gpg_output.len)
450 strbuf_addstr(&gpg_output, "No signature\n");
452 show_sig_lines(opt, status, gpg_output.buf);
454 out:
455 strbuf_release(&gpg_output);
456 strbuf_release(&payload);
457 strbuf_release(&signature);
460 static int which_parent(const struct object_id *oid, const struct commit *commit)
462 int nth;
463 const struct commit_list *parent;
465 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
466 if (!oidcmp(&parent->item->object.oid, oid))
467 return nth;
468 nth++;
470 return -1;
473 static int is_common_merge(const struct commit *commit)
475 return (commit->parents
476 && commit->parents->next
477 && !commit->parents->next->next);
480 static void show_one_mergetag(struct commit *commit,
481 struct commit_extra_header *extra,
482 void *data)
484 struct rev_info *opt = (struct rev_info *)data;
485 struct object_id oid;
486 struct tag *tag;
487 struct strbuf verify_message;
488 int status, nth;
489 size_t payload_size, gpg_message_offset;
491 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), oid.hash);
492 tag = lookup_tag(&oid);
493 if (!tag)
494 return; /* error message already given */
496 strbuf_init(&verify_message, 256);
497 if (parse_tag_buffer(tag, extra->value, extra->len))
498 strbuf_addstr(&verify_message, "malformed mergetag\n");
499 else if (is_common_merge(commit) &&
500 !oidcmp(&tag->tagged->oid,
501 &commit->parents->next->item->object.oid))
502 strbuf_addf(&verify_message,
503 "merged tag '%s'\n", tag->tag);
504 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
505 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
506 tag->tag, tag->tagged->oid.hash);
507 else
508 strbuf_addf(&verify_message,
509 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
510 gpg_message_offset = verify_message.len;
512 payload_size = parse_signature(extra->value, extra->len);
513 status = -1;
514 if (extra->len > payload_size) {
515 /* could have a good signature */
516 if (!verify_signed_buffer(extra->value, payload_size,
517 extra->value + payload_size,
518 extra->len - payload_size,
519 &verify_message, NULL))
520 status = 0; /* good */
521 else if (verify_message.len <= gpg_message_offset)
522 strbuf_addstr(&verify_message, "No signature\n");
523 /* otherwise we couldn't verify, which is shown as bad */
526 show_sig_lines(opt, status, verify_message.buf);
527 strbuf_release(&verify_message);
530 static void show_mergetag(struct rev_info *opt, struct commit *commit)
532 for_each_mergetag(show_one_mergetag, commit, opt);
535 void show_log(struct rev_info *opt)
537 struct strbuf msgbuf = STRBUF_INIT;
538 struct log_info *log = opt->loginfo;
539 struct commit *commit = log->commit, *parent = log->parent;
540 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
541 const char *extra_headers = opt->extra_headers;
542 struct pretty_print_context ctx = {0};
544 opt->loginfo = NULL;
545 if (!opt->verbose_header) {
546 graph_show_commit(opt->graph);
548 if (!opt->graph)
549 put_revision_mark(opt, commit);
550 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
551 if (opt->print_parents)
552 show_parents(commit, abbrev_commit, opt->diffopt.file);
553 if (opt->children.name)
554 show_children(opt, commit, abbrev_commit);
555 show_decorations(opt, commit);
556 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
557 putc('\n', opt->diffopt.file);
558 graph_show_remainder(opt->graph);
560 putc(opt->diffopt.line_termination, opt->diffopt.file);
561 return;
565 * If use_terminator is set, we already handled any record termination
566 * at the end of the last record.
567 * Otherwise, add a diffopt.line_termination character before all
568 * entries but the first. (IOW, as a separator between entries)
570 if (opt->shown_one && !opt->use_terminator) {
572 * If entries are separated by a newline, the output
573 * should look human-readable. If the last entry ended
574 * with a newline, print the graph output before this
575 * newline. Otherwise it will end up as a completely blank
576 * line and will look like a gap in the graph.
578 * If the entry separator is not a newline, the output is
579 * primarily intended for programmatic consumption, and we
580 * never want the extra graph output before the entry
581 * separator.
583 if (opt->diffopt.line_termination == '\n' &&
584 !opt->missing_newline)
585 graph_show_padding(opt->graph);
586 putc(opt->diffopt.line_termination, opt->diffopt.file);
588 opt->shown_one = 1;
591 * If the history graph was requested,
592 * print the graph, up to this commit's line
594 graph_show_commit(opt->graph);
597 * Print header line of header..
600 if (cmit_fmt_is_mail(opt->commit_format)) {
601 log_write_email_headers(opt, commit, &extra_headers,
602 &ctx.need_8bit_cte);
603 ctx.rev = opt;
604 ctx.print_email_subject = 1;
605 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
606 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
607 if (opt->commit_format != CMIT_FMT_ONELINE)
608 fputs("commit ", opt->diffopt.file);
610 if (!opt->graph)
611 put_revision_mark(opt, commit);
612 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
613 opt->diffopt.file);
614 if (opt->print_parents)
615 show_parents(commit, abbrev_commit, opt->diffopt.file);
616 if (opt->children.name)
617 show_children(opt, commit, abbrev_commit);
618 if (parent)
619 fprintf(opt->diffopt.file, " (from %s)",
620 find_unique_abbrev(parent->object.oid.hash,
621 abbrev_commit));
622 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
623 show_decorations(opt, commit);
624 if (opt->commit_format == CMIT_FMT_ONELINE) {
625 putc(' ', opt->diffopt.file);
626 } else {
627 putc('\n', opt->diffopt.file);
628 graph_show_oneline(opt->graph);
630 if (opt->reflog_info) {
632 * setup_revisions() ensures that opt->reflog_info
633 * and opt->graph cannot both be set,
634 * so we don't need to worry about printing the
635 * graph info here.
637 show_reflog_message(opt->reflog_info,
638 opt->commit_format == CMIT_FMT_ONELINE,
639 &opt->date_mode,
640 opt->date_mode_explicit);
641 if (opt->commit_format == CMIT_FMT_ONELINE)
642 return;
646 if (opt->show_signature) {
647 show_signature(opt, commit);
648 show_mergetag(opt, commit);
651 if (!get_cached_commit_buffer(commit, NULL))
652 return;
654 if (opt->show_notes) {
655 int raw;
656 struct strbuf notebuf = STRBUF_INIT;
658 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
659 format_display_notes(&commit->object.oid, &notebuf,
660 get_log_output_encoding(), raw);
661 ctx.notes_message = notebuf.len
662 ? strbuf_detach(&notebuf, NULL)
663 : xcalloc(1, 1);
667 * And then the pretty-printed message itself
669 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
670 ctx.need_8bit_cte =
671 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
672 getenv("GIT_COMMITTER_EMAIL")));
673 ctx.date_mode = opt->date_mode;
674 ctx.date_mode_explicit = opt->date_mode_explicit;
675 ctx.abbrev = opt->diffopt.abbrev;
676 ctx.after_subject = extra_headers;
677 ctx.preserve_subject = opt->preserve_subject;
678 ctx.reflog_info = opt->reflog_info;
679 ctx.fmt = opt->commit_format;
680 ctx.mailmap = opt->mailmap;
681 ctx.color = opt->diffopt.use_color;
682 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
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 if (opt->graph)
687 ctx.graph_width = graph_width(opt->graph);
688 pretty_print_commit(&ctx, commit, &msgbuf);
690 if (opt->add_signoff)
691 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
693 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
694 ctx.notes_message && *ctx.notes_message) {
695 if (cmit_fmt_is_mail(ctx.fmt)) {
696 strbuf_addstr(&msgbuf, "---\n");
697 opt->shown_dashes = 1;
699 strbuf_addstr(&msgbuf, ctx.notes_message);
702 if (opt->show_log_size) {
703 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
704 graph_show_oneline(opt->graph);
708 * Set opt->missing_newline if msgbuf doesn't
709 * end in a newline (including if it is empty)
711 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
712 opt->missing_newline = 1;
713 else
714 opt->missing_newline = 0;
716 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
717 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
718 if (!opt->missing_newline)
719 graph_show_padding(opt->graph);
720 putc(opt->diffopt.line_termination, opt->diffopt.file);
723 strbuf_release(&msgbuf);
724 free(ctx.notes_message);
727 int log_tree_diff_flush(struct rev_info *opt)
729 opt->shown_dashes = 0;
730 diffcore_std(&opt->diffopt);
732 if (diff_queue_is_empty()) {
733 int saved_fmt = opt->diffopt.output_format;
734 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
735 diff_flush(&opt->diffopt);
736 opt->diffopt.output_format = saved_fmt;
737 return 0;
740 if (opt->loginfo && !opt->no_commit_id) {
741 show_log(opt);
742 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
743 opt->verbose_header &&
744 opt->commit_format != CMIT_FMT_ONELINE &&
745 !commit_format_is_empty(opt->commit_format)) {
747 * When showing a verbose header (i.e. log message),
748 * and not in --pretty=oneline format, we would want
749 * an extra newline between the end of log and the
750 * diff/diffstat output for readability.
752 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
753 if (opt->diffopt.output_prefix) {
754 struct strbuf *msg = NULL;
755 msg = opt->diffopt.output_prefix(&opt->diffopt,
756 opt->diffopt.output_prefix_data);
757 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
761 * We may have shown three-dashes line early
762 * between notes and the log message, in which
763 * case we only want a blank line after the
764 * notes without (an extra) three-dashes line.
765 * Otherwise, we show the three-dashes line if
766 * we are showing the patch with diffstat, but
767 * in that case, there is no extra blank line
768 * after the three-dashes line.
770 if (!opt->shown_dashes &&
771 (pch & opt->diffopt.output_format) == pch)
772 fprintf(opt->diffopt.file, "---");
773 putc('\n', opt->diffopt.file);
776 diff_flush(&opt->diffopt);
777 return 1;
780 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
782 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
783 return !opt->loginfo;
787 * Show the diff of a commit.
789 * Return true if we printed any log info messages
791 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
793 int showed_log;
794 struct commit_list *parents;
795 struct object_id *oid;
797 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
798 return 0;
800 parse_commit_or_die(commit);
801 oid = &commit->tree->object.oid;
803 /* Root commit? */
804 parents = get_saved_parents(opt, commit);
805 if (!parents) {
806 if (opt->show_root_diff) {
807 diff_root_tree_oid(oid, "", &opt->diffopt);
808 log_tree_diff_flush(opt);
810 return !opt->loginfo;
813 /* More than one parent? */
814 if (parents && parents->next) {
815 if (opt->ignore_merges)
816 return 0;
817 else if (opt->combine_merges)
818 return do_diff_combined(opt, commit);
819 else if (opt->first_parent_only) {
821 * Generate merge log entry only for the first
822 * parent, showing summary diff of the others
823 * we merged _in_.
825 parse_commit_or_die(parents->item);
826 diff_tree_oid(&parents->item->tree->object.oid,
827 oid, "", &opt->diffopt);
828 log_tree_diff_flush(opt);
829 return !opt->loginfo;
832 /* If we show individual diffs, show the parent info */
833 log->parent = parents->item;
836 showed_log = 0;
837 for (;;) {
838 struct commit *parent = parents->item;
840 parse_commit_or_die(parent);
841 diff_tree_oid(&parent->tree->object.oid,
842 oid, "", &opt->diffopt);
843 log_tree_diff_flush(opt);
845 showed_log |= !opt->loginfo;
847 /* Set up the log info for the next parent, if any.. */
848 parents = parents->next;
849 if (!parents)
850 break;
851 log->parent = parents->item;
852 opt->loginfo = log;
854 return showed_log;
857 int log_tree_commit(struct rev_info *opt, struct commit *commit)
859 struct log_info log;
860 int shown, close_file = opt->diffopt.close_file;
862 log.commit = commit;
863 log.parent = NULL;
864 opt->loginfo = &log;
865 opt->diffopt.close_file = 0;
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 fprintf(opt->diffopt.file, "\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 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
880 opt->loginfo = NULL;
881 maybe_flush_or_die(opt->diffopt.file, "stdout");
882 if (close_file)
883 fclose(opt->diffopt.file);
884 return shown;