Merge branch 'bc/gc-crontab-fix'
[alt-git.git] / log-tree.c
blob3e8c70ddcf441847a900eff8057d013d5a414d62
1 #include "cache.h"
2 #include "commit-reach.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "object-store.h"
6 #include "repository.h"
7 #include "tmp-objdir.h"
8 #include "commit.h"
9 #include "tag.h"
10 #include "graph.h"
11 #include "log-tree.h"
12 #include "merge-ort.h"
13 #include "reflog-walk.h"
14 #include "refs.h"
15 #include "string-list.h"
16 #include "color.h"
17 #include "gpg-interface.h"
18 #include "sequencer.h"
19 #include "line-log.h"
20 #include "help.h"
21 #include "range-diff.h"
22 #include "strmap.h"
24 static struct decoration name_decoration = { "object names" };
25 static int decoration_loaded;
26 static int decoration_flags;
28 static char decoration_colors[][COLOR_MAXLEN] = {
29 GIT_COLOR_RESET,
30 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
31 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
32 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
33 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
34 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
35 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
38 static const char *color_decorate_slots[] = {
39 [DECORATION_REF_LOCAL] = "branch",
40 [DECORATION_REF_REMOTE] = "remoteBranch",
41 [DECORATION_REF_TAG] = "tag",
42 [DECORATION_REF_STASH] = "stash",
43 [DECORATION_REF_HEAD] = "HEAD",
44 [DECORATION_GRAFTED] = "grafted",
47 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
49 if (want_color(decorate_use_color))
50 return decoration_colors[ix];
51 return "";
54 define_list_config_array(color_decorate_slots);
56 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
58 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
59 if (slot < 0)
60 return 0;
61 if (!value)
62 return config_error_nonbool(var);
63 return color_parse(value, decoration_colors[slot]);
67 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
68 * for showing the commit sha1, use the same check for --decorate
70 #define decorate_get_color_opt(o, ix) \
71 decorate_get_color((o)->use_color, ix)
73 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
75 struct name_decoration *res;
76 FLEX_ALLOC_STR(res, name, name);
77 res->type = type;
78 res->next = add_decoration(&name_decoration, obj, res);
81 const struct name_decoration *get_name_decoration(const struct object *obj)
83 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
84 return lookup_decoration(&name_decoration, obj);
87 static int match_ref_pattern(const char *refname,
88 const struct string_list_item *item)
90 int matched = 0;
91 if (!item->util) {
92 if (!wildmatch(item->string, refname, 0))
93 matched = 1;
94 } else {
95 const char *rest;
96 if (skip_prefix(refname, item->string, &rest) &&
97 (!*rest || *rest == '/'))
98 matched = 1;
100 return matched;
103 static int ref_filter_match(const char *refname,
104 const struct decoration_filter *filter)
106 struct string_list_item *item;
107 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
108 const struct string_list *include_patterns = filter->include_ref_pattern;
109 const struct string_list *exclude_patterns_config =
110 filter->exclude_ref_config_pattern;
112 if (exclude_patterns && exclude_patterns->nr) {
113 for_each_string_list_item(item, exclude_patterns) {
114 if (match_ref_pattern(refname, item))
115 return 0;
119 if (include_patterns && include_patterns->nr) {
120 for_each_string_list_item(item, include_patterns) {
121 if (match_ref_pattern(refname, item))
122 return 1;
124 return 0;
127 if (exclude_patterns_config && exclude_patterns_config->nr) {
128 for_each_string_list_item(item, exclude_patterns_config) {
129 if (match_ref_pattern(refname, item))
130 return 0;
134 return 1;
137 static int add_ref_decoration(const char *refname, const struct object_id *oid,
138 int flags, void *cb_data)
140 int i;
141 struct object *obj;
142 enum object_type objtype;
143 enum decoration_type deco_type = DECORATION_NONE;
144 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
145 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
147 if (filter && !ref_filter_match(refname, filter))
148 return 0;
150 if (starts_with(refname, git_replace_ref_base)) {
151 struct object_id original_oid;
152 if (!read_replace_refs)
153 return 0;
154 if (get_oid_hex(refname + strlen(git_replace_ref_base),
155 &original_oid)) {
156 warning("invalid replace ref %s", refname);
157 return 0;
159 obj = parse_object(the_repository, &original_oid);
160 if (obj)
161 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
162 return 0;
165 objtype = oid_object_info(the_repository, oid, NULL);
166 if (objtype < 0)
167 return 0;
168 obj = lookup_object_by_type(the_repository, oid, objtype);
170 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
171 struct ref_namespace_info *info = &ref_namespace[i];
173 if (!info->decoration)
174 continue;
175 if (info->exact) {
176 if (!strcmp(refname, info->ref)) {
177 deco_type = info->decoration;
178 break;
180 } else if (starts_with(refname, info->ref)) {
181 deco_type = info->decoration;
182 break;
186 add_name_decoration(deco_type, refname, obj);
187 while (obj->type == OBJ_TAG) {
188 if (!obj->parsed)
189 parse_object(the_repository, &obj->oid);
190 obj = ((struct tag *)obj)->tagged;
191 if (!obj)
192 break;
193 add_name_decoration(DECORATION_REF_TAG, refname, obj);
195 return 0;
198 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
200 struct commit *commit = lookup_commit(the_repository, &graft->oid);
201 if (!commit)
202 return 0;
203 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
204 return 0;
207 void load_ref_decorations(struct decoration_filter *filter, int flags)
209 if (!decoration_loaded) {
210 if (filter) {
211 struct string_list_item *item;
212 for_each_string_list_item(item, filter->exclude_ref_pattern) {
213 normalize_glob_ref(item, NULL, item->string);
215 for_each_string_list_item(item, filter->include_ref_pattern) {
216 normalize_glob_ref(item, NULL, item->string);
218 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
219 normalize_glob_ref(item, NULL, item->string);
222 decoration_loaded = 1;
223 decoration_flags = flags;
224 for_each_ref(add_ref_decoration, filter);
225 head_ref(add_ref_decoration, filter);
226 for_each_commit_graft(add_graft_decoration, filter);
230 static void show_parents(struct commit *commit, int abbrev, FILE *file)
232 struct commit_list *p;
233 for (p = commit->parents; p ; p = p->next) {
234 struct commit *parent = p->item;
235 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
239 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
241 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
242 for ( ; p; p = p->next) {
243 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
248 * Do we have HEAD in the output, and also the branch it points at?
249 * If so, find that decoration entry for that current branch.
251 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
253 const struct name_decoration *list, *head = NULL;
254 const char *branch_name = NULL;
255 int rru_flags;
257 /* First find HEAD */
258 for (list = decoration; list; list = list->next)
259 if (list->type == DECORATION_REF_HEAD) {
260 head = list;
261 break;
263 if (!head)
264 return NULL;
266 /* Now resolve and find the matching current branch */
267 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
268 if (!branch_name || !(rru_flags & REF_ISSYMREF))
269 return NULL;
271 if (!starts_with(branch_name, "refs/"))
272 return NULL;
274 /* OK, do we have that ref in the list? */
275 for (list = decoration; list; list = list->next)
276 if ((list->type == DECORATION_REF_LOCAL) &&
277 !strcmp(branch_name, list->name)) {
278 return list;
281 return NULL;
284 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
286 if (decoration_flags == DECORATE_SHORT_REFS)
287 strbuf_addstr(sb, prettify_refname(decoration->name));
288 else
289 strbuf_addstr(sb, decoration->name);
293 * The caller makes sure there is no funny color before calling.
294 * format_decorations_extended makes sure the same after return.
296 void format_decorations_extended(struct strbuf *sb,
297 const struct commit *commit,
298 int use_color,
299 const char *prefix,
300 const char *separator,
301 const char *suffix)
303 const struct name_decoration *decoration;
304 const struct name_decoration *current_and_HEAD;
305 const char *color_commit =
306 diff_get_color(use_color, DIFF_COMMIT);
307 const char *color_reset =
308 decorate_get_color(use_color, DECORATION_NONE);
310 decoration = get_name_decoration(&commit->object);
311 if (!decoration)
312 return;
314 current_and_HEAD = current_pointed_by_HEAD(decoration);
315 while (decoration) {
317 * When both current and HEAD are there, only
318 * show HEAD->current where HEAD would have
319 * appeared, skipping the entry for current.
321 if (decoration != current_and_HEAD) {
322 strbuf_addstr(sb, color_commit);
323 strbuf_addstr(sb, prefix);
324 strbuf_addstr(sb, color_reset);
325 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
326 if (decoration->type == DECORATION_REF_TAG)
327 strbuf_addstr(sb, "tag: ");
329 show_name(sb, decoration);
331 if (current_and_HEAD &&
332 decoration->type == DECORATION_REF_HEAD) {
333 strbuf_addstr(sb, " -> ");
334 strbuf_addstr(sb, color_reset);
335 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
336 show_name(sb, current_and_HEAD);
338 strbuf_addstr(sb, color_reset);
340 prefix = separator;
342 decoration = decoration->next;
344 strbuf_addstr(sb, color_commit);
345 strbuf_addstr(sb, suffix);
346 strbuf_addstr(sb, color_reset);
349 void show_decorations(struct rev_info *opt, struct commit *commit)
351 struct strbuf sb = STRBUF_INIT;
353 if (opt->sources) {
354 char **slot = revision_sources_peek(opt->sources, commit);
356 if (slot && *slot)
357 fprintf(opt->diffopt.file, "\t%s", *slot);
359 if (!opt->show_decorations)
360 return;
361 format_decorations(&sb, commit, opt->diffopt.use_color);
362 fputs(sb.buf, opt->diffopt.file);
363 strbuf_release(&sb);
366 static unsigned int digits_in_number(unsigned int number)
368 unsigned int i = 10, result = 1;
369 while (i <= number) {
370 i *= 10;
371 result++;
373 return result;
376 void fmt_output_subject(struct strbuf *filename,
377 const char *subject,
378 struct rev_info *info)
380 const char *suffix = info->patch_suffix;
381 int nr = info->nr;
382 int start_len = filename->len;
383 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
385 if (info->reroll_count) {
386 struct strbuf temp = STRBUF_INIT;
388 strbuf_addf(&temp, "v%s", info->reroll_count);
389 format_sanitized_subject(filename, temp.buf, temp.len);
390 strbuf_addstr(filename, "-");
391 strbuf_release(&temp);
393 strbuf_addf(filename, "%04d-%s", nr, subject);
395 if (max_len < filename->len)
396 strbuf_setlen(filename, max_len);
397 strbuf_addstr(filename, suffix);
400 void fmt_output_commit(struct strbuf *filename,
401 struct commit *commit,
402 struct rev_info *info)
404 struct pretty_print_context ctx = {0};
405 struct strbuf subject = STRBUF_INIT;
407 format_commit_message(commit, "%f", &subject, &ctx);
408 fmt_output_subject(filename, subject.buf, info);
409 strbuf_release(&subject);
412 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
414 if (opt->total > 0) {
415 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
416 opt->subject_prefix,
417 *opt->subject_prefix ? " " : "",
418 digits_in_number(opt->total),
419 opt->nr, opt->total);
420 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
421 strbuf_addf(sb, "Subject: [%s] ",
422 opt->subject_prefix);
423 } else {
424 strbuf_addstr(sb, "Subject: ");
428 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
429 const char **extra_headers_p,
430 int *need_8bit_cte_p,
431 int maybe_multipart)
433 const char *extra_headers = opt->extra_headers;
434 const char *name = oid_to_hex(opt->zero_commit ?
435 null_oid() : &commit->object.oid);
437 *need_8bit_cte_p = 0; /* unknown */
439 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
440 graph_show_oneline(opt->graph);
441 if (opt->message_id) {
442 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
443 graph_show_oneline(opt->graph);
445 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
446 int i, n;
447 n = opt->ref_message_ids->nr;
448 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
449 for (i = 0; i < n; i++)
450 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
451 opt->ref_message_ids->items[i].string);
452 graph_show_oneline(opt->graph);
454 if (opt->mime_boundary && maybe_multipart) {
455 static struct strbuf subject_buffer = STRBUF_INIT;
456 static struct strbuf buffer = STRBUF_INIT;
457 struct strbuf filename = STRBUF_INIT;
458 *need_8bit_cte_p = -1; /* NEVER */
460 strbuf_reset(&subject_buffer);
461 strbuf_reset(&buffer);
463 strbuf_addf(&subject_buffer,
464 "%s"
465 "MIME-Version: 1.0\n"
466 "Content-Type: multipart/mixed;"
467 " boundary=\"%s%s\"\n"
468 "\n"
469 "This is a multi-part message in MIME "
470 "format.\n"
471 "--%s%s\n"
472 "Content-Type: text/plain; "
473 "charset=UTF-8; format=fixed\n"
474 "Content-Transfer-Encoding: 8bit\n\n",
475 extra_headers ? extra_headers : "",
476 mime_boundary_leader, opt->mime_boundary,
477 mime_boundary_leader, opt->mime_boundary);
478 extra_headers = subject_buffer.buf;
480 if (opt->numbered_files)
481 strbuf_addf(&filename, "%d", opt->nr);
482 else
483 fmt_output_commit(&filename, commit, opt);
484 strbuf_addf(&buffer,
485 "\n--%s%s\n"
486 "Content-Type: text/x-patch;"
487 " name=\"%s\"\n"
488 "Content-Transfer-Encoding: 8bit\n"
489 "Content-Disposition: %s;"
490 " filename=\"%s\"\n\n",
491 mime_boundary_leader, opt->mime_boundary,
492 filename.buf,
493 opt->no_inline ? "attachment" : "inline",
494 filename.buf);
495 opt->diffopt.stat_sep = buffer.buf;
496 strbuf_release(&filename);
498 *extra_headers_p = extra_headers;
501 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
503 const char *color, *reset, *eol;
505 color = diff_get_color_opt(&opt->diffopt,
506 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
507 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
508 while (*bol) {
509 eol = strchrnul(bol, '\n');
510 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
511 *eol ? "\n" : "");
512 graph_show_oneline(opt->graph);
513 bol = (*eol) ? (eol + 1) : eol;
517 static void show_signature(struct rev_info *opt, struct commit *commit)
519 struct strbuf payload = STRBUF_INIT;
520 struct strbuf signature = STRBUF_INIT;
521 struct signature_check sigc = { 0 };
522 int status;
524 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
525 goto out;
527 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
528 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
529 status = check_signature(&sigc, signature.buf, signature.len);
530 if (status && !sigc.output)
531 show_sig_lines(opt, status, "No signature\n");
532 else
533 show_sig_lines(opt, status, sigc.output);
534 signature_check_clear(&sigc);
536 out:
537 strbuf_release(&payload);
538 strbuf_release(&signature);
541 static int which_parent(const struct object_id *oid, const struct commit *commit)
543 int nth;
544 const struct commit_list *parent;
546 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
547 if (oideq(&parent->item->object.oid, oid))
548 return nth;
549 nth++;
551 return -1;
554 static int is_common_merge(const struct commit *commit)
556 return (commit->parents
557 && commit->parents->next
558 && !commit->parents->next->next);
561 static int show_one_mergetag(struct commit *commit,
562 struct commit_extra_header *extra,
563 void *data)
565 struct rev_info *opt = (struct rev_info *)data;
566 struct object_id oid;
567 struct tag *tag;
568 struct strbuf verify_message;
569 struct signature_check sigc = { 0 };
570 int status, nth;
571 struct strbuf payload = STRBUF_INIT;
572 struct strbuf signature = STRBUF_INIT;
574 hash_object_file(the_hash_algo, extra->value, extra->len,
575 OBJ_TAG, &oid);
576 tag = lookup_tag(the_repository, &oid);
577 if (!tag)
578 return -1; /* error message already given */
580 strbuf_init(&verify_message, 256);
581 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
582 strbuf_addstr(&verify_message, "malformed mergetag\n");
583 else if (is_common_merge(commit) &&
584 oideq(&tag->tagged->oid,
585 &commit->parents->next->item->object.oid))
586 strbuf_addf(&verify_message,
587 "merged tag '%s'\n", tag->tag);
588 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
589 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
590 tag->tag, oid_to_hex(&tag->tagged->oid));
591 else
592 strbuf_addf(&verify_message,
593 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
595 status = -1;
596 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
597 /* could have a good signature */
598 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
599 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
600 status = check_signature(&sigc, signature.buf, signature.len);
601 if (sigc.output)
602 strbuf_addstr(&verify_message, sigc.output);
603 else
604 strbuf_addstr(&verify_message, "No signature\n");
605 signature_check_clear(&sigc);
606 /* otherwise we couldn't verify, which is shown as bad */
609 show_sig_lines(opt, status, verify_message.buf);
610 strbuf_release(&verify_message);
611 strbuf_release(&payload);
612 strbuf_release(&signature);
613 return 0;
616 static int show_mergetag(struct rev_info *opt, struct commit *commit)
618 return for_each_mergetag(show_one_mergetag, commit, opt);
621 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
623 const char *x = opt->shown_dashes ? "\n" : "---\n";
624 if (sb)
625 strbuf_addstr(sb, x);
626 else
627 fputs(x, opt->diffopt.file);
628 opt->shown_dashes = 1;
631 void show_log(struct rev_info *opt)
633 struct strbuf msgbuf = STRBUF_INIT;
634 struct log_info *log = opt->loginfo;
635 struct commit *commit = log->commit, *parent = log->parent;
636 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
637 const char *extra_headers = opt->extra_headers;
638 struct pretty_print_context ctx = {0};
640 opt->loginfo = NULL;
641 if (!opt->verbose_header) {
642 graph_show_commit(opt->graph);
644 if (!opt->graph)
645 put_revision_mark(opt, commit);
646 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
647 if (opt->print_parents)
648 show_parents(commit, abbrev_commit, opt->diffopt.file);
649 if (opt->children.name)
650 show_children(opt, commit, abbrev_commit);
651 show_decorations(opt, commit);
652 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
653 putc('\n', opt->diffopt.file);
654 graph_show_remainder(opt->graph);
656 putc(opt->diffopt.line_termination, opt->diffopt.file);
657 return;
661 * If use_terminator is set, we already handled any record termination
662 * at the end of the last record.
663 * Otherwise, add a diffopt.line_termination character before all
664 * entries but the first. (IOW, as a separator between entries)
666 if (opt->shown_one && !opt->use_terminator) {
668 * If entries are separated by a newline, the output
669 * should look human-readable. If the last entry ended
670 * with a newline, print the graph output before this
671 * newline. Otherwise it will end up as a completely blank
672 * line and will look like a gap in the graph.
674 * If the entry separator is not a newline, the output is
675 * primarily intended for programmatic consumption, and we
676 * never want the extra graph output before the entry
677 * separator.
679 if (opt->diffopt.line_termination == '\n' &&
680 !opt->missing_newline)
681 graph_show_padding(opt->graph);
682 putc(opt->diffopt.line_termination, opt->diffopt.file);
684 opt->shown_one = 1;
687 * If the history graph was requested,
688 * print the graph, up to this commit's line
690 graph_show_commit(opt->graph);
693 * Print header line of header..
696 if (cmit_fmt_is_mail(opt->commit_format)) {
697 log_write_email_headers(opt, commit, &extra_headers,
698 &ctx.need_8bit_cte, 1);
699 ctx.rev = opt;
700 ctx.print_email_subject = 1;
701 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
702 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
703 if (opt->commit_format != CMIT_FMT_ONELINE)
704 fputs("commit ", opt->diffopt.file);
706 if (!opt->graph)
707 put_revision_mark(opt, commit);
708 fputs(find_unique_abbrev(&commit->object.oid,
709 abbrev_commit),
710 opt->diffopt.file);
711 if (opt->print_parents)
712 show_parents(commit, abbrev_commit, opt->diffopt.file);
713 if (opt->children.name)
714 show_children(opt, commit, abbrev_commit);
715 if (parent)
716 fprintf(opt->diffopt.file, " (from %s)",
717 find_unique_abbrev(&parent->object.oid, abbrev_commit));
718 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
719 show_decorations(opt, commit);
720 if (opt->commit_format == CMIT_FMT_ONELINE) {
721 putc(' ', opt->diffopt.file);
722 } else {
723 putc('\n', opt->diffopt.file);
724 graph_show_oneline(opt->graph);
726 if (opt->reflog_info) {
728 * setup_revisions() ensures that opt->reflog_info
729 * and opt->graph cannot both be set,
730 * so we don't need to worry about printing the
731 * graph info here.
733 show_reflog_message(opt->reflog_info,
734 opt->commit_format == CMIT_FMT_ONELINE,
735 &opt->date_mode,
736 opt->date_mode_explicit);
737 if (opt->commit_format == CMIT_FMT_ONELINE)
738 return;
742 if (opt->show_signature) {
743 show_signature(opt, commit);
744 show_mergetag(opt, commit);
747 if (opt->show_notes) {
748 int raw;
749 struct strbuf notebuf = STRBUF_INIT;
751 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
752 format_display_notes(&commit->object.oid, &notebuf,
753 get_log_output_encoding(), raw);
754 ctx.notes_message = strbuf_detach(&notebuf, NULL);
758 * And then the pretty-printed message itself
760 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
761 ctx.need_8bit_cte =
762 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
763 ctx.date_mode = opt->date_mode;
764 ctx.date_mode_explicit = opt->date_mode_explicit;
765 ctx.abbrev = opt->diffopt.abbrev;
766 ctx.after_subject = extra_headers;
767 ctx.preserve_subject = opt->preserve_subject;
768 ctx.encode_email_headers = opt->encode_email_headers;
769 ctx.reflog_info = opt->reflog_info;
770 ctx.fmt = opt->commit_format;
771 ctx.mailmap = opt->mailmap;
772 ctx.color = opt->diffopt.use_color;
773 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
774 ctx.output_encoding = get_log_output_encoding();
775 ctx.rev = opt;
776 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
777 ctx.from_ident = &opt->from_ident;
778 if (opt->graph)
779 ctx.graph_width = graph_width(opt->graph);
780 pretty_print_commit(&ctx, commit, &msgbuf);
782 if (opt->add_signoff)
783 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
785 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
786 ctx.notes_message && *ctx.notes_message) {
787 if (cmit_fmt_is_mail(ctx.fmt))
788 next_commentary_block(opt, &msgbuf);
789 strbuf_addstr(&msgbuf, ctx.notes_message);
792 if (opt->show_log_size) {
793 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
794 graph_show_oneline(opt->graph);
798 * Set opt->missing_newline if msgbuf doesn't
799 * end in a newline (including if it is empty)
801 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
802 opt->missing_newline = 1;
803 else
804 opt->missing_newline = 0;
806 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
807 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
808 if (!opt->missing_newline)
809 graph_show_padding(opt->graph);
810 putc(opt->diffopt.line_termination, opt->diffopt.file);
813 strbuf_release(&msgbuf);
814 free(ctx.notes_message);
816 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
817 struct diff_queue_struct dq;
819 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
820 DIFF_QUEUE_CLEAR(&diff_queued_diff);
822 next_commentary_block(opt, NULL);
823 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
824 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
825 &opt->diffopt);
827 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
830 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
831 struct diff_queue_struct dq;
832 struct diff_options opts;
833 struct range_diff_options range_diff_opts = {
834 .creation_factor = opt->creation_factor,
835 .dual_color = 1,
836 .diffopt = &opts
839 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
840 DIFF_QUEUE_CLEAR(&diff_queued_diff);
842 next_commentary_block(opt, NULL);
843 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
845 * Pass minimum required diff-options to range-diff; others
846 * can be added later if deemed desirable.
848 diff_setup(&opts);
849 opts.file = opt->diffopt.file;
850 opts.use_color = opt->diffopt.use_color;
851 diff_setup_done(&opts);
852 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
854 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
858 int log_tree_diff_flush(struct rev_info *opt)
860 opt->shown_dashes = 0;
861 diffcore_std(&opt->diffopt);
863 if (diff_queue_is_empty(&opt->diffopt)) {
864 int saved_fmt = opt->diffopt.output_format;
865 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
866 diff_flush(&opt->diffopt);
867 opt->diffopt.output_format = saved_fmt;
868 return 0;
871 if (opt->loginfo && !opt->no_commit_id) {
872 show_log(opt);
873 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
874 opt->verbose_header &&
875 opt->commit_format != CMIT_FMT_ONELINE &&
876 !commit_format_is_empty(opt->commit_format)) {
878 * When showing a verbose header (i.e. log message),
879 * and not in --pretty=oneline format, we would want
880 * an extra newline between the end of log and the
881 * diff/diffstat output for readability.
883 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
884 if (opt->diffopt.output_prefix) {
885 struct strbuf *msg = NULL;
886 msg = opt->diffopt.output_prefix(&opt->diffopt,
887 opt->diffopt.output_prefix_data);
888 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
892 * We may have shown three-dashes line early
893 * between generated commentary (notes, etc.)
894 * and the log message, in which case we only
895 * want a blank line after the commentary
896 * without (an extra) three-dashes line.
897 * Otherwise, we show the three-dashes line if
898 * we are showing the patch with diffstat, but
899 * in that case, there is no extra blank line
900 * after the three-dashes line.
902 if (!opt->shown_dashes &&
903 (pch & opt->diffopt.output_format) == pch)
904 fprintf(opt->diffopt.file, "---");
905 putc('\n', opt->diffopt.file);
908 diff_flush(&opt->diffopt);
909 return 1;
912 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
914 diff_tree_combined_merge(commit, opt);
915 return !opt->loginfo;
918 static void setup_additional_headers(struct diff_options *o,
919 struct strmap *all_headers)
921 struct hashmap_iter iter;
922 struct strmap_entry *entry;
925 * Make o->additional_path_headers contain the subset of all_headers
926 * that match o->pathspec. If there aren't any that match o->pathspec,
927 * then make o->additional_path_headers be NULL.
930 if (!o->pathspec.nr) {
931 o->additional_path_headers = all_headers;
932 return;
935 o->additional_path_headers = xmalloc(sizeof(struct strmap));
936 strmap_init_with_options(o->additional_path_headers, NULL, 0);
937 strmap_for_each_entry(all_headers, &iter, entry) {
938 if (match_pathspec(the_repository->index, &o->pathspec,
939 entry->key, strlen(entry->key),
940 0 /* prefix */, NULL /* seen */,
941 0 /* is_dir */))
942 strmap_put(o->additional_path_headers,
943 entry->key, entry->value);
945 if (!strmap_get_size(o->additional_path_headers)) {
946 strmap_clear(o->additional_path_headers, 0);
947 FREE_AND_NULL(o->additional_path_headers);
951 static void cleanup_additional_headers(struct diff_options *o)
953 if (!o->pathspec.nr) {
954 o->additional_path_headers = NULL;
955 return;
957 if (!o->additional_path_headers)
958 return;
960 strmap_clear(o->additional_path_headers, 0);
961 FREE_AND_NULL(o->additional_path_headers);
964 static int do_remerge_diff(struct rev_info *opt,
965 struct commit_list *parents,
966 struct object_id *oid)
968 struct merge_options o;
969 struct commit_list *bases;
970 struct merge_result res = {0};
971 struct pretty_print_context ctx = {0};
972 struct commit *parent1 = parents->item;
973 struct commit *parent2 = parents->next->item;
974 struct strbuf parent1_desc = STRBUF_INIT;
975 struct strbuf parent2_desc = STRBUF_INIT;
977 /* Setup merge options */
978 init_merge_options(&o, the_repository);
979 o.show_rename_progress = 0;
980 o.record_conflict_msgs_as_headers = 1;
981 o.msg_header_prefix = "remerge";
983 ctx.abbrev = DEFAULT_ABBREV;
984 format_commit_message(parent1, "%h (%s)", &parent1_desc, &ctx);
985 format_commit_message(parent2, "%h (%s)", &parent2_desc, &ctx);
986 o.branch1 = parent1_desc.buf;
987 o.branch2 = parent2_desc.buf;
989 /* Parse the relevant commits and get the merge bases */
990 parse_commit_or_die(parent1);
991 parse_commit_or_die(parent2);
992 bases = get_merge_bases(parent1, parent2);
994 /* Re-merge the parents */
995 merge_incore_recursive(&o, bases, parent1, parent2, &res);
997 /* Show the diff */
998 setup_additional_headers(&opt->diffopt, res.path_messages);
999 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1000 log_tree_diff_flush(opt);
1002 /* Cleanup */
1003 cleanup_additional_headers(&opt->diffopt);
1004 strbuf_release(&parent1_desc);
1005 strbuf_release(&parent2_desc);
1006 merge_finalize(&o, &res);
1008 /* Clean up the contents of the temporary object directory */
1009 if (opt->remerge_objdir)
1010 tmp_objdir_discard_objects(opt->remerge_objdir);
1011 else
1012 BUG("did a remerge diff without remerge_objdir?!?");
1014 return !opt->loginfo;
1018 * Show the diff of a commit.
1020 * Return true if we printed any log info messages
1022 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1024 int showed_log;
1025 struct commit_list *parents;
1026 struct object_id *oid;
1027 int is_merge;
1028 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1030 if (!all_need_diff && !opt->merges_need_diff)
1031 return 0;
1033 parse_commit_or_die(commit);
1034 oid = get_commit_tree_oid(commit);
1036 parents = get_saved_parents(opt, commit);
1037 is_merge = parents && parents->next;
1038 if (!is_merge && !all_need_diff)
1039 return 0;
1041 /* Root commit? */
1042 if (!parents) {
1043 if (opt->show_root_diff) {
1044 diff_root_tree_oid(oid, "", &opt->diffopt);
1045 log_tree_diff_flush(opt);
1047 return !opt->loginfo;
1050 if (is_merge) {
1051 int octopus = (parents->next->next != NULL);
1053 if (opt->remerge_diff) {
1054 if (octopus) {
1055 show_log(opt);
1056 fprintf(opt->diffopt.file,
1057 "diff: warning: Skipping remerge-diff "
1058 "for octopus merges.\n");
1059 return 1;
1061 return do_remerge_diff(opt, parents, oid);
1063 if (opt->combine_merges)
1064 return do_diff_combined(opt, commit);
1065 if (opt->separate_merges) {
1066 if (!opt->first_parent_merges) {
1067 /* Show parent info for multiple diffs */
1068 log->parent = parents->item;
1070 } else
1071 return 0;
1074 showed_log = 0;
1075 for (;;) {
1076 struct commit *parent = parents->item;
1078 parse_commit_or_die(parent);
1079 diff_tree_oid(get_commit_tree_oid(parent),
1080 oid, "", &opt->diffopt);
1081 log_tree_diff_flush(opt);
1083 showed_log |= !opt->loginfo;
1085 /* Set up the log info for the next parent, if any.. */
1086 parents = parents->next;
1087 if (!parents || opt->first_parent_merges)
1088 break;
1089 log->parent = parents->item;
1090 opt->loginfo = log;
1092 return showed_log;
1095 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1097 struct log_info log;
1098 int shown;
1099 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1100 int no_free = opt->diffopt.no_free;
1102 log.commit = commit;
1103 log.parent = NULL;
1104 opt->loginfo = &log;
1105 opt->diffopt.no_free = 1;
1107 /* NEEDSWORK: no restoring of no_free? Why? */
1108 if (opt->line_level_traverse)
1109 return line_log_print(opt, commit);
1111 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1112 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1113 shown = log_tree_diff(opt, commit, &log);
1114 if (!shown && opt->loginfo && opt->always_show_header) {
1115 log.parent = NULL;
1116 show_log(opt);
1117 shown = 1;
1119 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1120 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1121 opt->loginfo = NULL;
1122 maybe_flush_or_die(opt->diffopt.file, "stdout");
1123 opt->diffopt.no_free = no_free;
1124 diff_free(&opt->diffopt);
1125 return shown;