]> git.ipfire.org Git - thirdparty/git.git/blob - log-tree.c
decorate: avoid some unnecessary color overhead
[thirdparty/git.git] / log-tree.c
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"
31
32 static struct decoration name_decoration = { "object names" };
33 static int decoration_loaded;
34 static int decoration_flags;
35
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 */
44 };
45
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",
53 };
54
55 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
56 {
57 if (want_color(decorate_use_color))
58 return decoration_colors[ix];
59 return "";
60 }
61
62 define_list_config_array(color_decorate_slots);
63
64 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
65 {
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]);
72 }
73
74 /*
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
77 */
78 #define decorate_get_color_opt(o, ix) \
79 decorate_get_color((o)->use_color, ix)
80
81 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
82 {
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);
87 }
88
89 const struct name_decoration *get_name_decoration(const struct object *obj)
90 {
91 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
92 return lookup_decoration(&name_decoration, obj);
93 }
94
95 static int match_ref_pattern(const char *refname,
96 const struct string_list_item *item)
97 {
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;
107 }
108 return matched;
109 }
110
111 static int ref_filter_match(const char *refname,
112 const struct decoration_filter *filter)
113 {
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;
119
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;
124 }
125 }
126
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;
131 }
132 return 0;
133 }
134
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;
139 }
140 }
141
142 return 1;
143 }
144
145 static int add_ref_decoration(const char *refname, const struct object_id *oid,
146 int flags UNUSED,
147 void *cb_data)
148 {
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;
155
156 if (filter && !ref_filter_match(refname, filter))
157 return 0;
158
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;
167 }
168 obj = parse_object(the_repository, &original_oid);
169 if (obj)
170 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
171 return 0;
172 }
173
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);
178
179 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
180 struct ref_namespace_info *info = &ref_namespace[i];
181
182 if (!info->decoration)
183 continue;
184 if (info->exact) {
185 if (!strcmp(refname, info->ref)) {
186 deco_type = info->decoration;
187 break;
188 }
189 } else if (starts_with(refname, info->ref)) {
190 deco_type = info->decoration;
191 break;
192 }
193 }
194
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);
203 }
204 return 0;
205 }
206
207 static int add_graft_decoration(const struct commit_graft *graft,
208 void *cb_data UNUSED)
209 {
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;
215 }
216
217 void load_ref_decorations(struct decoration_filter *filter, int flags)
218 {
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);
224 }
225 for_each_string_list_item(item, filter->include_ref_pattern) {
226 normalize_glob_ref(item, NULL, item->string);
227 }
228 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
229 normalize_glob_ref(item, NULL, item->string);
230 }
231 }
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);
237 }
238 }
239
240 static void show_parents(struct commit *commit, int abbrev, FILE *file)
241 {
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));
247 }
248 }
249
250 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
251 {
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));
256 }
257 }
258
259 /*
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.
262 */
263 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
264 {
265 const struct name_decoration *list, *head = NULL;
266 const char *branch_name = NULL;
267 int rru_flags;
268
269 /* First find HEAD */
270 for (list = decoration; list; list = list->next)
271 if (list->type == DECORATION_REF_HEAD) {
272 head = list;
273 break;
274 }
275 if (!head)
276 return NULL;
277
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;
282
283 if (!starts_with(branch_name, "refs/"))
284 return NULL;
285
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;
291 }
292
293 return NULL;
294 }
295
296 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
297 {
298 if (decoration_flags == DECORATE_SHORT_REFS)
299 strbuf_addstr(sb, prettify_refname(decoration->name));
300 else
301 strbuf_addstr(sb, decoration->name);
302 }
303
304 /*
305 * The caller makes sure there is no funny color before calling.
306 * format_decorations ensures the same after return.
307 */
308 void format_decorations(struct strbuf *sb,
309 const struct commit *commit,
310 int use_color,
311 const struct decoration_options *opts)
312 {
313 const struct name_decoration *decoration;
314 const struct name_decoration *current_and_HEAD;
315 const char *color_commit, *color_reset;
316
317 const char *prefix = " (";
318 const char *suffix = ")";
319 const char *separator = ", ";
320
321 decoration = get_name_decoration(&commit->object);
322 if (!decoration)
323 return;
324
325 if (opts) {
326 if (opts->prefix)
327 prefix = opts->prefix;
328 if (opts->suffix)
329 suffix = opts->suffix;
330 if (opts->separator)
331 separator = opts->separator;
332 }
333
334 color_commit = diff_get_color(use_color, DIFF_COMMIT);
335 color_reset = decorate_get_color(use_color, DECORATION_NONE);
336
337 current_and_HEAD = current_pointed_by_HEAD(decoration);
338 while (decoration) {
339 /*
340 * When both current and HEAD are there, only
341 * show HEAD->current where HEAD would have
342 * appeared, skipping the entry for current.
343 */
344 if (decoration != current_and_HEAD) {
345 if (*prefix) {
346 strbuf_addstr(sb, color_commit);
347 strbuf_addstr(sb, prefix);
348 strbuf_addstr(sb, color_reset);
349 }
350
351 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
352 if (decoration->type == DECORATION_REF_TAG)
353 strbuf_addstr(sb, "tag: ");
354
355 show_name(sb, decoration);
356
357 if (current_and_HEAD &&
358 decoration->type == DECORATION_REF_HEAD) {
359 strbuf_addstr(sb, " -> ");
360 strbuf_addstr(sb, color_reset);
361 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
362 show_name(sb, current_and_HEAD);
363 }
364 strbuf_addstr(sb, color_reset);
365
366 prefix = separator;
367 }
368 decoration = decoration->next;
369 }
370 if (*suffix) {
371 strbuf_addstr(sb, color_commit);
372 strbuf_addstr(sb, suffix);
373 strbuf_addstr(sb, color_reset);
374 }
375 }
376
377 void show_decorations(struct rev_info *opt, struct commit *commit)
378 {
379 struct strbuf sb = STRBUF_INIT;
380
381 if (opt->sources) {
382 char **slot = revision_sources_peek(opt->sources, commit);
383
384 if (slot && *slot)
385 fprintf(opt->diffopt.file, "\t%s", *slot);
386 }
387 if (!opt->show_decorations)
388 return;
389 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
390 fputs(sb.buf, opt->diffopt.file);
391 strbuf_release(&sb);
392 }
393
394 static unsigned int digits_in_number(unsigned int number)
395 {
396 unsigned int i = 10, result = 1;
397 while (i <= number) {
398 i *= 10;
399 result++;
400 }
401 return result;
402 }
403
404 void fmt_output_subject(struct strbuf *filename,
405 const char *subject,
406 struct rev_info *info)
407 {
408 const char *suffix = info->patch_suffix;
409 int nr = info->nr;
410 int start_len = filename->len;
411 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
412
413 if (info->reroll_count) {
414 struct strbuf temp = STRBUF_INIT;
415
416 strbuf_addf(&temp, "v%s", info->reroll_count);
417 format_sanitized_subject(filename, temp.buf, temp.len);
418 strbuf_addstr(filename, "-");
419 strbuf_release(&temp);
420 }
421 strbuf_addf(filename, "%04d-%s", nr, subject);
422
423 if (max_len < filename->len)
424 strbuf_setlen(filename, max_len);
425 strbuf_addstr(filename, suffix);
426 }
427
428 void fmt_output_commit(struct strbuf *filename,
429 struct commit *commit,
430 struct rev_info *info)
431 {
432 struct pretty_print_context ctx = {0};
433 struct strbuf subject = STRBUF_INIT;
434
435 repo_format_commit_message(the_repository, commit, "%f", &subject,
436 &ctx);
437 fmt_output_subject(filename, subject.buf, info);
438 strbuf_release(&subject);
439 }
440
441 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
442 {
443 if (opt->total > 0) {
444 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
445 opt->subject_prefix,
446 *opt->subject_prefix ? " " : "",
447 digits_in_number(opt->total),
448 opt->nr, opt->total);
449 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
450 strbuf_addf(sb, "Subject: [%s] ",
451 opt->subject_prefix);
452 } else {
453 strbuf_addstr(sb, "Subject: ");
454 }
455 }
456
457 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
458 const char **extra_headers_p,
459 int *need_8bit_cte_p,
460 int maybe_multipart)
461 {
462 const char *extra_headers = opt->extra_headers;
463 const char *name = oid_to_hex(opt->zero_commit ?
464 null_oid() : &commit->object.oid);
465
466 *need_8bit_cte_p = 0; /* unknown */
467
468 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
469 graph_show_oneline(opt->graph);
470 if (opt->message_id) {
471 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
472 graph_show_oneline(opt->graph);
473 }
474 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
475 int i, n;
476 n = opt->ref_message_ids->nr;
477 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
478 for (i = 0; i < n; i++)
479 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
480 opt->ref_message_ids->items[i].string);
481 graph_show_oneline(opt->graph);
482 }
483 if (opt->mime_boundary && maybe_multipart) {
484 static struct strbuf subject_buffer = STRBUF_INIT;
485 static struct strbuf buffer = STRBUF_INIT;
486 struct strbuf filename = STRBUF_INIT;
487 *need_8bit_cte_p = -1; /* NEVER */
488
489 strbuf_reset(&subject_buffer);
490 strbuf_reset(&buffer);
491
492 strbuf_addf(&subject_buffer,
493 "%s"
494 "MIME-Version: 1.0\n"
495 "Content-Type: multipart/mixed;"
496 " boundary=\"%s%s\"\n"
497 "\n"
498 "This is a multi-part message in MIME "
499 "format.\n"
500 "--%s%s\n"
501 "Content-Type: text/plain; "
502 "charset=UTF-8; format=fixed\n"
503 "Content-Transfer-Encoding: 8bit\n\n",
504 extra_headers ? extra_headers : "",
505 mime_boundary_leader, opt->mime_boundary,
506 mime_boundary_leader, opt->mime_boundary);
507 extra_headers = subject_buffer.buf;
508
509 if (opt->numbered_files)
510 strbuf_addf(&filename, "%d", opt->nr);
511 else
512 fmt_output_commit(&filename, commit, opt);
513 strbuf_addf(&buffer,
514 "\n--%s%s\n"
515 "Content-Type: text/x-patch;"
516 " name=\"%s\"\n"
517 "Content-Transfer-Encoding: 8bit\n"
518 "Content-Disposition: %s;"
519 " filename=\"%s\"\n\n",
520 mime_boundary_leader, opt->mime_boundary,
521 filename.buf,
522 opt->no_inline ? "attachment" : "inline",
523 filename.buf);
524 opt->diffopt.stat_sep = buffer.buf;
525 strbuf_release(&filename);
526 }
527 *extra_headers_p = extra_headers;
528 }
529
530 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
531 {
532 const char *color, *reset, *eol;
533
534 color = diff_get_color_opt(&opt->diffopt,
535 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
536 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
537 while (*bol) {
538 eol = strchrnul(bol, '\n');
539 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
540 *eol ? "\n" : "");
541 graph_show_oneline(opt->graph);
542 bol = (*eol) ? (eol + 1) : eol;
543 }
544 }
545
546 static void show_signature(struct rev_info *opt, struct commit *commit)
547 {
548 struct strbuf payload = STRBUF_INIT;
549 struct strbuf signature = STRBUF_INIT;
550 struct signature_check sigc = { 0 };
551 int status;
552
553 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
554 goto out;
555
556 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
557 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
558 status = check_signature(&sigc, signature.buf, signature.len);
559 if (status && !sigc.output)
560 show_sig_lines(opt, status, "No signature\n");
561 else
562 show_sig_lines(opt, status, sigc.output);
563 signature_check_clear(&sigc);
564
565 out:
566 strbuf_release(&payload);
567 strbuf_release(&signature);
568 }
569
570 static int which_parent(const struct object_id *oid, const struct commit *commit)
571 {
572 int nth;
573 const struct commit_list *parent;
574
575 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
576 if (oideq(&parent->item->object.oid, oid))
577 return nth;
578 nth++;
579 }
580 return -1;
581 }
582
583 static int is_common_merge(const struct commit *commit)
584 {
585 return (commit->parents
586 && commit->parents->next
587 && !commit->parents->next->next);
588 }
589
590 static int show_one_mergetag(struct commit *commit,
591 struct commit_extra_header *extra,
592 void *data)
593 {
594 struct rev_info *opt = (struct rev_info *)data;
595 struct object_id oid;
596 struct tag *tag;
597 struct strbuf verify_message;
598 struct signature_check sigc = { 0 };
599 int status, nth;
600 struct strbuf payload = STRBUF_INIT;
601 struct strbuf signature = STRBUF_INIT;
602
603 hash_object_file(the_hash_algo, extra->value, extra->len,
604 OBJ_TAG, &oid);
605 tag = lookup_tag(the_repository, &oid);
606 if (!tag)
607 return -1; /* error message already given */
608
609 strbuf_init(&verify_message, 256);
610 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
611 strbuf_addstr(&verify_message, "malformed mergetag\n");
612 else if (is_common_merge(commit) &&
613 oideq(&tag->tagged->oid,
614 &commit->parents->next->item->object.oid))
615 strbuf_addf(&verify_message,
616 "merged tag '%s'\n", tag->tag);
617 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
618 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
619 tag->tag, oid_to_hex(&tag->tagged->oid));
620 else
621 strbuf_addf(&verify_message,
622 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
623
624 status = -1;
625 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
626 /* could have a good signature */
627 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
628 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
629 status = check_signature(&sigc, signature.buf, signature.len);
630 if (sigc.output)
631 strbuf_addstr(&verify_message, sigc.output);
632 else
633 strbuf_addstr(&verify_message, "No signature\n");
634 signature_check_clear(&sigc);
635 /* otherwise we couldn't verify, which is shown as bad */
636 }
637
638 show_sig_lines(opt, status, verify_message.buf);
639 strbuf_release(&verify_message);
640 strbuf_release(&payload);
641 strbuf_release(&signature);
642 return 0;
643 }
644
645 static int show_mergetag(struct rev_info *opt, struct commit *commit)
646 {
647 return for_each_mergetag(show_one_mergetag, commit, opt);
648 }
649
650 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
651 {
652 const char *x = opt->shown_dashes ? "\n" : "---\n";
653 if (sb)
654 strbuf_addstr(sb, x);
655 else
656 fputs(x, opt->diffopt.file);
657 opt->shown_dashes = 1;
658 }
659
660 void show_log(struct rev_info *opt)
661 {
662 struct strbuf msgbuf = STRBUF_INIT;
663 struct log_info *log = opt->loginfo;
664 struct commit *commit = log->commit, *parent = log->parent;
665 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
666 const char *extra_headers = opt->extra_headers;
667 struct pretty_print_context ctx = {0};
668
669 opt->loginfo = NULL;
670 if (!opt->verbose_header) {
671 graph_show_commit(opt->graph);
672
673 if (!opt->graph)
674 put_revision_mark(opt, commit);
675 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
676 opt->diffopt.file);
677 if (opt->print_parents)
678 show_parents(commit, abbrev_commit, opt->diffopt.file);
679 if (opt->children.name)
680 show_children(opt, commit, abbrev_commit);
681 show_decorations(opt, commit);
682 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
683 putc('\n', opt->diffopt.file);
684 graph_show_remainder(opt->graph);
685 }
686 putc(opt->diffopt.line_termination, opt->diffopt.file);
687 return;
688 }
689
690 /*
691 * If use_terminator is set, we already handled any record termination
692 * at the end of the last record.
693 * Otherwise, add a diffopt.line_termination character before all
694 * entries but the first. (IOW, as a separator between entries)
695 */
696 if (opt->shown_one && !opt->use_terminator) {
697 /*
698 * If entries are separated by a newline, the output
699 * should look human-readable. If the last entry ended
700 * with a newline, print the graph output before this
701 * newline. Otherwise it will end up as a completely blank
702 * line and will look like a gap in the graph.
703 *
704 * If the entry separator is not a newline, the output is
705 * primarily intended for programmatic consumption, and we
706 * never want the extra graph output before the entry
707 * separator.
708 */
709 if (opt->diffopt.line_termination == '\n' &&
710 !opt->missing_newline)
711 graph_show_padding(opt->graph);
712 putc(opt->diffopt.line_termination, opt->diffopt.file);
713 }
714 opt->shown_one = 1;
715
716 /*
717 * If the history graph was requested,
718 * print the graph, up to this commit's line
719 */
720 graph_show_commit(opt->graph);
721
722 /*
723 * Print header line of header..
724 */
725
726 if (cmit_fmt_is_mail(opt->commit_format)) {
727 log_write_email_headers(opt, commit, &extra_headers,
728 &ctx.need_8bit_cte, 1);
729 ctx.rev = opt;
730 ctx.print_email_subject = 1;
731 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
732 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
733 if (opt->commit_format != CMIT_FMT_ONELINE)
734 fputs("commit ", opt->diffopt.file);
735
736 if (!opt->graph)
737 put_revision_mark(opt, commit);
738 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
739 abbrev_commit),
740 opt->diffopt.file);
741 if (opt->print_parents)
742 show_parents(commit, abbrev_commit, opt->diffopt.file);
743 if (opt->children.name)
744 show_children(opt, commit, abbrev_commit);
745 if (parent)
746 fprintf(opt->diffopt.file, " (from %s)",
747 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
748 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
749 show_decorations(opt, commit);
750 if (opt->commit_format == CMIT_FMT_ONELINE) {
751 putc(' ', opt->diffopt.file);
752 } else {
753 putc('\n', opt->diffopt.file);
754 graph_show_oneline(opt->graph);
755 }
756 if (opt->reflog_info) {
757 /*
758 * setup_revisions() ensures that opt->reflog_info
759 * and opt->graph cannot both be set,
760 * so we don't need to worry about printing the
761 * graph info here.
762 */
763 show_reflog_message(opt->reflog_info,
764 opt->commit_format == CMIT_FMT_ONELINE,
765 &opt->date_mode,
766 opt->date_mode_explicit);
767 if (opt->commit_format == CMIT_FMT_ONELINE)
768 return;
769 }
770 }
771
772 if (opt->show_signature) {
773 show_signature(opt, commit);
774 show_mergetag(opt, commit);
775 }
776
777 if (opt->show_notes) {
778 int raw;
779 struct strbuf notebuf = STRBUF_INIT;
780
781 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
782 format_display_notes(&commit->object.oid, &notebuf,
783 get_log_output_encoding(), raw);
784 ctx.notes_message = strbuf_detach(&notebuf, NULL);
785 }
786
787 /*
788 * And then the pretty-printed message itself
789 */
790 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
791 ctx.need_8bit_cte =
792 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
793 ctx.date_mode = opt->date_mode;
794 ctx.date_mode_explicit = opt->date_mode_explicit;
795 ctx.abbrev = opt->diffopt.abbrev;
796 ctx.after_subject = extra_headers;
797 ctx.preserve_subject = opt->preserve_subject;
798 ctx.encode_email_headers = opt->encode_email_headers;
799 ctx.reflog_info = opt->reflog_info;
800 ctx.fmt = opt->commit_format;
801 ctx.mailmap = opt->mailmap;
802 ctx.color = opt->diffopt.use_color;
803 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
804 ctx.output_encoding = get_log_output_encoding();
805 ctx.rev = opt;
806 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
807 ctx.from_ident = &opt->from_ident;
808 if (opt->graph)
809 ctx.graph_width = graph_width(opt->graph);
810 pretty_print_commit(&ctx, commit, &msgbuf);
811
812 if (opt->add_signoff)
813 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
814
815 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
816 ctx.notes_message && *ctx.notes_message) {
817 if (cmit_fmt_is_mail(ctx.fmt))
818 next_commentary_block(opt, &msgbuf);
819 strbuf_addstr(&msgbuf, ctx.notes_message);
820 }
821
822 if (opt->show_log_size) {
823 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
824 graph_show_oneline(opt->graph);
825 }
826
827 /*
828 * Set opt->missing_newline if msgbuf doesn't
829 * end in a newline (including if it is empty)
830 */
831 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
832 opt->missing_newline = 1;
833 else
834 opt->missing_newline = 0;
835
836 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
837 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
838 if (!opt->missing_newline)
839 graph_show_padding(opt->graph);
840 putc(opt->diffopt.line_termination, opt->diffopt.file);
841 }
842
843 strbuf_release(&msgbuf);
844 free(ctx.notes_message);
845
846 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
847 struct diff_queue_struct dq;
848
849 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
850 DIFF_QUEUE_CLEAR(&diff_queued_diff);
851
852 next_commentary_block(opt, NULL);
853 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
854 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
855 &opt->diffopt);
856
857 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
858 }
859
860 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
861 struct diff_queue_struct dq;
862 struct diff_options opts;
863 struct range_diff_options range_diff_opts = {
864 .creation_factor = opt->creation_factor,
865 .dual_color = 1,
866 .diffopt = &opts
867 };
868
869 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
870 DIFF_QUEUE_CLEAR(&diff_queued_diff);
871
872 next_commentary_block(opt, NULL);
873 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
874 /*
875 * Pass minimum required diff-options to range-diff; others
876 * can be added later if deemed desirable.
877 */
878 repo_diff_setup(the_repository, &opts);
879 opts.file = opt->diffopt.file;
880 opts.use_color = opt->diffopt.use_color;
881 diff_setup_done(&opts);
882 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
883
884 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
885 }
886 }
887
888 int log_tree_diff_flush(struct rev_info *opt)
889 {
890 opt->shown_dashes = 0;
891 diffcore_std(&opt->diffopt);
892
893 if (diff_queue_is_empty(&opt->diffopt)) {
894 int saved_fmt = opt->diffopt.output_format;
895 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
896 diff_flush(&opt->diffopt);
897 opt->diffopt.output_format = saved_fmt;
898 return 0;
899 }
900
901 if (opt->loginfo && !opt->no_commit_id) {
902 show_log(opt);
903 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
904 opt->verbose_header &&
905 opt->commit_format != CMIT_FMT_ONELINE &&
906 !commit_format_is_empty(opt->commit_format)) {
907 /*
908 * When showing a verbose header (i.e. log message),
909 * and not in --pretty=oneline format, we would want
910 * an extra newline between the end of log and the
911 * diff/diffstat output for readability.
912 */
913 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
914 if (opt->diffopt.output_prefix) {
915 struct strbuf *msg = NULL;
916 msg = opt->diffopt.output_prefix(&opt->diffopt,
917 opt->diffopt.output_prefix_data);
918 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
919 }
920
921 /*
922 * We may have shown three-dashes line early
923 * between generated commentary (notes, etc.)
924 * and the log message, in which case we only
925 * want a blank line after the commentary
926 * without (an extra) three-dashes line.
927 * Otherwise, we show the three-dashes line if
928 * we are showing the patch with diffstat, but
929 * in that case, there is no extra blank line
930 * after the three-dashes line.
931 */
932 if (!opt->shown_dashes &&
933 (pch & opt->diffopt.output_format) == pch)
934 fprintf(opt->diffopt.file, "---");
935 putc('\n', opt->diffopt.file);
936 }
937 }
938 diff_flush(&opt->diffopt);
939 return 1;
940 }
941
942 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
943 {
944 diff_tree_combined_merge(commit, opt);
945 return !opt->loginfo;
946 }
947
948 static void setup_additional_headers(struct diff_options *o,
949 struct strmap *all_headers)
950 {
951 struct hashmap_iter iter;
952 struct strmap_entry *entry;
953
954 /*
955 * Make o->additional_path_headers contain the subset of all_headers
956 * that match o->pathspec. If there aren't any that match o->pathspec,
957 * then make o->additional_path_headers be NULL.
958 */
959
960 if (!o->pathspec.nr) {
961 o->additional_path_headers = all_headers;
962 return;
963 }
964
965 o->additional_path_headers = xmalloc(sizeof(struct strmap));
966 strmap_init_with_options(o->additional_path_headers, NULL, 0);
967 strmap_for_each_entry(all_headers, &iter, entry) {
968 if (match_pathspec(the_repository->index, &o->pathspec,
969 entry->key, strlen(entry->key),
970 0 /* prefix */, NULL /* seen */,
971 0 /* is_dir */))
972 strmap_put(o->additional_path_headers,
973 entry->key, entry->value);
974 }
975 if (!strmap_get_size(o->additional_path_headers)) {
976 strmap_clear(o->additional_path_headers, 0);
977 FREE_AND_NULL(o->additional_path_headers);
978 }
979 }
980
981 static void cleanup_additional_headers(struct diff_options *o)
982 {
983 if (!o->pathspec.nr) {
984 o->additional_path_headers = NULL;
985 return;
986 }
987 if (!o->additional_path_headers)
988 return;
989
990 strmap_clear(o->additional_path_headers, 0);
991 FREE_AND_NULL(o->additional_path_headers);
992 }
993
994 static int do_remerge_diff(struct rev_info *opt,
995 struct commit_list *parents,
996 struct object_id *oid)
997 {
998 struct merge_options o;
999 struct commit_list *bases;
1000 struct merge_result res = {0};
1001 struct pretty_print_context ctx = {0};
1002 struct commit *parent1 = parents->item;
1003 struct commit *parent2 = parents->next->item;
1004 struct strbuf parent1_desc = STRBUF_INIT;
1005 struct strbuf parent2_desc = STRBUF_INIT;
1006
1007 /* Setup merge options */
1008 init_merge_options(&o, the_repository);
1009 o.show_rename_progress = 0;
1010 o.record_conflict_msgs_as_headers = 1;
1011 o.msg_header_prefix = "remerge";
1012
1013 ctx.abbrev = DEFAULT_ABBREV;
1014 repo_format_commit_message(the_repository, parent1, "%h (%s)",
1015 &parent1_desc, &ctx);
1016 repo_format_commit_message(the_repository, parent2, "%h (%s)",
1017 &parent2_desc, &ctx);
1018 o.branch1 = parent1_desc.buf;
1019 o.branch2 = parent2_desc.buf;
1020
1021 /* Parse the relevant commits and get the merge bases */
1022 parse_commit_or_die(parent1);
1023 parse_commit_or_die(parent2);
1024 bases = repo_get_merge_bases(the_repository, parent1, parent2);
1025
1026 /* Re-merge the parents */
1027 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1028
1029 /* Show the diff */
1030 setup_additional_headers(&opt->diffopt, res.path_messages);
1031 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1032 log_tree_diff_flush(opt);
1033
1034 /* Cleanup */
1035 cleanup_additional_headers(&opt->diffopt);
1036 strbuf_release(&parent1_desc);
1037 strbuf_release(&parent2_desc);
1038 merge_finalize(&o, &res);
1039
1040 /* Clean up the contents of the temporary object directory */
1041 if (opt->remerge_objdir)
1042 tmp_objdir_discard_objects(opt->remerge_objdir);
1043 else
1044 BUG("did a remerge diff without remerge_objdir?!?");
1045
1046 return !opt->loginfo;
1047 }
1048
1049 /*
1050 * Show the diff of a commit.
1051 *
1052 * Return true if we printed any log info messages
1053 */
1054 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1055 {
1056 int showed_log;
1057 struct commit_list *parents;
1058 struct object_id *oid;
1059 int is_merge;
1060 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1061
1062 if (!all_need_diff && !opt->merges_need_diff)
1063 return 0;
1064
1065 parse_commit_or_die(commit);
1066 oid = get_commit_tree_oid(commit);
1067
1068 parents = get_saved_parents(opt, commit);
1069 is_merge = parents && parents->next;
1070 if (!is_merge && !all_need_diff)
1071 return 0;
1072
1073 /* Root commit? */
1074 if (!parents) {
1075 if (opt->show_root_diff) {
1076 diff_root_tree_oid(oid, "", &opt->diffopt);
1077 log_tree_diff_flush(opt);
1078 }
1079 return !opt->loginfo;
1080 }
1081
1082 if (is_merge) {
1083 int octopus = (parents->next->next != NULL);
1084
1085 if (opt->remerge_diff) {
1086 if (octopus) {
1087 show_log(opt);
1088 fprintf(opt->diffopt.file,
1089 "diff: warning: Skipping remerge-diff "
1090 "for octopus merges.\n");
1091 return 1;
1092 }
1093 return do_remerge_diff(opt, parents, oid);
1094 }
1095 if (opt->combine_merges)
1096 return do_diff_combined(opt, commit);
1097 if (opt->separate_merges) {
1098 if (!opt->first_parent_merges) {
1099 /* Show parent info for multiple diffs */
1100 log->parent = parents->item;
1101 }
1102 } else
1103 return 0;
1104 }
1105
1106 showed_log = 0;
1107 for (;;) {
1108 struct commit *parent = parents->item;
1109
1110 parse_commit_or_die(parent);
1111 diff_tree_oid(get_commit_tree_oid(parent),
1112 oid, "", &opt->diffopt);
1113 log_tree_diff_flush(opt);
1114
1115 showed_log |= !opt->loginfo;
1116
1117 /* Set up the log info for the next parent, if any.. */
1118 parents = parents->next;
1119 if (!parents || opt->first_parent_merges)
1120 break;
1121 log->parent = parents->item;
1122 opt->loginfo = log;
1123 }
1124 return showed_log;
1125 }
1126
1127 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1128 {
1129 struct log_info log;
1130 int shown;
1131 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1132 int no_free = opt->diffopt.no_free;
1133
1134 log.commit = commit;
1135 log.parent = NULL;
1136 opt->loginfo = &log;
1137 opt->diffopt.no_free = 1;
1138
1139 /* NEEDSWORK: no restoring of no_free? Why? */
1140 if (opt->line_level_traverse)
1141 return line_log_print(opt, commit);
1142
1143 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1144 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1145 shown = log_tree_diff(opt, commit, &log);
1146 if (!shown && opt->loginfo && opt->always_show_header) {
1147 log.parent = NULL;
1148 show_log(opt);
1149 shown = 1;
1150 }
1151 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1152 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1153 opt->loginfo = NULL;
1154 maybe_flush_or_die(opt->diffopt.file, "stdout");
1155 opt->diffopt.no_free = no_free;
1156 diff_free(&opt->diffopt);
1157 return shown;
1158 }