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