shallow: add repository argument to set_alternate_shallow_file
[git/debian.git] / log-tree.c
blob5660ee7c785efe953935aa6dd6b151c7513fcfe0
1 #include "cache.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "object-store.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "graph.h"
8 #include "log-tree.h"
9 #include "reflog-walk.h"
10 #include "refs.h"
11 #include "string-list.h"
12 #include "color.h"
13 #include "gpg-interface.h"
14 #include "sequencer.h"
15 #include "line-log.h"
17 static struct decoration name_decoration = { "object names" };
18 static int decoration_loaded;
19 static int decoration_flags;
21 static char decoration_colors[][COLOR_MAXLEN] = {
22 GIT_COLOR_RESET,
23 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
24 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
25 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
26 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
27 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
28 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
31 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
33 if (want_color(decorate_use_color))
34 return decoration_colors[ix];
35 return "";
38 static int parse_decorate_color_slot(const char *slot)
41 * We're comparing with 'ignore-case' on
42 * (because config.c sets them all tolower),
43 * but let's match the letters in the literal
44 * string values here with how they are
45 * documented in Documentation/config.txt, for
46 * consistency.
48 * We love being consistent, don't we?
50 if (!strcasecmp(slot, "branch"))
51 return DECORATION_REF_LOCAL;
52 if (!strcasecmp(slot, "remoteBranch"))
53 return DECORATION_REF_REMOTE;
54 if (!strcasecmp(slot, "tag"))
55 return DECORATION_REF_TAG;
56 if (!strcasecmp(slot, "stash"))
57 return DECORATION_REF_STASH;
58 if (!strcasecmp(slot, "HEAD"))
59 return DECORATION_REF_HEAD;
60 return -1;
63 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
65 int slot = parse_decorate_color_slot(slot_name);
66 if (slot < 0)
67 return 0;
68 if (!value)
69 return config_error_nonbool(var);
70 return color_parse(value, decoration_colors[slot]);
74 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
75 * for showing the commit sha1, use the same check for --decorate
77 #define decorate_get_color_opt(o, ix) \
78 decorate_get_color((o)->use_color, ix)
80 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
82 struct name_decoration *res;
83 FLEX_ALLOC_STR(res, name, name);
84 res->type = type;
85 res->next = add_decoration(&name_decoration, obj, res);
88 const struct name_decoration *get_name_decoration(const struct object *obj)
90 return lookup_decoration(&name_decoration, obj);
93 static int add_ref_decoration(const char *refname, const struct object_id *oid,
94 int flags, void *cb_data)
96 struct object *obj;
97 enum decoration_type type = DECORATION_NONE;
98 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
100 if (filter && !ref_filter_match(refname,
101 filter->include_ref_pattern,
102 filter->exclude_ref_pattern))
103 return 0;
105 if (starts_with(refname, git_replace_ref_base)) {
106 struct object_id original_oid;
107 if (!check_replace_refs)
108 return 0;
109 if (get_oid_hex(refname + strlen(git_replace_ref_base),
110 &original_oid)) {
111 warning("invalid replace ref %s", refname);
112 return 0;
114 obj = parse_object(&original_oid);
115 if (obj)
116 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
117 return 0;
120 obj = parse_object(oid);
121 if (!obj)
122 return 0;
124 if (starts_with(refname, "refs/heads/"))
125 type = DECORATION_REF_LOCAL;
126 else if (starts_with(refname, "refs/remotes/"))
127 type = DECORATION_REF_REMOTE;
128 else if (starts_with(refname, "refs/tags/"))
129 type = DECORATION_REF_TAG;
130 else if (!strcmp(refname, "refs/stash"))
131 type = DECORATION_REF_STASH;
132 else if (!strcmp(refname, "HEAD"))
133 type = DECORATION_REF_HEAD;
135 add_name_decoration(type, refname, obj);
136 while (obj->type == OBJ_TAG) {
137 obj = ((struct tag *)obj)->tagged;
138 if (!obj)
139 break;
140 if (!obj->parsed)
141 parse_object(&obj->oid);
142 add_name_decoration(DECORATION_REF_TAG, refname, obj);
144 return 0;
147 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
149 struct commit *commit = lookup_commit(&graft->oid);
150 if (!commit)
151 return 0;
152 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
153 return 0;
156 void load_ref_decorations(struct decoration_filter *filter, int flags)
158 if (!decoration_loaded) {
159 if (filter) {
160 struct string_list_item *item;
161 for_each_string_list_item(item, filter->exclude_ref_pattern) {
162 normalize_glob_ref(item, NULL, item->string);
164 for_each_string_list_item(item, filter->include_ref_pattern) {
165 normalize_glob_ref(item, NULL, item->string);
168 decoration_loaded = 1;
169 decoration_flags = flags;
170 for_each_ref(add_ref_decoration, filter);
171 head_ref(add_ref_decoration, filter);
172 for_each_commit_graft(add_graft_decoration, filter);
176 static void show_parents(struct commit *commit, int abbrev, FILE *file)
178 struct commit_list *p;
179 for (p = commit->parents; p ; p = p->next) {
180 struct commit *parent = p->item;
181 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
185 static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
187 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
188 for ( ; p; p = p->next) {
189 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
194 * Do we have HEAD in the output, and also the branch it points at?
195 * If so, find that decoration entry for that current branch.
197 static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
199 const struct name_decoration *list, *head = NULL;
200 const char *branch_name = NULL;
201 int rru_flags;
203 /* First find HEAD */
204 for (list = decoration; list; list = list->next)
205 if (list->type == DECORATION_REF_HEAD) {
206 head = list;
207 break;
209 if (!head)
210 return NULL;
212 /* Now resolve and find the matching current branch */
213 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
214 if (!branch_name || !(rru_flags & REF_ISSYMREF))
215 return NULL;
217 if (!starts_with(branch_name, "refs/"))
218 return NULL;
220 /* OK, do we have that ref in the list? */
221 for (list = decoration; list; list = list->next)
222 if ((list->type == DECORATION_REF_LOCAL) &&
223 !strcmp(branch_name, list->name)) {
224 return list;
227 return NULL;
230 static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
232 if (decoration_flags == DECORATE_SHORT_REFS)
233 strbuf_addstr(sb, prettify_refname(decoration->name));
234 else
235 strbuf_addstr(sb, decoration->name);
239 * The caller makes sure there is no funny color before calling.
240 * format_decorations_extended makes sure the same after return.
242 void format_decorations_extended(struct strbuf *sb,
243 const struct commit *commit,
244 int use_color,
245 const char *prefix,
246 const char *separator,
247 const char *suffix)
249 const struct name_decoration *decoration;
250 const struct name_decoration *current_and_HEAD;
251 const char *color_commit =
252 diff_get_color(use_color, DIFF_COMMIT);
253 const char *color_reset =
254 decorate_get_color(use_color, DECORATION_NONE);
256 decoration = get_name_decoration(&commit->object);
257 if (!decoration)
258 return;
260 current_and_HEAD = current_pointed_by_HEAD(decoration);
261 while (decoration) {
263 * When both current and HEAD are there, only
264 * show HEAD->current where HEAD would have
265 * appeared, skipping the entry for current.
267 if (decoration != current_and_HEAD) {
268 strbuf_addstr(sb, color_commit);
269 strbuf_addstr(sb, prefix);
270 strbuf_addstr(sb, color_reset);
271 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
272 if (decoration->type == DECORATION_REF_TAG)
273 strbuf_addstr(sb, "tag: ");
275 show_name(sb, decoration);
277 if (current_and_HEAD &&
278 decoration->type == DECORATION_REF_HEAD) {
279 strbuf_addstr(sb, " -> ");
280 strbuf_addstr(sb, color_reset);
281 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
282 show_name(sb, current_and_HEAD);
284 strbuf_addstr(sb, color_reset);
286 prefix = separator;
288 decoration = decoration->next;
290 strbuf_addstr(sb, color_commit);
291 strbuf_addstr(sb, suffix);
292 strbuf_addstr(sb, color_reset);
295 void show_decorations(struct rev_info *opt, struct commit *commit)
297 struct strbuf sb = STRBUF_INIT;
299 if (opt->show_source && commit->util)
300 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
301 if (!opt->show_decorations)
302 return;
303 format_decorations(&sb, commit, opt->diffopt.use_color);
304 fputs(sb.buf, opt->diffopt.file);
305 strbuf_release(&sb);
308 static unsigned int digits_in_number(unsigned int number)
310 unsigned int i = 10, result = 1;
311 while (i <= number) {
312 i *= 10;
313 result++;
315 return result;
318 void fmt_output_subject(struct strbuf *filename,
319 const char *subject,
320 struct rev_info *info)
322 const char *suffix = info->patch_suffix;
323 int nr = info->nr;
324 int start_len = filename->len;
325 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
327 if (0 < info->reroll_count)
328 strbuf_addf(filename, "v%d-", info->reroll_count);
329 strbuf_addf(filename, "%04d-%s", nr, subject);
331 if (max_len < filename->len)
332 strbuf_setlen(filename, max_len);
333 strbuf_addstr(filename, suffix);
336 void fmt_output_commit(struct strbuf *filename,
337 struct commit *commit,
338 struct rev_info *info)
340 struct pretty_print_context ctx = {0};
341 struct strbuf subject = STRBUF_INIT;
343 format_commit_message(commit, "%f", &subject, &ctx);
344 fmt_output_subject(filename, subject.buf, info);
345 strbuf_release(&subject);
348 void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
350 if (opt->total > 0) {
351 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
352 opt->subject_prefix,
353 *opt->subject_prefix ? " " : "",
354 digits_in_number(opt->total),
355 opt->nr, opt->total);
356 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
357 strbuf_addf(sb, "Subject: [%s] ",
358 opt->subject_prefix);
359 } else {
360 strbuf_addstr(sb, "Subject: ");
364 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
365 const char **extra_headers_p,
366 int *need_8bit_cte_p)
368 const char *extra_headers = opt->extra_headers;
369 const char *name = oid_to_hex(opt->zero_commit ?
370 &null_oid : &commit->object.oid);
372 *need_8bit_cte_p = 0; /* unknown */
374 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
375 graph_show_oneline(opt->graph);
376 if (opt->message_id) {
377 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
378 graph_show_oneline(opt->graph);
380 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
381 int i, n;
382 n = opt->ref_message_ids->nr;
383 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
384 for (i = 0; i < n; i++)
385 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
386 opt->ref_message_ids->items[i].string);
387 graph_show_oneline(opt->graph);
389 if (opt->mime_boundary) {
390 static char subject_buffer[1024];
391 static char buffer[1024];
392 struct strbuf filename = STRBUF_INIT;
393 *need_8bit_cte_p = -1; /* NEVER */
394 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
395 "%s"
396 "MIME-Version: 1.0\n"
397 "Content-Type: multipart/mixed;"
398 " boundary=\"%s%s\"\n"
399 "\n"
400 "This is a multi-part message in MIME "
401 "format.\n"
402 "--%s%s\n"
403 "Content-Type: text/plain; "
404 "charset=UTF-8; format=fixed\n"
405 "Content-Transfer-Encoding: 8bit\n\n",
406 extra_headers ? extra_headers : "",
407 mime_boundary_leader, opt->mime_boundary,
408 mime_boundary_leader, opt->mime_boundary);
409 extra_headers = subject_buffer;
411 if (opt->numbered_files)
412 strbuf_addf(&filename, "%d", opt->nr);
413 else
414 fmt_output_commit(&filename, commit, opt);
415 snprintf(buffer, sizeof(buffer) - 1,
416 "\n--%s%s\n"
417 "Content-Type: text/x-patch;"
418 " name=\"%s\"\n"
419 "Content-Transfer-Encoding: 8bit\n"
420 "Content-Disposition: %s;"
421 " filename=\"%s\"\n\n",
422 mime_boundary_leader, opt->mime_boundary,
423 filename.buf,
424 opt->no_inline ? "attachment" : "inline",
425 filename.buf);
426 opt->diffopt.stat_sep = buffer;
427 strbuf_release(&filename);
429 *extra_headers_p = extra_headers;
432 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
434 const char *color, *reset, *eol;
436 color = diff_get_color_opt(&opt->diffopt,
437 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
438 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
439 while (*bol) {
440 eol = strchrnul(bol, '\n');
441 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
442 *eol ? "\n" : "");
443 graph_show_oneline(opt->graph);
444 bol = (*eol) ? (eol + 1) : eol;
448 static void show_signature(struct rev_info *opt, struct commit *commit)
450 struct strbuf payload = STRBUF_INIT;
451 struct strbuf signature = STRBUF_INIT;
452 struct strbuf gpg_output = STRBUF_INIT;
453 int status;
455 if (parse_signed_commit(commit, &payload, &signature) <= 0)
456 goto out;
458 status = verify_signed_buffer(payload.buf, payload.len,
459 signature.buf, signature.len,
460 &gpg_output, NULL);
461 if (status && !gpg_output.len)
462 strbuf_addstr(&gpg_output, "No signature\n");
464 show_sig_lines(opt, status, gpg_output.buf);
466 out:
467 strbuf_release(&gpg_output);
468 strbuf_release(&payload);
469 strbuf_release(&signature);
472 static int which_parent(const struct object_id *oid, const struct commit *commit)
474 int nth;
475 const struct commit_list *parent;
477 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
478 if (!oidcmp(&parent->item->object.oid, oid))
479 return nth;
480 nth++;
482 return -1;
485 static int is_common_merge(const struct commit *commit)
487 return (commit->parents
488 && commit->parents->next
489 && !commit->parents->next->next);
492 static void show_one_mergetag(struct commit *commit,
493 struct commit_extra_header *extra,
494 void *data)
496 struct rev_info *opt = (struct rev_info *)data;
497 struct object_id oid;
498 struct tag *tag;
499 struct strbuf verify_message;
500 int status, nth;
501 size_t payload_size, gpg_message_offset;
503 hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
504 tag = lookup_tag(&oid);
505 if (!tag)
506 return; /* error message already given */
508 strbuf_init(&verify_message, 256);
509 if (parse_tag_buffer(tag, extra->value, extra->len))
510 strbuf_addstr(&verify_message, "malformed mergetag\n");
511 else if (is_common_merge(commit) &&
512 !oidcmp(&tag->tagged->oid,
513 &commit->parents->next->item->object.oid))
514 strbuf_addf(&verify_message,
515 "merged tag '%s'\n", tag->tag);
516 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
517 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
518 tag->tag, tag->tagged->oid.hash);
519 else
520 strbuf_addf(&verify_message,
521 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
522 gpg_message_offset = verify_message.len;
524 payload_size = parse_signature(extra->value, extra->len);
525 status = -1;
526 if (extra->len > payload_size) {
527 /* could have a good signature */
528 if (!verify_signed_buffer(extra->value, payload_size,
529 extra->value + payload_size,
530 extra->len - payload_size,
531 &verify_message, NULL))
532 status = 0; /* good */
533 else if (verify_message.len <= gpg_message_offset)
534 strbuf_addstr(&verify_message, "No signature\n");
535 /* otherwise we couldn't verify, which is shown as bad */
538 show_sig_lines(opt, status, verify_message.buf);
539 strbuf_release(&verify_message);
542 static void show_mergetag(struct rev_info *opt, struct commit *commit)
544 for_each_mergetag(show_one_mergetag, commit, opt);
547 void show_log(struct rev_info *opt)
549 struct strbuf msgbuf = STRBUF_INIT;
550 struct log_info *log = opt->loginfo;
551 struct commit *commit = log->commit, *parent = log->parent;
552 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
553 const char *extra_headers = opt->extra_headers;
554 struct pretty_print_context ctx = {0};
556 opt->loginfo = NULL;
557 if (!opt->verbose_header) {
558 graph_show_commit(opt->graph);
560 if (!opt->graph)
561 put_revision_mark(opt, commit);
562 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
563 if (opt->print_parents)
564 show_parents(commit, abbrev_commit, opt->diffopt.file);
565 if (opt->children.name)
566 show_children(opt, commit, abbrev_commit);
567 show_decorations(opt, commit);
568 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
569 putc('\n', opt->diffopt.file);
570 graph_show_remainder(opt->graph);
572 putc(opt->diffopt.line_termination, opt->diffopt.file);
573 return;
577 * If use_terminator is set, we already handled any record termination
578 * at the end of the last record.
579 * Otherwise, add a diffopt.line_termination character before all
580 * entries but the first. (IOW, as a separator between entries)
582 if (opt->shown_one && !opt->use_terminator) {
584 * If entries are separated by a newline, the output
585 * should look human-readable. If the last entry ended
586 * with a newline, print the graph output before this
587 * newline. Otherwise it will end up as a completely blank
588 * line and will look like a gap in the graph.
590 * If the entry separator is not a newline, the output is
591 * primarily intended for programmatic consumption, and we
592 * never want the extra graph output before the entry
593 * separator.
595 if (opt->diffopt.line_termination == '\n' &&
596 !opt->missing_newline)
597 graph_show_padding(opt->graph);
598 putc(opt->diffopt.line_termination, opt->diffopt.file);
600 opt->shown_one = 1;
603 * If the history graph was requested,
604 * print the graph, up to this commit's line
606 graph_show_commit(opt->graph);
609 * Print header line of header..
612 if (cmit_fmt_is_mail(opt->commit_format)) {
613 log_write_email_headers(opt, commit, &extra_headers,
614 &ctx.need_8bit_cte);
615 ctx.rev = opt;
616 ctx.print_email_subject = 1;
617 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
618 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
619 if (opt->commit_format != CMIT_FMT_ONELINE)
620 fputs("commit ", opt->diffopt.file);
622 if (!opt->graph)
623 put_revision_mark(opt, commit);
624 fputs(find_unique_abbrev(&commit->object.oid,
625 abbrev_commit),
626 opt->diffopt.file);
627 if (opt->print_parents)
628 show_parents(commit, abbrev_commit, opt->diffopt.file);
629 if (opt->children.name)
630 show_children(opt, commit, abbrev_commit);
631 if (parent)
632 fprintf(opt->diffopt.file, " (from %s)",
633 find_unique_abbrev(&parent->object.oid, abbrev_commit));
634 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
635 show_decorations(opt, commit);
636 if (opt->commit_format == CMIT_FMT_ONELINE) {
637 putc(' ', opt->diffopt.file);
638 } else {
639 putc('\n', opt->diffopt.file);
640 graph_show_oneline(opt->graph);
642 if (opt->reflog_info) {
644 * setup_revisions() ensures that opt->reflog_info
645 * and opt->graph cannot both be set,
646 * so we don't need to worry about printing the
647 * graph info here.
649 show_reflog_message(opt->reflog_info,
650 opt->commit_format == CMIT_FMT_ONELINE,
651 &opt->date_mode,
652 opt->date_mode_explicit);
653 if (opt->commit_format == CMIT_FMT_ONELINE)
654 return;
658 if (opt->show_signature) {
659 show_signature(opt, commit);
660 show_mergetag(opt, commit);
663 if (opt->show_notes) {
664 int raw;
665 struct strbuf notebuf = STRBUF_INIT;
667 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
668 format_display_notes(&commit->object.oid, &notebuf,
669 get_log_output_encoding(), raw);
670 ctx.notes_message = notebuf.len
671 ? strbuf_detach(&notebuf, NULL)
672 : xcalloc(1, 1);
676 * And then the pretty-printed message itself
678 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
679 ctx.need_8bit_cte =
680 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
681 getenv("GIT_COMMITTER_EMAIL")));
682 ctx.date_mode = opt->date_mode;
683 ctx.date_mode_explicit = opt->date_mode_explicit;
684 ctx.abbrev = opt->diffopt.abbrev;
685 ctx.after_subject = extra_headers;
686 ctx.preserve_subject = opt->preserve_subject;
687 ctx.reflog_info = opt->reflog_info;
688 ctx.fmt = opt->commit_format;
689 ctx.mailmap = opt->mailmap;
690 ctx.color = opt->diffopt.use_color;
691 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
692 ctx.output_encoding = get_log_output_encoding();
693 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
694 ctx.from_ident = &opt->from_ident;
695 if (opt->graph)
696 ctx.graph_width = graph_width(opt->graph);
697 pretty_print_commit(&ctx, commit, &msgbuf);
699 if (opt->add_signoff)
700 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
702 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
703 ctx.notes_message && *ctx.notes_message) {
704 if (cmit_fmt_is_mail(ctx.fmt)) {
705 strbuf_addstr(&msgbuf, "---\n");
706 opt->shown_dashes = 1;
708 strbuf_addstr(&msgbuf, ctx.notes_message);
711 if (opt->show_log_size) {
712 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
713 graph_show_oneline(opt->graph);
717 * Set opt->missing_newline if msgbuf doesn't
718 * end in a newline (including if it is empty)
720 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
721 opt->missing_newline = 1;
722 else
723 opt->missing_newline = 0;
725 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
726 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
727 if (!opt->missing_newline)
728 graph_show_padding(opt->graph);
729 putc(opt->diffopt.line_termination, opt->diffopt.file);
732 strbuf_release(&msgbuf);
733 free(ctx.notes_message);
736 int log_tree_diff_flush(struct rev_info *opt)
738 opt->shown_dashes = 0;
739 diffcore_std(&opt->diffopt);
741 if (diff_queue_is_empty()) {
742 int saved_fmt = opt->diffopt.output_format;
743 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
744 diff_flush(&opt->diffopt);
745 opt->diffopt.output_format = saved_fmt;
746 return 0;
749 if (opt->loginfo && !opt->no_commit_id) {
750 show_log(opt);
751 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
752 opt->verbose_header &&
753 opt->commit_format != CMIT_FMT_ONELINE &&
754 !commit_format_is_empty(opt->commit_format)) {
756 * When showing a verbose header (i.e. log message),
757 * and not in --pretty=oneline format, we would want
758 * an extra newline between the end of log and the
759 * diff/diffstat output for readability.
761 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
762 if (opt->diffopt.output_prefix) {
763 struct strbuf *msg = NULL;
764 msg = opt->diffopt.output_prefix(&opt->diffopt,
765 opt->diffopt.output_prefix_data);
766 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
770 * We may have shown three-dashes line early
771 * between notes and the log message, in which
772 * case we only want a blank line after the
773 * notes without (an extra) three-dashes line.
774 * Otherwise, we show the three-dashes line if
775 * we are showing the patch with diffstat, but
776 * in that case, there is no extra blank line
777 * after the three-dashes line.
779 if (!opt->shown_dashes &&
780 (pch & opt->diffopt.output_format) == pch)
781 fprintf(opt->diffopt.file, "---");
782 putc('\n', opt->diffopt.file);
785 diff_flush(&opt->diffopt);
786 return 1;
789 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
791 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
792 return !opt->loginfo;
796 * Show the diff of a commit.
798 * Return true if we printed any log info messages
800 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
802 int showed_log;
803 struct commit_list *parents;
804 struct object_id *oid;
806 if (!opt->diff && !opt->diffopt.flags.exit_with_status)
807 return 0;
809 parse_commit_or_die(commit);
810 oid = &commit->tree->object.oid;
812 /* Root commit? */
813 parents = get_saved_parents(opt, commit);
814 if (!parents) {
815 if (opt->show_root_diff) {
816 diff_root_tree_oid(oid, "", &opt->diffopt);
817 log_tree_diff_flush(opt);
819 return !opt->loginfo;
822 /* More than one parent? */
823 if (parents && parents->next) {
824 if (opt->ignore_merges)
825 return 0;
826 else if (opt->combine_merges)
827 return do_diff_combined(opt, commit);
828 else if (opt->first_parent_only) {
830 * Generate merge log entry only for the first
831 * parent, showing summary diff of the others
832 * we merged _in_.
834 parse_commit_or_die(parents->item);
835 diff_tree_oid(&parents->item->tree->object.oid,
836 oid, "", &opt->diffopt);
837 log_tree_diff_flush(opt);
838 return !opt->loginfo;
841 /* If we show individual diffs, show the parent info */
842 log->parent = parents->item;
845 showed_log = 0;
846 for (;;) {
847 struct commit *parent = parents->item;
849 parse_commit_or_die(parent);
850 diff_tree_oid(&parent->tree->object.oid,
851 oid, "", &opt->diffopt);
852 log_tree_diff_flush(opt);
854 showed_log |= !opt->loginfo;
856 /* Set up the log info for the next parent, if any.. */
857 parents = parents->next;
858 if (!parents)
859 break;
860 log->parent = parents->item;
861 opt->loginfo = log;
863 return showed_log;
866 int log_tree_commit(struct rev_info *opt, struct commit *commit)
868 struct log_info log;
869 int shown, close_file = opt->diffopt.close_file;
871 log.commit = commit;
872 log.parent = NULL;
873 opt->loginfo = &log;
874 opt->diffopt.close_file = 0;
876 if (opt->line_level_traverse)
877 return line_log_print(opt, commit);
879 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
880 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
881 shown = log_tree_diff(opt, commit, &log);
882 if (!shown && opt->loginfo && opt->always_show_header) {
883 log.parent = NULL;
884 show_log(opt);
885 shown = 1;
887 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
888 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
889 opt->loginfo = NULL;
890 maybe_flush_or_die(opt->diffopt.file, "stdout");
891 if (close_file)
892 fclose(opt->diffopt.file);
893 return shown;