Merge branch 'rs/format-patch-pathspec-fix'
[alt-git.git] / log-tree.c
blob3a03e34c305c07533574177a7b5483d038f5290c
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 == NULL) {
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 struct object *obj;
141 enum object_type objtype;
142 enum decoration_type deco_type = DECORATION_NONE;
143 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
145 if (filter && !ref_filter_match(refname, filter))
146 return 0;
148 if (starts_with(refname, git_replace_ref_base)) {
149 struct object_id original_oid;
150 if (!read_replace_refs)
151 return 0;
152 if (get_oid_hex(refname + strlen(git_replace_ref_base),
153 &original_oid)) {
154 warning("invalid replace ref %s", refname);
155 return 0;
157 obj = parse_object(the_repository, &original_oid);
158 if (obj)
159 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
160 return 0;
163 objtype = oid_object_info(the_repository, oid, NULL);
164 if (objtype < 0)
165 return 0;
166 obj = lookup_object_by_type(the_repository, oid, objtype);
168 if (starts_with(refname, "refs/heads/"))
169 deco_type = DECORATION_REF_LOCAL;
170 else if (starts_with(refname, "refs/remotes/"))
171 deco_type = DECORATION_REF_REMOTE;
172 else if (starts_with(refname, "refs/tags/"))
173 deco_type = DECORATION_REF_TAG;
174 else if (!strcmp(refname, "refs/stash"))
175 deco_type = DECORATION_REF_STASH;
176 else if (!strcmp(refname, "HEAD"))
177 deco_type = DECORATION_REF_HEAD;
179 add_name_decoration(deco_type, refname, obj);
180 while (obj->type == OBJ_TAG) {
181 if (!obj->parsed)
182 parse_object(the_repository, &obj->oid);
183 obj = ((struct tag *)obj)->tagged;
184 if (!obj)
185 break;
186 add_name_decoration(DECORATION_REF_TAG, refname, obj);
188 return 0;
191 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
193 struct commit *commit = lookup_commit(the_repository, &graft->oid);
194 if (!commit)
195 return 0;
196 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
197 return 0;
200 void load_ref_decorations(struct decoration_filter *filter, int flags)
202 if (!decoration_loaded) {
203 if (filter) {
204 struct string_list_item *item;
205 for_each_string_list_item(item, filter->exclude_ref_pattern) {
206 normalize_glob_ref(item, NULL, item->string);
208 for_each_string_list_item(item, filter->include_ref_pattern) {
209 normalize_glob_ref(item, NULL, item->string);
211 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
212 normalize_glob_ref(item, NULL, item->string);
215 decoration_loaded = 1;
216 decoration_flags = flags;
217 for_each_ref(add_ref_decoration, filter);
218 head_ref(add_ref_decoration, filter);
219 for_each_commit_graft(add_graft_decoration, filter);
223 static void show_parents(struct commit *commit, int abbrev, FILE *file)
225 struct commit_list *p;
226 for (p = commit->parents; p ; p = p->next) {
227 struct commit *parent = p->item;
228 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
232 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
234 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
235 for ( ; p; p = p->next) {
236 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
241 * Do we have HEAD in the output, and also the branch it points at?
242 * If so, find that decoration entry for that current branch.
244 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
246 const struct name_decoration *list, *head = NULL;
247 const char *branch_name = NULL;
248 int rru_flags;
250 /* First find HEAD */
251 for (list = decoration; list; list = list->next)
252 if (list->type == DECORATION_REF_HEAD) {
253 head = list;
254 break;
256 if (!head)
257 return NULL;
259 /* Now resolve and find the matching current branch */
260 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
261 if (!branch_name || !(rru_flags & REF_ISSYMREF))
262 return NULL;
264 if (!starts_with(branch_name, "refs/"))
265 return NULL;
267 /* OK, do we have that ref in the list? */
268 for (list = decoration; list; list = list->next)
269 if ((list->type == DECORATION_REF_LOCAL) &&
270 !strcmp(branch_name, list->name)) {
271 return list;
274 return NULL;
277 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
279 if (decoration_flags == DECORATE_SHORT_REFS)
280 strbuf_addstr(sb, prettify_refname(decoration->name));
281 else
282 strbuf_addstr(sb, decoration->name);
286 * The caller makes sure there is no funny color before calling.
287 * format_decorations_extended makes sure the same after return.
289 void format_decorations_extended(struct strbuf *sb,
290 const struct commit *commit,
291 int use_color,
292 const char *prefix,
293 const char *separator,
294 const char *suffix)
296 const struct name_decoration *decoration;
297 const struct name_decoration *current_and_HEAD;
298 const char *color_commit =
299 diff_get_color(use_color, DIFF_COMMIT);
300 const char *color_reset =
301 decorate_get_color(use_color, DECORATION_NONE);
303 decoration = get_name_decoration(&commit->object);
304 if (!decoration)
305 return;
307 current_and_HEAD = current_pointed_by_HEAD(decoration);
308 while (decoration) {
310 * When both current and HEAD are there, only
311 * show HEAD->current where HEAD would have
312 * appeared, skipping the entry for current.
314 if (decoration != current_and_HEAD) {
315 strbuf_addstr(sb, color_commit);
316 strbuf_addstr(sb, prefix);
317 strbuf_addstr(sb, color_reset);
318 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
319 if (decoration->type == DECORATION_REF_TAG)
320 strbuf_addstr(sb, "tag: ");
322 show_name(sb, decoration);
324 if (current_and_HEAD &&
325 decoration->type == DECORATION_REF_HEAD) {
326 strbuf_addstr(sb, " -> ");
327 strbuf_addstr(sb, color_reset);
328 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
329 show_name(sb, current_and_HEAD);
331 strbuf_addstr(sb, color_reset);
333 prefix = separator;
335 decoration = decoration->next;
337 strbuf_addstr(sb, color_commit);
338 strbuf_addstr(sb, suffix);
339 strbuf_addstr(sb, color_reset);
342 void show_decorations(struct rev_info *opt, struct commit *commit)
344 struct strbuf sb = STRBUF_INIT;
346 if (opt->sources) {
347 char **slot = revision_sources_peek(opt->sources, commit);
349 if (slot && *slot)
350 fprintf(opt->diffopt.file, "\t%s", *slot);
352 if (!opt->show_decorations)
353 return;
354 format_decorations(&sb, commit, opt->diffopt.use_color);
355 fputs(sb.buf, opt->diffopt.file);
356 strbuf_release(&sb);
359 static unsigned int digits_in_number(unsigned int number)
361 unsigned int i = 10, result = 1;
362 while (i <= number) {
363 i *= 10;
364 result++;
366 return result;
369 void fmt_output_subject(struct strbuf *filename,
370 const char *subject,
371 struct rev_info *info)
373 const char *suffix = info->patch_suffix;
374 int nr = info->nr;
375 int start_len = filename->len;
376 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
378 if (info->reroll_count) {
379 struct strbuf temp = STRBUF_INIT;
381 strbuf_addf(&temp, "v%s", info->reroll_count);
382 format_sanitized_subject(filename, temp.buf, temp.len);
383 strbuf_addstr(filename, "-");
384 strbuf_release(&temp);
386 strbuf_addf(filename, "%04d-%s", nr, subject);
388 if (max_len < filename->len)
389 strbuf_setlen(filename, max_len);
390 strbuf_addstr(filename, suffix);
393 void fmt_output_commit(struct strbuf *filename,
394 struct commit *commit,
395 struct rev_info *info)
397 struct pretty_print_context ctx = {0};
398 struct strbuf subject = STRBUF_INIT;
400 format_commit_message(commit, "%f", &subject, &ctx);
401 fmt_output_subject(filename, subject.buf, info);
402 strbuf_release(&subject);
405 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
407 if (opt->total > 0) {
408 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
409 opt->subject_prefix,
410 *opt->subject_prefix ? " " : "",
411 digits_in_number(opt->total),
412 opt->nr, opt->total);
413 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
414 strbuf_addf(sb, "Subject: [%s] ",
415 opt->subject_prefix);
416 } else {
417 strbuf_addstr(sb, "Subject: ");
421 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
422 const char **extra_headers_p,
423 int *need_8bit_cte_p,
424 int maybe_multipart)
426 const char *extra_headers = opt->extra_headers;
427 const char *name = oid_to_hex(opt->zero_commit ?
428 null_oid() : &commit->object.oid);
430 *need_8bit_cte_p = 0; /* unknown */
432 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
433 graph_show_oneline(opt->graph);
434 if (opt->message_id) {
435 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
436 graph_show_oneline(opt->graph);
438 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
439 int i, n;
440 n = opt->ref_message_ids->nr;
441 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
442 for (i = 0; i < n; i++)
443 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
444 opt->ref_message_ids->items[i].string);
445 graph_show_oneline(opt->graph);
447 if (opt->mime_boundary && maybe_multipart) {
448 static struct strbuf subject_buffer = STRBUF_INIT;
449 static struct strbuf buffer = STRBUF_INIT;
450 struct strbuf filename = STRBUF_INIT;
451 *need_8bit_cte_p = -1; /* NEVER */
453 strbuf_reset(&subject_buffer);
454 strbuf_reset(&buffer);
456 strbuf_addf(&subject_buffer,
457 "%s"
458 "MIME-Version: 1.0\n"
459 "Content-Type: multipart/mixed;"
460 " boundary=\"%s%s\"\n"
461 "\n"
462 "This is a multi-part message in MIME "
463 "format.\n"
464 "--%s%s\n"
465 "Content-Type: text/plain; "
466 "charset=UTF-8; format=fixed\n"
467 "Content-Transfer-Encoding: 8bit\n\n",
468 extra_headers ? extra_headers : "",
469 mime_boundary_leader, opt->mime_boundary,
470 mime_boundary_leader, opt->mime_boundary);
471 extra_headers = subject_buffer.buf;
473 if (opt->numbered_files)
474 strbuf_addf(&filename, "%d", opt->nr);
475 else
476 fmt_output_commit(&filename, commit, opt);
477 strbuf_addf(&buffer,
478 "\n--%s%s\n"
479 "Content-Type: text/x-patch;"
480 " name=\"%s\"\n"
481 "Content-Transfer-Encoding: 8bit\n"
482 "Content-Disposition: %s;"
483 " filename=\"%s\"\n\n",
484 mime_boundary_leader, opt->mime_boundary,
485 filename.buf,
486 opt->no_inline ? "attachment" : "inline",
487 filename.buf);
488 opt->diffopt.stat_sep = buffer.buf;
489 strbuf_release(&filename);
491 *extra_headers_p = extra_headers;
494 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
496 const char *color, *reset, *eol;
498 color = diff_get_color_opt(&opt->diffopt,
499 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
500 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
501 while (*bol) {
502 eol = strchrnul(bol, '\n');
503 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
504 *eol ? "\n" : "");
505 graph_show_oneline(opt->graph);
506 bol = (*eol) ? (eol + 1) : eol;
510 static void show_signature(struct rev_info *opt, struct commit *commit)
512 struct strbuf payload = STRBUF_INIT;
513 struct strbuf signature = STRBUF_INIT;
514 struct signature_check sigc = { 0 };
515 int status;
517 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
518 goto out;
520 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
521 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
522 status = check_signature(&sigc, signature.buf, signature.len);
523 if (status && !sigc.output)
524 show_sig_lines(opt, status, "No signature\n");
525 else
526 show_sig_lines(opt, status, sigc.output);
527 signature_check_clear(&sigc);
529 out:
530 strbuf_release(&payload);
531 strbuf_release(&signature);
534 static int which_parent(const struct object_id *oid, const struct commit *commit)
536 int nth;
537 const struct commit_list *parent;
539 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
540 if (oideq(&parent->item->object.oid, oid))
541 return nth;
542 nth++;
544 return -1;
547 static int is_common_merge(const struct commit *commit)
549 return (commit->parents
550 && commit->parents->next
551 && !commit->parents->next->next);
554 static int show_one_mergetag(struct commit *commit,
555 struct commit_extra_header *extra,
556 void *data)
558 struct rev_info *opt = (struct rev_info *)data;
559 struct object_id oid;
560 struct tag *tag;
561 struct strbuf verify_message;
562 struct signature_check sigc = { 0 };
563 int status, nth;
564 struct strbuf payload = STRBUF_INIT;
565 struct strbuf signature = STRBUF_INIT;
567 hash_object_file(the_hash_algo, extra->value, extra->len,
568 OBJ_TAG, &oid);
569 tag = lookup_tag(the_repository, &oid);
570 if (!tag)
571 return -1; /* error message already given */
573 strbuf_init(&verify_message, 256);
574 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
575 strbuf_addstr(&verify_message, "malformed mergetag\n");
576 else if (is_common_merge(commit) &&
577 oideq(&tag->tagged->oid,
578 &commit->parents->next->item->object.oid))
579 strbuf_addf(&verify_message,
580 "merged tag '%s'\n", tag->tag);
581 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
582 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
583 tag->tag, oid_to_hex(&tag->tagged->oid));
584 else
585 strbuf_addf(&verify_message,
586 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
588 status = -1;
589 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
590 /* could have a good signature */
591 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
592 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
593 status = check_signature(&sigc, signature.buf, signature.len);
594 if (sigc.output)
595 strbuf_addstr(&verify_message, sigc.output);
596 else
597 strbuf_addstr(&verify_message, "No signature\n");
598 signature_check_clear(&sigc);
599 /* otherwise we couldn't verify, which is shown as bad */
602 show_sig_lines(opt, status, verify_message.buf);
603 strbuf_release(&verify_message);
604 strbuf_release(&payload);
605 strbuf_release(&signature);
606 return 0;
609 static int show_mergetag(struct rev_info *opt, struct commit *commit)
611 return for_each_mergetag(show_one_mergetag, commit, opt);
614 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
616 const char *x = opt->shown_dashes ? "\n" : "---\n";
617 if (sb)
618 strbuf_addstr(sb, x);
619 else
620 fputs(x, opt->diffopt.file);
621 opt->shown_dashes = 1;
624 void show_log(struct rev_info *opt)
626 struct strbuf msgbuf = STRBUF_INIT;
627 struct log_info *log = opt->loginfo;
628 struct commit *commit = log->commit, *parent = log->parent;
629 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
630 const char *extra_headers = opt->extra_headers;
631 struct pretty_print_context ctx = {0};
633 opt->loginfo = NULL;
634 if (!opt->verbose_header) {
635 graph_show_commit(opt->graph);
637 if (!opt->graph)
638 put_revision_mark(opt, commit);
639 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
640 if (opt->print_parents)
641 show_parents(commit, abbrev_commit, opt->diffopt.file);
642 if (opt->children.name)
643 show_children(opt, commit, abbrev_commit);
644 show_decorations(opt, commit);
645 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
646 putc('\n', opt->diffopt.file);
647 graph_show_remainder(opt->graph);
649 putc(opt->diffopt.line_termination, opt->diffopt.file);
650 return;
654 * If use_terminator is set, we already handled any record termination
655 * at the end of the last record.
656 * Otherwise, add a diffopt.line_termination character before all
657 * entries but the first. (IOW, as a separator between entries)
659 if (opt->shown_one && !opt->use_terminator) {
661 * If entries are separated by a newline, the output
662 * should look human-readable. If the last entry ended
663 * with a newline, print the graph output before this
664 * newline. Otherwise it will end up as a completely blank
665 * line and will look like a gap in the graph.
667 * If the entry separator is not a newline, the output is
668 * primarily intended for programmatic consumption, and we
669 * never want the extra graph output before the entry
670 * separator.
672 if (opt->diffopt.line_termination == '\n' &&
673 !opt->missing_newline)
674 graph_show_padding(opt->graph);
675 putc(opt->diffopt.line_termination, opt->diffopt.file);
677 opt->shown_one = 1;
680 * If the history graph was requested,
681 * print the graph, up to this commit's line
683 graph_show_commit(opt->graph);
686 * Print header line of header..
689 if (cmit_fmt_is_mail(opt->commit_format)) {
690 log_write_email_headers(opt, commit, &extra_headers,
691 &ctx.need_8bit_cte, 1);
692 ctx.rev = opt;
693 ctx.print_email_subject = 1;
694 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
695 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
696 if (opt->commit_format != CMIT_FMT_ONELINE)
697 fputs("commit ", opt->diffopt.file);
699 if (!opt->graph)
700 put_revision_mark(opt, commit);
701 fputs(find_unique_abbrev(&commit->object.oid,
702 abbrev_commit),
703 opt->diffopt.file);
704 if (opt->print_parents)
705 show_parents(commit, abbrev_commit, opt->diffopt.file);
706 if (opt->children.name)
707 show_children(opt, commit, abbrev_commit);
708 if (parent)
709 fprintf(opt->diffopt.file, " (from %s)",
710 find_unique_abbrev(&parent->object.oid, abbrev_commit));
711 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
712 show_decorations(opt, commit);
713 if (opt->commit_format == CMIT_FMT_ONELINE) {
714 putc(' ', opt->diffopt.file);
715 } else {
716 putc('\n', opt->diffopt.file);
717 graph_show_oneline(opt->graph);
719 if (opt->reflog_info) {
721 * setup_revisions() ensures that opt->reflog_info
722 * and opt->graph cannot both be set,
723 * so we don't need to worry about printing the
724 * graph info here.
726 show_reflog_message(opt->reflog_info,
727 opt->commit_format == CMIT_FMT_ONELINE,
728 &opt->date_mode,
729 opt->date_mode_explicit);
730 if (opt->commit_format == CMIT_FMT_ONELINE)
731 return;
735 if (opt->show_signature) {
736 show_signature(opt, commit);
737 show_mergetag(opt, commit);
740 if (opt->show_notes) {
741 int raw;
742 struct strbuf notebuf = STRBUF_INIT;
744 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
745 format_display_notes(&commit->object.oid, &notebuf,
746 get_log_output_encoding(), raw);
747 ctx.notes_message = strbuf_detach(&notebuf, NULL);
751 * And then the pretty-printed message itself
753 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
754 ctx.need_8bit_cte =
755 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
756 ctx.date_mode = opt->date_mode;
757 ctx.date_mode_explicit = opt->date_mode_explicit;
758 ctx.abbrev = opt->diffopt.abbrev;
759 ctx.after_subject = extra_headers;
760 ctx.preserve_subject = opt->preserve_subject;
761 ctx.encode_email_headers = opt->encode_email_headers;
762 ctx.reflog_info = opt->reflog_info;
763 ctx.fmt = opt->commit_format;
764 ctx.mailmap = opt->mailmap;
765 ctx.color = opt->diffopt.use_color;
766 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
767 ctx.output_encoding = get_log_output_encoding();
768 ctx.rev = opt;
769 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
770 ctx.from_ident = &opt->from_ident;
771 if (opt->graph)
772 ctx.graph_width = graph_width(opt->graph);
773 pretty_print_commit(&ctx, commit, &msgbuf);
775 if (opt->add_signoff)
776 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
778 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
779 ctx.notes_message && *ctx.notes_message) {
780 if (cmit_fmt_is_mail(ctx.fmt))
781 next_commentary_block(opt, &msgbuf);
782 strbuf_addstr(&msgbuf, ctx.notes_message);
785 if (opt->show_log_size) {
786 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
787 graph_show_oneline(opt->graph);
791 * Set opt->missing_newline if msgbuf doesn't
792 * end in a newline (including if it is empty)
794 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
795 opt->missing_newline = 1;
796 else
797 opt->missing_newline = 0;
799 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
800 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
801 if (!opt->missing_newline)
802 graph_show_padding(opt->graph);
803 putc(opt->diffopt.line_termination, opt->diffopt.file);
806 strbuf_release(&msgbuf);
807 free(ctx.notes_message);
809 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
810 struct diff_queue_struct dq;
812 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
813 DIFF_QUEUE_CLEAR(&diff_queued_diff);
815 next_commentary_block(opt, NULL);
816 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
817 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
818 &opt->diffopt);
820 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
823 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
824 struct diff_queue_struct dq;
825 struct diff_options opts;
826 struct range_diff_options range_diff_opts = {
827 .creation_factor = opt->creation_factor,
828 .dual_color = 1,
829 .diffopt = &opts
832 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
833 DIFF_QUEUE_CLEAR(&diff_queued_diff);
835 next_commentary_block(opt, NULL);
836 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
838 * Pass minimum required diff-options to range-diff; others
839 * can be added later if deemed desirable.
841 diff_setup(&opts);
842 opts.file = opt->diffopt.file;
843 opts.use_color = opt->diffopt.use_color;
844 diff_setup_done(&opts);
845 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
847 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
851 int log_tree_diff_flush(struct rev_info *opt)
853 opt->shown_dashes = 0;
854 diffcore_std(&opt->diffopt);
856 if (diff_queue_is_empty(&opt->diffopt)) {
857 int saved_fmt = opt->diffopt.output_format;
858 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
859 diff_flush(&opt->diffopt);
860 opt->diffopt.output_format = saved_fmt;
861 return 0;
864 if (opt->loginfo && !opt->no_commit_id) {
865 show_log(opt);
866 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
867 opt->verbose_header &&
868 opt->commit_format != CMIT_FMT_ONELINE &&
869 !commit_format_is_empty(opt->commit_format)) {
871 * When showing a verbose header (i.e. log message),
872 * and not in --pretty=oneline format, we would want
873 * an extra newline between the end of log and the
874 * diff/diffstat output for readability.
876 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
877 if (opt->diffopt.output_prefix) {
878 struct strbuf *msg = NULL;
879 msg = opt->diffopt.output_prefix(&opt->diffopt,
880 opt->diffopt.output_prefix_data);
881 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
885 * We may have shown three-dashes line early
886 * between generated commentary (notes, etc.)
887 * and the log message, in which case we only
888 * want a blank line after the commentary
889 * without (an extra) three-dashes line.
890 * Otherwise, we show the three-dashes line if
891 * we are showing the patch with diffstat, but
892 * in that case, there is no extra blank line
893 * after the three-dashes line.
895 if (!opt->shown_dashes &&
896 (pch & opt->diffopt.output_format) == pch)
897 fprintf(opt->diffopt.file, "---");
898 putc('\n', opt->diffopt.file);
901 diff_flush(&opt->diffopt);
902 return 1;
905 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
907 diff_tree_combined_merge(commit, opt);
908 return !opt->loginfo;
911 static void setup_additional_headers(struct diff_options *o,
912 struct strmap *all_headers)
914 struct hashmap_iter iter;
915 struct strmap_entry *entry;
918 * Make o->additional_path_headers contain the subset of all_headers
919 * that match o->pathspec. If there aren't any that match o->pathspec,
920 * then make o->additional_path_headers be NULL.
923 if (!o->pathspec.nr) {
924 o->additional_path_headers = all_headers;
925 return;
928 o->additional_path_headers = xmalloc(sizeof(struct strmap));
929 strmap_init_with_options(o->additional_path_headers, NULL, 0);
930 strmap_for_each_entry(all_headers, &iter, entry) {
931 if (match_pathspec(the_repository->index, &o->pathspec,
932 entry->key, strlen(entry->key),
933 0 /* prefix */, NULL /* seen */,
934 0 /* is_dir */))
935 strmap_put(o->additional_path_headers,
936 entry->key, entry->value);
938 if (!strmap_get_size(o->additional_path_headers)) {
939 strmap_clear(o->additional_path_headers, 0);
940 FREE_AND_NULL(o->additional_path_headers);
944 static void cleanup_additional_headers(struct diff_options *o)
946 if (!o->pathspec.nr) {
947 o->additional_path_headers = NULL;
948 return;
950 if (!o->additional_path_headers)
951 return;
953 strmap_clear(o->additional_path_headers, 0);
954 FREE_AND_NULL(o->additional_path_headers);
957 static int do_remerge_diff(struct rev_info *opt,
958 struct commit_list *parents,
959 struct object_id *oid,
960 struct commit *commit)
962 struct merge_options o;
963 struct commit_list *bases;
964 struct merge_result res = {0};
965 struct pretty_print_context ctx = {0};
966 struct commit *parent1 = parents->item;
967 struct commit *parent2 = parents->next->item;
968 struct strbuf parent1_desc = STRBUF_INIT;
969 struct strbuf parent2_desc = STRBUF_INIT;
971 /* Setup merge options */
972 init_merge_options(&o, the_repository);
973 o.show_rename_progress = 0;
974 o.record_conflict_msgs_as_headers = 1;
975 o.msg_header_prefix = "remerge";
977 ctx.abbrev = DEFAULT_ABBREV;
978 format_commit_message(parent1, "%h (%s)", &parent1_desc, &ctx);
979 format_commit_message(parent2, "%h (%s)", &parent2_desc, &ctx);
980 o.branch1 = parent1_desc.buf;
981 o.branch2 = parent2_desc.buf;
983 /* Parse the relevant commits and get the merge bases */
984 parse_commit_or_die(parent1);
985 parse_commit_or_die(parent2);
986 bases = get_merge_bases(parent1, parent2);
988 /* Re-merge the parents */
989 merge_incore_recursive(&o, bases, parent1, parent2, &res);
991 /* Show the diff */
992 setup_additional_headers(&opt->diffopt, res.path_messages);
993 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
994 log_tree_diff_flush(opt);
996 /* Cleanup */
997 cleanup_additional_headers(&opt->diffopt);
998 strbuf_release(&parent1_desc);
999 strbuf_release(&parent2_desc);
1000 merge_finalize(&o, &res);
1002 /* Clean up the contents of the temporary object directory */
1003 if (opt->remerge_objdir)
1004 tmp_objdir_discard_objects(opt->remerge_objdir);
1005 else
1006 BUG("did a remerge diff without remerge_objdir?!?");
1008 return !opt->loginfo;
1012 * Show the diff of a commit.
1014 * Return true if we printed any log info messages
1016 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1018 int showed_log;
1019 struct commit_list *parents;
1020 struct object_id *oid;
1021 int is_merge;
1022 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1024 if (!all_need_diff && !opt->merges_need_diff)
1025 return 0;
1027 parse_commit_or_die(commit);
1028 oid = get_commit_tree_oid(commit);
1030 parents = get_saved_parents(opt, commit);
1031 is_merge = parents && parents->next;
1032 if (!is_merge && !all_need_diff)
1033 return 0;
1035 /* Root commit? */
1036 if (!parents) {
1037 if (opt->show_root_diff) {
1038 diff_root_tree_oid(oid, "", &opt->diffopt);
1039 log_tree_diff_flush(opt);
1041 return !opt->loginfo;
1044 if (is_merge) {
1045 int octopus = (parents->next->next != NULL);
1047 if (opt->remerge_diff) {
1048 if (octopus) {
1049 show_log(opt);
1050 fprintf(opt->diffopt.file,
1051 "diff: warning: Skipping remerge-diff "
1052 "for octopus merges.\n");
1053 return 1;
1055 return do_remerge_diff(opt, parents, oid, commit);
1057 if (opt->combine_merges)
1058 return do_diff_combined(opt, commit);
1059 if (opt->separate_merges) {
1060 if (!opt->first_parent_merges) {
1061 /* Show parent info for multiple diffs */
1062 log->parent = parents->item;
1064 } else
1065 return 0;
1068 showed_log = 0;
1069 for (;;) {
1070 struct commit *parent = parents->item;
1072 parse_commit_or_die(parent);
1073 diff_tree_oid(get_commit_tree_oid(parent),
1074 oid, "", &opt->diffopt);
1075 log_tree_diff_flush(opt);
1077 showed_log |= !opt->loginfo;
1079 /* Set up the log info for the next parent, if any.. */
1080 parents = parents->next;
1081 if (!parents || opt->first_parent_merges)
1082 break;
1083 log->parent = parents->item;
1084 opt->loginfo = log;
1086 return showed_log;
1089 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1091 struct log_info log;
1092 int shown;
1093 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1094 int no_free = opt->diffopt.no_free;
1096 log.commit = commit;
1097 log.parent = NULL;
1098 opt->loginfo = &log;
1099 opt->diffopt.no_free = 1;
1101 /* NEEDSWORK: no restoring of no_free? Why? */
1102 if (opt->line_level_traverse)
1103 return line_log_print(opt, commit);
1105 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1106 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1107 shown = log_tree_diff(opt, commit, &log);
1108 if (!shown && opt->loginfo && opt->always_show_header) {
1109 log.parent = NULL;
1110 show_log(opt);
1111 shown = 1;
1113 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1114 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1115 opt->loginfo = NULL;
1116 maybe_flush_or_die(opt->diffopt.file, "stdout");
1117 opt->diffopt.no_free = no_free;
1118 diff_free(&opt->diffopt);
1119 return shown;