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