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