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