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