sh-setup: add new peel_committish() helper
[git] / log-tree.c
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"
14
15 struct decoration name_decoration = { "object names" };
16
17 enum decoration_type {
18         DECORATION_NONE = 0,
19         DECORATION_REF_LOCAL,
20         DECORATION_REF_REMOTE,
21         DECORATION_REF_TAG,
22         DECORATION_REF_STASH,
23         DECORATION_REF_HEAD,
24         DECORATION_GRAFTED,
25 };
26
27 static char decoration_colors[][COLOR_MAXLEN] = {
28         GIT_COLOR_RESET,
29         GIT_COLOR_BOLD_GREEN,   /* REF_LOCAL */
30         GIT_COLOR_BOLD_RED,     /* REF_REMOTE */
31         GIT_COLOR_BOLD_YELLOW,  /* REF_TAG */
32         GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
33         GIT_COLOR_BOLD_CYAN,    /* REF_HEAD */
34         GIT_COLOR_BOLD_BLUE,    /* GRAFTED */
35 };
36
37 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
38 {
39         if (want_color(decorate_use_color))
40                 return decoration_colors[ix];
41         return "";
42 }
43
44 static int parse_decorate_color_slot(const char *slot)
45 {
46         /*
47          * We're comparing with 'ignore-case' on
48          * (because config.c sets them all tolower),
49          * but let's match the letters in the literal
50          * string values here with how they are
51          * documented in Documentation/config.txt, for
52          * consistency.
53          *
54          * We love being consistent, don't we?
55          */
56         if (!strcasecmp(slot, "branch"))
57                 return DECORATION_REF_LOCAL;
58         if (!strcasecmp(slot, "remoteBranch"))
59                 return DECORATION_REF_REMOTE;
60         if (!strcasecmp(slot, "tag"))
61                 return DECORATION_REF_TAG;
62         if (!strcasecmp(slot, "stash"))
63                 return DECORATION_REF_STASH;
64         if (!strcasecmp(slot, "HEAD"))
65                 return DECORATION_REF_HEAD;
66         return -1;
67 }
68
69 int parse_decorate_color_config(const char *var, const int ofs, const char *value)
70 {
71         int slot = parse_decorate_color_slot(var + ofs);
72         if (slot < 0)
73                 return 0;
74         if (!value)
75                 return config_error_nonbool(var);
76         color_parse(value, var, decoration_colors[slot]);
77         return 0;
78 }
79
80 /*
81  * log-tree.c uses DIFF_OPT_TST for determining whether to use color
82  * for showing the commit sha1, use the same check for --decorate
83  */
84 #define decorate_get_color_opt(o, ix) \
85         decorate_get_color((o)->use_color, ix)
86
87 static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
88 {
89         int nlen = strlen(name);
90         struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
91         memcpy(res->name, name, nlen + 1);
92         res->type = type;
93         res->next = add_decoration(&name_decoration, obj, res);
94 }
95
96 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
97 {
98         struct object *obj;
99         enum decoration_type type = DECORATION_NONE;
100
101         if (!prefixcmp(refname, "refs/replace/")) {
102                 unsigned char original_sha1[20];
103                 if (!read_replace_refs)
104                         return 0;
105                 if (get_sha1_hex(refname + 13, original_sha1)) {
106                         warning("invalid replace ref %s", refname);
107                         return 0;
108                 }
109                 obj = parse_object(original_sha1);
110                 if (obj)
111                         add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
112                 return 0;
113         }
114
115         obj = parse_object(sha1);
116         if (!obj)
117                 return 0;
118
119         if (!prefixcmp(refname, "refs/heads/"))
120                 type = DECORATION_REF_LOCAL;
121         else if (!prefixcmp(refname, "refs/remotes/"))
122                 type = DECORATION_REF_REMOTE;
123         else if (!prefixcmp(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;
129
130         if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
131                 refname = prettify_refname(refname);
132         add_name_decoration(type, refname, obj);
133         while (obj->type == OBJ_TAG) {
134                 obj = ((struct tag *)obj)->tagged;
135                 if (!obj)
136                         break;
137                 add_name_decoration(DECORATION_REF_TAG, refname, obj);
138         }
139         return 0;
140 }
141
142 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
143 {
144         struct commit *commit = lookup_commit(graft->sha1);
145         if (!commit)
146                 return 0;
147         add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
148         return 0;
149 }
150
151 void load_ref_decorations(int flags)
152 {
153         static int loaded;
154         if (!loaded) {
155                 loaded = 1;
156                 for_each_ref(add_ref_decoration, &flags);
157                 head_ref(add_ref_decoration, &flags);
158                 for_each_commit_graft(add_graft_decoration, NULL);
159         }
160 }
161
162 static void show_parents(struct commit *commit, int abbrev)
163 {
164         struct commit_list *p;
165         for (p = commit->parents; p ; p = p->next) {
166                 struct commit *parent = p->item;
167                 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
168         }
169 }
170
171 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
172 {
173         struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
174         for ( ; p; p = p->next) {
175                 printf(" %s", find_unique_abbrev(p->item->object.sha1, abbrev));
176         }
177 }
178
179 /*
180  * The caller makes sure there is no funny color before
181  * calling. format_decorations makes sure the same after return.
182  */
183 void format_decorations(struct strbuf *sb,
184                         const struct commit *commit,
185                         int use_color)
186 {
187         const char *prefix;
188         struct name_decoration *decoration;
189         const char *color_commit =
190                 diff_get_color(use_color, DIFF_COMMIT);
191         const char *color_reset =
192                 decorate_get_color(use_color, DECORATION_NONE);
193
194         decoration = lookup_decoration(&name_decoration, &commit->object);
195         if (!decoration)
196                 return;
197         prefix = " (";
198         while (decoration) {
199                 strbuf_addstr(sb, color_commit);
200                 strbuf_addstr(sb, prefix);
201                 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
202                 if (decoration->type == DECORATION_REF_TAG)
203                         strbuf_addstr(sb, "tag: ");
204                 strbuf_addstr(sb, decoration->name);
205                 strbuf_addstr(sb, color_reset);
206                 prefix = ", ";
207                 decoration = decoration->next;
208         }
209         strbuf_addstr(sb, color_commit);
210         strbuf_addch(sb, ')');
211         strbuf_addstr(sb, color_reset);
212 }
213
214 void show_decorations(struct rev_info *opt, struct commit *commit)
215 {
216         struct strbuf sb = STRBUF_INIT;
217
218         if (opt->show_source && commit->util)
219                 printf("\t%s", (char *) commit->util);
220         if (!opt->show_decorations)
221                 return;
222         format_decorations(&sb, commit, opt->diffopt.use_color);
223         fputs(sb.buf, stdout);
224         strbuf_release(&sb);
225 }
226
227 static unsigned int digits_in_number(unsigned int number)
228 {
229         unsigned int i = 10, result = 1;
230         while (i <= number) {
231                 i *= 10;
232                 result++;
233         }
234         return result;
235 }
236
237 void fmt_output_subject(struct strbuf *filename,
238                         const char *subject,
239                         struct rev_info *info)
240 {
241         const char *suffix = info->patch_suffix;
242         int nr = info->nr;
243         int start_len = filename->len;
244         int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
245
246         if (0 < info->reroll_count)
247                 strbuf_addf(filename, "v%d-", info->reroll_count);
248         strbuf_addf(filename, "%04d-%s", nr, subject);
249
250         if (max_len < filename->len)
251                 strbuf_setlen(filename, max_len);
252         strbuf_addstr(filename, suffix);
253 }
254
255 void fmt_output_commit(struct strbuf *filename,
256                        struct commit *commit,
257                        struct rev_info *info)
258 {
259         struct pretty_print_context ctx = {0};
260         struct strbuf subject = STRBUF_INIT;
261
262         format_commit_message(commit, "%f", &subject, &ctx);
263         fmt_output_subject(filename, subject.buf, info);
264         strbuf_release(&subject);
265 }
266
267 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
268                              const char **subject_p,
269                              const char **extra_headers_p,
270                              int *need_8bit_cte_p)
271 {
272         const char *subject = NULL;
273         const char *extra_headers = opt->extra_headers;
274         const char *name = sha1_to_hex(commit->object.sha1);
275
276         *need_8bit_cte_p = 0; /* unknown */
277         if (opt->total > 0) {
278                 static char buffer[64];
279                 snprintf(buffer, sizeof(buffer),
280                          "Subject: [%s%s%0*d/%d] ",
281                          opt->subject_prefix,
282                          *opt->subject_prefix ? " " : "",
283                          digits_in_number(opt->total),
284                          opt->nr, opt->total);
285                 subject = buffer;
286         } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
287                 static char buffer[256];
288                 snprintf(buffer, sizeof(buffer),
289                          "Subject: [%s] ",
290                          opt->subject_prefix);
291                 subject = buffer;
292         } else {
293                 subject = "Subject: ";
294         }
295
296         printf("From %s Mon Sep 17 00:00:00 2001\n", name);
297         graph_show_oneline(opt->graph);
298         if (opt->message_id) {
299                 printf("Message-Id: <%s>\n", opt->message_id);
300                 graph_show_oneline(opt->graph);
301         }
302         if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
303                 int i, n;
304                 n = opt->ref_message_ids->nr;
305                 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
306                 for (i = 0; i < n; i++)
307                         printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
308                                opt->ref_message_ids->items[i].string);
309                 graph_show_oneline(opt->graph);
310         }
311         if (opt->mime_boundary) {
312                 static char subject_buffer[1024];
313                 static char buffer[1024];
314                 struct strbuf filename =  STRBUF_INIT;
315                 *need_8bit_cte_p = -1; /* NEVER */
316                 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
317                          "%s"
318                          "MIME-Version: 1.0\n"
319                          "Content-Type: multipart/mixed;"
320                          " boundary=\"%s%s\"\n"
321                          "\n"
322                          "This is a multi-part message in MIME "
323                          "format.\n"
324                          "--%s%s\n"
325                          "Content-Type: text/plain; "
326                          "charset=UTF-8; format=fixed\n"
327                          "Content-Transfer-Encoding: 8bit\n\n",
328                          extra_headers ? extra_headers : "",
329                          mime_boundary_leader, opt->mime_boundary,
330                          mime_boundary_leader, opt->mime_boundary);
331                 extra_headers = subject_buffer;
332
333                 if (opt->numbered_files)
334                         strbuf_addf(&filename, "%d", opt->nr);
335                 else
336                         fmt_output_commit(&filename, commit, opt);
337                 snprintf(buffer, sizeof(buffer) - 1,
338                          "\n--%s%s\n"
339                          "Content-Type: text/x-patch;"
340                          " name=\"%s\"\n"
341                          "Content-Transfer-Encoding: 8bit\n"
342                          "Content-Disposition: %s;"
343                          " filename=\"%s\"\n\n",
344                          mime_boundary_leader, opt->mime_boundary,
345                          filename.buf,
346                          opt->no_inline ? "attachment" : "inline",
347                          filename.buf);
348                 opt->diffopt.stat_sep = buffer;
349                 strbuf_release(&filename);
350         }
351         *subject_p = subject;
352         *extra_headers_p = extra_headers;
353 }
354
355 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
356 {
357         const char *color, *reset, *eol;
358
359         color = diff_get_color_opt(&opt->diffopt,
360                                    status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
361         reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
362         while (*bol) {
363                 eol = strchrnul(bol, '\n');
364                 printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
365                        *eol ? "\n" : "");
366                 bol = (*eol) ? (eol + 1) : eol;
367         }
368 }
369
370 static void show_signature(struct rev_info *opt, struct commit *commit)
371 {
372         struct strbuf payload = STRBUF_INIT;
373         struct strbuf signature = STRBUF_INIT;
374         struct strbuf gpg_output = STRBUF_INIT;
375         int status;
376
377         if (parse_signed_commit(commit->object.sha1, &payload, &signature) <= 0)
378                 goto out;
379
380         status = verify_signed_buffer(payload.buf, payload.len,
381                                       signature.buf, signature.len,
382                                       &gpg_output, NULL);
383         if (status && !gpg_output.len)
384                 strbuf_addstr(&gpg_output, "No signature\n");
385
386         show_sig_lines(opt, status, gpg_output.buf);
387
388  out:
389         strbuf_release(&gpg_output);
390         strbuf_release(&payload);
391         strbuf_release(&signature);
392 }
393
394 static int which_parent(const unsigned char *sha1, const struct commit *commit)
395 {
396         int nth;
397         const struct commit_list *parent;
398
399         for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
400                 if (!hashcmp(parent->item->object.sha1, sha1))
401                         return nth;
402                 nth++;
403         }
404         return -1;
405 }
406
407 static int is_common_merge(const struct commit *commit)
408 {
409         return (commit->parents
410                 && commit->parents->next
411                 && !commit->parents->next->next);
412 }
413
414 static void show_one_mergetag(struct rev_info *opt,
415                               struct commit_extra_header *extra,
416                               struct commit *commit)
417 {
418         unsigned char sha1[20];
419         struct tag *tag;
420         struct strbuf verify_message;
421         int status, nth;
422         size_t payload_size, gpg_message_offset;
423
424         hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
425         tag = lookup_tag(sha1);
426         if (!tag)
427                 return; /* error message already given */
428
429         strbuf_init(&verify_message, 256);
430         if (parse_tag_buffer(tag, extra->value, extra->len))
431                 strbuf_addstr(&verify_message, "malformed mergetag\n");
432         else if (is_common_merge(commit) &&
433                  !hashcmp(tag->tagged->sha1,
434                           commit->parents->next->item->object.sha1))
435                 strbuf_addf(&verify_message,
436                             "merged tag '%s'\n", tag->tag);
437         else if ((nth = which_parent(tag->tagged->sha1, commit)) < 0)
438                 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
439                                     tag->tag, tag->tagged->sha1);
440         else
441                 strbuf_addf(&verify_message,
442                             "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
443         gpg_message_offset = verify_message.len;
444
445         payload_size = parse_signature(extra->value, extra->len);
446         status = -1;
447         if (extra->len > payload_size)
448                 if (verify_signed_buffer(extra->value, payload_size,
449                                          extra->value + payload_size,
450                                          extra->len - payload_size,
451                                          &verify_message, NULL)) {
452                         if (verify_message.len <= gpg_message_offset)
453                                 strbuf_addstr(&verify_message, "No signature\n");
454                         else
455                                 status = 0;
456                 }
457
458         show_sig_lines(opt, status, verify_message.buf);
459         strbuf_release(&verify_message);
460 }
461
462 static void show_mergetag(struct rev_info *opt, struct commit *commit)
463 {
464         struct commit_extra_header *extra, *to_free;
465
466         to_free = read_commit_extra_headers(commit, NULL);
467         for (extra = to_free; extra; extra = extra->next) {
468                 if (strcmp(extra->key, "mergetag"))
469                         continue; /* not a merge tag */
470                 show_one_mergetag(opt, extra, commit);
471         }
472         free_commit_extra_headers(to_free);
473 }
474
475 void show_log(struct rev_info *opt)
476 {
477         struct strbuf msgbuf = STRBUF_INIT;
478         struct log_info *log = opt->loginfo;
479         struct commit *commit = log->commit, *parent = log->parent;
480         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
481         const char *extra_headers = opt->extra_headers;
482         struct pretty_print_context ctx = {0};
483
484         opt->loginfo = NULL;
485         if (!opt->verbose_header) {
486                 graph_show_commit(opt->graph);
487
488                 if (!opt->graph)
489                         put_revision_mark(opt, commit);
490                 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
491                 if (opt->print_parents)
492                         show_parents(commit, abbrev_commit);
493                 if (opt->children.name)
494                         show_children(opt, commit, abbrev_commit);
495                 show_decorations(opt, commit);
496                 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
497                         putchar('\n');
498                         graph_show_remainder(opt->graph);
499                 }
500                 putchar(opt->diffopt.line_termination);
501                 return;
502         }
503
504         /*
505          * If use_terminator is set, we already handled any record termination
506          * at the end of the last record.
507          * Otherwise, add a diffopt.line_termination character before all
508          * entries but the first.  (IOW, as a separator between entries)
509          */
510         if (opt->shown_one && !opt->use_terminator) {
511                 /*
512                  * If entries are separated by a newline, the output
513                  * should look human-readable.  If the last entry ended
514                  * with a newline, print the graph output before this
515                  * newline.  Otherwise it will end up as a completely blank
516                  * line and will look like a gap in the graph.
517                  *
518                  * If the entry separator is not a newline, the output is
519                  * primarily intended for programmatic consumption, and we
520                  * never want the extra graph output before the entry
521                  * separator.
522                  */
523                 if (opt->diffopt.line_termination == '\n' &&
524                     !opt->missing_newline)
525                         graph_show_padding(opt->graph);
526                 putchar(opt->diffopt.line_termination);
527         }
528         opt->shown_one = 1;
529
530         /*
531          * If the history graph was requested,
532          * print the graph, up to this commit's line
533          */
534         graph_show_commit(opt->graph);
535
536         /*
537          * Print header line of header..
538          */
539
540         if (opt->commit_format == CMIT_FMT_EMAIL) {
541                 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
542                                         &ctx.need_8bit_cte);
543         } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
544                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
545                 if (opt->commit_format != CMIT_FMT_ONELINE)
546                         fputs("commit ", stdout);
547
548                 if (!opt->graph)
549                         put_revision_mark(opt, commit);
550                 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
551                       stdout);
552                 if (opt->print_parents)
553                         show_parents(commit, abbrev_commit);
554                 if (opt->children.name)
555                         show_children(opt, commit, abbrev_commit);
556                 if (parent)
557                         printf(" (from %s)",
558                                find_unique_abbrev(parent->object.sha1,
559                                                   abbrev_commit));
560                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
561                 show_decorations(opt, commit);
562                 if (opt->commit_format == CMIT_FMT_ONELINE) {
563                         putchar(' ');
564                 } else {
565                         putchar('\n');
566                         graph_show_oneline(opt->graph);
567                 }
568                 if (opt->reflog_info) {
569                         /*
570                          * setup_revisions() ensures that opt->reflog_info
571                          * and opt->graph cannot both be set,
572                          * so we don't need to worry about printing the
573                          * graph info here.
574                          */
575                         show_reflog_message(opt->reflog_info,
576                                             opt->commit_format == CMIT_FMT_ONELINE,
577                                             opt->date_mode,
578                                             opt->date_mode_explicit);
579                         if (opt->commit_format == CMIT_FMT_ONELINE)
580                                 return;
581                 }
582         }
583
584         if (opt->show_signature) {
585                 show_signature(opt, commit);
586                 show_mergetag(opt, commit);
587         }
588
589         if (!commit->buffer)
590                 return;
591
592         if (opt->show_notes) {
593                 int raw;
594                 struct strbuf notebuf = STRBUF_INIT;
595
596                 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
597                 format_display_notes(commit->object.sha1, &notebuf,
598                                      get_log_output_encoding(), raw);
599                 ctx.notes_message = notebuf.len
600                         ? strbuf_detach(&notebuf, NULL)
601                         : xcalloc(1, 1);
602         }
603
604         /*
605          * And then the pretty-printed message itself
606          */
607         if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
608                 ctx.need_8bit_cte =
609                         has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
610                                                getenv("GIT_COMMITTER_EMAIL")));
611         ctx.date_mode = opt->date_mode;
612         ctx.date_mode_explicit = opt->date_mode_explicit;
613         ctx.abbrev = opt->diffopt.abbrev;
614         ctx.after_subject = extra_headers;
615         ctx.preserve_subject = opt->preserve_subject;
616         ctx.reflog_info = opt->reflog_info;
617         ctx.fmt = opt->commit_format;
618         ctx.mailmap = opt->mailmap;
619         ctx.color = opt->diffopt.use_color;
620         pretty_print_commit(&ctx, commit, &msgbuf);
621
622         if (opt->add_signoff)
623                 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
624
625         if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
626             ctx.notes_message && *ctx.notes_message) {
627                 if (ctx.fmt == CMIT_FMT_EMAIL) {
628                         strbuf_addstr(&msgbuf, "---\n");
629                         opt->shown_dashes = 1;
630                 }
631                 strbuf_addstr(&msgbuf, ctx.notes_message);
632         }
633
634         if (opt->show_log_size) {
635                 printf("log size %i\n", (int)msgbuf.len);
636                 graph_show_oneline(opt->graph);
637         }
638
639         /*
640          * Set opt->missing_newline if msgbuf doesn't
641          * end in a newline (including if it is empty)
642          */
643         if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
644                 opt->missing_newline = 1;
645         else
646                 opt->missing_newline = 0;
647
648         if (opt->graph)
649                 graph_show_commit_msg(opt->graph, &msgbuf);
650         else
651                 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
652         if (opt->use_terminator) {
653                 if (!opt->missing_newline)
654                         graph_show_padding(opt->graph);
655                 putchar(opt->diffopt.line_termination);
656         }
657
658         strbuf_release(&msgbuf);
659         free(ctx.notes_message);
660 }
661
662 int log_tree_diff_flush(struct rev_info *opt)
663 {
664         opt->shown_dashes = 0;
665         diffcore_std(&opt->diffopt);
666
667         if (diff_queue_is_empty()) {
668                 int saved_fmt = opt->diffopt.output_format;
669                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
670                 diff_flush(&opt->diffopt);
671                 opt->diffopt.output_format = saved_fmt;
672                 return 0;
673         }
674
675         if (opt->loginfo && !opt->no_commit_id) {
676                 show_log(opt);
677                 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
678                     opt->verbose_header &&
679                     opt->commit_format != CMIT_FMT_ONELINE) {
680                         /*
681                          * When showing a verbose header (i.e. log message),
682                          * and not in --pretty=oneline format, we would want
683                          * an extra newline between the end of log and the
684                          * diff/diffstat output for readability.
685                          */
686                         int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
687                         if (opt->diffopt.output_prefix) {
688                                 struct strbuf *msg = NULL;
689                                 msg = opt->diffopt.output_prefix(&opt->diffopt,
690                                         opt->diffopt.output_prefix_data);
691                                 fwrite(msg->buf, msg->len, 1, stdout);
692                         }
693
694                         /*
695                          * We may have shown three-dashes line early
696                          * between notes and the log message, in which
697                          * case we only want a blank line after the
698                          * notes without (an extra) three-dashes line.
699                          * Otherwise, we show the three-dashes line if
700                          * we are showing the patch with diffstat, but
701                          * in that case, there is no extra blank line
702                          * after the three-dashes line.
703                          */
704                         if (!opt->shown_dashes &&
705                             (pch & opt->diffopt.output_format) == pch)
706                                 printf("---");
707                         putchar('\n');
708                 }
709         }
710         diff_flush(&opt->diffopt);
711         return 1;
712 }
713
714 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
715 {
716         diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
717         return !opt->loginfo;
718 }
719
720 /*
721  * Show the diff of a commit.
722  *
723  * Return true if we printed any log info messages
724  */
725 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
726 {
727         int showed_log;
728         struct commit_list *parents;
729         unsigned const char *sha1;
730
731         if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
732                 return 0;
733
734         parse_commit(commit);
735         sha1 = commit->tree->object.sha1;
736
737         /* Root commit? */
738         parents = commit->parents;
739         if (!parents) {
740                 if (opt->show_root_diff) {
741                         diff_root_tree_sha1(sha1, "", &opt->diffopt);
742                         log_tree_diff_flush(opt);
743                 }
744                 return !opt->loginfo;
745         }
746
747         /* More than one parent? */
748         if (parents && parents->next) {
749                 if (opt->ignore_merges)
750                         return 0;
751                 else if (opt->combine_merges)
752                         return do_diff_combined(opt, commit);
753                 else if (opt->first_parent_only) {
754                         /*
755                          * Generate merge log entry only for the first
756                          * parent, showing summary diff of the others
757                          * we merged _in_.
758                          */
759                         parse_commit(parents->item);
760                         diff_tree_sha1(parents->item->tree->object.sha1,
761                                        sha1, "", &opt->diffopt);
762                         log_tree_diff_flush(opt);
763                         return !opt->loginfo;
764                 }
765
766                 /* If we show individual diffs, show the parent info */
767                 log->parent = parents->item;
768         }
769
770         showed_log = 0;
771         for (;;) {
772                 struct commit *parent = parents->item;
773
774                 parse_commit(parent);
775                 diff_tree_sha1(parent->tree->object.sha1,
776                                sha1, "", &opt->diffopt);
777                 log_tree_diff_flush(opt);
778
779                 showed_log |= !opt->loginfo;
780
781                 /* Set up the log info for the next parent, if any.. */
782                 parents = parents->next;
783                 if (!parents)
784                         break;
785                 log->parent = parents->item;
786                 opt->loginfo = log;
787         }
788         return showed_log;
789 }
790
791 int log_tree_commit(struct rev_info *opt, struct commit *commit)
792 {
793         struct log_info log;
794         int shown;
795
796         log.commit = commit;
797         log.parent = NULL;
798         opt->loginfo = &log;
799
800         if (opt->line_level_traverse)
801                 return line_log_print(opt, commit);
802
803         shown = log_tree_diff(opt, commit, &log);
804         if (!shown && opt->loginfo && opt->always_show_header) {
805                 log.parent = NULL;
806                 show_log(opt);
807                 shown = 1;
808         }
809         opt->loginfo = NULL;
810         maybe_flush_or_die(stdout, "stdout");
811         return shown;
812 }