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