gpg-interface: prefer check_signature() for GPG verification
[git] / log-tree.c
1 #include "cache.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "object-store.h"
5 #include "repository.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "graph.h"
9 #include "log-tree.h"
10 #include "reflog-walk.h"
11 #include "refs.h"
12 #include "string-list.h"
13 #include "color.h"
14 #include "gpg-interface.h"
15 #include "sequencer.h"
16 #include "line-log.h"
17 #include "help.h"
18 #include "interdiff.h"
19 #include "range-diff.h"
20
21 static struct decoration name_decoration = { "object names" };
22 static int decoration_loaded;
23 static int decoration_flags;
24
25 static char decoration_colors[][COLOR_MAXLEN] = {
26         GIT_COLOR_RESET,
27         GIT_COLOR_BOLD_GREEN,   /* REF_LOCAL */
28         GIT_COLOR_BOLD_RED,     /* REF_REMOTE */
29         GIT_COLOR_BOLD_YELLOW,  /* REF_TAG */
30         GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
31         GIT_COLOR_BOLD_CYAN,    /* REF_HEAD */
32         GIT_COLOR_BOLD_BLUE,    /* GRAFTED */
33 };
34
35 static const char *color_decorate_slots[] = {
36         [DECORATION_REF_LOCAL]  = "branch",
37         [DECORATION_REF_REMOTE] = "remoteBranch",
38         [DECORATION_REF_TAG]    = "tag",
39         [DECORATION_REF_STASH]  = "stash",
40         [DECORATION_REF_HEAD]   = "HEAD",
41         [DECORATION_GRAFTED]    = "grafted",
42 };
43
44 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
45 {
46         if (want_color(decorate_use_color))
47                 return decoration_colors[ix];
48         return "";
49 }
50
51 define_list_config_array(color_decorate_slots);
52
53 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
54 {
55         int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
56         if (slot < 0)
57                 return 0;
58         if (!value)
59                 return config_error_nonbool(var);
60         return color_parse(value, decoration_colors[slot]);
61 }
62
63 /*
64  * log-tree.c uses DIFF_OPT_TST for determining whether to use color
65  * for showing the commit sha1, use the same check for --decorate
66  */
67 #define decorate_get_color_opt(o, ix) \
68         decorate_get_color((o)->use_color, ix)
69
70 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
71 {
72         struct name_decoration *res;
73         FLEX_ALLOC_STR(res, name, name);
74         res->type = type;
75         res->next = add_decoration(&name_decoration, obj, res);
76 }
77
78 const struct name_decoration *get_name_decoration(const struct object *obj)
79 {
80         return lookup_decoration(&name_decoration, obj);
81 }
82
83 static int add_ref_decoration(const char *refname, const struct object_id *oid,
84                               int flags, void *cb_data)
85 {
86         struct object *obj;
87         enum decoration_type type = DECORATION_NONE;
88         struct decoration_filter *filter = (struct decoration_filter *)cb_data;
89
90         if (filter && !ref_filter_match(refname,
91                               filter->include_ref_pattern,
92                               filter->exclude_ref_pattern))
93                 return 0;
94
95         if (starts_with(refname, git_replace_ref_base)) {
96                 struct object_id original_oid;
97                 if (!read_replace_refs)
98                         return 0;
99                 if (get_oid_hex(refname + strlen(git_replace_ref_base),
100                                 &original_oid)) {
101                         warning("invalid replace ref %s", refname);
102                         return 0;
103                 }
104                 obj = parse_object(the_repository, &original_oid);
105                 if (obj)
106                         add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
107                 return 0;
108         }
109
110         obj = parse_object(the_repository, oid);
111         if (!obj)
112                 return 0;
113
114         if (starts_with(refname, "refs/heads/"))
115                 type = DECORATION_REF_LOCAL;
116         else if (starts_with(refname, "refs/remotes/"))
117                 type = DECORATION_REF_REMOTE;
118         else if (starts_with(refname, "refs/tags/"))
119                 type = DECORATION_REF_TAG;
120         else if (!strcmp(refname, "refs/stash"))
121                 type = DECORATION_REF_STASH;
122         else if (!strcmp(refname, "HEAD"))
123                 type = DECORATION_REF_HEAD;
124
125         add_name_decoration(type, refname, obj);
126         while (obj->type == OBJ_TAG) {
127                 obj = ((struct tag *)obj)->tagged;
128                 if (!obj)
129                         break;
130                 if (!obj->parsed)
131                         parse_object(the_repository, &obj->oid);
132                 add_name_decoration(DECORATION_REF_TAG, refname, obj);
133         }
134         return 0;
135 }
136
137 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
138 {
139         struct commit *commit = lookup_commit(the_repository, &graft->oid);
140         if (!commit)
141                 return 0;
142         add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
143         return 0;
144 }
145
146 void load_ref_decorations(struct decoration_filter *filter, int flags)
147 {
148         if (!decoration_loaded) {
149                 if (filter) {
150                         struct string_list_item *item;
151                         for_each_string_list_item(item, filter->exclude_ref_pattern) {
152                                 normalize_glob_ref(item, NULL, item->string);
153                         }
154                         for_each_string_list_item(item, filter->include_ref_pattern) {
155                                 normalize_glob_ref(item, NULL, item->string);
156                         }
157                 }
158                 decoration_loaded = 1;
159                 decoration_flags = flags;
160                 for_each_ref(add_ref_decoration, filter);
161                 head_ref(add_ref_decoration, filter);
162                 for_each_commit_graft(add_graft_decoration, filter);
163         }
164 }
165
166 static void show_parents(struct commit *commit, int abbrev, FILE *file)
167 {
168         struct commit_list *p;
169         for (p = commit->parents; p ; p = p->next) {
170                 struct commit *parent = p->item;
171                 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
172         }
173 }
174
175 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
176 {
177         struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
178         for ( ; p; p = p->next) {
179                 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
180         }
181 }
182
183 /*
184  * Do we have HEAD in the output, and also the branch it points at?
185  * If so, find that decoration entry for that current branch.
186  */
187 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
188 {
189         const struct name_decoration *list, *head = NULL;
190         const char *branch_name = NULL;
191         int rru_flags;
192
193         /* First find HEAD */
194         for (list = decoration; list; list = list->next)
195                 if (list->type == DECORATION_REF_HEAD) {
196                         head = list;
197                         break;
198                 }
199         if (!head)
200                 return NULL;
201
202         /* Now resolve and find the matching current branch */
203         branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
204         if (!branch_name || !(rru_flags & REF_ISSYMREF))
205                 return NULL;
206
207         if (!starts_with(branch_name, "refs/"))
208                 return NULL;
209
210         /* OK, do we have that ref in the list? */
211         for (list = decoration; list; list = list->next)
212                 if ((list->type == DECORATION_REF_LOCAL) &&
213                     !strcmp(branch_name, list->name)) {
214                         return list;
215                 }
216
217         return NULL;
218 }
219
220 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
221 {
222         if (decoration_flags == DECORATE_SHORT_REFS)
223                 strbuf_addstr(sb, prettify_refname(decoration->name));
224         else
225                 strbuf_addstr(sb, decoration->name);
226 }
227
228 /*
229  * The caller makes sure there is no funny color before calling.
230  * format_decorations_extended makes sure the same after return.
231  */
232 void format_decorations_extended(struct strbuf *sb,
233                         const struct commit *commit,
234                         int use_color,
235                         const char *prefix,
236                         const char *separator,
237                         const char *suffix)
238 {
239         const struct name_decoration *decoration;
240         const struct name_decoration *current_and_HEAD;
241         const char *color_commit =
242                 diff_get_color(use_color, DIFF_COMMIT);
243         const char *color_reset =
244                 decorate_get_color(use_color, DECORATION_NONE);
245
246         decoration = get_name_decoration(&commit->object);
247         if (!decoration)
248                 return;
249
250         current_and_HEAD = current_pointed_by_HEAD(decoration);
251         while (decoration) {
252                 /*
253                  * When both current and HEAD are there, only
254                  * show HEAD->current where HEAD would have
255                  * appeared, skipping the entry for current.
256                  */
257                 if (decoration != current_and_HEAD) {
258                         strbuf_addstr(sb, color_commit);
259                         strbuf_addstr(sb, prefix);
260                         strbuf_addstr(sb, color_reset);
261                         strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
262                         if (decoration->type == DECORATION_REF_TAG)
263                                 strbuf_addstr(sb, "tag: ");
264
265                         show_name(sb, decoration);
266
267                         if (current_and_HEAD &&
268                             decoration->type == DECORATION_REF_HEAD) {
269                                 strbuf_addstr(sb, " -> ");
270                                 strbuf_addstr(sb, color_reset);
271                                 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
272                                 show_name(sb, current_and_HEAD);
273                         }
274                         strbuf_addstr(sb, color_reset);
275
276                         prefix = separator;
277                 }
278                 decoration = decoration->next;
279         }
280         strbuf_addstr(sb, color_commit);
281         strbuf_addstr(sb, suffix);
282         strbuf_addstr(sb, color_reset);
283 }
284
285 void show_decorations(struct rev_info *opt, struct commit *commit)
286 {
287         struct strbuf sb = STRBUF_INIT;
288
289         if (opt->sources) {
290                 char **slot = revision_sources_peek(opt->sources, commit);
291
292                 if (slot && *slot)
293                         fprintf(opt->diffopt.file, "\t%s", *slot);
294         }
295         if (!opt->show_decorations)
296                 return;
297         format_decorations(&sb, commit, opt->diffopt.use_color);
298         fputs(sb.buf, opt->diffopt.file);
299         strbuf_release(&sb);
300 }
301
302 static unsigned int digits_in_number(unsigned int number)
303 {
304         unsigned int i = 10, result = 1;
305         while (i <= number) {
306                 i *= 10;
307                 result++;
308         }
309         return result;
310 }
311
312 void fmt_output_subject(struct strbuf *filename,
313                         const char *subject,
314                         struct rev_info *info)
315 {
316         const char *suffix = info->patch_suffix;
317         int nr = info->nr;
318         int start_len = filename->len;
319         int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
320
321         if (0 < info->reroll_count)
322                 strbuf_addf(filename, "v%d-", info->reroll_count);
323         strbuf_addf(filename, "%04d-%s", nr, subject);
324
325         if (max_len < filename->len)
326                 strbuf_setlen(filename, max_len);
327         strbuf_addstr(filename, suffix);
328 }
329
330 void fmt_output_commit(struct strbuf *filename,
331                        struct commit *commit,
332                        struct rev_info *info)
333 {
334         struct pretty_print_context ctx = {0};
335         struct strbuf subject = STRBUF_INIT;
336
337         format_commit_message(commit, "%f", &subject, &ctx);
338         fmt_output_subject(filename, subject.buf, info);
339         strbuf_release(&subject);
340 }
341
342 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
343 {
344         if (opt->total > 0) {
345                 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
346                             opt->subject_prefix,
347                             *opt->subject_prefix ? " " : "",
348                             digits_in_number(opt->total),
349                             opt->nr, opt->total);
350         } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
351                 strbuf_addf(sb, "Subject: [%s] ",
352                             opt->subject_prefix);
353         } else {
354                 strbuf_addstr(sb, "Subject: ");
355         }
356 }
357
358 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
359                              const char **extra_headers_p,
360                              int *need_8bit_cte_p,
361                              int maybe_multipart)
362 {
363         const char *extra_headers = opt->extra_headers;
364         const char *name = oid_to_hex(opt->zero_commit ?
365                                       &null_oid : &commit->object.oid);
366
367         *need_8bit_cte_p = 0; /* unknown */
368
369         fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
370         graph_show_oneline(opt->graph);
371         if (opt->message_id) {
372                 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
373                 graph_show_oneline(opt->graph);
374         }
375         if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
376                 int i, n;
377                 n = opt->ref_message_ids->nr;
378                 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
379                 for (i = 0; i < n; i++)
380                         fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
381                                opt->ref_message_ids->items[i].string);
382                 graph_show_oneline(opt->graph);
383         }
384         if (opt->mime_boundary && maybe_multipart) {
385                 static struct strbuf subject_buffer = STRBUF_INIT;
386                 static struct strbuf buffer = STRBUF_INIT;
387                 struct strbuf filename =  STRBUF_INIT;
388                 *need_8bit_cte_p = -1; /* NEVER */
389
390                 strbuf_reset(&subject_buffer);
391                 strbuf_reset(&buffer);
392
393                 strbuf_addf(&subject_buffer,
394                          "%s"
395                          "MIME-Version: 1.0\n"
396                          "Content-Type: multipart/mixed;"
397                          " boundary=\"%s%s\"\n"
398                          "\n"
399                          "This is a multi-part message in MIME "
400                          "format.\n"
401                          "--%s%s\n"
402                          "Content-Type: text/plain; "
403                          "charset=UTF-8; format=fixed\n"
404                          "Content-Transfer-Encoding: 8bit\n\n",
405                          extra_headers ? extra_headers : "",
406                          mime_boundary_leader, opt->mime_boundary,
407                          mime_boundary_leader, opt->mime_boundary);
408                 extra_headers = subject_buffer.buf;
409
410                 if (opt->numbered_files)
411                         strbuf_addf(&filename, "%d", opt->nr);
412                 else
413                         fmt_output_commit(&filename, commit, opt);
414                 strbuf_addf(&buffer,
415                          "\n--%s%s\n"
416                          "Content-Type: text/x-patch;"
417                          " name=\"%s\"\n"
418                          "Content-Transfer-Encoding: 8bit\n"
419                          "Content-Disposition: %s;"
420                          " filename=\"%s\"\n\n",
421                          mime_boundary_leader, opt->mime_boundary,
422                          filename.buf,
423                          opt->no_inline ? "attachment" : "inline",
424                          filename.buf);
425                 opt->diffopt.stat_sep = buffer.buf;
426                 strbuf_release(&filename);
427         }
428         *extra_headers_p = extra_headers;
429 }
430
431 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
432 {
433         const char *color, *reset, *eol;
434
435         color = diff_get_color_opt(&opt->diffopt,
436                                    status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
437         reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
438         while (*bol) {
439                 eol = strchrnul(bol, '\n');
440                 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
441                        *eol ? "\n" : "");
442                 graph_show_oneline(opt->graph);
443                 bol = (*eol) ? (eol + 1) : eol;
444         }
445 }
446
447 static void show_signature(struct rev_info *opt, struct commit *commit)
448 {
449         struct strbuf payload = STRBUF_INIT;
450         struct strbuf signature = STRBUF_INIT;
451         struct signature_check sigc = { 0 };
452         int status;
453
454         if (parse_signed_commit(commit, &payload, &signature) <= 0)
455                 goto out;
456
457         status = check_signature(payload.buf, payload.len, signature.buf,
458                                  signature.len, &sigc);
459         if (status && sigc.result == 'N')
460                 show_sig_lines(opt, status, "No signature\n");
461         else {
462                 show_sig_lines(opt, status, sigc.gpg_output);
463                 signature_check_clear(&sigc);
464         }
465
466  out:
467         strbuf_release(&payload);
468         strbuf_release(&signature);
469 }
470
471 static int which_parent(const struct object_id *oid, const struct commit *commit)
472 {
473         int nth;
474         const struct commit_list *parent;
475
476         for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
477                 if (oideq(&parent->item->object.oid, oid))
478                         return nth;
479                 nth++;
480         }
481         return -1;
482 }
483
484 static int is_common_merge(const struct commit *commit)
485 {
486         return (commit->parents
487                 && commit->parents->next
488                 && !commit->parents->next->next);
489 }
490
491 static int show_one_mergetag(struct commit *commit,
492                              struct commit_extra_header *extra,
493                              void *data)
494 {
495         struct rev_info *opt = (struct rev_info *)data;
496         struct object_id oid;
497         struct tag *tag;
498         struct strbuf verify_message;
499         struct signature_check sigc = { 0 };
500         int status, nth;
501         size_t payload_size, gpg_message_offset;
502
503         hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
504         tag = lookup_tag(the_repository, &oid);
505         if (!tag)
506                 return -1; /* error message already given */
507
508         strbuf_init(&verify_message, 256);
509         if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
510                 strbuf_addstr(&verify_message, "malformed mergetag\n");
511         else if (is_common_merge(commit) &&
512                  oideq(&tag->tagged->oid,
513                        &commit->parents->next->item->object.oid))
514                 strbuf_addf(&verify_message,
515                             "merged tag '%s'\n", tag->tag);
516         else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
517                 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
518                                     tag->tag, tag->tagged->oid.hash);
519         else
520                 strbuf_addf(&verify_message,
521                             "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
522         gpg_message_offset = verify_message.len;
523
524         payload_size = parse_signature(extra->value, extra->len);
525         status = -1;
526         if (extra->len > payload_size) {
527                 /* could have a good signature */
528                 if (!check_signature(extra->value, payload_size,
529                                      extra->value + payload_size,
530                                      extra->len - payload_size, &sigc)) {
531                         strbuf_addstr(&verify_message, sigc.gpg_output);
532                         signature_check_clear(&sigc);
533                         status = 0; /* good */
534                 } else if (verify_message.len <= gpg_message_offset)
535                         strbuf_addstr(&verify_message, "No signature\n");
536                 /* otherwise we couldn't verify, which is shown as bad */
537         }
538
539         show_sig_lines(opt, status, verify_message.buf);
540         strbuf_release(&verify_message);
541         return 0;
542 }
543
544 static int show_mergetag(struct rev_info *opt, struct commit *commit)
545 {
546         return for_each_mergetag(show_one_mergetag, commit, opt);
547 }
548
549 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
550 {
551         const char *x = opt->shown_dashes ? "\n" : "---\n";
552         if (sb)
553                 strbuf_addstr(sb, x);
554         else
555                 fputs(x, opt->diffopt.file);
556         opt->shown_dashes = 1;
557 }
558
559 void show_log(struct rev_info *opt)
560 {
561         struct strbuf msgbuf = STRBUF_INIT;
562         struct log_info *log = opt->loginfo;
563         struct commit *commit = log->commit, *parent = log->parent;
564         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
565         const char *extra_headers = opt->extra_headers;
566         struct pretty_print_context ctx = {0};
567
568         opt->loginfo = NULL;
569         if (!opt->verbose_header) {
570                 graph_show_commit(opt->graph);
571
572                 if (!opt->graph)
573                         put_revision_mark(opt, commit);
574                 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
575                 if (opt->print_parents)
576                         show_parents(commit, abbrev_commit, opt->diffopt.file);
577                 if (opt->children.name)
578                         show_children(opt, commit, abbrev_commit);
579                 show_decorations(opt, commit);
580                 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
581                         putc('\n', opt->diffopt.file);
582                         graph_show_remainder(opt->graph);
583                 }
584                 putc(opt->diffopt.line_termination, opt->diffopt.file);
585                 return;
586         }
587
588         /*
589          * If use_terminator is set, we already handled any record termination
590          * at the end of the last record.
591          * Otherwise, add a diffopt.line_termination character before all
592          * entries but the first.  (IOW, as a separator between entries)
593          */
594         if (opt->shown_one && !opt->use_terminator) {
595                 /*
596                  * If entries are separated by a newline, the output
597                  * should look human-readable.  If the last entry ended
598                  * with a newline, print the graph output before this
599                  * newline.  Otherwise it will end up as a completely blank
600                  * line and will look like a gap in the graph.
601                  *
602                  * If the entry separator is not a newline, the output is
603                  * primarily intended for programmatic consumption, and we
604                  * never want the extra graph output before the entry
605                  * separator.
606                  */
607                 if (opt->diffopt.line_termination == '\n' &&
608                     !opt->missing_newline)
609                         graph_show_padding(opt->graph);
610                 putc(opt->diffopt.line_termination, opt->diffopt.file);
611         }
612         opt->shown_one = 1;
613
614         /*
615          * If the history graph was requested,
616          * print the graph, up to this commit's line
617          */
618         graph_show_commit(opt->graph);
619
620         /*
621          * Print header line of header..
622          */
623
624         if (cmit_fmt_is_mail(opt->commit_format)) {
625                 log_write_email_headers(opt, commit, &extra_headers,
626                                         &ctx.need_8bit_cte, 1);
627                 ctx.rev = opt;
628                 ctx.print_email_subject = 1;
629         } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
630                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
631                 if (opt->commit_format != CMIT_FMT_ONELINE)
632                         fputs("commit ", opt->diffopt.file);
633
634                 if (!opt->graph)
635                         put_revision_mark(opt, commit);
636                 fputs(find_unique_abbrev(&commit->object.oid,
637                                          abbrev_commit),
638                       opt->diffopt.file);
639                 if (opt->print_parents)
640                         show_parents(commit, abbrev_commit, opt->diffopt.file);
641                 if (opt->children.name)
642                         show_children(opt, commit, abbrev_commit);
643                 if (parent)
644                         fprintf(opt->diffopt.file, " (from %s)",
645                                find_unique_abbrev(&parent->object.oid, abbrev_commit));
646                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
647                 show_decorations(opt, commit);
648                 if (opt->commit_format == CMIT_FMT_ONELINE) {
649                         putc(' ', opt->diffopt.file);
650                 } else {
651                         putc('\n', opt->diffopt.file);
652                         graph_show_oneline(opt->graph);
653                 }
654                 if (opt->reflog_info) {
655                         /*
656                          * setup_revisions() ensures that opt->reflog_info
657                          * and opt->graph cannot both be set,
658                          * so we don't need to worry about printing the
659                          * graph info here.
660                          */
661                         show_reflog_message(opt->reflog_info,
662                                             opt->commit_format == CMIT_FMT_ONELINE,
663                                             &opt->date_mode,
664                                             opt->date_mode_explicit);
665                         if (opt->commit_format == CMIT_FMT_ONELINE)
666                                 return;
667                 }
668         }
669
670         if (opt->show_signature) {
671                 show_signature(opt, commit);
672                 show_mergetag(opt, commit);
673         }
674
675         if (opt->show_notes) {
676                 int raw;
677                 struct strbuf notebuf = STRBUF_INIT;
678
679                 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
680                 format_display_notes(&commit->object.oid, &notebuf,
681                                      get_log_output_encoding(), raw);
682                 ctx.notes_message = notebuf.len
683                         ? strbuf_detach(&notebuf, NULL)
684                         : xcalloc(1, 1);
685         }
686
687         /*
688          * And then the pretty-printed message itself
689          */
690         if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
691                 ctx.need_8bit_cte =
692                         has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
693         ctx.date_mode = opt->date_mode;
694         ctx.date_mode_explicit = opt->date_mode_explicit;
695         ctx.abbrev = opt->diffopt.abbrev;
696         ctx.after_subject = extra_headers;
697         ctx.preserve_subject = opt->preserve_subject;
698         ctx.reflog_info = opt->reflog_info;
699         ctx.fmt = opt->commit_format;
700         ctx.mailmap = opt->mailmap;
701         ctx.color = opt->diffopt.use_color;
702         ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
703         ctx.output_encoding = get_log_output_encoding();
704         ctx.rev = opt;
705         if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
706                 ctx.from_ident = &opt->from_ident;
707         if (opt->graph)
708                 ctx.graph_width = graph_width(opt->graph);
709         pretty_print_commit(&ctx, commit, &msgbuf);
710
711         if (opt->add_signoff)
712                 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
713
714         if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
715             ctx.notes_message && *ctx.notes_message) {
716                 if (cmit_fmt_is_mail(ctx.fmt))
717                         next_commentary_block(opt, &msgbuf);
718                 strbuf_addstr(&msgbuf, ctx.notes_message);
719         }
720
721         if (opt->show_log_size) {
722                 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
723                 graph_show_oneline(opt->graph);
724         }
725
726         /*
727          * Set opt->missing_newline if msgbuf doesn't
728          * end in a newline (including if it is empty)
729          */
730         if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
731                 opt->missing_newline = 1;
732         else
733                 opt->missing_newline = 0;
734
735         graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
736         if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
737                 if (!opt->missing_newline)
738                         graph_show_padding(opt->graph);
739                 putc(opt->diffopt.line_termination, opt->diffopt.file);
740         }
741
742         strbuf_release(&msgbuf);
743         free(ctx.notes_message);
744
745         if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
746                 struct diff_queue_struct dq;
747
748                 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
749                 DIFF_QUEUE_CLEAR(&diff_queued_diff);
750
751                 next_commentary_block(opt, NULL);
752                 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
753                 show_interdiff(opt, 2);
754
755                 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
756         }
757
758         if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
759                 struct diff_queue_struct dq;
760                 struct diff_options opts;
761
762                 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
763                 DIFF_QUEUE_CLEAR(&diff_queued_diff);
764
765                 next_commentary_block(opt, NULL);
766                 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
767                 /*
768                  * Pass minimum required diff-options to range-diff; others
769                  * can be added later if deemed desirable.
770                  */
771                 diff_setup(&opts);
772                 opts.file = opt->diffopt.file;
773                 opts.use_color = opt->diffopt.use_color;
774                 diff_setup_done(&opts);
775                 show_range_diff(opt->rdiff1, opt->rdiff2,
776                                 opt->creation_factor, 1, &opts);
777
778                 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
779         }
780 }
781
782 int log_tree_diff_flush(struct rev_info *opt)
783 {
784         opt->shown_dashes = 0;
785         diffcore_std(&opt->diffopt);
786
787         if (diff_queue_is_empty()) {
788                 int saved_fmt = opt->diffopt.output_format;
789                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
790                 diff_flush(&opt->diffopt);
791                 opt->diffopt.output_format = saved_fmt;
792                 return 0;
793         }
794
795         if (opt->loginfo && !opt->no_commit_id) {
796                 show_log(opt);
797                 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
798                     opt->verbose_header &&
799                     opt->commit_format != CMIT_FMT_ONELINE &&
800                     !commit_format_is_empty(opt->commit_format)) {
801                         /*
802                          * When showing a verbose header (i.e. log message),
803                          * and not in --pretty=oneline format, we would want
804                          * an extra newline between the end of log and the
805                          * diff/diffstat output for readability.
806                          */
807                         int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
808                         if (opt->diffopt.output_prefix) {
809                                 struct strbuf *msg = NULL;
810                                 msg = opt->diffopt.output_prefix(&opt->diffopt,
811                                         opt->diffopt.output_prefix_data);
812                                 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
813                         }
814
815                         /*
816                          * We may have shown three-dashes line early
817                          * between generated commentary (notes, etc.)
818                          * and the log message, in which case we only
819                          * want a blank line after the commentary
820                          * without (an extra) three-dashes line.
821                          * Otherwise, we show the three-dashes line if
822                          * we are showing the patch with diffstat, but
823                          * in that case, there is no extra blank line
824                          * after the three-dashes line.
825                          */
826                         if (!opt->shown_dashes &&
827                             (pch & opt->diffopt.output_format) == pch)
828                                 fprintf(opt->diffopt.file, "---");
829                         putc('\n', opt->diffopt.file);
830                 }
831         }
832         diff_flush(&opt->diffopt);
833         return 1;
834 }
835
836 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
837 {
838         diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
839         return !opt->loginfo;
840 }
841
842 /*
843  * Show the diff of a commit.
844  *
845  * Return true if we printed any log info messages
846  */
847 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
848 {
849         int showed_log;
850         struct commit_list *parents;
851         struct object_id *oid;
852
853         if (!opt->diff && !opt->diffopt.flags.exit_with_status)
854                 return 0;
855
856         parse_commit_or_die(commit);
857         oid = get_commit_tree_oid(commit);
858
859         /* Root commit? */
860         parents = get_saved_parents(opt, commit);
861         if (!parents) {
862                 if (opt->show_root_diff) {
863                         diff_root_tree_oid(oid, "", &opt->diffopt);
864                         log_tree_diff_flush(opt);
865                 }
866                 return !opt->loginfo;
867         }
868
869         /* More than one parent? */
870         if (parents && parents->next) {
871                 if (opt->ignore_merges)
872                         return 0;
873                 else if (opt->combine_merges)
874                         return do_diff_combined(opt, commit);
875                 else if (opt->first_parent_only) {
876                         /*
877                          * Generate merge log entry only for the first
878                          * parent, showing summary diff of the others
879                          * we merged _in_.
880                          */
881                         parse_commit_or_die(parents->item);
882                         diff_tree_oid(get_commit_tree_oid(parents->item),
883                                       oid, "", &opt->diffopt);
884                         log_tree_diff_flush(opt);
885                         return !opt->loginfo;
886                 }
887
888                 /* If we show individual diffs, show the parent info */
889                 log->parent = parents->item;
890         }
891
892         showed_log = 0;
893         for (;;) {
894                 struct commit *parent = parents->item;
895
896                 parse_commit_or_die(parent);
897                 diff_tree_oid(get_commit_tree_oid(parent),
898                               oid, "", &opt->diffopt);
899                 log_tree_diff_flush(opt);
900
901                 showed_log |= !opt->loginfo;
902
903                 /* Set up the log info for the next parent, if any.. */
904                 parents = parents->next;
905                 if (!parents)
906                         break;
907                 log->parent = parents->item;
908                 opt->loginfo = log;
909         }
910         return showed_log;
911 }
912
913 int log_tree_commit(struct rev_info *opt, struct commit *commit)
914 {
915         struct log_info log;
916         int shown, close_file = opt->diffopt.close_file;
917
918         log.commit = commit;
919         log.parent = NULL;
920         opt->loginfo = &log;
921         opt->diffopt.close_file = 0;
922
923         if (opt->line_level_traverse)
924                 return line_log_print(opt, commit);
925
926         if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
927                 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
928         shown = log_tree_diff(opt, commit, &log);
929         if (!shown && opt->loginfo && opt->always_show_header) {
930                 log.parent = NULL;
931                 show_log(opt);
932                 shown = 1;
933         }
934         if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
935                 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
936         opt->loginfo = NULL;
937         maybe_flush_or_die(opt->diffopt.file, "stdout");
938         if (close_file)
939                 fclose(opt->diffopt.file);
940         return shown;
941 }