Sync with 'maint'
[git.git] / log-tree.c
blob83cc4b1cfb798c923a8147364e63ceaebf1dacf8
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "commit-reach.h"
5 #include "config.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "environment.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "repository.h"
13 #include "tmp-objdir.h"
14 #include "commit.h"
15 #include "tag.h"
16 #include "graph.h"
17 #include "log-tree.h"
18 #include "merge-ort.h"
19 #include "reflog-walk.h"
20 #include "refs.h"
21 #include "replace-object.h"
22 #include "revision.h"
23 #include "string-list.h"
24 #include "color.h"
25 #include "gpg-interface.h"
26 #include "sequencer.h"
27 #include "line-log.h"
28 #include "help.h"
29 #include "range-diff.h"
30 #include "strmap.h"
31 #include "tree.h"
32 #include "wildmatch.h"
33 #include "write-or-die.h"
34 #include "pager.h"
36 static struct decoration name_decoration = { "object names" };
37 static int decoration_loaded;
38 static int decoration_flags;
40 static char decoration_colors[][COLOR_MAXLEN] = {
41 GIT_COLOR_RESET,
42 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
43 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
44 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
45 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
46 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
47 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
50 static const char *color_decorate_slots[] = {
51 [DECORATION_REF_LOCAL] = "branch",
52 [DECORATION_REF_REMOTE] = "remoteBranch",
53 [DECORATION_REF_TAG] = "tag",
54 [DECORATION_REF_STASH] = "stash",
55 [DECORATION_REF_HEAD] = "HEAD",
56 [DECORATION_GRAFTED] = "grafted",
59 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
61 if (want_color(decorate_use_color))
62 return decoration_colors[ix];
63 return "";
66 define_list_config_array(color_decorate_slots);
68 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
70 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
71 if (slot < 0)
72 return 0;
73 if (!value)
74 return config_error_nonbool(var);
75 return color_parse(value, decoration_colors[slot]);
79 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
80 * for showing the commit sha1, use the same check for --decorate
82 #define decorate_get_color_opt(o, ix) \
83 decorate_get_color((o)->use_color, ix)
85 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
87 struct name_decoration *res;
88 FLEX_ALLOC_STR(res, name, name);
89 res->type = type;
90 res->next = add_decoration(&name_decoration, obj, res);
93 const struct name_decoration *get_name_decoration(const struct object *obj)
95 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
96 return lookup_decoration(&name_decoration, obj);
99 static int match_ref_pattern(const char *refname,
100 const struct string_list_item *item)
102 int matched = 0;
103 if (!item->util) {
104 if (!wildmatch(item->string, refname, 0))
105 matched = 1;
106 } else {
107 const char *rest;
108 if (skip_prefix(refname, item->string, &rest) &&
109 (!*rest || *rest == '/'))
110 matched = 1;
112 return matched;
115 static int ref_filter_match(const char *refname,
116 const struct decoration_filter *filter)
118 struct string_list_item *item;
119 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
120 const struct string_list *include_patterns = filter->include_ref_pattern;
121 const struct string_list *exclude_patterns_config =
122 filter->exclude_ref_config_pattern;
124 if (exclude_patterns && exclude_patterns->nr) {
125 for_each_string_list_item(item, exclude_patterns) {
126 if (match_ref_pattern(refname, item))
127 return 0;
131 if (include_patterns && include_patterns->nr) {
132 for_each_string_list_item(item, include_patterns) {
133 if (match_ref_pattern(refname, item))
134 return 1;
136 return 0;
139 if (exclude_patterns_config && exclude_patterns_config->nr) {
140 for_each_string_list_item(item, exclude_patterns_config) {
141 if (match_ref_pattern(refname, item))
142 return 0;
146 return 1;
149 static int add_ref_decoration(const char *refname, const char *referent UNUSED, const struct object_id *oid,
150 int flags UNUSED,
151 void *cb_data)
153 int i;
154 struct object *obj;
155 enum object_type objtype;
156 enum decoration_type deco_type = DECORATION_NONE;
157 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
158 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
160 if (filter && !ref_filter_match(refname, filter))
161 return 0;
163 if (starts_with(refname, git_replace_ref_base)) {
164 struct object_id original_oid;
165 if (!replace_refs_enabled(the_repository))
166 return 0;
167 if (get_oid_hex(refname + strlen(git_replace_ref_base),
168 &original_oid)) {
169 warning("invalid replace ref %s", refname);
170 return 0;
172 obj = parse_object(the_repository, &original_oid);
173 if (obj)
174 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
175 return 0;
178 objtype = oid_object_info(the_repository, oid, NULL);
179 if (objtype < 0)
180 return 0;
181 obj = lookup_object_by_type(the_repository, oid, objtype);
183 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
184 struct ref_namespace_info *info = &ref_namespace[i];
186 if (!info->decoration)
187 continue;
188 if (info->exact) {
189 if (!strcmp(refname, info->ref)) {
190 deco_type = info->decoration;
191 break;
193 } else if (starts_with(refname, info->ref)) {
194 deco_type = info->decoration;
195 break;
199 add_name_decoration(deco_type, refname, obj);
200 while (obj->type == OBJ_TAG) {
201 if (!obj->parsed)
202 parse_object(the_repository, &obj->oid);
203 obj = ((struct tag *)obj)->tagged;
204 if (!obj)
205 break;
206 add_name_decoration(DECORATION_REF_TAG, refname, obj);
208 return 0;
211 static int add_graft_decoration(const struct commit_graft *graft,
212 void *cb_data UNUSED)
214 struct commit *commit = lookup_commit(the_repository, &graft->oid);
215 if (!commit)
216 return 0;
217 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
218 return 0;
221 void load_ref_decorations(struct decoration_filter *filter, int flags)
223 if (!decoration_loaded) {
224 if (filter) {
225 struct string_list_item *item;
226 for_each_string_list_item(item, filter->exclude_ref_pattern) {
227 normalize_glob_ref(item, NULL, item->string);
229 for_each_string_list_item(item, filter->include_ref_pattern) {
230 normalize_glob_ref(item, NULL, item->string);
232 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
233 normalize_glob_ref(item, NULL, item->string);
236 /* normalize_glob_ref duplicates the strings */
237 filter->exclude_ref_pattern->strdup_strings = 1;
238 filter->include_ref_pattern->strdup_strings = 1;
239 filter->exclude_ref_config_pattern->strdup_strings = 1;
241 decoration_loaded = 1;
242 decoration_flags = flags;
243 refs_for_each_ref(get_main_ref_store(the_repository),
244 add_ref_decoration, filter);
245 refs_head_ref(get_main_ref_store(the_repository),
246 add_ref_decoration, filter);
247 for_each_commit_graft(add_graft_decoration, filter);
251 void load_branch_decorations(void)
253 if (!decoration_loaded) {
254 struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
255 struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
256 struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
257 struct decoration_filter decoration_filter = {
258 .include_ref_pattern = &decorate_refs_include,
259 .exclude_ref_pattern = &decorate_refs_exclude,
260 .exclude_ref_config_pattern = &decorate_refs_exclude_config,
263 string_list_append(&decorate_refs_include, "refs/heads/");
264 load_ref_decorations(&decoration_filter, 0);
266 string_list_clear(&decorate_refs_exclude, 0);
267 string_list_clear(&decorate_refs_exclude_config, 0);
268 string_list_clear(&decorate_refs_include, 0);
272 static void show_parents(struct commit *commit, int abbrev, FILE *file)
274 struct commit_list *p;
275 for (p = commit->parents; p ; p = p->next) {
276 struct commit *parent = p->item;
277 fprintf(file, " %s",
278 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
282 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
284 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
285 for ( ; p; p = p->next) {
286 fprintf(opt->diffopt.file, " %s",
287 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
292 * Do we have HEAD in the output, and also the branch it points at?
293 * If so, find that decoration entry for that current branch.
295 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
297 const struct name_decoration *list, *head = NULL;
298 const char *branch_name = NULL;
299 int rru_flags;
301 /* First find HEAD */
302 for (list = decoration; list; list = list->next)
303 if (list->type == DECORATION_REF_HEAD) {
304 head = list;
305 break;
307 if (!head)
308 return NULL;
310 /* Now resolve and find the matching current branch */
311 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
312 "HEAD", 0, NULL, &rru_flags);
313 if (!branch_name || !(rru_flags & REF_ISSYMREF))
314 return NULL;
316 if (!starts_with(branch_name, "refs/"))
317 return NULL;
319 /* OK, do we have that ref in the list? */
320 for (list = decoration; list; list = list->next)
321 if ((list->type == DECORATION_REF_LOCAL) &&
322 !strcmp(branch_name, list->name)) {
323 return list;
326 return NULL;
329 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
331 if (decoration_flags == DECORATE_SHORT_REFS)
332 strbuf_addstr(sb, prettify_refname(decoration->name));
333 else
334 strbuf_addstr(sb, decoration->name);
338 * The caller makes sure there is no funny color before calling.
339 * format_decorations ensures the same after return.
341 void format_decorations(struct strbuf *sb,
342 const struct commit *commit,
343 int use_color,
344 const struct decoration_options *opts)
346 const struct name_decoration *decoration;
347 const struct name_decoration *current_and_HEAD;
348 const char *color_commit, *color_reset;
350 const char *prefix = " (";
351 const char *suffix = ")";
352 const char *separator = ", ";
353 const char *pointer = " -> ";
354 const char *tag = "tag: ";
356 decoration = get_name_decoration(&commit->object);
357 if (!decoration)
358 return;
360 if (opts) {
361 if (opts->prefix)
362 prefix = opts->prefix;
363 if (opts->suffix)
364 suffix = opts->suffix;
365 if (opts->separator)
366 separator = opts->separator;
367 if (opts->pointer)
368 pointer = opts->pointer;
369 if (opts->tag)
370 tag = opts->tag;
373 color_commit = diff_get_color(use_color, DIFF_COMMIT);
374 color_reset = decorate_get_color(use_color, DECORATION_NONE);
376 current_and_HEAD = current_pointed_by_HEAD(decoration);
377 while (decoration) {
379 * When both current and HEAD are there, only
380 * show HEAD->current where HEAD would have
381 * appeared, skipping the entry for current.
383 if (decoration != current_and_HEAD) {
384 const char *color =
385 decorate_get_color(use_color, decoration->type);
387 if (*prefix) {
388 strbuf_addstr(sb, color_commit);
389 strbuf_addstr(sb, prefix);
390 strbuf_addstr(sb, color_reset);
393 if (*tag && decoration->type == DECORATION_REF_TAG) {
394 strbuf_addstr(sb, color);
395 strbuf_addstr(sb, tag);
396 strbuf_addstr(sb, color_reset);
399 strbuf_addstr(sb, color);
400 show_name(sb, decoration);
401 strbuf_addstr(sb, color_reset);
403 if (current_and_HEAD &&
404 decoration->type == DECORATION_REF_HEAD) {
405 strbuf_addstr(sb, color_commit);
406 strbuf_addstr(sb, pointer);
407 strbuf_addstr(sb, color_reset);
408 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
409 show_name(sb, current_and_HEAD);
410 strbuf_addstr(sb, color_reset);
413 prefix = separator;
415 decoration = decoration->next;
417 if (*suffix) {
418 strbuf_addstr(sb, color_commit);
419 strbuf_addstr(sb, suffix);
420 strbuf_addstr(sb, color_reset);
424 void show_decorations(struct rev_info *opt, struct commit *commit)
426 struct strbuf sb = STRBUF_INIT;
428 if (opt->sources) {
429 char **slot = revision_sources_peek(opt->sources, commit);
431 if (slot && *slot)
432 fprintf(opt->diffopt.file, "\t%s", *slot);
434 if (!opt->show_decorations)
435 return;
436 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
437 fputs(sb.buf, opt->diffopt.file);
438 strbuf_release(&sb);
441 void fmt_output_subject(struct strbuf *filename,
442 const char *subject,
443 struct rev_info *info)
445 const char *suffix = info->patch_suffix;
446 int nr = info->nr;
447 int start_len = filename->len;
448 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
450 if (info->reroll_count) {
451 struct strbuf temp = STRBUF_INIT;
453 strbuf_addf(&temp, "v%s", info->reroll_count);
454 format_sanitized_subject(filename, temp.buf, temp.len);
455 strbuf_addstr(filename, "-");
456 strbuf_release(&temp);
458 strbuf_addf(filename, "%04d-%s", nr, subject);
460 if (max_len < filename->len)
461 strbuf_setlen(filename, max_len);
462 strbuf_addstr(filename, suffix);
465 void fmt_output_commit(struct strbuf *filename,
466 struct commit *commit,
467 struct rev_info *info)
469 struct pretty_print_context ctx = {0};
470 struct strbuf subject = STRBUF_INIT;
472 repo_format_commit_message(the_repository, commit, "%f", &subject,
473 &ctx);
474 fmt_output_subject(filename, subject.buf, info);
475 strbuf_release(&subject);
478 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
480 if (opt->total > 0) {
481 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
482 opt->subject_prefix,
483 *opt->subject_prefix ? " " : "",
484 decimal_width(opt->total),
485 opt->nr, opt->total);
486 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
487 strbuf_addf(sb, "Subject: [%s] ",
488 opt->subject_prefix);
489 } else {
490 strbuf_addstr(sb, "Subject: ");
494 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
495 char **extra_headers_p,
496 int *need_8bit_cte_p,
497 int maybe_multipart)
499 struct strbuf headers = STRBUF_INIT;
500 const char *name = oid_to_hex(opt->zero_commit ?
501 null_oid() : &commit->object.oid);
503 *need_8bit_cte_p = 0; /* unknown */
505 if (opt->extra_headers && *opt->extra_headers)
506 strbuf_addstr(&headers, opt->extra_headers);
508 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
509 graph_show_oneline(opt->graph);
510 if (opt->message_id) {
511 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
512 graph_show_oneline(opt->graph);
514 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
515 int i, n;
516 n = opt->ref_message_ids->nr;
517 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
518 for (i = 0; i < n; i++)
519 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
520 opt->ref_message_ids->items[i].string);
521 graph_show_oneline(opt->graph);
523 if (opt->mime_boundary && maybe_multipart) {
524 static struct strbuf buffer = STRBUF_INIT;
525 struct strbuf filename = STRBUF_INIT;
526 *need_8bit_cte_p = -1; /* NEVER */
528 strbuf_reset(&buffer);
530 strbuf_addf(&headers,
531 "MIME-Version: 1.0\n"
532 "Content-Type: multipart/mixed;"
533 " boundary=\"%s%s\"\n"
534 "\n"
535 "This is a multi-part message in MIME "
536 "format.\n"
537 "--%s%s\n"
538 "Content-Type: text/plain; "
539 "charset=UTF-8; format=fixed\n"
540 "Content-Transfer-Encoding: 8bit\n\n",
541 mime_boundary_leader, opt->mime_boundary,
542 mime_boundary_leader, opt->mime_boundary);
544 if (opt->numbered_files)
545 strbuf_addf(&filename, "%d", opt->nr);
546 else
547 fmt_output_commit(&filename, commit, opt);
548 strbuf_addf(&buffer,
549 "\n--%s%s\n"
550 "Content-Type: text/x-patch;"
551 " name=\"%s\"\n"
552 "Content-Transfer-Encoding: 8bit\n"
553 "Content-Disposition: %s;"
554 " filename=\"%s\"\n\n",
555 mime_boundary_leader, opt->mime_boundary,
556 filename.buf,
557 opt->no_inline ? "attachment" : "inline",
558 filename.buf);
559 opt->diffopt.stat_sep = buffer.buf;
560 strbuf_release(&filename);
562 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
565 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
567 const char *color, *reset, *eol;
569 color = diff_get_color_opt(&opt->diffopt,
570 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
571 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
572 while (*bol) {
573 eol = strchrnul(bol, '\n');
574 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
575 *eol ? "\n" : "");
576 graph_show_oneline(opt->graph);
577 bol = (*eol) ? (eol + 1) : eol;
581 static void show_signature(struct rev_info *opt, struct commit *commit)
583 struct strbuf payload = STRBUF_INIT;
584 struct strbuf signature = STRBUF_INIT;
585 struct signature_check sigc = { 0 };
586 int status;
588 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
589 goto out;
591 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
592 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
593 status = check_signature(&sigc, signature.buf, signature.len);
594 if (status && !sigc.output)
595 show_sig_lines(opt, status, "No signature\n");
596 else
597 show_sig_lines(opt, status, sigc.output);
598 signature_check_clear(&sigc);
600 out:
601 strbuf_release(&payload);
602 strbuf_release(&signature);
605 static int which_parent(const struct object_id *oid, const struct commit *commit)
607 int nth;
608 const struct commit_list *parent;
610 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
611 if (oideq(&parent->item->object.oid, oid))
612 return nth;
613 nth++;
615 return -1;
618 static int is_common_merge(const struct commit *commit)
620 return (commit->parents
621 && commit->parents->next
622 && !commit->parents->next->next);
625 static int show_one_mergetag(struct commit *commit,
626 struct commit_extra_header *extra,
627 void *data)
629 struct rev_info *opt = (struct rev_info *)data;
630 struct object_id oid;
631 struct tag *tag;
632 struct strbuf verify_message;
633 struct signature_check sigc = { 0 };
634 int status, nth;
635 struct strbuf payload = STRBUF_INIT;
636 struct strbuf signature = STRBUF_INIT;
638 hash_object_file(the_hash_algo, extra->value, extra->len,
639 OBJ_TAG, &oid);
640 tag = lookup_tag(the_repository, &oid);
641 if (!tag)
642 return -1; /* error message already given */
644 strbuf_init(&verify_message, 256);
645 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
646 strbuf_addstr(&verify_message, "malformed mergetag\n");
647 else if (is_common_merge(commit) &&
648 oideq(&tag->tagged->oid,
649 &commit->parents->next->item->object.oid))
650 strbuf_addf(&verify_message,
651 "merged tag '%s'\n", tag->tag);
652 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
653 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
654 tag->tag, oid_to_hex(&tag->tagged->oid));
655 else
656 strbuf_addf(&verify_message,
657 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
659 status = -1;
660 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
661 /* could have a good signature */
662 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
663 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
664 status = check_signature(&sigc, signature.buf, signature.len);
665 if (sigc.output)
666 strbuf_addstr(&verify_message, sigc.output);
667 else
668 strbuf_addstr(&verify_message, "No signature\n");
669 signature_check_clear(&sigc);
670 /* otherwise we couldn't verify, which is shown as bad */
673 show_sig_lines(opt, status, verify_message.buf);
674 strbuf_release(&verify_message);
675 strbuf_release(&payload);
676 strbuf_release(&signature);
677 return 0;
680 static int show_mergetag(struct rev_info *opt, struct commit *commit)
682 return for_each_mergetag(show_one_mergetag, commit, opt);
685 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
687 const char *x = opt->shown_dashes ? "\n" : "---\n";
688 if (sb)
689 strbuf_addstr(sb, x);
690 else
691 fputs(x, opt->diffopt.file);
692 opt->shown_dashes = 1;
695 static void show_diff_of_diff(struct rev_info *opt)
697 if (!cmit_fmt_is_mail(opt->commit_format))
698 return;
700 if (opt->idiff_oid1) {
701 struct diff_queue_struct dq;
703 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
704 diff_queue_init(&diff_queued_diff);
706 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
707 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
708 &opt->diffopt);
710 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
713 if (opt->rdiff1) {
714 struct diff_queue_struct dq;
715 struct diff_options opts;
716 struct range_diff_options range_diff_opts = {
717 .creation_factor = opt->creation_factor,
718 .dual_color = 1,
719 .diffopt = &opts
722 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
723 diff_queue_init(&diff_queued_diff);
725 fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
727 * Pass minimum required diff-options to range-diff; others
728 * can be added later if deemed desirable.
730 repo_diff_setup(the_repository, &opts);
731 opts.file = opt->diffopt.file;
732 opts.use_color = opt->diffopt.use_color;
733 diff_setup_done(&opts);
734 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
736 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
740 void show_log(struct rev_info *opt)
742 struct strbuf msgbuf = STRBUF_INIT;
743 struct log_info *log = opt->loginfo;
744 struct commit *commit = log->commit, *parent = log->parent;
745 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
746 struct pretty_print_context ctx = {0};
748 opt->loginfo = NULL;
749 if (!opt->verbose_header) {
750 graph_show_commit(opt->graph);
752 if (!opt->graph)
753 put_revision_mark(opt, commit);
754 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
755 opt->diffopt.file);
756 if (opt->print_parents)
757 show_parents(commit, abbrev_commit, opt->diffopt.file);
758 if (opt->children.name)
759 show_children(opt, commit, abbrev_commit);
760 show_decorations(opt, commit);
761 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
762 putc('\n', opt->diffopt.file);
763 graph_show_remainder(opt->graph);
765 putc(opt->diffopt.line_termination, opt->diffopt.file);
766 return;
770 * If use_terminator is set, we already handled any record termination
771 * at the end of the last record.
772 * Otherwise, add a diffopt.line_termination character before all
773 * entries but the first. (IOW, as a separator between entries)
775 if (opt->shown_one && !opt->use_terminator) {
777 * If entries are separated by a newline, the output
778 * should look human-readable. If the last entry ended
779 * with a newline, print the graph output before this
780 * newline. Otherwise it will end up as a completely blank
781 * line and will look like a gap in the graph.
783 * If the entry separator is not a newline, the output is
784 * primarily intended for programmatic consumption, and we
785 * never want the extra graph output before the entry
786 * separator.
788 if (opt->diffopt.line_termination == '\n' &&
789 !opt->missing_newline)
790 graph_show_padding(opt->graph);
791 putc(opt->diffopt.line_termination, opt->diffopt.file);
793 opt->shown_one = 1;
796 * If the history graph was requested,
797 * print the graph, up to this commit's line
799 graph_show_commit(opt->graph);
802 * Print header line of header..
805 if (cmit_fmt_is_mail(opt->commit_format)) {
806 log_write_email_headers(opt, commit, &ctx.after_subject,
807 &ctx.need_8bit_cte, 1);
808 ctx.rev = opt;
809 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
810 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
811 if (opt->commit_format != CMIT_FMT_ONELINE)
812 fputs("commit ", opt->diffopt.file);
814 if (!opt->graph)
815 put_revision_mark(opt, commit);
816 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
817 abbrev_commit),
818 opt->diffopt.file);
819 if (opt->print_parents)
820 show_parents(commit, abbrev_commit, opt->diffopt.file);
821 if (opt->children.name)
822 show_children(opt, commit, abbrev_commit);
823 if (parent)
824 fprintf(opt->diffopt.file, " (from %s)",
825 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
826 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
827 show_decorations(opt, commit);
828 if (opt->commit_format == CMIT_FMT_ONELINE) {
829 putc(' ', opt->diffopt.file);
830 } else {
831 putc('\n', opt->diffopt.file);
832 graph_show_oneline(opt->graph);
834 if (opt->reflog_info) {
836 * setup_revisions() ensures that opt->reflog_info
837 * and opt->graph cannot both be set,
838 * so we don't need to worry about printing the
839 * graph info here.
841 show_reflog_message(opt->reflog_info,
842 opt->commit_format == CMIT_FMT_ONELINE,
843 opt->date_mode,
844 opt->date_mode_explicit);
845 if (opt->commit_format == CMIT_FMT_ONELINE)
846 return;
850 if (opt->show_signature) {
851 show_signature(opt, commit);
852 show_mergetag(opt, commit);
855 if (opt->show_notes) {
856 int raw;
857 struct strbuf notebuf = STRBUF_INIT;
859 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
860 format_display_notes(&commit->object.oid, &notebuf,
861 get_log_output_encoding(), raw);
862 ctx.notes_message = strbuf_detach(&notebuf, NULL);
866 * And then the pretty-printed message itself
868 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
869 ctx.need_8bit_cte =
870 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
871 ctx.date_mode = opt->date_mode;
872 ctx.date_mode_explicit = opt->date_mode_explicit;
873 ctx.abbrev = opt->diffopt.abbrev;
874 ctx.preserve_subject = opt->preserve_subject;
875 ctx.encode_email_headers = opt->encode_email_headers;
876 ctx.reflog_info = opt->reflog_info;
877 ctx.fmt = opt->commit_format;
878 ctx.mailmap = opt->mailmap;
879 ctx.color = opt->diffopt.use_color;
880 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
881 ctx.output_encoding = get_log_output_encoding();
882 ctx.rev = opt;
883 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
884 ctx.from_ident = &opt->from_ident;
885 if (opt->graph)
886 ctx.graph_width = graph_width(opt->graph);
887 pretty_print_commit(&ctx, commit, &msgbuf);
889 if (opt->add_signoff)
890 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
892 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
893 ctx.notes_message && *ctx.notes_message) {
894 if (cmit_fmt_is_mail(ctx.fmt))
895 next_commentary_block(opt, &msgbuf);
896 strbuf_addstr(&msgbuf, ctx.notes_message);
899 if (opt->show_log_size) {
900 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
901 graph_show_oneline(opt->graph);
905 * Set opt->missing_newline if msgbuf doesn't
906 * end in a newline (including if it is empty)
908 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
909 opt->missing_newline = 1;
910 else
911 opt->missing_newline = 0;
913 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
914 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
915 if (!opt->missing_newline)
916 graph_show_padding(opt->graph);
917 putc(opt->diffopt.line_termination, opt->diffopt.file);
920 strbuf_release(&msgbuf);
921 free(ctx.notes_message);
922 free(ctx.after_subject);
925 int log_tree_diff_flush(struct rev_info *opt)
927 opt->shown_dashes = 0;
928 diffcore_std(&opt->diffopt);
930 if (diff_queue_is_empty(&opt->diffopt)) {
931 int saved_fmt = opt->diffopt.output_format;
932 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
933 diff_flush(&opt->diffopt);
934 opt->diffopt.output_format = saved_fmt;
935 return 0;
938 if (opt->loginfo && !opt->no_commit_id) {
939 show_log(opt);
940 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
941 opt->verbose_header &&
942 opt->commit_format != CMIT_FMT_ONELINE &&
943 !commit_format_is_empty(opt->commit_format)) {
945 * When showing a verbose header (i.e. log message),
946 * and not in --pretty=oneline format, we would want
947 * an extra newline between the end of log and the
948 * diff/diffstat output for readability.
950 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
951 fputs(diff_line_prefix(&opt->diffopt), opt->diffopt.file);
954 * We may have shown three-dashes line early
955 * between generated commentary (notes, etc.)
956 * and the log message, in which case we only
957 * want a blank line after the commentary
958 * without (an extra) three-dashes line.
959 * Otherwise, we show the three-dashes line if
960 * we are showing the patch with diffstat, but
961 * in that case, there is no extra blank line
962 * after the three-dashes line.
964 if (!opt->shown_dashes &&
965 (pch & opt->diffopt.output_format) == pch)
966 fprintf(opt->diffopt.file, "---");
967 putc('\n', opt->diffopt.file);
970 diff_flush(&opt->diffopt);
971 return 1;
974 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
976 diff_tree_combined_merge(commit, opt);
977 return !opt->loginfo;
980 static void setup_additional_headers(struct diff_options *o,
981 struct strmap *all_headers)
983 struct hashmap_iter iter;
984 struct strmap_entry *entry;
987 * Make o->additional_path_headers contain the subset of all_headers
988 * that match o->pathspec. If there aren't any that match o->pathspec,
989 * then make o->additional_path_headers be NULL.
992 if (!o->pathspec.nr) {
993 o->additional_path_headers = all_headers;
994 return;
997 o->additional_path_headers = xmalloc(sizeof(struct strmap));
998 strmap_init_with_options(o->additional_path_headers, NULL, 0);
999 strmap_for_each_entry(all_headers, &iter, entry) {
1000 if (match_pathspec(the_repository->index, &o->pathspec,
1001 entry->key, strlen(entry->key),
1002 0 /* prefix */, NULL /* seen */,
1003 0 /* is_dir */))
1004 strmap_put(o->additional_path_headers,
1005 entry->key, entry->value);
1007 if (!strmap_get_size(o->additional_path_headers)) {
1008 strmap_clear(o->additional_path_headers, 0);
1009 FREE_AND_NULL(o->additional_path_headers);
1013 static void cleanup_additional_headers(struct diff_options *o)
1015 if (!o->pathspec.nr) {
1016 o->additional_path_headers = NULL;
1017 return;
1019 if (!o->additional_path_headers)
1020 return;
1022 strmap_clear(o->additional_path_headers, 0);
1023 FREE_AND_NULL(o->additional_path_headers);
1026 static int do_remerge_diff(struct rev_info *opt,
1027 struct commit_list *parents,
1028 struct object_id *oid)
1030 struct merge_options o;
1031 struct commit_list *bases = NULL;
1032 struct merge_result res = {0};
1033 struct pretty_print_context ctx = {0};
1034 struct commit *parent1 = parents->item;
1035 struct commit *parent2 = parents->next->item;
1036 struct strbuf parent1_desc = STRBUF_INIT;
1037 struct strbuf parent2_desc = STRBUF_INIT;
1040 * Lazily prepare a temporary object directory and rotate it
1041 * into the alternative object store list as the primary.
1043 if (opt->remerge_diff && !opt->remerge_objdir) {
1044 opt->remerge_objdir = tmp_objdir_create("remerge-diff");
1045 if (!opt->remerge_objdir)
1046 return error(_("unable to create temporary object directory"));
1047 tmp_objdir_replace_primary_odb(opt->remerge_objdir, 1);
1050 /* Setup merge options */
1051 init_ui_merge_options(&o, the_repository);
1052 o.show_rename_progress = 0;
1053 o.record_conflict_msgs_as_headers = 1;
1054 o.msg_header_prefix = "remerge";
1056 ctx.abbrev = DEFAULT_ABBREV;
1057 repo_format_commit_message(the_repository, parent1, "%h (%s)",
1058 &parent1_desc, &ctx);
1059 repo_format_commit_message(the_repository, parent2, "%h (%s)",
1060 &parent2_desc, &ctx);
1061 o.branch1 = parent1_desc.buf;
1062 o.branch2 = parent2_desc.buf;
1064 /* Parse the relevant commits and get the merge bases */
1065 parse_commit_or_die(parent1);
1066 parse_commit_or_die(parent2);
1067 if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)
1068 exit(128);
1070 /* Re-merge the parents */
1071 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1073 /* Show the diff */
1074 setup_additional_headers(&opt->diffopt, res.path_messages);
1075 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1076 log_tree_diff_flush(opt);
1078 /* Cleanup */
1079 free_commit_list(bases);
1080 cleanup_additional_headers(&opt->diffopt);
1081 strbuf_release(&parent1_desc);
1082 strbuf_release(&parent2_desc);
1083 merge_finalize(&o, &res);
1085 /* Clean up the contents of the temporary object directory */
1086 tmp_objdir_discard_objects(opt->remerge_objdir);
1088 return !opt->loginfo;
1092 * Show the diff of a commit.
1094 * Return true if we printed any log info messages
1096 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1098 int showed_log;
1099 struct commit_list *parents;
1100 struct object_id *oid;
1101 int is_merge;
1102 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1104 if (!all_need_diff && !opt->merges_need_diff)
1105 return 0;
1107 parse_commit_or_die(commit);
1108 oid = get_commit_tree_oid(commit);
1110 parents = get_saved_parents(opt, commit);
1111 is_merge = parents && parents->next;
1112 if (!is_merge && !all_need_diff)
1113 return 0;
1115 /* Root commit? */
1116 if (!parents) {
1117 if (opt->show_root_diff) {
1118 diff_root_tree_oid(oid, "", &opt->diffopt);
1119 log_tree_diff_flush(opt);
1121 return !opt->loginfo;
1124 if (is_merge) {
1125 int octopus = (parents->next->next != NULL);
1127 if (opt->remerge_diff) {
1128 if (octopus) {
1129 show_log(opt);
1130 fprintf(opt->diffopt.file,
1131 "diff: warning: Skipping remerge-diff "
1132 "for octopus merges.\n");
1133 return 1;
1135 return do_remerge_diff(opt, parents, oid);
1137 if (opt->combine_merges)
1138 return do_diff_combined(opt, commit);
1139 if (opt->separate_merges) {
1140 if (!opt->first_parent_merges) {
1141 /* Show parent info for multiple diffs */
1142 log->parent = parents->item;
1144 } else
1145 return 0;
1148 showed_log = 0;
1149 for (;;) {
1150 struct commit *parent = parents->item;
1152 parse_commit_or_die(parent);
1153 diff_tree_oid(get_commit_tree_oid(parent),
1154 oid, "", &opt->diffopt);
1155 log_tree_diff_flush(opt);
1157 showed_log |= !opt->loginfo;
1159 /* Set up the log info for the next parent, if any.. */
1160 parents = parents->next;
1161 if (!parents || opt->first_parent_merges)
1162 break;
1163 log->parent = parents->item;
1164 opt->loginfo = log;
1166 return showed_log;
1169 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1171 struct log_info log;
1172 int shown;
1173 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1174 int no_free = opt->diffopt.no_free;
1176 log.commit = commit;
1177 log.parent = NULL;
1178 opt->loginfo = &log;
1179 opt->diffopt.no_free = 1;
1181 /* NEEDSWORK: no restoring of no_free? Why? */
1182 if (opt->line_level_traverse)
1183 return line_log_print(opt, commit);
1185 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1186 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1187 shown = log_tree_diff(opt, commit, &log);
1188 if (!shown && opt->loginfo && opt->always_show_header) {
1189 log.parent = NULL;
1190 show_log(opt);
1191 shown = 1;
1193 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1194 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1195 if (shown)
1196 show_diff_of_diff(opt);
1197 opt->loginfo = NULL;
1198 maybe_flush_or_die(opt->diffopt.file, "stdout");
1199 opt->diffopt.no_free = no_free;
1201 diff_free(&opt->diffopt);
1202 return shown;