]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
format-patch: update append_signoff prototype
[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"
5f1c3f07 12
5289c56a
NTND
13#define APPEND_SIGNOFF_DEDUP (1u <<0)
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
NTND
100
101 if (!prefixcmp(refname, "refs/replace/")) {
102 unsigned char original_sha1[20];
b9ad5002
MG
103 if (!read_replace_refs)
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
594ffe80 119 if (!prefixcmp(refname, "refs/heads/"))
a7524128 120 type = DECORATION_REF_LOCAL;
594ffe80 121 else if (!prefixcmp(refname, "refs/remotes/"))
a7524128 122 type = DECORATION_REF_REMOTE;
594ffe80 123 else if (!prefixcmp(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;
a7524128 137 add_name_decoration(DECORATION_REF_TAG, refname, obj);
cab4feb6
RS
138 }
139 return 0;
140}
141
76f5df30
NTND
142static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
143{
144 struct commit *commit = lookup_commit(graft->sha1);
145 if (!commit)
146 return 0;
147 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
148 return 0;
149}
150
33e7018c 151void load_ref_decorations(int flags)
cab4feb6
RS
152{
153 static int loaded;
154 if (!loaded) {
155 loaded = 1;
33e7018c 156 for_each_ref(add_ref_decoration, &flags);
77abcbdc 157 head_ref(add_ref_decoration, &flags);
76f5df30 158 for_each_commit_graft(add_graft_decoration, NULL);
cab4feb6
RS
159 }
160}
161
c8c893c6
LT
162static void show_parents(struct commit *commit, int abbrev)
163{
164 struct commit_list *p;
165 for (p = commit->parents; p ; p = p->next) {
166 struct commit *parent = p->item;
7fcda920 167 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
c8c893c6
LT
168 }
169}
170
91b849b2
JS
171static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
172{
173 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
174 for ( ; p; p = p->next) {
175 printf(" %s", find_unique_abbrev(p->item->object.sha1, abbrev));
176 }
177}
178
0f3a290b 179void show_decorations(struct rev_info *opt, struct commit *commit)
ca135e7a
LT
180{
181 const char *prefix;
182 struct name_decoration *decoration;
67a4b586
NR
183 const char *color_commit =
184 diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
185 const char *color_reset =
186 decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
ca135e7a 187
0f3a290b 188 if (opt->show_source && commit->util)
8c4021ab 189 printf("\t%s", (char *) commit->util);
d467a525
LT
190 if (!opt->show_decorations)
191 return;
ca135e7a
LT
192 decoration = lookup_decoration(&name_decoration, &commit->object);
193 if (!decoration)
194 return;
195 prefix = " (";
196 while (decoration) {
a7524128 197 printf("%s", prefix);
67a4b586
NR
198 fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
199 stdout);
a7524128 200 if (decoration->type == DECORATION_REF_TAG)
67a4b586 201 fputs("tag: ", stdout);
a7524128 202 printf("%s", decoration->name);
67a4b586
NR
203 fputs(color_reset, stdout);
204 fputs(color_commit, stdout);
ca135e7a
LT
205 prefix = ", ";
206 decoration = decoration->next;
207 }
208 putchar(')');
209}
210
fc1c75ec
FBH
211/*
212 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
213 * Signed-off-by: and Acked-by: lines.
214 */
215static int detect_any_signoff(char *letter, int size)
216{
fd13b21f 217 char *cp;
fc1c75ec
FBH
218 int seen_colon = 0;
219 int seen_at = 0;
220 int seen_name = 0;
221 int seen_head = 0;
222
223 cp = letter + size;
fd13b21f 224 while (letter <= --cp && *cp == '\n')
fc1c75ec
FBH
225 continue;
226
227 while (letter <= cp) {
fd13b21f 228 char ch = *cp--;
fc1c75ec
FBH
229 if (ch == '\n')
230 break;
231
232 if (!seen_at) {
233 if (ch == '@')
234 seen_at = 1;
235 continue;
236 }
237 if (!seen_colon) {
238 if (ch == '@')
239 return 0;
240 else if (ch == ':')
241 seen_colon = 1;
242 else
243 seen_name = 1;
244 continue;
245 }
246 if (('A' <= ch && ch <= 'Z') ||
247 ('a' <= ch && ch <= 'z') ||
248 ch == '-') {
249 seen_head = 1;
250 continue;
251 }
252 /* no empty last line doesn't match */
253 return 0;
254 }
255 return seen_head && seen_name;
256}
257
5289c56a 258static void append_signoff(struct strbuf *sb, int ignore_footer, unsigned flag)
cf2251b6 259{
5289c56a 260 unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
cf2251b6 261 static const char signed_off_by[] = "Signed-off-by: ";
5289c56a
NTND
262 char *signoff = xstrdup(fmt_name(getenv("GIT_COMMITTER_NAME"),
263 getenv("GIT_COMMITTER_EMAIL")));
80583c0e 264 size_t signoff_len = strlen(signoff);
fc1c75ec 265 int has_signoff = 0;
80583c0e 266 char *cp;
cf2251b6 267
674d1727 268 cp = sb->buf;
cf2251b6
JH
269
270 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
271 while ((cp = strstr(cp, signed_off_by))) {
272
273 has_signoff = 1;
274
cf2251b6 275 cp += strlen(signed_off_by);
674d1727 276 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
277 break;
278 if (strncmp(cp, signoff, signoff_len))
279 continue;
280 if (!isspace(cp[signoff_len]))
281 continue;
282 /* we already have him */
5289c56a 283 free(signoff);
674d1727 284 return;
cf2251b6
JH
285 }
286
fc1c75ec 287 if (!has_signoff)
674d1727 288 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
289
290 if (!has_signoff)
674d1727 291 strbuf_addch(sb, '\n');
c35f4c37 292
674d1727
PH
293 strbuf_addstr(sb, signed_off_by);
294 strbuf_add(sb, signoff, signoff_len);
295 strbuf_addch(sb, '\n');
5289c56a 296 free(signoff);
cf2251b6
JH
297}
298
e00de24b
JS
299static unsigned int digits_in_number(unsigned int number)
300{
301 unsigned int i = 10, result = 1;
302 while (i <= number) {
303 i *= 10;
304 result++;
305 }
306 return result;
307}
308
a21c2f94
JK
309void get_patch_filename(struct commit *commit, const char *subject, int nr,
310 const char *suffix, struct strbuf *buf)
6fa8e627
SB
311{
312 int suffix_len = strlen(suffix) + 1;
313 int start_len = buf->len;
314
a21c2f94
JK
315 strbuf_addf(buf, commit || subject ? "%04d-" : "%d", nr);
316 if (commit || subject) {
b09b868f 317 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
dd2e794a 318 struct pretty_print_context ctx = {0};
b09b868f 319
a21c2f94
JK
320 if (subject)
321 strbuf_addstr(buf, subject);
322 else if (commit)
323 format_commit_message(commit, "%f", buf, &ctx);
324
b09b868f
CC
325 if (max_len < buf->len)
326 strbuf_setlen(buf, max_len);
327 strbuf_addstr(buf, suffix);
6fa8e627
SB
328 }
329}
330
108dab28 331void log_write_email_headers(struct rev_info *opt, struct commit *commit,
267123b4
JH
332 const char **subject_p,
333 const char **extra_headers_p,
334 int *need_8bit_cte_p)
b02bd65f
DB
335{
336 const char *subject = NULL;
337 const char *extra_headers = opt->extra_headers;
108dab28 338 const char *name = sha1_to_hex(commit->object.sha1);
267123b4
JH
339
340 *need_8bit_cte_p = 0; /* unknown */
b02bd65f
DB
341 if (opt->total > 0) {
342 static char buffer[64];
343 snprintf(buffer, sizeof(buffer),
e7af8e49 344 "Subject: [%s%s%0*d/%d] ",
b02bd65f 345 opt->subject_prefix,
e7af8e49 346 *opt->subject_prefix ? " " : "",
b02bd65f
DB
347 digits_in_number(opt->total),
348 opt->nr, opt->total);
349 subject = buffer;
350 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
351 static char buffer[256];
352 snprintf(buffer, sizeof(buffer),
353 "Subject: [%s] ",
354 opt->subject_prefix);
355 subject = buffer;
356 } else {
357 subject = "Subject: ";
358 }
359
360 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
7fefda5c
AS
361 graph_show_oneline(opt->graph);
362 if (opt->message_id) {
b02bd65f 363 printf("Message-Id: <%s>\n", opt->message_id);
7fefda5c
AS
364 graph_show_oneline(opt->graph);
365 }
b079c50e
TR
366 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
367 int i, n;
368 n = opt->ref_message_ids->nr;
369 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
370 for (i = 0; i < n; i++)
371 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
372 opt->ref_message_ids->items[i].string);
7fefda5c
AS
373 graph_show_oneline(opt->graph);
374 }
b02bd65f
DB
375 if (opt->mime_boundary) {
376 static char subject_buffer[1024];
377 static char buffer[1024];
108dab28 378 struct strbuf filename = STRBUF_INIT;
267123b4 379 *need_8bit_cte_p = -1; /* NEVER */
b02bd65f
DB
380 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
381 "%s"
382 "MIME-Version: 1.0\n"
383 "Content-Type: multipart/mixed;"
384 " boundary=\"%s%s\"\n"
385 "\n"
386 "This is a multi-part message in MIME "
387 "format.\n"
388 "--%s%s\n"
389 "Content-Type: text/plain; "
390 "charset=UTF-8; format=fixed\n"
391 "Content-Transfer-Encoding: 8bit\n\n",
392 extra_headers ? extra_headers : "",
393 mime_boundary_leader, opt->mime_boundary,
394 mime_boundary_leader, opt->mime_boundary);
395 extra_headers = subject_buffer;
396
a21c2f94
JK
397 get_patch_filename(opt->numbered_files ? NULL : commit, NULL,
398 opt->nr, opt->patch_suffix, &filename);
b02bd65f 399 snprintf(buffer, sizeof(buffer) - 1,
6b2fbaaf 400 "\n--%s%s\n"
b02bd65f 401 "Content-Type: text/x-patch;"
108dab28 402 " name=\"%s\"\n"
b02bd65f
DB
403 "Content-Transfer-Encoding: 8bit\n"
404 "Content-Disposition: %s;"
108dab28 405 " filename=\"%s\"\n\n",
b02bd65f 406 mime_boundary_leader, opt->mime_boundary,
108dab28 407 filename.buf,
b02bd65f 408 opt->no_inline ? "attachment" : "inline",
108dab28 409 filename.buf);
b02bd65f 410 opt->diffopt.stat_sep = buffer;
108dab28 411 strbuf_release(&filename);
b02bd65f
DB
412 }
413 *subject_p = subject;
414 *extra_headers_p = extra_headers;
415}
416
c6b3ec41
JH
417static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
418{
419 const char *color, *reset, *eol;
420
421 color = diff_get_color_opt(&opt->diffopt,
422 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
423 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
424 while (*bol) {
425 eol = strchrnul(bol, '\n');
426 printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
427 *eol ? "\n" : "");
428 bol = (*eol) ? (eol + 1) : eol;
429 }
430}
431
0c37f1fc
JH
432static void show_signature(struct rev_info *opt, struct commit *commit)
433{
434 struct strbuf payload = STRBUF_INIT;
435 struct strbuf signature = STRBUF_INIT;
436 struct strbuf gpg_output = STRBUF_INIT;
437 int status;
0c37f1fc
JH
438
439 if (parse_signed_commit(commit->object.sha1, &payload, &signature) <= 0)
440 goto out;
441
442 status = verify_signed_buffer(payload.buf, payload.len,
443 signature.buf, signature.len,
444 &gpg_output);
445 if (status && !gpg_output.len)
446 strbuf_addstr(&gpg_output, "No signature\n");
447
c6b3ec41 448 show_sig_lines(opt, status, gpg_output.buf);
0c37f1fc
JH
449
450 out:
451 strbuf_release(&gpg_output);
452 strbuf_release(&payload);
453 strbuf_release(&signature);
454}
455
824958e5
JH
456static int which_parent(const unsigned char *sha1, const struct commit *commit)
457{
458 int nth;
459 const struct commit_list *parent;
460
461 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
462 if (!hashcmp(parent->item->object.sha1, sha1))
463 return nth;
464 nth++;
465 }
466 return -1;
467}
468
d041ffa5
JH
469static int is_common_merge(const struct commit *commit)
470{
471 return (commit->parents
472 && commit->parents->next
473 && !commit->parents->next->next);
474}
475
824958e5
JH
476static void show_one_mergetag(struct rev_info *opt,
477 struct commit_extra_header *extra,
478 struct commit *commit)
479{
480 unsigned char sha1[20];
481 struct tag *tag;
482 struct strbuf verify_message;
483 int status, nth;
484 size_t payload_size, gpg_message_offset;
485
486 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
487 tag = lookup_tag(sha1);
488 if (!tag)
489 return; /* error message already given */
490
491 strbuf_init(&verify_message, 256);
492 if (parse_tag_buffer(tag, extra->value, extra->len))
493 strbuf_addstr(&verify_message, "malformed mergetag\n");
d041ffa5
JH
494 else if (is_common_merge(commit) &&
495 !hashcmp(tag->tagged->sha1,
496 commit->parents->next->item->object.sha1))
497 strbuf_addf(&verify_message,
498 "merged tag '%s'\n", tag->tag);
824958e5
JH
499 else if ((nth = which_parent(tag->tagged->sha1, commit)) < 0)
500 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
501 tag->tag, tag->tagged->sha1);
502 else
503 strbuf_addf(&verify_message,
504 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
505 gpg_message_offset = verify_message.len;
506
507 payload_size = parse_signature(extra->value, extra->len);
508 if ((extra->len <= payload_size) ||
509 (verify_signed_buffer(extra->value, payload_size,
510 extra->value + payload_size,
511 extra->len - payload_size,
512 &verify_message) &&
513 verify_message.len <= gpg_message_offset)) {
514 strbuf_addstr(&verify_message, "No signature\n");
515 status = -1;
516 }
517 else if (strstr(verify_message.buf + gpg_message_offset,
518 ": Good signature from "))
519 status = 0;
520 else
521 status = -1;
522
523 show_sig_lines(opt, status, verify_message.buf);
524 strbuf_release(&verify_message);
525}
526
527static void show_mergetag(struct rev_info *opt, struct commit *commit)
528{
529 struct commit_extra_header *extra, *to_free;
530
531 to_free = read_commit_extra_headers(commit, NULL);
532 for (extra = to_free; extra; extra = extra->next) {
533 if (strcmp(extra->key, "mergetag"))
534 continue; /* not a merge tag */
535 show_one_mergetag(opt, extra, commit);
536 }
537 free_commit_extra_headers(to_free);
538}
539
02865655 540void show_log(struct rev_info *opt)
91539833 541{
f285a2d7 542 struct strbuf msgbuf = STRBUF_INIT;
39bc9a6c 543 struct log_info *log = opt->loginfo;
91539833 544 struct commit *commit = log->commit, *parent = log->parent;
91539833 545 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
dd2e794a
TR
546 const char *extra_headers = opt->extra_headers;
547 struct pretty_print_context ctx = {0};
91539833
LT
548
549 opt->loginfo = NULL;
550 if (!opt->verbose_header) {
7fefda5c
AS
551 graph_show_commit(opt->graph);
552
1df2d656 553 if (!opt->graph)
b1b47554 554 put_revision_mark(opt, commit);
7fcda920 555 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
885cf808 556 if (opt->print_parents)
c8c893c6 557 show_parents(commit, abbrev_commit);
91b849b2
JS
558 if (opt->children.name)
559 show_children(opt, commit, abbrev_commit);
0f3a290b 560 show_decorations(opt, commit);
7fefda5c
AS
561 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
562 putchar('\n');
563 graph_show_remainder(opt->graph);
564 }
1dcb6922 565 putchar(opt->diffopt.line_termination);
91539833
LT
566 return;
567 }
568
569 /*
8715227d
JK
570 * If use_terminator is set, we already handled any record termination
571 * at the end of the last record.
02865655
AS
572 * Otherwise, add a diffopt.line_termination character before all
573 * entries but the first. (IOW, as a separator between entries)
91539833 574 */
7fefda5c
AS
575 if (opt->shown_one && !opt->use_terminator) {
576 /*
577 * If entries are separated by a newline, the output
578 * should look human-readable. If the last entry ended
579 * with a newline, print the graph output before this
580 * newline. Otherwise it will end up as a completely blank
581 * line and will look like a gap in the graph.
582 *
583 * If the entry separator is not a newline, the output is
584 * primarily intended for programmatic consumption, and we
585 * never want the extra graph output before the entry
586 * separator.
587 */
588 if (opt->diffopt.line_termination == '\n' &&
589 !opt->missing_newline)
590 graph_show_padding(opt->graph);
abd4e222 591 putchar(opt->diffopt.line_termination);
7fefda5c 592 }
91539833
LT
593 opt->shown_one = 1;
594
7fefda5c
AS
595 /*
596 * If the history graph was requested,
597 * print the graph, up to this commit's line
598 */
599 graph_show_commit(opt->graph);
600
91539833
LT
601 /*
602 * Print header line of header..
603 */
3eefc189 604
596524b3 605 if (opt->commit_format == CMIT_FMT_EMAIL) {
dd2e794a
TR
606 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
607 &ctx.need_8bit_cte);
e52a5de4 608 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 609 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
610 if (opt->commit_format != CMIT_FMT_ONELINE)
611 fputs("commit ", stdout);
7528f27d 612
1df2d656 613 if (!opt->graph)
b1b47554 614 put_revision_mark(opt, commit);
7fcda920 615 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
74bd9029 616 stdout);
885cf808 617 if (opt->print_parents)
c66b6c06 618 show_parents(commit, abbrev_commit);
91b849b2
JS
619 if (opt->children.name)
620 show_children(opt, commit, abbrev_commit);
73f0a157 621 if (parent)
3eefc189 622 printf(" (from %s)",
7fcda920 623 find_unique_abbrev(parent->object.sha1,
3eefc189 624 abbrev_commit));
0f3a290b 625 show_decorations(opt, commit);
8f67f8ae 626 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
7fefda5c
AS
627 if (opt->commit_format == CMIT_FMT_ONELINE) {
628 putchar(' ');
629 } else {
630 putchar('\n');
631 graph_show_oneline(opt->graph);
632 }
903b45fe 633 if (opt->reflog_info) {
7fefda5c
AS
634 /*
635 * setup_revisions() ensures that opt->reflog_info
636 * and opt->graph cannot both be set,
637 * so we don't need to worry about printing the
638 * graph info here.
639 */
4d12a471 640 show_reflog_message(opt->reflog_info,
55ccf85a
JH
641 opt->commit_format == CMIT_FMT_ONELINE,
642 opt->date_mode,
643 opt->date_mode_explicit);
02865655 644 if (opt->commit_format == CMIT_FMT_ONELINE)
903b45fe 645 return;
903b45fe 646 }
3eefc189 647 }
91539833 648
824958e5 649 if (opt->show_signature) {
0c37f1fc 650 show_signature(opt, commit);
824958e5
JH
651 show_mergetag(opt, commit);
652 }
0c37f1fc 653
3131b713
LT
654 if (!commit->buffer)
655 return;
656
ddf333f6
JH
657 if (opt->show_notes) {
658 int raw;
659 struct strbuf notebuf = STRBUF_INIT;
660
661 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
662 format_display_notes(commit->object.sha1, &notebuf,
663 get_log_output_encoding(), raw);
664 ctx.notes_message = notebuf.len
665 ? strbuf_detach(&notebuf, NULL)
666 : xcalloc(1, 1);
667 }
668
91539833
LT
669 /*
670 * And then the pretty-printed message itself
671 */
5289c56a
NTND
672 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
673 ctx.need_8bit_cte =
674 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
675 getenv("GIT_COMMITTER_EMAIL")));
dd2e794a 676 ctx.date_mode = opt->date_mode;
f026c756 677 ctx.date_mode_explicit = opt->date_mode_explicit;
dd2e794a
TR
678 ctx.abbrev = opt->diffopt.abbrev;
679 ctx.after_subject = extra_headers;
9553d2b2 680 ctx.preserve_subject = opt->preserve_subject;
8f8f5476 681 ctx.reflog_info = opt->reflog_info;
6bf13944
JK
682 ctx.fmt = opt->commit_format;
683 pretty_print_commit(&ctx, commit, &msgbuf);
212620fe
JH
684
685 if (opt->add_signoff)
5289c56a 686 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
212620fe 687
5a664cf2 688 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
bd1470b8
JH
689 ctx.notes_message && *ctx.notes_message) {
690 if (ctx.fmt == CMIT_FMT_EMAIL) {
691 strbuf_addstr(&msgbuf, "---\n");
692 opt->shown_dashes = 1;
693 }
5a664cf2 694 strbuf_addstr(&msgbuf, ctx.notes_message);
bd1470b8 695 }
cf2251b6 696
7fefda5c 697 if (opt->show_log_size) {
674d1727 698 printf("log size %i\n", (int)msgbuf.len);
7fefda5c
AS
699 graph_show_oneline(opt->graph);
700 }
9fa3465d 701
7fefda5c
AS
702 /*
703 * Set opt->missing_newline if msgbuf doesn't
704 * end in a newline (including if it is empty)
705 */
706 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
707 opt->missing_newline = 1;
708 else
709 opt->missing_newline = 0;
710
711 if (opt->graph)
712 graph_show_commit_msg(opt->graph, &msgbuf);
713 else
42c8c74c 714 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
7fefda5c
AS
715 if (opt->use_terminator) {
716 if (!opt->missing_newline)
717 graph_show_padding(opt->graph);
3e065308 718 putchar(opt->diffopt.line_termination);
7fefda5c
AS
719 }
720
674d1727 721 strbuf_release(&msgbuf);
ddf333f6 722 free(ctx.notes_message);
91539833
LT
723}
724
cd2bdc53 725int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07 726{
bd1470b8 727 opt->shown_dashes = 0;
5f1c3f07 728 diffcore_std(&opt->diffopt);
91539833 729
5f1c3f07
JH
730 if (diff_queue_is_empty()) {
731 int saved_fmt = opt->diffopt.output_format;
732 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
733 diff_flush(&opt->diffopt);
734 opt->diffopt.output_format = saved_fmt;
735 return 0;
736 }
91539833 737
3969cf7d 738 if (opt->loginfo && !opt->no_commit_id) {
02865655 739 show_log(opt);
304b5af6
LT
740 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
741 opt->verbose_header &&
3969cf7d 742 opt->commit_format != CMIT_FMT_ONELINE) {
1d34c50f
JH
743 /*
744 * When showing a verbose header (i.e. log message),
745 * and not in --pretty=oneline format, we would want
746 * an extra newline between the end of log and the
747 * diff/diffstat output for readability.
748 */
3969cf7d 749 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
81bd1b2a
BY
750 if (opt->diffopt.output_prefix) {
751 struct strbuf *msg = NULL;
752 msg = opt->diffopt.output_prefix(&opt->diffopt,
753 opt->diffopt.output_prefix_data);
754 fwrite(msg->buf, msg->len, 1, stdout);
755 }
1d34c50f
JH
756
757 /*
758 * We may have shown three-dashes line early
759 * between notes and the log message, in which
760 * case we only want a blank line after the
761 * notes without (an extra) three-dashes line.
762 * Otherwise, we show the three-dashes line if
763 * we are showing the patch with diffstat, but
764 * in that case, there is no extra blank line
765 * after the three-dashes line.
766 */
767 if (!opt->shown_dashes &&
768 (pch & opt->diffopt.output_format) == pch)
769 printf("---");
770 putchar('\n');
3969cf7d
JH
771 }
772 }
5f1c3f07
JH
773 diff_flush(&opt->diffopt);
774 return 1;
775}
776
cd2bdc53 777static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07 778{
82889295 779 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
91539833 780 return !opt->loginfo;
5f1c3f07
JH
781}
782
91539833
LT
783/*
784 * Show the diff of a commit.
785 *
786 * Return true if we printed any log info messages
787 */
788static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 789{
91539833 790 int showed_log;
5f1c3f07
JH
791 struct commit_list *parents;
792 unsigned const char *sha1 = commit->object.sha1;
793
84102a33 794 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
91539833
LT
795 return 0;
796
5f1c3f07 797 /* Root commit? */
91539833
LT
798 parents = commit->parents;
799 if (!parents) {
2b60356d
RS
800 if (opt->show_root_diff) {
801 diff_root_tree_sha1(sha1, "", &opt->diffopt);
802 log_tree_diff_flush(opt);
803 }
91539833 804 return !opt->loginfo;
5f1c3f07
JH
805 }
806
807 /* More than one parent? */
91539833 808 if (parents && parents->next) {
5f1c3f07
JH
809 if (opt->ignore_merges)
810 return 0;
811 else if (opt->combine_merges)
812 return do_diff_combined(opt, commit);
88d9d45d
PB
813 else if (opt->first_parent_only) {
814 /*
815 * Generate merge log entry only for the first
816 * parent, showing summary diff of the others
817 * we merged _in_.
818 */
819 diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
820 log_tree_diff_flush(opt);
821 return !opt->loginfo;
822 }
91539833
LT
823
824 /* If we show individual diffs, show the parent info */
825 log->parent = parents->item;
5f1c3f07
JH
826 }
827
91539833
LT
828 showed_log = 0;
829 for (;;) {
5f1c3f07 830 struct commit *parent = parents->item;
5f1c3f07 831
91539833
LT
832 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
833 log_tree_diff_flush(opt);
834
835 showed_log |= !opt->loginfo;
836
837 /* Set up the log info for the next parent, if any.. */
838 parents = parents->next;
839 if (!parents)
840 break;
841 log->parent = parents->item;
842 opt->loginfo = log;
843 }
844 return showed_log;
845}
846
847int log_tree_commit(struct rev_info *opt, struct commit *commit)
848{
849 struct log_info log;
3eefc189 850 int shown;
91539833
LT
851
852 log.commit = commit;
853 log.parent = NULL;
854 opt->loginfo = &log;
855
3eefc189
JH
856 shown = log_tree_diff(opt, commit, &log);
857 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 858 log.parent = NULL;
02865655 859 show_log(opt);
3eefc189 860 shown = 1;
5f1c3f07 861 }
91539833 862 opt->loginfo = NULL;
06f59e9f 863 maybe_flush_or_die(stdout, "stdout");
3eefc189 864 return shown;
5f1c3f07 865}