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