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