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