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