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