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