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