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