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