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