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