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