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