4 #include "object-store.h"
9 #include "reflog-walk.h"
11 #include "string-list.h"
13 #include "gpg-interface.h"
14 #include "sequencer.h"
18 static struct decoration name_decoration
= { "object names" };
19 static int decoration_loaded
;
20 static int decoration_flags
;
22 static char decoration_colors
[][COLOR_MAXLEN
] = {
24 GIT_COLOR_BOLD_GREEN
, /* REF_LOCAL */
25 GIT_COLOR_BOLD_RED
, /* REF_REMOTE */
26 GIT_COLOR_BOLD_YELLOW
, /* REF_TAG */
27 GIT_COLOR_BOLD_MAGENTA
, /* REF_STASH */
28 GIT_COLOR_BOLD_CYAN
, /* REF_HEAD */
29 GIT_COLOR_BOLD_BLUE
, /* GRAFTED */
32 static const char *color_decorate_slots
[] = {
33 [DECORATION_REF_LOCAL
] = "branch",
34 [DECORATION_REF_REMOTE
] = "remoteBranch",
35 [DECORATION_REF_TAG
] = "tag",
36 [DECORATION_REF_STASH
] = "stash",
37 [DECORATION_REF_HEAD
] = "HEAD",
38 [DECORATION_GRAFTED
] = "grafted",
41 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
43 if (want_color(decorate_use_color
))
44 return decoration_colors
[ix
];
48 define_list_config_array(color_decorate_slots
);
50 int parse_decorate_color_config(const char *var
, const char *slot_name
, const char *value
)
52 int slot
= LOOKUP_CONFIG(color_decorate_slots
, slot_name
);
56 return config_error_nonbool(var
);
57 return color_parse(value
, decoration_colors
[slot
]);
61 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
62 * for showing the commit sha1, use the same check for --decorate
64 #define decorate_get_color_opt(o, ix) \
65 decorate_get_color((o)->use_color, ix)
67 void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
69 struct name_decoration
*res
;
70 FLEX_ALLOC_STR(res
, name
, name
);
72 res
->next
= add_decoration(&name_decoration
, obj
, res
);
75 const struct name_decoration
*get_name_decoration(const struct object
*obj
)
77 return lookup_decoration(&name_decoration
, obj
);
80 static int add_ref_decoration(const char *refname
, const struct object_id
*oid
,
81 int flags
, void *cb_data
)
84 enum decoration_type type
= DECORATION_NONE
;
85 struct decoration_filter
*filter
= (struct decoration_filter
*)cb_data
;
87 if (filter
&& !ref_filter_match(refname
,
88 filter
->include_ref_pattern
,
89 filter
->exclude_ref_pattern
))
92 if (starts_with(refname
, git_replace_ref_base
)) {
93 struct object_id original_oid
;
94 if (!check_replace_refs
)
96 if (get_oid_hex(refname
+ strlen(git_replace_ref_base
),
98 warning("invalid replace ref %s", refname
);
101 obj
= parse_object(&original_oid
);
103 add_name_decoration(DECORATION_GRAFTED
, "replaced", obj
);
107 obj
= parse_object(oid
);
111 if (starts_with(refname
, "refs/heads/"))
112 type
= DECORATION_REF_LOCAL
;
113 else if (starts_with(refname
, "refs/remotes/"))
114 type
= DECORATION_REF_REMOTE
;
115 else if (starts_with(refname
, "refs/tags/"))
116 type
= DECORATION_REF_TAG
;
117 else if (!strcmp(refname
, "refs/stash"))
118 type
= DECORATION_REF_STASH
;
119 else if (!strcmp(refname
, "HEAD"))
120 type
= DECORATION_REF_HEAD
;
122 add_name_decoration(type
, refname
, obj
);
123 while (obj
->type
== OBJ_TAG
) {
124 obj
= ((struct tag
*)obj
)->tagged
;
128 parse_object(&obj
->oid
);
129 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
134 static int add_graft_decoration(const struct commit_graft
*graft
, void *cb_data
)
136 struct commit
*commit
= lookup_commit(&graft
->oid
);
139 add_name_decoration(DECORATION_GRAFTED
, "grafted", &commit
->object
);
143 void load_ref_decorations(struct decoration_filter
*filter
, int flags
)
145 if (!decoration_loaded
) {
147 struct string_list_item
*item
;
148 for_each_string_list_item(item
, filter
->exclude_ref_pattern
) {
149 normalize_glob_ref(item
, NULL
, item
->string
);
151 for_each_string_list_item(item
, filter
->include_ref_pattern
) {
152 normalize_glob_ref(item
, NULL
, item
->string
);
155 decoration_loaded
= 1;
156 decoration_flags
= flags
;
157 for_each_ref(add_ref_decoration
, filter
);
158 head_ref(add_ref_decoration
, filter
);
159 for_each_commit_graft(add_graft_decoration
, filter
);
163 static void show_parents(struct commit
*commit
, int abbrev
, FILE *file
)
165 struct commit_list
*p
;
166 for (p
= commit
->parents
; p
; p
= p
->next
) {
167 struct commit
*parent
= p
->item
;
168 fprintf(file
, " %s", find_unique_abbrev(&parent
->object
.oid
, abbrev
));
172 static void show_children(struct rev_info
*opt
, struct commit
*commit
, int abbrev
)
174 struct commit_list
*p
= lookup_decoration(&opt
->children
, &commit
->object
);
175 for ( ; p
; p
= p
->next
) {
176 fprintf(opt
->diffopt
.file
, " %s", find_unique_abbrev(&p
->item
->object
.oid
, abbrev
));
181 * Do we have HEAD in the output, and also the branch it points at?
182 * If so, find that decoration entry for that current branch.
184 static const struct name_decoration
*current_pointed_by_HEAD(const struct name_decoration
*decoration
)
186 const struct name_decoration
*list
, *head
= NULL
;
187 const char *branch_name
= NULL
;
190 /* First find HEAD */
191 for (list
= decoration
; list
; list
= list
->next
)
192 if (list
->type
== DECORATION_REF_HEAD
) {
199 /* Now resolve and find the matching current branch */
200 branch_name
= resolve_ref_unsafe("HEAD", 0, NULL
, &rru_flags
);
201 if (!branch_name
|| !(rru_flags
& REF_ISSYMREF
))
204 if (!starts_with(branch_name
, "refs/"))
207 /* OK, do we have that ref in the list? */
208 for (list
= decoration
; list
; list
= list
->next
)
209 if ((list
->type
== DECORATION_REF_LOCAL
) &&
210 !strcmp(branch_name
, list
->name
)) {
217 static void show_name(struct strbuf
*sb
, const struct name_decoration
*decoration
)
219 if (decoration_flags
== DECORATE_SHORT_REFS
)
220 strbuf_addstr(sb
, prettify_refname(decoration
->name
));
222 strbuf_addstr(sb
, decoration
->name
);
226 * The caller makes sure there is no funny color before calling.
227 * format_decorations_extended makes sure the same after return.
229 void format_decorations_extended(struct strbuf
*sb
,
230 const struct commit
*commit
,
233 const char *separator
,
236 const struct name_decoration
*decoration
;
237 const struct name_decoration
*current_and_HEAD
;
238 const char *color_commit
=
239 diff_get_color(use_color
, DIFF_COMMIT
);
240 const char *color_reset
=
241 decorate_get_color(use_color
, DECORATION_NONE
);
243 decoration
= get_name_decoration(&commit
->object
);
247 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
250 * When both current and HEAD are there, only
251 * show HEAD->current where HEAD would have
252 * appeared, skipping the entry for current.
254 if (decoration
!= current_and_HEAD
) {
255 strbuf_addstr(sb
, color_commit
);
256 strbuf_addstr(sb
, prefix
);
257 strbuf_addstr(sb
, color_reset
);
258 strbuf_addstr(sb
, decorate_get_color(use_color
, decoration
->type
));
259 if (decoration
->type
== DECORATION_REF_TAG
)
260 strbuf_addstr(sb
, "tag: ");
262 show_name(sb
, decoration
);
264 if (current_and_HEAD
&&
265 decoration
->type
== DECORATION_REF_HEAD
) {
266 strbuf_addstr(sb
, " -> ");
267 strbuf_addstr(sb
, color_reset
);
268 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
269 show_name(sb
, current_and_HEAD
);
271 strbuf_addstr(sb
, color_reset
);
275 decoration
= decoration
->next
;
277 strbuf_addstr(sb
, color_commit
);
278 strbuf_addstr(sb
, suffix
);
279 strbuf_addstr(sb
, color_reset
);
282 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
284 struct strbuf sb
= STRBUF_INIT
;
287 char **slot
= revision_sources_peek(opt
->sources
, commit
);
290 fprintf(opt
->diffopt
.file
, "\t%s", *slot
);
292 if (!opt
->show_decorations
)
294 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
);
295 fputs(sb
.buf
, opt
->diffopt
.file
);
299 static unsigned int digits_in_number(unsigned int number
)
301 unsigned int i
= 10, result
= 1;
302 while (i
<= number
) {
309 void fmt_output_subject(struct strbuf
*filename
,
311 struct rev_info
*info
)
313 const char *suffix
= info
->patch_suffix
;
315 int start_len
= filename
->len
;
316 int max_len
= start_len
+ FORMAT_PATCH_NAME_MAX
- (strlen(suffix
) + 1);
318 if (0 < info
->reroll_count
)
319 strbuf_addf(filename
, "v%d-", info
->reroll_count
);
320 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
322 if (max_len
< filename
->len
)
323 strbuf_setlen(filename
, max_len
);
324 strbuf_addstr(filename
, suffix
);
327 void fmt_output_commit(struct strbuf
*filename
,
328 struct commit
*commit
,
329 struct rev_info
*info
)
331 struct pretty_print_context ctx
= {0};
332 struct strbuf subject
= STRBUF_INIT
;
334 format_commit_message(commit
, "%f", &subject
, &ctx
);
335 fmt_output_subject(filename
, subject
.buf
, info
);
336 strbuf_release(&subject
);
339 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
341 if (opt
->total
> 0) {
342 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
344 *opt
->subject_prefix
? " " : "",
345 digits_in_number(opt
->total
),
346 opt
->nr
, opt
->total
);
347 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
348 strbuf_addf(sb
, "Subject: [%s] ",
349 opt
->subject_prefix
);
351 strbuf_addstr(sb
, "Subject: ");
355 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
356 const char **extra_headers_p
,
357 int *need_8bit_cte_p
,
360 const char *extra_headers
= opt
->extra_headers
;
361 const char *name
= oid_to_hex(opt
->zero_commit
?
362 &null_oid
: &commit
->object
.oid
);
364 *need_8bit_cte_p
= 0; /* unknown */
366 fprintf(opt
->diffopt
.file
, "From %s Mon Sep 17 00:00:00 2001\n", name
);
367 graph_show_oneline(opt
->graph
);
368 if (opt
->message_id
) {
369 fprintf(opt
->diffopt
.file
, "Message-Id: <%s>\n", opt
->message_id
);
370 graph_show_oneline(opt
->graph
);
372 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
374 n
= opt
->ref_message_ids
->nr
;
375 fprintf(opt
->diffopt
.file
, "In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
376 for (i
= 0; i
< n
; i
++)
377 fprintf(opt
->diffopt
.file
, "%s<%s>\n", (i
> 0 ? "\t" : "References: "),
378 opt
->ref_message_ids
->items
[i
].string
);
379 graph_show_oneline(opt
->graph
);
381 if (opt
->mime_boundary
&& maybe_multipart
) {
382 static struct strbuf subject_buffer
= STRBUF_INIT
;
383 static struct strbuf buffer
= STRBUF_INIT
;
384 struct strbuf filename
= STRBUF_INIT
;
385 *need_8bit_cte_p
= -1; /* NEVER */
387 strbuf_reset(&subject_buffer
);
388 strbuf_reset(&buffer
);
390 strbuf_addf(&subject_buffer
,
392 "MIME-Version: 1.0\n"
393 "Content-Type: multipart/mixed;"
394 " boundary=\"%s%s\"\n"
396 "This is a multi-part message in MIME "
399 "Content-Type: text/plain; "
400 "charset=UTF-8; format=fixed\n"
401 "Content-Transfer-Encoding: 8bit\n\n",
402 extra_headers
? extra_headers
: "",
403 mime_boundary_leader
, opt
->mime_boundary
,
404 mime_boundary_leader
, opt
->mime_boundary
);
405 extra_headers
= subject_buffer
.buf
;
407 if (opt
->numbered_files
)
408 strbuf_addf(&filename
, "%d", opt
->nr
);
410 fmt_output_commit(&filename
, commit
, opt
);
413 "Content-Type: text/x-patch;"
415 "Content-Transfer-Encoding: 8bit\n"
416 "Content-Disposition: %s;"
417 " filename=\"%s\"\n\n",
418 mime_boundary_leader
, opt
->mime_boundary
,
420 opt
->no_inline
? "attachment" : "inline",
422 opt
->diffopt
.stat_sep
= buffer
.buf
;
423 strbuf_release(&filename
);
425 *extra_headers_p
= extra_headers
;
428 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
430 const char *color
, *reset
, *eol
;
432 color
= diff_get_color_opt(&opt
->diffopt
,
433 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
434 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
436 eol
= strchrnul(bol
, '\n');
437 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
439 graph_show_oneline(opt
->graph
);
440 bol
= (*eol
) ? (eol
+ 1) : eol
;
444 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
446 struct strbuf payload
= STRBUF_INIT
;
447 struct strbuf signature
= STRBUF_INIT
;
448 struct strbuf gpg_output
= STRBUF_INIT
;
451 if (parse_signed_commit(commit
, &payload
, &signature
) <= 0)
454 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
455 signature
.buf
, signature
.len
,
457 if (status
&& !gpg_output
.len
)
458 strbuf_addstr(&gpg_output
, "No signature\n");
460 show_sig_lines(opt
, status
, gpg_output
.buf
);
463 strbuf_release(&gpg_output
);
464 strbuf_release(&payload
);
465 strbuf_release(&signature
);
468 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
471 const struct commit_list
*parent
;
473 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
474 if (!oidcmp(&parent
->item
->object
.oid
, oid
))
481 static int is_common_merge(const struct commit
*commit
)
483 return (commit
->parents
484 && commit
->parents
->next
485 && !commit
->parents
->next
->next
);
488 static int show_one_mergetag(struct commit
*commit
,
489 struct commit_extra_header
*extra
,
492 struct rev_info
*opt
= (struct rev_info
*)data
;
493 struct object_id oid
;
495 struct strbuf verify_message
;
497 size_t payload_size
, gpg_message_offset
;
499 hash_object_file(extra
->value
, extra
->len
, type_name(OBJ_TAG
), &oid
);
500 tag
= lookup_tag(&oid
);
502 return -1; /* error message already given */
504 strbuf_init(&verify_message
, 256);
505 if (parse_tag_buffer(tag
, extra
->value
, extra
->len
))
506 strbuf_addstr(&verify_message
, "malformed mergetag\n");
507 else if (is_common_merge(commit
) &&
508 !oidcmp(&tag
->tagged
->oid
,
509 &commit
->parents
->next
->item
->object
.oid
))
510 strbuf_addf(&verify_message
,
511 "merged tag '%s'\n", tag
->tag
);
512 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
513 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
514 tag
->tag
, tag
->tagged
->oid
.hash
);
516 strbuf_addf(&verify_message
,
517 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
518 gpg_message_offset
= verify_message
.len
;
520 payload_size
= parse_signature(extra
->value
, extra
->len
);
522 if (extra
->len
> payload_size
) {
523 /* could have a good signature */
524 if (!verify_signed_buffer(extra
->value
, payload_size
,
525 extra
->value
+ payload_size
,
526 extra
->len
- payload_size
,
527 &verify_message
, NULL
))
528 status
= 0; /* good */
529 else if (verify_message
.len
<= gpg_message_offset
)
530 strbuf_addstr(&verify_message
, "No signature\n");
531 /* otherwise we couldn't verify, which is shown as bad */
534 show_sig_lines(opt
, status
, verify_message
.buf
);
535 strbuf_release(&verify_message
);
539 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
541 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
544 void show_log(struct rev_info
*opt
)
546 struct strbuf msgbuf
= STRBUF_INIT
;
547 struct log_info
*log
= opt
->loginfo
;
548 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
549 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: the_hash_algo
->hexsz
;
550 const char *extra_headers
= opt
->extra_headers
;
551 struct pretty_print_context ctx
= {0};
554 if (!opt
->verbose_header
) {
555 graph_show_commit(opt
->graph
);
558 put_revision_mark(opt
, commit
);
559 fputs(find_unique_abbrev(&commit
->object
.oid
, abbrev_commit
), opt
->diffopt
.file
);
560 if (opt
->print_parents
)
561 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
562 if (opt
->children
.name
)
563 show_children(opt
, commit
, abbrev_commit
);
564 show_decorations(opt
, commit
);
565 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
566 putc('\n', opt
->diffopt
.file
);
567 graph_show_remainder(opt
->graph
);
569 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
574 * If use_terminator is set, we already handled any record termination
575 * at the end of the last record.
576 * Otherwise, add a diffopt.line_termination character before all
577 * entries but the first. (IOW, as a separator between entries)
579 if (opt
->shown_one
&& !opt
->use_terminator
) {
581 * If entries are separated by a newline, the output
582 * should look human-readable. If the last entry ended
583 * with a newline, print the graph output before this
584 * newline. Otherwise it will end up as a completely blank
585 * line and will look like a gap in the graph.
587 * If the entry separator is not a newline, the output is
588 * primarily intended for programmatic consumption, and we
589 * never want the extra graph output before the entry
592 if (opt
->diffopt
.line_termination
== '\n' &&
593 !opt
->missing_newline
)
594 graph_show_padding(opt
->graph
);
595 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
600 * If the history graph was requested,
601 * print the graph, up to this commit's line
603 graph_show_commit(opt
->graph
);
606 * Print header line of header..
609 if (cmit_fmt_is_mail(opt
->commit_format
)) {
610 log_write_email_headers(opt
, commit
, &extra_headers
,
611 &ctx
.need_8bit_cte
, 1);
613 ctx
.print_email_subject
= 1;
614 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
615 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
616 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
617 fputs("commit ", opt
->diffopt
.file
);
620 put_revision_mark(opt
, commit
);
621 fputs(find_unique_abbrev(&commit
->object
.oid
,
624 if (opt
->print_parents
)
625 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
626 if (opt
->children
.name
)
627 show_children(opt
, commit
, abbrev_commit
);
629 fprintf(opt
->diffopt
.file
, " (from %s)",
630 find_unique_abbrev(&parent
->object
.oid
, abbrev_commit
));
631 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
632 show_decorations(opt
, commit
);
633 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
634 putc(' ', opt
->diffopt
.file
);
636 putc('\n', opt
->diffopt
.file
);
637 graph_show_oneline(opt
->graph
);
639 if (opt
->reflog_info
) {
641 * setup_revisions() ensures that opt->reflog_info
642 * and opt->graph cannot both be set,
643 * so we don't need to worry about printing the
646 show_reflog_message(opt
->reflog_info
,
647 opt
->commit_format
== CMIT_FMT_ONELINE
,
649 opt
->date_mode_explicit
);
650 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
655 if (opt
->show_signature
) {
656 show_signature(opt
, commit
);
657 show_mergetag(opt
, commit
);
660 if (opt
->show_notes
) {
662 struct strbuf notebuf
= STRBUF_INIT
;
664 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
665 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
666 get_log_output_encoding(), raw
);
667 ctx
.notes_message
= notebuf
.len
668 ? strbuf_detach(¬ebuf
, NULL
)
673 * And then the pretty-printed message itself
675 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
677 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
678 getenv("GIT_COMMITTER_EMAIL")));
679 ctx
.date_mode
= opt
->date_mode
;
680 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
681 ctx
.abbrev
= opt
->diffopt
.abbrev
;
682 ctx
.after_subject
= extra_headers
;
683 ctx
.preserve_subject
= opt
->preserve_subject
;
684 ctx
.reflog_info
= opt
->reflog_info
;
685 ctx
.fmt
= opt
->commit_format
;
686 ctx
.mailmap
= opt
->mailmap
;
687 ctx
.color
= opt
->diffopt
.use_color
;
688 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
689 ctx
.output_encoding
= get_log_output_encoding();
690 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
691 ctx
.from_ident
= &opt
->from_ident
;
693 ctx
.graph_width
= graph_width(opt
->graph
);
694 pretty_print_commit(&ctx
, commit
, &msgbuf
);
696 if (opt
->add_signoff
)
697 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
699 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
700 ctx
.notes_message
&& *ctx
.notes_message
) {
701 if (cmit_fmt_is_mail(ctx
.fmt
)) {
702 strbuf_addstr(&msgbuf
, "---\n");
703 opt
->shown_dashes
= 1;
705 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
708 if (opt
->show_log_size
) {
709 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
710 graph_show_oneline(opt
->graph
);
714 * Set opt->missing_newline if msgbuf doesn't
715 * end in a newline (including if it is empty)
717 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
718 opt
->missing_newline
= 1;
720 opt
->missing_newline
= 0;
722 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
723 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
724 if (!opt
->missing_newline
)
725 graph_show_padding(opt
->graph
);
726 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
729 strbuf_release(&msgbuf
);
730 free(ctx
.notes_message
);
733 int log_tree_diff_flush(struct rev_info
*opt
)
735 opt
->shown_dashes
= 0;
736 diffcore_std(&opt
->diffopt
);
738 if (diff_queue_is_empty()) {
739 int saved_fmt
= opt
->diffopt
.output_format
;
740 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
741 diff_flush(&opt
->diffopt
);
742 opt
->diffopt
.output_format
= saved_fmt
;
746 if (opt
->loginfo
&& !opt
->no_commit_id
) {
748 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
749 opt
->verbose_header
&&
750 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
751 !commit_format_is_empty(opt
->commit_format
)) {
753 * When showing a verbose header (i.e. log message),
754 * and not in --pretty=oneline format, we would want
755 * an extra newline between the end of log and the
756 * diff/diffstat output for readability.
758 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
759 if (opt
->diffopt
.output_prefix
) {
760 struct strbuf
*msg
= NULL
;
761 msg
= opt
->diffopt
.output_prefix(&opt
->diffopt
,
762 opt
->diffopt
.output_prefix_data
);
763 fwrite(msg
->buf
, msg
->len
, 1, opt
->diffopt
.file
);
767 * We may have shown three-dashes line early
768 * between notes and the log message, in which
769 * case we only want a blank line after the
770 * notes without (an extra) three-dashes line.
771 * Otherwise, we show the three-dashes line if
772 * we are showing the patch with diffstat, but
773 * in that case, there is no extra blank line
774 * after the three-dashes line.
776 if (!opt
->shown_dashes
&&
777 (pch
& opt
->diffopt
.output_format
) == pch
)
778 fprintf(opt
->diffopt
.file
, "---");
779 putc('\n', opt
->diffopt
.file
);
782 diff_flush(&opt
->diffopt
);
786 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
788 diff_tree_combined_merge(commit
, opt
->dense_combined_merges
, opt
);
789 return !opt
->loginfo
;
793 * Show the diff of a commit.
795 * Return true if we printed any log info messages
797 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
800 struct commit_list
*parents
;
801 struct object_id
*oid
;
803 if (!opt
->diff
&& !opt
->diffopt
.flags
.exit_with_status
)
806 parse_commit_or_die(commit
);
807 oid
= get_commit_tree_oid(commit
);
810 parents
= get_saved_parents(opt
, commit
);
812 if (opt
->show_root_diff
) {
813 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
814 log_tree_diff_flush(opt
);
816 return !opt
->loginfo
;
819 /* More than one parent? */
820 if (parents
&& parents
->next
) {
821 if (opt
->ignore_merges
)
823 else if (opt
->combine_merges
)
824 return do_diff_combined(opt
, commit
);
825 else if (opt
->first_parent_only
) {
827 * Generate merge log entry only for the first
828 * parent, showing summary diff of the others
831 parse_commit_or_die(parents
->item
);
832 diff_tree_oid(get_commit_tree_oid(parents
->item
),
833 oid
, "", &opt
->diffopt
);
834 log_tree_diff_flush(opt
);
835 return !opt
->loginfo
;
838 /* If we show individual diffs, show the parent info */
839 log
->parent
= parents
->item
;
844 struct commit
*parent
= parents
->item
;
846 parse_commit_or_die(parent
);
847 diff_tree_oid(get_commit_tree_oid(parent
),
848 oid
, "", &opt
->diffopt
);
849 log_tree_diff_flush(opt
);
851 showed_log
|= !opt
->loginfo
;
853 /* Set up the log info for the next parent, if any.. */
854 parents
= parents
->next
;
857 log
->parent
= parents
->item
;
863 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
866 int shown
, close_file
= opt
->diffopt
.close_file
;
871 opt
->diffopt
.close_file
= 0;
873 if (opt
->line_level_traverse
)
874 return line_log_print(opt
, commit
);
876 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
877 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
878 shown
= log_tree_diff(opt
, commit
, &log
);
879 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
884 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
885 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
887 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
889 fclose(opt
->diffopt
.file
);