git-p4: fix typos
[git/gitster.git] / log-tree.c
blob52feec4356ab12e206dcf79d313c65c07c49f75f
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "commit-reach.h"
5 #include "config.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "environment.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "repository.h"
13 #include "tmp-objdir.h"
14 #include "commit.h"
15 #include "tag.h"
16 #include "graph.h"
17 #include "log-tree.h"
18 #include "merge-ort.h"
19 #include "reflog-walk.h"
20 #include "refs.h"
21 #include "replace-object.h"
22 #include "revision.h"
23 #include "string-list.h"
24 #include "color.h"
25 #include "gpg-interface.h"
26 #include "sequencer.h"
27 #include "line-log.h"
28 #include "help.h"
29 #include "range-diff.h"
30 #include "strmap.h"
31 #include "tree.h"
32 #include "wildmatch.h"
33 #include "write-or-die.h"
35 static struct decoration name_decoration = { "object names" };
36 static int decoration_loaded;
37 static int decoration_flags;
39 static char decoration_colors[][COLOR_MAXLEN] = {
40 GIT_COLOR_RESET,
41 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
42 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
43 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
44 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
45 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
46 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
49 static const char *color_decorate_slots[] = {
50 [DECORATION_REF_LOCAL] = "branch",
51 [DECORATION_REF_REMOTE] = "remoteBranch",
52 [DECORATION_REF_TAG] = "tag",
53 [DECORATION_REF_STASH] = "stash",
54 [DECORATION_REF_HEAD] = "HEAD",
55 [DECORATION_GRAFTED] = "grafted",
58 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
60 if (want_color(decorate_use_color))
61 return decoration_colors[ix];
62 return "";
65 define_list_config_array(color_decorate_slots);
67 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
69 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
70 if (slot < 0)
71 return 0;
72 if (!value)
73 return config_error_nonbool(var);
74 return color_parse(value, decoration_colors[slot]);
78 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
79 * for showing the commit sha1, use the same check for --decorate
81 #define decorate_get_color_opt(o, ix) \
82 decorate_get_color((o)->use_color, ix)
84 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
86 struct name_decoration *res;
87 FLEX_ALLOC_STR(res, name, name);
88 res->type = type;
89 res->next = add_decoration(&name_decoration, obj, res);
92 const struct name_decoration *get_name_decoration(const struct object *obj)
94 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
95 return lookup_decoration(&name_decoration, obj);
98 static int match_ref_pattern(const char *refname,
99 const struct string_list_item *item)
101 int matched = 0;
102 if (!item->util) {
103 if (!wildmatch(item->string, refname, 0))
104 matched = 1;
105 } else {
106 const char *rest;
107 if (skip_prefix(refname, item->string, &rest) &&
108 (!*rest || *rest == '/'))
109 matched = 1;
111 return matched;
114 static int ref_filter_match(const char *refname,
115 const struct decoration_filter *filter)
117 struct string_list_item *item;
118 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
119 const struct string_list *include_patterns = filter->include_ref_pattern;
120 const struct string_list *exclude_patterns_config =
121 filter->exclude_ref_config_pattern;
123 if (exclude_patterns && exclude_patterns->nr) {
124 for_each_string_list_item(item, exclude_patterns) {
125 if (match_ref_pattern(refname, item))
126 return 0;
130 if (include_patterns && include_patterns->nr) {
131 for_each_string_list_item(item, include_patterns) {
132 if (match_ref_pattern(refname, item))
133 return 1;
135 return 0;
138 if (exclude_patterns_config && exclude_patterns_config->nr) {
139 for_each_string_list_item(item, exclude_patterns_config) {
140 if (match_ref_pattern(refname, item))
141 return 0;
145 return 1;
148 static int add_ref_decoration(const char *refname, const struct object_id *oid,
149 int flags UNUSED,
150 void *cb_data)
152 int i;
153 struct object *obj;
154 enum object_type objtype;
155 enum decoration_type deco_type = DECORATION_NONE;
156 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
157 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
159 if (filter && !ref_filter_match(refname, filter))
160 return 0;
162 if (starts_with(refname, git_replace_ref_base)) {
163 struct object_id original_oid;
164 if (!replace_refs_enabled(the_repository))
165 return 0;
166 if (get_oid_hex(refname + strlen(git_replace_ref_base),
167 &original_oid)) {
168 warning("invalid replace ref %s", refname);
169 return 0;
171 obj = parse_object(the_repository, &original_oid);
172 if (obj)
173 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
174 return 0;
177 objtype = oid_object_info(the_repository, oid, NULL);
178 if (objtype < 0)
179 return 0;
180 obj = lookup_object_by_type(the_repository, oid, objtype);
182 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
183 struct ref_namespace_info *info = &ref_namespace[i];
185 if (!info->decoration)
186 continue;
187 if (info->exact) {
188 if (!strcmp(refname, info->ref)) {
189 deco_type = info->decoration;
190 break;
192 } else if (starts_with(refname, info->ref)) {
193 deco_type = info->decoration;
194 break;
198 add_name_decoration(deco_type, refname, obj);
199 while (obj->type == OBJ_TAG) {
200 if (!obj->parsed)
201 parse_object(the_repository, &obj->oid);
202 obj = ((struct tag *)obj)->tagged;
203 if (!obj)
204 break;
205 add_name_decoration(DECORATION_REF_TAG, refname, obj);
207 return 0;
210 static int add_graft_decoration(const struct commit_graft *graft,
211 void *cb_data UNUSED)
213 struct commit *commit = lookup_commit(the_repository, &graft->oid);
214 if (!commit)
215 return 0;
216 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
217 return 0;
220 void load_ref_decorations(struct decoration_filter *filter, int flags)
222 if (!decoration_loaded) {
223 if (filter) {
224 struct string_list_item *item;
225 for_each_string_list_item(item, filter->exclude_ref_pattern) {
226 normalize_glob_ref(item, NULL, item->string);
228 for_each_string_list_item(item, filter->include_ref_pattern) {
229 normalize_glob_ref(item, NULL, item->string);
231 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
232 normalize_glob_ref(item, NULL, item->string);
235 decoration_loaded = 1;
236 decoration_flags = flags;
237 refs_for_each_ref(get_main_ref_store(the_repository),
238 add_ref_decoration, filter);
239 refs_head_ref(get_main_ref_store(the_repository),
240 add_ref_decoration, filter);
241 for_each_commit_graft(add_graft_decoration, filter);
245 static void show_parents(struct commit *commit, int abbrev, FILE *file)
247 struct commit_list *p;
248 for (p = commit->parents; p ; p = p->next) {
249 struct commit *parent = p->item;
250 fprintf(file, " %s",
251 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
255 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
257 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
258 for ( ; p; p = p->next) {
259 fprintf(opt->diffopt.file, " %s",
260 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
265 * Do we have HEAD in the output, and also the branch it points at?
266 * If so, find that decoration entry for that current branch.
268 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
270 const struct name_decoration *list, *head = NULL;
271 const char *branch_name = NULL;
272 int rru_flags;
274 /* First find HEAD */
275 for (list = decoration; list; list = list->next)
276 if (list->type == DECORATION_REF_HEAD) {
277 head = list;
278 break;
280 if (!head)
281 return NULL;
283 /* Now resolve and find the matching current branch */
284 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
285 "HEAD", 0, NULL, &rru_flags);
286 if (!branch_name || !(rru_flags & REF_ISSYMREF))
287 return NULL;
289 if (!starts_with(branch_name, "refs/"))
290 return NULL;
292 /* OK, do we have that ref in the list? */
293 for (list = decoration; list; list = list->next)
294 if ((list->type == DECORATION_REF_LOCAL) &&
295 !strcmp(branch_name, list->name)) {
296 return list;
299 return NULL;
302 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
304 if (decoration_flags == DECORATE_SHORT_REFS)
305 strbuf_addstr(sb, prettify_refname(decoration->name));
306 else
307 strbuf_addstr(sb, decoration->name);
311 * The caller makes sure there is no funny color before calling.
312 * format_decorations ensures the same after return.
314 void format_decorations(struct strbuf *sb,
315 const struct commit *commit,
316 int use_color,
317 const struct decoration_options *opts)
319 const struct name_decoration *decoration;
320 const struct name_decoration *current_and_HEAD;
321 const char *color_commit, *color_reset;
323 const char *prefix = " (";
324 const char *suffix = ")";
325 const char *separator = ", ";
326 const char *pointer = " -> ";
327 const char *tag = "tag: ";
329 decoration = get_name_decoration(&commit->object);
330 if (!decoration)
331 return;
333 if (opts) {
334 if (opts->prefix)
335 prefix = opts->prefix;
336 if (opts->suffix)
337 suffix = opts->suffix;
338 if (opts->separator)
339 separator = opts->separator;
340 if (opts->pointer)
341 pointer = opts->pointer;
342 if (opts->tag)
343 tag = opts->tag;
346 color_commit = diff_get_color(use_color, DIFF_COMMIT);
347 color_reset = decorate_get_color(use_color, DECORATION_NONE);
349 current_and_HEAD = current_pointed_by_HEAD(decoration);
350 while (decoration) {
352 * When both current and HEAD are there, only
353 * show HEAD->current where HEAD would have
354 * appeared, skipping the entry for current.
356 if (decoration != current_and_HEAD) {
357 const char *color =
358 decorate_get_color(use_color, decoration->type);
360 if (*prefix) {
361 strbuf_addstr(sb, color_commit);
362 strbuf_addstr(sb, prefix);
363 strbuf_addstr(sb, color_reset);
366 if (*tag && decoration->type == DECORATION_REF_TAG) {
367 strbuf_addstr(sb, color);
368 strbuf_addstr(sb, tag);
369 strbuf_addstr(sb, color_reset);
372 strbuf_addstr(sb, color);
373 show_name(sb, decoration);
374 strbuf_addstr(sb, color_reset);
376 if (current_and_HEAD &&
377 decoration->type == DECORATION_REF_HEAD) {
378 strbuf_addstr(sb, color_commit);
379 strbuf_addstr(sb, pointer);
380 strbuf_addstr(sb, color_reset);
381 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
382 show_name(sb, current_and_HEAD);
383 strbuf_addstr(sb, color_reset);
386 prefix = separator;
388 decoration = decoration->next;
390 if (*suffix) {
391 strbuf_addstr(sb, color_commit);
392 strbuf_addstr(sb, suffix);
393 strbuf_addstr(sb, color_reset);
397 void show_decorations(struct rev_info *opt, struct commit *commit)
399 struct strbuf sb = STRBUF_INIT;
401 if (opt->sources) {
402 char **slot = revision_sources_peek(opt->sources, commit);
404 if (slot && *slot)
405 fprintf(opt->diffopt.file, "\t%s", *slot);
407 if (!opt->show_decorations)
408 return;
409 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
410 fputs(sb.buf, opt->diffopt.file);
411 strbuf_release(&sb);
414 static unsigned int digits_in_number(unsigned int number)
416 unsigned int i = 10, result = 1;
417 while (i <= number) {
418 i *= 10;
419 result++;
421 return result;
424 void fmt_output_subject(struct strbuf *filename,
425 const char *subject,
426 struct rev_info *info)
428 const char *suffix = info->patch_suffix;
429 int nr = info->nr;
430 int start_len = filename->len;
431 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
433 if (info->reroll_count) {
434 struct strbuf temp = STRBUF_INIT;
436 strbuf_addf(&temp, "v%s", info->reroll_count);
437 format_sanitized_subject(filename, temp.buf, temp.len);
438 strbuf_addstr(filename, "-");
439 strbuf_release(&temp);
441 strbuf_addf(filename, "%04d-%s", nr, subject);
443 if (max_len < filename->len)
444 strbuf_setlen(filename, max_len);
445 strbuf_addstr(filename, suffix);
448 void fmt_output_commit(struct strbuf *filename,
449 struct commit *commit,
450 struct rev_info *info)
452 struct pretty_print_context ctx = {0};
453 struct strbuf subject = STRBUF_INIT;
455 repo_format_commit_message(the_repository, commit, "%f", &subject,
456 &ctx);
457 fmt_output_subject(filename, subject.buf, info);
458 strbuf_release(&subject);
461 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
463 if (opt->total > 0) {
464 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
465 opt->subject_prefix,
466 *opt->subject_prefix ? " " : "",
467 digits_in_number(opt->total),
468 opt->nr, opt->total);
469 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
470 strbuf_addf(sb, "Subject: [%s] ",
471 opt->subject_prefix);
472 } else {
473 strbuf_addstr(sb, "Subject: ");
477 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
478 char **extra_headers_p,
479 int *need_8bit_cte_p,
480 int maybe_multipart)
482 struct strbuf headers = STRBUF_INIT;
483 const char *name = oid_to_hex(opt->zero_commit ?
484 null_oid() : &commit->object.oid);
486 *need_8bit_cte_p = 0; /* unknown */
488 if (opt->extra_headers && *opt->extra_headers)
489 strbuf_addstr(&headers, opt->extra_headers);
491 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
492 graph_show_oneline(opt->graph);
493 if (opt->message_id) {
494 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
495 graph_show_oneline(opt->graph);
497 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
498 int i, n;
499 n = opt->ref_message_ids->nr;
500 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
501 for (i = 0; i < n; i++)
502 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
503 opt->ref_message_ids->items[i].string);
504 graph_show_oneline(opt->graph);
506 if (opt->mime_boundary && maybe_multipart) {
507 static struct strbuf buffer = STRBUF_INIT;
508 struct strbuf filename = STRBUF_INIT;
509 *need_8bit_cte_p = -1; /* NEVER */
511 strbuf_reset(&buffer);
513 strbuf_addf(&headers,
514 "MIME-Version: 1.0\n"
515 "Content-Type: multipart/mixed;"
516 " boundary=\"%s%s\"\n"
517 "\n"
518 "This is a multi-part message in MIME "
519 "format.\n"
520 "--%s%s\n"
521 "Content-Type: text/plain; "
522 "charset=UTF-8; format=fixed\n"
523 "Content-Transfer-Encoding: 8bit\n\n",
524 mime_boundary_leader, opt->mime_boundary,
525 mime_boundary_leader, opt->mime_boundary);
527 if (opt->numbered_files)
528 strbuf_addf(&filename, "%d", opt->nr);
529 else
530 fmt_output_commit(&filename, commit, opt);
531 strbuf_addf(&buffer,
532 "\n--%s%s\n"
533 "Content-Type: text/x-patch;"
534 " name=\"%s\"\n"
535 "Content-Transfer-Encoding: 8bit\n"
536 "Content-Disposition: %s;"
537 " filename=\"%s\"\n\n",
538 mime_boundary_leader, opt->mime_boundary,
539 filename.buf,
540 opt->no_inline ? "attachment" : "inline",
541 filename.buf);
542 opt->diffopt.stat_sep = buffer.buf;
543 strbuf_release(&filename);
545 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
548 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
550 const char *color, *reset, *eol;
552 color = diff_get_color_opt(&opt->diffopt,
553 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
554 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
555 while (*bol) {
556 eol = strchrnul(bol, '\n');
557 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
558 *eol ? "\n" : "");
559 graph_show_oneline(opt->graph);
560 bol = (*eol) ? (eol + 1) : eol;
564 static void show_signature(struct rev_info *opt, struct commit *commit)
566 struct strbuf payload = STRBUF_INIT;
567 struct strbuf signature = STRBUF_INIT;
568 struct signature_check sigc = { 0 };
569 int status;
571 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
572 goto out;
574 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
575 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
576 status = check_signature(&sigc, signature.buf, signature.len);
577 if (status && !sigc.output)
578 show_sig_lines(opt, status, "No signature\n");
579 else
580 show_sig_lines(opt, status, sigc.output);
581 signature_check_clear(&sigc);
583 out:
584 strbuf_release(&payload);
585 strbuf_release(&signature);
588 static int which_parent(const struct object_id *oid, const struct commit *commit)
590 int nth;
591 const struct commit_list *parent;
593 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
594 if (oideq(&parent->item->object.oid, oid))
595 return nth;
596 nth++;
598 return -1;
601 static int is_common_merge(const struct commit *commit)
603 return (commit->parents
604 && commit->parents->next
605 && !commit->parents->next->next);
608 static int show_one_mergetag(struct commit *commit,
609 struct commit_extra_header *extra,
610 void *data)
612 struct rev_info *opt = (struct rev_info *)data;
613 struct object_id oid;
614 struct tag *tag;
615 struct strbuf verify_message;
616 struct signature_check sigc = { 0 };
617 int status, nth;
618 struct strbuf payload = STRBUF_INIT;
619 struct strbuf signature = STRBUF_INIT;
621 hash_object_file(the_hash_algo, extra->value, extra->len,
622 OBJ_TAG, &oid);
623 tag = lookup_tag(the_repository, &oid);
624 if (!tag)
625 return -1; /* error message already given */
627 strbuf_init(&verify_message, 256);
628 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
629 strbuf_addstr(&verify_message, "malformed mergetag\n");
630 else if (is_common_merge(commit) &&
631 oideq(&tag->tagged->oid,
632 &commit->parents->next->item->object.oid))
633 strbuf_addf(&verify_message,
634 "merged tag '%s'\n", tag->tag);
635 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
636 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
637 tag->tag, oid_to_hex(&tag->tagged->oid));
638 else
639 strbuf_addf(&verify_message,
640 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
642 status = -1;
643 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
644 /* could have a good signature */
645 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
646 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
647 status = check_signature(&sigc, signature.buf, signature.len);
648 if (sigc.output)
649 strbuf_addstr(&verify_message, sigc.output);
650 else
651 strbuf_addstr(&verify_message, "No signature\n");
652 signature_check_clear(&sigc);
653 /* otherwise we couldn't verify, which is shown as bad */
656 show_sig_lines(opt, status, verify_message.buf);
657 strbuf_release(&verify_message);
658 strbuf_release(&payload);
659 strbuf_release(&signature);
660 return 0;
663 static int show_mergetag(struct rev_info *opt, struct commit *commit)
665 return for_each_mergetag(show_one_mergetag, commit, opt);
668 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
670 const char *x = opt->shown_dashes ? "\n" : "---\n";
671 if (sb)
672 strbuf_addstr(sb, x);
673 else
674 fputs(x, opt->diffopt.file);
675 opt->shown_dashes = 1;
678 static void show_diff_of_diff(struct rev_info *opt)
680 if (!cmit_fmt_is_mail(opt->commit_format))
681 return;
683 if (opt->idiff_oid1) {
684 struct diff_queue_struct dq;
686 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
687 DIFF_QUEUE_CLEAR(&diff_queued_diff);
689 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
690 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
691 &opt->diffopt);
693 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
696 if (opt->rdiff1) {
697 struct diff_queue_struct dq;
698 struct diff_options opts;
699 struct range_diff_options range_diff_opts = {
700 .creation_factor = opt->creation_factor,
701 .dual_color = 1,
702 .diffopt = &opts
705 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
706 DIFF_QUEUE_CLEAR(&diff_queued_diff);
708 fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
710 * Pass minimum required diff-options to range-diff; others
711 * can be added later if deemed desirable.
713 repo_diff_setup(the_repository, &opts);
714 opts.file = opt->diffopt.file;
715 opts.use_color = opt->diffopt.use_color;
716 diff_setup_done(&opts);
717 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
719 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
723 void show_log(struct rev_info *opt)
725 struct strbuf msgbuf = STRBUF_INIT;
726 struct log_info *log = opt->loginfo;
727 struct commit *commit = log->commit, *parent = log->parent;
728 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
729 struct pretty_print_context ctx = {0};
731 opt->loginfo = NULL;
732 if (!opt->verbose_header) {
733 graph_show_commit(opt->graph);
735 if (!opt->graph)
736 put_revision_mark(opt, commit);
737 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
738 opt->diffopt.file);
739 if (opt->print_parents)
740 show_parents(commit, abbrev_commit, opt->diffopt.file);
741 if (opt->children.name)
742 show_children(opt, commit, abbrev_commit);
743 show_decorations(opt, commit);
744 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
745 putc('\n', opt->diffopt.file);
746 graph_show_remainder(opt->graph);
748 putc(opt->diffopt.line_termination, opt->diffopt.file);
749 return;
753 * If use_terminator is set, we already handled any record termination
754 * at the end of the last record.
755 * Otherwise, add a diffopt.line_termination character before all
756 * entries but the first. (IOW, as a separator between entries)
758 if (opt->shown_one && !opt->use_terminator) {
760 * If entries are separated by a newline, the output
761 * should look human-readable. If the last entry ended
762 * with a newline, print the graph output before this
763 * newline. Otherwise it will end up as a completely blank
764 * line and will look like a gap in the graph.
766 * If the entry separator is not a newline, the output is
767 * primarily intended for programmatic consumption, and we
768 * never want the extra graph output before the entry
769 * separator.
771 if (opt->diffopt.line_termination == '\n' &&
772 !opt->missing_newline)
773 graph_show_padding(opt->graph);
774 putc(opt->diffopt.line_termination, opt->diffopt.file);
776 opt->shown_one = 1;
779 * If the history graph was requested,
780 * print the graph, up to this commit's line
782 graph_show_commit(opt->graph);
785 * Print header line of header..
788 if (cmit_fmt_is_mail(opt->commit_format)) {
789 log_write_email_headers(opt, commit, &ctx.after_subject,
790 &ctx.need_8bit_cte, 1);
791 ctx.rev = opt;
792 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
793 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
794 if (opt->commit_format != CMIT_FMT_ONELINE)
795 fputs("commit ", opt->diffopt.file);
797 if (!opt->graph)
798 put_revision_mark(opt, commit);
799 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
800 abbrev_commit),
801 opt->diffopt.file);
802 if (opt->print_parents)
803 show_parents(commit, abbrev_commit, opt->diffopt.file);
804 if (opt->children.name)
805 show_children(opt, commit, abbrev_commit);
806 if (parent)
807 fprintf(opt->diffopt.file, " (from %s)",
808 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
809 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
810 show_decorations(opt, commit);
811 if (opt->commit_format == CMIT_FMT_ONELINE) {
812 putc(' ', opt->diffopt.file);
813 } else {
814 putc('\n', opt->diffopt.file);
815 graph_show_oneline(opt->graph);
817 if (opt->reflog_info) {
819 * setup_revisions() ensures that opt->reflog_info
820 * and opt->graph cannot both be set,
821 * so we don't need to worry about printing the
822 * graph info here.
824 show_reflog_message(opt->reflog_info,
825 opt->commit_format == CMIT_FMT_ONELINE,
826 opt->date_mode,
827 opt->date_mode_explicit);
828 if (opt->commit_format == CMIT_FMT_ONELINE)
829 return;
833 if (opt->show_signature) {
834 show_signature(opt, commit);
835 show_mergetag(opt, commit);
838 if (opt->show_notes) {
839 int raw;
840 struct strbuf notebuf = STRBUF_INIT;
842 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
843 format_display_notes(&commit->object.oid, &notebuf,
844 get_log_output_encoding(), raw);
845 ctx.notes_message = strbuf_detach(&notebuf, NULL);
849 * And then the pretty-printed message itself
851 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
852 ctx.need_8bit_cte =
853 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
854 ctx.date_mode = opt->date_mode;
855 ctx.date_mode_explicit = opt->date_mode_explicit;
856 ctx.abbrev = opt->diffopt.abbrev;
857 ctx.preserve_subject = opt->preserve_subject;
858 ctx.encode_email_headers = opt->encode_email_headers;
859 ctx.reflog_info = opt->reflog_info;
860 ctx.fmt = opt->commit_format;
861 ctx.mailmap = opt->mailmap;
862 ctx.color = opt->diffopt.use_color;
863 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
864 ctx.output_encoding = get_log_output_encoding();
865 ctx.rev = opt;
866 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
867 ctx.from_ident = &opt->from_ident;
868 if (opt->graph)
869 ctx.graph_width = graph_width(opt->graph);
870 pretty_print_commit(&ctx, commit, &msgbuf);
872 if (opt->add_signoff)
873 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
875 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
876 ctx.notes_message && *ctx.notes_message) {
877 if (cmit_fmt_is_mail(ctx.fmt))
878 next_commentary_block(opt, &msgbuf);
879 strbuf_addstr(&msgbuf, ctx.notes_message);
882 if (opt->show_log_size) {
883 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
884 graph_show_oneline(opt->graph);
888 * Set opt->missing_newline if msgbuf doesn't
889 * end in a newline (including if it is empty)
891 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
892 opt->missing_newline = 1;
893 else
894 opt->missing_newline = 0;
896 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
897 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
898 if (!opt->missing_newline)
899 graph_show_padding(opt->graph);
900 putc(opt->diffopt.line_termination, opt->diffopt.file);
903 strbuf_release(&msgbuf);
904 free(ctx.notes_message);
905 free(ctx.after_subject);
908 int log_tree_diff_flush(struct rev_info *opt)
910 opt->shown_dashes = 0;
911 diffcore_std(&opt->diffopt);
913 if (diff_queue_is_empty(&opt->diffopt)) {
914 int saved_fmt = opt->diffopt.output_format;
915 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
916 diff_flush(&opt->diffopt);
917 opt->diffopt.output_format = saved_fmt;
918 return 0;
921 if (opt->loginfo && !opt->no_commit_id) {
922 show_log(opt);
923 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
924 opt->verbose_header &&
925 opt->commit_format != CMIT_FMT_ONELINE &&
926 !commit_format_is_empty(opt->commit_format)) {
928 * When showing a verbose header (i.e. log message),
929 * and not in --pretty=oneline format, we would want
930 * an extra newline between the end of log and the
931 * diff/diffstat output for readability.
933 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
934 if (opt->diffopt.output_prefix) {
935 struct strbuf *msg = NULL;
936 msg = opt->diffopt.output_prefix(&opt->diffopt,
937 opt->diffopt.output_prefix_data);
938 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
942 * We may have shown three-dashes line early
943 * between generated commentary (notes, etc.)
944 * and the log message, in which case we only
945 * want a blank line after the commentary
946 * without (an extra) three-dashes line.
947 * Otherwise, we show the three-dashes line if
948 * we are showing the patch with diffstat, but
949 * in that case, there is no extra blank line
950 * after the three-dashes line.
952 if (!opt->shown_dashes &&
953 (pch & opt->diffopt.output_format) == pch)
954 fprintf(opt->diffopt.file, "---");
955 putc('\n', opt->diffopt.file);
958 diff_flush(&opt->diffopt);
959 return 1;
962 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
964 diff_tree_combined_merge(commit, opt);
965 return !opt->loginfo;
968 static void setup_additional_headers(struct diff_options *o,
969 struct strmap *all_headers)
971 struct hashmap_iter iter;
972 struct strmap_entry *entry;
975 * Make o->additional_path_headers contain the subset of all_headers
976 * that match o->pathspec. If there aren't any that match o->pathspec,
977 * then make o->additional_path_headers be NULL.
980 if (!o->pathspec.nr) {
981 o->additional_path_headers = all_headers;
982 return;
985 o->additional_path_headers = xmalloc(sizeof(struct strmap));
986 strmap_init_with_options(o->additional_path_headers, NULL, 0);
987 strmap_for_each_entry(all_headers, &iter, entry) {
988 if (match_pathspec(the_repository->index, &o->pathspec,
989 entry->key, strlen(entry->key),
990 0 /* prefix */, NULL /* seen */,
991 0 /* is_dir */))
992 strmap_put(o->additional_path_headers,
993 entry->key, entry->value);
995 if (!strmap_get_size(o->additional_path_headers)) {
996 strmap_clear(o->additional_path_headers, 0);
997 FREE_AND_NULL(o->additional_path_headers);
1001 static void cleanup_additional_headers(struct diff_options *o)
1003 if (!o->pathspec.nr) {
1004 o->additional_path_headers = NULL;
1005 return;
1007 if (!o->additional_path_headers)
1008 return;
1010 strmap_clear(o->additional_path_headers, 0);
1011 FREE_AND_NULL(o->additional_path_headers);
1014 static int do_remerge_diff(struct rev_info *opt,
1015 struct commit_list *parents,
1016 struct object_id *oid)
1018 struct merge_options o;
1019 struct commit_list *bases = NULL;
1020 struct merge_result res = {0};
1021 struct pretty_print_context ctx = {0};
1022 struct commit *parent1 = parents->item;
1023 struct commit *parent2 = parents->next->item;
1024 struct strbuf parent1_desc = STRBUF_INIT;
1025 struct strbuf parent2_desc = STRBUF_INIT;
1027 /* Setup merge options */
1028 init_merge_options(&o, the_repository);
1029 o.show_rename_progress = 0;
1030 o.record_conflict_msgs_as_headers = 1;
1031 o.msg_header_prefix = "remerge";
1033 ctx.abbrev = DEFAULT_ABBREV;
1034 repo_format_commit_message(the_repository, parent1, "%h (%s)",
1035 &parent1_desc, &ctx);
1036 repo_format_commit_message(the_repository, parent2, "%h (%s)",
1037 &parent2_desc, &ctx);
1038 o.branch1 = parent1_desc.buf;
1039 o.branch2 = parent2_desc.buf;
1041 /* Parse the relevant commits and get the merge bases */
1042 parse_commit_or_die(parent1);
1043 parse_commit_or_die(parent2);
1044 if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)
1045 exit(128);
1047 /* Re-merge the parents */
1048 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1050 /* Show the diff */
1051 setup_additional_headers(&opt->diffopt, res.path_messages);
1052 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1053 log_tree_diff_flush(opt);
1055 /* Cleanup */
1056 free_commit_list(bases);
1057 cleanup_additional_headers(&opt->diffopt);
1058 strbuf_release(&parent1_desc);
1059 strbuf_release(&parent2_desc);
1060 merge_finalize(&o, &res);
1062 /* Clean up the contents of the temporary object directory */
1063 if (opt->remerge_objdir)
1064 tmp_objdir_discard_objects(opt->remerge_objdir);
1065 else
1066 BUG("did a remerge diff without remerge_objdir?!?");
1068 return !opt->loginfo;
1072 * Show the diff of a commit.
1074 * Return true if we printed any log info messages
1076 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1078 int showed_log;
1079 struct commit_list *parents;
1080 struct object_id *oid;
1081 int is_merge;
1082 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1084 if (!all_need_diff && !opt->merges_need_diff)
1085 return 0;
1087 parse_commit_or_die(commit);
1088 oid = get_commit_tree_oid(commit);
1090 parents = get_saved_parents(opt, commit);
1091 is_merge = parents && parents->next;
1092 if (!is_merge && !all_need_diff)
1093 return 0;
1095 /* Root commit? */
1096 if (!parents) {
1097 if (opt->show_root_diff) {
1098 diff_root_tree_oid(oid, "", &opt->diffopt);
1099 log_tree_diff_flush(opt);
1101 return !opt->loginfo;
1104 if (is_merge) {
1105 int octopus = (parents->next->next != NULL);
1107 if (opt->remerge_diff) {
1108 if (octopus) {
1109 show_log(opt);
1110 fprintf(opt->diffopt.file,
1111 "diff: warning: Skipping remerge-diff "
1112 "for octopus merges.\n");
1113 return 1;
1115 return do_remerge_diff(opt, parents, oid);
1117 if (opt->combine_merges)
1118 return do_diff_combined(opt, commit);
1119 if (opt->separate_merges) {
1120 if (!opt->first_parent_merges) {
1121 /* Show parent info for multiple diffs */
1122 log->parent = parents->item;
1124 } else
1125 return 0;
1128 showed_log = 0;
1129 for (;;) {
1130 struct commit *parent = parents->item;
1132 parse_commit_or_die(parent);
1133 diff_tree_oid(get_commit_tree_oid(parent),
1134 oid, "", &opt->diffopt);
1135 log_tree_diff_flush(opt);
1137 showed_log |= !opt->loginfo;
1139 /* Set up the log info for the next parent, if any.. */
1140 parents = parents->next;
1141 if (!parents || opt->first_parent_merges)
1142 break;
1143 log->parent = parents->item;
1144 opt->loginfo = log;
1146 return showed_log;
1149 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1151 struct log_info log;
1152 int shown;
1153 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1154 int no_free = opt->diffopt.no_free;
1156 log.commit = commit;
1157 log.parent = NULL;
1158 opt->loginfo = &log;
1159 opt->diffopt.no_free = 1;
1161 /* NEEDSWORK: no restoring of no_free? Why? */
1162 if (opt->line_level_traverse)
1163 return line_log_print(opt, commit);
1165 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1166 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1167 shown = log_tree_diff(opt, commit, &log);
1168 if (!shown && opt->loginfo && opt->always_show_header) {
1169 log.parent = NULL;
1170 show_log(opt);
1171 shown = 1;
1173 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1174 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1175 if (shown)
1176 show_diff_of_diff(opt);
1177 opt->loginfo = NULL;
1178 maybe_flush_or_die(opt->diffopt.file, "stdout");
1179 opt->diffopt.no_free = no_free;
1181 diff_free(&opt->diffopt);
1182 return shown;