]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
The fifth batch
[thirdparty/git.git] / log-tree.c
CommitLineData
e7da9385 1#define USE_THE_REPOSITORY_VARIABLE
41f43b82 2#define DISABLE_SIGN_COMPARE_WARNINGS
e7da9385 3
d812c3b6 4#include "git-compat-util.h"
db757e8b 5#include "commit-reach.h"
b2141fc1 6#include "config.h"
5f1c3f07 7#include "diff.h"
a28fe2d9 8#include "diffcore.h"
32a8f510 9#include "environment.h"
41771fa4 10#include "hex.h"
dabab1d6 11#include "object-name.h"
1a793261 12#include "object-file.h"
109cd76d 13#include "repository.h"
7b90ab46 14#include "tmp-objdir.h"
5f1c3f07 15#include "commit.h"
cab4feb6 16#include "tag.h"
7fefda5c 17#include "graph.h"
5f1c3f07 18#include "log-tree.h"
db757e8b 19#include "merge-ort.h"
8860fd42 20#include "reflog-walk.h"
cab4feb6 21#include "refs.h"
cbeab747 22#include "replace-object.h"
0fd2e215 23#include "revision.h"
b079c50e 24#include "string-list.h"
67a4b586 25#include "color.h"
0c37f1fc 26#include "gpg-interface.h"
959a2623 27#include "sequencer.h"
12da1d1f 28#include "line-log.h"
3ac68a93 29#include "help.h"
40ce4160 30#include "range-diff.h"
20323d10 31#include "strmap.h"
d4a4f929 32#include "tree.h"
dd77d587 33#include "wildmatch.h"
d48be35c 34#include "write-or-die.h"
0c4d5aa2 35#include "pager.h"
5f1c3f07 36
2608c249 37static struct decoration name_decoration = { "object names" };
76c61fbd
JH
38static int decoration_loaded;
39static int decoration_flags;
a7524128 40
67a4b586
NR
41static 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 */
76f5df30 48 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
67a4b586
NR
49};
50
a73b3680
NTND
51static 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",
09c4ba41 57 [DECORATION_GRAFTED] = "grafted",
a73b3680
NTND
58};
59
67a4b586
NR
60static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
61{
daa0c3d9 62 if (want_color(decorate_use_color))
67a4b586
NR
63 return decoration_colors[ix];
64 return "";
65}
66
3ac68a93 67define_list_config_array(color_decorate_slots);
5e11bee6 68
8852117a 69int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
5e11bee6 70{
a73b3680 71 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
5e11bee6
NR
72 if (slot < 0)
73 return 0;
74 if (!value)
75 return config_error_nonbool(var);
f6c5a296 76 return color_parse(value, decoration_colors[slot]);
5e11bee6
NR
77}
78
67a4b586
NR
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) \
f1c96261 84 decorate_get_color((o)->use_color, ix)
67a4b586 85
662174d2 86void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
cab4feb6 87{
96ffc06f
JK
88 struct name_decoration *res;
89 FLEX_ALLOC_STR(res, name, name);
a7524128 90 res->type = type;
cab4feb6
RS
91 res->next = add_decoration(&name_decoration, obj, res);
92}
93
2608c249
JK
94const struct name_decoration *get_name_decoration(const struct object *obj)
95{
0cc7380d 96 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
2608c249
JK
97 return lookup_decoration(&name_decoration, obj);
98}
99
c9f7a793
DS
100static int match_ref_pattern(const char *refname,
101 const struct string_list_item *item)
102{
103 int matched = 0;
afe8a907 104 if (!item->util) {
c9f7a793
DS
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
116static 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;
a6be5e67
DS
122 const struct string_list *exclude_patterns_config =
123 filter->exclude_ref_config_pattern;
c9f7a793
DS
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) {
c9f7a793 133 for_each_string_list_item(item, include_patterns) {
a6be5e67
DS
134 if (match_ref_pattern(refname, item))
135 return 1;
c9f7a793 136 }
a6be5e67
DS
137 return 0;
138 }
c9f7a793 139
a6be5e67
DS
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 }
c9f7a793 145 }
a6be5e67 146
c9f7a793
DS
147 return 1;
148}
149
e8207717 150static int add_ref_decoration(const char *refname, const char *referent UNUSED, const struct object_id *oid,
5cf88fd8 151 int flags UNUSED,
63e14ee2 152 void *cb_data)
cab4feb6 153{
94d421b8 154 int i;
5267d292 155 struct object *obj;
88473c8b 156 enum object_type objtype;
6afb265b 157 enum decoration_type deco_type = DECORATION_NONE;
65516f58 158 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
97e61e0f 159 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
5267d292 160
c9f7a793 161 if (filter && !ref_filter_match(refname, filter))
65516f58 162 return 0;
429ad204 163
58d121b2 164 if (starts_with(refname, git_replace_ref_base)) {
3d79f657 165 struct object_id original_oid;
f1178380 166 if (!replace_refs_enabled(the_repository))
b9ad5002 167 return 0;
31a0ad54
JH
168 if (get_oid_hex(refname + strlen(git_replace_ref_base),
169 &original_oid)) {
5267d292
NTND
170 warning("invalid replace ref %s", refname);
171 return 0;
172 }
109cd76d 173 obj = parse_object(the_repository, &original_oid);
5267d292
NTND
174 if (obj)
175 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
176 return 0;
177 }
178
88473c8b
JK
179 objtype = oid_object_info(the_repository, oid, NULL);
180 if (objtype < 0)
cab4feb6 181 return 0;
88473c8b 182 obj = lookup_object_by_type(the_repository, oid, objtype);
a7524128 183
94d421b8
DS
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 }
a7524128 199
6afb265b 200 add_name_decoration(deco_type, refname, obj);
cab4feb6 201 while (obj->type == OBJ_TAG) {
d1ed8d6c
JK
202 if (!obj->parsed)
203 parse_object(the_repository, &obj->oid);
cab4feb6
RS
204 obj = ((struct tag *)obj)->tagged;
205 if (!obj)
206 break;
a7524128 207 add_name_decoration(DECORATION_REF_TAG, refname, obj);
cab4feb6
RS
208 }
209 return 0;
210}
211
43090008
JK
212static int add_graft_decoration(const struct commit_graft *graft,
213 void *cb_data UNUSED)
76f5df30 214{
c1f5eb49 215 struct commit *commit = lookup_commit(the_repository, &graft->oid);
76f5df30
NTND
216 if (!commit)
217 return 0;
218 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
219 return 0;
220}
221
65516f58 222void load_ref_decorations(struct decoration_filter *filter, int flags)
cab4feb6 223{
76c61fbd 224 if (!decoration_loaded) {
65516f58
RA
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 }
a6be5e67
DS
233 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
234 normalize_glob_ref(item, NULL, item->string);
235 }
e4d03b79
NG
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;
65516f58 241 }
76c61fbd
JH
242 decoration_loaded = 1;
243 decoration_flags = flags;
2e5c4758
PS
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);
65516f58 248 for_each_commit_graft(add_graft_decoration, filter);
cab4feb6
RS
249 }
250}
251
68c9fcb0
NG
252void 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
4d7b0efc 273static void show_parents(struct commit *commit, int abbrev, FILE *file)
c8c893c6
LT
274{
275 struct commit_list *p;
276 for (p = commit->parents; p ; p = p->next) {
277 struct commit *parent = p->item;
d850b7a5
ÆAB
278 fprintf(file, " %s",
279 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
c8c893c6
LT
280 }
281}
282
91b849b2
JS
283static 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) {
d850b7a5
ÆAB
287 fprintf(opt->diffopt.file, " %s",
288 repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
91b849b2
JS
289 }
290}
291
51ff0f27
JH
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 */
296static 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;
51ff0f27
JH
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 */
2e5c4758
PS
312 branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
313 "HEAD", 0, NULL, &rru_flags);
d79be498 314 if (!branch_name || !(rru_flags & REF_ISSYMREF))
51ff0f27 315 return NULL;
76c61fbd 316
429ad204 317 if (!starts_with(branch_name, "refs/"))
51ff0f27
JH
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
429ad204
JH
330static 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
9d3f002f 338/*
9271095c 339 * The caller makes sure there is no funny color before calling.
a3883a65 340 * format_decorations ensures the same after return.
9d3f002f 341 */
a3883a65 342void format_decorations(struct strbuf *sb,
9d3f002f 343 const struct commit *commit,
9271095c 344 int use_color,
a3883a65 345 const struct decoration_options *opts)
ca135e7a 346{
2608c249 347 const struct name_decoration *decoration;
51ff0f27 348 const struct name_decoration *current_and_HEAD;
b87a9a2c 349 const char *color_commit, *color_reset;
ca135e7a 350
a3883a65
AK
351 const char *prefix = " (";
352 const char *suffix = ")";
353 const char *separator = ", ";
f1f8a258
AK
354 const char *pointer = " -> ";
355 const char *tag = "tag: ";
a3883a65 356
2608c249 357 decoration = get_name_decoration(&commit->object);
ca135e7a
LT
358 if (!decoration)
359 return;
51ff0f27 360
a3883a65
AK
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;
f1f8a258
AK
368 if (opts->pointer)
369 pointer = opts->pointer;
370 if (opts->tag)
371 tag = opts->tag;
a3883a65
AK
372 }
373
b87a9a2c
AK
374 color_commit = diff_get_color(use_color, DIFF_COMMIT);
375 color_reset = decorate_get_color(use_color, DECORATION_NONE);
376
51ff0f27 377 current_and_HEAD = current_pointed_by_HEAD(decoration);
ca135e7a 378 while (decoration) {
51ff0f27
JH
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) {
dcb347f8
AK
385 const char *color =
386 decorate_get_color(use_color, decoration->type);
387
b87a9a2c
AK
388 if (*prefix) {
389 strbuf_addstr(sb, color_commit);
390 strbuf_addstr(sb, prefix);
391 strbuf_addstr(sb, color_reset);
392 }
393
f1f8a258 394 if (*tag && decoration->type == DECORATION_REF_TAG) {
dcb347f8 395 strbuf_addstr(sb, color);
f1f8a258 396 strbuf_addstr(sb, tag);
dcb347f8
AK
397 strbuf_addstr(sb, color_reset);
398 }
51ff0f27 399
dcb347f8 400 strbuf_addstr(sb, color);
429ad204 401 show_name(sb, decoration);
dcb347f8 402 strbuf_addstr(sb, color_reset);
51ff0f27
JH
403
404 if (current_and_HEAD &&
405 decoration->type == DECORATION_REF_HEAD) {
1e63b34a 406 strbuf_addstr(sb, color_commit);
f1f8a258 407 strbuf_addstr(sb, pointer);
51ff0f27
JH
408 strbuf_addstr(sb, color_reset);
409 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
429ad204 410 show_name(sb, current_and_HEAD);
dcb347f8 411 strbuf_addstr(sb, color_reset);
51ff0f27 412 }
51ff0f27
JH
413
414 prefix = separator;
415 }
ca135e7a
LT
416 decoration = decoration->next;
417 }
b87a9a2c
AK
418 if (*suffix) {
419 strbuf_addstr(sb, color_commit);
420 strbuf_addstr(sb, suffix);
421 strbuf_addstr(sb, color_reset);
422 }
9d3f002f
NTND
423}
424
425void show_decorations(struct rev_info *opt, struct commit *commit)
426{
427 struct strbuf sb = STRBUF_INIT;
428
87be2523
NTND
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 }
9d3f002f
NTND
435 if (!opt->show_decorations)
436 return;
a3883a65 437 format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
4d7b0efc 438 fputs(sb.buf, opt->diffopt.file);
9d3f002f 439 strbuf_release(&sb);
ca135e7a
LT
440}
441
d28b5d47
JH
442void fmt_output_subject(struct strbuf *filename,
443 const char *subject,
021f2f4c 444 struct rev_info *info)
6fa8e627 445{
021f2f4c
JH
446 const char *suffix = info->patch_suffix;
447 int nr = info->nr;
d28b5d47 448 int start_len = filename->len;
3baf58bf 449 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
d28b5d47 450
db91988a
ZH
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 }
d28b5d47
JH
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
466void 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
bab82164
ÆAB
473 repo_format_commit_message(the_repository, commit, "%f", &subject,
474 &ctx);
d28b5d47
JH
475 fmt_output_subject(filename, subject.buf, info);
476 strbuf_release(&subject);
6fa8e627
SB
477}
478
8ffc8dc6
RS
479void 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 ? " " : "",
0c4d5aa2 485 decimal_width(opt->total),
8ffc8dc6
RS
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
108dab28 495void log_write_email_headers(struct rev_info *opt, struct commit *commit,
305a6814 496 char **extra_headers_p,
50cd54ef 497 int *need_8bit_cte_p,
498 int maybe_multipart)
b02bd65f 499{
838ba014 500 struct strbuf headers = STRBUF_INIT;
3a30aa17 501 const char *name = oid_to_hex(opt->zero_commit ?
7d70b29c 502 null_oid(the_hash_algo) : &commit->object.oid);
267123b4
JH
503
504 *need_8bit_cte_p = 0; /* unknown */
b02bd65f 505
1c10b8e5 506 if (opt->extra_headers && *opt->extra_headers)
838ba014
JK
507 strbuf_addstr(&headers, opt->extra_headers);
508
4d7b0efc 509 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
7fefda5c
AS
510 graph_show_oneline(opt->graph);
511 if (opt->message_id) {
ba4324c4 512 fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
7fefda5c
AS
513 graph_show_oneline(opt->graph);
514 }
b079c50e
TR
515 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
516 int i, n;
517 n = opt->ref_message_ids->nr;
4d7b0efc 518 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
b079c50e 519 for (i = 0; i < n; i++)
4d7b0efc 520 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
b079c50e 521 opt->ref_message_ids->items[i].string);
7fefda5c
AS
522 graph_show_oneline(opt->graph);
523 }
50cd54ef 524 if (opt->mime_boundary && maybe_multipart) {
d50b69b8 525 static struct strbuf buffer = STRBUF_INIT;
108dab28 526 struct strbuf filename = STRBUF_INIT;
267123b4 527 *need_8bit_cte_p = -1; /* NEVER */
d50b69b8 528
d50b69b8
JK
529 strbuf_reset(&buffer);
530
838ba014 531 strbuf_addf(&headers,
b02bd65f
DB
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",
b02bd65f
DB
542 mime_boundary_leader, opt->mime_boundary,
543 mime_boundary_leader, opt->mime_boundary);
b02bd65f 544
38ec23ac
JH
545 if (opt->numbered_files)
546 strbuf_addf(&filename, "%d", opt->nr);
547 else
d28b5d47 548 fmt_output_commit(&filename, commit, opt);
d50b69b8 549 strbuf_addf(&buffer,
6b2fbaaf 550 "\n--%s%s\n"
b02bd65f 551 "Content-Type: text/x-patch;"
108dab28 552 " name=\"%s\"\n"
b02bd65f
DB
553 "Content-Transfer-Encoding: 8bit\n"
554 "Content-Disposition: %s;"
108dab28 555 " filename=\"%s\"\n\n",
b02bd65f 556 mime_boundary_leader, opt->mime_boundary,
108dab28 557 filename.buf,
b02bd65f 558 opt->no_inline ? "attachment" : "inline",
108dab28 559 filename.buf);
d50b69b8 560 opt->diffopt.stat_sep = buffer.buf;
108dab28 561 strbuf_release(&filename);
b02bd65f 562 }
838ba014 563 *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
b02bd65f
DB
564}
565
c6b3ec41
JH
566static 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');
4d7b0efc 575 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
c6b3ec41 576 *eol ? "\n" : "");
cf3983d1 577 graph_show_oneline(opt->graph);
c6b3ec41
JH
578 bol = (*eol) ? (eol + 1) : eol;
579 }
580}
581
0c37f1fc
JH
582static void show_signature(struct rev_info *opt, struct commit *commit)
583{
584 struct strbuf payload = STRBUF_INIT;
585 struct strbuf signature = STRBUF_INIT;
67948981 586 struct signature_check sigc = { 0 };
0c37f1fc 587 int status;
0c37f1fc 588
1fb5cf0d 589 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
0c37f1fc
JH
590 goto out;
591
4bbf3780 592 sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
02769437
FS
593 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
594 status = check_signature(&sigc, signature.buf, signature.len);
b5726a5d 595 if (status && !sigc.output)
67948981
HJI
596 show_sig_lines(opt, status, "No signature\n");
597 else
b5726a5d 598 show_sig_lines(opt, status, sigc.output);
67948981 599 signature_check_clear(&sigc);
0c37f1fc
JH
600
601 out:
0c37f1fc
JH
602 strbuf_release(&payload);
603 strbuf_release(&signature);
604}
605
a92ea68f 606static int which_parent(const struct object_id *oid, const struct commit *commit)
824958e5
JH
607{
608 int nth;
609 const struct commit_list *parent;
610
611 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
4a7e27e9 612 if (oideq(&parent->item->object.oid, oid))
824958e5
JH
613 return nth;
614 nth++;
615 }
616 return -1;
617}
618
d041ffa5
JH
619static int is_common_merge(const struct commit *commit)
620{
621 return (commit->parents
622 && commit->parents->next
623 && !commit->parents->next->next);
624}
625
fef461ea
JS
626static int show_one_mergetag(struct commit *commit,
627 struct commit_extra_header *extra,
628 void *data)
824958e5 629{
063da62b 630 struct rev_info *opt = (struct rev_info *)data;
a92ea68f 631 struct object_id oid;
824958e5
JH
632 struct tag *tag;
633 struct strbuf verify_message;
67948981 634 struct signature_check sigc = { 0 };
824958e5 635 int status, nth;
482c1191 636 struct strbuf payload = STRBUF_INIT;
637 struct strbuf signature = STRBUF_INIT;
824958e5 638
2dcde20e 639 hash_object_file(the_hash_algo, extra->value, extra->len,
44439c1c 640 OBJ_TAG, &oid);
ce71efb7 641 tag = lookup_tag(the_repository, &oid);
824958e5 642 if (!tag)
fef461ea 643 return -1; /* error message already given */
824958e5
JH
644
645 strbuf_init(&verify_message, 256);
0e740fed 646 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
824958e5 647 strbuf_addstr(&verify_message, "malformed mergetag\n");
d041ffa5 648 else if (is_common_merge(commit) &&
4a7e27e9
JK
649 oideq(&tag->tagged->oid,
650 &commit->parents->next->item->object.oid))
d041ffa5
JH
651 strbuf_addf(&verify_message,
652 "merged tag '%s'\n", tag->tag);
a92ea68f 653 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
824958e5 654 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
237a2817 655 tag->tag, oid_to_hex(&tag->tagged->oid));
824958e5
JH
656 else
657 strbuf_addf(&verify_message,
658 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
824958e5 659
1315093f 660 status = -1;
482c1191 661 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
42c55ce4 662 /* could have a good signature */
4bbf3780 663 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
02769437
FS
664 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
665 status = check_signature(&sigc, signature.buf, signature.len);
b5726a5d
FS
666 if (sigc.output)
667 strbuf_addstr(&verify_message, sigc.output);
67948981 668 else
42c55ce4 669 strbuf_addstr(&verify_message, "No signature\n");
67948981 670 signature_check_clear(&sigc);
42c55ce4
MG
671 /* otherwise we couldn't verify, which is shown as bad */
672 }
824958e5
JH
673
674 show_sig_lines(opt, status, verify_message.buf);
675 strbuf_release(&verify_message);
482c1191 676 strbuf_release(&payload);
677 strbuf_release(&signature);
fef461ea 678 return 0;
824958e5
JH
679}
680
fef461ea 681static int show_mergetag(struct rev_info *opt, struct commit *commit)
824958e5 682{
fef461ea 683 return for_each_mergetag(show_one_mergetag, commit, opt);
824958e5
JH
684}
685
3fcc7a23
ES
686static 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
84ed5055
JH
696static 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));
a5aecb2c 705 diff_queue_init(&diff_queued_diff);
84ed5055 706
2fa04ceb 707 fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
84ed5055
JH
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));
a5aecb2c 724 diff_queue_init(&diff_queued_diff);
84ed5055 725
2fa04ceb 726 fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
84ed5055
JH
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
02865655 741void show_log(struct rev_info *opt)
91539833 742{
f285a2d7 743 struct strbuf msgbuf = STRBUF_INIT;
39bc9a6c 744 struct log_info *log = opt->loginfo;
91539833 745 struct commit *commit = log->commit, *parent = log->parent;
2ed1960a 746 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
dd2e794a 747 struct pretty_print_context ctx = {0};
91539833
LT
748
749 opt->loginfo = NULL;
750 if (!opt->verbose_header) {
7fefda5c
AS
751 graph_show_commit(opt->graph);
752
1df2d656 753 if (!opt->graph)
b1b47554 754 put_revision_mark(opt, commit);
d850b7a5
ÆAB
755 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
756 opt->diffopt.file);
885cf808 757 if (opt->print_parents)
4d7b0efc 758 show_parents(commit, abbrev_commit, opt->diffopt.file);
91b849b2
JS
759 if (opt->children.name)
760 show_children(opt, commit, abbrev_commit);
0f3a290b 761 show_decorations(opt, commit);
7fefda5c 762 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
4d7b0efc 763 putc('\n', opt->diffopt.file);
7fefda5c
AS
764 graph_show_remainder(opt->graph);
765 }
4d7b0efc 766 putc(opt->diffopt.line_termination, opt->diffopt.file);
91539833
LT
767 return;
768 }
769
770 /*
8715227d
JK
771 * If use_terminator is set, we already handled any record termination
772 * at the end of the last record.
02865655
AS
773 * Otherwise, add a diffopt.line_termination character before all
774 * entries but the first. (IOW, as a separator between entries)
91539833 775 */
7fefda5c
AS
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);
4d7b0efc 792 putc(opt->diffopt.line_termination, opt->diffopt.file);
7fefda5c 793 }
91539833
LT
794 opt->shown_one = 1;
795
7fefda5c
AS
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
91539833
LT
802 /*
803 * Print header line of header..
804 */
3eefc189 805
9f23e040 806 if (cmit_fmt_is_mail(opt->commit_format)) {
82363d96 807 log_write_email_headers(opt, commit, &ctx.after_subject,
50cd54ef 808 &ctx.need_8bit_cte, 1);
6d167fd7 809 ctx.rev = opt;
e52a5de4 810 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
4d7b0efc 811 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
74bd9029 812 if (opt->commit_format != CMIT_FMT_ONELINE)
4d7b0efc 813 fputs("commit ", opt->diffopt.file);
7528f27d 814
1df2d656 815 if (!opt->graph)
b1b47554 816 put_revision_mark(opt, commit);
d850b7a5
ÆAB
817 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
818 abbrev_commit),
4d7b0efc 819 opt->diffopt.file);
885cf808 820 if (opt->print_parents)
4d7b0efc 821 show_parents(commit, abbrev_commit, opt->diffopt.file);
91b849b2
JS
822 if (opt->children.name)
823 show_children(opt, commit, abbrev_commit);
73f0a157 824 if (parent)
4d7b0efc 825 fprintf(opt->diffopt.file, " (from %s)",
d850b7a5 826 repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
4d7b0efc 827 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
0f3a290b 828 show_decorations(opt, commit);
7fefda5c 829 if (opt->commit_format == CMIT_FMT_ONELINE) {
4d7b0efc 830 putc(' ', opt->diffopt.file);
7fefda5c 831 } else {
4d7b0efc 832 putc('\n', opt->diffopt.file);
7fefda5c
AS
833 graph_show_oneline(opt->graph);
834 }
903b45fe 835 if (opt->reflog_info) {
7fefda5c
AS
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 */
4d12a471 842 show_reflog_message(opt->reflog_info,
55ccf85a 843 opt->commit_format == CMIT_FMT_ONELINE,
9720d23e 844 opt->date_mode,
55ccf85a 845 opt->date_mode_explicit);
02865655 846 if (opt->commit_format == CMIT_FMT_ONELINE)
903b45fe 847 return;
903b45fe 848 }
3eefc189 849 }
91539833 850
824958e5 851 if (opt->show_signature) {
0c37f1fc 852 show_signature(opt, commit);
824958e5
JH
853 show_mergetag(opt, commit);
854 }
0c37f1fc 855
ddf333f6
JH
856 if (opt->show_notes) {
857 int raw;
858 struct strbuf notebuf = STRBUF_INIT;
859
860 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
fb61e4d3 861 format_display_notes(&commit->object.oid, &notebuf,
ddf333f6 862 get_log_output_encoding(), raw);
82f51af3 863 ctx.notes_message = strbuf_detach(&notebuf, NULL);
ddf333f6
JH
864 }
865
91539833
LT
866 /*
867 * And then the pretty-printed message itself
868 */
5289c56a
NTND
869 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
870 ctx.need_8bit_cte =
39ab4d09 871 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
dd2e794a 872 ctx.date_mode = opt->date_mode;
f026c756 873 ctx.date_mode_explicit = opt->date_mode_explicit;
dd2e794a 874 ctx.abbrev = opt->diffopt.abbrev;
9553d2b2 875 ctx.preserve_subject = opt->preserve_subject;
19d097e3 876 ctx.encode_email_headers = opt->encode_email_headers;
8f8f5476 877 ctx.reflog_info = opt->reflog_info;
6bf13944 878 ctx.fmt = opt->commit_format;
0e2913b0 879 ctx.mailmap = opt->mailmap;
30825178 880 ctx.color = opt->diffopt.use_color;
7cc13c71 881 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
ecaee805 882 ctx.output_encoding = get_log_output_encoding();
ad6f028f 883 ctx.rev = opt;
a9080475
JK
884 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
885 ctx.from_ident = &opt->from_ident;
3ad87c80
JK
886 if (opt->graph)
887 ctx.graph_width = graph_width(opt->graph);
6bf13944 888 pretty_print_commit(&ctx, commit, &msgbuf);
212620fe
JH
889
890 if (opt->add_signoff)
5289c56a 891 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
212620fe 892
5a664cf2 893 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
bd1470b8 894 ctx.notes_message && *ctx.notes_message) {
3fcc7a23
ES
895 if (cmit_fmt_is_mail(ctx.fmt))
896 next_commentary_block(opt, &msgbuf);
5a664cf2 897 strbuf_addstr(&msgbuf, ctx.notes_message);
bd1470b8 898 }
cf2251b6 899
7fefda5c 900 if (opt->show_log_size) {
4d7b0efc 901 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
7fefda5c
AS
902 graph_show_oneline(opt->graph);
903 }
9fa3465d 904
7fefda5c
AS
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
660e113c 914 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
b9c7d6e4 915 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
7fefda5c
AS
916 if (!opt->missing_newline)
917 graph_show_padding(opt->graph);
4d7b0efc 918 putc(opt->diffopt.line_termination, opt->diffopt.file);
7fefda5c
AS
919 }
920
674d1727 921 strbuf_release(&msgbuf);
ddf333f6 922 free(ctx.notes_message);
305a6814 923 free(ctx.after_subject);
91539833
LT
924}
925
cd2bdc53 926int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07 927{
bd1470b8 928 opt->shown_dashes = 0;
5f1c3f07 929 diffcore_std(&opt->diffopt);
91539833 930
95433eee 931 if (diff_queue_is_empty(&opt->diffopt)) {
5f1c3f07
JH
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 }
91539833 938
3969cf7d 939 if (opt->loginfo && !opt->no_commit_id) {
02865655 940 show_log(opt);
304b5af6
LT
941 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
942 opt->verbose_header &&
b9c7d6e4
JK
943 opt->commit_format != CMIT_FMT_ONELINE &&
944 !commit_format_is_empty(opt->commit_format)) {
1d34c50f
JH
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 */
3969cf7d 951 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
436728fe 952 fputs(diff_line_prefix(&opt->diffopt), opt->diffopt.file);
1d34c50f
JH
953
954 /*
955 * We may have shown three-dashes line early
3fcc7a23
ES
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.
1d34c50f
JH
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)
4d7b0efc
JS
967 fprintf(opt->diffopt.file, "---");
968 putc('\n', opt->diffopt.file);
3969cf7d
JH
969 }
970 }
5f1c3f07
JH
971 diff_flush(&opt->diffopt);
972 return 1;
973}
974
cd2bdc53 975static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07 976{
d01141de 977 diff_tree_combined_merge(commit, opt);
91539833 978 return !opt->loginfo;
5f1c3f07
JH
979}
980
20323d10
EN
981static 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
1014static 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
db757e8b
EN
1027static int do_remerge_diff(struct rev_info *opt,
1028 struct commit_list *parents,
e2841f70 1029 struct object_id *oid)
db757e8b
EN
1030{
1031 struct merge_options o;
76e2a099 1032 struct commit_list *bases = NULL;
db757e8b
EN
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
245cac5c
JH
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) {
727c71a1 1045 opt->remerge_objdir = tmp_objdir_create(the_repository, "remerge-diff");
245cac5c
JH
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
db757e8b 1051 /* Setup merge options */
9c93ba4d 1052 init_ui_merge_options(&o, the_repository);
db757e8b 1053 o.show_rename_progress = 0;
20323d10
EN
1054 o.record_conflict_msgs_as_headers = 1;
1055 o.msg_header_prefix = "remerge";
db757e8b
EN
1056
1057 ctx.abbrev = DEFAULT_ABBREV;
bab82164
ÆAB
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);
db757e8b
EN
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);
76e2a099
JS
1068 if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)
1069 exit(128);
db757e8b
EN
1070
1071 /* Re-merge the parents */
1072 merge_incore_recursive(&o, bases, parent1, parent2, &res);
1073
1074 /* Show the diff */
20323d10 1075 setup_additional_headers(&opt->diffopt, res.path_messages);
db757e8b
EN
1076 diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1077 log_tree_diff_flush(opt);
1078
1079 /* Cleanup */
44ec7c57 1080 free_commit_list(bases);
20323d10 1081 cleanup_additional_headers(&opt->diffopt);
db757e8b
EN
1082 strbuf_release(&parent1_desc);
1083 strbuf_release(&parent2_desc);
1084 merge_finalize(&o, &res);
7b90ab46
EN
1085
1086 /* Clean up the contents of the temporary object directory */
245cac5c 1087 tmp_objdir_discard_objects(opt->remerge_objdir);
db757e8b
EN
1088
1089 return !opt->loginfo;
1090}
1091
91539833
LT
1092/*
1093 * Show the diff of a commit.
1094 *
1095 * Return true if we printed any log info messages
1096 */
1097static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 1098{
91539833 1099 int showed_log;
5f1c3f07 1100 struct commit_list *parents;
f2fd0760 1101 struct object_id *oid;
a6d19ecc
SO
1102 int is_merge;
1103 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
5f1c3f07 1104
a6d19ecc 1105 if (!all_need_diff && !opt->merges_need_diff)
91539833
LT
1106 return 0;
1107
7059dccc 1108 parse_commit_or_die(commit);
2e27bd77 1109 oid = get_commit_tree_oid(commit);
d1b9b767 1110
53d00b39 1111 parents = get_saved_parents(opt, commit);
a6d19ecc
SO
1112 is_merge = parents && parents->next;
1113 if (!is_merge && !all_need_diff)
1114 return 0;
1115
1116 /* Root commit? */
91539833 1117 if (!parents) {
2b60356d 1118 if (opt->show_root_diff) {
7b8dea0c 1119 diff_root_tree_oid(oid, "", &opt->diffopt);
2b60356d
RS
1120 log_tree_diff_flush(opt);
1121 }
91539833 1122 return !opt->loginfo;
5f1c3f07
JH
1123 }
1124
a6d19ecc 1125 if (is_merge) {
db757e8b
EN
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 }
e2841f70 1136 return do_remerge_diff(opt, parents, oid);
db757e8b 1137 }
1a2c4d80 1138 if (opt->combine_merges)
5f1c3f07 1139 return do_diff_combined(opt, commit);
1a2c4d80
SO
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;
5f1c3f07
JH
1147 }
1148
91539833
LT
1149 showed_log = 0;
1150 for (;;) {
5f1c3f07 1151 struct commit *parent = parents->item;
5f1c3f07 1152
7059dccc 1153 parse_commit_or_die(parent);
2e27bd77 1154 diff_tree_oid(get_commit_tree_oid(parent),
66f414f8 1155 oid, "", &opt->diffopt);
91539833
LT
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;
3291eea3 1162 if (!parents || opt->first_parent_merges)
91539833
LT
1163 break;
1164 log->parent = parents->item;
1165 opt->loginfo = log;
1166 }
1167 return showed_log;
1168}
1169
1170int log_tree_commit(struct rev_info *opt, struct commit *commit)
1171{
1172 struct log_info log;
e900d494
ÆAB
1173 int shown;
1174 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1175 int no_free = opt->diffopt.no_free;
91539833
LT
1176
1177 log.commit = commit;
1178 log.parent = NULL;
1179 opt->loginfo = &log;
e900d494 1180 opt->diffopt.no_free = 1;
91539833 1181
f8781bfd 1182 /* NEEDSWORK: no restoring of no_free? Why? */
12da1d1f
TR
1183 if (opt->line_level_traverse)
1184 return line_log_print(opt, commit);
1185
1b32dece 1186 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
4d7b0efc 1187 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
3eefc189
JH
1188 shown = log_tree_diff(opt, commit, &log);
1189 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 1190 log.parent = NULL;
02865655 1191 show_log(opt);
3eefc189 1192 shown = 1;
5f1c3f07 1193 }
1b32dece 1194 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
4d7b0efc 1195 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
2fa04ceb
JH
1196 if (shown)
1197 show_diff_of_diff(opt);
91539833 1198 opt->loginfo = NULL;
4d7b0efc 1199 maybe_flush_or_die(opt->diffopt.file, "stdout");
e900d494 1200 opt->diffopt.no_free = no_free;
2fa04ceb 1201
e900d494 1202 diff_free(&opt->diffopt);
3eefc189 1203 return shown;
5f1c3f07 1204}