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