]> git.ipfire.org Git - thirdparty/git.git/blob - log-tree.c
1d05dc1c7010d58039893dea81ffc0346d66968e
[thirdparty/git.git] / log-tree.c
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(int 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 char *refname, const char *referent UNUSED, const struct object_id *oid,
151 int flags UNUSED,
152 void *cb_data)
153 {
154 int i;
155 struct object *obj;
156 enum object_type objtype;
157 enum decoration_type deco_type = DECORATION_NONE;
158 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
159 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
160
161 if (filter && !ref_filter_match(refname, filter))
162 return 0;
163
164 if (starts_with(refname, git_replace_ref_base)) {
165 struct object_id original_oid;
166 if (!replace_refs_enabled(the_repository))
167 return 0;
168 if (get_oid_hex(refname + strlen(git_replace_ref_base),
169 &original_oid)) {
170 warning("invalid replace ref %s", refname);
171 return 0;
172 }
173 obj = parse_object(the_repository, &original_oid);
174 if (obj)
175 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
176 return 0;
177 }
178
179 objtype = oid_object_info(the_repository, oid, NULL);
180 if (objtype < 0)
181 return 0;
182 obj = lookup_object_by_type(the_repository, oid, objtype);
183
184 for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
185 struct ref_namespace_info *info = &ref_namespace[i];
186
187 if (!info->decoration)
188 continue;
189 if (info->exact) {
190 if (!strcmp(refname, info->ref)) {
191 deco_type = info->decoration;
192 break;
193 }
194 } else if (starts_with(refname, info->ref)) {
195 deco_type = info->decoration;
196 break;
197 }
198 }
199
200 add_name_decoration(deco_type, refname, obj);
201 while (obj->type == OBJ_TAG) {
202 if (!obj->parsed)
203 parse_object(the_repository, &obj->oid);
204 obj = ((struct tag *)obj)->tagged;
205 if (!obj)
206 break;
207 add_name_decoration(DECORATION_REF_TAG, refname, obj);
208 }
209 return 0;
210 }
211
212 static int add_graft_decoration(const struct commit_graft *graft,
213 void *cb_data UNUSED)
214 {
215 struct commit *commit = lookup_commit(the_repository, &graft->oid);
216 if (!commit)
217 return 0;
218 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
219 return 0;
220 }
221
222 void load_ref_decorations(struct decoration_filter *filter, int flags)
223 {
224 if (!decoration_loaded) {
225 if (filter) {
226 struct string_list_item *item;
227 for_each_string_list_item(item, filter->exclude_ref_pattern) {
228 normalize_glob_ref(item, NULL, item->string);
229 }
230 for_each_string_list_item(item, filter->include_ref_pattern) {
231 normalize_glob_ref(item, NULL, item->string);
232 }
233 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
234 normalize_glob_ref(item, NULL, item->string);
235 }
236
237 /* normalize_glob_ref duplicates the strings */
238 filter->exclude_ref_pattern->strdup_strings = 1;
239 filter->include_ref_pattern->strdup_strings = 1;
240 filter->exclude_ref_config_pattern->strdup_strings = 1;
241 }
242 decoration_loaded = 1;
243 decoration_flags = flags;
244 refs_for_each_ref(get_main_ref_store(the_repository),
245 add_ref_decoration, filter);
246 refs_head_ref(get_main_ref_store(the_repository),
247 add_ref_decoration, filter);
248 for_each_commit_graft(add_graft_decoration, filter);
249 }
250 }
251
252 void load_branch_decorations(void)
253 {
254 if (!decoration_loaded) {
255 struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
256 struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
257 struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
258 struct decoration_filter decoration_filter = {
259 .include_ref_pattern = &decorate_refs_include,
260 .exclude_ref_pattern = &decorate_refs_exclude,
261 .exclude_ref_config_pattern = &decorate_refs_exclude_config,
262 };
263
264 string_list_append(&decorate_refs_include, "refs/heads/");
265 load_ref_decorations(&decoration_filter, 0);
266
267 string_list_clear(&decorate_refs_exclude, 0);
268 string_list_clear(&decorate_refs_exclude_config, 0);
269 string_list_clear(&decorate_refs_include, 0);
270 }
271 }
272
273 static void show_parents(struct commit *commit, int abbrev, FILE *file)
274 {
275 struct commit_list *p;
276 for (p = commit->parents; p ; p = p->next) {
277 struct commit *parent = p->item;
278 fprintf(file, " %s",
279 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
280 }
281 }
282
283 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
284 {
285 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
286 for ( ; p; p = p->next) {
287 fprintf(opt->diffopt.file, " %s",
288 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
289 }
290 }
291
292 /*
293 * Do we have HEAD in the output, and also the branch it points at?
294 * If so, find that decoration entry for that current branch.
295 */
296 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
297 {
298 const struct name_decoration *list, *head = NULL;
299 const char *branch_name = NULL;
300 int rru_flags;
301
302 /* First find HEAD */
303 for (list = decoration; list; list = list->next)
304 if (list->type == DECORATION_REF_HEAD) {
305 head = list;
306 break;
307 }
308 if (!head)
309 return NULL;
310
311 /* Now resolve and find the matching current branch */
312 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
313 "HEAD", 0, NULL, &rru_flags);
314 if (!branch_name || !(rru_flags & REF_ISSYMREF))
315 return NULL;
316
317 if (!starts_with(branch_name, "refs/"))
318 return NULL;
319
320 /* OK, do we have that ref in the list? */
321 for (list = decoration; list; list = list->next)
322 if ((list->type == DECORATION_REF_LOCAL) &&
323 !strcmp(branch_name, list->name)) {
324 return list;
325 }
326
327 return NULL;
328 }
329
330 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
331 {
332 if (decoration_flags == DECORATE_SHORT_REFS)
333 strbuf_addstr(sb, prettify_refname(decoration->name));
334 else
335 strbuf_addstr(sb, decoration->name);
336 }
337
338 /*
339 * The caller makes sure there is no funny color before calling.
340 * format_decorations ensures the same after return.
341 */
342 void format_decorations(struct strbuf *sb,
343 const struct commit *commit,
344 int use_color,
345 const struct decoration_options *opts)
346 {
347 const struct name_decoration *decoration;
348 const struct name_decoration *current_and_HEAD;
349 const char *color_commit, *color_reset;
350
351 const char *prefix = " (";
352 const char *suffix = ")";
353 const char *separator = ", ";
354 const char *pointer = " -> ";
355 const char *tag = "tag: ";
356
357 decoration = get_name_decoration(&commit->object);
358 if (!decoration)
359 return;
360
361 if (opts) {
362 if (opts->prefix)
363 prefix = opts->prefix;
364 if (opts->suffix)
365 suffix = opts->suffix;
366 if (opts->separator)
367 separator = opts->separator;
368 if (opts->pointer)
369 pointer = opts->pointer;
370 if (opts->tag)
371 tag = opts->tag;
372 }
373
374 color_commit = diff_get_color(use_color, DIFF_COMMIT);
375 color_reset = decorate_get_color(use_color, DECORATION_NONE);
376
377 current_and_HEAD = current_pointed_by_HEAD(decoration);
378 while (decoration) {
379 /*
380 * When both current and HEAD are there, only
381 * show HEAD->current where HEAD would have
382 * appeared, skipping the entry for current.
383 */
384 if (decoration != current_and_HEAD) {
385 const char *color =
386 decorate_get_color(use_color, decoration->type);
387
388 if (*prefix) {
389 strbuf_addstr(sb, color_commit);
390 strbuf_addstr(sb, prefix);
391 strbuf_addstr(sb, color_reset);
392 }
393
394 if (*tag && decoration->type == DECORATION_REF_TAG) {
395 strbuf_addstr(sb, color);
396 strbuf_addstr(sb, tag);
397 strbuf_addstr(sb, color_reset);
398 }
399
400 strbuf_addstr(sb, color);
401 show_name(sb, decoration);
402 strbuf_addstr(sb, color_reset);
403
404 if (current_and_HEAD &&
405 decoration->type == DECORATION_REF_HEAD) {
406 strbuf_addstr(sb, color_commit);
407 strbuf_addstr(sb, pointer);
408 strbuf_addstr(sb, color_reset);
409 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
410 show_name(sb, current_and_HEAD);
411 strbuf_addstr(sb, color_reset);
412 }
413
414 prefix = separator;
415 }
416 decoration = decoration->next;
417 }
418 if (*suffix) {
419 strbuf_addstr(sb, color_commit);
420 strbuf_addstr(sb, suffix);
421 strbuf_addstr(sb, color_reset);
422 }
423 }
424
425 void show_decorations(struct rev_info *opt, struct commit *commit)
426 {
427 struct strbuf sb = STRBUF_INIT;
428
429 if (opt->sources) {
430 char **slot = revision_sources_peek(opt->sources, commit);
431
432 if (slot && *slot)
433 fprintf(opt->diffopt.file, "\t%s", *slot);
434 }
435 if (!opt->show_decorations)
436 return;
437 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
438 fputs(sb.buf, opt->diffopt.file);
439 strbuf_release(&sb);
440 }
441
442 void fmt_output_subject(struct strbuf *filename,
443 const char *subject,
444 struct rev_info *info)
445 {
446 const char *suffix = info->patch_suffix;
447 int nr = info->nr;
448 int start_len = filename->len;
449 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
450
451 if (info->reroll_count) {
452 struct strbuf temp = STRBUF_INIT;
453
454 strbuf_addf(&temp, "v%s", info->reroll_count);
455 format_sanitized_subject(filename, temp.buf, temp.len);
456 strbuf_addstr(filename, "-");
457 strbuf_release(&temp);
458 }
459 strbuf_addf(filename, "%04d-%s", nr, subject);
460
461 if (max_len < filename->len)
462 strbuf_setlen(filename, max_len);
463 strbuf_addstr(filename, suffix);
464 }
465
466 void fmt_output_commit(struct strbuf *filename,
467 struct commit *commit,
468 struct rev_info *info)
469 {
470 struct pretty_print_context ctx = {0};
471 struct strbuf subject = STRBUF_INIT;
472
473 repo_format_commit_message(the_repository, commit, "%f", &subject,
474 &ctx);
475 fmt_output_subject(filename, subject.buf, info);
476 strbuf_release(&subject);
477 }
478
479 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
480 {
481 if (opt->total > 0) {
482 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
483 opt->subject_prefix,
484 *opt->subject_prefix ? " " : "",
485 decimal_width(opt->total),
486 opt->nr, opt->total);
487 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
488 strbuf_addf(sb, "Subject: [%s] ",
489 opt->subject_prefix);
490 } else {
491 strbuf_addstr(sb, "Subject: ");
492 }
493 }
494
495 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
496 char **extra_headers_p,
497 int *need_8bit_cte_p,
498 int maybe_multipart)
499 {
500 struct strbuf headers = STRBUF_INIT;
501 const char *name = oid_to_hex(opt->zero_commit ?
502 null_oid(the_hash_algo) : &commit->object.oid);
503
504 *need_8bit_cte_p = 0; /* unknown */
505
506 if (opt->extra_headers && *opt->extra_headers)
507 strbuf_addstr(&headers, opt->extra_headers);
508
509 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
510 graph_show_oneline(opt->graph);
511 if (opt->message_id) {
512 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
513 graph_show_oneline(opt->graph);
514 }
515 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
516 int i, n;
517 n = opt->ref_message_ids->nr;
518 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
519 for (i = 0; i < n; i++)
520 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
521 opt->ref_message_ids->items[i].string);
522 graph_show_oneline(opt->graph);
523 }
524 if (opt->mime_boundary && maybe_multipart) {
525 static struct strbuf buffer = STRBUF_INIT;
526 struct strbuf filename = STRBUF_INIT;
527 *need_8bit_cte_p = -1; /* NEVER */
528
529 strbuf_reset(&buffer);
530
531 strbuf_addf(&headers,
532 "MIME-Version: 1.0\n"
533 "Content-Type: multipart/mixed;"
534 " boundary=\"%s%s\"\n"
535 "\n"
536 "This is a multi-part message in MIME "
537 "format.\n"
538 "--%s%s\n"
539 "Content-Type: text/plain; "
540 "charset=UTF-8; format=fixed\n"
541 "Content-Transfer-Encoding: 8bit\n\n",
542 mime_boundary_leader, opt->mime_boundary,
543 mime_boundary_leader, opt->mime_boundary);
544
545 if (opt->numbered_files)
546 strbuf_addf(&filename, "%d", opt->nr);
547 else
548 fmt_output_commit(&filename, commit, opt);
549 strbuf_addf(&buffer,
550 "\n--%s%s\n"
551 "Content-Type: text/x-patch;"
552 " name=\"%s\"\n"
553 "Content-Transfer-Encoding: 8bit\n"
554 "Content-Disposition: %s;"
555 " filename=\"%s\"\n\n",
556 mime_boundary_leader, opt->mime_boundary,
557 filename.buf,
558 opt->no_inline ? "attachment" : "inline",
559 filename.buf);
560 opt->diffopt.stat_sep = buffer.buf;
561 strbuf_release(&filename);
562 }
563 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
564 }
565
566 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
567 {
568 const char *color, *reset, *eol;
569
570 color = diff_get_color_opt(&opt->diffopt,
571 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
572 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
573 while (*bol) {
574 eol = strchrnul(bol, '\n');
575 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
576 *eol ? "\n" : "");
577 graph_show_oneline(opt->graph);
578 bol = (*eol) ? (eol + 1) : eol;
579 }
580 }
581
582 static void show_signature(struct rev_info *opt, struct commit *commit)
583 {
584 struct strbuf payload = STRBUF_INIT;
585 struct strbuf signature = STRBUF_INIT;
586 struct signature_check sigc = { 0 };
587 int status;
588
589 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
590 goto out;
591
592 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
593 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
594 status = check_signature(&sigc, signature.buf, signature.len);
595 if (status && !sigc.output)
596 show_sig_lines(opt, status, "No signature\n");
597 else
598 show_sig_lines(opt, status, sigc.output);
599 signature_check_clear(&sigc);
600
601 out:
602 strbuf_release(&payload);
603 strbuf_release(&signature);
604 }
605
606 static int which_parent(const struct object_id *oid, const struct commit *commit)
607 {
608 int nth;
609 const struct commit_list *parent;
610
611 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
612 if (oideq(&parent->item->object.oid, oid))
613 return nth;
614 nth++;
615 }
616 return -1;
617 }
618
619 static int is_common_merge(const struct commit *commit)
620 {
621 return (commit->parents
622 && commit->parents->next
623 && !commit->parents->next->next);
624 }
625
626 static int show_one_mergetag(struct commit *commit,
627 struct commit_extra_header *extra,
628 void *data)
629 {
630 struct rev_info *opt = (struct rev_info *)data;
631 struct object_id oid;
632 struct tag *tag;
633 struct strbuf verify_message;
634 struct signature_check sigc = { 0 };
635 int status, nth;
636 struct strbuf payload = STRBUF_INIT;
637 struct strbuf signature = STRBUF_INIT;
638
639 hash_object_file(the_hash_algo, extra->value, extra->len,
640 OBJ_TAG, &oid);
641 tag = lookup_tag(the_repository, &oid);
642 if (!tag)
643 return -1; /* error message already given */
644
645 strbuf_init(&verify_message, 256);
646 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
647 strbuf_addstr(&verify_message, "malformed mergetag\n");
648 else if (is_common_merge(commit) &&
649 oideq(&tag->tagged->oid,
650 &commit->parents->next->item->object.oid))
651 strbuf_addf(&verify_message,
652 "merged tag '%s'\n", tag->tag);
653 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
654 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
655 tag->tag, oid_to_hex(&tag->tagged->oid));
656 else
657 strbuf_addf(&verify_message,
658 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
659
660 status = -1;
661 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
662 /* could have a good signature */
663 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
664 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
665 status = check_signature(&sigc, signature.buf, signature.len);
666 if (sigc.output)
667 strbuf_addstr(&verify_message, sigc.output);
668 else
669 strbuf_addstr(&verify_message, "No signature\n");
670 signature_check_clear(&sigc);
671 /* otherwise we couldn't verify, which is shown as bad */
672 }
673
674 show_sig_lines(opt, status, verify_message.buf);
675 strbuf_release(&verify_message);
676 strbuf_release(&payload);
677 strbuf_release(&signature);
678 return 0;
679 }
680
681 static int show_mergetag(struct rev_info *opt, struct commit *commit)
682 {
683 return for_each_mergetag(show_one_mergetag, commit, opt);
684 }
685
686 static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
687 {
688 const char *x = opt->shown_dashes ? "\n" : "---\n";
689 if (sb)
690 strbuf_addstr(sb, x);
691 else
692 fputs(x, opt->diffopt.file);
693 opt->shown_dashes = 1;
694 }
695
696 static void show_diff_of_diff(struct rev_info *opt)
697 {
698 if (!cmit_fmt_is_mail(opt->commit_format))
699 return;
700
701 if (opt->idiff_oid1) {
702 struct diff_queue_struct dq;
703
704 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
705 diff_queue_init(&diff_queued_diff);
706
707 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
708 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
709 &opt->diffopt);
710
711 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
712 }
713
714 if (opt->rdiff1) {
715 struct diff_queue_struct dq;
716 struct diff_options opts;
717 struct range_diff_options range_diff_opts = {
718 .creation_factor = opt->creation_factor,
719 .dual_color = 1,
720 .diffopt = &opts
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 free_commit_list(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 parse_commit_or_die(commit);
1109 oid = get_commit_tree_oid(commit);
1110
1111 parents = get_saved_parents(opt, commit);
1112 is_merge = parents && parents->next;
1113 if (!is_merge && !all_need_diff)
1114 return 0;
1115
1116 /* Root commit? */
1117 if (!parents) {
1118 if (opt->show_root_diff) {
1119 diff_root_tree_oid(oid, "", &opt->diffopt);
1120 log_tree_diff_flush(opt);
1121 }
1122 return !opt->loginfo;
1123 }
1124
1125 if (is_merge) {
1126 int octopus = (parents->next->next != NULL);
1127
1128 if (opt->remerge_diff) {
1129 if (octopus) {
1130 show_log(opt);
1131 fprintf(opt->diffopt.file,
1132 "diff: warning: Skipping remerge-diff "
1133 "for octopus merges.\n");
1134 return 1;
1135 }
1136 return do_remerge_diff(opt, parents, oid);
1137 }
1138 if (opt->combine_merges)
1139 return do_diff_combined(opt, commit);
1140 if (opt->separate_merges) {
1141 if (!opt->first_parent_merges) {
1142 /* Show parent info for multiple diffs */
1143 log->parent = parents->item;
1144 }
1145 } else
1146 return 0;
1147 }
1148
1149 showed_log = 0;
1150 for (;;) {
1151 struct commit *parent = parents->item;
1152
1153 parse_commit_or_die(parent);
1154 diff_tree_oid(get_commit_tree_oid(parent),
1155 oid, "", &opt->diffopt);
1156 log_tree_diff_flush(opt);
1157
1158 showed_log |= !opt->loginfo;
1159
1160 /* Set up the log info for the next parent, if any.. */
1161 parents = parents->next;
1162 if (!parents || opt->first_parent_merges)
1163 break;
1164 log->parent = parents->item;
1165 opt->loginfo = log;
1166 }
1167 return showed_log;
1168 }
1169
1170 int log_tree_commit(struct rev_info *opt, struct commit *commit)
1171 {
1172 struct log_info log;
1173 int shown;
1174 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1175 int no_free = opt->diffopt.no_free;
1176
1177 log.commit = commit;
1178 log.parent = NULL;
1179 opt->loginfo = &log;
1180 opt->diffopt.no_free = 1;
1181
1182 /* NEEDSWORK: no restoring of no_free? Why? */
1183 if (opt->line_level_traverse)
1184 return line_log_print(opt, commit);
1185
1186 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1187 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1188 shown = log_tree_diff(opt, commit, &log);
1189 if (!shown && opt->loginfo && opt->always_show_header) {
1190 log.parent = NULL;
1191 show_log(opt);
1192 shown = 1;
1193 }
1194 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1195 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1196 if (shown)
1197 show_diff_of_diff(opt);
1198 opt->loginfo = NULL;
1199 maybe_flush_or_die(opt->diffopt.file, "stdout");
1200 opt->diffopt.no_free = no_free;
1201
1202 diff_free(&opt->diffopt);
1203 return shown;
1204 }