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