treewide: replace cache.h with more direct headers, where possible
[git.git] / log-tree.c
blob043e0df685b7db16dff71d92900dcf8b04f6a0d2
1 #include "cache.h"
2 #include "commit-reach.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "hex.h"
6 #include "object-store.h"
7 #include "repository.h"
8 #include "tmp-objdir.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "graph.h"
12 #include "log-tree.h"
13 #include "merge-ort.h"
14 #include "reflog-walk.h"
15 #include "refs.h"
16 #include "replace-object.h"
17 #include "string-list.h"
18 #include "color.h"
19 #include "gpg-interface.h"
20 #include "sequencer.h"
21 #include "line-log.h"
22 #include "help.h"
23 #include "range-diff.h"
24 #include "strmap.h"
26 static struct decoration name_decoration = { "object names" };
27 static int decoration_loaded;
28 static int decoration_flags;
30 static char decoration_colors[][COLOR_MAXLEN] = {
31 GIT_COLOR_RESET,
32 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
33 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
34 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
35 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
36 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
37 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
40 static const char *color_decorate_slots[] = {
41 [DECORATION_REF_LOCAL] = "branch",
42 [DECORATION_REF_REMOTE] = "remoteBranch",
43 [DECORATION_REF_TAG] = "tag",
44 [DECORATION_REF_STASH] = "stash",
45 [DECORATION_REF_HEAD] = "HEAD",
46 [DECORATION_GRAFTED] = "grafted",
49 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
51 if (want_color(decorate_use_color))
52 return decoration_colors[ix];
53 return "";
56 define_list_config_array(color_decorate_slots);
58 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
60 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
61 if (slot < 0)
62 return 0;
63 if (!value)
64 return config_error_nonbool(var);
65 return color_parse(value, decoration_colors[slot]);
69 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
70 * for showing the commit sha1, use the same check for --decorate
72 #define decorate_get_color_opt(o, ix) \
73 decorate_get_color((o)->use_color, ix)
75 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
77 struct name_decoration *res;
78 FLEX_ALLOC_STR(res, name, name);
79 res->type = type;
80 res->next = add_decoration(&name_decoration, obj, res);
83 const struct name_decoration *get_name_decoration(const struct object *obj)
85 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
86 return lookup_decoration(&name_decoration, obj);
89 static int match_ref_pattern(const char *refname,
90 const struct string_list_item *item)
92 int matched = 0;
93 if (!item->util) {
94 if (!wildmatch(item->string, refname, 0))
95 matched = 1;
96 } else {
97 const char *rest;
98 if (skip_prefix(refname, item->string, &rest) &&
99 (!*rest || *rest == '/'))
100 matched = 1;
102 return matched;
105 static int ref_filter_match(const char *refname,
106 const struct decoration_filter *filter)
108 struct string_list_item *item;
109 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
110 const struct string_list *include_patterns = filter->include_ref_pattern;
111 const struct string_list *exclude_patterns_config =
112 filter->exclude_ref_config_pattern;
114 if (exclude_patterns && exclude_patterns->nr) {
115 for_each_string_list_item(item, exclude_patterns) {
116 if (match_ref_pattern(refname, item))
117 return 0;
121 if (include_patterns && include_patterns->nr) {
122 for_each_string_list_item(item, include_patterns) {
123 if (match_ref_pattern(refname, item))
124 return 1;
126 return 0;
129 if (exclude_patterns_config && exclude_patterns_config->nr) {
130 for_each_string_list_item(item, exclude_patterns_config) {
131 if (match_ref_pattern(refname, item))
132 return 0;
136 return 1;
139 static int add_ref_decoration(const char *refname, const struct object_id *oid,
140 int flags UNUSED,
141 void *cb_data)
143 int i;
144 struct object *obj;
145 enum object_type objtype;
146 enum decoration_type deco_type = DECORATION_NONE;
147 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
148 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
150 if (filter && !ref_filter_match(refname, filter))
151 return 0;
153 if (starts_with(refname, git_replace_ref_base)) {
154 struct object_id original_oid;
155 if (!read_replace_refs)
156 return 0;
157 if (get_oid_hex(refname + strlen(git_replace_ref_base),
158 &original_oid)) {
159 warning("invalid replace ref %s", refname);
160 return 0;
162 obj = parse_object(the_repository, &original_oid);
163 if (obj)
164 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
165 return 0;
168 objtype = oid_object_info(the_repository, oid, NULL);
169 if (objtype < 0)
170 return 0;
171 obj = lookup_object_by_type(the_repository, oid, objtype);
173 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
174 struct ref_namespace_info *info = &ref_namespace[i];
176 if (!info->decoration)
177 continue;
178 if (info->exact) {
179 if (!strcmp(refname, info->ref)) {
180 deco_type = info->decoration;
181 break;
183 } else if (starts_with(refname, info->ref)) {
184 deco_type = info->decoration;
185 break;
189 add_name_decoration(deco_type, refname, obj);
190 while (obj->type == OBJ_TAG) {
191 if (!obj->parsed)
192 parse_object(the_repository, &obj->oid);
193 obj = ((struct tag *)obj)->tagged;
194 if (!obj)
195 break;
196 add_name_decoration(DECORATION_REF_TAG, refname, obj);
198 return 0;
201 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
203 struct commit *commit = lookup_commit(the_repository, &graft->oid);
204 if (!commit)
205 return 0;
206 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
207 return 0;
210 void load_ref_decorations(struct decoration_filter *filter, int flags)
212 if (!decoration_loaded) {
213 if (filter) {
214 struct string_list_item *item;
215 for_each_string_list_item(item, filter->exclude_ref_pattern) {
216 normalize_glob_ref(item, NULL, item->string);
218 for_each_string_list_item(item, filter->include_ref_pattern) {
219 normalize_glob_ref(item, NULL, item->string);
221 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
222 normalize_glob_ref(item, NULL, item->string);
225 decoration_loaded = 1;
226 decoration_flags = flags;
227 for_each_ref(add_ref_decoration, filter);
228 head_ref(add_ref_decoration, filter);
229 for_each_commit_graft(add_graft_decoration, filter);
233 static void show_parents(struct commit *commit, int abbrev, FILE *file)
235 struct commit_list *p;
236 for (p = commit->parents; p ; p = p->next) {
237 struct commit *parent = p->item;
238 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
242 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
244 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
245 for ( ; p; p = p->next) {
246 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
251 * Do we have HEAD in the output, and also the branch it points at?
252 * If so, find that decoration entry for that current branch.
254 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
256 const struct name_decoration *list, *head = NULL;
257 const char *branch_name = NULL;
258 int rru_flags;
260 /* First find HEAD */
261 for (list = decoration; list; list = list->next)
262 if (list->type == DECORATION_REF_HEAD) {
263 head = list;
264 break;
266 if (!head)
267 return NULL;
269 /* Now resolve and find the matching current branch */
270 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
271 if (!branch_name || !(rru_flags & REF_ISSYMREF))
272 return NULL;
274 if (!starts_with(branch_name, "refs/"))
275 return NULL;
277 /* OK, do we have that ref in the list? */
278 for (list = decoration; list; list = list->next)
279 if ((list->type == DECORATION_REF_LOCAL) &&
280 !strcmp(branch_name, list->name)) {
281 return list;
284 return NULL;
287 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
289 if (decoration_flags == DECORATE_SHORT_REFS)
290 strbuf_addstr(sb, prettify_refname(decoration->name));
291 else
292 strbuf_addstr(sb, decoration->name);
296 * The caller makes sure there is no funny color before calling.
297 * format_decorations_extended makes sure the same after return.
299 void format_decorations_extended(struct strbuf *sb,
300 const struct commit *commit,
301 int use_color,
302 const char *prefix,
303 const char *separator,
304 const char *suffix)
306 const struct name_decoration *decoration;
307 const struct name_decoration *current_and_HEAD;
308 const char *color_commit =
309 diff_get_color(use_color, DIFF_COMMIT);
310 const char *color_reset =
311 decorate_get_color(use_color, DECORATION_NONE);
313 decoration = get_name_decoration(&commit->object);
314 if (!decoration)
315 return;
317 current_and_HEAD = current_pointed_by_HEAD(decoration);
318 while (decoration) {
320 * When both current and HEAD are there, only
321 * show HEAD->current where HEAD would have
322 * appeared, skipping the entry for current.
324 if (decoration != current_and_HEAD) {
325 strbuf_addstr(sb, color_commit);
326 strbuf_addstr(sb, prefix);
327 strbuf_addstr(sb, color_reset);
328 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
329 if (decoration->type == DECORATION_REF_TAG)
330 strbuf_addstr(sb, "tag: ");
332 show_name(sb, decoration);
334 if (current_and_HEAD &&
335 decoration->type == DECORATION_REF_HEAD) {
336 strbuf_addstr(sb, " -> ");
337 strbuf_addstr(sb, color_reset);
338 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
339 show_name(sb, current_and_HEAD);
341 strbuf_addstr(sb, color_reset);
343 prefix = separator;
345 decoration = decoration->next;
347 strbuf_addstr(sb, color_commit);
348 strbuf_addstr(sb, suffix);
349 strbuf_addstr(sb, color_reset);
352 void show_decorations(struct rev_info *opt, struct commit *commit)
354 struct strbuf sb = STRBUF_INIT;
356 if (opt->sources) {
357 char **slot = revision_sources_peek(opt->sources, commit);
359 if (slot && *slot)
360 fprintf(opt->diffopt.file, "\t%s", *slot);
362 if (!opt->show_decorations)
363 return;
364 format_decorations(&sb, commit, opt->diffopt.use_color);
365 fputs(sb.buf, opt->diffopt.file);
366 strbuf_release(&sb);
369 static unsigned int digits_in_number(unsigned int number)
371 unsigned int i = 10, result = 1;
372 while (i <= number) {
373 i *= 10;
374 result++;
376 return result;
379 void fmt_output_subject(struct strbuf *filename,
380 const char *subject,
381 struct rev_info *info)
383 const char *suffix = info->patch_suffix;
384 int nr = info->nr;
385 int start_len = filename->len;
386 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
388 if (info->reroll_count) {
389 struct strbuf temp = STRBUF_INIT;
391 strbuf_addf(&temp, "v%s", info->reroll_count);
392 format_sanitized_subject(filename, temp.buf, temp.len);
393 strbuf_addstr(filename, "-");
394 strbuf_release(&temp);
396 strbuf_addf(filename, "%04d-%s", nr, subject);
398 if (max_len < filename->len)
399 strbuf_setlen(filename, max_len);
400 strbuf_addstr(filename, suffix);
403 void fmt_output_commit(struct strbuf *filename,
404 struct commit *commit,
405 struct rev_info *info)
407 struct pretty_print_context ctx = {0};
408 struct strbuf subject = STRBUF_INIT;
410 format_commit_message(commit, "%f", &subject, &ctx);
411 fmt_output_subject(filename, subject.buf, info);
412 strbuf_release(&subject);
415 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
417 if (opt->total > 0) {
418 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
419 opt->subject_prefix,
420 *opt->subject_prefix ? " " : "",
421 digits_in_number(opt->total),
422 opt->nr, opt->total);
423 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
424 strbuf_addf(sb, "Subject: [%s] ",
425 opt->subject_prefix);
426 } else {
427 strbuf_addstr(sb, "Subject: ");
431 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
432 const char **extra_headers_p,
433 int *need_8bit_cte_p,
434 int maybe_multipart)
436 const char *extra_headers = opt->extra_headers;
437 const char *name = oid_to_hex(opt->zero_commit ?
438 null_oid() : &commit->object.oid);
440 *need_8bit_cte_p = 0; /* unknown */
442 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
443 graph_show_oneline(opt->graph);
444 if (opt->message_id) {
445 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
446 graph_show_oneline(opt->graph);
448 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
449 int i, n;
450 n = opt->ref_message_ids->nr;
451 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
452 for (i = 0; i < n; i++)
453 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
454 opt->ref_message_ids->items[i].string);
455 graph_show_oneline(opt->graph);
457 if (opt->mime_boundary && maybe_multipart) {
458 static struct strbuf subject_buffer = STRBUF_INIT;
459 static struct strbuf buffer = STRBUF_INIT;
460 struct strbuf filename = STRBUF_INIT;
461 *need_8bit_cte_p = -1; /* NEVER */
463 strbuf_reset(&subject_buffer);
464 strbuf_reset(&buffer);
466 strbuf_addf(&subject_buffer,
467 "%s"
468 "MIME-Version: 1.0\n"
469 "Content-Type: multipart/mixed;"
470 " boundary=\"%s%s\"\n"
471 "\n"
472 "This is a multi-part message in MIME "
473 "format.\n"
474 "--%s%s\n"
475 "Content-Type: text/plain; "
476 "charset=UTF-8; format=fixed\n"
477 "Content-Transfer-Encoding: 8bit\n\n",
478 extra_headers ? extra_headers : "",
479 mime_boundary_leader, opt->mime_boundary,
480 mime_boundary_leader, opt->mime_boundary);
481 extra_headers = subject_buffer.buf;
483 if (opt->numbered_files)
484 strbuf_addf(&filename, "%d", opt->nr);
485 else
486 fmt_output_commit(&filename, commit, opt);
487 strbuf_addf(&buffer,
488 "\n--%s%s\n"
489 "Content-Type: text/x-patch;"
490 " name=\"%s\"\n"
491 "Content-Transfer-Encoding: 8bit\n"
492 "Content-Disposition: %s;"
493 " filename=\"%s\"\n\n",
494 mime_boundary_leader, opt->mime_boundary,
495 filename.buf,
496 opt->no_inline ? "attachment" : "inline",
497 filename.buf);
498 opt->diffopt.stat_sep = buffer.buf;
499 strbuf_release(&filename);
501 *extra_headers_p = extra_headers;
504 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
506 const char *color, *reset, *eol;
508 color = diff_get_color_opt(&opt->diffopt,
509 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
510 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
511 while (*bol) {
512 eol = strchrnul(bol, '\n');
513 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
514 *eol ? "\n" : "");
515 graph_show_oneline(opt->graph);
516 bol = (*eol) ? (eol + 1) : eol;
520 static void show_signature(struct rev_info *opt, struct commit *commit)
522 struct strbuf payload = STRBUF_INIT;
523 struct strbuf signature = STRBUF_INIT;
524 struct signature_check sigc = { 0 };
525 int status;
527 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
528 goto out;
530 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
531 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
532 status = check_signature(&sigc, signature.buf, signature.len);
533 if (status && !sigc.output)
534 show_sig_lines(opt, status, "No signature\n");
535 else
536 show_sig_lines(opt, status, sigc.output);
537 signature_check_clear(&sigc);
539 out:
540 strbuf_release(&payload);
541 strbuf_release(&signature);
544 static int which_parent(const struct object_id *oid, const struct commit *commit)
546 int nth;
547 const struct commit_list *parent;
549 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
550 if (oideq(&parent->item->object.oid, oid))
551 return nth;
552 nth++;
554 return -1;
557 static int is_common_merge(const struct commit *commit)
559 return (commit->parents
560 && commit->parents->next
561 && !commit->parents->next->next);
564 static int show_one_mergetag(struct commit *commit,
565 struct commit_extra_header *extra,
566 void *data)
568 struct rev_info *opt = (struct rev_info *)data;
569 struct object_id oid;
570 struct tag *tag;
571 struct strbuf verify_message;
572 struct signature_check sigc = { 0 };
573 int status, nth;
574 struct strbuf payload = STRBUF_INIT;
575 struct strbuf signature = STRBUF_INIT;
577 hash_object_file(the_hash_algo, extra->value, extra->len,
578 OBJ_TAG, &oid);
579 tag = lookup_tag(the_repository, &oid);
580 if (!tag)
581 return -1; /* error message already given */
583 strbuf_init(&verify_message, 256);
584 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
585 strbuf_addstr(&verify_message, "malformed mergetag\n");
586 else if (is_common_merge(commit) &&
587 oideq(&tag->tagged->oid,
588 &commit->parents->next->item->object.oid))
589 strbuf_addf(&verify_message,
590 "merged tag '%s'\n", tag->tag);
591 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
592 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
593 tag->tag, oid_to_hex(&tag->tagged->oid));
594 else
595 strbuf_addf(&verify_message,
596 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
598 status = -1;
599 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
600 /* could have a good signature */
601 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
602 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
603 status = check_signature(&sigc, signature.buf, signature.len);
604 if (sigc.output)
605 strbuf_addstr(&verify_message, sigc.output);
606 else
607 strbuf_addstr(&verify_message, "No signature\n");
608 signature_check_clear(&sigc);
609 /* otherwise we couldn't verify, which is shown as bad */
612 show_sig_lines(opt, status, verify_message.buf);
613 strbuf_release(&verify_message);
614 strbuf_release(&payload);
615 strbuf_release(&signature);
616 return 0;
619 static int show_mergetag(struct rev_info *opt, struct commit *commit)
621 return for_each_mergetag(show_one_mergetag, commit, opt);
624 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
626 const char *x = opt->shown_dashes ? "\n" : "---\n";
627 if (sb)
628 strbuf_addstr(sb, x);
629 else
630 fputs(x, opt->diffopt.file);
631 opt->shown_dashes = 1;
634 void show_log(struct rev_info *opt)
636 struct strbuf msgbuf = STRBUF_INIT;
637 struct log_info *log = opt->loginfo;
638 struct commit *commit = log->commit, *parent = log->parent;
639 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
640 const char *extra_headers = opt->extra_headers;
641 struct pretty_print_context ctx = {0};
643 opt->loginfo = NULL;
644 if (!opt->verbose_header) {
645 graph_show_commit(opt->graph);
647 if (!opt->graph)
648 put_revision_mark(opt, commit);
649 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
650 if (opt->print_parents)
651 show_parents(commit, abbrev_commit, opt->diffopt.file);
652 if (opt->children.name)
653 show_children(opt, commit, abbrev_commit);
654 show_decorations(opt, commit);
655 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
656 putc('\n', opt->diffopt.file);
657 graph_show_remainder(opt->graph);
659 putc(opt->diffopt.line_termination, opt->diffopt.file);
660 return;
664 * If use_terminator is set, we already handled any record termination
665 * at the end of the last record.
666 * Otherwise, add a diffopt.line_termination character before all
667 * entries but the first. (IOW, as a separator between entries)
669 if (opt->shown_one && !opt->use_terminator) {
671 * If entries are separated by a newline, the output
672 * should look human-readable. If the last entry ended
673 * with a newline, print the graph output before this
674 * newline. Otherwise it will end up as a completely blank
675 * line and will look like a gap in the graph.
677 * If the entry separator is not a newline, the output is
678 * primarily intended for programmatic consumption, and we
679 * never want the extra graph output before the entry
680 * separator.
682 if (opt->diffopt.line_termination == '\n' &&
683 !opt->missing_newline)
684 graph_show_padding(opt->graph);
685 putc(opt->diffopt.line_termination, opt->diffopt.file);
687 opt->shown_one = 1;
690 * If the history graph was requested,
691 * print the graph, up to this commit's line
693 graph_show_commit(opt->graph);
696 * Print header line of header..
699 if (cmit_fmt_is_mail(opt->commit_format)) {
700 log_write_email_headers(opt, commit, &extra_headers,
701 &ctx.need_8bit_cte, 1);
702 ctx.rev = opt;
703 ctx.print_email_subject = 1;
704 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
705 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
706 if (opt->commit_format != CMIT_FMT_ONELINE)
707 fputs("commit ", opt->diffopt.file);
709 if (!opt->graph)
710 put_revision_mark(opt, commit);
711 fputs(find_unique_abbrev(&commit->object.oid,
712 abbrev_commit),
713 opt->diffopt.file);
714 if (opt->print_parents)
715 show_parents(commit, abbrev_commit, opt->diffopt.file);
716 if (opt->children.name)
717 show_children(opt, commit, abbrev_commit);
718 if (parent)
719 fprintf(opt->diffopt.file, " (from %s)",
720 find_unique_abbrev(&parent->object.oid, abbrev_commit));
721 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
722 show_decorations(opt, commit);
723 if (opt->commit_format == CMIT_FMT_ONELINE) {
724 putc(' ', opt->diffopt.file);
725 } else {
726 putc('\n', opt->diffopt.file);
727 graph_show_oneline(opt->graph);
729 if (opt->reflog_info) {
731 * setup_revisions() ensures that opt->reflog_info
732 * and opt->graph cannot both be set,
733 * so we don't need to worry about printing the
734 * graph info here.
736 show_reflog_message(opt->reflog_info,
737 opt->commit_format == CMIT_FMT_ONELINE,
738 &opt->date_mode,
739 opt->date_mode_explicit);
740 if (opt->commit_format == CMIT_FMT_ONELINE)
741 return;
745 if (opt->show_signature) {
746 show_signature(opt, commit);
747 show_mergetag(opt, commit);
750 if (opt->show_notes) {
751 int raw;
752 struct strbuf notebuf = STRBUF_INIT;
754 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
755 format_display_notes(&commit->object.oid, &notebuf,
756 get_log_output_encoding(), raw);
757 ctx.notes_message = strbuf_detach(&notebuf, NULL);
761 * And then the pretty-printed message itself
763 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
764 ctx.need_8bit_cte =
765 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
766 ctx.date_mode = opt->date_mode;
767 ctx.date_mode_explicit = opt->date_mode_explicit;
768 ctx.abbrev = opt->diffopt.abbrev;
769 ctx.after_subject = extra_headers;
770 ctx.preserve_subject = opt->preserve_subject;
771 ctx.encode_email_headers = opt->encode_email_headers;
772 ctx.reflog_info = opt->reflog_info;
773 ctx.fmt = opt->commit_format;
774 ctx.mailmap = opt->mailmap;
775 ctx.color = opt->diffopt.use_color;
776 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
777 ctx.output_encoding = get_log_output_encoding();
778 ctx.rev = opt;
779 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
780 ctx.from_ident = &opt->from_ident;
781 if (opt->graph)
782 ctx.graph_width = graph_width(opt->graph);
783 pretty_print_commit(&ctx, commit, &msgbuf);
785 if (opt->add_signoff)
786 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
788 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
789 ctx.notes_message && *ctx.notes_message) {
790 if (cmit_fmt_is_mail(ctx.fmt))
791 next_commentary_block(opt, &msgbuf);
792 strbuf_addstr(&msgbuf, ctx.notes_message);
795 if (opt->show_log_size) {
796 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
797 graph_show_oneline(opt->graph);
801 * Set opt->missing_newline if msgbuf doesn't
802 * end in a newline (including if it is empty)
804 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
805 opt->missing_newline = 1;
806 else
807 opt->missing_newline = 0;
809 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
810 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
811 if (!opt->missing_newline)
812 graph_show_padding(opt->graph);
813 putc(opt->diffopt.line_termination, opt->diffopt.file);
816 strbuf_release(&msgbuf);
817 free(ctx.notes_message);
819 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
820 struct diff_queue_struct dq;
822 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
823 DIFF_QUEUE_CLEAR(&diff_queued_diff);
825 next_commentary_block(opt, NULL);
826 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
827 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
828 &opt->diffopt);
830 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
833 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
834 struct diff_queue_struct dq;
835 struct diff_options opts;
836 struct range_diff_options range_diff_opts = {
837 .creation_factor = opt->creation_factor,
838 .dual_color = 1,
839 .diffopt = &opts
842 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
843 DIFF_QUEUE_CLEAR(&diff_queued_diff);
845 next_commentary_block(opt, NULL);
846 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
848 * Pass minimum required diff-options to range-diff; others
849 * can be added later if deemed desirable.
851 diff_setup(&opts);
852 opts.file = opt->diffopt.file;
853 opts.use_color = opt->diffopt.use_color;
854 diff_setup_done(&opts);
855 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
857 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
861 int log_tree_diff_flush(struct rev_info *opt)
863 opt->shown_dashes = 0;
864 diffcore_std(&opt->diffopt);
866 if (diff_queue_is_empty(&opt->diffopt)) {
867 int saved_fmt = opt->diffopt.output_format;
868 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
869 diff_flush(&opt->diffopt);
870 opt->diffopt.output_format = saved_fmt;
871 return 0;
874 if (opt->loginfo && !opt->no_commit_id) {
875 show_log(opt);
876 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
877 opt->verbose_header &&
878 opt->commit_format != CMIT_FMT_ONELINE &&
879 !commit_format_is_empty(opt->commit_format)) {
881 * When showing a verbose header (i.e. log message),
882 * and not in --pretty=oneline format, we would want
883 * an extra newline between the end of log and the
884 * diff/diffstat output for readability.
886 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
887 if (opt->diffopt.output_prefix) {
888 struct strbuf *msg = NULL;
889 msg = opt->diffopt.output_prefix(&opt->diffopt,
890 opt->diffopt.output_prefix_data);
891 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
895 * We may have shown three-dashes line early
896 * between generated commentary (notes, etc.)
897 * and the log message, in which case we only
898 * want a blank line after the commentary
899 * without (an extra) three-dashes line.
900 * Otherwise, we show the three-dashes line if
901 * we are showing the patch with diffstat, but
902 * in that case, there is no extra blank line
903 * after the three-dashes line.
905 if (!opt->shown_dashes &&
906 (pch & opt->diffopt.output_format) == pch)
907 fprintf(opt->diffopt.file, "---");
908 putc('\n', opt->diffopt.file);
911 diff_flush(&opt->diffopt);
912 return 1;
915 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
917 diff_tree_combined_merge(commit, opt);
918 return !opt->loginfo;
921 static void setup_additional_headers(struct diff_options *o,
922 struct strmap *all_headers)
924 struct hashmap_iter iter;
925 struct strmap_entry *entry;
928 * Make o->additional_path_headers contain the subset of all_headers
929 * that match o->pathspec. If there aren't any that match o->pathspec,
930 * then make o->additional_path_headers be NULL.
933 if (!o->pathspec.nr) {
934 o->additional_path_headers = all_headers;
935 return;
938 o->additional_path_headers = xmalloc(sizeof(struct strmap));
939 strmap_init_with_options(o->additional_path_headers, NULL, 0);
940 strmap_for_each_entry(all_headers, &iter, entry) {
941 if (match_pathspec(the_repository->index, &o->pathspec,
942 entry->key, strlen(entry->key),
943 0 /* prefix */, NULL /* seen */,
944 0 /* is_dir */))
945 strmap_put(o->additional_path_headers,
946 entry->key, entry->value);
948 if (!strmap_get_size(o->additional_path_headers)) {
949 strmap_clear(o->additional_path_headers, 0);
950 FREE_AND_NULL(o->additional_path_headers);
954 static void cleanup_additional_headers(struct diff_options *o)
956 if (!o->pathspec.nr) {
957 o->additional_path_headers = NULL;
958 return;
960 if (!o->additional_path_headers)
961 return;
963 strmap_clear(o->additional_path_headers, 0);
964 FREE_AND_NULL(o->additional_path_headers);
967 static int do_remerge_diff(struct rev_info *opt,
968 struct commit_list *parents,
969 struct object_id *oid)
971 struct merge_options o;
972 struct commit_list *bases;
973 struct merge_result res = {0};
974 struct pretty_print_context ctx = {0};
975 struct commit *parent1 = parents->item;
976 struct commit *parent2 = parents->next->item;
977 struct strbuf parent1_desc = STRBUF_INIT;
978 struct strbuf parent2_desc = STRBUF_INIT;
980 /* Setup merge options */
981 init_merge_options(&o, the_repository);
982 o.show_rename_progress = 0;
983 o.record_conflict_msgs_as_headers = 1;
984 o.msg_header_prefix = "remerge";
986 ctx.abbrev = DEFAULT_ABBREV;
987 format_commit_message(parent1, "%h (%s)", &parent1_desc, &ctx);
988 format_commit_message(parent2, "%h (%s)", &parent2_desc, &ctx);
989 o.branch1 = parent1_desc.buf;
990 o.branch2 = parent2_desc.buf;
992 /* Parse the relevant commits and get the merge bases */
993 parse_commit_or_die(parent1);
994 parse_commit_or_die(parent2);
995 bases = get_merge_bases(parent1, parent2);
997 /* Re-merge the parents */
998 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1000 /* Show the diff */
1001 setup_additional_headers(&opt->diffopt, res.path_messages);
1002 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1003 log_tree_diff_flush(opt);
1005 /* Cleanup */
1006 cleanup_additional_headers(&opt->diffopt);
1007 strbuf_release(&parent1_desc);
1008 strbuf_release(&parent2_desc);
1009 merge_finalize(&o, &res);
1011 /* Clean up the contents of the temporary object directory */
1012 if (opt->remerge_objdir)
1013 tmp_objdir_discard_objects(opt->remerge_objdir);
1014 else
1015 BUG("did a remerge diff without remerge_objdir?!?");
1017 return !opt->loginfo;
1021 * Show the diff of a commit.
1023 * Return true if we printed any log info messages
1025 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1027 int showed_log;
1028 struct commit_list *parents;
1029 struct object_id *oid;
1030 int is_merge;
1031 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1033 if (!all_need_diff && !opt->merges_need_diff)
1034 return 0;
1036 parse_commit_or_die(commit);
1037 oid = get_commit_tree_oid(commit);
1039 parents = get_saved_parents(opt, commit);
1040 is_merge = parents && parents->next;
1041 if (!is_merge && !all_need_diff)
1042 return 0;
1044 /* Root commit? */
1045 if (!parents) {
1046 if (opt->show_root_diff) {
1047 diff_root_tree_oid(oid, "", &opt->diffopt);
1048 log_tree_diff_flush(opt);
1050 return !opt->loginfo;
1053 if (is_merge) {
1054 int octopus = (parents->next->next != NULL);
1056 if (opt->remerge_diff) {
1057 if (octopus) {
1058 show_log(opt);
1059 fprintf(opt->diffopt.file,
1060 "diff: warning: Skipping remerge-diff "
1061 "for octopus merges.\n");
1062 return 1;
1064 return do_remerge_diff(opt, parents, oid);
1066 if (opt->combine_merges)
1067 return do_diff_combined(opt, commit);
1068 if (opt->separate_merges) {
1069 if (!opt->first_parent_merges) {
1070 /* Show parent info for multiple diffs */
1071 log->parent = parents->item;
1073 } else
1074 return 0;
1077 showed_log = 0;
1078 for (;;) {
1079 struct commit *parent = parents->item;
1081 parse_commit_or_die(parent);
1082 diff_tree_oid(get_commit_tree_oid(parent),
1083 oid, "", &opt->diffopt);
1084 log_tree_diff_flush(opt);
1086 showed_log |= !opt->loginfo;
1088 /* Set up the log info for the next parent, if any.. */
1089 parents = parents->next;
1090 if (!parents || opt->first_parent_merges)
1091 break;
1092 log->parent = parents->item;
1093 opt->loginfo = log;
1095 return showed_log;
1098 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1100 struct log_info log;
1101 int shown;
1102 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1103 int no_free = opt->diffopt.no_free;
1105 log.commit = commit;
1106 log.parent = NULL;
1107 opt->loginfo = &log;
1108 opt->diffopt.no_free = 1;
1110 /* NEEDSWORK: no restoring of no_free? Why? */
1111 if (opt->line_level_traverse)
1112 return line_log_print(opt, commit);
1114 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1115 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1116 shown = log_tree_diff(opt, commit, &log);
1117 if (!shown && opt->loginfo && opt->always_show_header) {
1118 log.parent = NULL;
1119 show_log(opt);
1120 shown = 1;
1122 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1123 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1124 opt->loginfo = NULL;
1125 maybe_flush_or_die(opt->diffopt.file, "stdout");
1126 opt->diffopt.no_free = no_free;
1127 diff_free(&opt->diffopt);
1128 return shown;