]>
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" |
5f1c3f07 | 11 | |
ca135e7a LT |
12 | struct decoration name_decoration = { "object names" }; |
13 | ||
a7524128 NR |
14 | enum decoration_type { |
15 | DECORATION_NONE = 0, | |
16 | DECORATION_REF_LOCAL, | |
17 | DECORATION_REF_REMOTE, | |
18 | DECORATION_REF_TAG, | |
19 | DECORATION_REF_STASH, | |
20 | DECORATION_REF_HEAD, | |
76f5df30 | 21 | DECORATION_GRAFTED, |
a7524128 NR |
22 | }; |
23 | ||
67a4b586 NR |
24 | static char decoration_colors[][COLOR_MAXLEN] = { |
25 | GIT_COLOR_RESET, | |
26 | GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */ | |
27 | GIT_COLOR_BOLD_RED, /* REF_REMOTE */ | |
28 | GIT_COLOR_BOLD_YELLOW, /* REF_TAG */ | |
29 | GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */ | |
30 | GIT_COLOR_BOLD_CYAN, /* REF_HEAD */ | |
76f5df30 | 31 | GIT_COLOR_BOLD_BLUE, /* GRAFTED */ |
67a4b586 NR |
32 | }; |
33 | ||
34 | static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix) | |
35 | { | |
36 | if (decorate_use_color) | |
37 | return decoration_colors[ix]; | |
38 | return ""; | |
39 | } | |
40 | ||
5e11bee6 NR |
41 | static int parse_decorate_color_slot(const char *slot) |
42 | { | |
43 | /* | |
44 | * We're comparing with 'ignore-case' on | |
45 | * (because config.c sets them all tolower), | |
46 | * but let's match the letters in the literal | |
47 | * string values here with how they are | |
48 | * documented in Documentation/config.txt, for | |
49 | * consistency. | |
50 | * | |
51 | * We love being consistent, don't we? | |
52 | */ | |
53 | if (!strcasecmp(slot, "branch")) | |
54 | return DECORATION_REF_LOCAL; | |
55 | if (!strcasecmp(slot, "remoteBranch")) | |
56 | return DECORATION_REF_REMOTE; | |
57 | if (!strcasecmp(slot, "tag")) | |
58 | return DECORATION_REF_TAG; | |
59 | if (!strcasecmp(slot, "stash")) | |
60 | return DECORATION_REF_STASH; | |
61 | if (!strcasecmp(slot, "HEAD")) | |
62 | return DECORATION_REF_HEAD; | |
63 | return -1; | |
64 | } | |
65 | ||
66 | int parse_decorate_color_config(const char *var, const int ofs, const char *value) | |
67 | { | |
68 | int slot = parse_decorate_color_slot(var + ofs); | |
69 | if (slot < 0) | |
70 | return 0; | |
71 | if (!value) | |
72 | return config_error_nonbool(var); | |
73 | color_parse(value, var, decoration_colors[slot]); | |
74 | return 0; | |
75 | } | |
76 | ||
67a4b586 NR |
77 | /* |
78 | * log-tree.c uses DIFF_OPT_TST for determining whether to use color | |
79 | * for showing the commit sha1, use the same check for --decorate | |
80 | */ | |
81 | #define decorate_get_color_opt(o, ix) \ | |
82 | decorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix) | |
83 | ||
a7524128 | 84 | static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj) |
cab4feb6 | 85 | { |
cab4feb6 | 86 | int nlen = strlen(name); |
a7524128 NR |
87 | struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen); |
88 | memcpy(res->name, name, nlen + 1); | |
89 | res->type = type; | |
cab4feb6 RS |
90 | res->next = add_decoration(&name_decoration, obj, res); |
91 | } | |
92 | ||
93 | static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data) | |
94 | { | |
5267d292 | 95 | struct object *obj; |
a7524128 | 96 | enum decoration_type type = DECORATION_NONE; |
5267d292 NTND |
97 | |
98 | if (!prefixcmp(refname, "refs/replace/")) { | |
99 | unsigned char original_sha1[20]; | |
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 | |
594ffe80 | 114 | if (!prefixcmp(refname, "refs/heads/")) |
a7524128 | 115 | type = DECORATION_REF_LOCAL; |
594ffe80 | 116 | else if (!prefixcmp(refname, "refs/remotes/")) |
a7524128 | 117 | type = DECORATION_REF_REMOTE; |
594ffe80 | 118 | else if (!prefixcmp(refname, "refs/tags/")) |
a7524128 NR |
119 | type = DECORATION_REF_TAG; |
120 | else if (!prefixcmp(refname, "refs/stash")) | |
121 | type = DECORATION_REF_STASH; | |
122 | else if (!prefixcmp(refname, "HEAD")) | |
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; | |
a7524128 | 132 | add_name_decoration(DECORATION_REF_TAG, refname, obj); |
cab4feb6 RS |
133 | } |
134 | return 0; | |
135 | } | |
136 | ||
76f5df30 NTND |
137 | static int add_graft_decoration(const struct commit_graft *graft, void *cb_data) |
138 | { | |
139 | struct commit *commit = lookup_commit(graft->sha1); | |
140 | if (!commit) | |
141 | return 0; | |
142 | add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object); | |
143 | return 0; | |
144 | } | |
145 | ||
33e7018c | 146 | void load_ref_decorations(int flags) |
cab4feb6 RS |
147 | { |
148 | static int loaded; | |
149 | if (!loaded) { | |
150 | loaded = 1; | |
33e7018c | 151 | for_each_ref(add_ref_decoration, &flags); |
77abcbdc | 152 | head_ref(add_ref_decoration, &flags); |
76f5df30 | 153 | for_each_commit_graft(add_graft_decoration, NULL); |
cab4feb6 RS |
154 | } |
155 | } | |
156 | ||
c8c893c6 LT |
157 | static void show_parents(struct commit *commit, int abbrev) |
158 | { | |
159 | struct commit_list *p; | |
160 | for (p = commit->parents; p ; p = p->next) { | |
161 | struct commit *parent = p->item; | |
7fcda920 | 162 | printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev)); |
c8c893c6 LT |
163 | } |
164 | } | |
165 | ||
0f3a290b | 166 | void show_decorations(struct rev_info *opt, struct commit *commit) |
ca135e7a LT |
167 | { |
168 | const char *prefix; | |
169 | struct name_decoration *decoration; | |
67a4b586 NR |
170 | const char *color_commit = |
171 | diff_get_color_opt(&opt->diffopt, DIFF_COMMIT); | |
172 | const char *color_reset = | |
173 | decorate_get_color_opt(&opt->diffopt, DECORATION_NONE); | |
ca135e7a | 174 | |
0f3a290b | 175 | if (opt->show_source && commit->util) |
8c4021ab | 176 | printf("\t%s", (char *) commit->util); |
d467a525 LT |
177 | if (!opt->show_decorations) |
178 | return; | |
ca135e7a LT |
179 | decoration = lookup_decoration(&name_decoration, &commit->object); |
180 | if (!decoration) | |
181 | return; | |
182 | prefix = " ("; | |
183 | while (decoration) { | |
a7524128 | 184 | printf("%s", prefix); |
67a4b586 NR |
185 | fputs(decorate_get_color_opt(&opt->diffopt, decoration->type), |
186 | stdout); | |
a7524128 | 187 | if (decoration->type == DECORATION_REF_TAG) |
67a4b586 | 188 | fputs("tag: ", stdout); |
a7524128 | 189 | printf("%s", decoration->name); |
67a4b586 NR |
190 | fputs(color_reset, stdout); |
191 | fputs(color_commit, stdout); | |
ca135e7a LT |
192 | prefix = ", "; |
193 | decoration = decoration->next; | |
194 | } | |
195 | putchar(')'); | |
196 | } | |
197 | ||
fc1c75ec FBH |
198 | /* |
199 | * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches | |
200 | * Signed-off-by: and Acked-by: lines. | |
201 | */ | |
202 | static int detect_any_signoff(char *letter, int size) | |
203 | { | |
fd13b21f | 204 | char *cp; |
fc1c75ec FBH |
205 | int seen_colon = 0; |
206 | int seen_at = 0; | |
207 | int seen_name = 0; | |
208 | int seen_head = 0; | |
209 | ||
210 | cp = letter + size; | |
fd13b21f | 211 | while (letter <= --cp && *cp == '\n') |
fc1c75ec FBH |
212 | continue; |
213 | ||
214 | while (letter <= cp) { | |
fd13b21f | 215 | char ch = *cp--; |
fc1c75ec FBH |
216 | if (ch == '\n') |
217 | break; | |
218 | ||
219 | if (!seen_at) { | |
220 | if (ch == '@') | |
221 | seen_at = 1; | |
222 | continue; | |
223 | } | |
224 | if (!seen_colon) { | |
225 | if (ch == '@') | |
226 | return 0; | |
227 | else if (ch == ':') | |
228 | seen_colon = 1; | |
229 | else | |
230 | seen_name = 1; | |
231 | continue; | |
232 | } | |
233 | if (('A' <= ch && ch <= 'Z') || | |
234 | ('a' <= ch && ch <= 'z') || | |
235 | ch == '-') { | |
236 | seen_head = 1; | |
237 | continue; | |
238 | } | |
239 | /* no empty last line doesn't match */ | |
240 | return 0; | |
241 | } | |
242 | return seen_head && seen_name; | |
243 | } | |
244 | ||
674d1727 | 245 | static void append_signoff(struct strbuf *sb, const char *signoff) |
cf2251b6 | 246 | { |
cf2251b6 | 247 | static const char signed_off_by[] = "Signed-off-by: "; |
80583c0e | 248 | size_t signoff_len = strlen(signoff); |
fc1c75ec | 249 | int has_signoff = 0; |
80583c0e | 250 | char *cp; |
cf2251b6 | 251 | |
674d1727 | 252 | cp = sb->buf; |
cf2251b6 JH |
253 | |
254 | /* First see if we already have the sign-off by the signer */ | |
fc1c75ec FBH |
255 | while ((cp = strstr(cp, signed_off_by))) { |
256 | ||
257 | has_signoff = 1; | |
258 | ||
cf2251b6 | 259 | cp += strlen(signed_off_by); |
674d1727 | 260 | if (cp + signoff_len >= sb->buf + sb->len) |
fc1c75ec FBH |
261 | break; |
262 | if (strncmp(cp, signoff, signoff_len)) | |
263 | continue; | |
264 | if (!isspace(cp[signoff_len])) | |
265 | continue; | |
266 | /* we already have him */ | |
674d1727 | 267 | return; |
cf2251b6 JH |
268 | } |
269 | ||
fc1c75ec | 270 | if (!has_signoff) |
674d1727 | 271 | has_signoff = detect_any_signoff(sb->buf, sb->len); |
fc1c75ec FBH |
272 | |
273 | if (!has_signoff) | |
674d1727 | 274 | strbuf_addch(sb, '\n'); |
c35f4c37 | 275 | |
674d1727 PH |
276 | strbuf_addstr(sb, signed_off_by); |
277 | strbuf_add(sb, signoff, signoff_len); | |
278 | strbuf_addch(sb, '\n'); | |
cf2251b6 JH |
279 | } |
280 | ||
e00de24b JS |
281 | static unsigned int digits_in_number(unsigned int number) |
282 | { | |
283 | unsigned int i = 10, result = 1; | |
284 | while (i <= number) { | |
285 | i *= 10; | |
286 | result++; | |
287 | } | |
288 | return result; | |
289 | } | |
290 | ||
6fa8e627 SB |
291 | void get_patch_filename(struct commit *commit, int nr, const char *suffix, |
292 | struct strbuf *buf) | |
293 | { | |
294 | int suffix_len = strlen(suffix) + 1; | |
295 | int start_len = buf->len; | |
296 | ||
297 | strbuf_addf(buf, commit ? "%04d-" : "%d", nr); | |
298 | if (commit) { | |
b09b868f | 299 | int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len; |
dd2e794a TR |
300 | struct pretty_print_context ctx = {0}; |
301 | ctx.date_mode = DATE_NORMAL; | |
b09b868f | 302 | |
dd2e794a | 303 | format_commit_message(commit, "%f", buf, &ctx); |
b09b868f CC |
304 | if (max_len < buf->len) |
305 | strbuf_setlen(buf, max_len); | |
306 | strbuf_addstr(buf, suffix); | |
6fa8e627 SB |
307 | } |
308 | } | |
309 | ||
108dab28 | 310 | void log_write_email_headers(struct rev_info *opt, struct commit *commit, |
267123b4 JH |
311 | const char **subject_p, |
312 | const char **extra_headers_p, | |
313 | int *need_8bit_cte_p) | |
b02bd65f DB |
314 | { |
315 | const char *subject = NULL; | |
316 | const char *extra_headers = opt->extra_headers; | |
108dab28 | 317 | const char *name = sha1_to_hex(commit->object.sha1); |
267123b4 JH |
318 | |
319 | *need_8bit_cte_p = 0; /* unknown */ | |
b02bd65f DB |
320 | if (opt->total > 0) { |
321 | static char buffer[64]; | |
322 | snprintf(buffer, sizeof(buffer), | |
e7af8e49 | 323 | "Subject: [%s%s%0*d/%d] ", |
b02bd65f | 324 | opt->subject_prefix, |
e7af8e49 | 325 | *opt->subject_prefix ? " " : "", |
b02bd65f DB |
326 | digits_in_number(opt->total), |
327 | opt->nr, opt->total); | |
328 | subject = buffer; | |
329 | } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) { | |
330 | static char buffer[256]; | |
331 | snprintf(buffer, sizeof(buffer), | |
332 | "Subject: [%s] ", | |
333 | opt->subject_prefix); | |
334 | subject = buffer; | |
335 | } else { | |
336 | subject = "Subject: "; | |
337 | } | |
338 | ||
339 | printf("From %s Mon Sep 17 00:00:00 2001\n", name); | |
7fefda5c AS |
340 | graph_show_oneline(opt->graph); |
341 | if (opt->message_id) { | |
b02bd65f | 342 | printf("Message-Id: <%s>\n", opt->message_id); |
7fefda5c AS |
343 | graph_show_oneline(opt->graph); |
344 | } | |
b079c50e TR |
345 | if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) { |
346 | int i, n; | |
347 | n = opt->ref_message_ids->nr; | |
348 | printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string); | |
349 | for (i = 0; i < n; i++) | |
350 | printf("%s<%s>\n", (i > 0 ? "\t" : "References: "), | |
351 | opt->ref_message_ids->items[i].string); | |
7fefda5c AS |
352 | graph_show_oneline(opt->graph); |
353 | } | |
b02bd65f DB |
354 | if (opt->mime_boundary) { |
355 | static char subject_buffer[1024]; | |
356 | static char buffer[1024]; | |
108dab28 | 357 | struct strbuf filename = STRBUF_INIT; |
267123b4 | 358 | *need_8bit_cte_p = -1; /* NEVER */ |
b02bd65f DB |
359 | snprintf(subject_buffer, sizeof(subject_buffer) - 1, |
360 | "%s" | |
361 | "MIME-Version: 1.0\n" | |
362 | "Content-Type: multipart/mixed;" | |
363 | " boundary=\"%s%s\"\n" | |
364 | "\n" | |
365 | "This is a multi-part message in MIME " | |
366 | "format.\n" | |
367 | "--%s%s\n" | |
368 | "Content-Type: text/plain; " | |
369 | "charset=UTF-8; format=fixed\n" | |
370 | "Content-Transfer-Encoding: 8bit\n\n", | |
371 | extra_headers ? extra_headers : "", | |
372 | mime_boundary_leader, opt->mime_boundary, | |
373 | mime_boundary_leader, opt->mime_boundary); | |
374 | extra_headers = subject_buffer; | |
375 | ||
108dab28 SB |
376 | get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr, |
377 | opt->patch_suffix, &filename); | |
b02bd65f | 378 | snprintf(buffer, sizeof(buffer) - 1, |
6b2fbaaf | 379 | "\n--%s%s\n" |
b02bd65f | 380 | "Content-Type: text/x-patch;" |
108dab28 | 381 | " name=\"%s\"\n" |
b02bd65f DB |
382 | "Content-Transfer-Encoding: 8bit\n" |
383 | "Content-Disposition: %s;" | |
108dab28 | 384 | " filename=\"%s\"\n\n", |
b02bd65f | 385 | mime_boundary_leader, opt->mime_boundary, |
108dab28 | 386 | filename.buf, |
b02bd65f | 387 | opt->no_inline ? "attachment" : "inline", |
108dab28 | 388 | filename.buf); |
b02bd65f | 389 | opt->diffopt.stat_sep = buffer; |
108dab28 | 390 | strbuf_release(&filename); |
b02bd65f DB |
391 | } |
392 | *subject_p = subject; | |
393 | *extra_headers_p = extra_headers; | |
394 | } | |
395 | ||
02865655 | 396 | void show_log(struct rev_info *opt) |
91539833 | 397 | { |
f285a2d7 | 398 | struct strbuf msgbuf = STRBUF_INIT; |
39bc9a6c | 399 | struct log_info *log = opt->loginfo; |
91539833 | 400 | struct commit *commit = log->commit, *parent = log->parent; |
91539833 | 401 | int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40; |
dd2e794a TR |
402 | const char *extra_headers = opt->extra_headers; |
403 | struct pretty_print_context ctx = {0}; | |
91539833 LT |
404 | |
405 | opt->loginfo = NULL; | |
66b2ed09 | 406 | ctx.show_notes = opt->show_notes; |
91539833 | 407 | if (!opt->verbose_header) { |
7fefda5c AS |
408 | graph_show_commit(opt->graph); |
409 | ||
1df2d656 | 410 | if (!opt->graph) |
b1b47554 | 411 | put_revision_mark(opt, commit); |
7fcda920 | 412 | fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout); |
885cf808 | 413 | if (opt->print_parents) |
c8c893c6 | 414 | show_parents(commit, abbrev_commit); |
0f3a290b | 415 | show_decorations(opt, commit); |
7fefda5c AS |
416 | if (opt->graph && !graph_is_commit_finished(opt->graph)) { |
417 | putchar('\n'); | |
418 | graph_show_remainder(opt->graph); | |
419 | } | |
1dcb6922 | 420 | putchar(opt->diffopt.line_termination); |
91539833 LT |
421 | return; |
422 | } | |
423 | ||
424 | /* | |
8715227d JK |
425 | * If use_terminator is set, we already handled any record termination |
426 | * at the end of the last record. | |
02865655 AS |
427 | * Otherwise, add a diffopt.line_termination character before all |
428 | * entries but the first. (IOW, as a separator between entries) | |
91539833 | 429 | */ |
7fefda5c AS |
430 | if (opt->shown_one && !opt->use_terminator) { |
431 | /* | |
432 | * If entries are separated by a newline, the output | |
433 | * should look human-readable. If the last entry ended | |
434 | * with a newline, print the graph output before this | |
435 | * newline. Otherwise it will end up as a completely blank | |
436 | * line and will look like a gap in the graph. | |
437 | * | |
438 | * If the entry separator is not a newline, the output is | |
439 | * primarily intended for programmatic consumption, and we | |
440 | * never want the extra graph output before the entry | |
441 | * separator. | |
442 | */ | |
443 | if (opt->diffopt.line_termination == '\n' && | |
444 | !opt->missing_newline) | |
445 | graph_show_padding(opt->graph); | |
abd4e222 | 446 | putchar(opt->diffopt.line_termination); |
7fefda5c | 447 | } |
91539833 LT |
448 | opt->shown_one = 1; |
449 | ||
7fefda5c AS |
450 | /* |
451 | * If the history graph was requested, | |
452 | * print the graph, up to this commit's line | |
453 | */ | |
454 | graph_show_commit(opt->graph); | |
455 | ||
91539833 LT |
456 | /* |
457 | * Print header line of header.. | |
458 | */ | |
3eefc189 | 459 | |
596524b3 | 460 | if (opt->commit_format == CMIT_FMT_EMAIL) { |
dd2e794a TR |
461 | log_write_email_headers(opt, commit, &ctx.subject, &extra_headers, |
462 | &ctx.need_8bit_cte); | |
e52a5de4 | 463 | } else if (opt->commit_format != CMIT_FMT_USERFORMAT) { |
8f67f8ae | 464 | fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout); |
74bd9029 JH |
465 | if (opt->commit_format != CMIT_FMT_ONELINE) |
466 | fputs("commit ", stdout); | |
7528f27d | 467 | |
1df2d656 | 468 | if (!opt->graph) |
b1b47554 | 469 | put_revision_mark(opt, commit); |
7fcda920 | 470 | fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), |
74bd9029 | 471 | stdout); |
885cf808 | 472 | if (opt->print_parents) |
c66b6c06 | 473 | show_parents(commit, abbrev_commit); |
73f0a157 | 474 | if (parent) |
3eefc189 | 475 | printf(" (from %s)", |
7fcda920 | 476 | find_unique_abbrev(parent->object.sha1, |
3eefc189 | 477 | abbrev_commit)); |
0f3a290b | 478 | show_decorations(opt, commit); |
8f67f8ae | 479 | printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET)); |
7fefda5c AS |
480 | if (opt->commit_format == CMIT_FMT_ONELINE) { |
481 | putchar(' '); | |
482 | } else { | |
483 | putchar('\n'); | |
484 | graph_show_oneline(opt->graph); | |
485 | } | |
903b45fe | 486 | if (opt->reflog_info) { |
7fefda5c AS |
487 | /* |
488 | * setup_revisions() ensures that opt->reflog_info | |
489 | * and opt->graph cannot both be set, | |
490 | * so we don't need to worry about printing the | |
491 | * graph info here. | |
492 | */ | |
4d12a471 | 493 | show_reflog_message(opt->reflog_info, |
4e244cbc | 494 | opt->commit_format == CMIT_FMT_ONELINE, |
f4ea32f0 JK |
495 | opt->date_mode_explicit ? |
496 | opt->date_mode : | |
497 | DATE_NORMAL); | |
02865655 | 498 | if (opt->commit_format == CMIT_FMT_ONELINE) |
903b45fe | 499 | return; |
903b45fe | 500 | } |
3eefc189 | 501 | } |
91539833 | 502 | |
3131b713 LT |
503 | if (!commit->buffer) |
504 | return; | |
505 | ||
91539833 LT |
506 | /* |
507 | * And then the pretty-printed message itself | |
508 | */ | |
dd2e794a TR |
509 | if (ctx.need_8bit_cte >= 0) |
510 | ctx.need_8bit_cte = has_non_ascii(opt->add_signoff); | |
511 | ctx.date_mode = opt->date_mode; | |
512 | ctx.abbrev = opt->diffopt.abbrev; | |
513 | ctx.after_subject = extra_headers; | |
9553d2b2 | 514 | ctx.preserve_subject = opt->preserve_subject; |
8f8f5476 | 515 | ctx.reflog_info = opt->reflog_info; |
6bf13944 JK |
516 | ctx.fmt = opt->commit_format; |
517 | pretty_print_commit(&ctx, commit, &msgbuf); | |
cf2251b6 JH |
518 | |
519 | if (opt->add_signoff) | |
674d1727 | 520 | append_signoff(&msgbuf, opt->add_signoff); |
7fefda5c | 521 | if (opt->show_log_size) { |
674d1727 | 522 | printf("log size %i\n", (int)msgbuf.len); |
7fefda5c AS |
523 | graph_show_oneline(opt->graph); |
524 | } | |
9fa3465d | 525 | |
7fefda5c AS |
526 | /* |
527 | * Set opt->missing_newline if msgbuf doesn't | |
528 | * end in a newline (including if it is empty) | |
529 | */ | |
530 | if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n') | |
531 | opt->missing_newline = 1; | |
532 | else | |
533 | opt->missing_newline = 0; | |
534 | ||
535 | if (opt->graph) | |
536 | graph_show_commit_msg(opt->graph, &msgbuf); | |
537 | else | |
42c8c74c | 538 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout); |
7fefda5c AS |
539 | if (opt->use_terminator) { |
540 | if (!opt->missing_newline) | |
541 | graph_show_padding(opt->graph); | |
9b58bfe8 | 542 | putchar('\n'); |
7fefda5c AS |
543 | } |
544 | ||
674d1727 | 545 | strbuf_release(&msgbuf); |
91539833 LT |
546 | } |
547 | ||
cd2bdc53 | 548 | int log_tree_diff_flush(struct rev_info *opt) |
5f1c3f07 JH |
549 | { |
550 | diffcore_std(&opt->diffopt); | |
91539833 | 551 | |
5f1c3f07 JH |
552 | if (diff_queue_is_empty()) { |
553 | int saved_fmt = opt->diffopt.output_format; | |
554 | opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; | |
555 | diff_flush(&opt->diffopt); | |
556 | opt->diffopt.output_format = saved_fmt; | |
557 | return 0; | |
558 | } | |
91539833 | 559 | |
3969cf7d JH |
560 | if (opt->loginfo && !opt->no_commit_id) { |
561 | /* When showing a verbose header (i.e. log message), | |
562 | * and not in --pretty=oneline format, we would want | |
563 | * an extra newline between the end of log and the | |
564 | * output for readability. | |
565 | */ | |
02865655 | 566 | show_log(opt); |
304b5af6 LT |
567 | if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) && |
568 | opt->verbose_header && | |
3969cf7d JH |
569 | opt->commit_format != CMIT_FMT_ONELINE) { |
570 | int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH; | |
571 | if ((pch & opt->diffopt.output_format) == pch) | |
abd4e222 | 572 | printf("---"); |
81bd1b2a BY |
573 | if (opt->diffopt.output_prefix) { |
574 | struct strbuf *msg = NULL; | |
575 | msg = opt->diffopt.output_prefix(&opt->diffopt, | |
576 | opt->diffopt.output_prefix_data); | |
577 | fwrite(msg->buf, msg->len, 1, stdout); | |
578 | } | |
abd4e222 | 579 | putchar('\n'); |
3969cf7d JH |
580 | } |
581 | } | |
5f1c3f07 JH |
582 | diff_flush(&opt->diffopt); |
583 | return 1; | |
584 | } | |
585 | ||
cd2bdc53 | 586 | static int do_diff_combined(struct rev_info *opt, struct commit *commit) |
5f1c3f07 JH |
587 | { |
588 | unsigned const char *sha1 = commit->object.sha1; | |
589 | ||
91539833 LT |
590 | diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt); |
591 | return !opt->loginfo; | |
5f1c3f07 JH |
592 | } |
593 | ||
91539833 LT |
594 | /* |
595 | * Show the diff of a commit. | |
596 | * | |
597 | * Return true if we printed any log info messages | |
598 | */ | |
599 | static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) | |
5f1c3f07 | 600 | { |
91539833 | 601 | int showed_log; |
5f1c3f07 JH |
602 | struct commit_list *parents; |
603 | unsigned const char *sha1 = commit->object.sha1; | |
604 | ||
84102a33 | 605 | if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS)) |
91539833 LT |
606 | return 0; |
607 | ||
5f1c3f07 | 608 | /* Root commit? */ |
91539833 LT |
609 | parents = commit->parents; |
610 | if (!parents) { | |
2b60356d RS |
611 | if (opt->show_root_diff) { |
612 | diff_root_tree_sha1(sha1, "", &opt->diffopt); | |
613 | log_tree_diff_flush(opt); | |
614 | } | |
91539833 | 615 | return !opt->loginfo; |
5f1c3f07 JH |
616 | } |
617 | ||
618 | /* More than one parent? */ | |
91539833 | 619 | if (parents && parents->next) { |
5f1c3f07 JH |
620 | if (opt->ignore_merges) |
621 | return 0; | |
622 | else if (opt->combine_merges) | |
623 | return do_diff_combined(opt, commit); | |
88d9d45d PB |
624 | else if (opt->first_parent_only) { |
625 | /* | |
626 | * Generate merge log entry only for the first | |
627 | * parent, showing summary diff of the others | |
628 | * we merged _in_. | |
629 | */ | |
630 | diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt); | |
631 | log_tree_diff_flush(opt); | |
632 | return !opt->loginfo; | |
633 | } | |
91539833 LT |
634 | |
635 | /* If we show individual diffs, show the parent info */ | |
636 | log->parent = parents->item; | |
5f1c3f07 JH |
637 | } |
638 | ||
91539833 LT |
639 | showed_log = 0; |
640 | for (;;) { | |
5f1c3f07 | 641 | struct commit *parent = parents->item; |
5f1c3f07 | 642 | |
91539833 LT |
643 | diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt); |
644 | log_tree_diff_flush(opt); | |
645 | ||
646 | showed_log |= !opt->loginfo; | |
647 | ||
648 | /* Set up the log info for the next parent, if any.. */ | |
649 | parents = parents->next; | |
650 | if (!parents) | |
651 | break; | |
652 | log->parent = parents->item; | |
653 | opt->loginfo = log; | |
654 | } | |
655 | return showed_log; | |
656 | } | |
657 | ||
658 | int log_tree_commit(struct rev_info *opt, struct commit *commit) | |
659 | { | |
660 | struct log_info log; | |
3eefc189 | 661 | int shown; |
91539833 LT |
662 | |
663 | log.commit = commit; | |
664 | log.parent = NULL; | |
665 | opt->loginfo = &log; | |
666 | ||
3eefc189 JH |
667 | shown = log_tree_diff(opt, commit, &log); |
668 | if (!shown && opt->loginfo && opt->always_show_header) { | |
91539833 | 669 | log.parent = NULL; |
02865655 | 670 | show_log(opt); |
3eefc189 | 671 | shown = 1; |
5f1c3f07 | 672 | } |
91539833 | 673 | opt->loginfo = NULL; |
06f59e9f | 674 | maybe_flush_or_die(stdout, "stdout"); |
3eefc189 | 675 | return shown; |
5f1c3f07 | 676 | } |