Merge branch 'pw/rebase-i-error-message'
[git/debian.git] / log-tree.c
blob7de744911e8e068ab0fb486718b980dea4f43c5d
1 #include "git-compat-util.h"
2 #include "commit-reach.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "diffcore.h"
6 #include "environment.h"
7 #include "hex.h"
8 #include "object-name.h"
9 #include "object-store-ll.h"
10 #include "repository.h"
11 #include "tmp-objdir.h"
12 #include "commit.h"
13 #include "tag.h"
14 #include "graph.h"
15 #include "log-tree.h"
16 #include "merge-ort.h"
17 #include "reflog-walk.h"
18 #include "refs.h"
19 #include "replace-object.h"
20 #include "revision.h"
21 #include "string-list.h"
22 #include "color.h"
23 #include "gpg-interface.h"
24 #include "sequencer.h"
25 #include "line-log.h"
26 #include "help.h"
27 #include "range-diff.h"
28 #include "strmap.h"
29 #include "tree.h"
30 #include "wildmatch.h"
31 #include "write-or-die.h"
33 static struct decoration name_decoration = { "object names" };
34 static int decoration_loaded;
35 static int decoration_flags;
37 static char decoration_colors[][COLOR_MAXLEN] = {
38 GIT_COLOR_RESET,
39 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
40 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
41 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
42 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
43 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
44 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
47 static const char *color_decorate_slots[] = {
48 [DECORATION_REF_LOCAL] = "branch",
49 [DECORATION_REF_REMOTE] = "remoteBranch",
50 [DECORATION_REF_TAG] = "tag",
51 [DECORATION_REF_STASH] = "stash",
52 [DECORATION_REF_HEAD] = "HEAD",
53 [DECORATION_GRAFTED] = "grafted",
56 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
58 if (want_color(decorate_use_color))
59 return decoration_colors[ix];
60 return "";
63 define_list_config_array(color_decorate_slots);
65 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
67 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
68 if (slot < 0)
69 return 0;
70 if (!value)
71 return config_error_nonbool(var);
72 return color_parse(value, decoration_colors[slot]);
76 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
77 * for showing the commit sha1, use the same check for --decorate
79 #define decorate_get_color_opt(o, ix) \
80 decorate_get_color((o)->use_color, ix)
82 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
84 struct name_decoration *res;
85 FLEX_ALLOC_STR(res, name, name);
86 res->type = type;
87 res->next = add_decoration(&name_decoration, obj, res);
90 const struct name_decoration *get_name_decoration(const struct object *obj)
92 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
93 return lookup_decoration(&name_decoration, obj);
96 static int match_ref_pattern(const char *refname,
97 const struct string_list_item *item)
99 int matched = 0;
100 if (!item->util) {
101 if (!wildmatch(item->string, refname, 0))
102 matched = 1;
103 } else {
104 const char *rest;
105 if (skip_prefix(refname, item->string, &rest) &&
106 (!*rest || *rest == '/'))
107 matched = 1;
109 return matched;
112 static int ref_filter_match(const char *refname,
113 const struct decoration_filter *filter)
115 struct string_list_item *item;
116 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
117 const struct string_list *include_patterns = filter->include_ref_pattern;
118 const struct string_list *exclude_patterns_config =
119 filter->exclude_ref_config_pattern;
121 if (exclude_patterns && exclude_patterns->nr) {
122 for_each_string_list_item(item, exclude_patterns) {
123 if (match_ref_pattern(refname, item))
124 return 0;
128 if (include_patterns && include_patterns->nr) {
129 for_each_string_list_item(item, include_patterns) {
130 if (match_ref_pattern(refname, item))
131 return 1;
133 return 0;
136 if (exclude_patterns_config && exclude_patterns_config->nr) {
137 for_each_string_list_item(item, exclude_patterns_config) {
138 if (match_ref_pattern(refname, item))
139 return 0;
143 return 1;
146 static int add_ref_decoration(const char *refname, const struct object_id *oid,
147 int flags UNUSED,
148 void *cb_data)
150 int i;
151 struct object *obj;
152 enum object_type objtype;
153 enum decoration_type deco_type = DECORATION_NONE;
154 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
155 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
157 if (filter && !ref_filter_match(refname, filter))
158 return 0;
160 if (starts_with(refname, git_replace_ref_base)) {
161 struct object_id original_oid;
162 if (!replace_refs_enabled(the_repository))
163 return 0;
164 if (get_oid_hex(refname + strlen(git_replace_ref_base),
165 &original_oid)) {
166 warning("invalid replace ref %s", refname);
167 return 0;
169 obj = parse_object(the_repository, &original_oid);
170 if (obj)
171 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
172 return 0;
175 objtype = oid_object_info(the_repository, oid, NULL);
176 if (objtype < 0)
177 return 0;
178 obj = lookup_object_by_type(the_repository, oid, objtype);
180 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
181 struct ref_namespace_info *info = &ref_namespace[i];
183 if (!info->decoration)
184 continue;
185 if (info->exact) {
186 if (!strcmp(refname, info->ref)) {
187 deco_type = info->decoration;
188 break;
190 } else if (starts_with(refname, info->ref)) {
191 deco_type = info->decoration;
192 break;
196 add_name_decoration(deco_type, refname, obj);
197 while (obj->type == OBJ_TAG) {
198 if (!obj->parsed)
199 parse_object(the_repository, &obj->oid);
200 obj = ((struct tag *)obj)->tagged;
201 if (!obj)
202 break;
203 add_name_decoration(DECORATION_REF_TAG, refname, obj);
205 return 0;
208 static int add_graft_decoration(const struct commit_graft *graft,
209 void *cb_data UNUSED)
211 struct commit *commit = lookup_commit(the_repository, &graft->oid);
212 if (!commit)
213 return 0;
214 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
215 return 0;
218 void load_ref_decorations(struct decoration_filter *filter, int flags)
220 if (!decoration_loaded) {
221 if (filter) {
222 struct string_list_item *item;
223 for_each_string_list_item(item, filter->exclude_ref_pattern) {
224 normalize_glob_ref(item, NULL, item->string);
226 for_each_string_list_item(item, filter->include_ref_pattern) {
227 normalize_glob_ref(item, NULL, item->string);
229 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
230 normalize_glob_ref(item, NULL, item->string);
233 decoration_loaded = 1;
234 decoration_flags = flags;
235 refs_for_each_ref(get_main_ref_store(the_repository),
236 add_ref_decoration, filter);
237 refs_head_ref(get_main_ref_store(the_repository),
238 add_ref_decoration, filter);
239 for_each_commit_graft(add_graft_decoration, filter);
243 static void show_parents(struct commit *commit, int abbrev, FILE *file)
245 struct commit_list *p;
246 for (p = commit->parents; p ; p = p->next) {
247 struct commit *parent = p->item;
248 fprintf(file, " %s",
249 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
253 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
255 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
256 for ( ; p; p = p->next) {
257 fprintf(opt->diffopt.file, " %s",
258 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
263 * Do we have HEAD in the output, and also the branch it points at?
264 * If so, find that decoration entry for that current branch.
266 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
268 const struct name_decoration *list, *head = NULL;
269 const char *branch_name = NULL;
270 int rru_flags;
272 /* First find HEAD */
273 for (list = decoration; list; list = list->next)
274 if (list->type == DECORATION_REF_HEAD) {
275 head = list;
276 break;
278 if (!head)
279 return NULL;
281 /* Now resolve and find the matching current branch */
282 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
283 "HEAD", 0, NULL, &rru_flags);
284 if (!branch_name || !(rru_flags & REF_ISSYMREF))
285 return NULL;
287 if (!starts_with(branch_name, "refs/"))
288 return NULL;
290 /* OK, do we have that ref in the list? */
291 for (list = decoration; list; list = list->next)
292 if ((list->type == DECORATION_REF_LOCAL) &&
293 !strcmp(branch_name, list->name)) {
294 return list;
297 return NULL;
300 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
302 if (decoration_flags == DECORATE_SHORT_REFS)
303 strbuf_addstr(sb, prettify_refname(decoration->name));
304 else
305 strbuf_addstr(sb, decoration->name);
309 * The caller makes sure there is no funny color before calling.
310 * format_decorations ensures the same after return.
312 void format_decorations(struct strbuf *sb,
313 const struct commit *commit,
314 int use_color,
315 const struct decoration_options *opts)
317 const struct name_decoration *decoration;
318 const struct name_decoration *current_and_HEAD;
319 const char *color_commit, *color_reset;
321 const char *prefix = " (";
322 const char *suffix = ")";
323 const char *separator = ", ";
324 const char *pointer = " -> ";
325 const char *tag = "tag: ";
327 decoration = get_name_decoration(&commit->object);
328 if (!decoration)
329 return;
331 if (opts) {
332 if (opts->prefix)
333 prefix = opts->prefix;
334 if (opts->suffix)
335 suffix = opts->suffix;
336 if (opts->separator)
337 separator = opts->separator;
338 if (opts->pointer)
339 pointer = opts->pointer;
340 if (opts->tag)
341 tag = opts->tag;
344 color_commit = diff_get_color(use_color, DIFF_COMMIT);
345 color_reset = decorate_get_color(use_color, DECORATION_NONE);
347 current_and_HEAD = current_pointed_by_HEAD(decoration);
348 while (decoration) {
350 * When both current and HEAD are there, only
351 * show HEAD->current where HEAD would have
352 * appeared, skipping the entry for current.
354 if (decoration != current_and_HEAD) {
355 const char *color =
356 decorate_get_color(use_color, decoration->type);
358 if (*prefix) {
359 strbuf_addstr(sb, color_commit);
360 strbuf_addstr(sb, prefix);
361 strbuf_addstr(sb, color_reset);
364 if (*tag && decoration->type == DECORATION_REF_TAG) {
365 strbuf_addstr(sb, color);
366 strbuf_addstr(sb, tag);
367 strbuf_addstr(sb, color_reset);
370 strbuf_addstr(sb, color);
371 show_name(sb, decoration);
372 strbuf_addstr(sb, color_reset);
374 if (current_and_HEAD &&
375 decoration->type == DECORATION_REF_HEAD) {
376 strbuf_addstr(sb, color_commit);
377 strbuf_addstr(sb, pointer);
378 strbuf_addstr(sb, color_reset);
379 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
380 show_name(sb, current_and_HEAD);
381 strbuf_addstr(sb, color_reset);
384 prefix = separator;
386 decoration = decoration->next;
388 if (*suffix) {
389 strbuf_addstr(sb, color_commit);
390 strbuf_addstr(sb, suffix);
391 strbuf_addstr(sb, color_reset);
395 void show_decorations(struct rev_info *opt, struct commit *commit)
397 struct strbuf sb = STRBUF_INIT;
399 if (opt->sources) {
400 char **slot = revision_sources_peek(opt->sources, commit);
402 if (slot && *slot)
403 fprintf(opt->diffopt.file, "\t%s", *slot);
405 if (!opt->show_decorations)
406 return;
407 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
408 fputs(sb.buf, opt->diffopt.file);
409 strbuf_release(&sb);
412 static unsigned int digits_in_number(unsigned int number)
414 unsigned int i = 10, result = 1;
415 while (i <= number) {
416 i *= 10;
417 result++;
419 return result;
422 void fmt_output_subject(struct strbuf *filename,
423 const char *subject,
424 struct rev_info *info)
426 const char *suffix = info->patch_suffix;
427 int nr = info->nr;
428 int start_len = filename->len;
429 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
431 if (info->reroll_count) {
432 struct strbuf temp = STRBUF_INIT;
434 strbuf_addf(&temp, "v%s", info->reroll_count);
435 format_sanitized_subject(filename, temp.buf, temp.len);
436 strbuf_addstr(filename, "-");
437 strbuf_release(&temp);
439 strbuf_addf(filename, "%04d-%s", nr, subject);
441 if (max_len < filename->len)
442 strbuf_setlen(filename, max_len);
443 strbuf_addstr(filename, suffix);
446 void fmt_output_commit(struct strbuf *filename,
447 struct commit *commit,
448 struct rev_info *info)
450 struct pretty_print_context ctx = {0};
451 struct strbuf subject = STRBUF_INIT;
453 repo_format_commit_message(the_repository, commit, "%f", &subject,
454 &ctx);
455 fmt_output_subject(filename, subject.buf, info);
456 strbuf_release(&subject);
459 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
461 if (opt->total > 0) {
462 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
463 opt->subject_prefix,
464 *opt->subject_prefix ? " " : "",
465 digits_in_number(opt->total),
466 opt->nr, opt->total);
467 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
468 strbuf_addf(sb, "Subject: [%s] ",
469 opt->subject_prefix);
470 } else {
471 strbuf_addstr(sb, "Subject: ");
475 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
476 char **extra_headers_p,
477 int *need_8bit_cte_p,
478 int maybe_multipart)
480 struct strbuf headers = STRBUF_INIT;
481 const char *name = oid_to_hex(opt->zero_commit ?
482 null_oid() : &commit->object.oid);
484 *need_8bit_cte_p = 0; /* unknown */
486 if (opt->extra_headers && *opt->extra_headers)
487 strbuf_addstr(&headers, opt->extra_headers);
489 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
490 graph_show_oneline(opt->graph);
491 if (opt->message_id) {
492 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
493 graph_show_oneline(opt->graph);
495 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
496 int i, n;
497 n = opt->ref_message_ids->nr;
498 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
499 for (i = 0; i < n; i++)
500 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
501 opt->ref_message_ids->items[i].string);
502 graph_show_oneline(opt->graph);
504 if (opt->mime_boundary && maybe_multipart) {
505 static struct strbuf buffer = STRBUF_INIT;
506 struct strbuf filename = STRBUF_INIT;
507 *need_8bit_cte_p = -1; /* NEVER */
509 strbuf_reset(&buffer);
511 strbuf_addf(&headers,
512 "MIME-Version: 1.0\n"
513 "Content-Type: multipart/mixed;"
514 " boundary=\"%s%s\"\n"
515 "\n"
516 "This is a multi-part message in MIME "
517 "format.\n"
518 "--%s%s\n"
519 "Content-Type: text/plain; "
520 "charset=UTF-8; format=fixed\n"
521 "Content-Transfer-Encoding: 8bit\n\n",
522 mime_boundary_leader, opt->mime_boundary,
523 mime_boundary_leader, opt->mime_boundary);
525 if (opt->numbered_files)
526 strbuf_addf(&filename, "%d", opt->nr);
527 else
528 fmt_output_commit(&filename, commit, opt);
529 strbuf_addf(&buffer,
530 "\n--%s%s\n"
531 "Content-Type: text/x-patch;"
532 " name=\"%s\"\n"
533 "Content-Transfer-Encoding: 8bit\n"
534 "Content-Disposition: %s;"
535 " filename=\"%s\"\n\n",
536 mime_boundary_leader, opt->mime_boundary,
537 filename.buf,
538 opt->no_inline ? "attachment" : "inline",
539 filename.buf);
540 opt->diffopt.stat_sep = buffer.buf;
541 strbuf_release(&filename);
543 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
546 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
548 const char *color, *reset, *eol;
550 color = diff_get_color_opt(&opt->diffopt,
551 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
552 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
553 while (*bol) {
554 eol = strchrnul(bol, '\n');
555 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
556 *eol ? "\n" : "");
557 graph_show_oneline(opt->graph);
558 bol = (*eol) ? (eol + 1) : eol;
562 static void show_signature(struct rev_info *opt, struct commit *commit)
564 struct strbuf payload = STRBUF_INIT;
565 struct strbuf signature = STRBUF_INIT;
566 struct signature_check sigc = { 0 };
567 int status;
569 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
570 goto out;
572 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
573 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
574 status = check_signature(&sigc, signature.buf, signature.len);
575 if (status && !sigc.output)
576 show_sig_lines(opt, status, "No signature\n");
577 else
578 show_sig_lines(opt, status, sigc.output);
579 signature_check_clear(&sigc);
581 out:
582 strbuf_release(&payload);
583 strbuf_release(&signature);
586 static int which_parent(const struct object_id *oid, const struct commit *commit)
588 int nth;
589 const struct commit_list *parent;
591 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
592 if (oideq(&parent->item->object.oid, oid))
593 return nth;
594 nth++;
596 return -1;
599 static int is_common_merge(const struct commit *commit)
601 return (commit->parents
602 && commit->parents->next
603 && !commit->parents->next->next);
606 static int show_one_mergetag(struct commit *commit,
607 struct commit_extra_header *extra,
608 void *data)
610 struct rev_info *opt = (struct rev_info *)data;
611 struct object_id oid;
612 struct tag *tag;
613 struct strbuf verify_message;
614 struct signature_check sigc = { 0 };
615 int status, nth;
616 struct strbuf payload = STRBUF_INIT;
617 struct strbuf signature = STRBUF_INIT;
619 hash_object_file(the_hash_algo, extra->value, extra->len,
620 OBJ_TAG, &oid);
621 tag = lookup_tag(the_repository, &oid);
622 if (!tag)
623 return -1; /* error message already given */
625 strbuf_init(&verify_message, 256);
626 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
627 strbuf_addstr(&verify_message, "malformed mergetag\n");
628 else if (is_common_merge(commit) &&
629 oideq(&tag->tagged->oid,
630 &commit->parents->next->item->object.oid))
631 strbuf_addf(&verify_message,
632 "merged tag '%s'\n", tag->tag);
633 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
634 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
635 tag->tag, oid_to_hex(&tag->tagged->oid));
636 else
637 strbuf_addf(&verify_message,
638 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
640 status = -1;
641 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
642 /* could have a good signature */
643 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
644 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
645 status = check_signature(&sigc, signature.buf, signature.len);
646 if (sigc.output)
647 strbuf_addstr(&verify_message, sigc.output);
648 else
649 strbuf_addstr(&verify_message, "No signature\n");
650 signature_check_clear(&sigc);
651 /* otherwise we couldn't verify, which is shown as bad */
654 show_sig_lines(opt, status, verify_message.buf);
655 strbuf_release(&verify_message);
656 strbuf_release(&payload);
657 strbuf_release(&signature);
658 return 0;
661 static int show_mergetag(struct rev_info *opt, struct commit *commit)
663 return for_each_mergetag(show_one_mergetag, commit, opt);
666 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
668 const char *x = opt->shown_dashes ? "\n" : "---\n";
669 if (sb)
670 strbuf_addstr(sb, x);
671 else
672 fputs(x, opt->diffopt.file);
673 opt->shown_dashes = 1;
676 static void show_diff_of_diff(struct rev_info *opt)
678 if (!cmit_fmt_is_mail(opt->commit_format))
679 return;
681 if (opt->idiff_oid1) {
682 struct diff_queue_struct dq;
684 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
685 DIFF_QUEUE_CLEAR(&diff_queued_diff);
687 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
688 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
689 &opt->diffopt);
691 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
694 if (opt->rdiff1) {
695 struct diff_queue_struct dq;
696 struct diff_options opts;
697 struct range_diff_options range_diff_opts = {
698 .creation_factor = opt->creation_factor,
699 .dual_color = 1,
700 .diffopt = &opts
703 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
704 DIFF_QUEUE_CLEAR(&diff_queued_diff);
706 fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
708 * Pass minimum required diff-options to range-diff; others
709 * can be added later if deemed desirable.
711 repo_diff_setup(the_repository, &opts);
712 opts.file = opt->diffopt.file;
713 opts.use_color = opt->diffopt.use_color;
714 diff_setup_done(&opts);
715 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
717 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
721 void show_log(struct rev_info *opt)
723 struct strbuf msgbuf = STRBUF_INIT;
724 struct log_info *log = opt->loginfo;
725 struct commit *commit = log->commit, *parent = log->parent;
726 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
727 struct pretty_print_context ctx = {0};
729 opt->loginfo = NULL;
730 if (!opt->verbose_header) {
731 graph_show_commit(opt->graph);
733 if (!opt->graph)
734 put_revision_mark(opt, commit);
735 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
736 opt->diffopt.file);
737 if (opt->print_parents)
738 show_parents(commit, abbrev_commit, opt->diffopt.file);
739 if (opt->children.name)
740 show_children(opt, commit, abbrev_commit);
741 show_decorations(opt, commit);
742 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
743 putc('\n', opt->diffopt.file);
744 graph_show_remainder(opt->graph);
746 putc(opt->diffopt.line_termination, opt->diffopt.file);
747 return;
751 * If use_terminator is set, we already handled any record termination
752 * at the end of the last record.
753 * Otherwise, add a diffopt.line_termination character before all
754 * entries but the first. (IOW, as a separator between entries)
756 if (opt->shown_one && !opt->use_terminator) {
758 * If entries are separated by a newline, the output
759 * should look human-readable. If the last entry ended
760 * with a newline, print the graph output before this
761 * newline. Otherwise it will end up as a completely blank
762 * line and will look like a gap in the graph.
764 * If the entry separator is not a newline, the output is
765 * primarily intended for programmatic consumption, and we
766 * never want the extra graph output before the entry
767 * separator.
769 if (opt->diffopt.line_termination == '\n' &&
770 !opt->missing_newline)
771 graph_show_padding(opt->graph);
772 putc(opt->diffopt.line_termination, opt->diffopt.file);
774 opt->shown_one = 1;
777 * If the history graph was requested,
778 * print the graph, up to this commit's line
780 graph_show_commit(opt->graph);
783 * Print header line of header..
786 if (cmit_fmt_is_mail(opt->commit_format)) {
787 log_write_email_headers(opt, commit, &ctx.after_subject,
788 &ctx.need_8bit_cte, 1);
789 ctx.rev = opt;
790 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
791 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
792 if (opt->commit_format != CMIT_FMT_ONELINE)
793 fputs("commit ", opt->diffopt.file);
795 if (!opt->graph)
796 put_revision_mark(opt, commit);
797 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
798 abbrev_commit),
799 opt->diffopt.file);
800 if (opt->print_parents)
801 show_parents(commit, abbrev_commit, opt->diffopt.file);
802 if (opt->children.name)
803 show_children(opt, commit, abbrev_commit);
804 if (parent)
805 fprintf(opt->diffopt.file, " (from %s)",
806 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
807 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
808 show_decorations(opt, commit);
809 if (opt->commit_format == CMIT_FMT_ONELINE) {
810 putc(' ', opt->diffopt.file);
811 } else {
812 putc('\n', opt->diffopt.file);
813 graph_show_oneline(opt->graph);
815 if (opt->reflog_info) {
817 * setup_revisions() ensures that opt->reflog_info
818 * and opt->graph cannot both be set,
819 * so we don't need to worry about printing the
820 * graph info here.
822 show_reflog_message(opt->reflog_info,
823 opt->commit_format == CMIT_FMT_ONELINE,
824 opt->date_mode,
825 opt->date_mode_explicit);
826 if (opt->commit_format == CMIT_FMT_ONELINE)
827 return;
831 if (opt->show_signature) {
832 show_signature(opt, commit);
833 show_mergetag(opt, commit);
836 if (opt->show_notes) {
837 int raw;
838 struct strbuf notebuf = STRBUF_INIT;
840 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
841 format_display_notes(&commit->object.oid, &notebuf,
842 get_log_output_encoding(), raw);
843 ctx.notes_message = strbuf_detach(&notebuf, NULL);
847 * And then the pretty-printed message itself
849 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
850 ctx.need_8bit_cte =
851 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
852 ctx.date_mode = opt->date_mode;
853 ctx.date_mode_explicit = opt->date_mode_explicit;
854 ctx.abbrev = opt->diffopt.abbrev;
855 ctx.preserve_subject = opt->preserve_subject;
856 ctx.encode_email_headers = opt->encode_email_headers;
857 ctx.reflog_info = opt->reflog_info;
858 ctx.fmt = opt->commit_format;
859 ctx.mailmap = opt->mailmap;
860 ctx.color = opt->diffopt.use_color;
861 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
862 ctx.output_encoding = get_log_output_encoding();
863 ctx.rev = opt;
864 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
865 ctx.from_ident = &opt->from_ident;
866 if (opt->graph)
867 ctx.graph_width = graph_width(opt->graph);
868 pretty_print_commit(&ctx, commit, &msgbuf);
870 if (opt->add_signoff)
871 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
873 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
874 ctx.notes_message && *ctx.notes_message) {
875 if (cmit_fmt_is_mail(ctx.fmt))
876 next_commentary_block(opt, &msgbuf);
877 strbuf_addstr(&msgbuf, ctx.notes_message);
880 if (opt->show_log_size) {
881 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
882 graph_show_oneline(opt->graph);
886 * Set opt->missing_newline if msgbuf doesn't
887 * end in a newline (including if it is empty)
889 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
890 opt->missing_newline = 1;
891 else
892 opt->missing_newline = 0;
894 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
895 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
896 if (!opt->missing_newline)
897 graph_show_padding(opt->graph);
898 putc(opt->diffopt.line_termination, opt->diffopt.file);
901 strbuf_release(&msgbuf);
902 free(ctx.notes_message);
903 free(ctx.after_subject);
906 int log_tree_diff_flush(struct rev_info *opt)
908 opt->shown_dashes = 0;
909 diffcore_std(&opt->diffopt);
911 if (diff_queue_is_empty(&opt->diffopt)) {
912 int saved_fmt = opt->diffopt.output_format;
913 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
914 diff_flush(&opt->diffopt);
915 opt->diffopt.output_format = saved_fmt;
916 return 0;
919 if (opt->loginfo && !opt->no_commit_id) {
920 show_log(opt);
921 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
922 opt->verbose_header &&
923 opt->commit_format != CMIT_FMT_ONELINE &&
924 !commit_format_is_empty(opt->commit_format)) {
926 * When showing a verbose header (i.e. log message),
927 * and not in --pretty=oneline format, we would want
928 * an extra newline between the end of log and the
929 * diff/diffstat output for readability.
931 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
932 if (opt->diffopt.output_prefix) {
933 struct strbuf *msg = NULL;
934 msg = opt->diffopt.output_prefix(&opt->diffopt,
935 opt->diffopt.output_prefix_data);
936 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
940 * We may have shown three-dashes line early
941 * between generated commentary (notes, etc.)
942 * and the log message, in which case we only
943 * want a blank line after the commentary
944 * without (an extra) three-dashes line.
945 * Otherwise, we show the three-dashes line if
946 * we are showing the patch with diffstat, but
947 * in that case, there is no extra blank line
948 * after the three-dashes line.
950 if (!opt->shown_dashes &&
951 (pch & opt->diffopt.output_format) == pch)
952 fprintf(opt->diffopt.file, "---");
953 putc('\n', opt->diffopt.file);
956 diff_flush(&opt->diffopt);
957 return 1;
960 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
962 diff_tree_combined_merge(commit, opt);
963 return !opt->loginfo;
966 static void setup_additional_headers(struct diff_options *o,
967 struct strmap *all_headers)
969 struct hashmap_iter iter;
970 struct strmap_entry *entry;
973 * Make o->additional_path_headers contain the subset of all_headers
974 * that match o->pathspec. If there aren't any that match o->pathspec,
975 * then make o->additional_path_headers be NULL.
978 if (!o->pathspec.nr) {
979 o->additional_path_headers = all_headers;
980 return;
983 o->additional_path_headers = xmalloc(sizeof(struct strmap));
984 strmap_init_with_options(o->additional_path_headers, NULL, 0);
985 strmap_for_each_entry(all_headers, &iter, entry) {
986 if (match_pathspec(the_repository->index, &o->pathspec,
987 entry->key, strlen(entry->key),
988 0 /* prefix */, NULL /* seen */,
989 0 /* is_dir */))
990 strmap_put(o->additional_path_headers,
991 entry->key, entry->value);
993 if (!strmap_get_size(o->additional_path_headers)) {
994 strmap_clear(o->additional_path_headers, 0);
995 FREE_AND_NULL(o->additional_path_headers);
999 static void cleanup_additional_headers(struct diff_options *o)
1001 if (!o->pathspec.nr) {
1002 o->additional_path_headers = NULL;
1003 return;
1005 if (!o->additional_path_headers)
1006 return;
1008 strmap_clear(o->additional_path_headers, 0);
1009 FREE_AND_NULL(o->additional_path_headers);
1012 static int do_remerge_diff(struct rev_info *opt,
1013 struct commit_list *parents,
1014 struct object_id *oid)
1016 struct merge_options o;
1017 struct commit_list *bases = NULL;
1018 struct merge_result res = {0};
1019 struct pretty_print_context ctx = {0};
1020 struct commit *parent1 = parents->item;
1021 struct commit *parent2 = parents->next->item;
1022 struct strbuf parent1_desc = STRBUF_INIT;
1023 struct strbuf parent2_desc = STRBUF_INIT;
1025 /* Setup merge options */
1026 init_merge_options(&o, the_repository);
1027 o.show_rename_progress = 0;
1028 o.record_conflict_msgs_as_headers = 1;
1029 o.msg_header_prefix = "remerge";
1031 ctx.abbrev = DEFAULT_ABBREV;
1032 repo_format_commit_message(the_repository, parent1, "%h (%s)",
1033 &parent1_desc, &ctx);
1034 repo_format_commit_message(the_repository, parent2, "%h (%s)",
1035 &parent2_desc, &ctx);
1036 o.branch1 = parent1_desc.buf;
1037 o.branch2 = parent2_desc.buf;
1039 /* Parse the relevant commits and get the merge bases */
1040 parse_commit_or_die(parent1);
1041 parse_commit_or_die(parent2);
1042 if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)
1043 exit(128);
1045 /* Re-merge the parents */
1046 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1048 /* Show the diff */
1049 setup_additional_headers(&opt->diffopt, res.path_messages);
1050 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1051 log_tree_diff_flush(opt);
1053 /* Cleanup */
1054 cleanup_additional_headers(&opt->diffopt);
1055 strbuf_release(&parent1_desc);
1056 strbuf_release(&parent2_desc);
1057 merge_finalize(&o, &res);
1059 /* Clean up the contents of the temporary object directory */
1060 if (opt->remerge_objdir)
1061 tmp_objdir_discard_objects(opt->remerge_objdir);
1062 else
1063 BUG("did a remerge diff without remerge_objdir?!?");
1065 return !opt->loginfo;
1069 * Show the diff of a commit.
1071 * Return true if we printed any log info messages
1073 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1075 int showed_log;
1076 struct commit_list *parents;
1077 struct object_id *oid;
1078 int is_merge;
1079 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1081 if (!all_need_diff && !opt->merges_need_diff)
1082 return 0;
1084 parse_commit_or_die(commit);
1085 oid = get_commit_tree_oid(commit);
1087 parents = get_saved_parents(opt, commit);
1088 is_merge = parents && parents->next;
1089 if (!is_merge && !all_need_diff)
1090 return 0;
1092 /* Root commit? */
1093 if (!parents) {
1094 if (opt->show_root_diff) {
1095 diff_root_tree_oid(oid, "", &opt->diffopt);
1096 log_tree_diff_flush(opt);
1098 return !opt->loginfo;
1101 if (is_merge) {
1102 int octopus = (parents->next->next != NULL);
1104 if (opt->remerge_diff) {
1105 if (octopus) {
1106 show_log(opt);
1107 fprintf(opt->diffopt.file,
1108 "diff: warning: Skipping remerge-diff "
1109 "for octopus merges.\n");
1110 return 1;
1112 return do_remerge_diff(opt, parents, oid);
1114 if (opt->combine_merges)
1115 return do_diff_combined(opt, commit);
1116 if (opt->separate_merges) {
1117 if (!opt->first_parent_merges) {
1118 /* Show parent info for multiple diffs */
1119 log->parent = parents->item;
1121 } else
1122 return 0;
1125 showed_log = 0;
1126 for (;;) {
1127 struct commit *parent = parents->item;
1129 parse_commit_or_die(parent);
1130 diff_tree_oid(get_commit_tree_oid(parent),
1131 oid, "", &opt->diffopt);
1132 log_tree_diff_flush(opt);
1134 showed_log |= !opt->loginfo;
1136 /* Set up the log info for the next parent, if any.. */
1137 parents = parents->next;
1138 if (!parents || opt->first_parent_merges)
1139 break;
1140 log->parent = parents->item;
1141 opt->loginfo = log;
1143 return showed_log;
1146 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1148 struct log_info log;
1149 int shown;
1150 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1151 int no_free = opt->diffopt.no_free;
1153 log.commit = commit;
1154 log.parent = NULL;
1155 opt->loginfo = &log;
1156 opt->diffopt.no_free = 1;
1158 /* NEEDSWORK: no restoring of no_free? Why? */
1159 if (opt->line_level_traverse)
1160 return line_log_print(opt, commit);
1162 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1163 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1164 shown = log_tree_diff(opt, commit, &log);
1165 if (!shown && opt->loginfo && opt->always_show_header) {
1166 log.parent = NULL;
1167 show_log(opt);
1168 shown = 1;
1170 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1171 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1172 if (shown)
1173 show_diff_of_diff(opt);
1174 opt->loginfo = NULL;
1175 maybe_flush_or_die(opt->diffopt.file, "stdout");
1176 opt->diffopt.no_free = no_free;
1178 diff_free(&opt->diffopt);
1179 return shown;