receive-pack: relay connectivity errors to sideband
[alt-git.git] / log-tree.c
blobd0062e6788e95620bb6e1a534661dbb70bdbb098
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 struct name_decoration *res;
81 FLEX_ALLOC_STR(res, name, name);
82 res->type = type;
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 struct object_id *oid,
92 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, git_replace_ref_base)) {
100 struct object_id original_oid;
101 if (!check_replace_refs)
102 return 0;
103 if (get_oid_hex(refname + strlen(git_replace_ref_base),
104 &original_oid)) {
105 warning("invalid replace ref %s", refname);
106 return 0;
108 obj = parse_object(original_oid.hash);
109 if (obj)
110 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
111 return 0;
114 obj = parse_object(oid->hash);
115 if (!obj)
116 return 0;
118 if (starts_with(refname, "refs/heads/"))
119 type = DECORATION_REF_LOCAL;
120 else if (starts_with(refname, "refs/remotes/"))
121 type = DECORATION_REF_REMOTE;
122 else if (starts_with(refname, "refs/tags/"))
123 type = DECORATION_REF_TAG;
124 else if (!strcmp(refname, "refs/stash"))
125 type = DECORATION_REF_STASH;
126 else if (!strcmp(refname, "HEAD"))
127 type = DECORATION_REF_HEAD;
129 add_name_decoration(type, refname, obj);
130 while (obj->type == OBJ_TAG) {
131 obj = ((struct tag *)obj)->tagged;
132 if (!obj)
133 break;
134 if (!obj->parsed)
135 parse_object(obj->oid.hash);
136 add_name_decoration(DECORATION_REF_TAG, refname, obj);
138 return 0;
141 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
143 struct commit *commit = lookup_commit(graft->oid.hash);
144 if (!commit)
145 return 0;
146 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
147 return 0;
150 void load_ref_decorations(int flags)
152 if (!decoration_loaded) {
154 decoration_loaded = 1;
155 decoration_flags = flags;
156 for_each_ref(add_ref_decoration, NULL);
157 head_ref(add_ref_decoration, NULL);
158 for_each_commit_graft(add_graft_decoration, NULL);
162 static void show_parents(struct commit *commit, int abbrev, FILE *file)
164 struct commit_list *p;
165 for (p = commit->parents; p ; p = p->next) {
166 struct commit *parent = p->item;
167 fprintf(file, " %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
171 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
173 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
174 for ( ; p; p = p->next) {
175 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
180 * Do we have HEAD in the output, and also the branch it points at?
181 * If so, find that decoration entry for that current branch.
183 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
185 const struct name_decoration *list, *head = NULL;
186 const char *branch_name = NULL;
187 unsigned char unused[20];
188 int rru_flags;
190 /* First find HEAD */
191 for (list = decoration; list; list = list->next)
192 if (list->type == DECORATION_REF_HEAD) {
193 head = list;
194 break;
196 if (!head)
197 return NULL;
199 /* Now resolve and find the matching current branch */
200 branch_name = resolve_ref_unsafe("HEAD", 0, unused, &rru_flags);
201 if (!(rru_flags & REF_ISSYMREF))
202 return NULL;
204 if (!starts_with(branch_name, "refs/"))
205 return NULL;
207 /* OK, do we have that ref in the list? */
208 for (list = decoration; list; list = list->next)
209 if ((list->type == DECORATION_REF_LOCAL) &&
210 !strcmp(branch_name, list->name)) {
211 return list;
214 return NULL;
217 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
219 if (decoration_flags == DECORATE_SHORT_REFS)
220 strbuf_addstr(sb, prettify_refname(decoration->name));
221 else
222 strbuf_addstr(sb, decoration->name);
226 * The caller makes sure there is no funny color before calling.
227 * format_decorations_extended makes sure the same after return.
229 void format_decorations_extended(struct strbuf *sb,
230 const struct commit *commit,
231 int use_color,
232 const char *prefix,
233 const char *separator,
234 const char *suffix)
236 const struct name_decoration *decoration;
237 const struct name_decoration *current_and_HEAD;
238 const char *color_commit =
239 diff_get_color(use_color, DIFF_COMMIT);
240 const char *color_reset =
241 decorate_get_color(use_color, DECORATION_NONE);
243 decoration = get_name_decoration(&commit->object);
244 if (!decoration)
245 return;
247 current_and_HEAD = current_pointed_by_HEAD(decoration);
248 while (decoration) {
250 * When both current and HEAD are there, only
251 * show HEAD->current where HEAD would have
252 * appeared, skipping the entry for current.
254 if (decoration != current_and_HEAD) {
255 strbuf_addstr(sb, color_commit);
256 strbuf_addstr(sb, prefix);
257 strbuf_addstr(sb, color_reset);
258 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
259 if (decoration->type == DECORATION_REF_TAG)
260 strbuf_addstr(sb, "tag: ");
262 show_name(sb, decoration);
264 if (current_and_HEAD &&
265 decoration->type == DECORATION_REF_HEAD) {
266 strbuf_addstr(sb, color_reset);
267 strbuf_addstr(sb, color_commit);
268 strbuf_addstr(sb, " -> ");
269 strbuf_addstr(sb, color_reset);
270 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
271 show_name(sb, current_and_HEAD);
273 strbuf_addstr(sb, color_reset);
275 prefix = separator;
277 decoration = decoration->next;
279 strbuf_addstr(sb, color_commit);
280 strbuf_addstr(sb, suffix);
281 strbuf_addstr(sb, color_reset);
284 void show_decorations(struct rev_info *opt, struct commit *commit)
286 struct strbuf sb = STRBUF_INIT;
288 if (opt->show_source && commit->util)
289 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
290 if (!opt->show_decorations)
291 return;
292 format_decorations(&sb, commit, opt->diffopt.use_color);
293 fputs(sb.buf, opt->diffopt.file);
294 strbuf_release(&sb);
297 static unsigned int digits_in_number(unsigned int number)
299 unsigned int i = 10, result = 1;
300 while (i <= number) {
301 i *= 10;
302 result++;
304 return result;
307 void fmt_output_subject(struct strbuf *filename,
308 const char *subject,
309 struct rev_info *info)
311 const char *suffix = info->patch_suffix;
312 int nr = info->nr;
313 int start_len = filename->len;
314 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
316 if (0 < info->reroll_count)
317 strbuf_addf(filename, "v%d-", info->reroll_count);
318 strbuf_addf(filename, "%04d-%s", nr, subject);
320 if (max_len < filename->len)
321 strbuf_setlen(filename, max_len);
322 strbuf_addstr(filename, suffix);
325 void fmt_output_commit(struct strbuf *filename,
326 struct commit *commit,
327 struct rev_info *info)
329 struct pretty_print_context ctx = {0};
330 struct strbuf subject = STRBUF_INIT;
332 format_commit_message(commit, "%f", &subject, &ctx);
333 fmt_output_subject(filename, subject.buf, info);
334 strbuf_release(&subject);
337 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
338 const char **subject_p,
339 const char **extra_headers_p,
340 int *need_8bit_cte_p)
342 const char *subject = NULL;
343 const char *extra_headers = opt->extra_headers;
344 const char *name = oid_to_hex(opt->zero_commit ?
345 &null_oid : &commit->object.oid);
347 *need_8bit_cte_p = 0; /* unknown */
348 if (opt->total > 0) {
349 static char buffer[64];
350 snprintf(buffer, sizeof(buffer),
351 "Subject: [%s%s%0*d/%d] ",
352 opt->subject_prefix,
353 *opt->subject_prefix ? " " : "",
354 digits_in_number(opt->total),
355 opt->nr, opt->total);
356 subject = buffer;
357 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
358 static char buffer[256];
359 snprintf(buffer, sizeof(buffer),
360 "Subject: [%s] ",
361 opt->subject_prefix);
362 subject = buffer;
363 } else {
364 subject = "Subject: ";
367 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
368 graph_show_oneline(opt->graph);
369 if (opt->message_id) {
370 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
371 graph_show_oneline(opt->graph);
373 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
374 int i, n;
375 n = opt->ref_message_ids->nr;
376 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
377 for (i = 0; i < n; i++)
378 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
379 opt->ref_message_ids->items[i].string);
380 graph_show_oneline(opt->graph);
382 if (opt->mime_boundary) {
383 static char subject_buffer[1024];
384 static char buffer[1024];
385 struct strbuf filename = STRBUF_INIT;
386 *need_8bit_cte_p = -1; /* NEVER */
387 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
388 "%s"
389 "MIME-Version: 1.0\n"
390 "Content-Type: multipart/mixed;"
391 " boundary=\"%s%s\"\n"
392 "\n"
393 "This is a multi-part message in MIME "
394 "format.\n"
395 "--%s%s\n"
396 "Content-Type: text/plain; "
397 "charset=UTF-8; format=fixed\n"
398 "Content-Transfer-Encoding: 8bit\n\n",
399 extra_headers ? extra_headers : "",
400 mime_boundary_leader, opt->mime_boundary,
401 mime_boundary_leader, opt->mime_boundary);
402 extra_headers = subject_buffer;
404 if (opt->numbered_files)
405 strbuf_addf(&filename, "%d", opt->nr);
406 else
407 fmt_output_commit(&filename, commit, opt);
408 snprintf(buffer, sizeof(buffer) - 1,
409 "\n--%s%s\n"
410 "Content-Type: text/x-patch;"
411 " name=\"%s\"\n"
412 "Content-Transfer-Encoding: 8bit\n"
413 "Content-Disposition: %s;"
414 " filename=\"%s\"\n\n",
415 mime_boundary_leader, opt->mime_boundary,
416 filename.buf,
417 opt->no_inline ? "attachment" : "inline",
418 filename.buf);
419 opt->diffopt.stat_sep = buffer;
420 strbuf_release(&filename);
422 *subject_p = subject;
423 *extra_headers_p = extra_headers;
426 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
428 const char *color, *reset, *eol;
430 color = diff_get_color_opt(&opt->diffopt,
431 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
432 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
433 while (*bol) {
434 eol = strchrnul(bol, '\n');
435 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
436 *eol ? "\n" : "");
437 graph_show_oneline(opt->graph);
438 bol = (*eol) ? (eol + 1) : eol;
442 static void show_signature(struct rev_info *opt, struct commit *commit)
444 struct strbuf payload = STRBUF_INIT;
445 struct strbuf signature = STRBUF_INIT;
446 struct strbuf gpg_output = STRBUF_INIT;
447 int status;
449 if (parse_signed_commit(commit, &payload, &signature) <= 0)
450 goto out;
452 status = verify_signed_buffer(payload.buf, payload.len,
453 signature.buf, signature.len,
454 &gpg_output, NULL);
455 if (status && !gpg_output.len)
456 strbuf_addstr(&gpg_output, "No signature\n");
458 show_sig_lines(opt, status, gpg_output.buf);
460 out:
461 strbuf_release(&gpg_output);
462 strbuf_release(&payload);
463 strbuf_release(&signature);
466 static int which_parent(const unsigned char *sha1, const struct commit *commit)
468 int nth;
469 const struct commit_list *parent;
471 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
472 if (!hashcmp(parent->item->object.oid.hash, sha1))
473 return nth;
474 nth++;
476 return -1;
479 static int is_common_merge(const struct commit *commit)
481 return (commit->parents
482 && commit->parents->next
483 && !commit->parents->next->next);
486 static void show_one_mergetag(struct commit *commit,
487 struct commit_extra_header *extra,
488 void *data)
490 struct rev_info *opt = (struct rev_info *)data;
491 unsigned char sha1[20];
492 struct tag *tag;
493 struct strbuf verify_message;
494 int status, nth;
495 size_t payload_size, gpg_message_offset;
497 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
498 tag = lookup_tag(sha1);
499 if (!tag)
500 return; /* error message already given */
502 strbuf_init(&verify_message, 256);
503 if (parse_tag_buffer(tag, extra->value, extra->len))
504 strbuf_addstr(&verify_message, "malformed mergetag\n");
505 else if (is_common_merge(commit) &&
506 !oidcmp(&tag->tagged->oid,
507 &commit->parents->next->item->object.oid))
508 strbuf_addf(&verify_message,
509 "merged tag '%s'\n", tag->tag);
510 else if ((nth = which_parent(tag->tagged->oid.hash, commit)) < 0)
511 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
512 tag->tag, tag->tagged->oid.hash);
513 else
514 strbuf_addf(&verify_message,
515 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
516 gpg_message_offset = verify_message.len;
518 payload_size = parse_signature(extra->value, extra->len);
519 status = -1;
520 if (extra->len > payload_size) {
521 /* could have a good signature */
522 if (!verify_signed_buffer(extra->value, payload_size,
523 extra->value + payload_size,
524 extra->len - payload_size,
525 &verify_message, NULL))
526 status = 0; /* good */
527 else if (verify_message.len <= gpg_message_offset)
528 strbuf_addstr(&verify_message, "No signature\n");
529 /* otherwise we couldn't verify, which is shown as bad */
532 show_sig_lines(opt, status, verify_message.buf);
533 strbuf_release(&verify_message);
536 static void show_mergetag(struct rev_info *opt, struct commit *commit)
538 for_each_mergetag(show_one_mergetag, commit, opt);
541 void show_log(struct rev_info *opt)
543 struct strbuf msgbuf = STRBUF_INIT;
544 struct log_info *log = opt->loginfo;
545 struct commit *commit = log->commit, *parent = log->parent;
546 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
547 const char *extra_headers = opt->extra_headers;
548 struct pretty_print_context ctx = {0};
550 opt->loginfo = NULL;
551 if (!opt->verbose_header) {
552 graph_show_commit(opt->graph);
554 if (!opt->graph)
555 put_revision_mark(opt, commit);
556 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
557 if (opt->print_parents)
558 show_parents(commit, abbrev_commit, opt->diffopt.file);
559 if (opt->children.name)
560 show_children(opt, commit, abbrev_commit);
561 show_decorations(opt, commit);
562 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
563 putc('\n', opt->diffopt.file);
564 graph_show_remainder(opt->graph);
566 putc(opt->diffopt.line_termination, opt->diffopt.file);
567 return;
571 * If use_terminator is set, we already handled any record termination
572 * at the end of the last record.
573 * Otherwise, add a diffopt.line_termination character before all
574 * entries but the first. (IOW, as a separator between entries)
576 if (opt->shown_one && !opt->use_terminator) {
578 * If entries are separated by a newline, the output
579 * should look human-readable. If the last entry ended
580 * with a newline, print the graph output before this
581 * newline. Otherwise it will end up as a completely blank
582 * line and will look like a gap in the graph.
584 * If the entry separator is not a newline, the output is
585 * primarily intended for programmatic consumption, and we
586 * never want the extra graph output before the entry
587 * separator.
589 if (opt->diffopt.line_termination == '\n' &&
590 !opt->missing_newline)
591 graph_show_padding(opt->graph);
592 putc(opt->diffopt.line_termination, opt->diffopt.file);
594 opt->shown_one = 1;
597 * If the history graph was requested,
598 * print the graph, up to this commit's line
600 graph_show_commit(opt->graph);
603 * Print header line of header..
606 if (cmit_fmt_is_mail(opt->commit_format)) {
607 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
608 &ctx.need_8bit_cte);
609 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
610 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
611 if (opt->commit_format != CMIT_FMT_ONELINE)
612 fputs("commit ", opt->diffopt.file);
614 if (!opt->graph)
615 put_revision_mark(opt, commit);
616 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
617 opt->diffopt.file);
618 if (opt->print_parents)
619 show_parents(commit, abbrev_commit, opt->diffopt.file);
620 if (opt->children.name)
621 show_children(opt, commit, abbrev_commit);
622 if (parent)
623 fprintf(opt->diffopt.file, " (from %s)",
624 find_unique_abbrev(parent->object.oid.hash,
625 abbrev_commit));
626 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
627 show_decorations(opt, commit);
628 if (opt->commit_format == CMIT_FMT_ONELINE) {
629 putc(' ', opt->diffopt.file);
630 } else {
631 putc('\n', opt->diffopt.file);
632 graph_show_oneline(opt->graph);
634 if (opt->reflog_info) {
636 * setup_revisions() ensures that opt->reflog_info
637 * and opt->graph cannot both be set,
638 * so we don't need to worry about printing the
639 * graph info here.
641 show_reflog_message(opt->reflog_info,
642 opt->commit_format == CMIT_FMT_ONELINE,
643 &opt->date_mode,
644 opt->date_mode_explicit);
645 if (opt->commit_format == CMIT_FMT_ONELINE)
646 return;
650 if (opt->show_signature) {
651 show_signature(opt, commit);
652 show_mergetag(opt, commit);
655 if (!get_cached_commit_buffer(commit, NULL))
656 return;
658 if (opt->show_notes) {
659 int raw;
660 struct strbuf notebuf = STRBUF_INIT;
662 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
663 format_display_notes(commit->object.oid.hash, &notebuf,
664 get_log_output_encoding(), raw);
665 ctx.notes_message = notebuf.len
666 ? strbuf_detach(&notebuf, NULL)
667 : xcalloc(1, 1);
671 * And then the pretty-printed message itself
673 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
674 ctx.need_8bit_cte =
675 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
676 getenv("GIT_COMMITTER_EMAIL")));
677 ctx.date_mode = opt->date_mode;
678 ctx.date_mode_explicit = opt->date_mode_explicit;
679 ctx.abbrev = opt->diffopt.abbrev;
680 ctx.after_subject = extra_headers;
681 ctx.preserve_subject = opt->preserve_subject;
682 ctx.reflog_info = opt->reflog_info;
683 ctx.fmt = opt->commit_format;
684 ctx.mailmap = opt->mailmap;
685 ctx.color = opt->diffopt.use_color;
686 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
687 ctx.output_encoding = get_log_output_encoding();
688 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
689 ctx.from_ident = &opt->from_ident;
690 if (opt->graph)
691 ctx.graph_width = graph_width(opt->graph);
692 pretty_print_commit(&ctx, commit, &msgbuf);
694 if (opt->add_signoff)
695 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
697 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
698 ctx.notes_message && *ctx.notes_message) {
699 if (cmit_fmt_is_mail(ctx.fmt)) {
700 strbuf_addstr(&msgbuf, "---\n");
701 opt->shown_dashes = 1;
703 strbuf_addstr(&msgbuf, ctx.notes_message);
706 if (opt->show_log_size) {
707 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
708 graph_show_oneline(opt->graph);
712 * Set opt->missing_newline if msgbuf doesn't
713 * end in a newline (including if it is empty)
715 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
716 opt->missing_newline = 1;
717 else
718 opt->missing_newline = 0;
720 if (opt->graph)
721 graph_show_commit_msg(opt->graph, &msgbuf);
722 else
723 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, opt->diffopt.file);
724 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
725 if (!opt->missing_newline)
726 graph_show_padding(opt->graph);
727 putc(opt->diffopt.line_termination, opt->diffopt.file);
730 strbuf_release(&msgbuf);
731 free(ctx.notes_message);
734 int log_tree_diff_flush(struct rev_info *opt)
736 opt->shown_dashes = 0;
737 diffcore_std(&opt->diffopt);
739 if (diff_queue_is_empty()) {
740 int saved_fmt = opt->diffopt.output_format;
741 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
742 diff_flush(&opt->diffopt);
743 opt->diffopt.output_format = saved_fmt;
744 return 0;
747 if (opt->loginfo && !opt->no_commit_id) {
748 show_log(opt);
749 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
750 opt->verbose_header &&
751 opt->commit_format != CMIT_FMT_ONELINE &&
752 !commit_format_is_empty(opt->commit_format)) {
754 * When showing a verbose header (i.e. log message),
755 * and not in --pretty=oneline format, we would want
756 * an extra newline between the end of log and the
757 * diff/diffstat output for readability.
759 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
760 if (opt->diffopt.output_prefix) {
761 struct strbuf *msg = NULL;
762 msg = opt->diffopt.output_prefix(&opt->diffopt,
763 opt->diffopt.output_prefix_data);
764 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
768 * We may have shown three-dashes line early
769 * between notes and the log message, in which
770 * case we only want a blank line after the
771 * notes without (an extra) three-dashes line.
772 * Otherwise, we show the three-dashes line if
773 * we are showing the patch with diffstat, but
774 * in that case, there is no extra blank line
775 * after the three-dashes line.
777 if (!opt->shown_dashes &&
778 (pch & opt->diffopt.output_format) == pch)
779 fprintf(opt->diffopt.file, "---");
780 putc('\n', opt->diffopt.file);
783 diff_flush(&opt->diffopt);
784 return 1;
787 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
789 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
790 return !opt->loginfo;
794 * Show the diff of a commit.
796 * Return true if we printed any log info messages
798 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
800 int showed_log;
801 struct commit_list *parents;
802 struct object_id *oid;
804 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
805 return 0;
807 parse_commit_or_die(commit);
808 oid = &commit->tree->object.oid;
810 /* Root commit? */
811 parents = get_saved_parents(opt, commit);
812 if (!parents) {
813 if (opt->show_root_diff) {
814 diff_root_tree_sha1(oid->hash, "", &opt->diffopt);
815 log_tree_diff_flush(opt);
817 return !opt->loginfo;
820 /* More than one parent? */
821 if (parents && parents->next) {
822 if (opt->ignore_merges)
823 return 0;
824 else if (opt->combine_merges)
825 return do_diff_combined(opt, commit);
826 else if (opt->first_parent_only) {
828 * Generate merge log entry only for the first
829 * parent, showing summary diff of the others
830 * we merged _in_.
832 parse_commit_or_die(parents->item);
833 diff_tree_sha1(parents->item->tree->object.oid.hash,
834 oid->hash, "", &opt->diffopt);
835 log_tree_diff_flush(opt);
836 return !opt->loginfo;
839 /* If we show individual diffs, show the parent info */
840 log->parent = parents->item;
843 showed_log = 0;
844 for (;;) {
845 struct commit *parent = parents->item;
847 parse_commit_or_die(parent);
848 diff_tree_sha1(parent->tree->object.oid.hash,
849 oid->hash, "", &opt->diffopt);
850 log_tree_diff_flush(opt);
852 showed_log |= !opt->loginfo;
854 /* Set up the log info for the next parent, if any.. */
855 parents = parents->next;
856 if (!parents)
857 break;
858 log->parent = parents->item;
859 opt->loginfo = log;
861 return showed_log;
864 int log_tree_commit(struct rev_info *opt, struct commit *commit)
866 struct log_info log;
867 int shown, close_file = opt->diffopt.close_file;
869 log.commit = commit;
870 log.parent = NULL;
871 opt->loginfo = &log;
872 opt->diffopt.close_file = 0;
874 if (opt->line_level_traverse)
875 return line_log_print(opt, commit);
877 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
878 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
879 shown = log_tree_diff(opt, commit, &log);
880 if (!shown && opt->loginfo && opt->always_show_header) {
881 log.parent = NULL;
882 show_log(opt);
883 shown = 1;
885 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
886 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
887 opt->loginfo = NULL;
888 maybe_flush_or_die(opt->diffopt.file, "stdout");
889 if (close_file)
890 fclose(opt->diffopt.file);
891 return shown;