notes.c: append separator instead of insert by pos
[alt-git.git] / log-tree.c
blobdbb088473e08e784ef5bc32a5f029ca627684b93
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"
26 #include "write-or-die.h"
28 static struct decoration name_decoration = { "object names" };
29 static int decoration_loaded;
30 static int decoration_flags;
32 static char decoration_colors[][COLOR_MAXLEN] = {
33 GIT_COLOR_RESET,
34 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
35 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
36 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
37 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
38 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
39 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
42 static const char *color_decorate_slots[] = {
43 [DECORATION_REF_LOCAL] = "branch",
44 [DECORATION_REF_REMOTE] = "remoteBranch",
45 [DECORATION_REF_TAG] = "tag",
46 [DECORATION_REF_STASH] = "stash",
47 [DECORATION_REF_HEAD] = "HEAD",
48 [DECORATION_GRAFTED] = "grafted",
51 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
53 if (want_color(decorate_use_color))
54 return decoration_colors[ix];
55 return "";
58 define_list_config_array(color_decorate_slots);
60 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
62 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
63 if (slot < 0)
64 return 0;
65 if (!value)
66 return config_error_nonbool(var);
67 return color_parse(value, decoration_colors[slot]);
71 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
72 * for showing the commit sha1, use the same check for --decorate
74 #define decorate_get_color_opt(o, ix) \
75 decorate_get_color((o)->use_color, ix)
77 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
79 struct name_decoration *res;
80 FLEX_ALLOC_STR(res, name, name);
81 res->type = type;
82 res->next = add_decoration(&name_decoration, obj, res);
85 const struct name_decoration *get_name_decoration(const struct object *obj)
87 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
88 return lookup_decoration(&name_decoration, obj);
91 static int match_ref_pattern(const char *refname,
92 const struct string_list_item *item)
94 int matched = 0;
95 if (!item->util) {
96 if (!wildmatch(item->string, refname, 0))
97 matched = 1;
98 } else {
99 const char *rest;
100 if (skip_prefix(refname, item->string, &rest) &&
101 (!*rest || *rest == '/'))
102 matched = 1;
104 return matched;
107 static int ref_filter_match(const char *refname,
108 const struct decoration_filter *filter)
110 struct string_list_item *item;
111 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
112 const struct string_list *include_patterns = filter->include_ref_pattern;
113 const struct string_list *exclude_patterns_config =
114 filter->exclude_ref_config_pattern;
116 if (exclude_patterns && exclude_patterns->nr) {
117 for_each_string_list_item(item, exclude_patterns) {
118 if (match_ref_pattern(refname, item))
119 return 0;
123 if (include_patterns && include_patterns->nr) {
124 for_each_string_list_item(item, include_patterns) {
125 if (match_ref_pattern(refname, item))
126 return 1;
128 return 0;
131 if (exclude_patterns_config && exclude_patterns_config->nr) {
132 for_each_string_list_item(item, exclude_patterns_config) {
133 if (match_ref_pattern(refname, item))
134 return 0;
138 return 1;
141 static int add_ref_decoration(const char *refname, const struct object_id *oid,
142 int flags UNUSED,
143 void *cb_data)
145 int i;
146 struct object *obj;
147 enum object_type objtype;
148 enum decoration_type deco_type = DECORATION_NONE;
149 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
150 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
152 if (filter && !ref_filter_match(refname, filter))
153 return 0;
155 if (starts_with(refname, git_replace_ref_base)) {
156 struct object_id original_oid;
157 if (!read_replace_refs)
158 return 0;
159 if (get_oid_hex(refname + strlen(git_replace_ref_base),
160 &original_oid)) {
161 warning("invalid replace ref %s", refname);
162 return 0;
164 obj = parse_object(the_repository, &original_oid);
165 if (obj)
166 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
167 return 0;
170 objtype = oid_object_info(the_repository, oid, NULL);
171 if (objtype < 0)
172 return 0;
173 obj = lookup_object_by_type(the_repository, oid, objtype);
175 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
176 struct ref_namespace_info *info = &ref_namespace[i];
178 if (!info->decoration)
179 continue;
180 if (info->exact) {
181 if (!strcmp(refname, info->ref)) {
182 deco_type = info->decoration;
183 break;
185 } else if (starts_with(refname, info->ref)) {
186 deco_type = info->decoration;
187 break;
191 add_name_decoration(deco_type, refname, obj);
192 while (obj->type == OBJ_TAG) {
193 if (!obj->parsed)
194 parse_object(the_repository, &obj->oid);
195 obj = ((struct tag *)obj)->tagged;
196 if (!obj)
197 break;
198 add_name_decoration(DECORATION_REF_TAG, refname, obj);
200 return 0;
203 static int add_graft_decoration(const struct commit_graft *graft,
204 void *cb_data UNUSED)
206 struct commit *commit = lookup_commit(the_repository, &graft->oid);
207 if (!commit)
208 return 0;
209 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
210 return 0;
213 void load_ref_decorations(struct decoration_filter *filter, int flags)
215 if (!decoration_loaded) {
216 if (filter) {
217 struct string_list_item *item;
218 for_each_string_list_item(item, filter->exclude_ref_pattern) {
219 normalize_glob_ref(item, NULL, item->string);
221 for_each_string_list_item(item, filter->include_ref_pattern) {
222 normalize_glob_ref(item, NULL, item->string);
224 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
225 normalize_glob_ref(item, NULL, item->string);
228 decoration_loaded = 1;
229 decoration_flags = flags;
230 for_each_ref(add_ref_decoration, filter);
231 head_ref(add_ref_decoration, filter);
232 for_each_commit_graft(add_graft_decoration, filter);
236 static void show_parents(struct commit *commit, int abbrev, FILE *file)
238 struct commit_list *p;
239 for (p = commit->parents; p ; p = p->next) {
240 struct commit *parent = p->item;
241 fprintf(file, " %s",
242 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
246 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
248 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
249 for ( ; p; p = p->next) {
250 fprintf(opt->diffopt.file, " %s",
251 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
256 * Do we have HEAD in the output, and also the branch it points at?
257 * If so, find that decoration entry for that current branch.
259 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
261 const struct name_decoration *list, *head = NULL;
262 const char *branch_name = NULL;
263 int rru_flags;
265 /* First find HEAD */
266 for (list = decoration; list; list = list->next)
267 if (list->type == DECORATION_REF_HEAD) {
268 head = list;
269 break;
271 if (!head)
272 return NULL;
274 /* Now resolve and find the matching current branch */
275 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
276 if (!branch_name || !(rru_flags & REF_ISSYMREF))
277 return NULL;
279 if (!starts_with(branch_name, "refs/"))
280 return NULL;
282 /* OK, do we have that ref in the list? */
283 for (list = decoration; list; list = list->next)
284 if ((list->type == DECORATION_REF_LOCAL) &&
285 !strcmp(branch_name, list->name)) {
286 return list;
289 return NULL;
292 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
294 if (decoration_flags == DECORATE_SHORT_REFS)
295 strbuf_addstr(sb, prettify_refname(decoration->name));
296 else
297 strbuf_addstr(sb, decoration->name);
301 * The caller makes sure there is no funny color before calling.
302 * format_decorations_extended makes sure the same after return.
304 void format_decorations_extended(struct strbuf *sb,
305 const struct commit *commit,
306 int use_color,
307 const char *prefix,
308 const char *separator,
309 const char *suffix)
311 const struct name_decoration *decoration;
312 const struct name_decoration *current_and_HEAD;
313 const char *color_commit =
314 diff_get_color(use_color, DIFF_COMMIT);
315 const char *color_reset =
316 decorate_get_color(use_color, DECORATION_NONE);
318 decoration = get_name_decoration(&commit->object);
319 if (!decoration)
320 return;
322 current_and_HEAD = current_pointed_by_HEAD(decoration);
323 while (decoration) {
325 * When both current and HEAD are there, only
326 * show HEAD->current where HEAD would have
327 * appeared, skipping the entry for current.
329 if (decoration != current_and_HEAD) {
330 strbuf_addstr(sb, color_commit);
331 strbuf_addstr(sb, prefix);
332 strbuf_addstr(sb, color_reset);
333 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
334 if (decoration->type == DECORATION_REF_TAG)
335 strbuf_addstr(sb, "tag: ");
337 show_name(sb, decoration);
339 if (current_and_HEAD &&
340 decoration->type == DECORATION_REF_HEAD) {
341 strbuf_addstr(sb, " -> ");
342 strbuf_addstr(sb, color_reset);
343 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
344 show_name(sb, current_and_HEAD);
346 strbuf_addstr(sb, color_reset);
348 prefix = separator;
350 decoration = decoration->next;
352 strbuf_addstr(sb, color_commit);
353 strbuf_addstr(sb, suffix);
354 strbuf_addstr(sb, color_reset);
357 void show_decorations(struct rev_info *opt, struct commit *commit)
359 struct strbuf sb = STRBUF_INIT;
361 if (opt->sources) {
362 char **slot = revision_sources_peek(opt->sources, commit);
364 if (slot && *slot)
365 fprintf(opt->diffopt.file, "\t%s", *slot);
367 if (!opt->show_decorations)
368 return;
369 format_decorations(&sb, commit, opt->diffopt.use_color);
370 fputs(sb.buf, opt->diffopt.file);
371 strbuf_release(&sb);
374 static unsigned int digits_in_number(unsigned int number)
376 unsigned int i = 10, result = 1;
377 while (i <= number) {
378 i *= 10;
379 result++;
381 return result;
384 void fmt_output_subject(struct strbuf *filename,
385 const char *subject,
386 struct rev_info *info)
388 const char *suffix = info->patch_suffix;
389 int nr = info->nr;
390 int start_len = filename->len;
391 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
393 if (info->reroll_count) {
394 struct strbuf temp = STRBUF_INIT;
396 strbuf_addf(&temp, "v%s", info->reroll_count);
397 format_sanitized_subject(filename, temp.buf, temp.len);
398 strbuf_addstr(filename, "-");
399 strbuf_release(&temp);
401 strbuf_addf(filename, "%04d-%s", nr, subject);
403 if (max_len < filename->len)
404 strbuf_setlen(filename, max_len);
405 strbuf_addstr(filename, suffix);
408 void fmt_output_commit(struct strbuf *filename,
409 struct commit *commit,
410 struct rev_info *info)
412 struct pretty_print_context ctx = {0};
413 struct strbuf subject = STRBUF_INIT;
415 repo_format_commit_message(the_repository, commit, "%f", &subject,
416 &ctx);
417 fmt_output_subject(filename, subject.buf, info);
418 strbuf_release(&subject);
421 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
423 if (opt->total > 0) {
424 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
425 opt->subject_prefix,
426 *opt->subject_prefix ? " " : "",
427 digits_in_number(opt->total),
428 opt->nr, opt->total);
429 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
430 strbuf_addf(sb, "Subject: [%s] ",
431 opt->subject_prefix);
432 } else {
433 strbuf_addstr(sb, "Subject: ");
437 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
438 const char **extra_headers_p,
439 int *need_8bit_cte_p,
440 int maybe_multipart)
442 const char *extra_headers = opt->extra_headers;
443 const char *name = oid_to_hex(opt->zero_commit ?
444 null_oid() : &commit->object.oid);
446 *need_8bit_cte_p = 0; /* unknown */
448 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
449 graph_show_oneline(opt->graph);
450 if (opt->message_id) {
451 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
452 graph_show_oneline(opt->graph);
454 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
455 int i, n;
456 n = opt->ref_message_ids->nr;
457 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
458 for (i = 0; i < n; i++)
459 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
460 opt->ref_message_ids->items[i].string);
461 graph_show_oneline(opt->graph);
463 if (opt->mime_boundary && maybe_multipart) {
464 static struct strbuf subject_buffer = STRBUF_INIT;
465 static struct strbuf buffer = STRBUF_INIT;
466 struct strbuf filename = STRBUF_INIT;
467 *need_8bit_cte_p = -1; /* NEVER */
469 strbuf_reset(&subject_buffer);
470 strbuf_reset(&buffer);
472 strbuf_addf(&subject_buffer,
473 "%s"
474 "MIME-Version: 1.0\n"
475 "Content-Type: multipart/mixed;"
476 " boundary=\"%s%s\"\n"
477 "\n"
478 "This is a multi-part message in MIME "
479 "format.\n"
480 "--%s%s\n"
481 "Content-Type: text/plain; "
482 "charset=UTF-8; format=fixed\n"
483 "Content-Transfer-Encoding: 8bit\n\n",
484 extra_headers ? extra_headers : "",
485 mime_boundary_leader, opt->mime_boundary,
486 mime_boundary_leader, opt->mime_boundary);
487 extra_headers = subject_buffer.buf;
489 if (opt->numbered_files)
490 strbuf_addf(&filename, "%d", opt->nr);
491 else
492 fmt_output_commit(&filename, commit, opt);
493 strbuf_addf(&buffer,
494 "\n--%s%s\n"
495 "Content-Type: text/x-patch;"
496 " name=\"%s\"\n"
497 "Content-Transfer-Encoding: 8bit\n"
498 "Content-Disposition: %s;"
499 " filename=\"%s\"\n\n",
500 mime_boundary_leader, opt->mime_boundary,
501 filename.buf,
502 opt->no_inline ? "attachment" : "inline",
503 filename.buf);
504 opt->diffopt.stat_sep = buffer.buf;
505 strbuf_release(&filename);
507 *extra_headers_p = extra_headers;
510 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
512 const char *color, *reset, *eol;
514 color = diff_get_color_opt(&opt->diffopt,
515 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
516 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
517 while (*bol) {
518 eol = strchrnul(bol, '\n');
519 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
520 *eol ? "\n" : "");
521 graph_show_oneline(opt->graph);
522 bol = (*eol) ? (eol + 1) : eol;
526 static void show_signature(struct rev_info *opt, struct commit *commit)
528 struct strbuf payload = STRBUF_INIT;
529 struct strbuf signature = STRBUF_INIT;
530 struct signature_check sigc = { 0 };
531 int status;
533 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
534 goto out;
536 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
537 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
538 status = check_signature(&sigc, signature.buf, signature.len);
539 if (status && !sigc.output)
540 show_sig_lines(opt, status, "No signature\n");
541 else
542 show_sig_lines(opt, status, sigc.output);
543 signature_check_clear(&sigc);
545 out:
546 strbuf_release(&payload);
547 strbuf_release(&signature);
550 static int which_parent(const struct object_id *oid, const struct commit *commit)
552 int nth;
553 const struct commit_list *parent;
555 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
556 if (oideq(&parent->item->object.oid, oid))
557 return nth;
558 nth++;
560 return -1;
563 static int is_common_merge(const struct commit *commit)
565 return (commit->parents
566 && commit->parents->next
567 && !commit->parents->next->next);
570 static int show_one_mergetag(struct commit *commit,
571 struct commit_extra_header *extra,
572 void *data)
574 struct rev_info *opt = (struct rev_info *)data;
575 struct object_id oid;
576 struct tag *tag;
577 struct strbuf verify_message;
578 struct signature_check sigc = { 0 };
579 int status, nth;
580 struct strbuf payload = STRBUF_INIT;
581 struct strbuf signature = STRBUF_INIT;
583 hash_object_file(the_hash_algo, extra->value, extra->len,
584 OBJ_TAG, &oid);
585 tag = lookup_tag(the_repository, &oid);
586 if (!tag)
587 return -1; /* error message already given */
589 strbuf_init(&verify_message, 256);
590 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
591 strbuf_addstr(&verify_message, "malformed mergetag\n");
592 else if (is_common_merge(commit) &&
593 oideq(&tag->tagged->oid,
594 &commit->parents->next->item->object.oid))
595 strbuf_addf(&verify_message,
596 "merged tag '%s'\n", tag->tag);
597 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
598 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
599 tag->tag, oid_to_hex(&tag->tagged->oid));
600 else
601 strbuf_addf(&verify_message,
602 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
604 status = -1;
605 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
606 /* could have a good signature */
607 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
608 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
609 status = check_signature(&sigc, signature.buf, signature.len);
610 if (sigc.output)
611 strbuf_addstr(&verify_message, sigc.output);
612 else
613 strbuf_addstr(&verify_message, "No signature\n");
614 signature_check_clear(&sigc);
615 /* otherwise we couldn't verify, which is shown as bad */
618 show_sig_lines(opt, status, verify_message.buf);
619 strbuf_release(&verify_message);
620 strbuf_release(&payload);
621 strbuf_release(&signature);
622 return 0;
625 static int show_mergetag(struct rev_info *opt, struct commit *commit)
627 return for_each_mergetag(show_one_mergetag, commit, opt);
630 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
632 const char *x = opt->shown_dashes ? "\n" : "---\n";
633 if (sb)
634 strbuf_addstr(sb, x);
635 else
636 fputs(x, opt->diffopt.file);
637 opt->shown_dashes = 1;
640 void show_log(struct rev_info *opt)
642 struct strbuf msgbuf = STRBUF_INIT;
643 struct log_info *log = opt->loginfo;
644 struct commit *commit = log->commit, *parent = log->parent;
645 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
646 const char *extra_headers = opt->extra_headers;
647 struct pretty_print_context ctx = {0};
649 opt->loginfo = NULL;
650 if (!opt->verbose_header) {
651 graph_show_commit(opt->graph);
653 if (!opt->graph)
654 put_revision_mark(opt, commit);
655 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
656 opt->diffopt.file);
657 if (opt->print_parents)
658 show_parents(commit, abbrev_commit, opt->diffopt.file);
659 if (opt->children.name)
660 show_children(opt, commit, abbrev_commit);
661 show_decorations(opt, commit);
662 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
663 putc('\n', opt->diffopt.file);
664 graph_show_remainder(opt->graph);
666 putc(opt->diffopt.line_termination, opt->diffopt.file);
667 return;
671 * If use_terminator is set, we already handled any record termination
672 * at the end of the last record.
673 * Otherwise, add a diffopt.line_termination character before all
674 * entries but the first. (IOW, as a separator between entries)
676 if (opt->shown_one && !opt->use_terminator) {
678 * If entries are separated by a newline, the output
679 * should look human-readable. If the last entry ended
680 * with a newline, print the graph output before this
681 * newline. Otherwise it will end up as a completely blank
682 * line and will look like a gap in the graph.
684 * If the entry separator is not a newline, the output is
685 * primarily intended for programmatic consumption, and we
686 * never want the extra graph output before the entry
687 * separator.
689 if (opt->diffopt.line_termination == '\n' &&
690 !opt->missing_newline)
691 graph_show_padding(opt->graph);
692 putc(opt->diffopt.line_termination, opt->diffopt.file);
694 opt->shown_one = 1;
697 * If the history graph was requested,
698 * print the graph, up to this commit's line
700 graph_show_commit(opt->graph);
703 * Print header line of header..
706 if (cmit_fmt_is_mail(opt->commit_format)) {
707 log_write_email_headers(opt, commit, &extra_headers,
708 &ctx.need_8bit_cte, 1);
709 ctx.rev = opt;
710 ctx.print_email_subject = 1;
711 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
712 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
713 if (opt->commit_format != CMIT_FMT_ONELINE)
714 fputs("commit ", opt->diffopt.file);
716 if (!opt->graph)
717 put_revision_mark(opt, commit);
718 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
719 abbrev_commit),
720 opt->diffopt.file);
721 if (opt->print_parents)
722 show_parents(commit, abbrev_commit, opt->diffopt.file);
723 if (opt->children.name)
724 show_children(opt, commit, abbrev_commit);
725 if (parent)
726 fprintf(opt->diffopt.file, " (from %s)",
727 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
728 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
729 show_decorations(opt, commit);
730 if (opt->commit_format == CMIT_FMT_ONELINE) {
731 putc(' ', opt->diffopt.file);
732 } else {
733 putc('\n', opt->diffopt.file);
734 graph_show_oneline(opt->graph);
736 if (opt->reflog_info) {
738 * setup_revisions() ensures that opt->reflog_info
739 * and opt->graph cannot both be set,
740 * so we don't need to worry about printing the
741 * graph info here.
743 show_reflog_message(opt->reflog_info,
744 opt->commit_format == CMIT_FMT_ONELINE,
745 &opt->date_mode,
746 opt->date_mode_explicit);
747 if (opt->commit_format == CMIT_FMT_ONELINE)
748 return;
752 if (opt->show_signature) {
753 show_signature(opt, commit);
754 show_mergetag(opt, commit);
757 if (opt->show_notes) {
758 int raw;
759 struct strbuf notebuf = STRBUF_INIT;
761 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
762 format_display_notes(&commit->object.oid, &notebuf,
763 get_log_output_encoding(), raw);
764 ctx.notes_message = strbuf_detach(&notebuf, NULL);
768 * And then the pretty-printed message itself
770 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
771 ctx.need_8bit_cte =
772 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
773 ctx.date_mode = opt->date_mode;
774 ctx.date_mode_explicit = opt->date_mode_explicit;
775 ctx.abbrev = opt->diffopt.abbrev;
776 ctx.after_subject = extra_headers;
777 ctx.preserve_subject = opt->preserve_subject;
778 ctx.encode_email_headers = opt->encode_email_headers;
779 ctx.reflog_info = opt->reflog_info;
780 ctx.fmt = opt->commit_format;
781 ctx.mailmap = opt->mailmap;
782 ctx.color = opt->diffopt.use_color;
783 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
784 ctx.output_encoding = get_log_output_encoding();
785 ctx.rev = opt;
786 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
787 ctx.from_ident = &opt->from_ident;
788 if (opt->graph)
789 ctx.graph_width = graph_width(opt->graph);
790 pretty_print_commit(&ctx, commit, &msgbuf);
792 if (opt->add_signoff)
793 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
795 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
796 ctx.notes_message && *ctx.notes_message) {
797 if (cmit_fmt_is_mail(ctx.fmt))
798 next_commentary_block(opt, &msgbuf);
799 strbuf_addstr(&msgbuf, ctx.notes_message);
802 if (opt->show_log_size) {
803 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
804 graph_show_oneline(opt->graph);
808 * Set opt->missing_newline if msgbuf doesn't
809 * end in a newline (including if it is empty)
811 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
812 opt->missing_newline = 1;
813 else
814 opt->missing_newline = 0;
816 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
817 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
818 if (!opt->missing_newline)
819 graph_show_padding(opt->graph);
820 putc(opt->diffopt.line_termination, opt->diffopt.file);
823 strbuf_release(&msgbuf);
824 free(ctx.notes_message);
826 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
827 struct diff_queue_struct dq;
829 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
830 DIFF_QUEUE_CLEAR(&diff_queued_diff);
832 next_commentary_block(opt, NULL);
833 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
834 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
835 &opt->diffopt);
837 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
840 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
841 struct diff_queue_struct dq;
842 struct diff_options opts;
843 struct range_diff_options range_diff_opts = {
844 .creation_factor = opt->creation_factor,
845 .dual_color = 1,
846 .diffopt = &opts
849 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
850 DIFF_QUEUE_CLEAR(&diff_queued_diff);
852 next_commentary_block(opt, NULL);
853 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
855 * Pass minimum required diff-options to range-diff; others
856 * can be added later if deemed desirable.
858 repo_diff_setup(the_repository, &opts);
859 opts.file = opt->diffopt.file;
860 opts.use_color = opt->diffopt.use_color;
861 diff_setup_done(&opts);
862 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
864 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
868 int log_tree_diff_flush(struct rev_info *opt)
870 opt->shown_dashes = 0;
871 diffcore_std(&opt->diffopt);
873 if (diff_queue_is_empty(&opt->diffopt)) {
874 int saved_fmt = opt->diffopt.output_format;
875 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
876 diff_flush(&opt->diffopt);
877 opt->diffopt.output_format = saved_fmt;
878 return 0;
881 if (opt->loginfo && !opt->no_commit_id) {
882 show_log(opt);
883 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
884 opt->verbose_header &&
885 opt->commit_format != CMIT_FMT_ONELINE &&
886 !commit_format_is_empty(opt->commit_format)) {
888 * When showing a verbose header (i.e. log message),
889 * and not in --pretty=oneline format, we would want
890 * an extra newline between the end of log and the
891 * diff/diffstat output for readability.
893 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
894 if (opt->diffopt.output_prefix) {
895 struct strbuf *msg = NULL;
896 msg = opt->diffopt.output_prefix(&opt->diffopt,
897 opt->diffopt.output_prefix_data);
898 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
902 * We may have shown three-dashes line early
903 * between generated commentary (notes, etc.)
904 * and the log message, in which case we only
905 * want a blank line after the commentary
906 * without (an extra) three-dashes line.
907 * Otherwise, we show the three-dashes line if
908 * we are showing the patch with diffstat, but
909 * in that case, there is no extra blank line
910 * after the three-dashes line.
912 if (!opt->shown_dashes &&
913 (pch & opt->diffopt.output_format) == pch)
914 fprintf(opt->diffopt.file, "---");
915 putc('\n', opt->diffopt.file);
918 diff_flush(&opt->diffopt);
919 return 1;
922 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
924 diff_tree_combined_merge(commit, opt);
925 return !opt->loginfo;
928 static void setup_additional_headers(struct diff_options *o,
929 struct strmap *all_headers)
931 struct hashmap_iter iter;
932 struct strmap_entry *entry;
935 * Make o->additional_path_headers contain the subset of all_headers
936 * that match o->pathspec. If there aren't any that match o->pathspec,
937 * then make o->additional_path_headers be NULL.
940 if (!o->pathspec.nr) {
941 o->additional_path_headers = all_headers;
942 return;
945 o->additional_path_headers = xmalloc(sizeof(struct strmap));
946 strmap_init_with_options(o->additional_path_headers, NULL, 0);
947 strmap_for_each_entry(all_headers, &iter, entry) {
948 if (match_pathspec(the_repository->index, &o->pathspec,
949 entry->key, strlen(entry->key),
950 0 /* prefix */, NULL /* seen */,
951 0 /* is_dir */))
952 strmap_put(o->additional_path_headers,
953 entry->key, entry->value);
955 if (!strmap_get_size(o->additional_path_headers)) {
956 strmap_clear(o->additional_path_headers, 0);
957 FREE_AND_NULL(o->additional_path_headers);
961 static void cleanup_additional_headers(struct diff_options *o)
963 if (!o->pathspec.nr) {
964 o->additional_path_headers = NULL;
965 return;
967 if (!o->additional_path_headers)
968 return;
970 strmap_clear(o->additional_path_headers, 0);
971 FREE_AND_NULL(o->additional_path_headers);
974 static int do_remerge_diff(struct rev_info *opt,
975 struct commit_list *parents,
976 struct object_id *oid)
978 struct merge_options o;
979 struct commit_list *bases;
980 struct merge_result res = {0};
981 struct pretty_print_context ctx = {0};
982 struct commit *parent1 = parents->item;
983 struct commit *parent2 = parents->next->item;
984 struct strbuf parent1_desc = STRBUF_INIT;
985 struct strbuf parent2_desc = STRBUF_INIT;
987 /* Setup merge options */
988 init_merge_options(&o, the_repository);
989 o.show_rename_progress = 0;
990 o.record_conflict_msgs_as_headers = 1;
991 o.msg_header_prefix = "remerge";
993 ctx.abbrev = DEFAULT_ABBREV;
994 repo_format_commit_message(the_repository, parent1, "%h (%s)",
995 &parent1_desc, &ctx);
996 repo_format_commit_message(the_repository, parent2, "%h (%s)",
997 &parent2_desc, &ctx);
998 o.branch1 = parent1_desc.buf;
999 o.branch2 = parent2_desc.buf;
1001 /* Parse the relevant commits and get the merge bases */
1002 parse_commit_or_die(parent1);
1003 parse_commit_or_die(parent2);
1004 bases = repo_get_merge_bases(the_repository, parent1, parent2);
1006 /* Re-merge the parents */
1007 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1009 /* Show the diff */
1010 setup_additional_headers(&opt->diffopt, res.path_messages);
1011 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1012 log_tree_diff_flush(opt);
1014 /* Cleanup */
1015 cleanup_additional_headers(&opt->diffopt);
1016 strbuf_release(&parent1_desc);
1017 strbuf_release(&parent2_desc);
1018 merge_finalize(&o, &res);
1020 /* Clean up the contents of the temporary object directory */
1021 if (opt->remerge_objdir)
1022 tmp_objdir_discard_objects(opt->remerge_objdir);
1023 else
1024 BUG("did a remerge diff without remerge_objdir?!?");
1026 return !opt->loginfo;
1030 * Show the diff of a commit.
1032 * Return true if we printed any log info messages
1034 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1036 int showed_log;
1037 struct commit_list *parents;
1038 struct object_id *oid;
1039 int is_merge;
1040 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1042 if (!all_need_diff && !opt->merges_need_diff)
1043 return 0;
1045 parse_commit_or_die(commit);
1046 oid = get_commit_tree_oid(commit);
1048 parents = get_saved_parents(opt, commit);
1049 is_merge = parents && parents->next;
1050 if (!is_merge && !all_need_diff)
1051 return 0;
1053 /* Root commit? */
1054 if (!parents) {
1055 if (opt->show_root_diff) {
1056 diff_root_tree_oid(oid, "", &opt->diffopt);
1057 log_tree_diff_flush(opt);
1059 return !opt->loginfo;
1062 if (is_merge) {
1063 int octopus = (parents->next->next != NULL);
1065 if (opt->remerge_diff) {
1066 if (octopus) {
1067 show_log(opt);
1068 fprintf(opt->diffopt.file,
1069 "diff: warning: Skipping remerge-diff "
1070 "for octopus merges.\n");
1071 return 1;
1073 return do_remerge_diff(opt, parents, oid);
1075 if (opt->combine_merges)
1076 return do_diff_combined(opt, commit);
1077 if (opt->separate_merges) {
1078 if (!opt->first_parent_merges) {
1079 /* Show parent info for multiple diffs */
1080 log->parent = parents->item;
1082 } else
1083 return 0;
1086 showed_log = 0;
1087 for (;;) {
1088 struct commit *parent = parents->item;
1090 parse_commit_or_die(parent);
1091 diff_tree_oid(get_commit_tree_oid(parent),
1092 oid, "", &opt->diffopt);
1093 log_tree_diff_flush(opt);
1095 showed_log |= !opt->loginfo;
1097 /* Set up the log info for the next parent, if any.. */
1098 parents = parents->next;
1099 if (!parents || opt->first_parent_merges)
1100 break;
1101 log->parent = parents->item;
1102 opt->loginfo = log;
1104 return showed_log;
1107 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1109 struct log_info log;
1110 int shown;
1111 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1112 int no_free = opt->diffopt.no_free;
1114 log.commit = commit;
1115 log.parent = NULL;
1116 opt->loginfo = &log;
1117 opt->diffopt.no_free = 1;
1119 /* NEEDSWORK: no restoring of no_free? Why? */
1120 if (opt->line_level_traverse)
1121 return line_log_print(opt, commit);
1123 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1124 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1125 shown = log_tree_diff(opt, commit, &log);
1126 if (!shown && opt->loginfo && opt->always_show_header) {
1127 log.parent = NULL;
1128 show_log(opt);
1129 shown = 1;
1131 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1132 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1133 opt->loginfo = NULL;
1134 maybe_flush_or_die(opt->diffopt.file, "stdout");
1135 opt->diffopt.no_free = no_free;
1136 diff_free(&opt->diffopt);
1137 return shown;