config: mark unused callback parameters
[git.git] / log-tree.c
blob114bd80365e31b4852effe494e7100c2d2713af8
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 UNUSED(flags),
139 void *cb_data)
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;
146 if (filter && !ref_filter_match(refname, filter))
147 return 0;
149 if (starts_with(refname, git_replace_ref_base)) {
150 struct object_id original_oid;
151 if (!read_replace_refs)
152 return 0;
153 if (get_oid_hex(refname + strlen(git_replace_ref_base),
154 &original_oid)) {
155 warning("invalid replace ref %s", refname);
156 return 0;
158 obj = parse_object(the_repository, &original_oid);
159 if (obj)
160 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
161 return 0;
164 objtype = oid_object_info(the_repository, oid, NULL);
165 if (objtype < 0)
166 return 0;
167 obj = lookup_object_by_type(the_repository, oid, objtype);
169 if (starts_with(refname, "refs/heads/"))
170 deco_type = DECORATION_REF_LOCAL;
171 else if (starts_with(refname, "refs/remotes/"))
172 deco_type = DECORATION_REF_REMOTE;
173 else if (starts_with(refname, "refs/tags/"))
174 deco_type = DECORATION_REF_TAG;
175 else if (!strcmp(refname, "refs/stash"))
176 deco_type = DECORATION_REF_STASH;
177 else if (!strcmp(refname, "HEAD"))
178 deco_type = DECORATION_REF_HEAD;
180 add_name_decoration(deco_type, refname, obj);
181 while (obj->type == OBJ_TAG) {
182 if (!obj->parsed)
183 parse_object(the_repository, &obj->oid);
184 obj = ((struct tag *)obj)->tagged;
185 if (!obj)
186 break;
187 add_name_decoration(DECORATION_REF_TAG, refname, obj);
189 return 0;
192 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
194 struct commit *commit = lookup_commit(the_repository, &graft->oid);
195 if (!commit)
196 return 0;
197 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
198 return 0;
201 void load_ref_decorations(struct decoration_filter *filter, int flags)
203 if (!decoration_loaded) {
204 if (filter) {
205 struct string_list_item *item;
206 for_each_string_list_item(item, filter->exclude_ref_pattern) {
207 normalize_glob_ref(item, NULL, item->string);
209 for_each_string_list_item(item, filter->include_ref_pattern) {
210 normalize_glob_ref(item, NULL, item->string);
212 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
213 normalize_glob_ref(item, NULL, item->string);
216 decoration_loaded = 1;
217 decoration_flags = flags;
218 for_each_ref(add_ref_decoration, filter);
219 head_ref(add_ref_decoration, filter);
220 for_each_commit_graft(add_graft_decoration, filter);
224 static void show_parents(struct commit *commit, int abbrev, FILE *file)
226 struct commit_list *p;
227 for (p = commit->parents; p ; p = p->next) {
228 struct commit *parent = p->item;
229 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
233 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
235 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
236 for ( ; p; p = p->next) {
237 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
242 * Do we have HEAD in the output, and also the branch it points at?
243 * If so, find that decoration entry for that current branch.
245 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
247 const struct name_decoration *list, *head = NULL;
248 const char *branch_name = NULL;
249 int rru_flags;
251 /* First find HEAD */
252 for (list = decoration; list; list = list->next)
253 if (list->type == DECORATION_REF_HEAD) {
254 head = list;
255 break;
257 if (!head)
258 return NULL;
260 /* Now resolve and find the matching current branch */
261 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
262 if (!branch_name || !(rru_flags & REF_ISSYMREF))
263 return NULL;
265 if (!starts_with(branch_name, "refs/"))
266 return NULL;
268 /* OK, do we have that ref in the list? */
269 for (list = decoration; list; list = list->next)
270 if ((list->type == DECORATION_REF_LOCAL) &&
271 !strcmp(branch_name, list->name)) {
272 return list;
275 return NULL;
278 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
280 if (decoration_flags == DECORATE_SHORT_REFS)
281 strbuf_addstr(sb, prettify_refname(decoration->name));
282 else
283 strbuf_addstr(sb, decoration->name);
287 * The caller makes sure there is no funny color before calling.
288 * format_decorations_extended makes sure the same after return.
290 void format_decorations_extended(struct strbuf *sb,
291 const struct commit *commit,
292 int use_color,
293 const char *prefix,
294 const char *separator,
295 const char *suffix)
297 const struct name_decoration *decoration;
298 const struct name_decoration *current_and_HEAD;
299 const char *color_commit =
300 diff_get_color(use_color, DIFF_COMMIT);
301 const char *color_reset =
302 decorate_get_color(use_color, DECORATION_NONE);
304 decoration = get_name_decoration(&commit->object);
305 if (!decoration)
306 return;
308 current_and_HEAD = current_pointed_by_HEAD(decoration);
309 while (decoration) {
311 * When both current and HEAD are there, only
312 * show HEAD->current where HEAD would have
313 * appeared, skipping the entry for current.
315 if (decoration != current_and_HEAD) {
316 strbuf_addstr(sb, color_commit);
317 strbuf_addstr(sb, prefix);
318 strbuf_addstr(sb, color_reset);
319 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
320 if (decoration->type == DECORATION_REF_TAG)
321 strbuf_addstr(sb, "tag: ");
323 show_name(sb, decoration);
325 if (current_and_HEAD &&
326 decoration->type == DECORATION_REF_HEAD) {
327 strbuf_addstr(sb, " -> ");
328 strbuf_addstr(sb, color_reset);
329 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
330 show_name(sb, current_and_HEAD);
332 strbuf_addstr(sb, color_reset);
334 prefix = separator;
336 decoration = decoration->next;
338 strbuf_addstr(sb, color_commit);
339 strbuf_addstr(sb, suffix);
340 strbuf_addstr(sb, color_reset);
343 void show_decorations(struct rev_info *opt, struct commit *commit)
345 struct strbuf sb = STRBUF_INIT;
347 if (opt->sources) {
348 char **slot = revision_sources_peek(opt->sources, commit);
350 if (slot && *slot)
351 fprintf(opt->diffopt.file, "\t%s", *slot);
353 if (!opt->show_decorations)
354 return;
355 format_decorations(&sb, commit, opt->diffopt.use_color);
356 fputs(sb.buf, opt->diffopt.file);
357 strbuf_release(&sb);
360 static unsigned int digits_in_number(unsigned int number)
362 unsigned int i = 10, result = 1;
363 while (i <= number) {
364 i *= 10;
365 result++;
367 return result;
370 void fmt_output_subject(struct strbuf *filename,
371 const char *subject,
372 struct rev_info *info)
374 const char *suffix = info->patch_suffix;
375 int nr = info->nr;
376 int start_len = filename->len;
377 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
379 if (info->reroll_count) {
380 struct strbuf temp = STRBUF_INIT;
382 strbuf_addf(&temp, "v%s", info->reroll_count);
383 format_sanitized_subject(filename, temp.buf, temp.len);
384 strbuf_addstr(filename, "-");
385 strbuf_release(&temp);
387 strbuf_addf(filename, "%04d-%s", nr, subject);
389 if (max_len < filename->len)
390 strbuf_setlen(filename, max_len);
391 strbuf_addstr(filename, suffix);
394 void fmt_output_commit(struct strbuf *filename,
395 struct commit *commit,
396 struct rev_info *info)
398 struct pretty_print_context ctx = {0};
399 struct strbuf subject = STRBUF_INIT;
401 format_commit_message(commit, "%f", &subject, &ctx);
402 fmt_output_subject(filename, subject.buf, info);
403 strbuf_release(&subject);
406 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
408 if (opt->total > 0) {
409 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
410 opt->subject_prefix,
411 *opt->subject_prefix ? " " : "",
412 digits_in_number(opt->total),
413 opt->nr, opt->total);
414 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
415 strbuf_addf(sb, "Subject: [%s] ",
416 opt->subject_prefix);
417 } else {
418 strbuf_addstr(sb, "Subject: ");
422 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
423 const char **extra_headers_p,
424 int *need_8bit_cte_p,
425 int maybe_multipart)
427 const char *extra_headers = opt->extra_headers;
428 const char *name = oid_to_hex(opt->zero_commit ?
429 null_oid() : &commit->object.oid);
431 *need_8bit_cte_p = 0; /* unknown */
433 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
434 graph_show_oneline(opt->graph);
435 if (opt->message_id) {
436 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
437 graph_show_oneline(opt->graph);
439 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
440 int i, n;
441 n = opt->ref_message_ids->nr;
442 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
443 for (i = 0; i < n; i++)
444 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
445 opt->ref_message_ids->items[i].string);
446 graph_show_oneline(opt->graph);
448 if (opt->mime_boundary && maybe_multipart) {
449 static struct strbuf subject_buffer = STRBUF_INIT;
450 static struct strbuf buffer = STRBUF_INIT;
451 struct strbuf filename = STRBUF_INIT;
452 *need_8bit_cte_p = -1; /* NEVER */
454 strbuf_reset(&subject_buffer);
455 strbuf_reset(&buffer);
457 strbuf_addf(&subject_buffer,
458 "%s"
459 "MIME-Version: 1.0\n"
460 "Content-Type: multipart/mixed;"
461 " boundary=\"%s%s\"\n"
462 "\n"
463 "This is a multi-part message in MIME "
464 "format.\n"
465 "--%s%s\n"
466 "Content-Type: text/plain; "
467 "charset=UTF-8; format=fixed\n"
468 "Content-Transfer-Encoding: 8bit\n\n",
469 extra_headers ? extra_headers : "",
470 mime_boundary_leader, opt->mime_boundary,
471 mime_boundary_leader, opt->mime_boundary);
472 extra_headers = subject_buffer.buf;
474 if (opt->numbered_files)
475 strbuf_addf(&filename, "%d", opt->nr);
476 else
477 fmt_output_commit(&filename, commit, opt);
478 strbuf_addf(&buffer,
479 "\n--%s%s\n"
480 "Content-Type: text/x-patch;"
481 " name=\"%s\"\n"
482 "Content-Transfer-Encoding: 8bit\n"
483 "Content-Disposition: %s;"
484 " filename=\"%s\"\n\n",
485 mime_boundary_leader, opt->mime_boundary,
486 filename.buf,
487 opt->no_inline ? "attachment" : "inline",
488 filename.buf);
489 opt->diffopt.stat_sep = buffer.buf;
490 strbuf_release(&filename);
492 *extra_headers_p = extra_headers;
495 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
497 const char *color, *reset, *eol;
499 color = diff_get_color_opt(&opt->diffopt,
500 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
501 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
502 while (*bol) {
503 eol = strchrnul(bol, '\n');
504 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
505 *eol ? "\n" : "");
506 graph_show_oneline(opt->graph);
507 bol = (*eol) ? (eol + 1) : eol;
511 static void show_signature(struct rev_info *opt, struct commit *commit)
513 struct strbuf payload = STRBUF_INIT;
514 struct strbuf signature = STRBUF_INIT;
515 struct signature_check sigc = { 0 };
516 int status;
518 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
519 goto out;
521 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
522 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
523 status = check_signature(&sigc, signature.buf, signature.len);
524 if (status && !sigc.output)
525 show_sig_lines(opt, status, "No signature\n");
526 else
527 show_sig_lines(opt, status, sigc.output);
528 signature_check_clear(&sigc);
530 out:
531 strbuf_release(&payload);
532 strbuf_release(&signature);
535 static int which_parent(const struct object_id *oid, const struct commit *commit)
537 int nth;
538 const struct commit_list *parent;
540 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
541 if (oideq(&parent->item->object.oid, oid))
542 return nth;
543 nth++;
545 return -1;
548 static int is_common_merge(const struct commit *commit)
550 return (commit->parents
551 && commit->parents->next
552 && !commit->parents->next->next);
555 static int show_one_mergetag(struct commit *commit,
556 struct commit_extra_header *extra,
557 void *data)
559 struct rev_info *opt = (struct rev_info *)data;
560 struct object_id oid;
561 struct tag *tag;
562 struct strbuf verify_message;
563 struct signature_check sigc = { 0 };
564 int status, nth;
565 struct strbuf payload = STRBUF_INIT;
566 struct strbuf signature = STRBUF_INIT;
568 hash_object_file(the_hash_algo, extra->value, extra->len,
569 OBJ_TAG, &oid);
570 tag = lookup_tag(the_repository, &oid);
571 if (!tag)
572 return -1; /* error message already given */
574 strbuf_init(&verify_message, 256);
575 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
576 strbuf_addstr(&verify_message, "malformed mergetag\n");
577 else if (is_common_merge(commit) &&
578 oideq(&tag->tagged->oid,
579 &commit->parents->next->item->object.oid))
580 strbuf_addf(&verify_message,
581 "merged tag '%s'\n", tag->tag);
582 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
583 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
584 tag->tag, oid_to_hex(&tag->tagged->oid));
585 else
586 strbuf_addf(&verify_message,
587 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
589 status = -1;
590 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
591 /* could have a good signature */
592 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
593 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
594 status = check_signature(&sigc, signature.buf, signature.len);
595 if (sigc.output)
596 strbuf_addstr(&verify_message, sigc.output);
597 else
598 strbuf_addstr(&verify_message, "No signature\n");
599 signature_check_clear(&sigc);
600 /* otherwise we couldn't verify, which is shown as bad */
603 show_sig_lines(opt, status, verify_message.buf);
604 strbuf_release(&verify_message);
605 strbuf_release(&payload);
606 strbuf_release(&signature);
607 return 0;
610 static int show_mergetag(struct rev_info *opt, struct commit *commit)
612 return for_each_mergetag(show_one_mergetag, commit, opt);
615 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
617 const char *x = opt->shown_dashes ? "\n" : "---\n";
618 if (sb)
619 strbuf_addstr(sb, x);
620 else
621 fputs(x, opt->diffopt.file);
622 opt->shown_dashes = 1;
625 void show_log(struct rev_info *opt)
627 struct strbuf msgbuf = STRBUF_INIT;
628 struct log_info *log = opt->loginfo;
629 struct commit *commit = log->commit, *parent = log->parent;
630 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
631 const char *extra_headers = opt->extra_headers;
632 struct pretty_print_context ctx = {0};
634 opt->loginfo = NULL;
635 if (!opt->verbose_header) {
636 graph_show_commit(opt->graph);
638 if (!opt->graph)
639 put_revision_mark(opt, commit);
640 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
641 if (opt->print_parents)
642 show_parents(commit, abbrev_commit, opt->diffopt.file);
643 if (opt->children.name)
644 show_children(opt, commit, abbrev_commit);
645 show_decorations(opt, commit);
646 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
647 putc('\n', opt->diffopt.file);
648 graph_show_remainder(opt->graph);
650 putc(opt->diffopt.line_termination, opt->diffopt.file);
651 return;
655 * If use_terminator is set, we already handled any record termination
656 * at the end of the last record.
657 * Otherwise, add a diffopt.line_termination character before all
658 * entries but the first. (IOW, as a separator between entries)
660 if (opt->shown_one && !opt->use_terminator) {
662 * If entries are separated by a newline, the output
663 * should look human-readable. If the last entry ended
664 * with a newline, print the graph output before this
665 * newline. Otherwise it will end up as a completely blank
666 * line and will look like a gap in the graph.
668 * If the entry separator is not a newline, the output is
669 * primarily intended for programmatic consumption, and we
670 * never want the extra graph output before the entry
671 * separator.
673 if (opt->diffopt.line_termination == '\n' &&
674 !opt->missing_newline)
675 graph_show_padding(opt->graph);
676 putc(opt->diffopt.line_termination, opt->diffopt.file);
678 opt->shown_one = 1;
681 * If the history graph was requested,
682 * print the graph, up to this commit's line
684 graph_show_commit(opt->graph);
687 * Print header line of header..
690 if (cmit_fmt_is_mail(opt->commit_format)) {
691 log_write_email_headers(opt, commit, &extra_headers,
692 &ctx.need_8bit_cte, 1);
693 ctx.rev = opt;
694 ctx.print_email_subject = 1;
695 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
696 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
697 if (opt->commit_format != CMIT_FMT_ONELINE)
698 fputs("commit ", opt->diffopt.file);
700 if (!opt->graph)
701 put_revision_mark(opt, commit);
702 fputs(find_unique_abbrev(&commit->object.oid,
703 abbrev_commit),
704 opt->diffopt.file);
705 if (opt->print_parents)
706 show_parents(commit, abbrev_commit, opt->diffopt.file);
707 if (opt->children.name)
708 show_children(opt, commit, abbrev_commit);
709 if (parent)
710 fprintf(opt->diffopt.file, " (from %s)",
711 find_unique_abbrev(&parent->object.oid, abbrev_commit));
712 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
713 show_decorations(opt, commit);
714 if (opt->commit_format == CMIT_FMT_ONELINE) {
715 putc(' ', opt->diffopt.file);
716 } else {
717 putc('\n', opt->diffopt.file);
718 graph_show_oneline(opt->graph);
720 if (opt->reflog_info) {
722 * setup_revisions() ensures that opt->reflog_info
723 * and opt->graph cannot both be set,
724 * so we don't need to worry about printing the
725 * graph info here.
727 show_reflog_message(opt->reflog_info,
728 opt->commit_format == CMIT_FMT_ONELINE,
729 &opt->date_mode,
730 opt->date_mode_explicit);
731 if (opt->commit_format == CMIT_FMT_ONELINE)
732 return;
736 if (opt->show_signature) {
737 show_signature(opt, commit);
738 show_mergetag(opt, commit);
741 if (opt->show_notes) {
742 int raw;
743 struct strbuf notebuf = STRBUF_INIT;
745 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
746 format_display_notes(&commit->object.oid, &notebuf,
747 get_log_output_encoding(), raw);
748 ctx.notes_message = strbuf_detach(&notebuf, NULL);
752 * And then the pretty-printed message itself
754 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
755 ctx.need_8bit_cte =
756 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
757 ctx.date_mode = opt->date_mode;
758 ctx.date_mode_explicit = opt->date_mode_explicit;
759 ctx.abbrev = opt->diffopt.abbrev;
760 ctx.after_subject = extra_headers;
761 ctx.preserve_subject = opt->preserve_subject;
762 ctx.encode_email_headers = opt->encode_email_headers;
763 ctx.reflog_info = opt->reflog_info;
764 ctx.fmt = opt->commit_format;
765 ctx.mailmap = opt->mailmap;
766 ctx.color = opt->diffopt.use_color;
767 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
768 ctx.output_encoding = get_log_output_encoding();
769 ctx.rev = opt;
770 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
771 ctx.from_ident = &opt->from_ident;
772 if (opt->graph)
773 ctx.graph_width = graph_width(opt->graph);
774 pretty_print_commit(&ctx, commit, &msgbuf);
776 if (opt->add_signoff)
777 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
779 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
780 ctx.notes_message && *ctx.notes_message) {
781 if (cmit_fmt_is_mail(ctx.fmt))
782 next_commentary_block(opt, &msgbuf);
783 strbuf_addstr(&msgbuf, ctx.notes_message);
786 if (opt->show_log_size) {
787 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
788 graph_show_oneline(opt->graph);
792 * Set opt->missing_newline if msgbuf doesn't
793 * end in a newline (including if it is empty)
795 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
796 opt->missing_newline = 1;
797 else
798 opt->missing_newline = 0;
800 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
801 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
802 if (!opt->missing_newline)
803 graph_show_padding(opt->graph);
804 putc(opt->diffopt.line_termination, opt->diffopt.file);
807 strbuf_release(&msgbuf);
808 free(ctx.notes_message);
810 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
811 struct diff_queue_struct dq;
813 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
814 DIFF_QUEUE_CLEAR(&diff_queued_diff);
816 next_commentary_block(opt, NULL);
817 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
818 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
819 &opt->diffopt);
821 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
824 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
825 struct diff_queue_struct dq;
826 struct diff_options opts;
827 struct range_diff_options range_diff_opts = {
828 .creation_factor = opt->creation_factor,
829 .dual_color = 1,
830 .diffopt = &opts
833 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
834 DIFF_QUEUE_CLEAR(&diff_queued_diff);
836 next_commentary_block(opt, NULL);
837 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
839 * Pass minimum required diff-options to range-diff; others
840 * can be added later if deemed desirable.
842 diff_setup(&opts);
843 opts.file = opt->diffopt.file;
844 opts.use_color = opt->diffopt.use_color;
845 diff_setup_done(&opts);
846 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
848 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
852 int log_tree_diff_flush(struct rev_info *opt)
854 opt->shown_dashes = 0;
855 diffcore_std(&opt->diffopt);
857 if (diff_queue_is_empty(&opt->diffopt)) {
858 int saved_fmt = opt->diffopt.output_format;
859 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
860 diff_flush(&opt->diffopt);
861 opt->diffopt.output_format = saved_fmt;
862 return 0;
865 if (opt->loginfo && !opt->no_commit_id) {
866 show_log(opt);
867 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
868 opt->verbose_header &&
869 opt->commit_format != CMIT_FMT_ONELINE &&
870 !commit_format_is_empty(opt->commit_format)) {
872 * When showing a verbose header (i.e. log message),
873 * and not in --pretty=oneline format, we would want
874 * an extra newline between the end of log and the
875 * diff/diffstat output for readability.
877 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
878 if (opt->diffopt.output_prefix) {
879 struct strbuf *msg = NULL;
880 msg = opt->diffopt.output_prefix(&opt->diffopt,
881 opt->diffopt.output_prefix_data);
882 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
886 * We may have shown three-dashes line early
887 * between generated commentary (notes, etc.)
888 * and the log message, in which case we only
889 * want a blank line after the commentary
890 * without (an extra) three-dashes line.
891 * Otherwise, we show the three-dashes line if
892 * we are showing the patch with diffstat, but
893 * in that case, there is no extra blank line
894 * after the three-dashes line.
896 if (!opt->shown_dashes &&
897 (pch & opt->diffopt.output_format) == pch)
898 fprintf(opt->diffopt.file, "---");
899 putc('\n', opt->diffopt.file);
902 diff_flush(&opt->diffopt);
903 return 1;
906 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
908 diff_tree_combined_merge(commit, opt);
909 return !opt->loginfo;
912 static void setup_additional_headers(struct diff_options *o,
913 struct strmap *all_headers)
915 struct hashmap_iter iter;
916 struct strmap_entry *entry;
919 * Make o->additional_path_headers contain the subset of all_headers
920 * that match o->pathspec. If there aren't any that match o->pathspec,
921 * then make o->additional_path_headers be NULL.
924 if (!o->pathspec.nr) {
925 o->additional_path_headers = all_headers;
926 return;
929 o->additional_path_headers = xmalloc(sizeof(struct strmap));
930 strmap_init_with_options(o->additional_path_headers, NULL, 0);
931 strmap_for_each_entry(all_headers, &iter, entry) {
932 if (match_pathspec(the_repository->index, &o->pathspec,
933 entry->key, strlen(entry->key),
934 0 /* prefix */, NULL /* seen */,
935 0 /* is_dir */))
936 strmap_put(o->additional_path_headers,
937 entry->key, entry->value);
939 if (!strmap_get_size(o->additional_path_headers)) {
940 strmap_clear(o->additional_path_headers, 0);
941 FREE_AND_NULL(o->additional_path_headers);
945 static void cleanup_additional_headers(struct diff_options *o)
947 if (!o->pathspec.nr) {
948 o->additional_path_headers = NULL;
949 return;
951 if (!o->additional_path_headers)
952 return;
954 strmap_clear(o->additional_path_headers, 0);
955 FREE_AND_NULL(o->additional_path_headers);
958 static int do_remerge_diff(struct rev_info *opt,
959 struct commit_list *parents,
960 struct object_id *oid,
961 struct commit *commit)
963 struct merge_options o;
964 struct commit_list *bases;
965 struct merge_result res = {0};
966 struct pretty_print_context ctx = {0};
967 struct commit *parent1 = parents->item;
968 struct commit *parent2 = parents->next->item;
969 struct strbuf parent1_desc = STRBUF_INIT;
970 struct strbuf parent2_desc = STRBUF_INIT;
972 /* Setup merge options */
973 init_merge_options(&o, the_repository);
974 o.show_rename_progress = 0;
975 o.record_conflict_msgs_as_headers = 1;
976 o.msg_header_prefix = "remerge";
978 ctx.abbrev = DEFAULT_ABBREV;
979 format_commit_message(parent1, "%h (%s)", &parent1_desc, &ctx);
980 format_commit_message(parent2, "%h (%s)", &parent2_desc, &ctx);
981 o.branch1 = parent1_desc.buf;
982 o.branch2 = parent2_desc.buf;
984 /* Parse the relevant commits and get the merge bases */
985 parse_commit_or_die(parent1);
986 parse_commit_or_die(parent2);
987 bases = get_merge_bases(parent1, parent2);
989 /* Re-merge the parents */
990 merge_incore_recursive(&o, bases, parent1, parent2, &res);
992 /* Show the diff */
993 setup_additional_headers(&opt->diffopt, res.path_messages);
994 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
995 log_tree_diff_flush(opt);
997 /* Cleanup */
998 cleanup_additional_headers(&opt->diffopt);
999 strbuf_release(&parent1_desc);
1000 strbuf_release(&parent2_desc);
1001 merge_finalize(&o, &res);
1003 /* Clean up the contents of the temporary object directory */
1004 if (opt->remerge_objdir)
1005 tmp_objdir_discard_objects(opt->remerge_objdir);
1006 else
1007 BUG("did a remerge diff without remerge_objdir?!?");
1009 return !opt->loginfo;
1013 * Show the diff of a commit.
1015 * Return true if we printed any log info messages
1017 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1019 int showed_log;
1020 struct commit_list *parents;
1021 struct object_id *oid;
1022 int is_merge;
1023 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1025 if (!all_need_diff && !opt->merges_need_diff)
1026 return 0;
1028 parse_commit_or_die(commit);
1029 oid = get_commit_tree_oid(commit);
1031 parents = get_saved_parents(opt, commit);
1032 is_merge = parents && parents->next;
1033 if (!is_merge && !all_need_diff)
1034 return 0;
1036 /* Root commit? */
1037 if (!parents) {
1038 if (opt->show_root_diff) {
1039 diff_root_tree_oid(oid, "", &opt->diffopt);
1040 log_tree_diff_flush(opt);
1042 return !opt->loginfo;
1045 if (is_merge) {
1046 int octopus = (parents->next->next != NULL);
1048 if (opt->remerge_diff) {
1049 if (octopus) {
1050 show_log(opt);
1051 fprintf(opt->diffopt.file,
1052 "diff: warning: Skipping remerge-diff "
1053 "for octopus merges.\n");
1054 return 1;
1056 return do_remerge_diff(opt, parents, oid, commit);
1058 if (opt->combine_merges)
1059 return do_diff_combined(opt, commit);
1060 if (opt->separate_merges) {
1061 if (!opt->first_parent_merges) {
1062 /* Show parent info for multiple diffs */
1063 log->parent = parents->item;
1065 } else
1066 return 0;
1069 showed_log = 0;
1070 for (;;) {
1071 struct commit *parent = parents->item;
1073 parse_commit_or_die(parent);
1074 diff_tree_oid(get_commit_tree_oid(parent),
1075 oid, "", &opt->diffopt);
1076 log_tree_diff_flush(opt);
1078 showed_log |= !opt->loginfo;
1080 /* Set up the log info for the next parent, if any.. */
1081 parents = parents->next;
1082 if (!parents || opt->first_parent_merges)
1083 break;
1084 log->parent = parents->item;
1085 opt->loginfo = log;
1087 return showed_log;
1090 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1092 struct log_info log;
1093 int shown;
1094 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1095 int no_free = opt->diffopt.no_free;
1097 log.commit = commit;
1098 log.parent = NULL;
1099 opt->loginfo = &log;
1100 opt->diffopt.no_free = 1;
1102 /* NEEDSWORK: no restoring of no_free? Why? */
1103 if (opt->line_level_traverse)
1104 return line_log_print(opt, commit);
1106 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1107 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1108 shown = log_tree_diff(opt, commit, &log);
1109 if (!shown && opt->loginfo && opt->always_show_header) {
1110 log.parent = NULL;
1111 show_log(opt);
1112 shown = 1;
1114 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1115 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1116 opt->loginfo = NULL;
1117 maybe_flush_or_die(opt->diffopt.file, "stdout");
1118 opt->diffopt.no_free = no_free;
1119 diff_free(&opt->diffopt);
1120 return shown;