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