Merge branch 'js/sequencer-and-root-commits'
[git.git] / log-tree.c
blob724bae0de25b5b6e22dfecee233ff999be880dd5
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;
97 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
99 if (filter && !ref_filter_match(refname,
100 filter->include_ref_pattern,
101 filter->exclude_ref_pattern))
102 return 0;
104 if (starts_with(refname, git_replace_ref_base)) {
105 struct object_id original_oid;
106 if (!check_replace_refs)
107 return 0;
108 if (get_oid_hex(refname + strlen(git_replace_ref_base),
109 &original_oid)) {
110 warning("invalid replace ref %s", refname);
111 return 0;
113 obj = parse_object(&original_oid);
114 if (obj)
115 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
116 return 0;
119 obj = parse_object(oid);
120 if (!obj)
121 return 0;
123 if (starts_with(refname, "refs/heads/"))
124 type = DECORATION_REF_LOCAL;
125 else if (starts_with(refname, "refs/remotes/"))
126 type = DECORATION_REF_REMOTE;
127 else if (starts_with(refname, "refs/tags/"))
128 type = DECORATION_REF_TAG;
129 else if (!strcmp(refname, "refs/stash"))
130 type = DECORATION_REF_STASH;
131 else if (!strcmp(refname, "HEAD"))
132 type = DECORATION_REF_HEAD;
134 add_name_decoration(type, refname, obj);
135 while (obj->type == OBJ_TAG) {
136 obj = ((struct tag *)obj)->tagged;
137 if (!obj)
138 break;
139 if (!obj->parsed)
140 parse_object(&obj->oid);
141 add_name_decoration(DECORATION_REF_TAG, refname, obj);
143 return 0;
146 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
148 struct commit *commit = lookup_commit(&graft->oid);
149 if (!commit)
150 return 0;
151 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
152 return 0;
155 void load_ref_decorations(struct decoration_filter *filter, int flags)
157 if (!decoration_loaded) {
158 if (filter) {
159 struct string_list_item *item;
160 for_each_string_list_item(item, filter->exclude_ref_pattern) {
161 normalize_glob_ref(item, NULL, item->string);
163 for_each_string_list_item(item, filter->include_ref_pattern) {
164 normalize_glob_ref(item, NULL, item->string);
167 decoration_loaded = 1;
168 decoration_flags = flags;
169 for_each_ref(add_ref_decoration, filter);
170 head_ref(add_ref_decoration, filter);
171 for_each_commit_graft(add_graft_decoration, filter);
175 static void show_parents(struct commit *commit, int abbrev, FILE *file)
177 struct commit_list *p;
178 for (p = commit->parents; p ; p = p->next) {
179 struct commit *parent = p->item;
180 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
184 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
186 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
187 for ( ; p; p = p->next) {
188 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
193 * Do we have HEAD in the output, and also the branch it points at?
194 * If so, find that decoration entry for that current branch.
196 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
198 const struct name_decoration *list, *head = NULL;
199 const char *branch_name = NULL;
200 int rru_flags;
202 /* First find HEAD */
203 for (list = decoration; list; list = list->next)
204 if (list->type == DECORATION_REF_HEAD) {
205 head = list;
206 break;
208 if (!head)
209 return NULL;
211 /* Now resolve and find the matching current branch */
212 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
213 if (!branch_name || !(rru_flags & REF_ISSYMREF))
214 return NULL;
216 if (!starts_with(branch_name, "refs/"))
217 return NULL;
219 /* OK, do we have that ref in the list? */
220 for (list = decoration; list; list = list->next)
221 if ((list->type == DECORATION_REF_LOCAL) &&
222 !strcmp(branch_name, list->name)) {
223 return list;
226 return NULL;
229 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
231 if (decoration_flags == DECORATE_SHORT_REFS)
232 strbuf_addstr(sb, prettify_refname(decoration->name));
233 else
234 strbuf_addstr(sb, decoration->name);
238 * The caller makes sure there is no funny color before calling.
239 * format_decorations_extended makes sure the same after return.
241 void format_decorations_extended(struct strbuf *sb,
242 const struct commit *commit,
243 int use_color,
244 const char *prefix,
245 const char *separator,
246 const char *suffix)
248 const struct name_decoration *decoration;
249 const struct name_decoration *current_and_HEAD;
250 const char *color_commit =
251 diff_get_color(use_color, DIFF_COMMIT);
252 const char *color_reset =
253 decorate_get_color(use_color, DECORATION_NONE);
255 decoration = get_name_decoration(&commit->object);
256 if (!decoration)
257 return;
259 current_and_HEAD = current_pointed_by_HEAD(decoration);
260 while (decoration) {
262 * When both current and HEAD are there, only
263 * show HEAD->current where HEAD would have
264 * appeared, skipping the entry for current.
266 if (decoration != current_and_HEAD) {
267 strbuf_addstr(sb, color_commit);
268 strbuf_addstr(sb, prefix);
269 strbuf_addstr(sb, color_reset);
270 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
271 if (decoration->type == DECORATION_REF_TAG)
272 strbuf_addstr(sb, "tag: ");
274 show_name(sb, decoration);
276 if (current_and_HEAD &&
277 decoration->type == DECORATION_REF_HEAD) {
278 strbuf_addstr(sb, " -> ");
279 strbuf_addstr(sb, color_reset);
280 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
281 show_name(sb, current_and_HEAD);
283 strbuf_addstr(sb, color_reset);
285 prefix = separator;
287 decoration = decoration->next;
289 strbuf_addstr(sb, color_commit);
290 strbuf_addstr(sb, suffix);
291 strbuf_addstr(sb, color_reset);
294 void show_decorations(struct rev_info *opt, struct commit *commit)
296 struct strbuf sb = STRBUF_INIT;
298 if (opt->show_source && commit->util)
299 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
300 if (!opt->show_decorations)
301 return;
302 format_decorations(&sb, commit, opt->diffopt.use_color);
303 fputs(sb.buf, opt->diffopt.file);
304 strbuf_release(&sb);
307 static unsigned int digits_in_number(unsigned int number)
309 unsigned int i = 10, result = 1;
310 while (i <= number) {
311 i *= 10;
312 result++;
314 return result;
317 void fmt_output_subject(struct strbuf *filename,
318 const char *subject,
319 struct rev_info *info)
321 const char *suffix = info->patch_suffix;
322 int nr = info->nr;
323 int start_len = filename->len;
324 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
326 if (0 < info->reroll_count)
327 strbuf_addf(filename, "v%d-", info->reroll_count);
328 strbuf_addf(filename, "%04d-%s", nr, subject);
330 if (max_len < filename->len)
331 strbuf_setlen(filename, max_len);
332 strbuf_addstr(filename, suffix);
335 void fmt_output_commit(struct strbuf *filename,
336 struct commit *commit,
337 struct rev_info *info)
339 struct pretty_print_context ctx = {0};
340 struct strbuf subject = STRBUF_INIT;
342 format_commit_message(commit, "%f", &subject, &ctx);
343 fmt_output_subject(filename, subject.buf, info);
344 strbuf_release(&subject);
347 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
349 if (opt->total > 0) {
350 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
351 opt->subject_prefix,
352 *opt->subject_prefix ? " " : "",
353 digits_in_number(opt->total),
354 opt->nr, opt->total);
355 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
356 strbuf_addf(sb, "Subject: [%s] ",
357 opt->subject_prefix);
358 } else {
359 strbuf_addstr(sb, "Subject: ");
363 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
364 const char **extra_headers_p,
365 int *need_8bit_cte_p,
366 int maybe_multipart)
368 const char *extra_headers = opt->extra_headers;
369 const char *name = oid_to_hex(opt->zero_commit ?
370 &null_oid : &commit->object.oid);
372 *need_8bit_cte_p = 0; /* unknown */
374 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
375 graph_show_oneline(opt->graph);
376 if (opt->message_id) {
377 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
378 graph_show_oneline(opt->graph);
380 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
381 int i, n;
382 n = opt->ref_message_ids->nr;
383 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
384 for (i = 0; i < n; i++)
385 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
386 opt->ref_message_ids->items[i].string);
387 graph_show_oneline(opt->graph);
389 if (opt->mime_boundary && maybe_multipart) {
390 static char subject_buffer[1024];
391 static char buffer[1024];
392 struct strbuf filename = STRBUF_INIT;
393 *need_8bit_cte_p = -1; /* NEVER */
394 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
395 "%s"
396 "MIME-Version: 1.0\n"
397 "Content-Type: multipart/mixed;"
398 " boundary=\"%s%s\"\n"
399 "\n"
400 "This is a multi-part message in MIME "
401 "format.\n"
402 "--%s%s\n"
403 "Content-Type: text/plain; "
404 "charset=UTF-8; format=fixed\n"
405 "Content-Transfer-Encoding: 8bit\n\n",
406 extra_headers ? extra_headers : "",
407 mime_boundary_leader, opt->mime_boundary,
408 mime_boundary_leader, opt->mime_boundary);
409 extra_headers = subject_buffer;
411 if (opt->numbered_files)
412 strbuf_addf(&filename, "%d", opt->nr);
413 else
414 fmt_output_commit(&filename, commit, opt);
415 snprintf(buffer, sizeof(buffer) - 1,
416 "\n--%s%s\n"
417 "Content-Type: text/x-patch;"
418 " name=\"%s\"\n"
419 "Content-Transfer-Encoding: 8bit\n"
420 "Content-Disposition: %s;"
421 " filename=\"%s\"\n\n",
422 mime_boundary_leader, opt->mime_boundary,
423 filename.buf,
424 opt->no_inline ? "attachment" : "inline",
425 filename.buf);
426 opt->diffopt.stat_sep = buffer;
427 strbuf_release(&filename);
429 *extra_headers_p = extra_headers;
432 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
434 const char *color, *reset, *eol;
436 color = diff_get_color_opt(&opt->diffopt,
437 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
438 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
439 while (*bol) {
440 eol = strchrnul(bol, '\n');
441 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
442 *eol ? "\n" : "");
443 graph_show_oneline(opt->graph);
444 bol = (*eol) ? (eol + 1) : eol;
448 static void show_signature(struct rev_info *opt, struct commit *commit)
450 struct strbuf payload = STRBUF_INIT;
451 struct strbuf signature = STRBUF_INIT;
452 struct strbuf gpg_output = STRBUF_INIT;
453 int status;
455 if (parse_signed_commit(commit, &payload, &signature) <= 0)
456 goto out;
458 status = verify_signed_buffer(payload.buf, payload.len,
459 signature.buf, signature.len,
460 &gpg_output, NULL);
461 if (status && !gpg_output.len)
462 strbuf_addstr(&gpg_output, "No signature\n");
464 show_sig_lines(opt, status, gpg_output.buf);
466 out:
467 strbuf_release(&gpg_output);
468 strbuf_release(&payload);
469 strbuf_release(&signature);
472 static int which_parent(const struct object_id *oid, const struct commit *commit)
474 int nth;
475 const struct commit_list *parent;
477 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
478 if (!oidcmp(&parent->item->object.oid, oid))
479 return nth;
480 nth++;
482 return -1;
485 static int is_common_merge(const struct commit *commit)
487 return (commit->parents
488 && commit->parents->next
489 && !commit->parents->next->next);
492 static int show_one_mergetag(struct commit *commit,
493 struct commit_extra_header *extra,
494 void *data)
496 struct rev_info *opt = (struct rev_info *)data;
497 struct object_id oid;
498 struct tag *tag;
499 struct strbuf verify_message;
500 int status, nth;
501 size_t payload_size, gpg_message_offset;
503 hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
504 tag = lookup_tag(&oid);
505 if (!tag)
506 return -1; /* error message already given */
508 strbuf_init(&verify_message, 256);
509 if (parse_tag_buffer(tag, extra->value, extra->len))
510 strbuf_addstr(&verify_message, "malformed mergetag\n");
511 else if (is_common_merge(commit) &&
512 !oidcmp(&tag->tagged->oid,
513 &commit->parents->next->item->object.oid))
514 strbuf_addf(&verify_message,
515 "merged tag '%s'\n", tag->tag);
516 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
517 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
518 tag->tag, tag->tagged->oid.hash);
519 else
520 strbuf_addf(&verify_message,
521 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
522 gpg_message_offset = verify_message.len;
524 payload_size = parse_signature(extra->value, extra->len);
525 status = -1;
526 if (extra->len > payload_size) {
527 /* could have a good signature */
528 if (!verify_signed_buffer(extra->value, payload_size,
529 extra->value + payload_size,
530 extra->len - payload_size,
531 &verify_message, NULL))
532 status = 0; /* good */
533 else if (verify_message.len <= gpg_message_offset)
534 strbuf_addstr(&verify_message, "No signature\n");
535 /* otherwise we couldn't verify, which is shown as bad */
538 show_sig_lines(opt, status, verify_message.buf);
539 strbuf_release(&verify_message);
540 return 0;
543 static int show_mergetag(struct rev_info *opt, struct commit *commit)
545 return for_each_mergetag(show_one_mergetag, commit, opt);
548 void show_log(struct rev_info *opt)
550 struct strbuf msgbuf = STRBUF_INIT;
551 struct log_info *log = opt->loginfo;
552 struct commit *commit = log->commit, *parent = log->parent;
553 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
554 const char *extra_headers = opt->extra_headers;
555 struct pretty_print_context ctx = {0};
557 opt->loginfo = NULL;
558 if (!opt->verbose_header) {
559 graph_show_commit(opt->graph);
561 if (!opt->graph)
562 put_revision_mark(opt, commit);
563 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
564 if (opt->print_parents)
565 show_parents(commit, abbrev_commit, opt->diffopt.file);
566 if (opt->children.name)
567 show_children(opt, commit, abbrev_commit);
568 show_decorations(opt, commit);
569 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
570 putc('\n', opt->diffopt.file);
571 graph_show_remainder(opt->graph);
573 putc(opt->diffopt.line_termination, opt->diffopt.file);
574 return;
578 * If use_terminator is set, we already handled any record termination
579 * at the end of the last record.
580 * Otherwise, add a diffopt.line_termination character before all
581 * entries but the first. (IOW, as a separator between entries)
583 if (opt->shown_one && !opt->use_terminator) {
585 * If entries are separated by a newline, the output
586 * should look human-readable. If the last entry ended
587 * with a newline, print the graph output before this
588 * newline. Otherwise it will end up as a completely blank
589 * line and will look like a gap in the graph.
591 * If the entry separator is not a newline, the output is
592 * primarily intended for programmatic consumption, and we
593 * never want the extra graph output before the entry
594 * separator.
596 if (opt->diffopt.line_termination == '\n' &&
597 !opt->missing_newline)
598 graph_show_padding(opt->graph);
599 putc(opt->diffopt.line_termination, opt->diffopt.file);
601 opt->shown_one = 1;
604 * If the history graph was requested,
605 * print the graph, up to this commit's line
607 graph_show_commit(opt->graph);
610 * Print header line of header..
613 if (cmit_fmt_is_mail(opt->commit_format)) {
614 log_write_email_headers(opt, commit, &extra_headers,
615 &ctx.need_8bit_cte, 1);
616 ctx.rev = opt;
617 ctx.print_email_subject = 1;
618 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
619 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
620 if (opt->commit_format != CMIT_FMT_ONELINE)
621 fputs("commit ", opt->diffopt.file);
623 if (!opt->graph)
624 put_revision_mark(opt, commit);
625 fputs(find_unique_abbrev(&commit->object.oid,
626 abbrev_commit),
627 opt->diffopt.file);
628 if (opt->print_parents)
629 show_parents(commit, abbrev_commit, opt->diffopt.file);
630 if (opt->children.name)
631 show_children(opt, commit, abbrev_commit);
632 if (parent)
633 fprintf(opt->diffopt.file, " (from %s)",
634 find_unique_abbrev(&parent->object.oid, abbrev_commit));
635 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
636 show_decorations(opt, commit);
637 if (opt->commit_format == CMIT_FMT_ONELINE) {
638 putc(' ', opt->diffopt.file);
639 } else {
640 putc('\n', opt->diffopt.file);
641 graph_show_oneline(opt->graph);
643 if (opt->reflog_info) {
645 * setup_revisions() ensures that opt->reflog_info
646 * and opt->graph cannot both be set,
647 * so we don't need to worry about printing the
648 * graph info here.
650 show_reflog_message(opt->reflog_info,
651 opt->commit_format == CMIT_FMT_ONELINE,
652 &opt->date_mode,
653 opt->date_mode_explicit);
654 if (opt->commit_format == CMIT_FMT_ONELINE)
655 return;
659 if (opt->show_signature) {
660 show_signature(opt, commit);
661 show_mergetag(opt, commit);
664 if (opt->show_notes) {
665 int raw;
666 struct strbuf notebuf = STRBUF_INIT;
668 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
669 format_display_notes(&commit->object.oid, &notebuf,
670 get_log_output_encoding(), raw);
671 ctx.notes_message = notebuf.len
672 ? strbuf_detach(&notebuf, NULL)
673 : xcalloc(1, 1);
677 * And then the pretty-printed message itself
679 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
680 ctx.need_8bit_cte =
681 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
682 getenv("GIT_COMMITTER_EMAIL")));
683 ctx.date_mode = opt->date_mode;
684 ctx.date_mode_explicit = opt->date_mode_explicit;
685 ctx.abbrev = opt->diffopt.abbrev;
686 ctx.after_subject = extra_headers;
687 ctx.preserve_subject = opt->preserve_subject;
688 ctx.reflog_info = opt->reflog_info;
689 ctx.fmt = opt->commit_format;
690 ctx.mailmap = opt->mailmap;
691 ctx.color = opt->diffopt.use_color;
692 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
693 ctx.output_encoding = get_log_output_encoding();
694 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
695 ctx.from_ident = &opt->from_ident;
696 if (opt->graph)
697 ctx.graph_width = graph_width(opt->graph);
698 pretty_print_commit(&ctx, commit, &msgbuf);
700 if (opt->add_signoff)
701 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
703 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
704 ctx.notes_message && *ctx.notes_message) {
705 if (cmit_fmt_is_mail(ctx.fmt)) {
706 strbuf_addstr(&msgbuf, "---\n");
707 opt->shown_dashes = 1;
709 strbuf_addstr(&msgbuf, ctx.notes_message);
712 if (opt->show_log_size) {
713 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
714 graph_show_oneline(opt->graph);
718 * Set opt->missing_newline if msgbuf doesn't
719 * end in a newline (including if it is empty)
721 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
722 opt->missing_newline = 1;
723 else
724 opt->missing_newline = 0;
726 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
727 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
728 if (!opt->missing_newline)
729 graph_show_padding(opt->graph);
730 putc(opt->diffopt.line_termination, opt->diffopt.file);
733 strbuf_release(&msgbuf);
734 free(ctx.notes_message);
737 int log_tree_diff_flush(struct rev_info *opt)
739 opt->shown_dashes = 0;
740 diffcore_std(&opt->diffopt);
742 if (diff_queue_is_empty()) {
743 int saved_fmt = opt->diffopt.output_format;
744 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
745 diff_flush(&opt->diffopt);
746 opt->diffopt.output_format = saved_fmt;
747 return 0;
750 if (opt->loginfo && !opt->no_commit_id) {
751 show_log(opt);
752 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
753 opt->verbose_header &&
754 opt->commit_format != CMIT_FMT_ONELINE &&
755 !commit_format_is_empty(opt->commit_format)) {
757 * When showing a verbose header (i.e. log message),
758 * and not in --pretty=oneline format, we would want
759 * an extra newline between the end of log and the
760 * diff/diffstat output for readability.
762 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
763 if (opt->diffopt.output_prefix) {
764 struct strbuf *msg = NULL;
765 msg = opt->diffopt.output_prefix(&opt->diffopt,
766 opt->diffopt.output_prefix_data);
767 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
771 * We may have shown three-dashes line early
772 * between notes and the log message, in which
773 * case we only want a blank line after the
774 * notes without (an extra) three-dashes line.
775 * Otherwise, we show the three-dashes line if
776 * we are showing the patch with diffstat, but
777 * in that case, there is no extra blank line
778 * after the three-dashes line.
780 if (!opt->shown_dashes &&
781 (pch & opt->diffopt.output_format) == pch)
782 fprintf(opt->diffopt.file, "---");
783 putc('\n', opt->diffopt.file);
786 diff_flush(&opt->diffopt);
787 return 1;
790 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
792 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
793 return !opt->loginfo;
797 * Show the diff of a commit.
799 * Return true if we printed any log info messages
801 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
803 int showed_log;
804 struct commit_list *parents;
805 struct object_id *oid;
807 if (!opt->diff && !opt->diffopt.flags.exit_with_status)
808 return 0;
810 parse_commit_or_die(commit);
811 oid = get_commit_tree_oid(commit);
813 /* Root commit? */
814 parents = get_saved_parents(opt, commit);
815 if (!parents) {
816 if (opt->show_root_diff) {
817 diff_root_tree_oid(oid, "", &opt->diffopt);
818 log_tree_diff_flush(opt);
820 return !opt->loginfo;
823 /* More than one parent? */
824 if (parents && parents->next) {
825 if (opt->ignore_merges)
826 return 0;
827 else if (opt->combine_merges)
828 return do_diff_combined(opt, commit);
829 else if (opt->first_parent_only) {
831 * Generate merge log entry only for the first
832 * parent, showing summary diff of the others
833 * we merged _in_.
835 parse_commit_or_die(parents->item);
836 diff_tree_oid(get_commit_tree_oid(parents->item),
837 oid, "", &opt->diffopt);
838 log_tree_diff_flush(opt);
839 return !opt->loginfo;
842 /* If we show individual diffs, show the parent info */
843 log->parent = parents->item;
846 showed_log = 0;
847 for (;;) {
848 struct commit *parent = parents->item;
850 parse_commit_or_die(parent);
851 diff_tree_oid(get_commit_tree_oid(parent),
852 oid, "", &opt->diffopt);
853 log_tree_diff_flush(opt);
855 showed_log |= !opt->loginfo;
857 /* Set up the log info for the next parent, if any.. */
858 parents = parents->next;
859 if (!parents)
860 break;
861 log->parent = parents->item;
862 opt->loginfo = log;
864 return showed_log;
867 int log_tree_commit(struct rev_info *opt, struct commit *commit)
869 struct log_info log;
870 int shown, close_file = opt->diffopt.close_file;
872 log.commit = commit;
873 log.parent = NULL;
874 opt->loginfo = &log;
875 opt->diffopt.close_file = 0;
877 if (opt->line_level_traverse)
878 return line_log_print(opt, commit);
880 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
881 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
882 shown = log_tree_diff(opt, commit, &log);
883 if (!shown && opt->loginfo && opt->always_show_header) {
884 log.parent = NULL;
885 show_log(opt);
886 shown = 1;
888 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
889 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
890 opt->loginfo = NULL;
891 maybe_flush_or_die(opt->diffopt.file, "stdout");
892 if (close_file)
893 fclose(opt->diffopt.file);
894 return shown;