branch: update the message to refuse touching a branch in-use
[git/debian.git] / log-tree.c
blob208c69cbb79021ad471467292572c5587b103a6d
1 #include "git-compat-util.h"
2 #include "commit-reach.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "environment.h"
6 #include "hex.h"
7 #include "object-name.h"
8 #include "object-store-ll.h"
9 #include "repository.h"
10 #include "tmp-objdir.h"
11 #include "commit.h"
12 #include "tag.h"
13 #include "graph.h"
14 #include "log-tree.h"
15 #include "merge-ort.h"
16 #include "reflog-walk.h"
17 #include "refs.h"
18 #include "replace-object.h"
19 #include "revision.h"
20 #include "string-list.h"
21 #include "color.h"
22 #include "gpg-interface.h"
23 #include "sequencer.h"
24 #include "line-log.h"
25 #include "help.h"
26 #include "range-diff.h"
27 #include "strmap.h"
28 #include "tree.h"
29 #include "wildmatch.h"
30 #include "write-or-die.h"
32 static struct decoration name_decoration = { "object names" };
33 static int decoration_loaded;
34 static int decoration_flags;
36 static char decoration_colors[][COLOR_MAXLEN] = {
37 GIT_COLOR_RESET,
38 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
39 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
40 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
41 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
42 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
43 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
46 static const char *color_decorate_slots[] = {
47 [DECORATION_REF_LOCAL] = "branch",
48 [DECORATION_REF_REMOTE] = "remoteBranch",
49 [DECORATION_REF_TAG] = "tag",
50 [DECORATION_REF_STASH] = "stash",
51 [DECORATION_REF_HEAD] = "HEAD",
52 [DECORATION_GRAFTED] = "grafted",
55 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
57 if (want_color(decorate_use_color))
58 return decoration_colors[ix];
59 return "";
62 define_list_config_array(color_decorate_slots);
64 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
66 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
67 if (slot < 0)
68 return 0;
69 if (!value)
70 return config_error_nonbool(var);
71 return color_parse(value, decoration_colors[slot]);
75 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
76 * for showing the commit sha1, use the same check for --decorate
78 #define decorate_get_color_opt(o, ix) \
79 decorate_get_color((o)->use_color, ix)
81 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
83 struct name_decoration *res;
84 FLEX_ALLOC_STR(res, name, name);
85 res->type = type;
86 res->next = add_decoration(&name_decoration, obj, res);
89 const struct name_decoration *get_name_decoration(const struct object *obj)
91 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
92 return lookup_decoration(&name_decoration, obj);
95 static int match_ref_pattern(const char *refname,
96 const struct string_list_item *item)
98 int matched = 0;
99 if (!item->util) {
100 if (!wildmatch(item->string, refname, 0))
101 matched = 1;
102 } else {
103 const char *rest;
104 if (skip_prefix(refname, item->string, &rest) &&
105 (!*rest || *rest == '/'))
106 matched = 1;
108 return matched;
111 static int ref_filter_match(const char *refname,
112 const struct decoration_filter *filter)
114 struct string_list_item *item;
115 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
116 const struct string_list *include_patterns = filter->include_ref_pattern;
117 const struct string_list *exclude_patterns_config =
118 filter->exclude_ref_config_pattern;
120 if (exclude_patterns && exclude_patterns->nr) {
121 for_each_string_list_item(item, exclude_patterns) {
122 if (match_ref_pattern(refname, item))
123 return 0;
127 if (include_patterns && include_patterns->nr) {
128 for_each_string_list_item(item, include_patterns) {
129 if (match_ref_pattern(refname, item))
130 return 1;
132 return 0;
135 if (exclude_patterns_config && exclude_patterns_config->nr) {
136 for_each_string_list_item(item, exclude_patterns_config) {
137 if (match_ref_pattern(refname, item))
138 return 0;
142 return 1;
145 static int add_ref_decoration(const char *refname, const struct object_id *oid,
146 int flags UNUSED,
147 void *cb_data)
149 int i;
150 struct object *obj;
151 enum object_type objtype;
152 enum decoration_type deco_type = DECORATION_NONE;
153 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
154 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
156 if (filter && !ref_filter_match(refname, filter))
157 return 0;
159 if (starts_with(refname, git_replace_ref_base)) {
160 struct object_id original_oid;
161 if (!replace_refs_enabled(the_repository))
162 return 0;
163 if (get_oid_hex(refname + strlen(git_replace_ref_base),
164 &original_oid)) {
165 warning("invalid replace ref %s", refname);
166 return 0;
168 obj = parse_object(the_repository, &original_oid);
169 if (obj)
170 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
171 return 0;
174 objtype = oid_object_info(the_repository, oid, NULL);
175 if (objtype < 0)
176 return 0;
177 obj = lookup_object_by_type(the_repository, oid, objtype);
179 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
180 struct ref_namespace_info *info = &ref_namespace[i];
182 if (!info->decoration)
183 continue;
184 if (info->exact) {
185 if (!strcmp(refname, info->ref)) {
186 deco_type = info->decoration;
187 break;
189 } else if (starts_with(refname, info->ref)) {
190 deco_type = info->decoration;
191 break;
195 add_name_decoration(deco_type, refname, obj);
196 while (obj->type == OBJ_TAG) {
197 if (!obj->parsed)
198 parse_object(the_repository, &obj->oid);
199 obj = ((struct tag *)obj)->tagged;
200 if (!obj)
201 break;
202 add_name_decoration(DECORATION_REF_TAG, refname, obj);
204 return 0;
207 static int add_graft_decoration(const struct commit_graft *graft,
208 void *cb_data UNUSED)
210 struct commit *commit = lookup_commit(the_repository, &graft->oid);
211 if (!commit)
212 return 0;
213 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
214 return 0;
217 void load_ref_decorations(struct decoration_filter *filter, int flags)
219 if (!decoration_loaded) {
220 if (filter) {
221 struct string_list_item *item;
222 for_each_string_list_item(item, filter->exclude_ref_pattern) {
223 normalize_glob_ref(item, NULL, item->string);
225 for_each_string_list_item(item, filter->include_ref_pattern) {
226 normalize_glob_ref(item, NULL, item->string);
228 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
229 normalize_glob_ref(item, NULL, item->string);
232 decoration_loaded = 1;
233 decoration_flags = flags;
234 for_each_ref(add_ref_decoration, filter);
235 head_ref(add_ref_decoration, filter);
236 for_each_commit_graft(add_graft_decoration, filter);
240 static void show_parents(struct commit *commit, int abbrev, FILE *file)
242 struct commit_list *p;
243 for (p = commit->parents; p ; p = p->next) {
244 struct commit *parent = p->item;
245 fprintf(file, " %s",
246 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
250 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
252 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
253 for ( ; p; p = p->next) {
254 fprintf(opt->diffopt.file, " %s",
255 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
260 * Do we have HEAD in the output, and also the branch it points at?
261 * If so, find that decoration entry for that current branch.
263 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
265 const struct name_decoration *list, *head = NULL;
266 const char *branch_name = NULL;
267 int rru_flags;
269 /* First find HEAD */
270 for (list = decoration; list; list = list->next)
271 if (list->type == DECORATION_REF_HEAD) {
272 head = list;
273 break;
275 if (!head)
276 return NULL;
278 /* Now resolve and find the matching current branch */
279 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
280 if (!branch_name || !(rru_flags & REF_ISSYMREF))
281 return NULL;
283 if (!starts_with(branch_name, "refs/"))
284 return NULL;
286 /* OK, do we have that ref in the list? */
287 for (list = decoration; list; list = list->next)
288 if ((list->type == DECORATION_REF_LOCAL) &&
289 !strcmp(branch_name, list->name)) {
290 return list;
293 return NULL;
296 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
298 if (decoration_flags == DECORATE_SHORT_REFS)
299 strbuf_addstr(sb, prettify_refname(decoration->name));
300 else
301 strbuf_addstr(sb, decoration->name);
305 * The caller makes sure there is no funny color before calling.
306 * format_decorations_extended makes sure the same after return.
308 void format_decorations_extended(struct strbuf *sb,
309 const struct commit *commit,
310 int use_color,
311 const char *prefix,
312 const char *separator,
313 const char *suffix)
315 const struct name_decoration *decoration;
316 const struct name_decoration *current_and_HEAD;
317 const char *color_commit =
318 diff_get_color(use_color, DIFF_COMMIT);
319 const char *color_reset =
320 decorate_get_color(use_color, DECORATION_NONE);
322 decoration = get_name_decoration(&commit->object);
323 if (!decoration)
324 return;
326 current_and_HEAD = current_pointed_by_HEAD(decoration);
327 while (decoration) {
329 * When both current and HEAD are there, only
330 * show HEAD->current where HEAD would have
331 * appeared, skipping the entry for current.
333 if (decoration != current_and_HEAD) {
334 strbuf_addstr(sb, color_commit);
335 strbuf_addstr(sb, prefix);
336 strbuf_addstr(sb, color_reset);
337 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
338 if (decoration->type == DECORATION_REF_TAG)
339 strbuf_addstr(sb, "tag: ");
341 show_name(sb, decoration);
343 if (current_and_HEAD &&
344 decoration->type == DECORATION_REF_HEAD) {
345 strbuf_addstr(sb, " -> ");
346 strbuf_addstr(sb, color_reset);
347 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
348 show_name(sb, current_and_HEAD);
350 strbuf_addstr(sb, color_reset);
352 prefix = separator;
354 decoration = decoration->next;
356 strbuf_addstr(sb, color_commit);
357 strbuf_addstr(sb, suffix);
358 strbuf_addstr(sb, color_reset);
361 void show_decorations(struct rev_info *opt, struct commit *commit)
363 struct strbuf sb = STRBUF_INIT;
365 if (opt->sources) {
366 char **slot = revision_sources_peek(opt->sources, commit);
368 if (slot && *slot)
369 fprintf(opt->diffopt.file, "\t%s", *slot);
371 if (!opt->show_decorations)
372 return;
373 format_decorations(&sb, commit, opt->diffopt.use_color);
374 fputs(sb.buf, opt->diffopt.file);
375 strbuf_release(&sb);
378 static unsigned int digits_in_number(unsigned int number)
380 unsigned int i = 10, result = 1;
381 while (i <= number) {
382 i *= 10;
383 result++;
385 return result;
388 void fmt_output_subject(struct strbuf *filename,
389 const char *subject,
390 struct rev_info *info)
392 const char *suffix = info->patch_suffix;
393 int nr = info->nr;
394 int start_len = filename->len;
395 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
397 if (info->reroll_count) {
398 struct strbuf temp = STRBUF_INIT;
400 strbuf_addf(&temp, "v%s", info->reroll_count);
401 format_sanitized_subject(filename, temp.buf, temp.len);
402 strbuf_addstr(filename, "-");
403 strbuf_release(&temp);
405 strbuf_addf(filename, "%04d-%s", nr, subject);
407 if (max_len < filename->len)
408 strbuf_setlen(filename, max_len);
409 strbuf_addstr(filename, suffix);
412 void fmt_output_commit(struct strbuf *filename,
413 struct commit *commit,
414 struct rev_info *info)
416 struct pretty_print_context ctx = {0};
417 struct strbuf subject = STRBUF_INIT;
419 repo_format_commit_message(the_repository, commit, "%f", &subject,
420 &ctx);
421 fmt_output_subject(filename, subject.buf, info);
422 strbuf_release(&subject);
425 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
427 if (opt->total > 0) {
428 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
429 opt->subject_prefix,
430 *opt->subject_prefix ? " " : "",
431 digits_in_number(opt->total),
432 opt->nr, opt->total);
433 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
434 strbuf_addf(sb, "Subject: [%s] ",
435 opt->subject_prefix);
436 } else {
437 strbuf_addstr(sb, "Subject: ");
441 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
442 const char **extra_headers_p,
443 int *need_8bit_cte_p,
444 int maybe_multipart)
446 const char *extra_headers = opt->extra_headers;
447 const char *name = oid_to_hex(opt->zero_commit ?
448 null_oid() : &commit->object.oid);
450 *need_8bit_cte_p = 0; /* unknown */
452 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
453 graph_show_oneline(opt->graph);
454 if (opt->message_id) {
455 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
456 graph_show_oneline(opt->graph);
458 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
459 int i, n;
460 n = opt->ref_message_ids->nr;
461 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
462 for (i = 0; i < n; i++)
463 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
464 opt->ref_message_ids->items[i].string);
465 graph_show_oneline(opt->graph);
467 if (opt->mime_boundary && maybe_multipart) {
468 static struct strbuf subject_buffer = STRBUF_INIT;
469 static struct strbuf buffer = STRBUF_INIT;
470 struct strbuf filename = STRBUF_INIT;
471 *need_8bit_cte_p = -1; /* NEVER */
473 strbuf_reset(&subject_buffer);
474 strbuf_reset(&buffer);
476 strbuf_addf(&subject_buffer,
477 "%s"
478 "MIME-Version: 1.0\n"
479 "Content-Type: multipart/mixed;"
480 " boundary=\"%s%s\"\n"
481 "\n"
482 "This is a multi-part message in MIME "
483 "format.\n"
484 "--%s%s\n"
485 "Content-Type: text/plain; "
486 "charset=UTF-8; format=fixed\n"
487 "Content-Transfer-Encoding: 8bit\n\n",
488 extra_headers ? extra_headers : "",
489 mime_boundary_leader, opt->mime_boundary,
490 mime_boundary_leader, opt->mime_boundary);
491 extra_headers = subject_buffer.buf;
493 if (opt->numbered_files)
494 strbuf_addf(&filename, "%d", opt->nr);
495 else
496 fmt_output_commit(&filename, commit, opt);
497 strbuf_addf(&buffer,
498 "\n--%s%s\n"
499 "Content-Type: text/x-patch;"
500 " name=\"%s\"\n"
501 "Content-Transfer-Encoding: 8bit\n"
502 "Content-Disposition: %s;"
503 " filename=\"%s\"\n\n",
504 mime_boundary_leader, opt->mime_boundary,
505 filename.buf,
506 opt->no_inline ? "attachment" : "inline",
507 filename.buf);
508 opt->diffopt.stat_sep = buffer.buf;
509 strbuf_release(&filename);
511 *extra_headers_p = extra_headers;
514 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
516 const char *color, *reset, *eol;
518 color = diff_get_color_opt(&opt->diffopt,
519 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
520 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
521 while (*bol) {
522 eol = strchrnul(bol, '\n');
523 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
524 *eol ? "\n" : "");
525 graph_show_oneline(opt->graph);
526 bol = (*eol) ? (eol + 1) : eol;
530 static void show_signature(struct rev_info *opt, struct commit *commit)
532 struct strbuf payload = STRBUF_INIT;
533 struct strbuf signature = STRBUF_INIT;
534 struct signature_check sigc = { 0 };
535 int status;
537 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
538 goto out;
540 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
541 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
542 status = check_signature(&sigc, signature.buf, signature.len);
543 if (status && !sigc.output)
544 show_sig_lines(opt, status, "No signature\n");
545 else
546 show_sig_lines(opt, status, sigc.output);
547 signature_check_clear(&sigc);
549 out:
550 strbuf_release(&payload);
551 strbuf_release(&signature);
554 static int which_parent(const struct object_id *oid, const struct commit *commit)
556 int nth;
557 const struct commit_list *parent;
559 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
560 if (oideq(&parent->item->object.oid, oid))
561 return nth;
562 nth++;
564 return -1;
567 static int is_common_merge(const struct commit *commit)
569 return (commit->parents
570 && commit->parents->next
571 && !commit->parents->next->next);
574 static int show_one_mergetag(struct commit *commit,
575 struct commit_extra_header *extra,
576 void *data)
578 struct rev_info *opt = (struct rev_info *)data;
579 struct object_id oid;
580 struct tag *tag;
581 struct strbuf verify_message;
582 struct signature_check sigc = { 0 };
583 int status, nth;
584 struct strbuf payload = STRBUF_INIT;
585 struct strbuf signature = STRBUF_INIT;
587 hash_object_file(the_hash_algo, extra->value, extra->len,
588 OBJ_TAG, &oid);
589 tag = lookup_tag(the_repository, &oid);
590 if (!tag)
591 return -1; /* error message already given */
593 strbuf_init(&verify_message, 256);
594 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
595 strbuf_addstr(&verify_message, "malformed mergetag\n");
596 else if (is_common_merge(commit) &&
597 oideq(&tag->tagged->oid,
598 &commit->parents->next->item->object.oid))
599 strbuf_addf(&verify_message,
600 "merged tag '%s'\n", tag->tag);
601 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
602 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
603 tag->tag, oid_to_hex(&tag->tagged->oid));
604 else
605 strbuf_addf(&verify_message,
606 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
608 status = -1;
609 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
610 /* could have a good signature */
611 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
612 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
613 status = check_signature(&sigc, signature.buf, signature.len);
614 if (sigc.output)
615 strbuf_addstr(&verify_message, sigc.output);
616 else
617 strbuf_addstr(&verify_message, "No signature\n");
618 signature_check_clear(&sigc);
619 /* otherwise we couldn't verify, which is shown as bad */
622 show_sig_lines(opt, status, verify_message.buf);
623 strbuf_release(&verify_message);
624 strbuf_release(&payload);
625 strbuf_release(&signature);
626 return 0;
629 static int show_mergetag(struct rev_info *opt, struct commit *commit)
631 return for_each_mergetag(show_one_mergetag, commit, opt);
634 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
636 const char *x = opt->shown_dashes ? "\n" : "---\n";
637 if (sb)
638 strbuf_addstr(sb, x);
639 else
640 fputs(x, opt->diffopt.file);
641 opt->shown_dashes = 1;
644 void show_log(struct rev_info *opt)
646 struct strbuf msgbuf = STRBUF_INIT;
647 struct log_info *log = opt->loginfo;
648 struct commit *commit = log->commit, *parent = log->parent;
649 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
650 const char *extra_headers = opt->extra_headers;
651 struct pretty_print_context ctx = {0};
653 opt->loginfo = NULL;
654 if (!opt->verbose_header) {
655 graph_show_commit(opt->graph);
657 if (!opt->graph)
658 put_revision_mark(opt, commit);
659 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
660 opt->diffopt.file);
661 if (opt->print_parents)
662 show_parents(commit, abbrev_commit, opt->diffopt.file);
663 if (opt->children.name)
664 show_children(opt, commit, abbrev_commit);
665 show_decorations(opt, commit);
666 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
667 putc('\n', opt->diffopt.file);
668 graph_show_remainder(opt->graph);
670 putc(opt->diffopt.line_termination, opt->diffopt.file);
671 return;
675 * If use_terminator is set, we already handled any record termination
676 * at the end of the last record.
677 * Otherwise, add a diffopt.line_termination character before all
678 * entries but the first. (IOW, as a separator between entries)
680 if (opt->shown_one && !opt->use_terminator) {
682 * If entries are separated by a newline, the output
683 * should look human-readable. If the last entry ended
684 * with a newline, print the graph output before this
685 * newline. Otherwise it will end up as a completely blank
686 * line and will look like a gap in the graph.
688 * If the entry separator is not a newline, the output is
689 * primarily intended for programmatic consumption, and we
690 * never want the extra graph output before the entry
691 * separator.
693 if (opt->diffopt.line_termination == '\n' &&
694 !opt->missing_newline)
695 graph_show_padding(opt->graph);
696 putc(opt->diffopt.line_termination, opt->diffopt.file);
698 opt->shown_one = 1;
701 * If the history graph was requested,
702 * print the graph, up to this commit's line
704 graph_show_commit(opt->graph);
707 * Print header line of header..
710 if (cmit_fmt_is_mail(opt->commit_format)) {
711 log_write_email_headers(opt, commit, &extra_headers,
712 &ctx.need_8bit_cte, 1);
713 ctx.rev = opt;
714 ctx.print_email_subject = 1;
715 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
716 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
717 if (opt->commit_format != CMIT_FMT_ONELINE)
718 fputs("commit ", opt->diffopt.file);
720 if (!opt->graph)
721 put_revision_mark(opt, commit);
722 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
723 abbrev_commit),
724 opt->diffopt.file);
725 if (opt->print_parents)
726 show_parents(commit, abbrev_commit, opt->diffopt.file);
727 if (opt->children.name)
728 show_children(opt, commit, abbrev_commit);
729 if (parent)
730 fprintf(opt->diffopt.file, " (from %s)",
731 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
732 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
733 show_decorations(opt, commit);
734 if (opt->commit_format == CMIT_FMT_ONELINE) {
735 putc(' ', opt->diffopt.file);
736 } else {
737 putc('\n', opt->diffopt.file);
738 graph_show_oneline(opt->graph);
740 if (opt->reflog_info) {
742 * setup_revisions() ensures that opt->reflog_info
743 * and opt->graph cannot both be set,
744 * so we don't need to worry about printing the
745 * graph info here.
747 show_reflog_message(opt->reflog_info,
748 opt->commit_format == CMIT_FMT_ONELINE,
749 &opt->date_mode,
750 opt->date_mode_explicit);
751 if (opt->commit_format == CMIT_FMT_ONELINE)
752 return;
756 if (opt->show_signature) {
757 show_signature(opt, commit);
758 show_mergetag(opt, commit);
761 if (opt->show_notes) {
762 int raw;
763 struct strbuf notebuf = STRBUF_INIT;
765 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
766 format_display_notes(&commit->object.oid, &notebuf,
767 get_log_output_encoding(), raw);
768 ctx.notes_message = strbuf_detach(&notebuf, NULL);
772 * And then the pretty-printed message itself
774 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
775 ctx.need_8bit_cte =
776 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
777 ctx.date_mode = opt->date_mode;
778 ctx.date_mode_explicit = opt->date_mode_explicit;
779 ctx.abbrev = opt->diffopt.abbrev;
780 ctx.after_subject = extra_headers;
781 ctx.preserve_subject = opt->preserve_subject;
782 ctx.encode_email_headers = opt->encode_email_headers;
783 ctx.reflog_info = opt->reflog_info;
784 ctx.fmt = opt->commit_format;
785 ctx.mailmap = opt->mailmap;
786 ctx.color = opt->diffopt.use_color;
787 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
788 ctx.output_encoding = get_log_output_encoding();
789 ctx.rev = opt;
790 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
791 ctx.from_ident = &opt->from_ident;
792 if (opt->graph)
793 ctx.graph_width = graph_width(opt->graph);
794 pretty_print_commit(&ctx, commit, &msgbuf);
796 if (opt->add_signoff)
797 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
799 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
800 ctx.notes_message && *ctx.notes_message) {
801 if (cmit_fmt_is_mail(ctx.fmt))
802 next_commentary_block(opt, &msgbuf);
803 strbuf_addstr(&msgbuf, ctx.notes_message);
806 if (opt->show_log_size) {
807 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
808 graph_show_oneline(opt->graph);
812 * Set opt->missing_newline if msgbuf doesn't
813 * end in a newline (including if it is empty)
815 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
816 opt->missing_newline = 1;
817 else
818 opt->missing_newline = 0;
820 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
821 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
822 if (!opt->missing_newline)
823 graph_show_padding(opt->graph);
824 putc(opt->diffopt.line_termination, opt->diffopt.file);
827 strbuf_release(&msgbuf);
828 free(ctx.notes_message);
830 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
831 struct diff_queue_struct dq;
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->idiff_title);
838 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
839 &opt->diffopt);
841 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
844 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
845 struct diff_queue_struct dq;
846 struct diff_options opts;
847 struct range_diff_options range_diff_opts = {
848 .creation_factor = opt->creation_factor,
849 .dual_color = 1,
850 .diffopt = &opts
853 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
854 DIFF_QUEUE_CLEAR(&diff_queued_diff);
856 next_commentary_block(opt, NULL);
857 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
859 * Pass minimum required diff-options to range-diff; others
860 * can be added later if deemed desirable.
862 repo_diff_setup(the_repository, &opts);
863 opts.file = opt->diffopt.file;
864 opts.use_color = opt->diffopt.use_color;
865 diff_setup_done(&opts);
866 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
868 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
872 int log_tree_diff_flush(struct rev_info *opt)
874 opt->shown_dashes = 0;
875 diffcore_std(&opt->diffopt);
877 if (diff_queue_is_empty(&opt->diffopt)) {
878 int saved_fmt = opt->diffopt.output_format;
879 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
880 diff_flush(&opt->diffopt);
881 opt->diffopt.output_format = saved_fmt;
882 return 0;
885 if (opt->loginfo && !opt->no_commit_id) {
886 show_log(opt);
887 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
888 opt->verbose_header &&
889 opt->commit_format != CMIT_FMT_ONELINE &&
890 !commit_format_is_empty(opt->commit_format)) {
892 * When showing a verbose header (i.e. log message),
893 * and not in --pretty=oneline format, we would want
894 * an extra newline between the end of log and the
895 * diff/diffstat output for readability.
897 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
898 if (opt->diffopt.output_prefix) {
899 struct strbuf *msg = NULL;
900 msg = opt->diffopt.output_prefix(&opt->diffopt,
901 opt->diffopt.output_prefix_data);
902 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
906 * We may have shown three-dashes line early
907 * between generated commentary (notes, etc.)
908 * and the log message, in which case we only
909 * want a blank line after the commentary
910 * without (an extra) three-dashes line.
911 * Otherwise, we show the three-dashes line if
912 * we are showing the patch with diffstat, but
913 * in that case, there is no extra blank line
914 * after the three-dashes line.
916 if (!opt->shown_dashes &&
917 (pch & opt->diffopt.output_format) == pch)
918 fprintf(opt->diffopt.file, "---");
919 putc('\n', opt->diffopt.file);
922 diff_flush(&opt->diffopt);
923 return 1;
926 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
928 diff_tree_combined_merge(commit, opt);
929 return !opt->loginfo;
932 static void setup_additional_headers(struct diff_options *o,
933 struct strmap *all_headers)
935 struct hashmap_iter iter;
936 struct strmap_entry *entry;
939 * Make o->additional_path_headers contain the subset of all_headers
940 * that match o->pathspec. If there aren't any that match o->pathspec,
941 * then make o->additional_path_headers be NULL.
944 if (!o->pathspec.nr) {
945 o->additional_path_headers = all_headers;
946 return;
949 o->additional_path_headers = xmalloc(sizeof(struct strmap));
950 strmap_init_with_options(o->additional_path_headers, NULL, 0);
951 strmap_for_each_entry(all_headers, &iter, entry) {
952 if (match_pathspec(the_repository->index, &o->pathspec,
953 entry->key, strlen(entry->key),
954 0 /* prefix */, NULL /* seen */,
955 0 /* is_dir */))
956 strmap_put(o->additional_path_headers,
957 entry->key, entry->value);
959 if (!strmap_get_size(o->additional_path_headers)) {
960 strmap_clear(o->additional_path_headers, 0);
961 FREE_AND_NULL(o->additional_path_headers);
965 static void cleanup_additional_headers(struct diff_options *o)
967 if (!o->pathspec.nr) {
968 o->additional_path_headers = NULL;
969 return;
971 if (!o->additional_path_headers)
972 return;
974 strmap_clear(o->additional_path_headers, 0);
975 FREE_AND_NULL(o->additional_path_headers);
978 static int do_remerge_diff(struct rev_info *opt,
979 struct commit_list *parents,
980 struct object_id *oid)
982 struct merge_options o;
983 struct commit_list *bases;
984 struct merge_result res = {0};
985 struct pretty_print_context ctx = {0};
986 struct commit *parent1 = parents->item;
987 struct commit *parent2 = parents->next->item;
988 struct strbuf parent1_desc = STRBUF_INIT;
989 struct strbuf parent2_desc = STRBUF_INIT;
991 /* Setup merge options */
992 init_merge_options(&o, the_repository);
993 o.show_rename_progress = 0;
994 o.record_conflict_msgs_as_headers = 1;
995 o.msg_header_prefix = "remerge";
997 ctx.abbrev = DEFAULT_ABBREV;
998 repo_format_commit_message(the_repository, parent1, "%h (%s)",
999 &parent1_desc, &ctx);
1000 repo_format_commit_message(the_repository, parent2, "%h (%s)",
1001 &parent2_desc, &ctx);
1002 o.branch1 = parent1_desc.buf;
1003 o.branch2 = parent2_desc.buf;
1005 /* Parse the relevant commits and get the merge bases */
1006 parse_commit_or_die(parent1);
1007 parse_commit_or_die(parent2);
1008 bases = repo_get_merge_bases(the_repository, parent1, parent2);
1010 /* Re-merge the parents */
1011 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1013 /* Show the diff */
1014 setup_additional_headers(&opt->diffopt, res.path_messages);
1015 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1016 log_tree_diff_flush(opt);
1018 /* Cleanup */
1019 cleanup_additional_headers(&opt->diffopt);
1020 strbuf_release(&parent1_desc);
1021 strbuf_release(&parent2_desc);
1022 merge_finalize(&o, &res);
1024 /* Clean up the contents of the temporary object directory */
1025 if (opt->remerge_objdir)
1026 tmp_objdir_discard_objects(opt->remerge_objdir);
1027 else
1028 BUG("did a remerge diff without remerge_objdir?!?");
1030 return !opt->loginfo;
1034 * Show the diff of a commit.
1036 * Return true if we printed any log info messages
1038 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1040 int showed_log;
1041 struct commit_list *parents;
1042 struct object_id *oid;
1043 int is_merge;
1044 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1046 if (!all_need_diff && !opt->merges_need_diff)
1047 return 0;
1049 parse_commit_or_die(commit);
1050 oid = get_commit_tree_oid(commit);
1052 parents = get_saved_parents(opt, commit);
1053 is_merge = parents && parents->next;
1054 if (!is_merge && !all_need_diff)
1055 return 0;
1057 /* Root commit? */
1058 if (!parents) {
1059 if (opt->show_root_diff) {
1060 diff_root_tree_oid(oid, "", &opt->diffopt);
1061 log_tree_diff_flush(opt);
1063 return !opt->loginfo;
1066 if (is_merge) {
1067 int octopus = (parents->next->next != NULL);
1069 if (opt->remerge_diff) {
1070 if (octopus) {
1071 show_log(opt);
1072 fprintf(opt->diffopt.file,
1073 "diff: warning: Skipping remerge-diff "
1074 "for octopus merges.\n");
1075 return 1;
1077 return do_remerge_diff(opt, parents, oid);
1079 if (opt->combine_merges)
1080 return do_diff_combined(opt, commit);
1081 if (opt->separate_merges) {
1082 if (!opt->first_parent_merges) {
1083 /* Show parent info for multiple diffs */
1084 log->parent = parents->item;
1086 } else
1087 return 0;
1090 showed_log = 0;
1091 for (;;) {
1092 struct commit *parent = parents->item;
1094 parse_commit_or_die(parent);
1095 diff_tree_oid(get_commit_tree_oid(parent),
1096 oid, "", &opt->diffopt);
1097 log_tree_diff_flush(opt);
1099 showed_log |= !opt->loginfo;
1101 /* Set up the log info for the next parent, if any.. */
1102 parents = parents->next;
1103 if (!parents || opt->first_parent_merges)
1104 break;
1105 log->parent = parents->item;
1106 opt->loginfo = log;
1108 return showed_log;
1111 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1113 struct log_info log;
1114 int shown;
1115 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1116 int no_free = opt->diffopt.no_free;
1118 log.commit = commit;
1119 log.parent = NULL;
1120 opt->loginfo = &log;
1121 opt->diffopt.no_free = 1;
1123 /* NEEDSWORK: no restoring of no_free? Why? */
1124 if (opt->line_level_traverse)
1125 return line_log_print(opt, commit);
1127 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1128 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1129 shown = log_tree_diff(opt, commit, &log);
1130 if (!shown && opt->loginfo && opt->always_show_header) {
1131 log.parent = NULL;
1132 show_log(opt);
1133 shown = 1;
1135 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1136 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1137 opt->loginfo = NULL;
1138 maybe_flush_or_die(opt->diffopt.file, "stdout");
1139 opt->diffopt.no_free = no_free;
1140 diff_free(&opt->diffopt);
1141 return shown;