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