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