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