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