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