Third batch for 2.8 cycle
[git/debian.git] / log-tree.c
blobf70a30e12702fa68c77f6b2490692f3861ad32e7
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "graph.h"
6 #include "log-tree.h"
7 #include "reflog-walk.h"
8 #include "refs.h"
9 #include "string-list.h"
10 #include "color.h"
11 #include "gpg-interface.h"
12 #include "sequencer.h"
13 #include "line-log.h"
15 static struct decoration name_decoration = { "object names" };
16 static int decoration_loaded;
17 static int decoration_flags;
19 static char decoration_colors[][COLOR_MAXLEN] = {
20 GIT_COLOR_RESET,
21 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
22 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
23 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
24 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
25 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
26 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
29 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
31 if (want_color(decorate_use_color))
32 return decoration_colors[ix];
33 return "";
36 static int parse_decorate_color_slot(const char *slot)
39 * We're comparing with 'ignore-case' on
40 * (because config.c sets them all tolower),
41 * but let's match the letters in the literal
42 * string values here with how they are
43 * documented in Documentation/config.txt, for
44 * consistency.
46 * We love being consistent, don't we?
48 if (!strcasecmp(slot, "branch"))
49 return DECORATION_REF_LOCAL;
50 if (!strcasecmp(slot, "remoteBranch"))
51 return DECORATION_REF_REMOTE;
52 if (!strcasecmp(slot, "tag"))
53 return DECORATION_REF_TAG;
54 if (!strcasecmp(slot, "stash"))
55 return DECORATION_REF_STASH;
56 if (!strcasecmp(slot, "HEAD"))
57 return DECORATION_REF_HEAD;
58 return -1;
61 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
63 int slot = parse_decorate_color_slot(slot_name);
64 if (slot < 0)
65 return 0;
66 if (!value)
67 return config_error_nonbool(var);
68 return color_parse(value, decoration_colors[slot]);
72 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
73 * for showing the commit sha1, use the same check for --decorate
75 #define decorate_get_color_opt(o, ix) \
76 decorate_get_color((o)->use_color, ix)
78 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
80 int nlen = strlen(name);
81 struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
82 memcpy(res->name, name, nlen + 1);
83 res->type = type;
84 res->next = add_decoration(&name_decoration, obj, res);
87 const struct name_decoration *get_name_decoration(const struct object *obj)
89 return lookup_decoration(&name_decoration, obj);
92 static int add_ref_decoration(const char *refname, const 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.hash);
110 if (obj)
111 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
112 return 0;
115 obj = parse_object(oid->hash);
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.hash);
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.hash);
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)
165 struct commit_list *p;
166 for (p = commit->parents; p ; p = p->next) {
167 struct commit *parent = p->item;
168 printf(" %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 printf(" %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 unsigned char unused[20];
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, &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, color_reset);
268 strbuf_addstr(sb, color_commit);
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);
276 prefix = separator;
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;
289 if (opt->show_source && commit->util)
290 printf("\t%s", (char *) commit->util);
291 if (!opt->show_decorations)
292 return;
293 format_decorations(&sb, commit, opt->diffopt.use_color);
294 fputs(sb.buf, stdout);
295 strbuf_release(&sb);
298 static unsigned int digits_in_number(unsigned int number)
300 unsigned int i = 10, result = 1;
301 while (i <= number) {
302 i *= 10;
303 result++;
305 return result;
308 void fmt_output_subject(struct strbuf *filename,
309 const char *subject,
310 struct rev_info *info)
312 const char *suffix = info->patch_suffix;
313 int nr = info->nr;
314 int start_len = filename->len;
315 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
317 if (0 < info->reroll_count)
318 strbuf_addf(filename, "v%d-", info->reroll_count);
319 strbuf_addf(filename, "%04d-%s", nr, subject);
321 if (max_len < filename->len)
322 strbuf_setlen(filename, max_len);
323 strbuf_addstr(filename, suffix);
326 void fmt_output_commit(struct strbuf *filename,
327 struct commit *commit,
328 struct rev_info *info)
330 struct pretty_print_context ctx = {0};
331 struct strbuf subject = STRBUF_INIT;
333 format_commit_message(commit, "%f", &subject, &ctx);
334 fmt_output_subject(filename, subject.buf, info);
335 strbuf_release(&subject);
338 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
339 const char **subject_p,
340 const char **extra_headers_p,
341 int *need_8bit_cte_p)
343 const char *subject = NULL;
344 const char *extra_headers = opt->extra_headers;
345 const char *name = oid_to_hex(opt->zero_commit ?
346 &null_oid : &commit->object.oid);
348 *need_8bit_cte_p = 0; /* unknown */
349 if (opt->total > 0) {
350 static char buffer[64];
351 snprintf(buffer, sizeof(buffer),
352 "Subject: [%s%s%0*d/%d] ",
353 opt->subject_prefix,
354 *opt->subject_prefix ? " " : "",
355 digits_in_number(opt->total),
356 opt->nr, opt->total);
357 subject = buffer;
358 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
359 static char buffer[256];
360 snprintf(buffer, sizeof(buffer),
361 "Subject: [%s] ",
362 opt->subject_prefix);
363 subject = buffer;
364 } else {
365 subject = "Subject: ";
368 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
369 graph_show_oneline(opt->graph);
370 if (opt->message_id) {
371 printf("Message-Id: <%s>\n", opt->message_id);
372 graph_show_oneline(opt->graph);
374 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
375 int i, n;
376 n = opt->ref_message_ids->nr;
377 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
378 for (i = 0; i < n; i++)
379 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
380 opt->ref_message_ids->items[i].string);
381 graph_show_oneline(opt->graph);
383 if (opt->mime_boundary) {
384 static char subject_buffer[1024];
385 static char buffer[1024];
386 struct strbuf filename = STRBUF_INIT;
387 *need_8bit_cte_p = -1; /* NEVER */
388 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
389 "%s"
390 "MIME-Version: 1.0\n"
391 "Content-Type: multipart/mixed;"
392 " boundary=\"%s%s\"\n"
393 "\n"
394 "This is a multi-part message in MIME "
395 "format.\n"
396 "--%s%s\n"
397 "Content-Type: text/plain; "
398 "charset=UTF-8; format=fixed\n"
399 "Content-Transfer-Encoding: 8bit\n\n",
400 extra_headers ? extra_headers : "",
401 mime_boundary_leader, opt->mime_boundary,
402 mime_boundary_leader, opt->mime_boundary);
403 extra_headers = subject_buffer;
405 if (opt->numbered_files)
406 strbuf_addf(&filename, "%d", opt->nr);
407 else
408 fmt_output_commit(&filename, commit, opt);
409 snprintf(buffer, sizeof(buffer) - 1,
410 "\n--%s%s\n"
411 "Content-Type: text/x-patch;"
412 " name=\"%s\"\n"
413 "Content-Transfer-Encoding: 8bit\n"
414 "Content-Disposition: %s;"
415 " filename=\"%s\"\n\n",
416 mime_boundary_leader, opt->mime_boundary,
417 filename.buf,
418 opt->no_inline ? "attachment" : "inline",
419 filename.buf);
420 opt->diffopt.stat_sep = buffer;
421 strbuf_release(&filename);
423 *subject_p = subject;
424 *extra_headers_p = extra_headers;
427 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
429 const char *color, *reset, *eol;
431 color = diff_get_color_opt(&opt->diffopt,
432 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
433 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
434 while (*bol) {
435 eol = strchrnul(bol, '\n');
436 printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
437 *eol ? "\n" : "");
438 graph_show_oneline(opt->graph);
439 bol = (*eol) ? (eol + 1) : eol;
443 static void show_signature(struct rev_info *opt, struct commit *commit)
445 struct strbuf payload = STRBUF_INIT;
446 struct strbuf signature = STRBUF_INIT;
447 struct strbuf gpg_output = STRBUF_INIT;
448 int status;
450 if (parse_signed_commit(commit, &payload, &signature) <= 0)
451 goto out;
453 status = verify_signed_buffer(payload.buf, payload.len,
454 signature.buf, signature.len,
455 &gpg_output, NULL);
456 if (status && !gpg_output.len)
457 strbuf_addstr(&gpg_output, "No signature\n");
459 show_sig_lines(opt, status, gpg_output.buf);
461 out:
462 strbuf_release(&gpg_output);
463 strbuf_release(&payload);
464 strbuf_release(&signature);
467 static int which_parent(const unsigned char *sha1, const struct commit *commit)
469 int nth;
470 const struct commit_list *parent;
472 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
473 if (!hashcmp(parent->item->object.oid.hash, sha1))
474 return nth;
475 nth++;
477 return -1;
480 static int is_common_merge(const struct commit *commit)
482 return (commit->parents
483 && commit->parents->next
484 && !commit->parents->next->next);
487 static void show_one_mergetag(struct commit *commit,
488 struct commit_extra_header *extra,
489 void *data)
491 struct rev_info *opt = (struct rev_info *)data;
492 unsigned char sha1[20];
493 struct tag *tag;
494 struct strbuf verify_message;
495 int status, nth;
496 size_t payload_size, gpg_message_offset;
498 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
499 tag = lookup_tag(sha1);
500 if (!tag)
501 return; /* error message already given */
503 strbuf_init(&verify_message, 256);
504 if (parse_tag_buffer(tag, extra->value, extra->len))
505 strbuf_addstr(&verify_message, "malformed mergetag\n");
506 else if (is_common_merge(commit) &&
507 !oidcmp(&tag->tagged->oid,
508 &commit->parents->next->item->object.oid))
509 strbuf_addf(&verify_message,
510 "merged tag '%s'\n", tag->tag);
511 else if ((nth = which_parent(tag->tagged->oid.hash, commit)) < 0)
512 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
513 tag->tag, tag->tagged->oid.hash);
514 else
515 strbuf_addf(&verify_message,
516 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
517 gpg_message_offset = verify_message.len;
519 payload_size = parse_signature(extra->value, extra->len);
520 status = -1;
521 if (extra->len > payload_size) {
522 /* could have a good signature */
523 if (!verify_signed_buffer(extra->value, payload_size,
524 extra->value + payload_size,
525 extra->len - payload_size,
526 &verify_message, NULL))
527 status = 0; /* good */
528 else if (verify_message.len <= gpg_message_offset)
529 strbuf_addstr(&verify_message, "No signature\n");
530 /* otherwise we couldn't verify, which is shown as bad */
533 show_sig_lines(opt, status, verify_message.buf);
534 strbuf_release(&verify_message);
537 static void show_mergetag(struct rev_info *opt, struct commit *commit)
539 for_each_mergetag(show_one_mergetag, commit, opt);
542 void show_log(struct rev_info *opt)
544 struct strbuf msgbuf = STRBUF_INIT;
545 struct log_info *log = opt->loginfo;
546 struct commit *commit = log->commit, *parent = log->parent;
547 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
548 const char *extra_headers = opt->extra_headers;
549 struct pretty_print_context ctx = {0};
551 opt->loginfo = NULL;
552 if (!opt->verbose_header) {
553 graph_show_commit(opt->graph);
555 if (!opt->graph)
556 put_revision_mark(opt, commit);
557 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), stdout);
558 if (opt->print_parents)
559 show_parents(commit, abbrev_commit);
560 if (opt->children.name)
561 show_children(opt, commit, abbrev_commit);
562 show_decorations(opt, commit);
563 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
564 putchar('\n');
565 graph_show_remainder(opt->graph);
567 putchar(opt->diffopt.line_termination);
568 return;
572 * If use_terminator is set, we already handled any record termination
573 * at the end of the last record.
574 * Otherwise, add a diffopt.line_termination character before all
575 * entries but the first. (IOW, as a separator between entries)
577 if (opt->shown_one && !opt->use_terminator) {
579 * If entries are separated by a newline, the output
580 * should look human-readable. If the last entry ended
581 * with a newline, print the graph output before this
582 * newline. Otherwise it will end up as a completely blank
583 * line and will look like a gap in the graph.
585 * If the entry separator is not a newline, the output is
586 * primarily intended for programmatic consumption, and we
587 * never want the extra graph output before the entry
588 * separator.
590 if (opt->diffopt.line_termination == '\n' &&
591 !opt->missing_newline)
592 graph_show_padding(opt->graph);
593 putchar(opt->diffopt.line_termination);
595 opt->shown_one = 1;
598 * If the history graph was requested,
599 * print the graph, up to this commit's line
601 graph_show_commit(opt->graph);
604 * Print header line of header..
607 if (opt->commit_format == CMIT_FMT_EMAIL) {
608 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
609 &ctx.need_8bit_cte);
610 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
611 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
612 if (opt->commit_format != CMIT_FMT_ONELINE)
613 fputs("commit ", stdout);
615 if (!opt->graph)
616 put_revision_mark(opt, commit);
617 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
618 stdout);
619 if (opt->print_parents)
620 show_parents(commit, abbrev_commit);
621 if (opt->children.name)
622 show_children(opt, commit, abbrev_commit);
623 if (parent)
624 printf(" (from %s)",
625 find_unique_abbrev(parent->object.oid.hash,
626 abbrev_commit));
627 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
628 show_decorations(opt, commit);
629 if (opt->commit_format == CMIT_FMT_ONELINE) {
630 putchar(' ');
631 } else {
632 putchar('\n');
633 graph_show_oneline(opt->graph);
635 if (opt->reflog_info) {
637 * setup_revisions() ensures that opt->reflog_info
638 * and opt->graph cannot both be set,
639 * so we don't need to worry about printing the
640 * graph info here.
642 show_reflog_message(opt->reflog_info,
643 opt->commit_format == CMIT_FMT_ONELINE,
644 &opt->date_mode,
645 opt->date_mode_explicit);
646 if (opt->commit_format == CMIT_FMT_ONELINE)
647 return;
651 if (opt->show_signature) {
652 show_signature(opt, commit);
653 show_mergetag(opt, commit);
656 if (!get_cached_commit_buffer(commit, NULL))
657 return;
659 if (opt->show_notes) {
660 int raw;
661 struct strbuf notebuf = STRBUF_INIT;
663 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
664 format_display_notes(commit->object.oid.hash, &notebuf,
665 get_log_output_encoding(), raw);
666 ctx.notes_message = notebuf.len
667 ? strbuf_detach(&notebuf, NULL)
668 : xcalloc(1, 1);
672 * And then the pretty-printed message itself
674 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
675 ctx.need_8bit_cte =
676 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
677 getenv("GIT_COMMITTER_EMAIL")));
678 ctx.date_mode = opt->date_mode;
679 ctx.date_mode_explicit = opt->date_mode_explicit;
680 ctx.abbrev = opt->diffopt.abbrev;
681 ctx.after_subject = extra_headers;
682 ctx.preserve_subject = opt->preserve_subject;
683 ctx.reflog_info = opt->reflog_info;
684 ctx.fmt = opt->commit_format;
685 ctx.mailmap = opt->mailmap;
686 ctx.color = opt->diffopt.use_color;
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 pretty_print_commit(&ctx, commit, &msgbuf);
692 if (opt->add_signoff)
693 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
695 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
696 ctx.notes_message && *ctx.notes_message) {
697 if (ctx.fmt == CMIT_FMT_EMAIL) {
698 strbuf_addstr(&msgbuf, "---\n");
699 opt->shown_dashes = 1;
701 strbuf_addstr(&msgbuf, ctx.notes_message);
704 if (opt->show_log_size) {
705 printf("log size %i\n", (int)msgbuf.len);
706 graph_show_oneline(opt->graph);
710 * Set opt->missing_newline if msgbuf doesn't
711 * end in a newline (including if it is empty)
713 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
714 opt->missing_newline = 1;
715 else
716 opt->missing_newline = 0;
718 if (opt->graph)
719 graph_show_commit_msg(opt->graph, &msgbuf);
720 else
721 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
722 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
723 if (!opt->missing_newline)
724 graph_show_padding(opt->graph);
725 putchar(opt->diffopt.line_termination);
728 strbuf_release(&msgbuf);
729 free(ctx.notes_message);
732 int log_tree_diff_flush(struct rev_info *opt)
734 opt->shown_dashes = 0;
735 diffcore_std(&opt->diffopt);
737 if (diff_queue_is_empty()) {
738 int saved_fmt = opt->diffopt.output_format;
739 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
740 diff_flush(&opt->diffopt);
741 opt->diffopt.output_format = saved_fmt;
742 return 0;
745 if (opt->loginfo && !opt->no_commit_id) {
746 show_log(opt);
747 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
748 opt->verbose_header &&
749 opt->commit_format != CMIT_FMT_ONELINE &&
750 !commit_format_is_empty(opt->commit_format)) {
752 * When showing a verbose header (i.e. log message),
753 * and not in --pretty=oneline format, we would want
754 * an extra newline between the end of log and the
755 * diff/diffstat output for readability.
757 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
758 if (opt->diffopt.output_prefix) {
759 struct strbuf *msg = NULL;
760 msg = opt->diffopt.output_prefix(&opt->diffopt,
761 opt->diffopt.output_prefix_data);
762 fwrite(msg->buf, msg->len, 1, stdout);
766 * We may have shown three-dashes line early
767 * between notes and the log message, in which
768 * case we only want a blank line after the
769 * notes without (an extra) three-dashes line.
770 * Otherwise, we show the three-dashes line if
771 * we are showing the patch with diffstat, but
772 * in that case, there is no extra blank line
773 * after the three-dashes line.
775 if (!opt->shown_dashes &&
776 (pch & opt->diffopt.output_format) == pch)
777 printf("---");
778 putchar('\n');
781 diff_flush(&opt->diffopt);
782 return 1;
785 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
787 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
788 return !opt->loginfo;
792 * Show the diff of a commit.
794 * Return true if we printed any log info messages
796 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
798 int showed_log;
799 struct commit_list *parents;
800 struct object_id *oid;
802 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
803 return 0;
805 parse_commit_or_die(commit);
806 oid = &commit->tree->object.oid;
808 /* Root commit? */
809 parents = get_saved_parents(opt, commit);
810 if (!parents) {
811 if (opt->show_root_diff) {
812 diff_root_tree_sha1(oid->hash, "", &opt->diffopt);
813 log_tree_diff_flush(opt);
815 return !opt->loginfo;
818 /* More than one parent? */
819 if (parents && parents->next) {
820 if (opt->ignore_merges)
821 return 0;
822 else if (opt->combine_merges)
823 return do_diff_combined(opt, commit);
824 else if (opt->first_parent_only) {
826 * Generate merge log entry only for the first
827 * parent, showing summary diff of the others
828 * we merged _in_.
830 parse_commit_or_die(parents->item);
831 diff_tree_sha1(parents->item->tree->object.oid.hash,
832 oid->hash, "", &opt->diffopt);
833 log_tree_diff_flush(opt);
834 return !opt->loginfo;
837 /* If we show individual diffs, show the parent info */
838 log->parent = parents->item;
841 showed_log = 0;
842 for (;;) {
843 struct commit *parent = parents->item;
845 parse_commit_or_die(parent);
846 diff_tree_sha1(parent->tree->object.oid.hash,
847 oid->hash, "", &opt->diffopt);
848 log_tree_diff_flush(opt);
850 showed_log |= !opt->loginfo;
852 /* Set up the log info for the next parent, if any.. */
853 parents = parents->next;
854 if (!parents)
855 break;
856 log->parent = parents->item;
857 opt->loginfo = log;
859 return showed_log;
862 int log_tree_commit(struct rev_info *opt, struct commit *commit)
864 struct log_info log;
865 int shown;
867 log.commit = commit;
868 log.parent = NULL;
869 opt->loginfo = &log;
871 if (opt->line_level_traverse)
872 return line_log_print(opt, commit);
874 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
875 printf("\n%s\n", opt->break_bar);
876 shown = log_tree_diff(opt, commit, &log);
877 if (!shown && opt->loginfo && opt->always_show_header) {
878 log.parent = NULL;
879 show_log(opt);
880 shown = 1;
882 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
883 printf("\n%s\n", opt->break_bar);
884 opt->loginfo = NULL;
885 maybe_flush_or_die(stdout, "stdout");
886 return shown;