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