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