object.h: move some inline functions and defines from cache.h
[alt-git.git] / log-tree.c
blobf0e885635e062cbea4abd3e456d259d39e3f2d72
1 #include "cache.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 "write-or-die.h"
29 static struct decoration name_decoration = { "object names" };
30 static int decoration_loaded;
31 static int decoration_flags;
33 static char decoration_colors[][COLOR_MAXLEN] = {
34 GIT_COLOR_RESET,
35 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
36 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
37 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
38 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
39 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
40 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
43 static const char *color_decorate_slots[] = {
44 [DECORATION_REF_LOCAL] = "branch",
45 [DECORATION_REF_REMOTE] = "remoteBranch",
46 [DECORATION_REF_TAG] = "tag",
47 [DECORATION_REF_STASH] = "stash",
48 [DECORATION_REF_HEAD] = "HEAD",
49 [DECORATION_GRAFTED] = "grafted",
52 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
54 if (want_color(decorate_use_color))
55 return decoration_colors[ix];
56 return "";
59 define_list_config_array(color_decorate_slots);
61 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
63 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
64 if (slot < 0)
65 return 0;
66 if (!value)
67 return config_error_nonbool(var);
68 return color_parse(value, decoration_colors[slot]);
72 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
73 * for showing the commit sha1, use the same check for --decorate
75 #define decorate_get_color_opt(o, ix) \
76 decorate_get_color((o)->use_color, ix)
78 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
80 struct name_decoration *res;
81 FLEX_ALLOC_STR(res, name, name);
82 res->type = type;
83 res->next = add_decoration(&name_decoration, obj, res);
86 const struct name_decoration *get_name_decoration(const struct object *obj)
88 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
89 return lookup_decoration(&name_decoration, obj);
92 static int match_ref_pattern(const char *refname,
93 const struct string_list_item *item)
95 int matched = 0;
96 if (!item->util) {
97 if (!wildmatch(item->string, refname, 0))
98 matched = 1;
99 } else {
100 const char *rest;
101 if (skip_prefix(refname, item->string, &rest) &&
102 (!*rest || *rest == '/'))
103 matched = 1;
105 return matched;
108 static int ref_filter_match(const char *refname,
109 const struct decoration_filter *filter)
111 struct string_list_item *item;
112 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
113 const struct string_list *include_patterns = filter->include_ref_pattern;
114 const struct string_list *exclude_patterns_config =
115 filter->exclude_ref_config_pattern;
117 if (exclude_patterns && exclude_patterns->nr) {
118 for_each_string_list_item(item, exclude_patterns) {
119 if (match_ref_pattern(refname, item))
120 return 0;
124 if (include_patterns && include_patterns->nr) {
125 for_each_string_list_item(item, include_patterns) {
126 if (match_ref_pattern(refname, item))
127 return 1;
129 return 0;
132 if (exclude_patterns_config && exclude_patterns_config->nr) {
133 for_each_string_list_item(item, exclude_patterns_config) {
134 if (match_ref_pattern(refname, item))
135 return 0;
139 return 1;
142 static int add_ref_decoration(const char *refname, const struct object_id *oid,
143 int flags UNUSED,
144 void *cb_data)
146 int i;
147 struct object *obj;
148 enum object_type objtype;
149 enum decoration_type deco_type = DECORATION_NONE;
150 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
151 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
153 if (filter && !ref_filter_match(refname, filter))
154 return 0;
156 if (starts_with(refname, git_replace_ref_base)) {
157 struct object_id original_oid;
158 if (!read_replace_refs)
159 return 0;
160 if (get_oid_hex(refname + strlen(git_replace_ref_base),
161 &original_oid)) {
162 warning("invalid replace ref %s", refname);
163 return 0;
165 obj = parse_object(the_repository, &original_oid);
166 if (obj)
167 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
168 return 0;
171 objtype = oid_object_info(the_repository, oid, NULL);
172 if (objtype < 0)
173 return 0;
174 obj = lookup_object_by_type(the_repository, oid, objtype);
176 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
177 struct ref_namespace_info *info = &ref_namespace[i];
179 if (!info->decoration)
180 continue;
181 if (info->exact) {
182 if (!strcmp(refname, info->ref)) {
183 deco_type = info->decoration;
184 break;
186 } else if (starts_with(refname, info->ref)) {
187 deco_type = info->decoration;
188 break;
192 add_name_decoration(deco_type, refname, obj);
193 while (obj->type == OBJ_TAG) {
194 if (!obj->parsed)
195 parse_object(the_repository, &obj->oid);
196 obj = ((struct tag *)obj)->tagged;
197 if (!obj)
198 break;
199 add_name_decoration(DECORATION_REF_TAG, refname, obj);
201 return 0;
204 static int add_graft_decoration(const struct commit_graft *graft,
205 void *cb_data UNUSED)
207 struct commit *commit = lookup_commit(the_repository, &graft->oid);
208 if (!commit)
209 return 0;
210 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
211 return 0;
214 void load_ref_decorations(struct decoration_filter *filter, int flags)
216 if (!decoration_loaded) {
217 if (filter) {
218 struct string_list_item *item;
219 for_each_string_list_item(item, filter->exclude_ref_pattern) {
220 normalize_glob_ref(item, NULL, item->string);
222 for_each_string_list_item(item, filter->include_ref_pattern) {
223 normalize_glob_ref(item, NULL, item->string);
225 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
226 normalize_glob_ref(item, NULL, item->string);
229 decoration_loaded = 1;
230 decoration_flags = flags;
231 for_each_ref(add_ref_decoration, filter);
232 head_ref(add_ref_decoration, filter);
233 for_each_commit_graft(add_graft_decoration, filter);
237 static void show_parents(struct commit *commit, int abbrev, FILE *file)
239 struct commit_list *p;
240 for (p = commit->parents; p ; p = p->next) {
241 struct commit *parent = p->item;
242 fprintf(file, " %s",
243 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
247 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
249 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
250 for ( ; p; p = p->next) {
251 fprintf(opt->diffopt.file, " %s",
252 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
257 * Do we have HEAD in the output, and also the branch it points at?
258 * If so, find that decoration entry for that current branch.
260 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
262 const struct name_decoration *list, *head = NULL;
263 const char *branch_name = NULL;
264 int rru_flags;
266 /* First find HEAD */
267 for (list = decoration; list; list = list->next)
268 if (list->type == DECORATION_REF_HEAD) {
269 head = list;
270 break;
272 if (!head)
273 return NULL;
275 /* Now resolve and find the matching current branch */
276 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
277 if (!branch_name || !(rru_flags & REF_ISSYMREF))
278 return NULL;
280 if (!starts_with(branch_name, "refs/"))
281 return NULL;
283 /* OK, do we have that ref in the list? */
284 for (list = decoration; list; list = list->next)
285 if ((list->type == DECORATION_REF_LOCAL) &&
286 !strcmp(branch_name, list->name)) {
287 return list;
290 return NULL;
293 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
295 if (decoration_flags == DECORATE_SHORT_REFS)
296 strbuf_addstr(sb, prettify_refname(decoration->name));
297 else
298 strbuf_addstr(sb, decoration->name);
302 * The caller makes sure there is no funny color before calling.
303 * format_decorations_extended makes sure the same after return.
305 void format_decorations_extended(struct strbuf *sb,
306 const struct commit *commit,
307 int use_color,
308 const char *prefix,
309 const char *separator,
310 const char *suffix)
312 const struct name_decoration *decoration;
313 const struct name_decoration *current_and_HEAD;
314 const char *color_commit =
315 diff_get_color(use_color, DIFF_COMMIT);
316 const char *color_reset =
317 decorate_get_color(use_color, DECORATION_NONE);
319 decoration = get_name_decoration(&commit->object);
320 if (!decoration)
321 return;
323 current_and_HEAD = current_pointed_by_HEAD(decoration);
324 while (decoration) {
326 * When both current and HEAD are there, only
327 * show HEAD->current where HEAD would have
328 * appeared, skipping the entry for current.
330 if (decoration != current_and_HEAD) {
331 strbuf_addstr(sb, color_commit);
332 strbuf_addstr(sb, prefix);
333 strbuf_addstr(sb, color_reset);
334 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
335 if (decoration->type == DECORATION_REF_TAG)
336 strbuf_addstr(sb, "tag: ");
338 show_name(sb, decoration);
340 if (current_and_HEAD &&
341 decoration->type == DECORATION_REF_HEAD) {
342 strbuf_addstr(sb, " -> ");
343 strbuf_addstr(sb, color_reset);
344 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
345 show_name(sb, current_and_HEAD);
347 strbuf_addstr(sb, color_reset);
349 prefix = separator;
351 decoration = decoration->next;
353 strbuf_addstr(sb, color_commit);
354 strbuf_addstr(sb, suffix);
355 strbuf_addstr(sb, color_reset);
358 void show_decorations(struct rev_info *opt, struct commit *commit)
360 struct strbuf sb = STRBUF_INIT;
362 if (opt->sources) {
363 char **slot = revision_sources_peek(opt->sources, commit);
365 if (slot && *slot)
366 fprintf(opt->diffopt.file, "\t%s", *slot);
368 if (!opt->show_decorations)
369 return;
370 format_decorations(&sb, commit, opt->diffopt.use_color);
371 fputs(sb.buf, opt->diffopt.file);
372 strbuf_release(&sb);
375 static unsigned int digits_in_number(unsigned int number)
377 unsigned int i = 10, result = 1;
378 while (i <= number) {
379 i *= 10;
380 result++;
382 return result;
385 void fmt_output_subject(struct strbuf *filename,
386 const char *subject,
387 struct rev_info *info)
389 const char *suffix = info->patch_suffix;
390 int nr = info->nr;
391 int start_len = filename->len;
392 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
394 if (info->reroll_count) {
395 struct strbuf temp = STRBUF_INIT;
397 strbuf_addf(&temp, "v%s", info->reroll_count);
398 format_sanitized_subject(filename, temp.buf, temp.len);
399 strbuf_addstr(filename, "-");
400 strbuf_release(&temp);
402 strbuf_addf(filename, "%04d-%s", nr, subject);
404 if (max_len < filename->len)
405 strbuf_setlen(filename, max_len);
406 strbuf_addstr(filename, suffix);
409 void fmt_output_commit(struct strbuf *filename,
410 struct commit *commit,
411 struct rev_info *info)
413 struct pretty_print_context ctx = {0};
414 struct strbuf subject = STRBUF_INIT;
416 repo_format_commit_message(the_repository, commit, "%f", &subject,
417 &ctx);
418 fmt_output_subject(filename, subject.buf, info);
419 strbuf_release(&subject);
422 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
424 if (opt->total > 0) {
425 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
426 opt->subject_prefix,
427 *opt->subject_prefix ? " " : "",
428 digits_in_number(opt->total),
429 opt->nr, opt->total);
430 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
431 strbuf_addf(sb, "Subject: [%s] ",
432 opt->subject_prefix);
433 } else {
434 strbuf_addstr(sb, "Subject: ");
438 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
439 const char **extra_headers_p,
440 int *need_8bit_cte_p,
441 int maybe_multipart)
443 const char *extra_headers = opt->extra_headers;
444 const char *name = oid_to_hex(opt->zero_commit ?
445 null_oid() : &commit->object.oid);
447 *need_8bit_cte_p = 0; /* unknown */
449 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
450 graph_show_oneline(opt->graph);
451 if (opt->message_id) {
452 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
453 graph_show_oneline(opt->graph);
455 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
456 int i, n;
457 n = opt->ref_message_ids->nr;
458 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
459 for (i = 0; i < n; i++)
460 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
461 opt->ref_message_ids->items[i].string);
462 graph_show_oneline(opt->graph);
464 if (opt->mime_boundary && maybe_multipart) {
465 static struct strbuf subject_buffer = STRBUF_INIT;
466 static struct strbuf buffer = STRBUF_INIT;
467 struct strbuf filename = STRBUF_INIT;
468 *need_8bit_cte_p = -1; /* NEVER */
470 strbuf_reset(&subject_buffer);
471 strbuf_reset(&buffer);
473 strbuf_addf(&subject_buffer,
474 "%s"
475 "MIME-Version: 1.0\n"
476 "Content-Type: multipart/mixed;"
477 " boundary=\"%s%s\"\n"
478 "\n"
479 "This is a multi-part message in MIME "
480 "format.\n"
481 "--%s%s\n"
482 "Content-Type: text/plain; "
483 "charset=UTF-8; format=fixed\n"
484 "Content-Transfer-Encoding: 8bit\n\n",
485 extra_headers ? extra_headers : "",
486 mime_boundary_leader, opt->mime_boundary,
487 mime_boundary_leader, opt->mime_boundary);
488 extra_headers = subject_buffer.buf;
490 if (opt->numbered_files)
491 strbuf_addf(&filename, "%d", opt->nr);
492 else
493 fmt_output_commit(&filename, commit, opt);
494 strbuf_addf(&buffer,
495 "\n--%s%s\n"
496 "Content-Type: text/x-patch;"
497 " name=\"%s\"\n"
498 "Content-Transfer-Encoding: 8bit\n"
499 "Content-Disposition: %s;"
500 " filename=\"%s\"\n\n",
501 mime_boundary_leader, opt->mime_boundary,
502 filename.buf,
503 opt->no_inline ? "attachment" : "inline",
504 filename.buf);
505 opt->diffopt.stat_sep = buffer.buf;
506 strbuf_release(&filename);
508 *extra_headers_p = extra_headers;
511 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
513 const char *color, *reset, *eol;
515 color = diff_get_color_opt(&opt->diffopt,
516 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
517 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
518 while (*bol) {
519 eol = strchrnul(bol, '\n');
520 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
521 *eol ? "\n" : "");
522 graph_show_oneline(opt->graph);
523 bol = (*eol) ? (eol + 1) : eol;
527 static void show_signature(struct rev_info *opt, struct commit *commit)
529 struct strbuf payload = STRBUF_INIT;
530 struct strbuf signature = STRBUF_INIT;
531 struct signature_check sigc = { 0 };
532 int status;
534 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
535 goto out;
537 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
538 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
539 status = check_signature(&sigc, signature.buf, signature.len);
540 if (status && !sigc.output)
541 show_sig_lines(opt, status, "No signature\n");
542 else
543 show_sig_lines(opt, status, sigc.output);
544 signature_check_clear(&sigc);
546 out:
547 strbuf_release(&payload);
548 strbuf_release(&signature);
551 static int which_parent(const struct object_id *oid, const struct commit *commit)
553 int nth;
554 const struct commit_list *parent;
556 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
557 if (oideq(&parent->item->object.oid, oid))
558 return nth;
559 nth++;
561 return -1;
564 static int is_common_merge(const struct commit *commit)
566 return (commit->parents
567 && commit->parents->next
568 && !commit->parents->next->next);
571 static int show_one_mergetag(struct commit *commit,
572 struct commit_extra_header *extra,
573 void *data)
575 struct rev_info *opt = (struct rev_info *)data;
576 struct object_id oid;
577 struct tag *tag;
578 struct strbuf verify_message;
579 struct signature_check sigc = { 0 };
580 int status, nth;
581 struct strbuf payload = STRBUF_INIT;
582 struct strbuf signature = STRBUF_INIT;
584 hash_object_file(the_hash_algo, extra->value, extra->len,
585 OBJ_TAG, &oid);
586 tag = lookup_tag(the_repository, &oid);
587 if (!tag)
588 return -1; /* error message already given */
590 strbuf_init(&verify_message, 256);
591 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
592 strbuf_addstr(&verify_message, "malformed mergetag\n");
593 else if (is_common_merge(commit) &&
594 oideq(&tag->tagged->oid,
595 &commit->parents->next->item->object.oid))
596 strbuf_addf(&verify_message,
597 "merged tag '%s'\n", tag->tag);
598 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
599 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
600 tag->tag, oid_to_hex(&tag->tagged->oid));
601 else
602 strbuf_addf(&verify_message,
603 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
605 status = -1;
606 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
607 /* could have a good signature */
608 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
609 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
610 status = check_signature(&sigc, signature.buf, signature.len);
611 if (sigc.output)
612 strbuf_addstr(&verify_message, sigc.output);
613 else
614 strbuf_addstr(&verify_message, "No signature\n");
615 signature_check_clear(&sigc);
616 /* otherwise we couldn't verify, which is shown as bad */
619 show_sig_lines(opt, status, verify_message.buf);
620 strbuf_release(&verify_message);
621 strbuf_release(&payload);
622 strbuf_release(&signature);
623 return 0;
626 static int show_mergetag(struct rev_info *opt, struct commit *commit)
628 return for_each_mergetag(show_one_mergetag, commit, opt);
631 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
633 const char *x = opt->shown_dashes ? "\n" : "---\n";
634 if (sb)
635 strbuf_addstr(sb, x);
636 else
637 fputs(x, opt->diffopt.file);
638 opt->shown_dashes = 1;
641 void show_log(struct rev_info *opt)
643 struct strbuf msgbuf = STRBUF_INIT;
644 struct log_info *log = opt->loginfo;
645 struct commit *commit = log->commit, *parent = log->parent;
646 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
647 const char *extra_headers = opt->extra_headers;
648 struct pretty_print_context ctx = {0};
650 opt->loginfo = NULL;
651 if (!opt->verbose_header) {
652 graph_show_commit(opt->graph);
654 if (!opt->graph)
655 put_revision_mark(opt, commit);
656 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
657 opt->diffopt.file);
658 if (opt->print_parents)
659 show_parents(commit, abbrev_commit, opt->diffopt.file);
660 if (opt->children.name)
661 show_children(opt, commit, abbrev_commit);
662 show_decorations(opt, commit);
663 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
664 putc('\n', opt->diffopt.file);
665 graph_show_remainder(opt->graph);
667 putc(opt->diffopt.line_termination, opt->diffopt.file);
668 return;
672 * If use_terminator is set, we already handled any record termination
673 * at the end of the last record.
674 * Otherwise, add a diffopt.line_termination character before all
675 * entries but the first. (IOW, as a separator between entries)
677 if (opt->shown_one && !opt->use_terminator) {
679 * If entries are separated by a newline, the output
680 * should look human-readable. If the last entry ended
681 * with a newline, print the graph output before this
682 * newline. Otherwise it will end up as a completely blank
683 * line and will look like a gap in the graph.
685 * If the entry separator is not a newline, the output is
686 * primarily intended for programmatic consumption, and we
687 * never want the extra graph output before the entry
688 * separator.
690 if (opt->diffopt.line_termination == '\n' &&
691 !opt->missing_newline)
692 graph_show_padding(opt->graph);
693 putc(opt->diffopt.line_termination, opt->diffopt.file);
695 opt->shown_one = 1;
698 * If the history graph was requested,
699 * print the graph, up to this commit's line
701 graph_show_commit(opt->graph);
704 * Print header line of header..
707 if (cmit_fmt_is_mail(opt->commit_format)) {
708 log_write_email_headers(opt, commit, &extra_headers,
709 &ctx.need_8bit_cte, 1);
710 ctx.rev = opt;
711 ctx.print_email_subject = 1;
712 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
713 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
714 if (opt->commit_format != CMIT_FMT_ONELINE)
715 fputs("commit ", opt->diffopt.file);
717 if (!opt->graph)
718 put_revision_mark(opt, commit);
719 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
720 abbrev_commit),
721 opt->diffopt.file);
722 if (opt->print_parents)
723 show_parents(commit, abbrev_commit, opt->diffopt.file);
724 if (opt->children.name)
725 show_children(opt, commit, abbrev_commit);
726 if (parent)
727 fprintf(opt->diffopt.file, " (from %s)",
728 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
729 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
730 show_decorations(opt, commit);
731 if (opt->commit_format == CMIT_FMT_ONELINE) {
732 putc(' ', opt->diffopt.file);
733 } else {
734 putc('\n', opt->diffopt.file);
735 graph_show_oneline(opt->graph);
737 if (opt->reflog_info) {
739 * setup_revisions() ensures that opt->reflog_info
740 * and opt->graph cannot both be set,
741 * so we don't need to worry about printing the
742 * graph info here.
744 show_reflog_message(opt->reflog_info,
745 opt->commit_format == CMIT_FMT_ONELINE,
746 &opt->date_mode,
747 opt->date_mode_explicit);
748 if (opt->commit_format == CMIT_FMT_ONELINE)
749 return;
753 if (opt->show_signature) {
754 show_signature(opt, commit);
755 show_mergetag(opt, commit);
758 if (opt->show_notes) {
759 int raw;
760 struct strbuf notebuf = STRBUF_INIT;
762 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
763 format_display_notes(&commit->object.oid, &notebuf,
764 get_log_output_encoding(), raw);
765 ctx.notes_message = strbuf_detach(&notebuf, NULL);
769 * And then the pretty-printed message itself
771 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
772 ctx.need_8bit_cte =
773 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
774 ctx.date_mode = opt->date_mode;
775 ctx.date_mode_explicit = opt->date_mode_explicit;
776 ctx.abbrev = opt->diffopt.abbrev;
777 ctx.after_subject = extra_headers;
778 ctx.preserve_subject = opt->preserve_subject;
779 ctx.encode_email_headers = opt->encode_email_headers;
780 ctx.reflog_info = opt->reflog_info;
781 ctx.fmt = opt->commit_format;
782 ctx.mailmap = opt->mailmap;
783 ctx.color = opt->diffopt.use_color;
784 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
785 ctx.output_encoding = get_log_output_encoding();
786 ctx.rev = opt;
787 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
788 ctx.from_ident = &opt->from_ident;
789 if (opt->graph)
790 ctx.graph_width = graph_width(opt->graph);
791 pretty_print_commit(&ctx, commit, &msgbuf);
793 if (opt->add_signoff)
794 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
796 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
797 ctx.notes_message && *ctx.notes_message) {
798 if (cmit_fmt_is_mail(ctx.fmt))
799 next_commentary_block(opt, &msgbuf);
800 strbuf_addstr(&msgbuf, ctx.notes_message);
803 if (opt->show_log_size) {
804 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
805 graph_show_oneline(opt->graph);
809 * Set opt->missing_newline if msgbuf doesn't
810 * end in a newline (including if it is empty)
812 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
813 opt->missing_newline = 1;
814 else
815 opt->missing_newline = 0;
817 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
818 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
819 if (!opt->missing_newline)
820 graph_show_padding(opt->graph);
821 putc(opt->diffopt.line_termination, opt->diffopt.file);
824 strbuf_release(&msgbuf);
825 free(ctx.notes_message);
827 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
828 struct diff_queue_struct dq;
830 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
831 DIFF_QUEUE_CLEAR(&diff_queued_diff);
833 next_commentary_block(opt, NULL);
834 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
835 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
836 &opt->diffopt);
838 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
841 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
842 struct diff_queue_struct dq;
843 struct diff_options opts;
844 struct range_diff_options range_diff_opts = {
845 .creation_factor = opt->creation_factor,
846 .dual_color = 1,
847 .diffopt = &opts
850 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
851 DIFF_QUEUE_CLEAR(&diff_queued_diff);
853 next_commentary_block(opt, NULL);
854 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
856 * Pass minimum required diff-options to range-diff; others
857 * can be added later if deemed desirable.
859 repo_diff_setup(the_repository, &opts);
860 opts.file = opt->diffopt.file;
861 opts.use_color = opt->diffopt.use_color;
862 diff_setup_done(&opts);
863 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
865 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
869 int log_tree_diff_flush(struct rev_info *opt)
871 opt->shown_dashes = 0;
872 diffcore_std(&opt->diffopt);
874 if (diff_queue_is_empty(&opt->diffopt)) {
875 int saved_fmt = opt->diffopt.output_format;
876 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
877 diff_flush(&opt->diffopt);
878 opt->diffopt.output_format = saved_fmt;
879 return 0;
882 if (opt->loginfo && !opt->no_commit_id) {
883 show_log(opt);
884 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
885 opt->verbose_header &&
886 opt->commit_format != CMIT_FMT_ONELINE &&
887 !commit_format_is_empty(opt->commit_format)) {
889 * When showing a verbose header (i.e. log message),
890 * and not in --pretty=oneline format, we would want
891 * an extra newline between the end of log and the
892 * diff/diffstat output for readability.
894 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
895 if (opt->diffopt.output_prefix) {
896 struct strbuf *msg = NULL;
897 msg = opt->diffopt.output_prefix(&opt->diffopt,
898 opt->diffopt.output_prefix_data);
899 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
903 * We may have shown three-dashes line early
904 * between generated commentary (notes, etc.)
905 * and the log message, in which case we only
906 * want a blank line after the commentary
907 * without (an extra) three-dashes line.
908 * Otherwise, we show the three-dashes line if
909 * we are showing the patch with diffstat, but
910 * in that case, there is no extra blank line
911 * after the three-dashes line.
913 if (!opt->shown_dashes &&
914 (pch & opt->diffopt.output_format) == pch)
915 fprintf(opt->diffopt.file, "---");
916 putc('\n', opt->diffopt.file);
919 diff_flush(&opt->diffopt);
920 return 1;
923 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
925 diff_tree_combined_merge(commit, opt);
926 return !opt->loginfo;
929 static void setup_additional_headers(struct diff_options *o,
930 struct strmap *all_headers)
932 struct hashmap_iter iter;
933 struct strmap_entry *entry;
936 * Make o->additional_path_headers contain the subset of all_headers
937 * that match o->pathspec. If there aren't any that match o->pathspec,
938 * then make o->additional_path_headers be NULL.
941 if (!o->pathspec.nr) {
942 o->additional_path_headers = all_headers;
943 return;
946 o->additional_path_headers = xmalloc(sizeof(struct strmap));
947 strmap_init_with_options(o->additional_path_headers, NULL, 0);
948 strmap_for_each_entry(all_headers, &iter, entry) {
949 if (match_pathspec(the_repository->index, &o->pathspec,
950 entry->key, strlen(entry->key),
951 0 /* prefix */, NULL /* seen */,
952 0 /* is_dir */))
953 strmap_put(o->additional_path_headers,
954 entry->key, entry->value);
956 if (!strmap_get_size(o->additional_path_headers)) {
957 strmap_clear(o->additional_path_headers, 0);
958 FREE_AND_NULL(o->additional_path_headers);
962 static void cleanup_additional_headers(struct diff_options *o)
964 if (!o->pathspec.nr) {
965 o->additional_path_headers = NULL;
966 return;
968 if (!o->additional_path_headers)
969 return;
971 strmap_clear(o->additional_path_headers, 0);
972 FREE_AND_NULL(o->additional_path_headers);
975 static int do_remerge_diff(struct rev_info *opt,
976 struct commit_list *parents,
977 struct object_id *oid)
979 struct merge_options o;
980 struct commit_list *bases;
981 struct merge_result res = {0};
982 struct pretty_print_context ctx = {0};
983 struct commit *parent1 = parents->item;
984 struct commit *parent2 = parents->next->item;
985 struct strbuf parent1_desc = STRBUF_INIT;
986 struct strbuf parent2_desc = STRBUF_INIT;
988 /* Setup merge options */
989 init_merge_options(&o, the_repository);
990 o.show_rename_progress = 0;
991 o.record_conflict_msgs_as_headers = 1;
992 o.msg_header_prefix = "remerge";
994 ctx.abbrev = DEFAULT_ABBREV;
995 repo_format_commit_message(the_repository, parent1, "%h (%s)",
996 &parent1_desc, &ctx);
997 repo_format_commit_message(the_repository, parent2, "%h (%s)",
998 &parent2_desc, &ctx);
999 o.branch1 = parent1_desc.buf;
1000 o.branch2 = parent2_desc.buf;
1002 /* Parse the relevant commits and get the merge bases */
1003 parse_commit_or_die(parent1);
1004 parse_commit_or_die(parent2);
1005 bases = repo_get_merge_bases(the_repository, parent1, parent2);
1007 /* Re-merge the parents */
1008 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1010 /* Show the diff */
1011 setup_additional_headers(&opt->diffopt, res.path_messages);
1012 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1013 log_tree_diff_flush(opt);
1015 /* Cleanup */
1016 cleanup_additional_headers(&opt->diffopt);
1017 strbuf_release(&parent1_desc);
1018 strbuf_release(&parent2_desc);
1019 merge_finalize(&o, &res);
1021 /* Clean up the contents of the temporary object directory */
1022 if (opt->remerge_objdir)
1023 tmp_objdir_discard_objects(opt->remerge_objdir);
1024 else
1025 BUG("did a remerge diff without remerge_objdir?!?");
1027 return !opt->loginfo;
1031 * Show the diff of a commit.
1033 * Return true if we printed any log info messages
1035 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1037 int showed_log;
1038 struct commit_list *parents;
1039 struct object_id *oid;
1040 int is_merge;
1041 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1043 if (!all_need_diff && !opt->merges_need_diff)
1044 return 0;
1046 parse_commit_or_die(commit);
1047 oid = get_commit_tree_oid(commit);
1049 parents = get_saved_parents(opt, commit);
1050 is_merge = parents && parents->next;
1051 if (!is_merge && !all_need_diff)
1052 return 0;
1054 /* Root commit? */
1055 if (!parents) {
1056 if (opt->show_root_diff) {
1057 diff_root_tree_oid(oid, "", &opt->diffopt);
1058 log_tree_diff_flush(opt);
1060 return !opt->loginfo;
1063 if (is_merge) {
1064 int octopus = (parents->next->next != NULL);
1066 if (opt->remerge_diff) {
1067 if (octopus) {
1068 show_log(opt);
1069 fprintf(opt->diffopt.file,
1070 "diff: warning: Skipping remerge-diff "
1071 "for octopus merges.\n");
1072 return 1;
1074 return do_remerge_diff(opt, parents, oid);
1076 if (opt->combine_merges)
1077 return do_diff_combined(opt, commit);
1078 if (opt->separate_merges) {
1079 if (!opt->first_parent_merges) {
1080 /* Show parent info for multiple diffs */
1081 log->parent = parents->item;
1083 } else
1084 return 0;
1087 showed_log = 0;
1088 for (;;) {
1089 struct commit *parent = parents->item;
1091 parse_commit_or_die(parent);
1092 diff_tree_oid(get_commit_tree_oid(parent),
1093 oid, "", &opt->diffopt);
1094 log_tree_diff_flush(opt);
1096 showed_log |= !opt->loginfo;
1098 /* Set up the log info for the next parent, if any.. */
1099 parents = parents->next;
1100 if (!parents || opt->first_parent_merges)
1101 break;
1102 log->parent = parents->item;
1103 opt->loginfo = log;
1105 return showed_log;
1108 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1110 struct log_info log;
1111 int shown;
1112 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1113 int no_free = opt->diffopt.no_free;
1115 log.commit = commit;
1116 log.parent = NULL;
1117 opt->loginfo = &log;
1118 opt->diffopt.no_free = 1;
1120 /* NEEDSWORK: no restoring of no_free? Why? */
1121 if (opt->line_level_traverse)
1122 return line_log_print(opt, commit);
1124 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1125 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1126 shown = log_tree_diff(opt, commit, &log);
1127 if (!shown && opt->loginfo && opt->always_show_header) {
1128 log.parent = NULL;
1129 show_log(opt);
1130 shown = 1;
1132 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1133 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1134 opt->loginfo = NULL;
1135 maybe_flush_or_die(opt->diffopt.file, "stdout");
1136 opt->diffopt.no_free = no_free;
1137 diff_free(&opt->diffopt);
1138 return shown;