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