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