setup.h: move declarations for setup.c functions from cache.h
[git.git] / log-tree.c
blobe2bf8d6df75da73172c7e95c279b6b10da82aa74
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-store.h"
8 #include "repository.h"
9 #include "tmp-objdir.h"
10 #include "commit.h"
11 #include "tag.h"
12 #include "graph.h"
13 #include "log-tree.h"
14 #include "merge-ort.h"
15 #include "reflog-walk.h"
16 #include "refs.h"
17 #include "replace-object.h"
18 #include "string-list.h"
19 #include "color.h"
20 #include "gpg-interface.h"
21 #include "sequencer.h"
22 #include "line-log.h"
23 #include "help.h"
24 #include "range-diff.h"
25 #include "strmap.h"
27 static struct decoration name_decoration = { "object names" };
28 static int decoration_loaded;
29 static int decoration_flags;
31 static char decoration_colors[][COLOR_MAXLEN] = {
32 GIT_COLOR_RESET,
33 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
34 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
35 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
36 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
37 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
38 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
41 static const char *color_decorate_slots[] = {
42 [DECORATION_REF_LOCAL] = "branch",
43 [DECORATION_REF_REMOTE] = "remoteBranch",
44 [DECORATION_REF_TAG] = "tag",
45 [DECORATION_REF_STASH] = "stash",
46 [DECORATION_REF_HEAD] = "HEAD",
47 [DECORATION_GRAFTED] = "grafted",
50 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
52 if (want_color(decorate_use_color))
53 return decoration_colors[ix];
54 return "";
57 define_list_config_array(color_decorate_slots);
59 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
61 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
62 if (slot < 0)
63 return 0;
64 if (!value)
65 return config_error_nonbool(var);
66 return color_parse(value, decoration_colors[slot]);
70 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
71 * for showing the commit sha1, use the same check for --decorate
73 #define decorate_get_color_opt(o, ix) \
74 decorate_get_color((o)->use_color, ix)
76 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
78 struct name_decoration *res;
79 FLEX_ALLOC_STR(res, name, name);
80 res->type = type;
81 res->next = add_decoration(&name_decoration, obj, res);
84 const struct name_decoration *get_name_decoration(const struct object *obj)
86 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
87 return lookup_decoration(&name_decoration, obj);
90 static int match_ref_pattern(const char *refname,
91 const struct string_list_item *item)
93 int matched = 0;
94 if (!item->util) {
95 if (!wildmatch(item->string, refname, 0))
96 matched = 1;
97 } else {
98 const char *rest;
99 if (skip_prefix(refname, item->string, &rest) &&
100 (!*rest || *rest == '/'))
101 matched = 1;
103 return matched;
106 static int ref_filter_match(const char *refname,
107 const struct decoration_filter *filter)
109 struct string_list_item *item;
110 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
111 const struct string_list *include_patterns = filter->include_ref_pattern;
112 const struct string_list *exclude_patterns_config =
113 filter->exclude_ref_config_pattern;
115 if (exclude_patterns && exclude_patterns->nr) {
116 for_each_string_list_item(item, exclude_patterns) {
117 if (match_ref_pattern(refname, item))
118 return 0;
122 if (include_patterns && include_patterns->nr) {
123 for_each_string_list_item(item, include_patterns) {
124 if (match_ref_pattern(refname, item))
125 return 1;
127 return 0;
130 if (exclude_patterns_config && exclude_patterns_config->nr) {
131 for_each_string_list_item(item, exclude_patterns_config) {
132 if (match_ref_pattern(refname, item))
133 return 0;
137 return 1;
140 static int add_ref_decoration(const char *refname, const struct object_id *oid,
141 int flags UNUSED,
142 void *cb_data)
144 int i;
145 struct object *obj;
146 enum object_type objtype;
147 enum decoration_type deco_type = DECORATION_NONE;
148 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
149 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
151 if (filter && !ref_filter_match(refname, filter))
152 return 0;
154 if (starts_with(refname, git_replace_ref_base)) {
155 struct object_id original_oid;
156 if (!read_replace_refs)
157 return 0;
158 if (get_oid_hex(refname + strlen(git_replace_ref_base),
159 &original_oid)) {
160 warning("invalid replace ref %s", refname);
161 return 0;
163 obj = parse_object(the_repository, &original_oid);
164 if (obj)
165 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
166 return 0;
169 objtype = oid_object_info(the_repository, oid, NULL);
170 if (objtype < 0)
171 return 0;
172 obj = lookup_object_by_type(the_repository, oid, objtype);
174 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
175 struct ref_namespace_info *info = &ref_namespace[i];
177 if (!info->decoration)
178 continue;
179 if (info->exact) {
180 if (!strcmp(refname, info->ref)) {
181 deco_type = info->decoration;
182 break;
184 } else if (starts_with(refname, info->ref)) {
185 deco_type = info->decoration;
186 break;
190 add_name_decoration(deco_type, refname, obj);
191 while (obj->type == OBJ_TAG) {
192 if (!obj->parsed)
193 parse_object(the_repository, &obj->oid);
194 obj = ((struct tag *)obj)->tagged;
195 if (!obj)
196 break;
197 add_name_decoration(DECORATION_REF_TAG, refname, obj);
199 return 0;
202 static int add_graft_decoration(const struct commit_graft *graft,
203 void *cb_data UNUSED)
205 struct commit *commit = lookup_commit(the_repository, &graft->oid);
206 if (!commit)
207 return 0;
208 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
209 return 0;
212 void load_ref_decorations(struct decoration_filter *filter, int flags)
214 if (!decoration_loaded) {
215 if (filter) {
216 struct string_list_item *item;
217 for_each_string_list_item(item, filter->exclude_ref_pattern) {
218 normalize_glob_ref(item, NULL, item->string);
220 for_each_string_list_item(item, filter->include_ref_pattern) {
221 normalize_glob_ref(item, NULL, item->string);
223 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
224 normalize_glob_ref(item, NULL, item->string);
227 decoration_loaded = 1;
228 decoration_flags = flags;
229 for_each_ref(add_ref_decoration, filter);
230 head_ref(add_ref_decoration, filter);
231 for_each_commit_graft(add_graft_decoration, filter);
235 static void show_parents(struct commit *commit, int abbrev, FILE *file)
237 struct commit_list *p;
238 for (p = commit->parents; p ; p = p->next) {
239 struct commit *parent = p->item;
240 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
244 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
246 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
247 for ( ; p; p = p->next) {
248 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
253 * Do we have HEAD in the output, and also the branch it points at?
254 * If so, find that decoration entry for that current branch.
256 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
258 const struct name_decoration *list, *head = NULL;
259 const char *branch_name = NULL;
260 int rru_flags;
262 /* First find HEAD */
263 for (list = decoration; list; list = list->next)
264 if (list->type == DECORATION_REF_HEAD) {
265 head = list;
266 break;
268 if (!head)
269 return NULL;
271 /* Now resolve and find the matching current branch */
272 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
273 if (!branch_name || !(rru_flags & REF_ISSYMREF))
274 return NULL;
276 if (!starts_with(branch_name, "refs/"))
277 return NULL;
279 /* OK, do we have that ref in the list? */
280 for (list = decoration; list; list = list->next)
281 if ((list->type == DECORATION_REF_LOCAL) &&
282 !strcmp(branch_name, list->name)) {
283 return list;
286 return NULL;
289 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
291 if (decoration_flags == DECORATE_SHORT_REFS)
292 strbuf_addstr(sb, prettify_refname(decoration->name));
293 else
294 strbuf_addstr(sb, decoration->name);
298 * The caller makes sure there is no funny color before calling.
299 * format_decorations_extended makes sure the same after return.
301 void format_decorations_extended(struct strbuf *sb,
302 const struct commit *commit,
303 int use_color,
304 const char *prefix,
305 const char *separator,
306 const char *suffix)
308 const struct name_decoration *decoration;
309 const struct name_decoration *current_and_HEAD;
310 const char *color_commit =
311 diff_get_color(use_color, DIFF_COMMIT);
312 const char *color_reset =
313 decorate_get_color(use_color, DECORATION_NONE);
315 decoration = get_name_decoration(&commit->object);
316 if (!decoration)
317 return;
319 current_and_HEAD = current_pointed_by_HEAD(decoration);
320 while (decoration) {
322 * When both current and HEAD are there, only
323 * show HEAD->current where HEAD would have
324 * appeared, skipping the entry for current.
326 if (decoration != current_and_HEAD) {
327 strbuf_addstr(sb, color_commit);
328 strbuf_addstr(sb, prefix);
329 strbuf_addstr(sb, color_reset);
330 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
331 if (decoration->type == DECORATION_REF_TAG)
332 strbuf_addstr(sb, "tag: ");
334 show_name(sb, decoration);
336 if (current_and_HEAD &&
337 decoration->type == DECORATION_REF_HEAD) {
338 strbuf_addstr(sb, " -> ");
339 strbuf_addstr(sb, color_reset);
340 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
341 show_name(sb, current_and_HEAD);
343 strbuf_addstr(sb, color_reset);
345 prefix = separator;
347 decoration = decoration->next;
349 strbuf_addstr(sb, color_commit);
350 strbuf_addstr(sb, suffix);
351 strbuf_addstr(sb, color_reset);
354 void show_decorations(struct rev_info *opt, struct commit *commit)
356 struct strbuf sb = STRBUF_INIT;
358 if (opt->sources) {
359 char **slot = revision_sources_peek(opt->sources, commit);
361 if (slot && *slot)
362 fprintf(opt->diffopt.file, "\t%s", *slot);
364 if (!opt->show_decorations)
365 return;
366 format_decorations(&sb, commit, opt->diffopt.use_color);
367 fputs(sb.buf, opt->diffopt.file);
368 strbuf_release(&sb);
371 static unsigned int digits_in_number(unsigned int number)
373 unsigned int i = 10, result = 1;
374 while (i <= number) {
375 i *= 10;
376 result++;
378 return result;
381 void fmt_output_subject(struct strbuf *filename,
382 const char *subject,
383 struct rev_info *info)
385 const char *suffix = info->patch_suffix;
386 int nr = info->nr;
387 int start_len = filename->len;
388 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
390 if (info->reroll_count) {
391 struct strbuf temp = STRBUF_INIT;
393 strbuf_addf(&temp, "v%s", info->reroll_count);
394 format_sanitized_subject(filename, temp.buf, temp.len);
395 strbuf_addstr(filename, "-");
396 strbuf_release(&temp);
398 strbuf_addf(filename, "%04d-%s", nr, subject);
400 if (max_len < filename->len)
401 strbuf_setlen(filename, max_len);
402 strbuf_addstr(filename, suffix);
405 void fmt_output_commit(struct strbuf *filename,
406 struct commit *commit,
407 struct rev_info *info)
409 struct pretty_print_context ctx = {0};
410 struct strbuf subject = STRBUF_INIT;
412 format_commit_message(commit, "%f", &subject, &ctx);
413 fmt_output_subject(filename, subject.buf, info);
414 strbuf_release(&subject);
417 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
419 if (opt->total > 0) {
420 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
421 opt->subject_prefix,
422 *opt->subject_prefix ? " " : "",
423 digits_in_number(opt->total),
424 opt->nr, opt->total);
425 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
426 strbuf_addf(sb, "Subject: [%s] ",
427 opt->subject_prefix);
428 } else {
429 strbuf_addstr(sb, "Subject: ");
433 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
434 const char **extra_headers_p,
435 int *need_8bit_cte_p,
436 int maybe_multipart)
438 const char *extra_headers = opt->extra_headers;
439 const char *name = oid_to_hex(opt->zero_commit ?
440 null_oid() : &commit->object.oid);
442 *need_8bit_cte_p = 0; /* unknown */
444 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
445 graph_show_oneline(opt->graph);
446 if (opt->message_id) {
447 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
448 graph_show_oneline(opt->graph);
450 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
451 int i, n;
452 n = opt->ref_message_ids->nr;
453 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
454 for (i = 0; i < n; i++)
455 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
456 opt->ref_message_ids->items[i].string);
457 graph_show_oneline(opt->graph);
459 if (opt->mime_boundary && maybe_multipart) {
460 static struct strbuf subject_buffer = STRBUF_INIT;
461 static struct strbuf buffer = STRBUF_INIT;
462 struct strbuf filename = STRBUF_INIT;
463 *need_8bit_cte_p = -1; /* NEVER */
465 strbuf_reset(&subject_buffer);
466 strbuf_reset(&buffer);
468 strbuf_addf(&subject_buffer,
469 "%s"
470 "MIME-Version: 1.0\n"
471 "Content-Type: multipart/mixed;"
472 " boundary=\"%s%s\"\n"
473 "\n"
474 "This is a multi-part message in MIME "
475 "format.\n"
476 "--%s%s\n"
477 "Content-Type: text/plain; "
478 "charset=UTF-8; format=fixed\n"
479 "Content-Transfer-Encoding: 8bit\n\n",
480 extra_headers ? extra_headers : "",
481 mime_boundary_leader, opt->mime_boundary,
482 mime_boundary_leader, opt->mime_boundary);
483 extra_headers = subject_buffer.buf;
485 if (opt->numbered_files)
486 strbuf_addf(&filename, "%d", opt->nr);
487 else
488 fmt_output_commit(&filename, commit, opt);
489 strbuf_addf(&buffer,
490 "\n--%s%s\n"
491 "Content-Type: text/x-patch;"
492 " name=\"%s\"\n"
493 "Content-Transfer-Encoding: 8bit\n"
494 "Content-Disposition: %s;"
495 " filename=\"%s\"\n\n",
496 mime_boundary_leader, opt->mime_boundary,
497 filename.buf,
498 opt->no_inline ? "attachment" : "inline",
499 filename.buf);
500 opt->diffopt.stat_sep = buffer.buf;
501 strbuf_release(&filename);
503 *extra_headers_p = extra_headers;
506 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
508 const char *color, *reset, *eol;
510 color = diff_get_color_opt(&opt->diffopt,
511 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
512 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
513 while (*bol) {
514 eol = strchrnul(bol, '\n');
515 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
516 *eol ? "\n" : "");
517 graph_show_oneline(opt->graph);
518 bol = (*eol) ? (eol + 1) : eol;
522 static void show_signature(struct rev_info *opt, struct commit *commit)
524 struct strbuf payload = STRBUF_INIT;
525 struct strbuf signature = STRBUF_INIT;
526 struct signature_check sigc = { 0 };
527 int status;
529 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
530 goto out;
532 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
533 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
534 status = check_signature(&sigc, signature.buf, signature.len);
535 if (status && !sigc.output)
536 show_sig_lines(opt, status, "No signature\n");
537 else
538 show_sig_lines(opt, status, sigc.output);
539 signature_check_clear(&sigc);
541 out:
542 strbuf_release(&payload);
543 strbuf_release(&signature);
546 static int which_parent(const struct object_id *oid, const struct commit *commit)
548 int nth;
549 const struct commit_list *parent;
551 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
552 if (oideq(&parent->item->object.oid, oid))
553 return nth;
554 nth++;
556 return -1;
559 static int is_common_merge(const struct commit *commit)
561 return (commit->parents
562 && commit->parents->next
563 && !commit->parents->next->next);
566 static int show_one_mergetag(struct commit *commit,
567 struct commit_extra_header *extra,
568 void *data)
570 struct rev_info *opt = (struct rev_info *)data;
571 struct object_id oid;
572 struct tag *tag;
573 struct strbuf verify_message;
574 struct signature_check sigc = { 0 };
575 int status, nth;
576 struct strbuf payload = STRBUF_INIT;
577 struct strbuf signature = STRBUF_INIT;
579 hash_object_file(the_hash_algo, extra->value, extra->len,
580 OBJ_TAG, &oid);
581 tag = lookup_tag(the_repository, &oid);
582 if (!tag)
583 return -1; /* error message already given */
585 strbuf_init(&verify_message, 256);
586 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
587 strbuf_addstr(&verify_message, "malformed mergetag\n");
588 else if (is_common_merge(commit) &&
589 oideq(&tag->tagged->oid,
590 &commit->parents->next->item->object.oid))
591 strbuf_addf(&verify_message,
592 "merged tag '%s'\n", tag->tag);
593 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
594 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
595 tag->tag, oid_to_hex(&tag->tagged->oid));
596 else
597 strbuf_addf(&verify_message,
598 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
600 status = -1;
601 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
602 /* could have a good signature */
603 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
604 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
605 status = check_signature(&sigc, signature.buf, signature.len);
606 if (sigc.output)
607 strbuf_addstr(&verify_message, sigc.output);
608 else
609 strbuf_addstr(&verify_message, "No signature\n");
610 signature_check_clear(&sigc);
611 /* otherwise we couldn't verify, which is shown as bad */
614 show_sig_lines(opt, status, verify_message.buf);
615 strbuf_release(&verify_message);
616 strbuf_release(&payload);
617 strbuf_release(&signature);
618 return 0;
621 static int show_mergetag(struct rev_info *opt, struct commit *commit)
623 return for_each_mergetag(show_one_mergetag, commit, opt);
626 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
628 const char *x = opt->shown_dashes ? "\n" : "---\n";
629 if (sb)
630 strbuf_addstr(sb, x);
631 else
632 fputs(x, opt->diffopt.file);
633 opt->shown_dashes = 1;
636 void show_log(struct rev_info *opt)
638 struct strbuf msgbuf = STRBUF_INIT;
639 struct log_info *log = opt->loginfo;
640 struct commit *commit = log->commit, *parent = log->parent;
641 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
642 const char *extra_headers = opt->extra_headers;
643 struct pretty_print_context ctx = {0};
645 opt->loginfo = NULL;
646 if (!opt->verbose_header) {
647 graph_show_commit(opt->graph);
649 if (!opt->graph)
650 put_revision_mark(opt, commit);
651 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
652 if (opt->print_parents)
653 show_parents(commit, abbrev_commit, opt->diffopt.file);
654 if (opt->children.name)
655 show_children(opt, commit, abbrev_commit);
656 show_decorations(opt, commit);
657 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
658 putc('\n', opt->diffopt.file);
659 graph_show_remainder(opt->graph);
661 putc(opt->diffopt.line_termination, opt->diffopt.file);
662 return;
666 * If use_terminator is set, we already handled any record termination
667 * at the end of the last record.
668 * Otherwise, add a diffopt.line_termination character before all
669 * entries but the first. (IOW, as a separator between entries)
671 if (opt->shown_one && !opt->use_terminator) {
673 * If entries are separated by a newline, the output
674 * should look human-readable. If the last entry ended
675 * with a newline, print the graph output before this
676 * newline. Otherwise it will end up as a completely blank
677 * line and will look like a gap in the graph.
679 * If the entry separator is not a newline, the output is
680 * primarily intended for programmatic consumption, and we
681 * never want the extra graph output before the entry
682 * separator.
684 if (opt->diffopt.line_termination == '\n' &&
685 !opt->missing_newline)
686 graph_show_padding(opt->graph);
687 putc(opt->diffopt.line_termination, opt->diffopt.file);
689 opt->shown_one = 1;
692 * If the history graph was requested,
693 * print the graph, up to this commit's line
695 graph_show_commit(opt->graph);
698 * Print header line of header..
701 if (cmit_fmt_is_mail(opt->commit_format)) {
702 log_write_email_headers(opt, commit, &extra_headers,
703 &ctx.need_8bit_cte, 1);
704 ctx.rev = opt;
705 ctx.print_email_subject = 1;
706 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
707 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
708 if (opt->commit_format != CMIT_FMT_ONELINE)
709 fputs("commit ", opt->diffopt.file);
711 if (!opt->graph)
712 put_revision_mark(opt, commit);
713 fputs(find_unique_abbrev(&commit->object.oid,
714 abbrev_commit),
715 opt->diffopt.file);
716 if (opt->print_parents)
717 show_parents(commit, abbrev_commit, opt->diffopt.file);
718 if (opt->children.name)
719 show_children(opt, commit, abbrev_commit);
720 if (parent)
721 fprintf(opt->diffopt.file, " (from %s)",
722 find_unique_abbrev(&parent->object.oid, abbrev_commit));
723 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
724 show_decorations(opt, commit);
725 if (opt->commit_format == CMIT_FMT_ONELINE) {
726 putc(' ', opt->diffopt.file);
727 } else {
728 putc('\n', opt->diffopt.file);
729 graph_show_oneline(opt->graph);
731 if (opt->reflog_info) {
733 * setup_revisions() ensures that opt->reflog_info
734 * and opt->graph cannot both be set,
735 * so we don't need to worry about printing the
736 * graph info here.
738 show_reflog_message(opt->reflog_info,
739 opt->commit_format == CMIT_FMT_ONELINE,
740 &opt->date_mode,
741 opt->date_mode_explicit);
742 if (opt->commit_format == CMIT_FMT_ONELINE)
743 return;
747 if (opt->show_signature) {
748 show_signature(opt, commit);
749 show_mergetag(opt, commit);
752 if (opt->show_notes) {
753 int raw;
754 struct strbuf notebuf = STRBUF_INIT;
756 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
757 format_display_notes(&commit->object.oid, &notebuf,
758 get_log_output_encoding(), raw);
759 ctx.notes_message = strbuf_detach(&notebuf, NULL);
763 * And then the pretty-printed message itself
765 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
766 ctx.need_8bit_cte =
767 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
768 ctx.date_mode = opt->date_mode;
769 ctx.date_mode_explicit = opt->date_mode_explicit;
770 ctx.abbrev = opt->diffopt.abbrev;
771 ctx.after_subject = extra_headers;
772 ctx.preserve_subject = opt->preserve_subject;
773 ctx.encode_email_headers = opt->encode_email_headers;
774 ctx.reflog_info = opt->reflog_info;
775 ctx.fmt = opt->commit_format;
776 ctx.mailmap = opt->mailmap;
777 ctx.color = opt->diffopt.use_color;
778 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
779 ctx.output_encoding = get_log_output_encoding();
780 ctx.rev = opt;
781 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
782 ctx.from_ident = &opt->from_ident;
783 if (opt->graph)
784 ctx.graph_width = graph_width(opt->graph);
785 pretty_print_commit(&ctx, commit, &msgbuf);
787 if (opt->add_signoff)
788 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
790 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
791 ctx.notes_message && *ctx.notes_message) {
792 if (cmit_fmt_is_mail(ctx.fmt))
793 next_commentary_block(opt, &msgbuf);
794 strbuf_addstr(&msgbuf, ctx.notes_message);
797 if (opt->show_log_size) {
798 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
799 graph_show_oneline(opt->graph);
803 * Set opt->missing_newline if msgbuf doesn't
804 * end in a newline (including if it is empty)
806 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
807 opt->missing_newline = 1;
808 else
809 opt->missing_newline = 0;
811 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
812 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
813 if (!opt->missing_newline)
814 graph_show_padding(opt->graph);
815 putc(opt->diffopt.line_termination, opt->diffopt.file);
818 strbuf_release(&msgbuf);
819 free(ctx.notes_message);
821 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
822 struct diff_queue_struct dq;
824 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
825 DIFF_QUEUE_CLEAR(&diff_queued_diff);
827 next_commentary_block(opt, NULL);
828 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
829 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
830 &opt->diffopt);
832 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
835 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
836 struct diff_queue_struct dq;
837 struct diff_options opts;
838 struct range_diff_options range_diff_opts = {
839 .creation_factor = opt->creation_factor,
840 .dual_color = 1,
841 .diffopt = &opts
844 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
845 DIFF_QUEUE_CLEAR(&diff_queued_diff);
847 next_commentary_block(opt, NULL);
848 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
850 * Pass minimum required diff-options to range-diff; others
851 * can be added later if deemed desirable.
853 diff_setup(&opts);
854 opts.file = opt->diffopt.file;
855 opts.use_color = opt->diffopt.use_color;
856 diff_setup_done(&opts);
857 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
859 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
863 int log_tree_diff_flush(struct rev_info *opt)
865 opt->shown_dashes = 0;
866 diffcore_std(&opt->diffopt);
868 if (diff_queue_is_empty(&opt->diffopt)) {
869 int saved_fmt = opt->diffopt.output_format;
870 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
871 diff_flush(&opt->diffopt);
872 opt->diffopt.output_format = saved_fmt;
873 return 0;
876 if (opt->loginfo && !opt->no_commit_id) {
877 show_log(opt);
878 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
879 opt->verbose_header &&
880 opt->commit_format != CMIT_FMT_ONELINE &&
881 !commit_format_is_empty(opt->commit_format)) {
883 * When showing a verbose header (i.e. log message),
884 * and not in --pretty=oneline format, we would want
885 * an extra newline between the end of log and the
886 * diff/diffstat output for readability.
888 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
889 if (opt->diffopt.output_prefix) {
890 struct strbuf *msg = NULL;
891 msg = opt->diffopt.output_prefix(&opt->diffopt,
892 opt->diffopt.output_prefix_data);
893 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
897 * We may have shown three-dashes line early
898 * between generated commentary (notes, etc.)
899 * and the log message, in which case we only
900 * want a blank line after the commentary
901 * without (an extra) three-dashes line.
902 * Otherwise, we show the three-dashes line if
903 * we are showing the patch with diffstat, but
904 * in that case, there is no extra blank line
905 * after the three-dashes line.
907 if (!opt->shown_dashes &&
908 (pch & opt->diffopt.output_format) == pch)
909 fprintf(opt->diffopt.file, "---");
910 putc('\n', opt->diffopt.file);
913 diff_flush(&opt->diffopt);
914 return 1;
917 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
919 diff_tree_combined_merge(commit, opt);
920 return !opt->loginfo;
923 static void setup_additional_headers(struct diff_options *o,
924 struct strmap *all_headers)
926 struct hashmap_iter iter;
927 struct strmap_entry *entry;
930 * Make o->additional_path_headers contain the subset of all_headers
931 * that match o->pathspec. If there aren't any that match o->pathspec,
932 * then make o->additional_path_headers be NULL.
935 if (!o->pathspec.nr) {
936 o->additional_path_headers = all_headers;
937 return;
940 o->additional_path_headers = xmalloc(sizeof(struct strmap));
941 strmap_init_with_options(o->additional_path_headers, NULL, 0);
942 strmap_for_each_entry(all_headers, &iter, entry) {
943 if (match_pathspec(the_repository->index, &o->pathspec,
944 entry->key, strlen(entry->key),
945 0 /* prefix */, NULL /* seen */,
946 0 /* is_dir */))
947 strmap_put(o->additional_path_headers,
948 entry->key, entry->value);
950 if (!strmap_get_size(o->additional_path_headers)) {
951 strmap_clear(o->additional_path_headers, 0);
952 FREE_AND_NULL(o->additional_path_headers);
956 static void cleanup_additional_headers(struct diff_options *o)
958 if (!o->pathspec.nr) {
959 o->additional_path_headers = NULL;
960 return;
962 if (!o->additional_path_headers)
963 return;
965 strmap_clear(o->additional_path_headers, 0);
966 FREE_AND_NULL(o->additional_path_headers);
969 static int do_remerge_diff(struct rev_info *opt,
970 struct commit_list *parents,
971 struct object_id *oid)
973 struct merge_options o;
974 struct commit_list *bases;
975 struct merge_result res = {0};
976 struct pretty_print_context ctx = {0};
977 struct commit *parent1 = parents->item;
978 struct commit *parent2 = parents->next->item;
979 struct strbuf parent1_desc = STRBUF_INIT;
980 struct strbuf parent2_desc = STRBUF_INIT;
982 /* Setup merge options */
983 init_merge_options(&o, the_repository);
984 o.show_rename_progress = 0;
985 o.record_conflict_msgs_as_headers = 1;
986 o.msg_header_prefix = "remerge";
988 ctx.abbrev = DEFAULT_ABBREV;
989 format_commit_message(parent1, "%h (%s)", &parent1_desc, &ctx);
990 format_commit_message(parent2, "%h (%s)", &parent2_desc, &ctx);
991 o.branch1 = parent1_desc.buf;
992 o.branch2 = parent2_desc.buf;
994 /* Parse the relevant commits and get the merge bases */
995 parse_commit_or_die(parent1);
996 parse_commit_or_die(parent2);
997 bases = get_merge_bases(parent1, parent2);
999 /* Re-merge the parents */
1000 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1002 /* Show the diff */
1003 setup_additional_headers(&opt->diffopt, res.path_messages);
1004 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1005 log_tree_diff_flush(opt);
1007 /* Cleanup */
1008 cleanup_additional_headers(&opt->diffopt);
1009 strbuf_release(&parent1_desc);
1010 strbuf_release(&parent2_desc);
1011 merge_finalize(&o, &res);
1013 /* Clean up the contents of the temporary object directory */
1014 if (opt->remerge_objdir)
1015 tmp_objdir_discard_objects(opt->remerge_objdir);
1016 else
1017 BUG("did a remerge diff without remerge_objdir?!?");
1019 return !opt->loginfo;
1023 * Show the diff of a commit.
1025 * Return true if we printed any log info messages
1027 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1029 int showed_log;
1030 struct commit_list *parents;
1031 struct object_id *oid;
1032 int is_merge;
1033 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1035 if (!all_need_diff && !opt->merges_need_diff)
1036 return 0;
1038 parse_commit_or_die(commit);
1039 oid = get_commit_tree_oid(commit);
1041 parents = get_saved_parents(opt, commit);
1042 is_merge = parents && parents->next;
1043 if (!is_merge && !all_need_diff)
1044 return 0;
1046 /* Root commit? */
1047 if (!parents) {
1048 if (opt->show_root_diff) {
1049 diff_root_tree_oid(oid, "", &opt->diffopt);
1050 log_tree_diff_flush(opt);
1052 return !opt->loginfo;
1055 if (is_merge) {
1056 int octopus = (parents->next->next != NULL);
1058 if (opt->remerge_diff) {
1059 if (octopus) {
1060 show_log(opt);
1061 fprintf(opt->diffopt.file,
1062 "diff: warning: Skipping remerge-diff "
1063 "for octopus merges.\n");
1064 return 1;
1066 return do_remerge_diff(opt, parents, oid);
1068 if (opt->combine_merges)
1069 return do_diff_combined(opt, commit);
1070 if (opt->separate_merges) {
1071 if (!opt->first_parent_merges) {
1072 /* Show parent info for multiple diffs */
1073 log->parent = parents->item;
1075 } else
1076 return 0;
1079 showed_log = 0;
1080 for (;;) {
1081 struct commit *parent = parents->item;
1083 parse_commit_or_die(parent);
1084 diff_tree_oid(get_commit_tree_oid(parent),
1085 oid, "", &opt->diffopt);
1086 log_tree_diff_flush(opt);
1088 showed_log |= !opt->loginfo;
1090 /* Set up the log info for the next parent, if any.. */
1091 parents = parents->next;
1092 if (!parents || opt->first_parent_merges)
1093 break;
1094 log->parent = parents->item;
1095 opt->loginfo = log;
1097 return showed_log;
1100 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1102 struct log_info log;
1103 int shown;
1104 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1105 int no_free = opt->diffopt.no_free;
1107 log.commit = commit;
1108 log.parent = NULL;
1109 opt->loginfo = &log;
1110 opt->diffopt.no_free = 1;
1112 /* NEEDSWORK: no restoring of no_free? Why? */
1113 if (opt->line_level_traverse)
1114 return line_log_print(opt, commit);
1116 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1117 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1118 shown = log_tree_diff(opt, commit, &log);
1119 if (!shown && opt->loginfo && opt->always_show_header) {
1120 log.parent = NULL;
1121 show_log(opt);
1122 shown = 1;
1124 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1125 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1126 opt->loginfo = NULL;
1127 maybe_flush_or_die(opt->diffopt.file, "stdout");
1128 opt->diffopt.no_free = no_free;
1129 diff_free(&opt->diffopt);
1130 return shown;