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