var: avoid a segmentation fault when `HOME` is unset
[git.git] / log-tree.c
blobf4b22a60cc53c7b0d723de1fce1a00282ddd41ff
1 #include "git-compat-util.h"
2 #include "commit-reach.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "environment.h"
6 #include "hex.h"
7 #include "object-name.h"
8 #include "object-store.h"
9 #include "repository.h"
10 #include "tmp-objdir.h"
11 #include "commit.h"
12 #include "tag.h"
13 #include "graph.h"
14 #include "log-tree.h"
15 #include "merge-ort.h"
16 #include "reflog-walk.h"
17 #include "refs.h"
18 #include "replace-object.h"
19 #include "string-list.h"
20 #include "color.h"
21 #include "gpg-interface.h"
22 #include "sequencer.h"
23 #include "line-log.h"
24 #include "help.h"
25 #include "range-diff.h"
26 #include "strmap.h"
27 #include "tree.h"
28 #include "write-or-die.h"
30 static struct decoration name_decoration = { "object names" };
31 static int decoration_loaded;
32 static int decoration_flags;
34 static char decoration_colors[][COLOR_MAXLEN] = {
35 GIT_COLOR_RESET,
36 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
37 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
38 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
39 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
40 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
41 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
44 static const char *color_decorate_slots[] = {
45 [DECORATION_REF_LOCAL] = "branch",
46 [DECORATION_REF_REMOTE] = "remoteBranch",
47 [DECORATION_REF_TAG] = "tag",
48 [DECORATION_REF_STASH] = "stash",
49 [DECORATION_REF_HEAD] = "HEAD",
50 [DECORATION_GRAFTED] = "grafted",
53 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
55 if (want_color(decorate_use_color))
56 return decoration_colors[ix];
57 return "";
60 define_list_config_array(color_decorate_slots);
62 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
64 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
65 if (slot < 0)
66 return 0;
67 if (!value)
68 return config_error_nonbool(var);
69 return color_parse(value, decoration_colors[slot]);
73 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
74 * for showing the commit sha1, use the same check for --decorate
76 #define decorate_get_color_opt(o, ix) \
77 decorate_get_color((o)->use_color, ix)
79 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
81 struct name_decoration *res;
82 FLEX_ALLOC_STR(res, name, name);
83 res->type = type;
84 res->next = add_decoration(&name_decoration, obj, res);
87 const struct name_decoration *get_name_decoration(const struct object *obj)
89 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
90 return lookup_decoration(&name_decoration, obj);
93 static int match_ref_pattern(const char *refname,
94 const struct string_list_item *item)
96 int matched = 0;
97 if (!item->util) {
98 if (!wildmatch(item->string, refname, 0))
99 matched = 1;
100 } else {
101 const char *rest;
102 if (skip_prefix(refname, item->string, &rest) &&
103 (!*rest || *rest == '/'))
104 matched = 1;
106 return matched;
109 static int ref_filter_match(const char *refname,
110 const struct decoration_filter *filter)
112 struct string_list_item *item;
113 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
114 const struct string_list *include_patterns = filter->include_ref_pattern;
115 const struct string_list *exclude_patterns_config =
116 filter->exclude_ref_config_pattern;
118 if (exclude_patterns && exclude_patterns->nr) {
119 for_each_string_list_item(item, exclude_patterns) {
120 if (match_ref_pattern(refname, item))
121 return 0;
125 if (include_patterns && include_patterns->nr) {
126 for_each_string_list_item(item, include_patterns) {
127 if (match_ref_pattern(refname, item))
128 return 1;
130 return 0;
133 if (exclude_patterns_config && exclude_patterns_config->nr) {
134 for_each_string_list_item(item, exclude_patterns_config) {
135 if (match_ref_pattern(refname, item))
136 return 0;
140 return 1;
143 static int add_ref_decoration(const char *refname, const struct object_id *oid,
144 int flags UNUSED,
145 void *cb_data)
147 int i;
148 struct object *obj;
149 enum object_type objtype;
150 enum decoration_type deco_type = DECORATION_NONE;
151 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
152 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
154 if (filter && !ref_filter_match(refname, filter))
155 return 0;
157 if (starts_with(refname, git_replace_ref_base)) {
158 struct object_id original_oid;
159 if (!read_replace_refs)
160 return 0;
161 if (get_oid_hex(refname + strlen(git_replace_ref_base),
162 &original_oid)) {
163 warning("invalid replace ref %s", refname);
164 return 0;
166 obj = parse_object(the_repository, &original_oid);
167 if (obj)
168 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
169 return 0;
172 objtype = oid_object_info(the_repository, oid, NULL);
173 if (objtype < 0)
174 return 0;
175 obj = lookup_object_by_type(the_repository, oid, objtype);
177 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
178 struct ref_namespace_info *info = &ref_namespace[i];
180 if (!info->decoration)
181 continue;
182 if (info->exact) {
183 if (!strcmp(refname, info->ref)) {
184 deco_type = info->decoration;
185 break;
187 } else if (starts_with(refname, info->ref)) {
188 deco_type = info->decoration;
189 break;
193 add_name_decoration(deco_type, refname, obj);
194 while (obj->type == OBJ_TAG) {
195 if (!obj->parsed)
196 parse_object(the_repository, &obj->oid);
197 obj = ((struct tag *)obj)->tagged;
198 if (!obj)
199 break;
200 add_name_decoration(DECORATION_REF_TAG, refname, obj);
202 return 0;
205 static int add_graft_decoration(const struct commit_graft *graft,
206 void *cb_data UNUSED)
208 struct commit *commit = lookup_commit(the_repository, &graft->oid);
209 if (!commit)
210 return 0;
211 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
212 return 0;
215 void load_ref_decorations(struct decoration_filter *filter, int flags)
217 if (!decoration_loaded) {
218 if (filter) {
219 struct string_list_item *item;
220 for_each_string_list_item(item, filter->exclude_ref_pattern) {
221 normalize_glob_ref(item, NULL, item->string);
223 for_each_string_list_item(item, filter->include_ref_pattern) {
224 normalize_glob_ref(item, NULL, item->string);
226 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
227 normalize_glob_ref(item, NULL, item->string);
230 decoration_loaded = 1;
231 decoration_flags = flags;
232 for_each_ref(add_ref_decoration, filter);
233 head_ref(add_ref_decoration, filter);
234 for_each_commit_graft(add_graft_decoration, filter);
238 static void show_parents(struct commit *commit, int abbrev, FILE *file)
240 struct commit_list *p;
241 for (p = commit->parents; p ; p = p->next) {
242 struct commit *parent = p->item;
243 fprintf(file, " %s",
244 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
248 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
250 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
251 for ( ; p; p = p->next) {
252 fprintf(opt->diffopt.file, " %s",
253 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
258 * Do we have HEAD in the output, and also the branch it points at?
259 * If so, find that decoration entry for that current branch.
261 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
263 const struct name_decoration *list, *head = NULL;
264 const char *branch_name = NULL;
265 int rru_flags;
267 /* First find HEAD */
268 for (list = decoration; list; list = list->next)
269 if (list->type == DECORATION_REF_HEAD) {
270 head = list;
271 break;
273 if (!head)
274 return NULL;
276 /* Now resolve and find the matching current branch */
277 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
278 if (!branch_name || !(rru_flags & REF_ISSYMREF))
279 return NULL;
281 if (!starts_with(branch_name, "refs/"))
282 return NULL;
284 /* OK, do we have that ref in the list? */
285 for (list = decoration; list; list = list->next)
286 if ((list->type == DECORATION_REF_LOCAL) &&
287 !strcmp(branch_name, list->name)) {
288 return list;
291 return NULL;
294 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
296 if (decoration_flags == DECORATE_SHORT_REFS)
297 strbuf_addstr(sb, prettify_refname(decoration->name));
298 else
299 strbuf_addstr(sb, decoration->name);
303 * The caller makes sure there is no funny color before calling.
304 * format_decorations_extended makes sure the same after return.
306 void format_decorations_extended(struct strbuf *sb,
307 const struct commit *commit,
308 int use_color,
309 const char *prefix,
310 const char *separator,
311 const char *suffix)
313 const struct name_decoration *decoration;
314 const struct name_decoration *current_and_HEAD;
315 const char *color_commit =
316 diff_get_color(use_color, DIFF_COMMIT);
317 const char *color_reset =
318 decorate_get_color(use_color, DECORATION_NONE);
320 decoration = get_name_decoration(&commit->object);
321 if (!decoration)
322 return;
324 current_and_HEAD = current_pointed_by_HEAD(decoration);
325 while (decoration) {
327 * When both current and HEAD are there, only
328 * show HEAD->current where HEAD would have
329 * appeared, skipping the entry for current.
331 if (decoration != current_and_HEAD) {
332 strbuf_addstr(sb, color_commit);
333 strbuf_addstr(sb, prefix);
334 strbuf_addstr(sb, color_reset);
335 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
336 if (decoration->type == DECORATION_REF_TAG)
337 strbuf_addstr(sb, "tag: ");
339 show_name(sb, decoration);
341 if (current_and_HEAD &&
342 decoration->type == DECORATION_REF_HEAD) {
343 strbuf_addstr(sb, " -> ");
344 strbuf_addstr(sb, color_reset);
345 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
346 show_name(sb, current_and_HEAD);
348 strbuf_addstr(sb, color_reset);
350 prefix = separator;
352 decoration = decoration->next;
354 strbuf_addstr(sb, color_commit);
355 strbuf_addstr(sb, suffix);
356 strbuf_addstr(sb, color_reset);
359 void show_decorations(struct rev_info *opt, struct commit *commit)
361 struct strbuf sb = STRBUF_INIT;
363 if (opt->sources) {
364 char **slot = revision_sources_peek(opt->sources, commit);
366 if (slot && *slot)
367 fprintf(opt->diffopt.file, "\t%s", *slot);
369 if (!opt->show_decorations)
370 return;
371 format_decorations(&sb, commit, opt->diffopt.use_color);
372 fputs(sb.buf, opt->diffopt.file);
373 strbuf_release(&sb);
376 static unsigned int digits_in_number(unsigned int number)
378 unsigned int i = 10, result = 1;
379 while (i <= number) {
380 i *= 10;
381 result++;
383 return result;
386 void fmt_output_subject(struct strbuf *filename,
387 const char *subject,
388 struct rev_info *info)
390 const char *suffix = info->patch_suffix;
391 int nr = info->nr;
392 int start_len = filename->len;
393 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
395 if (info->reroll_count) {
396 struct strbuf temp = STRBUF_INIT;
398 strbuf_addf(&temp, "v%s", info->reroll_count);
399 format_sanitized_subject(filename, temp.buf, temp.len);
400 strbuf_addstr(filename, "-");
401 strbuf_release(&temp);
403 strbuf_addf(filename, "%04d-%s", nr, subject);
405 if (max_len < filename->len)
406 strbuf_setlen(filename, max_len);
407 strbuf_addstr(filename, suffix);
410 void fmt_output_commit(struct strbuf *filename,
411 struct commit *commit,
412 struct rev_info *info)
414 struct pretty_print_context ctx = {0};
415 struct strbuf subject = STRBUF_INIT;
417 repo_format_commit_message(the_repository, commit, "%f", &subject,
418 &ctx);
419 fmt_output_subject(filename, subject.buf, info);
420 strbuf_release(&subject);
423 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
425 if (opt->total > 0) {
426 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
427 opt->subject_prefix,
428 *opt->subject_prefix ? " " : "",
429 digits_in_number(opt->total),
430 opt->nr, opt->total);
431 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
432 strbuf_addf(sb, "Subject: [%s] ",
433 opt->subject_prefix);
434 } else {
435 strbuf_addstr(sb, "Subject: ");
439 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
440 const char **extra_headers_p,
441 int *need_8bit_cte_p,
442 int maybe_multipart)
444 const char *extra_headers = opt->extra_headers;
445 const char *name = oid_to_hex(opt->zero_commit ?
446 null_oid() : &commit->object.oid);
448 *need_8bit_cte_p = 0; /* unknown */
450 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
451 graph_show_oneline(opt->graph);
452 if (opt->message_id) {
453 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
454 graph_show_oneline(opt->graph);
456 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
457 int i, n;
458 n = opt->ref_message_ids->nr;
459 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
460 for (i = 0; i < n; i++)
461 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
462 opt->ref_message_ids->items[i].string);
463 graph_show_oneline(opt->graph);
465 if (opt->mime_boundary && maybe_multipart) {
466 static struct strbuf subject_buffer = STRBUF_INIT;
467 static struct strbuf buffer = STRBUF_INIT;
468 struct strbuf filename = STRBUF_INIT;
469 *need_8bit_cte_p = -1; /* NEVER */
471 strbuf_reset(&subject_buffer);
472 strbuf_reset(&buffer);
474 strbuf_addf(&subject_buffer,
475 "%s"
476 "MIME-Version: 1.0\n"
477 "Content-Type: multipart/mixed;"
478 " boundary=\"%s%s\"\n"
479 "\n"
480 "This is a multi-part message in MIME "
481 "format.\n"
482 "--%s%s\n"
483 "Content-Type: text/plain; "
484 "charset=UTF-8; format=fixed\n"
485 "Content-Transfer-Encoding: 8bit\n\n",
486 extra_headers ? extra_headers : "",
487 mime_boundary_leader, opt->mime_boundary,
488 mime_boundary_leader, opt->mime_boundary);
489 extra_headers = subject_buffer.buf;
491 if (opt->numbered_files)
492 strbuf_addf(&filename, "%d", opt->nr);
493 else
494 fmt_output_commit(&filename, commit, opt);
495 strbuf_addf(&buffer,
496 "\n--%s%s\n"
497 "Content-Type: text/x-patch;"
498 " name=\"%s\"\n"
499 "Content-Transfer-Encoding: 8bit\n"
500 "Content-Disposition: %s;"
501 " filename=\"%s\"\n\n",
502 mime_boundary_leader, opt->mime_boundary,
503 filename.buf,
504 opt->no_inline ? "attachment" : "inline",
505 filename.buf);
506 opt->diffopt.stat_sep = buffer.buf;
507 strbuf_release(&filename);
509 *extra_headers_p = extra_headers;
512 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
514 const char *color, *reset, *eol;
516 color = diff_get_color_opt(&opt->diffopt,
517 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
518 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
519 while (*bol) {
520 eol = strchrnul(bol, '\n');
521 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
522 *eol ? "\n" : "");
523 graph_show_oneline(opt->graph);
524 bol = (*eol) ? (eol + 1) : eol;
528 static void show_signature(struct rev_info *opt, struct commit *commit)
530 struct strbuf payload = STRBUF_INIT;
531 struct strbuf signature = STRBUF_INIT;
532 struct signature_check sigc = { 0 };
533 int status;
535 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
536 goto out;
538 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
539 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
540 status = check_signature(&sigc, signature.buf, signature.len);
541 if (status && !sigc.output)
542 show_sig_lines(opt, status, "No signature\n");
543 else
544 show_sig_lines(opt, status, sigc.output);
545 signature_check_clear(&sigc);
547 out:
548 strbuf_release(&payload);
549 strbuf_release(&signature);
552 static int which_parent(const struct object_id *oid, const struct commit *commit)
554 int nth;
555 const struct commit_list *parent;
557 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
558 if (oideq(&parent->item->object.oid, oid))
559 return nth;
560 nth++;
562 return -1;
565 static int is_common_merge(const struct commit *commit)
567 return (commit->parents
568 && commit->parents->next
569 && !commit->parents->next->next);
572 static int show_one_mergetag(struct commit *commit,
573 struct commit_extra_header *extra,
574 void *data)
576 struct rev_info *opt = (struct rev_info *)data;
577 struct object_id oid;
578 struct tag *tag;
579 struct strbuf verify_message;
580 struct signature_check sigc = { 0 };
581 int status, nth;
582 struct strbuf payload = STRBUF_INIT;
583 struct strbuf signature = STRBUF_INIT;
585 hash_object_file(the_hash_algo, extra->value, extra->len,
586 OBJ_TAG, &oid);
587 tag = lookup_tag(the_repository, &oid);
588 if (!tag)
589 return -1; /* error message already given */
591 strbuf_init(&verify_message, 256);
592 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
593 strbuf_addstr(&verify_message, "malformed mergetag\n");
594 else if (is_common_merge(commit) &&
595 oideq(&tag->tagged->oid,
596 &commit->parents->next->item->object.oid))
597 strbuf_addf(&verify_message,
598 "merged tag '%s'\n", tag->tag);
599 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
600 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
601 tag->tag, oid_to_hex(&tag->tagged->oid));
602 else
603 strbuf_addf(&verify_message,
604 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
606 status = -1;
607 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
608 /* could have a good signature */
609 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
610 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
611 status = check_signature(&sigc, signature.buf, signature.len);
612 if (sigc.output)
613 strbuf_addstr(&verify_message, sigc.output);
614 else
615 strbuf_addstr(&verify_message, "No signature\n");
616 signature_check_clear(&sigc);
617 /* otherwise we couldn't verify, which is shown as bad */
620 show_sig_lines(opt, status, verify_message.buf);
621 strbuf_release(&verify_message);
622 strbuf_release(&payload);
623 strbuf_release(&signature);
624 return 0;
627 static int show_mergetag(struct rev_info *opt, struct commit *commit)
629 return for_each_mergetag(show_one_mergetag, commit, opt);
632 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
634 const char *x = opt->shown_dashes ? "\n" : "---\n";
635 if (sb)
636 strbuf_addstr(sb, x);
637 else
638 fputs(x, opt->diffopt.file);
639 opt->shown_dashes = 1;
642 void show_log(struct rev_info *opt)
644 struct strbuf msgbuf = STRBUF_INIT;
645 struct log_info *log = opt->loginfo;
646 struct commit *commit = log->commit, *parent = log->parent;
647 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
648 const char *extra_headers = opt->extra_headers;
649 struct pretty_print_context ctx = {0};
651 opt->loginfo = NULL;
652 if (!opt->verbose_header) {
653 graph_show_commit(opt->graph);
655 if (!opt->graph)
656 put_revision_mark(opt, commit);
657 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
658 opt->diffopt.file);
659 if (opt->print_parents)
660 show_parents(commit, abbrev_commit, opt->diffopt.file);
661 if (opt->children.name)
662 show_children(opt, commit, abbrev_commit);
663 show_decorations(opt, commit);
664 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
665 putc('\n', opt->diffopt.file);
666 graph_show_remainder(opt->graph);
668 putc(opt->diffopt.line_termination, opt->diffopt.file);
669 return;
673 * If use_terminator is set, we already handled any record termination
674 * at the end of the last record.
675 * Otherwise, add a diffopt.line_termination character before all
676 * entries but the first. (IOW, as a separator between entries)
678 if (opt->shown_one && !opt->use_terminator) {
680 * If entries are separated by a newline, the output
681 * should look human-readable. If the last entry ended
682 * with a newline, print the graph output before this
683 * newline. Otherwise it will end up as a completely blank
684 * line and will look like a gap in the graph.
686 * If the entry separator is not a newline, the output is
687 * primarily intended for programmatic consumption, and we
688 * never want the extra graph output before the entry
689 * separator.
691 if (opt->diffopt.line_termination == '\n' &&
692 !opt->missing_newline)
693 graph_show_padding(opt->graph);
694 putc(opt->diffopt.line_termination, opt->diffopt.file);
696 opt->shown_one = 1;
699 * If the history graph was requested,
700 * print the graph, up to this commit's line
702 graph_show_commit(opt->graph);
705 * Print header line of header..
708 if (cmit_fmt_is_mail(opt->commit_format)) {
709 log_write_email_headers(opt, commit, &extra_headers,
710 &ctx.need_8bit_cte, 1);
711 ctx.rev = opt;
712 ctx.print_email_subject = 1;
713 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
714 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
715 if (opt->commit_format != CMIT_FMT_ONELINE)
716 fputs("commit ", opt->diffopt.file);
718 if (!opt->graph)
719 put_revision_mark(opt, commit);
720 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
721 abbrev_commit),
722 opt->diffopt.file);
723 if (opt->print_parents)
724 show_parents(commit, abbrev_commit, opt->diffopt.file);
725 if (opt->children.name)
726 show_children(opt, commit, abbrev_commit);
727 if (parent)
728 fprintf(opt->diffopt.file, " (from %s)",
729 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
730 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
731 show_decorations(opt, commit);
732 if (opt->commit_format == CMIT_FMT_ONELINE) {
733 putc(' ', opt->diffopt.file);
734 } else {
735 putc('\n', opt->diffopt.file);
736 graph_show_oneline(opt->graph);
738 if (opt->reflog_info) {
740 * setup_revisions() ensures that opt->reflog_info
741 * and opt->graph cannot both be set,
742 * so we don't need to worry about printing the
743 * graph info here.
745 show_reflog_message(opt->reflog_info,
746 opt->commit_format == CMIT_FMT_ONELINE,
747 &opt->date_mode,
748 opt->date_mode_explicit);
749 if (opt->commit_format == CMIT_FMT_ONELINE)
750 return;
754 if (opt->show_signature) {
755 show_signature(opt, commit);
756 show_mergetag(opt, commit);
759 if (opt->show_notes) {
760 int raw;
761 struct strbuf notebuf = STRBUF_INIT;
763 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
764 format_display_notes(&commit->object.oid, &notebuf,
765 get_log_output_encoding(), raw);
766 ctx.notes_message = strbuf_detach(&notebuf, NULL);
770 * And then the pretty-printed message itself
772 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
773 ctx.need_8bit_cte =
774 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
775 ctx.date_mode = opt->date_mode;
776 ctx.date_mode_explicit = opt->date_mode_explicit;
777 ctx.abbrev = opt->diffopt.abbrev;
778 ctx.after_subject = extra_headers;
779 ctx.preserve_subject = opt->preserve_subject;
780 ctx.encode_email_headers = opt->encode_email_headers;
781 ctx.reflog_info = opt->reflog_info;
782 ctx.fmt = opt->commit_format;
783 ctx.mailmap = opt->mailmap;
784 ctx.color = opt->diffopt.use_color;
785 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
786 ctx.output_encoding = get_log_output_encoding();
787 ctx.rev = opt;
788 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
789 ctx.from_ident = &opt->from_ident;
790 if (opt->graph)
791 ctx.graph_width = graph_width(opt->graph);
792 pretty_print_commit(&ctx, commit, &msgbuf);
794 if (opt->add_signoff)
795 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
797 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
798 ctx.notes_message && *ctx.notes_message) {
799 if (cmit_fmt_is_mail(ctx.fmt))
800 next_commentary_block(opt, &msgbuf);
801 strbuf_addstr(&msgbuf, ctx.notes_message);
804 if (opt->show_log_size) {
805 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
806 graph_show_oneline(opt->graph);
810 * Set opt->missing_newline if msgbuf doesn't
811 * end in a newline (including if it is empty)
813 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
814 opt->missing_newline = 1;
815 else
816 opt->missing_newline = 0;
818 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
819 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
820 if (!opt->missing_newline)
821 graph_show_padding(opt->graph);
822 putc(opt->diffopt.line_termination, opt->diffopt.file);
825 strbuf_release(&msgbuf);
826 free(ctx.notes_message);
828 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
829 struct diff_queue_struct dq;
831 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
832 DIFF_QUEUE_CLEAR(&diff_queued_diff);
834 next_commentary_block(opt, NULL);
835 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
836 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
837 &opt->diffopt);
839 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
842 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
843 struct diff_queue_struct dq;
844 struct diff_options opts;
845 struct range_diff_options range_diff_opts = {
846 .creation_factor = opt->creation_factor,
847 .dual_color = 1,
848 .diffopt = &opts
851 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
852 DIFF_QUEUE_CLEAR(&diff_queued_diff);
854 next_commentary_block(opt, NULL);
855 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
857 * Pass minimum required diff-options to range-diff; others
858 * can be added later if deemed desirable.
860 repo_diff_setup(the_repository, &opts);
861 opts.file = opt->diffopt.file;
862 opts.use_color = opt->diffopt.use_color;
863 diff_setup_done(&opts);
864 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
866 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
870 int log_tree_diff_flush(struct rev_info *opt)
872 opt->shown_dashes = 0;
873 diffcore_std(&opt->diffopt);
875 if (diff_queue_is_empty(&opt->diffopt)) {
876 int saved_fmt = opt->diffopt.output_format;
877 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
878 diff_flush(&opt->diffopt);
879 opt->diffopt.output_format = saved_fmt;
880 return 0;
883 if (opt->loginfo && !opt->no_commit_id) {
884 show_log(opt);
885 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
886 opt->verbose_header &&
887 opt->commit_format != CMIT_FMT_ONELINE &&
888 !commit_format_is_empty(opt->commit_format)) {
890 * When showing a verbose header (i.e. log message),
891 * and not in --pretty=oneline format, we would want
892 * an extra newline between the end of log and the
893 * diff/diffstat output for readability.
895 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
896 if (opt->diffopt.output_prefix) {
897 struct strbuf *msg = NULL;
898 msg = opt->diffopt.output_prefix(&opt->diffopt,
899 opt->diffopt.output_prefix_data);
900 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
904 * We may have shown three-dashes line early
905 * between generated commentary (notes, etc.)
906 * and the log message, in which case we only
907 * want a blank line after the commentary
908 * without (an extra) three-dashes line.
909 * Otherwise, we show the three-dashes line if
910 * we are showing the patch with diffstat, but
911 * in that case, there is no extra blank line
912 * after the three-dashes line.
914 if (!opt->shown_dashes &&
915 (pch & opt->diffopt.output_format) == pch)
916 fprintf(opt->diffopt.file, "---");
917 putc('\n', opt->diffopt.file);
920 diff_flush(&opt->diffopt);
921 return 1;
924 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
926 diff_tree_combined_merge(commit, opt);
927 return !opt->loginfo;
930 static void setup_additional_headers(struct diff_options *o,
931 struct strmap *all_headers)
933 struct hashmap_iter iter;
934 struct strmap_entry *entry;
937 * Make o->additional_path_headers contain the subset of all_headers
938 * that match o->pathspec. If there aren't any that match o->pathspec,
939 * then make o->additional_path_headers be NULL.
942 if (!o->pathspec.nr) {
943 o->additional_path_headers = all_headers;
944 return;
947 o->additional_path_headers = xmalloc(sizeof(struct strmap));
948 strmap_init_with_options(o->additional_path_headers, NULL, 0);
949 strmap_for_each_entry(all_headers, &iter, entry) {
950 if (match_pathspec(the_repository->index, &o->pathspec,
951 entry->key, strlen(entry->key),
952 0 /* prefix */, NULL /* seen */,
953 0 /* is_dir */))
954 strmap_put(o->additional_path_headers,
955 entry->key, entry->value);
957 if (!strmap_get_size(o->additional_path_headers)) {
958 strmap_clear(o->additional_path_headers, 0);
959 FREE_AND_NULL(o->additional_path_headers);
963 static void cleanup_additional_headers(struct diff_options *o)
965 if (!o->pathspec.nr) {
966 o->additional_path_headers = NULL;
967 return;
969 if (!o->additional_path_headers)
970 return;
972 strmap_clear(o->additional_path_headers, 0);
973 FREE_AND_NULL(o->additional_path_headers);
976 static int do_remerge_diff(struct rev_info *opt,
977 struct commit_list *parents,
978 struct object_id *oid)
980 struct merge_options o;
981 struct commit_list *bases;
982 struct merge_result res = {0};
983 struct pretty_print_context ctx = {0};
984 struct commit *parent1 = parents->item;
985 struct commit *parent2 = parents->next->item;
986 struct strbuf parent1_desc = STRBUF_INIT;
987 struct strbuf parent2_desc = STRBUF_INIT;
989 /* Setup merge options */
990 init_merge_options(&o, the_repository);
991 o.show_rename_progress = 0;
992 o.record_conflict_msgs_as_headers = 1;
993 o.msg_header_prefix = "remerge";
995 ctx.abbrev = DEFAULT_ABBREV;
996 repo_format_commit_message(the_repository, parent1, "%h (%s)",
997 &parent1_desc, &ctx);
998 repo_format_commit_message(the_repository, parent2, "%h (%s)",
999 &parent2_desc, &ctx);
1000 o.branch1 = parent1_desc.buf;
1001 o.branch2 = parent2_desc.buf;
1003 /* Parse the relevant commits and get the merge bases */
1004 parse_commit_or_die(parent1);
1005 parse_commit_or_die(parent2);
1006 bases = repo_get_merge_bases(the_repository, parent1, parent2);
1008 /* Re-merge the parents */
1009 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1011 /* Show the diff */
1012 setup_additional_headers(&opt->diffopt, res.path_messages);
1013 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1014 log_tree_diff_flush(opt);
1016 /* Cleanup */
1017 cleanup_additional_headers(&opt->diffopt);
1018 strbuf_release(&parent1_desc);
1019 strbuf_release(&parent2_desc);
1020 merge_finalize(&o, &res);
1022 /* Clean up the contents of the temporary object directory */
1023 if (opt->remerge_objdir)
1024 tmp_objdir_discard_objects(opt->remerge_objdir);
1025 else
1026 BUG("did a remerge diff without remerge_objdir?!?");
1028 return !opt->loginfo;
1032 * Show the diff of a commit.
1034 * Return true if we printed any log info messages
1036 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1038 int showed_log;
1039 struct commit_list *parents;
1040 struct object_id *oid;
1041 int is_merge;
1042 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1044 if (!all_need_diff && !opt->merges_need_diff)
1045 return 0;
1047 parse_commit_or_die(commit);
1048 oid = get_commit_tree_oid(commit);
1050 parents = get_saved_parents(opt, commit);
1051 is_merge = parents && parents->next;
1052 if (!is_merge && !all_need_diff)
1053 return 0;
1055 /* Root commit? */
1056 if (!parents) {
1057 if (opt->show_root_diff) {
1058 diff_root_tree_oid(oid, "", &opt->diffopt);
1059 log_tree_diff_flush(opt);
1061 return !opt->loginfo;
1064 if (is_merge) {
1065 int octopus = (parents->next->next != NULL);
1067 if (opt->remerge_diff) {
1068 if (octopus) {
1069 show_log(opt);
1070 fprintf(opt->diffopt.file,
1071 "diff: warning: Skipping remerge-diff "
1072 "for octopus merges.\n");
1073 return 1;
1075 return do_remerge_diff(opt, parents, oid);
1077 if (opt->combine_merges)
1078 return do_diff_combined(opt, commit);
1079 if (opt->separate_merges) {
1080 if (!opt->first_parent_merges) {
1081 /* Show parent info for multiple diffs */
1082 log->parent = parents->item;
1084 } else
1085 return 0;
1088 showed_log = 0;
1089 for (;;) {
1090 struct commit *parent = parents->item;
1092 parse_commit_or_die(parent);
1093 diff_tree_oid(get_commit_tree_oid(parent),
1094 oid, "", &opt->diffopt);
1095 log_tree_diff_flush(opt);
1097 showed_log |= !opt->loginfo;
1099 /* Set up the log info for the next parent, if any.. */
1100 parents = parents->next;
1101 if (!parents || opt->first_parent_merges)
1102 break;
1103 log->parent = parents->item;
1104 opt->loginfo = log;
1106 return showed_log;
1109 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1111 struct log_info log;
1112 int shown;
1113 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1114 int no_free = opt->diffopt.no_free;
1116 log.commit = commit;
1117 log.parent = NULL;
1118 opt->loginfo = &log;
1119 opt->diffopt.no_free = 1;
1121 /* NEEDSWORK: no restoring of no_free? Why? */
1122 if (opt->line_level_traverse)
1123 return line_log_print(opt, commit);
1125 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1126 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1127 shown = log_tree_diff(opt, commit, &log);
1128 if (!shown && opt->loginfo && opt->always_show_header) {
1129 log.parent = NULL;
1130 show_log(opt);
1131 shown = 1;
1133 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1134 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1135 opt->loginfo = NULL;
1136 maybe_flush_or_die(opt->diffopt.file, "stdout");
1137 opt->diffopt.no_free = no_free;
1138 diff_free(&opt->diffopt);
1139 return shown;