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