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