Move write_shallow_commits to fetch-pack.c
[git/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
cab4feb6 4#include "tag.h"
7fefda5c 5#include "graph.h"
5f1c3f07 6#include "log-tree.h"
8860fd42 7#include "reflog-walk.h"
cab4feb6 8#include "refs.h"
b079c50e 9#include "string-list.h"
67a4b586 10#include "color.h"
5f1c3f07 11
ca135e7a
LT
12struct decoration name_decoration = { "object names" };
13
a7524128
NR
14enum decoration_type {
15 DECORATION_NONE = 0,
16 DECORATION_REF_LOCAL,
17 DECORATION_REF_REMOTE,
18 DECORATION_REF_TAG,
19 DECORATION_REF_STASH,
20 DECORATION_REF_HEAD,
21};
22
67a4b586
NR
23static char decoration_colors[][COLOR_MAXLEN] = {
24 GIT_COLOR_RESET,
25 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
26 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
27 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
28 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
29 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
30};
31
32static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
33{
34 if (decorate_use_color)
35 return decoration_colors[ix];
36 return "";
37}
38
5e11bee6
NR
39static int parse_decorate_color_slot(const char *slot)
40{
41 /*
42 * We're comparing with 'ignore-case' on
43 * (because config.c sets them all tolower),
44 * but let's match the letters in the literal
45 * string values here with how they are
46 * documented in Documentation/config.txt, for
47 * consistency.
48 *
49 * We love being consistent, don't we?
50 */
51 if (!strcasecmp(slot, "branch"))
52 return DECORATION_REF_LOCAL;
53 if (!strcasecmp(slot, "remoteBranch"))
54 return DECORATION_REF_REMOTE;
55 if (!strcasecmp(slot, "tag"))
56 return DECORATION_REF_TAG;
57 if (!strcasecmp(slot, "stash"))
58 return DECORATION_REF_STASH;
59 if (!strcasecmp(slot, "HEAD"))
60 return DECORATION_REF_HEAD;
61 return -1;
62}
63
64int parse_decorate_color_config(const char *var, const int ofs, const char *value)
65{
66 int slot = parse_decorate_color_slot(var + ofs);
67 if (slot < 0)
68 return 0;
69 if (!value)
70 return config_error_nonbool(var);
71 color_parse(value, var, decoration_colors[slot]);
72 return 0;
73}
74
67a4b586
NR
75/*
76 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
77 * for showing the commit sha1, use the same check for --decorate
78 */
79#define decorate_get_color_opt(o, ix) \
80 decorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)
81
a7524128 82static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
cab4feb6 83{
cab4feb6 84 int nlen = strlen(name);
a7524128
NR
85 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
86 memcpy(res->name, name, nlen + 1);
87 res->type = type;
cab4feb6
RS
88 res->next = add_decoration(&name_decoration, obj, res);
89}
90
91static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
92{
93 struct object *obj = parse_object(sha1);
a7524128 94 enum decoration_type type = DECORATION_NONE;
cab4feb6
RS
95 if (!obj)
96 return 0;
a7524128 97
594ffe80 98 if (!prefixcmp(refname, "refs/heads/"))
a7524128 99 type = DECORATION_REF_LOCAL;
594ffe80 100 else if (!prefixcmp(refname, "refs/remotes/"))
a7524128 101 type = DECORATION_REF_REMOTE;
594ffe80 102 else if (!prefixcmp(refname, "refs/tags/"))
a7524128
NR
103 type = DECORATION_REF_TAG;
104 else if (!prefixcmp(refname, "refs/stash"))
105 type = DECORATION_REF_STASH;
106 else if (!prefixcmp(refname, "HEAD"))
107 type = DECORATION_REF_HEAD;
108
33e7018c
LH
109 if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
110 refname = prettify_refname(refname);
a7524128 111 add_name_decoration(type, refname, obj);
cab4feb6
RS
112 while (obj->type == OBJ_TAG) {
113 obj = ((struct tag *)obj)->tagged;
114 if (!obj)
115 break;
a7524128 116 add_name_decoration(DECORATION_REF_TAG, refname, obj);
cab4feb6
RS
117 }
118 return 0;
119}
120
33e7018c 121void load_ref_decorations(int flags)
cab4feb6
RS
122{
123 static int loaded;
124 if (!loaded) {
125 loaded = 1;
33e7018c 126 for_each_ref(add_ref_decoration, &flags);
77abcbdc 127 head_ref(add_ref_decoration, &flags);
cab4feb6
RS
128 }
129}
130
c8c893c6
LT
131static void show_parents(struct commit *commit, int abbrev)
132{
133 struct commit_list *p;
134 for (p = commit->parents; p ; p = p->next) {
135 struct commit *parent = p->item;
7fcda920 136 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
c8c893c6
LT
137 }
138}
139
0f3a290b 140void show_decorations(struct rev_info *opt, struct commit *commit)
ca135e7a
LT
141{
142 const char *prefix;
143 struct name_decoration *decoration;
67a4b586
NR
144 const char *color_commit =
145 diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
146 const char *color_reset =
147 decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
ca135e7a 148
0f3a290b 149 if (opt->show_source && commit->util)
8c4021ab 150 printf("\t%s", (char *) commit->util);
d467a525
LT
151 if (!opt->show_decorations)
152 return;
ca135e7a
LT
153 decoration = lookup_decoration(&name_decoration, &commit->object);
154 if (!decoration)
155 return;
156 prefix = " (";
157 while (decoration) {
a7524128 158 printf("%s", prefix);
67a4b586
NR
159 fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
160 stdout);
a7524128 161 if (decoration->type == DECORATION_REF_TAG)
67a4b586 162 fputs("tag: ", stdout);
a7524128 163 printf("%s", decoration->name);
67a4b586
NR
164 fputs(color_reset, stdout);
165 fputs(color_commit, stdout);
ca135e7a
LT
166 prefix = ", ";
167 decoration = decoration->next;
168 }
169 putchar(')');
170}
171
fc1c75ec
FBH
172/*
173 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
174 * Signed-off-by: and Acked-by: lines.
175 */
176static int detect_any_signoff(char *letter, int size)
177{
fd13b21f 178 char *cp;
fc1c75ec
FBH
179 int seen_colon = 0;
180 int seen_at = 0;
181 int seen_name = 0;
182 int seen_head = 0;
183
184 cp = letter + size;
fd13b21f 185 while (letter <= --cp && *cp == '\n')
fc1c75ec
FBH
186 continue;
187
188 while (letter <= cp) {
fd13b21f 189 char ch = *cp--;
fc1c75ec
FBH
190 if (ch == '\n')
191 break;
192
193 if (!seen_at) {
194 if (ch == '@')
195 seen_at = 1;
196 continue;
197 }
198 if (!seen_colon) {
199 if (ch == '@')
200 return 0;
201 else if (ch == ':')
202 seen_colon = 1;
203 else
204 seen_name = 1;
205 continue;
206 }
207 if (('A' <= ch && ch <= 'Z') ||
208 ('a' <= ch && ch <= 'z') ||
209 ch == '-') {
210 seen_head = 1;
211 continue;
212 }
213 /* no empty last line doesn't match */
214 return 0;
215 }
216 return seen_head && seen_name;
217}
218
674d1727 219static void append_signoff(struct strbuf *sb, const char *signoff)
cf2251b6 220{
cf2251b6 221 static const char signed_off_by[] = "Signed-off-by: ";
80583c0e 222 size_t signoff_len = strlen(signoff);
fc1c75ec 223 int has_signoff = 0;
80583c0e 224 char *cp;
cf2251b6 225
674d1727 226 cp = sb->buf;
cf2251b6
JH
227
228 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
229 while ((cp = strstr(cp, signed_off_by))) {
230
231 has_signoff = 1;
232
cf2251b6 233 cp += strlen(signed_off_by);
674d1727 234 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
235 break;
236 if (strncmp(cp, signoff, signoff_len))
237 continue;
238 if (!isspace(cp[signoff_len]))
239 continue;
240 /* we already have him */
674d1727 241 return;
cf2251b6
JH
242 }
243
fc1c75ec 244 if (!has_signoff)
674d1727 245 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
246
247 if (!has_signoff)
674d1727 248 strbuf_addch(sb, '\n');
c35f4c37 249
674d1727
PH
250 strbuf_addstr(sb, signed_off_by);
251 strbuf_add(sb, signoff, signoff_len);
252 strbuf_addch(sb, '\n');
cf2251b6
JH
253}
254
e00de24b
JS
255static unsigned int digits_in_number(unsigned int number)
256{
257 unsigned int i = 10, result = 1;
258 while (i <= number) {
259 i *= 10;
260 result++;
261 }
262 return result;
263}
264
6fa8e627
SB
265void get_patch_filename(struct commit *commit, int nr, const char *suffix,
266 struct strbuf *buf)
267{
268 int suffix_len = strlen(suffix) + 1;
269 int start_len = buf->len;
270
271 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
272 if (commit) {
b09b868f 273 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
dd2e794a
TR
274 struct pretty_print_context ctx = {0};
275 ctx.date_mode = DATE_NORMAL;
b09b868f 276
dd2e794a 277 format_commit_message(commit, "%f", buf, &ctx);
b09b868f
CC
278 if (max_len < buf->len)
279 strbuf_setlen(buf, max_len);
280 strbuf_addstr(buf, suffix);
6fa8e627
SB
281 }
282}
283
108dab28 284void log_write_email_headers(struct rev_info *opt, struct commit *commit,
267123b4
JH
285 const char **subject_p,
286 const char **extra_headers_p,
287 int *need_8bit_cte_p)
b02bd65f
DB
288{
289 const char *subject = NULL;
290 const char *extra_headers = opt->extra_headers;
108dab28 291 const char *name = sha1_to_hex(commit->object.sha1);
267123b4
JH
292
293 *need_8bit_cte_p = 0; /* unknown */
b02bd65f
DB
294 if (opt->total > 0) {
295 static char buffer[64];
296 snprintf(buffer, sizeof(buffer),
e7af8e49 297 "Subject: [%s%s%0*d/%d] ",
b02bd65f 298 opt->subject_prefix,
e7af8e49 299 *opt->subject_prefix ? " " : "",
b02bd65f
DB
300 digits_in_number(opt->total),
301 opt->nr, opt->total);
302 subject = buffer;
303 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
304 static char buffer[256];
305 snprintf(buffer, sizeof(buffer),
306 "Subject: [%s] ",
307 opt->subject_prefix);
308 subject = buffer;
309 } else {
310 subject = "Subject: ";
311 }
312
313 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
7fefda5c
AS
314 graph_show_oneline(opt->graph);
315 if (opt->message_id) {
b02bd65f 316 printf("Message-Id: <%s>\n", opt->message_id);
7fefda5c
AS
317 graph_show_oneline(opt->graph);
318 }
b079c50e
TR
319 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
320 int i, n;
321 n = opt->ref_message_ids->nr;
322 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
323 for (i = 0; i < n; i++)
324 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
325 opt->ref_message_ids->items[i].string);
7fefda5c
AS
326 graph_show_oneline(opt->graph);
327 }
b02bd65f
DB
328 if (opt->mime_boundary) {
329 static char subject_buffer[1024];
330 static char buffer[1024];
108dab28 331 struct strbuf filename = STRBUF_INIT;
267123b4 332 *need_8bit_cte_p = -1; /* NEVER */
b02bd65f
DB
333 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
334 "%s"
335 "MIME-Version: 1.0\n"
336 "Content-Type: multipart/mixed;"
337 " boundary=\"%s%s\"\n"
338 "\n"
339 "This is a multi-part message in MIME "
340 "format.\n"
341 "--%s%s\n"
342 "Content-Type: text/plain; "
343 "charset=UTF-8; format=fixed\n"
344 "Content-Transfer-Encoding: 8bit\n\n",
345 extra_headers ? extra_headers : "",
346 mime_boundary_leader, opt->mime_boundary,
347 mime_boundary_leader, opt->mime_boundary);
348 extra_headers = subject_buffer;
349
108dab28
SB
350 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
351 opt->patch_suffix, &filename);
b02bd65f 352 snprintf(buffer, sizeof(buffer) - 1,
6b2fbaaf 353 "\n--%s%s\n"
b02bd65f 354 "Content-Type: text/x-patch;"
108dab28 355 " name=\"%s\"\n"
b02bd65f
DB
356 "Content-Transfer-Encoding: 8bit\n"
357 "Content-Disposition: %s;"
108dab28 358 " filename=\"%s\"\n\n",
b02bd65f 359 mime_boundary_leader, opt->mime_boundary,
108dab28 360 filename.buf,
b02bd65f 361 opt->no_inline ? "attachment" : "inline",
108dab28 362 filename.buf);
b02bd65f 363 opt->diffopt.stat_sep = buffer;
108dab28 364 strbuf_release(&filename);
b02bd65f
DB
365 }
366 *subject_p = subject;
367 *extra_headers_p = extra_headers;
368}
369
02865655 370void show_log(struct rev_info *opt)
91539833 371{
f285a2d7 372 struct strbuf msgbuf = STRBUF_INIT;
39bc9a6c 373 struct log_info *log = opt->loginfo;
91539833 374 struct commit *commit = log->commit, *parent = log->parent;
91539833 375 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
dd2e794a
TR
376 const char *extra_headers = opt->extra_headers;
377 struct pretty_print_context ctx = {0};
91539833
LT
378
379 opt->loginfo = NULL;
66b2ed09 380 ctx.show_notes = opt->show_notes;
91539833 381 if (!opt->verbose_header) {
7fefda5c
AS
382 graph_show_commit(opt->graph);
383
1df2d656 384 if (!opt->graph)
b1b47554 385 put_revision_mark(opt, commit);
7fcda920 386 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
885cf808 387 if (opt->print_parents)
c8c893c6 388 show_parents(commit, abbrev_commit);
0f3a290b 389 show_decorations(opt, commit);
7fefda5c
AS
390 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
391 putchar('\n');
392 graph_show_remainder(opt->graph);
393 }
1dcb6922 394 putchar(opt->diffopt.line_termination);
91539833
LT
395 return;
396 }
397
398 /*
8715227d
JK
399 * If use_terminator is set, we already handled any record termination
400 * at the end of the last record.
02865655
AS
401 * Otherwise, add a diffopt.line_termination character before all
402 * entries but the first. (IOW, as a separator between entries)
91539833 403 */
7fefda5c
AS
404 if (opt->shown_one && !opt->use_terminator) {
405 /*
406 * If entries are separated by a newline, the output
407 * should look human-readable. If the last entry ended
408 * with a newline, print the graph output before this
409 * newline. Otherwise it will end up as a completely blank
410 * line and will look like a gap in the graph.
411 *
412 * If the entry separator is not a newline, the output is
413 * primarily intended for programmatic consumption, and we
414 * never want the extra graph output before the entry
415 * separator.
416 */
417 if (opt->diffopt.line_termination == '\n' &&
418 !opt->missing_newline)
419 graph_show_padding(opt->graph);
abd4e222 420 putchar(opt->diffopt.line_termination);
7fefda5c 421 }
91539833
LT
422 opt->shown_one = 1;
423
7fefda5c
AS
424 /*
425 * If the history graph was requested,
426 * print the graph, up to this commit's line
427 */
428 graph_show_commit(opt->graph);
429
91539833
LT
430 /*
431 * Print header line of header..
432 */
3eefc189 433
596524b3 434 if (opt->commit_format == CMIT_FMT_EMAIL) {
dd2e794a
TR
435 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
436 &ctx.need_8bit_cte);
e52a5de4 437 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 438 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
439 if (opt->commit_format != CMIT_FMT_ONELINE)
440 fputs("commit ", stdout);
7528f27d 441
1df2d656 442 if (!opt->graph)
b1b47554 443 put_revision_mark(opt, commit);
7fcda920 444 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
74bd9029 445 stdout);
885cf808 446 if (opt->print_parents)
c66b6c06 447 show_parents(commit, abbrev_commit);
73f0a157 448 if (parent)
3eefc189 449 printf(" (from %s)",
7fcda920 450 find_unique_abbrev(parent->object.sha1,
3eefc189 451 abbrev_commit));
0f3a290b 452 show_decorations(opt, commit);
8f67f8ae 453 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
7fefda5c
AS
454 if (opt->commit_format == CMIT_FMT_ONELINE) {
455 putchar(' ');
456 } else {
457 putchar('\n');
458 graph_show_oneline(opt->graph);
459 }
903b45fe 460 if (opt->reflog_info) {
7fefda5c
AS
461 /*
462 * setup_revisions() ensures that opt->reflog_info
463 * and opt->graph cannot both be set,
464 * so we don't need to worry about printing the
465 * graph info here.
466 */
4d12a471 467 show_reflog_message(opt->reflog_info,
4e244cbc 468 opt->commit_format == CMIT_FMT_ONELINE,
f4ea32f0
JK
469 opt->date_mode_explicit ?
470 opt->date_mode :
471 DATE_NORMAL);
02865655 472 if (opt->commit_format == CMIT_FMT_ONELINE)
903b45fe 473 return;
903b45fe 474 }
3eefc189 475 }
91539833 476
3131b713
LT
477 if (!commit->buffer)
478 return;
479
91539833
LT
480 /*
481 * And then the pretty-printed message itself
482 */
dd2e794a
TR
483 if (ctx.need_8bit_cte >= 0)
484 ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
485 ctx.date_mode = opt->date_mode;
486 ctx.abbrev = opt->diffopt.abbrev;
487 ctx.after_subject = extra_headers;
9553d2b2 488 ctx.preserve_subject = opt->preserve_subject;
8f8f5476 489 ctx.reflog_info = opt->reflog_info;
6bf13944
JK
490 ctx.fmt = opt->commit_format;
491 pretty_print_commit(&ctx, commit, &msgbuf);
cf2251b6
JH
492
493 if (opt->add_signoff)
674d1727 494 append_signoff(&msgbuf, opt->add_signoff);
7fefda5c 495 if (opt->show_log_size) {
674d1727 496 printf("log size %i\n", (int)msgbuf.len);
7fefda5c
AS
497 graph_show_oneline(opt->graph);
498 }
9fa3465d 499
7fefda5c
AS
500 /*
501 * Set opt->missing_newline if msgbuf doesn't
502 * end in a newline (including if it is empty)
503 */
504 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
505 opt->missing_newline = 1;
506 else
507 opt->missing_newline = 0;
508
509 if (opt->graph)
510 graph_show_commit_msg(opt->graph, &msgbuf);
511 else
42c8c74c 512 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
7fefda5c
AS
513 if (opt->use_terminator) {
514 if (!opt->missing_newline)
515 graph_show_padding(opt->graph);
9b58bfe8 516 putchar('\n');
7fefda5c
AS
517 }
518
674d1727 519 strbuf_release(&msgbuf);
91539833
LT
520}
521
cd2bdc53 522int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
523{
524 diffcore_std(&opt->diffopt);
91539833 525
5f1c3f07
JH
526 if (diff_queue_is_empty()) {
527 int saved_fmt = opt->diffopt.output_format;
528 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
529 diff_flush(&opt->diffopt);
530 opt->diffopt.output_format = saved_fmt;
531 return 0;
532 }
91539833 533
3969cf7d
JH
534 if (opt->loginfo && !opt->no_commit_id) {
535 /* When showing a verbose header (i.e. log message),
536 * and not in --pretty=oneline format, we would want
537 * an extra newline between the end of log and the
538 * output for readability.
539 */
02865655 540 show_log(opt);
304b5af6
LT
541 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
542 opt->verbose_header &&
3969cf7d
JH
543 opt->commit_format != CMIT_FMT_ONELINE) {
544 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
545 if ((pch & opt->diffopt.output_format) == pch)
abd4e222 546 printf("---");
81bd1b2a
BY
547 if (opt->diffopt.output_prefix) {
548 struct strbuf *msg = NULL;
549 msg = opt->diffopt.output_prefix(&opt->diffopt,
550 opt->diffopt.output_prefix_data);
551 fwrite(msg->buf, msg->len, 1, stdout);
552 }
abd4e222 553 putchar('\n');
3969cf7d
JH
554 }
555 }
5f1c3f07
JH
556 diff_flush(&opt->diffopt);
557 return 1;
558}
559
cd2bdc53 560static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
561{
562 unsigned const char *sha1 = commit->object.sha1;
563
91539833
LT
564 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
565 return !opt->loginfo;
5f1c3f07
JH
566}
567
91539833
LT
568/*
569 * Show the diff of a commit.
570 *
571 * Return true if we printed any log info messages
572 */
573static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 574{
91539833 575 int showed_log;
5f1c3f07
JH
576 struct commit_list *parents;
577 unsigned const char *sha1 = commit->object.sha1;
578
84102a33 579 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
91539833
LT
580 return 0;
581
5f1c3f07 582 /* Root commit? */
91539833
LT
583 parents = commit->parents;
584 if (!parents) {
2b60356d
RS
585 if (opt->show_root_diff) {
586 diff_root_tree_sha1(sha1, "", &opt->diffopt);
587 log_tree_diff_flush(opt);
588 }
91539833 589 return !opt->loginfo;
5f1c3f07
JH
590 }
591
592 /* More than one parent? */
91539833 593 if (parents && parents->next) {
5f1c3f07
JH
594 if (opt->ignore_merges)
595 return 0;
596 else if (opt->combine_merges)
597 return do_diff_combined(opt, commit);
88d9d45d
PB
598 else if (opt->first_parent_only) {
599 /*
600 * Generate merge log entry only for the first
601 * parent, showing summary diff of the others
602 * we merged _in_.
603 */
604 diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
605 log_tree_diff_flush(opt);
606 return !opt->loginfo;
607 }
91539833
LT
608
609 /* If we show individual diffs, show the parent info */
610 log->parent = parents->item;
5f1c3f07
JH
611 }
612
91539833
LT
613 showed_log = 0;
614 for (;;) {
5f1c3f07 615 struct commit *parent = parents->item;
5f1c3f07 616
91539833
LT
617 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
618 log_tree_diff_flush(opt);
619
620 showed_log |= !opt->loginfo;
621
622 /* Set up the log info for the next parent, if any.. */
623 parents = parents->next;
624 if (!parents)
625 break;
626 log->parent = parents->item;
627 opt->loginfo = log;
628 }
629 return showed_log;
630}
631
632int log_tree_commit(struct rev_info *opt, struct commit *commit)
633{
634 struct log_info log;
3eefc189 635 int shown;
91539833
LT
636
637 log.commit = commit;
638 log.parent = NULL;
639 opt->loginfo = &log;
640
3eefc189
JH
641 shown = log_tree_diff(opt, commit, &log);
642 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 643 log.parent = NULL;
02865655 644 show_log(opt);
3eefc189 645 shown = 1;
5f1c3f07 646 }
91539833 647 opt->loginfo = NULL;
06f59e9f 648 maybe_flush_or_die(stdout, "stdout");
3eefc189 649 return shown;
5f1c3f07 650}