Start preparing for Git 2.46.2
[git.git] / log-tree.c
blob13524bc888186aabcf3caff2d92106203007211e
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 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 decoration_loaded = 1;
237 decoration_flags = flags;
238 refs_for_each_ref(get_main_ref_store(the_repository),
239 add_ref_decoration, filter);
240 refs_head_ref(get_main_ref_store(the_repository),
241 add_ref_decoration, filter);
242 for_each_commit_graft(add_graft_decoration, filter);
246 static void show_parents(struct commit *commit, int abbrev, FILE *file)
248 struct commit_list *p;
249 for (p = commit->parents; p ; p = p->next) {
250 struct commit *parent = p->item;
251 fprintf(file, " %s",
252 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
256 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
258 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
259 for ( ; p; p = p->next) {
260 fprintf(opt->diffopt.file, " %s",
261 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
266 * Do we have HEAD in the output, and also the branch it points at?
267 * If so, find that decoration entry for that current branch.
269 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
271 const struct name_decoration *list, *head = NULL;
272 const char *branch_name = NULL;
273 int rru_flags;
275 /* First find HEAD */
276 for (list = decoration; list; list = list->next)
277 if (list->type == DECORATION_REF_HEAD) {
278 head = list;
279 break;
281 if (!head)
282 return NULL;
284 /* Now resolve and find the matching current branch */
285 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
286 "HEAD", 0, NULL, &rru_flags);
287 if (!branch_name || !(rru_flags & REF_ISSYMREF))
288 return NULL;
290 if (!starts_with(branch_name, "refs/"))
291 return NULL;
293 /* OK, do we have that ref in the list? */
294 for (list = decoration; list; list = list->next)
295 if ((list->type == DECORATION_REF_LOCAL) &&
296 !strcmp(branch_name, list->name)) {
297 return list;
300 return NULL;
303 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
305 if (decoration_flags == DECORATE_SHORT_REFS)
306 strbuf_addstr(sb, prettify_refname(decoration->name));
307 else
308 strbuf_addstr(sb, decoration->name);
312 * The caller makes sure there is no funny color before calling.
313 * format_decorations ensures the same after return.
315 void format_decorations(struct strbuf *sb,
316 const struct commit *commit,
317 int use_color,
318 const struct decoration_options *opts)
320 const struct name_decoration *decoration;
321 const struct name_decoration *current_and_HEAD;
322 const char *color_commit, *color_reset;
324 const char *prefix = " (";
325 const char *suffix = ")";
326 const char *separator = ", ";
327 const char *pointer = " -> ";
328 const char *tag = "tag: ";
330 decoration = get_name_decoration(&commit->object);
331 if (!decoration)
332 return;
334 if (opts) {
335 if (opts->prefix)
336 prefix = opts->prefix;
337 if (opts->suffix)
338 suffix = opts->suffix;
339 if (opts->separator)
340 separator = opts->separator;
341 if (opts->pointer)
342 pointer = opts->pointer;
343 if (opts->tag)
344 tag = opts->tag;
347 color_commit = diff_get_color(use_color, DIFF_COMMIT);
348 color_reset = decorate_get_color(use_color, DECORATION_NONE);
350 current_and_HEAD = current_pointed_by_HEAD(decoration);
351 while (decoration) {
353 * When both current and HEAD are there, only
354 * show HEAD->current where HEAD would have
355 * appeared, skipping the entry for current.
357 if (decoration != current_and_HEAD) {
358 const char *color =
359 decorate_get_color(use_color, decoration->type);
361 if (*prefix) {
362 strbuf_addstr(sb, color_commit);
363 strbuf_addstr(sb, prefix);
364 strbuf_addstr(sb, color_reset);
367 if (*tag && decoration->type == DECORATION_REF_TAG) {
368 strbuf_addstr(sb, color);
369 strbuf_addstr(sb, tag);
370 strbuf_addstr(sb, color_reset);
373 strbuf_addstr(sb, color);
374 show_name(sb, decoration);
375 strbuf_addstr(sb, color_reset);
377 if (current_and_HEAD &&
378 decoration->type == DECORATION_REF_HEAD) {
379 strbuf_addstr(sb, color_commit);
380 strbuf_addstr(sb, pointer);
381 strbuf_addstr(sb, color_reset);
382 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
383 show_name(sb, current_and_HEAD);
384 strbuf_addstr(sb, color_reset);
387 prefix = separator;
389 decoration = decoration->next;
391 if (*suffix) {
392 strbuf_addstr(sb, color_commit);
393 strbuf_addstr(sb, suffix);
394 strbuf_addstr(sb, color_reset);
398 void show_decorations(struct rev_info *opt, struct commit *commit)
400 struct strbuf sb = STRBUF_INIT;
402 if (opt->sources) {
403 char **slot = revision_sources_peek(opt->sources, commit);
405 if (slot && *slot)
406 fprintf(opt->diffopt.file, "\t%s", *slot);
408 if (!opt->show_decorations)
409 return;
410 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
411 fputs(sb.buf, opt->diffopt.file);
412 strbuf_release(&sb);
415 void fmt_output_subject(struct strbuf *filename,
416 const char *subject,
417 struct rev_info *info)
419 const char *suffix = info->patch_suffix;
420 int nr = info->nr;
421 int start_len = filename->len;
422 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
424 if (info->reroll_count) {
425 struct strbuf temp = STRBUF_INIT;
427 strbuf_addf(&temp, "v%s", info->reroll_count);
428 format_sanitized_subject(filename, temp.buf, temp.len);
429 strbuf_addstr(filename, "-");
430 strbuf_release(&temp);
432 strbuf_addf(filename, "%04d-%s", nr, subject);
434 if (max_len < filename->len)
435 strbuf_setlen(filename, max_len);
436 strbuf_addstr(filename, suffix);
439 void fmt_output_commit(struct strbuf *filename,
440 struct commit *commit,
441 struct rev_info *info)
443 struct pretty_print_context ctx = {0};
444 struct strbuf subject = STRBUF_INIT;
446 repo_format_commit_message(the_repository, commit, "%f", &subject,
447 &ctx);
448 fmt_output_subject(filename, subject.buf, info);
449 strbuf_release(&subject);
452 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
454 if (opt->total > 0) {
455 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
456 opt->subject_prefix,
457 *opt->subject_prefix ? " " : "",
458 decimal_width(opt->total),
459 opt->nr, opt->total);
460 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
461 strbuf_addf(sb, "Subject: [%s] ",
462 opt->subject_prefix);
463 } else {
464 strbuf_addstr(sb, "Subject: ");
468 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
469 char **extra_headers_p,
470 int *need_8bit_cte_p,
471 int maybe_multipart)
473 struct strbuf headers = STRBUF_INIT;
474 const char *name = oid_to_hex(opt->zero_commit ?
475 null_oid() : &commit->object.oid);
477 *need_8bit_cte_p = 0; /* unknown */
479 if (opt->extra_headers && *opt->extra_headers)
480 strbuf_addstr(&headers, opt->extra_headers);
482 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
483 graph_show_oneline(opt->graph);
484 if (opt->message_id) {
485 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
486 graph_show_oneline(opt->graph);
488 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
489 int i, n;
490 n = opt->ref_message_ids->nr;
491 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
492 for (i = 0; i < n; i++)
493 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
494 opt->ref_message_ids->items[i].string);
495 graph_show_oneline(opt->graph);
497 if (opt->mime_boundary && maybe_multipart) {
498 static struct strbuf buffer = STRBUF_INIT;
499 struct strbuf filename = STRBUF_INIT;
500 *need_8bit_cte_p = -1; /* NEVER */
502 strbuf_reset(&buffer);
504 strbuf_addf(&headers,
505 "MIME-Version: 1.0\n"
506 "Content-Type: multipart/mixed;"
507 " boundary=\"%s%s\"\n"
508 "\n"
509 "This is a multi-part message in MIME "
510 "format.\n"
511 "--%s%s\n"
512 "Content-Type: text/plain; "
513 "charset=UTF-8; format=fixed\n"
514 "Content-Transfer-Encoding: 8bit\n\n",
515 mime_boundary_leader, opt->mime_boundary,
516 mime_boundary_leader, opt->mime_boundary);
518 if (opt->numbered_files)
519 strbuf_addf(&filename, "%d", opt->nr);
520 else
521 fmt_output_commit(&filename, commit, opt);
522 strbuf_addf(&buffer,
523 "\n--%s%s\n"
524 "Content-Type: text/x-patch;"
525 " name=\"%s\"\n"
526 "Content-Transfer-Encoding: 8bit\n"
527 "Content-Disposition: %s;"
528 " filename=\"%s\"\n\n",
529 mime_boundary_leader, opt->mime_boundary,
530 filename.buf,
531 opt->no_inline ? "attachment" : "inline",
532 filename.buf);
533 opt->diffopt.stat_sep = buffer.buf;
534 strbuf_release(&filename);
536 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
539 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
541 const char *color, *reset, *eol;
543 color = diff_get_color_opt(&opt->diffopt,
544 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
545 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
546 while (*bol) {
547 eol = strchrnul(bol, '\n');
548 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
549 *eol ? "\n" : "");
550 graph_show_oneline(opt->graph);
551 bol = (*eol) ? (eol + 1) : eol;
555 static void show_signature(struct rev_info *opt, struct commit *commit)
557 struct strbuf payload = STRBUF_INIT;
558 struct strbuf signature = STRBUF_INIT;
559 struct signature_check sigc = { 0 };
560 int status;
562 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
563 goto out;
565 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
566 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
567 status = check_signature(&sigc, signature.buf, signature.len);
568 if (status && !sigc.output)
569 show_sig_lines(opt, status, "No signature\n");
570 else
571 show_sig_lines(opt, status, sigc.output);
572 signature_check_clear(&sigc);
574 out:
575 strbuf_release(&payload);
576 strbuf_release(&signature);
579 static int which_parent(const struct object_id *oid, const struct commit *commit)
581 int nth;
582 const struct commit_list *parent;
584 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
585 if (oideq(&parent->item->object.oid, oid))
586 return nth;
587 nth++;
589 return -1;
592 static int is_common_merge(const struct commit *commit)
594 return (commit->parents
595 && commit->parents->next
596 && !commit->parents->next->next);
599 static int show_one_mergetag(struct commit *commit,
600 struct commit_extra_header *extra,
601 void *data)
603 struct rev_info *opt = (struct rev_info *)data;
604 struct object_id oid;
605 struct tag *tag;
606 struct strbuf verify_message;
607 struct signature_check sigc = { 0 };
608 int status, nth;
609 struct strbuf payload = STRBUF_INIT;
610 struct strbuf signature = STRBUF_INIT;
612 hash_object_file(the_hash_algo, extra->value, extra->len,
613 OBJ_TAG, &oid);
614 tag = lookup_tag(the_repository, &oid);
615 if (!tag)
616 return -1; /* error message already given */
618 strbuf_init(&verify_message, 256);
619 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
620 strbuf_addstr(&verify_message, "malformed mergetag\n");
621 else if (is_common_merge(commit) &&
622 oideq(&tag->tagged->oid,
623 &commit->parents->next->item->object.oid))
624 strbuf_addf(&verify_message,
625 "merged tag '%s'\n", tag->tag);
626 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
627 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
628 tag->tag, oid_to_hex(&tag->tagged->oid));
629 else
630 strbuf_addf(&verify_message,
631 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
633 status = -1;
634 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
635 /* could have a good signature */
636 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
637 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
638 status = check_signature(&sigc, signature.buf, signature.len);
639 if (sigc.output)
640 strbuf_addstr(&verify_message, sigc.output);
641 else
642 strbuf_addstr(&verify_message, "No signature\n");
643 signature_check_clear(&sigc);
644 /* otherwise we couldn't verify, which is shown as bad */
647 show_sig_lines(opt, status, verify_message.buf);
648 strbuf_release(&verify_message);
649 strbuf_release(&payload);
650 strbuf_release(&signature);
651 return 0;
654 static int show_mergetag(struct rev_info *opt, struct commit *commit)
656 return for_each_mergetag(show_one_mergetag, commit, opt);
659 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
661 const char *x = opt->shown_dashes ? "\n" : "---\n";
662 if (sb)
663 strbuf_addstr(sb, x);
664 else
665 fputs(x, opt->diffopt.file);
666 opt->shown_dashes = 1;
669 static void show_diff_of_diff(struct rev_info *opt)
671 if (!cmit_fmt_is_mail(opt->commit_format))
672 return;
674 if (opt->idiff_oid1) {
675 struct diff_queue_struct dq;
677 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
678 DIFF_QUEUE_CLEAR(&diff_queued_diff);
680 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
681 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
682 &opt->diffopt);
684 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
687 if (opt->rdiff1) {
688 struct diff_queue_struct dq;
689 struct diff_options opts;
690 struct range_diff_options range_diff_opts = {
691 .creation_factor = opt->creation_factor,
692 .dual_color = 1,
693 .diffopt = &opts
696 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
697 DIFF_QUEUE_CLEAR(&diff_queued_diff);
699 fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
701 * Pass minimum required diff-options to range-diff; others
702 * can be added later if deemed desirable.
704 repo_diff_setup(the_repository, &opts);
705 opts.file = opt->diffopt.file;
706 opts.use_color = opt->diffopt.use_color;
707 diff_setup_done(&opts);
708 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
710 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
714 void show_log(struct rev_info *opt)
716 struct strbuf msgbuf = STRBUF_INIT;
717 struct log_info *log = opt->loginfo;
718 struct commit *commit = log->commit, *parent = log->parent;
719 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
720 struct pretty_print_context ctx = {0};
722 opt->loginfo = NULL;
723 if (!opt->verbose_header) {
724 graph_show_commit(opt->graph);
726 if (!opt->graph)
727 put_revision_mark(opt, commit);
728 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
729 opt->diffopt.file);
730 if (opt->print_parents)
731 show_parents(commit, abbrev_commit, opt->diffopt.file);
732 if (opt->children.name)
733 show_children(opt, commit, abbrev_commit);
734 show_decorations(opt, commit);
735 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
736 putc('\n', opt->diffopt.file);
737 graph_show_remainder(opt->graph);
739 putc(opt->diffopt.line_termination, opt->diffopt.file);
740 return;
744 * If use_terminator is set, we already handled any record termination
745 * at the end of the last record.
746 * Otherwise, add a diffopt.line_termination character before all
747 * entries but the first. (IOW, as a separator between entries)
749 if (opt->shown_one && !opt->use_terminator) {
751 * If entries are separated by a newline, the output
752 * should look human-readable. If the last entry ended
753 * with a newline, print the graph output before this
754 * newline. Otherwise it will end up as a completely blank
755 * line and will look like a gap in the graph.
757 * If the entry separator is not a newline, the output is
758 * primarily intended for programmatic consumption, and we
759 * never want the extra graph output before the entry
760 * separator.
762 if (opt->diffopt.line_termination == '\n' &&
763 !opt->missing_newline)
764 graph_show_padding(opt->graph);
765 putc(opt->diffopt.line_termination, opt->diffopt.file);
767 opt->shown_one = 1;
770 * If the history graph was requested,
771 * print the graph, up to this commit's line
773 graph_show_commit(opt->graph);
776 * Print header line of header..
779 if (cmit_fmt_is_mail(opt->commit_format)) {
780 log_write_email_headers(opt, commit, &ctx.after_subject,
781 &ctx.need_8bit_cte, 1);
782 ctx.rev = opt;
783 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
784 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
785 if (opt->commit_format != CMIT_FMT_ONELINE)
786 fputs("commit ", opt->diffopt.file);
788 if (!opt->graph)
789 put_revision_mark(opt, commit);
790 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
791 abbrev_commit),
792 opt->diffopt.file);
793 if (opt->print_parents)
794 show_parents(commit, abbrev_commit, opt->diffopt.file);
795 if (opt->children.name)
796 show_children(opt, commit, abbrev_commit);
797 if (parent)
798 fprintf(opt->diffopt.file, " (from %s)",
799 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
800 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
801 show_decorations(opt, commit);
802 if (opt->commit_format == CMIT_FMT_ONELINE) {
803 putc(' ', opt->diffopt.file);
804 } else {
805 putc('\n', opt->diffopt.file);
806 graph_show_oneline(opt->graph);
808 if (opt->reflog_info) {
810 * setup_revisions() ensures that opt->reflog_info
811 * and opt->graph cannot both be set,
812 * so we don't need to worry about printing the
813 * graph info here.
815 show_reflog_message(opt->reflog_info,
816 opt->commit_format == CMIT_FMT_ONELINE,
817 opt->date_mode,
818 opt->date_mode_explicit);
819 if (opt->commit_format == CMIT_FMT_ONELINE)
820 return;
824 if (opt->show_signature) {
825 show_signature(opt, commit);
826 show_mergetag(opt, commit);
829 if (opt->show_notes) {
830 int raw;
831 struct strbuf notebuf = STRBUF_INIT;
833 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
834 format_display_notes(&commit->object.oid, &notebuf,
835 get_log_output_encoding(), raw);
836 ctx.notes_message = strbuf_detach(&notebuf, NULL);
840 * And then the pretty-printed message itself
842 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
843 ctx.need_8bit_cte =
844 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
845 ctx.date_mode = opt->date_mode;
846 ctx.date_mode_explicit = opt->date_mode_explicit;
847 ctx.abbrev = opt->diffopt.abbrev;
848 ctx.preserve_subject = opt->preserve_subject;
849 ctx.encode_email_headers = opt->encode_email_headers;
850 ctx.reflog_info = opt->reflog_info;
851 ctx.fmt = opt->commit_format;
852 ctx.mailmap = opt->mailmap;
853 ctx.color = opt->diffopt.use_color;
854 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
855 ctx.output_encoding = get_log_output_encoding();
856 ctx.rev = opt;
857 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
858 ctx.from_ident = &opt->from_ident;
859 if (opt->graph)
860 ctx.graph_width = graph_width(opt->graph);
861 pretty_print_commit(&ctx, commit, &msgbuf);
863 if (opt->add_signoff)
864 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
866 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
867 ctx.notes_message && *ctx.notes_message) {
868 if (cmit_fmt_is_mail(ctx.fmt))
869 next_commentary_block(opt, &msgbuf);
870 strbuf_addstr(&msgbuf, ctx.notes_message);
873 if (opt->show_log_size) {
874 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
875 graph_show_oneline(opt->graph);
879 * Set opt->missing_newline if msgbuf doesn't
880 * end in a newline (including if it is empty)
882 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
883 opt->missing_newline = 1;
884 else
885 opt->missing_newline = 0;
887 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
888 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
889 if (!opt->missing_newline)
890 graph_show_padding(opt->graph);
891 putc(opt->diffopt.line_termination, opt->diffopt.file);
894 strbuf_release(&msgbuf);
895 free(ctx.notes_message);
896 free(ctx.after_subject);
899 int log_tree_diff_flush(struct rev_info *opt)
901 opt->shown_dashes = 0;
902 diffcore_std(&opt->diffopt);
904 if (diff_queue_is_empty(&opt->diffopt)) {
905 int saved_fmt = opt->diffopt.output_format;
906 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
907 diff_flush(&opt->diffopt);
908 opt->diffopt.output_format = saved_fmt;
909 return 0;
912 if (opt->loginfo && !opt->no_commit_id) {
913 show_log(opt);
914 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
915 opt->verbose_header &&
916 opt->commit_format != CMIT_FMT_ONELINE &&
917 !commit_format_is_empty(opt->commit_format)) {
919 * When showing a verbose header (i.e. log message),
920 * and not in --pretty=oneline format, we would want
921 * an extra newline between the end of log and the
922 * diff/diffstat output for readability.
924 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
925 if (opt->diffopt.output_prefix) {
926 struct strbuf *msg = NULL;
927 msg = opt->diffopt.output_prefix(&opt->diffopt,
928 opt->diffopt.output_prefix_data);
929 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
933 * We may have shown three-dashes line early
934 * between generated commentary (notes, etc.)
935 * and the log message, in which case we only
936 * want a blank line after the commentary
937 * without (an extra) three-dashes line.
938 * Otherwise, we show the three-dashes line if
939 * we are showing the patch with diffstat, but
940 * in that case, there is no extra blank line
941 * after the three-dashes line.
943 if (!opt->shown_dashes &&
944 (pch & opt->diffopt.output_format) == pch)
945 fprintf(opt->diffopt.file, "---");
946 putc('\n', opt->diffopt.file);
949 diff_flush(&opt->diffopt);
950 return 1;
953 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
955 diff_tree_combined_merge(commit, opt);
956 return !opt->loginfo;
959 static void setup_additional_headers(struct diff_options *o,
960 struct strmap *all_headers)
962 struct hashmap_iter iter;
963 struct strmap_entry *entry;
966 * Make o->additional_path_headers contain the subset of all_headers
967 * that match o->pathspec. If there aren't any that match o->pathspec,
968 * then make o->additional_path_headers be NULL.
971 if (!o->pathspec.nr) {
972 o->additional_path_headers = all_headers;
973 return;
976 o->additional_path_headers = xmalloc(sizeof(struct strmap));
977 strmap_init_with_options(o->additional_path_headers, NULL, 0);
978 strmap_for_each_entry(all_headers, &iter, entry) {
979 if (match_pathspec(the_repository->index, &o->pathspec,
980 entry->key, strlen(entry->key),
981 0 /* prefix */, NULL /* seen */,
982 0 /* is_dir */))
983 strmap_put(o->additional_path_headers,
984 entry->key, entry->value);
986 if (!strmap_get_size(o->additional_path_headers)) {
987 strmap_clear(o->additional_path_headers, 0);
988 FREE_AND_NULL(o->additional_path_headers);
992 static void cleanup_additional_headers(struct diff_options *o)
994 if (!o->pathspec.nr) {
995 o->additional_path_headers = NULL;
996 return;
998 if (!o->additional_path_headers)
999 return;
1001 strmap_clear(o->additional_path_headers, 0);
1002 FREE_AND_NULL(o->additional_path_headers);
1005 static int do_remerge_diff(struct rev_info *opt,
1006 struct commit_list *parents,
1007 struct object_id *oid)
1009 struct merge_options o;
1010 struct commit_list *bases = NULL;
1011 struct merge_result res = {0};
1012 struct pretty_print_context ctx = {0};
1013 struct commit *parent1 = parents->item;
1014 struct commit *parent2 = parents->next->item;
1015 struct strbuf parent1_desc = STRBUF_INIT;
1016 struct strbuf parent2_desc = STRBUF_INIT;
1018 /* Setup merge options */
1019 init_merge_options(&o, the_repository);
1020 o.show_rename_progress = 0;
1021 o.record_conflict_msgs_as_headers = 1;
1022 o.msg_header_prefix = "remerge";
1024 ctx.abbrev = DEFAULT_ABBREV;
1025 repo_format_commit_message(the_repository, parent1, "%h (%s)",
1026 &parent1_desc, &ctx);
1027 repo_format_commit_message(the_repository, parent2, "%h (%s)",
1028 &parent2_desc, &ctx);
1029 o.branch1 = parent1_desc.buf;
1030 o.branch2 = parent2_desc.buf;
1032 /* Parse the relevant commits and get the merge bases */
1033 parse_commit_or_die(parent1);
1034 parse_commit_or_die(parent2);
1035 if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)
1036 exit(128);
1038 /* Re-merge the parents */
1039 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1041 /* Show the diff */
1042 setup_additional_headers(&opt->diffopt, res.path_messages);
1043 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1044 log_tree_diff_flush(opt);
1046 /* Cleanup */
1047 free_commit_list(bases);
1048 cleanup_additional_headers(&opt->diffopt);
1049 strbuf_release(&parent1_desc);
1050 strbuf_release(&parent2_desc);
1051 merge_finalize(&o, &res);
1053 /* Clean up the contents of the temporary object directory */
1054 if (opt->remerge_objdir)
1055 tmp_objdir_discard_objects(opt->remerge_objdir);
1056 else
1057 BUG("did a remerge diff without remerge_objdir?!?");
1059 return !opt->loginfo;
1063 * Show the diff of a commit.
1065 * Return true if we printed any log info messages
1067 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1069 int showed_log;
1070 struct commit_list *parents;
1071 struct object_id *oid;
1072 int is_merge;
1073 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1075 if (!all_need_diff && !opt->merges_need_diff)
1076 return 0;
1078 parse_commit_or_die(commit);
1079 oid = get_commit_tree_oid(commit);
1081 parents = get_saved_parents(opt, commit);
1082 is_merge = parents && parents->next;
1083 if (!is_merge && !all_need_diff)
1084 return 0;
1086 /* Root commit? */
1087 if (!parents) {
1088 if (opt->show_root_diff) {
1089 diff_root_tree_oid(oid, "", &opt->diffopt);
1090 log_tree_diff_flush(opt);
1092 return !opt->loginfo;
1095 if (is_merge) {
1096 int octopus = (parents->next->next != NULL);
1098 if (opt->remerge_diff) {
1099 if (octopus) {
1100 show_log(opt);
1101 fprintf(opt->diffopt.file,
1102 "diff: warning: Skipping remerge-diff "
1103 "for octopus merges.\n");
1104 return 1;
1106 return do_remerge_diff(opt, parents, oid);
1108 if (opt->combine_merges)
1109 return do_diff_combined(opt, commit);
1110 if (opt->separate_merges) {
1111 if (!opt->first_parent_merges) {
1112 /* Show parent info for multiple diffs */
1113 log->parent = parents->item;
1115 } else
1116 return 0;
1119 showed_log = 0;
1120 for (;;) {
1121 struct commit *parent = parents->item;
1123 parse_commit_or_die(parent);
1124 diff_tree_oid(get_commit_tree_oid(parent),
1125 oid, "", &opt->diffopt);
1126 log_tree_diff_flush(opt);
1128 showed_log |= !opt->loginfo;
1130 /* Set up the log info for the next parent, if any.. */
1131 parents = parents->next;
1132 if (!parents || opt->first_parent_merges)
1133 break;
1134 log->parent = parents->item;
1135 opt->loginfo = log;
1137 return showed_log;
1140 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1142 struct log_info log;
1143 int shown;
1144 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1145 int no_free = opt->diffopt.no_free;
1147 log.commit = commit;
1148 log.parent = NULL;
1149 opt->loginfo = &log;
1150 opt->diffopt.no_free = 1;
1152 /* NEEDSWORK: no restoring of no_free? Why? */
1153 if (opt->line_level_traverse)
1154 return line_log_print(opt, commit);
1156 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1157 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1158 shown = log_tree_diff(opt, commit, &log);
1159 if (!shown && opt->loginfo && opt->always_show_header) {
1160 log.parent = NULL;
1161 show_log(opt);
1162 shown = 1;
1164 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1165 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1166 if (shown)
1167 show_diff_of_diff(opt);
1168 opt->loginfo = NULL;
1169 maybe_flush_or_die(opt->diffopt.file, "stdout");
1170 opt->diffopt.no_free = no_free;
1172 diff_free(&opt->diffopt);
1173 return shown;