8 #include "reflog-walk.h"
10 #include "string-list.h"
12 #include "gpg-interface.h"
13 #include "sequencer.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
] = {
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 *color_decorate_slots
[] = {
32 [DECORATION_REF_LOCAL
] = "branch",
33 [DECORATION_REF_REMOTE
] = "remoteBranch",
34 [DECORATION_REF_TAG
] = "tag",
35 [DECORATION_REF_STASH
] = "stash",
36 [DECORATION_REF_HEAD
] = "HEAD",
37 [DECORATION_GRAFTED
] = "grafted",
40 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
42 if (want_color(decorate_use_color
))
43 return decoration_colors
[ix
];
47 define_list_config_array(color_decorate_slots
);
49 int parse_decorate_color_config(const char *var
, const char *slot_name
, const char *value
)
51 int slot
= LOOKUP_CONFIG(color_decorate_slots
, slot_name
);
55 return config_error_nonbool(var
);
56 return color_parse(value
, decoration_colors
[slot
]);
60 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
61 * for showing the commit sha1, use the same check for --decorate
63 #define decorate_get_color_opt(o, ix) \
64 decorate_get_color((o)->use_color, ix)
66 void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
68 struct name_decoration
*res
;
69 FLEX_ALLOC_STR(res
, name
, name
);
71 res
->next
= add_decoration(&name_decoration
, obj
, res
);
74 const struct name_decoration
*get_name_decoration(const struct object
*obj
)
76 return lookup_decoration(&name_decoration
, obj
);
79 static int add_ref_decoration(const char *refname
, const struct object_id
*oid
,
80 int flags
, void *cb_data
)
83 enum decoration_type type
= DECORATION_NONE
;
84 struct decoration_filter
*filter
= (struct decoration_filter
*)cb_data
;
86 if (filter
&& !ref_filter_match(refname
,
87 filter
->include_ref_pattern
,
88 filter
->exclude_ref_pattern
))
91 if (starts_with(refname
, git_replace_ref_base
)) {
92 struct object_id original_oid
;
93 if (!check_replace_refs
)
95 if (get_oid_hex(refname
+ strlen(git_replace_ref_base
),
97 warning("invalid replace ref %s", refname
);
100 obj
= parse_object(&original_oid
);
102 add_name_decoration(DECORATION_GRAFTED
, "replaced", obj
);
106 obj
= parse_object(oid
);
110 if (starts_with(refname
, "refs/heads/"))
111 type
= DECORATION_REF_LOCAL
;
112 else if (starts_with(refname
, "refs/remotes/"))
113 type
= DECORATION_REF_REMOTE
;
114 else if (starts_with(refname
, "refs/tags/"))
115 type
= DECORATION_REF_TAG
;
116 else if (!strcmp(refname
, "refs/stash"))
117 type
= DECORATION_REF_STASH
;
118 else if (!strcmp(refname
, "HEAD"))
119 type
= DECORATION_REF_HEAD
;
121 add_name_decoration(type
, refname
, obj
);
122 while (obj
->type
== OBJ_TAG
) {
123 obj
= ((struct tag
*)obj
)->tagged
;
127 parse_object(&obj
->oid
);
128 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
133 static int add_graft_decoration(const struct commit_graft
*graft
, void *cb_data
)
135 struct commit
*commit
= lookup_commit(&graft
->oid
);
138 add_name_decoration(DECORATION_GRAFTED
, "grafted", &commit
->object
);
142 void load_ref_decorations(struct decoration_filter
*filter
, int flags
)
144 if (!decoration_loaded
) {
146 struct string_list_item
*item
;
147 for_each_string_list_item(item
, filter
->exclude_ref_pattern
) {
148 normalize_glob_ref(item
, NULL
, item
->string
);
150 for_each_string_list_item(item
, filter
->include_ref_pattern
) {
151 normalize_glob_ref(item
, NULL
, item
->string
);
154 decoration_loaded
= 1;
155 decoration_flags
= flags
;
156 for_each_ref(add_ref_decoration
, filter
);
157 head_ref(add_ref_decoration
, filter
);
158 for_each_commit_graft(add_graft_decoration
, filter
);
162 static void show_parents(struct commit
*commit
, int abbrev
, FILE *file
)
164 struct commit_list
*p
;
165 for (p
= commit
->parents
; p
; p
= p
->next
) {
166 struct commit
*parent
= p
->item
;
167 fprintf(file
, " %s", find_unique_abbrev(&parent
->object
.oid
, abbrev
));
171 static void show_children(struct rev_info
*opt
, struct commit
*commit
, int abbrev
)
173 struct commit_list
*p
= lookup_decoration(&opt
->children
, &commit
->object
);
174 for ( ; p
; p
= p
->next
) {
175 fprintf(opt
->diffopt
.file
, " %s", find_unique_abbrev(&p
->item
->object
.oid
, abbrev
));
180 * Do we have HEAD in the output, and also the branch it points at?
181 * If so, find that decoration entry for that current branch.
183 static const struct name_decoration
*current_pointed_by_HEAD(const struct name_decoration
*decoration
)
185 const struct name_decoration
*list
, *head
= NULL
;
186 const char *branch_name
= NULL
;
189 /* First find HEAD */
190 for (list
= decoration
; list
; list
= list
->next
)
191 if (list
->type
== DECORATION_REF_HEAD
) {
198 /* Now resolve and find the matching current branch */
199 branch_name
= resolve_ref_unsafe("HEAD", 0, NULL
, &rru_flags
);
200 if (!branch_name
|| !(rru_flags
& REF_ISSYMREF
))
203 if (!starts_with(branch_name
, "refs/"))
206 /* OK, do we have that ref in the list? */
207 for (list
= decoration
; list
; list
= list
->next
)
208 if ((list
->type
== DECORATION_REF_LOCAL
) &&
209 !strcmp(branch_name
, list
->name
)) {
216 static void show_name(struct strbuf
*sb
, const struct name_decoration
*decoration
)
218 if (decoration_flags
== DECORATE_SHORT_REFS
)
219 strbuf_addstr(sb
, prettify_refname(decoration
->name
));
221 strbuf_addstr(sb
, decoration
->name
);
225 * The caller makes sure there is no funny color before calling.
226 * format_decorations_extended makes sure the same after return.
228 void format_decorations_extended(struct strbuf
*sb
,
229 const struct commit
*commit
,
232 const char *separator
,
235 const struct name_decoration
*decoration
;
236 const struct name_decoration
*current_and_HEAD
;
237 const char *color_commit
=
238 diff_get_color(use_color
, DIFF_COMMIT
);
239 const char *color_reset
=
240 decorate_get_color(use_color
, DECORATION_NONE
);
242 decoration
= get_name_decoration(&commit
->object
);
246 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
249 * When both current and HEAD are there, only
250 * show HEAD->current where HEAD would have
251 * appeared, skipping the entry for current.
253 if (decoration
!= current_and_HEAD
) {
254 strbuf_addstr(sb
, color_commit
);
255 strbuf_addstr(sb
, prefix
);
256 strbuf_addstr(sb
, color_reset
);
257 strbuf_addstr(sb
, decorate_get_color(use_color
, decoration
->type
));
258 if (decoration
->type
== DECORATION_REF_TAG
)
259 strbuf_addstr(sb
, "tag: ");
261 show_name(sb
, decoration
);
263 if (current_and_HEAD
&&
264 decoration
->type
== DECORATION_REF_HEAD
) {
265 strbuf_addstr(sb
, " -> ");
266 strbuf_addstr(sb
, color_reset
);
267 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
268 show_name(sb
, current_and_HEAD
);
270 strbuf_addstr(sb
, color_reset
);
274 decoration
= decoration
->next
;
276 strbuf_addstr(sb
, color_commit
);
277 strbuf_addstr(sb
, suffix
);
278 strbuf_addstr(sb
, color_reset
);
281 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
283 struct strbuf sb
= STRBUF_INIT
;
285 if (opt
->show_source
&& commit
->util
)
286 fprintf(opt
->diffopt
.file
, "\t%s", (char *) commit
->util
);
287 if (!opt
->show_decorations
)
289 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
);
290 fputs(sb
.buf
, opt
->diffopt
.file
);
294 static unsigned int digits_in_number(unsigned int number
)
296 unsigned int i
= 10, result
= 1;
297 while (i
<= number
) {
304 void fmt_output_subject(struct strbuf
*filename
,
306 struct rev_info
*info
)
308 const char *suffix
= info
->patch_suffix
;
310 int start_len
= filename
->len
;
311 int max_len
= start_len
+ FORMAT_PATCH_NAME_MAX
- (strlen(suffix
) + 1);
313 if (0 < info
->reroll_count
)
314 strbuf_addf(filename
, "v%d-", info
->reroll_count
);
315 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
317 if (max_len
< filename
->len
)
318 strbuf_setlen(filename
, max_len
);
319 strbuf_addstr(filename
, suffix
);
322 void fmt_output_commit(struct strbuf
*filename
,
323 struct commit
*commit
,
324 struct rev_info
*info
)
326 struct pretty_print_context ctx
= {0};
327 struct strbuf subject
= STRBUF_INIT
;
329 format_commit_message(commit
, "%f", &subject
, &ctx
);
330 fmt_output_subject(filename
, subject
.buf
, info
);
331 strbuf_release(&subject
);
334 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
336 if (opt
->total
> 0) {
337 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
339 *opt
->subject_prefix
? " " : "",
340 digits_in_number(opt
->total
),
341 opt
->nr
, opt
->total
);
342 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
343 strbuf_addf(sb
, "Subject: [%s] ",
344 opt
->subject_prefix
);
346 strbuf_addstr(sb
, "Subject: ");
350 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
351 const char **extra_headers_p
,
352 int *need_8bit_cte_p
,
355 const char *extra_headers
= opt
->extra_headers
;
356 const char *name
= oid_to_hex(opt
->zero_commit
?
357 &null_oid
: &commit
->object
.oid
);
359 *need_8bit_cte_p
= 0; /* unknown */
361 fprintf(opt
->diffopt
.file
, "From %s Mon Sep 17 00:00:00 2001\n", name
);
362 graph_show_oneline(opt
->graph
);
363 if (opt
->message_id
) {
364 fprintf(opt
->diffopt
.file
, "Message-Id: <%s>\n", opt
->message_id
);
365 graph_show_oneline(opt
->graph
);
367 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
369 n
= opt
->ref_message_ids
->nr
;
370 fprintf(opt
->diffopt
.file
, "In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
371 for (i
= 0; i
< n
; i
++)
372 fprintf(opt
->diffopt
.file
, "%s<%s>\n", (i
> 0 ? "\t" : "References: "),
373 opt
->ref_message_ids
->items
[i
].string
);
374 graph_show_oneline(opt
->graph
);
376 if (opt
->mime_boundary
&& maybe_multipart
) {
377 static char subject_buffer
[1024];
378 static char buffer
[1024];
379 struct strbuf filename
= STRBUF_INIT
;
380 *need_8bit_cte_p
= -1; /* NEVER */
381 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
383 "MIME-Version: 1.0\n"
384 "Content-Type: multipart/mixed;"
385 " boundary=\"%s%s\"\n"
387 "This is a multi-part message in MIME "
390 "Content-Type: text/plain; "
391 "charset=UTF-8; format=fixed\n"
392 "Content-Transfer-Encoding: 8bit\n\n",
393 extra_headers
? extra_headers
: "",
394 mime_boundary_leader
, opt
->mime_boundary
,
395 mime_boundary_leader
, opt
->mime_boundary
);
396 extra_headers
= subject_buffer
;
398 if (opt
->numbered_files
)
399 strbuf_addf(&filename
, "%d", opt
->nr
);
401 fmt_output_commit(&filename
, commit
, opt
);
402 snprintf(buffer
, sizeof(buffer
) - 1,
404 "Content-Type: text/x-patch;"
406 "Content-Transfer-Encoding: 8bit\n"
407 "Content-Disposition: %s;"
408 " filename=\"%s\"\n\n",
409 mime_boundary_leader
, opt
->mime_boundary
,
411 opt
->no_inline
? "attachment" : "inline",
413 opt
->diffopt
.stat_sep
= buffer
;
414 strbuf_release(&filename
);
416 *extra_headers_p
= extra_headers
;
419 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
421 const char *color
, *reset
, *eol
;
423 color
= diff_get_color_opt(&opt
->diffopt
,
424 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
425 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
427 eol
= strchrnul(bol
, '\n');
428 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
430 graph_show_oneline(opt
->graph
);
431 bol
= (*eol
) ? (eol
+ 1) : eol
;
435 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
437 struct strbuf payload
= STRBUF_INIT
;
438 struct strbuf signature
= STRBUF_INIT
;
439 struct strbuf gpg_output
= STRBUF_INIT
;
442 if (parse_signed_commit(commit
, &payload
, &signature
) <= 0)
445 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
446 signature
.buf
, signature
.len
,
448 if (status
&& !gpg_output
.len
)
449 strbuf_addstr(&gpg_output
, "No signature\n");
451 show_sig_lines(opt
, status
, gpg_output
.buf
);
454 strbuf_release(&gpg_output
);
455 strbuf_release(&payload
);
456 strbuf_release(&signature
);
459 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
462 const struct commit_list
*parent
;
464 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
465 if (!oidcmp(&parent
->item
->object
.oid
, oid
))
472 static int is_common_merge(const struct commit
*commit
)
474 return (commit
->parents
475 && commit
->parents
->next
476 && !commit
->parents
->next
->next
);
479 static int show_one_mergetag(struct commit
*commit
,
480 struct commit_extra_header
*extra
,
483 struct rev_info
*opt
= (struct rev_info
*)data
;
484 struct object_id oid
;
486 struct strbuf verify_message
;
488 size_t payload_size
, gpg_message_offset
;
490 hash_object_file(extra
->value
, extra
->len
, type_name(OBJ_TAG
), &oid
);
491 tag
= lookup_tag(&oid
);
493 return -1; /* error message already given */
495 strbuf_init(&verify_message
, 256);
496 if (parse_tag_buffer(tag
, extra
->value
, extra
->len
))
497 strbuf_addstr(&verify_message
, "malformed mergetag\n");
498 else if (is_common_merge(commit
) &&
499 !oidcmp(&tag
->tagged
->oid
,
500 &commit
->parents
->next
->item
->object
.oid
))
501 strbuf_addf(&verify_message
,
502 "merged tag '%s'\n", tag
->tag
);
503 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
504 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
505 tag
->tag
, tag
->tagged
->oid
.hash
);
507 strbuf_addf(&verify_message
,
508 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
509 gpg_message_offset
= verify_message
.len
;
511 payload_size
= parse_signature(extra
->value
, extra
->len
);
513 if (extra
->len
> payload_size
) {
514 /* could have a good signature */
515 if (!verify_signed_buffer(extra
->value
, payload_size
,
516 extra
->value
+ payload_size
,
517 extra
->len
- payload_size
,
518 &verify_message
, NULL
))
519 status
= 0; /* good */
520 else if (verify_message
.len
<= gpg_message_offset
)
521 strbuf_addstr(&verify_message
, "No signature\n");
522 /* otherwise we couldn't verify, which is shown as bad */
525 show_sig_lines(opt
, status
, verify_message
.buf
);
526 strbuf_release(&verify_message
);
530 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
532 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
535 void show_log(struct rev_info
*opt
)
537 struct strbuf msgbuf
= STRBUF_INIT
;
538 struct log_info
*log
= opt
->loginfo
;
539 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
540 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: GIT_SHA1_HEXSZ
;
541 const char *extra_headers
= opt
->extra_headers
;
542 struct pretty_print_context ctx
= {0};
545 if (!opt
->verbose_header
) {
546 graph_show_commit(opt
->graph
);
549 put_revision_mark(opt
, commit
);
550 fputs(find_unique_abbrev(&commit
->object
.oid
, abbrev_commit
), opt
->diffopt
.file
);
551 if (opt
->print_parents
)
552 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
553 if (opt
->children
.name
)
554 show_children(opt
, commit
, abbrev_commit
);
555 show_decorations(opt
, commit
);
556 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
557 putc('\n', opt
->diffopt
.file
);
558 graph_show_remainder(opt
->graph
);
560 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
565 * If use_terminator is set, we already handled any record termination
566 * at the end of the last record.
567 * Otherwise, add a diffopt.line_termination character before all
568 * entries but the first. (IOW, as a separator between entries)
570 if (opt
->shown_one
&& !opt
->use_terminator
) {
572 * If entries are separated by a newline, the output
573 * should look human-readable. If the last entry ended
574 * with a newline, print the graph output before this
575 * newline. Otherwise it will end up as a completely blank
576 * line and will look like a gap in the graph.
578 * If the entry separator is not a newline, the output is
579 * primarily intended for programmatic consumption, and we
580 * never want the extra graph output before the entry
583 if (opt
->diffopt
.line_termination
== '\n' &&
584 !opt
->missing_newline
)
585 graph_show_padding(opt
->graph
);
586 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
591 * If the history graph was requested,
592 * print the graph, up to this commit's line
594 graph_show_commit(opt
->graph
);
597 * Print header line of header..
600 if (cmit_fmt_is_mail(opt
->commit_format
)) {
601 log_write_email_headers(opt
, commit
, &extra_headers
,
602 &ctx
.need_8bit_cte
, 1);
604 ctx
.print_email_subject
= 1;
605 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
606 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
607 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
608 fputs("commit ", opt
->diffopt
.file
);
611 put_revision_mark(opt
, commit
);
612 fputs(find_unique_abbrev(&commit
->object
.oid
,
615 if (opt
->print_parents
)
616 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
617 if (opt
->children
.name
)
618 show_children(opt
, commit
, abbrev_commit
);
620 fprintf(opt
->diffopt
.file
, " (from %s)",
621 find_unique_abbrev(&parent
->object
.oid
, abbrev_commit
));
622 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
623 show_decorations(opt
, commit
);
624 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
625 putc(' ', opt
->diffopt
.file
);
627 putc('\n', opt
->diffopt
.file
);
628 graph_show_oneline(opt
->graph
);
630 if (opt
->reflog_info
) {
632 * setup_revisions() ensures that opt->reflog_info
633 * and opt->graph cannot both be set,
634 * so we don't need to worry about printing the
637 show_reflog_message(opt
->reflog_info
,
638 opt
->commit_format
== CMIT_FMT_ONELINE
,
640 opt
->date_mode_explicit
);
641 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
646 if (opt
->show_signature
) {
647 show_signature(opt
, commit
);
648 show_mergetag(opt
, commit
);
651 if (opt
->show_notes
) {
653 struct strbuf notebuf
= STRBUF_INIT
;
655 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
656 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
657 get_log_output_encoding(), raw
);
658 ctx
.notes_message
= notebuf
.len
659 ? strbuf_detach(¬ebuf
, NULL
)
664 * And then the pretty-printed message itself
666 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
668 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
669 getenv("GIT_COMMITTER_EMAIL")));
670 ctx
.date_mode
= opt
->date_mode
;
671 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
672 ctx
.abbrev
= opt
->diffopt
.abbrev
;
673 ctx
.after_subject
= extra_headers
;
674 ctx
.preserve_subject
= opt
->preserve_subject
;
675 ctx
.reflog_info
= opt
->reflog_info
;
676 ctx
.fmt
= opt
->commit_format
;
677 ctx
.mailmap
= opt
->mailmap
;
678 ctx
.color
= opt
->diffopt
.use_color
;
679 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
680 ctx
.output_encoding
= get_log_output_encoding();
681 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
682 ctx
.from_ident
= &opt
->from_ident
;
684 ctx
.graph_width
= graph_width(opt
->graph
);
685 pretty_print_commit(&ctx
, commit
, &msgbuf
);
687 if (opt
->add_signoff
)
688 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
690 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
691 ctx
.notes_message
&& *ctx
.notes_message
) {
692 if (cmit_fmt_is_mail(ctx
.fmt
)) {
693 strbuf_addstr(&msgbuf
, "---\n");
694 opt
->shown_dashes
= 1;
696 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
699 if (opt
->show_log_size
) {
700 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
701 graph_show_oneline(opt
->graph
);
705 * Set opt->missing_newline if msgbuf doesn't
706 * end in a newline (including if it is empty)
708 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
709 opt
->missing_newline
= 1;
711 opt
->missing_newline
= 0;
713 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
714 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
715 if (!opt
->missing_newline
)
716 graph_show_padding(opt
->graph
);
717 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
720 strbuf_release(&msgbuf
);
721 free(ctx
.notes_message
);
724 int log_tree_diff_flush(struct rev_info
*opt
)
726 opt
->shown_dashes
= 0;
727 diffcore_std(&opt
->diffopt
);
729 if (diff_queue_is_empty()) {
730 int saved_fmt
= opt
->diffopt
.output_format
;
731 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
732 diff_flush(&opt
->diffopt
);
733 opt
->diffopt
.output_format
= saved_fmt
;
737 if (opt
->loginfo
&& !opt
->no_commit_id
) {
739 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
740 opt
->verbose_header
&&
741 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
742 !commit_format_is_empty(opt
->commit_format
)) {
744 * When showing a verbose header (i.e. log message),
745 * and not in --pretty=oneline format, we would want
746 * an extra newline between the end of log and the
747 * diff/diffstat output for readability.
749 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
750 if (opt
->diffopt
.output_prefix
) {
751 struct strbuf
*msg
= NULL
;
752 msg
= opt
->diffopt
.output_prefix(&opt
->diffopt
,
753 opt
->diffopt
.output_prefix_data
);
754 fwrite(msg
->buf
, msg
->len
, 1, opt
->diffopt
.file
);
758 * We may have shown three-dashes line early
759 * between notes and the log message, in which
760 * case we only want a blank line after the
761 * notes without (an extra) three-dashes line.
762 * Otherwise, we show the three-dashes line if
763 * we are showing the patch with diffstat, but
764 * in that case, there is no extra blank line
765 * after the three-dashes line.
767 if (!opt
->shown_dashes
&&
768 (pch
& opt
->diffopt
.output_format
) == pch
)
769 fprintf(opt
->diffopt
.file
, "---");
770 putc('\n', opt
->diffopt
.file
);
773 diff_flush(&opt
->diffopt
);
777 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
779 diff_tree_combined_merge(commit
, opt
->dense_combined_merges
, opt
);
780 return !opt
->loginfo
;
784 * Show the diff of a commit.
786 * Return true if we printed any log info messages
788 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
791 struct commit_list
*parents
;
792 struct object_id
*oid
;
794 if (!opt
->diff
&& !opt
->diffopt
.flags
.exit_with_status
)
797 parse_commit_or_die(commit
);
798 oid
= get_commit_tree_oid(commit
);
801 parents
= get_saved_parents(opt
, commit
);
803 if (opt
->show_root_diff
) {
804 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
805 log_tree_diff_flush(opt
);
807 return !opt
->loginfo
;
810 /* More than one parent? */
811 if (parents
&& parents
->next
) {
812 if (opt
->ignore_merges
)
814 else if (opt
->combine_merges
)
815 return do_diff_combined(opt
, commit
);
816 else if (opt
->first_parent_only
) {
818 * Generate merge log entry only for the first
819 * parent, showing summary diff of the others
822 parse_commit_or_die(parents
->item
);
823 diff_tree_oid(get_commit_tree_oid(parents
->item
),
824 oid
, "", &opt
->diffopt
);
825 log_tree_diff_flush(opt
);
826 return !opt
->loginfo
;
829 /* If we show individual diffs, show the parent info */
830 log
->parent
= parents
->item
;
835 struct commit
*parent
= parents
->item
;
837 parse_commit_or_die(parent
);
838 diff_tree_oid(get_commit_tree_oid(parent
),
839 oid
, "", &opt
->diffopt
);
840 log_tree_diff_flush(opt
);
842 showed_log
|= !opt
->loginfo
;
844 /* Set up the log info for the next parent, if any.. */
845 parents
= parents
->next
;
848 log
->parent
= parents
->item
;
854 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
857 int shown
, close_file
= opt
->diffopt
.close_file
;
862 opt
->diffopt
.close_file
= 0;
864 if (opt
->line_level_traverse
)
865 return line_log_print(opt
, commit
);
867 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
868 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
869 shown
= log_tree_diff(opt
, commit
, &log
);
870 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
875 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
876 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
878 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
880 fclose(opt
->diffopt
.file
);