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