4 #include "object-store.h"
5 #include "repository.h"
10 #include "reflog-walk.h"
12 #include "string-list.h"
14 #include "gpg-interface.h"
15 #include "sequencer.h"
18 #include "range-diff.h"
20 static struct decoration name_decoration
= { "object names" };
21 static int decoration_loaded
;
22 static int decoration_flags
;
24 static char decoration_colors
[][COLOR_MAXLEN
] = {
26 GIT_COLOR_BOLD_GREEN
, /* REF_LOCAL */
27 GIT_COLOR_BOLD_RED
, /* REF_REMOTE */
28 GIT_COLOR_BOLD_YELLOW
, /* REF_TAG */
29 GIT_COLOR_BOLD_MAGENTA
, /* REF_STASH */
30 GIT_COLOR_BOLD_CYAN
, /* REF_HEAD */
31 GIT_COLOR_BOLD_BLUE
, /* GRAFTED */
34 static const char *color_decorate_slots
[] = {
35 [DECORATION_REF_LOCAL
] = "branch",
36 [DECORATION_REF_REMOTE
] = "remoteBranch",
37 [DECORATION_REF_TAG
] = "tag",
38 [DECORATION_REF_STASH
] = "stash",
39 [DECORATION_REF_HEAD
] = "HEAD",
40 [DECORATION_GRAFTED
] = "grafted",
43 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
45 if (want_color(decorate_use_color
))
46 return decoration_colors
[ix
];
50 define_list_config_array(color_decorate_slots
);
52 int parse_decorate_color_config(const char *var
, const char *slot_name
, const char *value
)
54 int slot
= LOOKUP_CONFIG(color_decorate_slots
, slot_name
);
58 return config_error_nonbool(var
);
59 return color_parse(value
, decoration_colors
[slot
]);
63 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
64 * for showing the commit sha1, use the same check for --decorate
66 #define decorate_get_color_opt(o, ix) \
67 decorate_get_color((o)->use_color, ix)
69 void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
71 struct name_decoration
*res
;
72 FLEX_ALLOC_STR(res
, name
, name
);
74 res
->next
= add_decoration(&name_decoration
, obj
, res
);
77 const struct name_decoration
*get_name_decoration(const struct object
*obj
)
79 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
80 return lookup_decoration(&name_decoration
, obj
);
83 static int match_ref_pattern(const char *refname
,
84 const struct string_list_item
*item
)
87 if (item
->util
== NULL
) {
88 if (!wildmatch(item
->string
, refname
, 0))
92 if (skip_prefix(refname
, item
->string
, &rest
) &&
93 (!*rest
|| *rest
== '/'))
99 static int ref_filter_match(const char *refname
,
100 const struct decoration_filter
*filter
)
102 struct string_list_item
*item
;
103 const struct string_list
*exclude_patterns
= filter
->exclude_ref_pattern
;
104 const struct string_list
*include_patterns
= filter
->include_ref_pattern
;
105 const struct string_list
*exclude_patterns_config
=
106 filter
->exclude_ref_config_pattern
;
108 if (exclude_patterns
&& exclude_patterns
->nr
) {
109 for_each_string_list_item(item
, exclude_patterns
) {
110 if (match_ref_pattern(refname
, item
))
115 if (include_patterns
&& include_patterns
->nr
) {
116 for_each_string_list_item(item
, include_patterns
) {
117 if (match_ref_pattern(refname
, item
))
123 if (exclude_patterns_config
&& exclude_patterns_config
->nr
) {
124 for_each_string_list_item(item
, exclude_patterns_config
) {
125 if (match_ref_pattern(refname
, item
))
133 static int add_ref_decoration(const char *refname
, const struct object_id
*oid
,
134 int flags
, void *cb_data
)
137 enum decoration_type type
= DECORATION_NONE
;
138 struct decoration_filter
*filter
= (struct decoration_filter
*)cb_data
;
140 if (filter
&& !ref_filter_match(refname
, filter
))
143 if (starts_with(refname
, git_replace_ref_base
)) {
144 struct object_id original_oid
;
145 if (!read_replace_refs
)
147 if (get_oid_hex(refname
+ strlen(git_replace_ref_base
),
149 warning("invalid replace ref %s", refname
);
152 obj
= parse_object(the_repository
, &original_oid
);
154 add_name_decoration(DECORATION_GRAFTED
, "replaced", obj
);
158 obj
= parse_object(the_repository
, oid
);
162 if (starts_with(refname
, "refs/heads/"))
163 type
= DECORATION_REF_LOCAL
;
164 else if (starts_with(refname
, "refs/remotes/"))
165 type
= DECORATION_REF_REMOTE
;
166 else if (starts_with(refname
, "refs/tags/"))
167 type
= DECORATION_REF_TAG
;
168 else if (!strcmp(refname
, "refs/stash"))
169 type
= DECORATION_REF_STASH
;
170 else if (!strcmp(refname
, "HEAD"))
171 type
= DECORATION_REF_HEAD
;
173 add_name_decoration(type
, refname
, obj
);
174 while (obj
->type
== OBJ_TAG
) {
175 obj
= ((struct tag
*)obj
)->tagged
;
179 parse_object(the_repository
, &obj
->oid
);
180 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
185 static int add_graft_decoration(const struct commit_graft
*graft
, void *cb_data
)
187 struct commit
*commit
= lookup_commit(the_repository
, &graft
->oid
);
190 add_name_decoration(DECORATION_GRAFTED
, "grafted", &commit
->object
);
194 void load_ref_decorations(struct decoration_filter
*filter
, int flags
)
196 if (!decoration_loaded
) {
198 struct string_list_item
*item
;
199 for_each_string_list_item(item
, filter
->exclude_ref_pattern
) {
200 normalize_glob_ref(item
, NULL
, item
->string
);
202 for_each_string_list_item(item
, filter
->include_ref_pattern
) {
203 normalize_glob_ref(item
, NULL
, item
->string
);
205 for_each_string_list_item(item
, filter
->exclude_ref_config_pattern
) {
206 normalize_glob_ref(item
, NULL
, item
->string
);
209 decoration_loaded
= 1;
210 decoration_flags
= flags
;
211 for_each_ref(add_ref_decoration
, filter
);
212 head_ref(add_ref_decoration
, filter
);
213 for_each_commit_graft(add_graft_decoration
, filter
);
217 static void show_parents(struct commit
*commit
, int abbrev
, FILE *file
)
219 struct commit_list
*p
;
220 for (p
= commit
->parents
; p
; p
= p
->next
) {
221 struct commit
*parent
= p
->item
;
222 fprintf(file
, " %s", find_unique_abbrev(&parent
->object
.oid
, abbrev
));
226 static void show_children(struct rev_info
*opt
, struct commit
*commit
, int abbrev
)
228 struct commit_list
*p
= lookup_decoration(&opt
->children
, &commit
->object
);
229 for ( ; p
; p
= p
->next
) {
230 fprintf(opt
->diffopt
.file
, " %s", find_unique_abbrev(&p
->item
->object
.oid
, abbrev
));
235 * Do we have HEAD in the output, and also the branch it points at?
236 * If so, find that decoration entry for that current branch.
238 static const struct name_decoration
*current_pointed_by_HEAD(const struct name_decoration
*decoration
)
240 const struct name_decoration
*list
, *head
= NULL
;
241 const char *branch_name
= NULL
;
244 /* First find HEAD */
245 for (list
= decoration
; list
; list
= list
->next
)
246 if (list
->type
== DECORATION_REF_HEAD
) {
253 /* Now resolve and find the matching current branch */
254 branch_name
= resolve_ref_unsafe("HEAD", 0, NULL
, &rru_flags
);
255 if (!branch_name
|| !(rru_flags
& REF_ISSYMREF
))
258 if (!starts_with(branch_name
, "refs/"))
261 /* OK, do we have that ref in the list? */
262 for (list
= decoration
; list
; list
= list
->next
)
263 if ((list
->type
== DECORATION_REF_LOCAL
) &&
264 !strcmp(branch_name
, list
->name
)) {
271 static void show_name(struct strbuf
*sb
, const struct name_decoration
*decoration
)
273 if (decoration_flags
== DECORATE_SHORT_REFS
)
274 strbuf_addstr(sb
, prettify_refname(decoration
->name
));
276 strbuf_addstr(sb
, decoration
->name
);
280 * The caller makes sure there is no funny color before calling.
281 * format_decorations_extended makes sure the same after return.
283 void format_decorations_extended(struct strbuf
*sb
,
284 const struct commit
*commit
,
287 const char *separator
,
290 const struct name_decoration
*decoration
;
291 const struct name_decoration
*current_and_HEAD
;
292 const char *color_commit
=
293 diff_get_color(use_color
, DIFF_COMMIT
);
294 const char *color_reset
=
295 decorate_get_color(use_color
, DECORATION_NONE
);
297 decoration
= get_name_decoration(&commit
->object
);
301 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
304 * When both current and HEAD are there, only
305 * show HEAD->current where HEAD would have
306 * appeared, skipping the entry for current.
308 if (decoration
!= current_and_HEAD
) {
309 strbuf_addstr(sb
, color_commit
);
310 strbuf_addstr(sb
, prefix
);
311 strbuf_addstr(sb
, color_reset
);
312 strbuf_addstr(sb
, decorate_get_color(use_color
, decoration
->type
));
313 if (decoration
->type
== DECORATION_REF_TAG
)
314 strbuf_addstr(sb
, "tag: ");
316 show_name(sb
, decoration
);
318 if (current_and_HEAD
&&
319 decoration
->type
== DECORATION_REF_HEAD
) {
320 strbuf_addstr(sb
, " -> ");
321 strbuf_addstr(sb
, color_reset
);
322 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
323 show_name(sb
, current_and_HEAD
);
325 strbuf_addstr(sb
, color_reset
);
329 decoration
= decoration
->next
;
331 strbuf_addstr(sb
, color_commit
);
332 strbuf_addstr(sb
, suffix
);
333 strbuf_addstr(sb
, color_reset
);
336 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
338 struct strbuf sb
= STRBUF_INIT
;
341 char **slot
= revision_sources_peek(opt
->sources
, commit
);
344 fprintf(opt
->diffopt
.file
, "\t%s", *slot
);
346 if (!opt
->show_decorations
)
348 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
);
349 fputs(sb
.buf
, opt
->diffopt
.file
);
353 static unsigned int digits_in_number(unsigned int number
)
355 unsigned int i
= 10, result
= 1;
356 while (i
<= number
) {
363 void fmt_output_subject(struct strbuf
*filename
,
365 struct rev_info
*info
)
367 const char *suffix
= info
->patch_suffix
;
369 int start_len
= filename
->len
;
370 int max_len
= start_len
+ info
->patch_name_max
- (strlen(suffix
) + 1);
372 if (0 < info
->reroll_count
)
373 strbuf_addf(filename
, "v%d-", info
->reroll_count
);
374 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
376 if (max_len
< filename
->len
)
377 strbuf_setlen(filename
, max_len
);
378 strbuf_addstr(filename
, suffix
);
381 void fmt_output_commit(struct strbuf
*filename
,
382 struct commit
*commit
,
383 struct rev_info
*info
)
385 struct pretty_print_context ctx
= {0};
386 struct strbuf subject
= STRBUF_INIT
;
388 format_commit_message(commit
, "%f", &subject
, &ctx
);
389 fmt_output_subject(filename
, subject
.buf
, info
);
390 strbuf_release(&subject
);
393 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
395 if (opt
->total
> 0) {
396 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
398 *opt
->subject_prefix
? " " : "",
399 digits_in_number(opt
->total
),
400 opt
->nr
, opt
->total
);
401 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
402 strbuf_addf(sb
, "Subject: [%s] ",
403 opt
->subject_prefix
);
405 strbuf_addstr(sb
, "Subject: ");
409 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
410 const char **extra_headers_p
,
411 int *need_8bit_cte_p
,
414 const char *extra_headers
= opt
->extra_headers
;
415 const char *name
= oid_to_hex(opt
->zero_commit
?
416 &null_oid
: &commit
->object
.oid
);
418 *need_8bit_cte_p
= 0; /* unknown */
420 fprintf(opt
->diffopt
.file
, "From %s Mon Sep 17 00:00:00 2001\n", name
);
421 graph_show_oneline(opt
->graph
);
422 if (opt
->message_id
) {
423 fprintf(opt
->diffopt
.file
, "Message-Id: <%s>\n", opt
->message_id
);
424 graph_show_oneline(opt
->graph
);
426 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
428 n
= opt
->ref_message_ids
->nr
;
429 fprintf(opt
->diffopt
.file
, "In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
430 for (i
= 0; i
< n
; i
++)
431 fprintf(opt
->diffopt
.file
, "%s<%s>\n", (i
> 0 ? "\t" : "References: "),
432 opt
->ref_message_ids
->items
[i
].string
);
433 graph_show_oneline(opt
->graph
);
435 if (opt
->mime_boundary
&& maybe_multipart
) {
436 static struct strbuf subject_buffer
= STRBUF_INIT
;
437 static struct strbuf buffer
= STRBUF_INIT
;
438 struct strbuf filename
= STRBUF_INIT
;
439 *need_8bit_cte_p
= -1; /* NEVER */
441 strbuf_reset(&subject_buffer
);
442 strbuf_reset(&buffer
);
444 strbuf_addf(&subject_buffer
,
446 "MIME-Version: 1.0\n"
447 "Content-Type: multipart/mixed;"
448 " boundary=\"%s%s\"\n"
450 "This is a multi-part message in MIME "
453 "Content-Type: text/plain; "
454 "charset=UTF-8; format=fixed\n"
455 "Content-Transfer-Encoding: 8bit\n\n",
456 extra_headers
? extra_headers
: "",
457 mime_boundary_leader
, opt
->mime_boundary
,
458 mime_boundary_leader
, opt
->mime_boundary
);
459 extra_headers
= subject_buffer
.buf
;
461 if (opt
->numbered_files
)
462 strbuf_addf(&filename
, "%d", opt
->nr
);
464 fmt_output_commit(&filename
, commit
, opt
);
467 "Content-Type: text/x-patch;"
469 "Content-Transfer-Encoding: 8bit\n"
470 "Content-Disposition: %s;"
471 " filename=\"%s\"\n\n",
472 mime_boundary_leader
, opt
->mime_boundary
,
474 opt
->no_inline
? "attachment" : "inline",
476 opt
->diffopt
.stat_sep
= buffer
.buf
;
477 strbuf_release(&filename
);
479 *extra_headers_p
= extra_headers
;
482 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
484 const char *color
, *reset
, *eol
;
486 color
= diff_get_color_opt(&opt
->diffopt
,
487 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
488 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
490 eol
= strchrnul(bol
, '\n');
491 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
493 graph_show_oneline(opt
->graph
);
494 bol
= (*eol
) ? (eol
+ 1) : eol
;
498 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
500 struct strbuf payload
= STRBUF_INIT
;
501 struct strbuf signature
= STRBUF_INIT
;
502 struct signature_check sigc
= { 0 };
505 if (parse_signed_commit(commit
, &payload
, &signature
) <= 0)
508 status
= check_signature(payload
.buf
, payload
.len
, signature
.buf
,
509 signature
.len
, &sigc
);
510 if (status
&& !sigc
.gpg_output
)
511 show_sig_lines(opt
, status
, "No signature\n");
513 show_sig_lines(opt
, status
, sigc
.gpg_output
);
514 signature_check_clear(&sigc
);
517 strbuf_release(&payload
);
518 strbuf_release(&signature
);
521 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
524 const struct commit_list
*parent
;
526 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
527 if (oideq(&parent
->item
->object
.oid
, oid
))
534 static int is_common_merge(const struct commit
*commit
)
536 return (commit
->parents
537 && commit
->parents
->next
538 && !commit
->parents
->next
->next
);
541 static int show_one_mergetag(struct commit
*commit
,
542 struct commit_extra_header
*extra
,
545 struct rev_info
*opt
= (struct rev_info
*)data
;
546 struct object_id oid
;
548 struct strbuf verify_message
;
549 struct signature_check sigc
= { 0 };
553 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
554 type_name(OBJ_TAG
), &oid
);
555 tag
= lookup_tag(the_repository
, &oid
);
557 return -1; /* error message already given */
559 strbuf_init(&verify_message
, 256);
560 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
561 strbuf_addstr(&verify_message
, "malformed mergetag\n");
562 else if (is_common_merge(commit
) &&
563 oideq(&tag
->tagged
->oid
,
564 &commit
->parents
->next
->item
->object
.oid
))
565 strbuf_addf(&verify_message
,
566 "merged tag '%s'\n", tag
->tag
);
567 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
568 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
569 tag
->tag
, oid_to_hex(&tag
->tagged
->oid
));
571 strbuf_addf(&verify_message
,
572 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
574 payload_size
= parse_signature(extra
->value
, extra
->len
);
576 if (extra
->len
> payload_size
) {
577 /* could have a good signature */
578 status
= check_signature(extra
->value
, payload_size
,
579 extra
->value
+ payload_size
,
580 extra
->len
- payload_size
, &sigc
);
582 strbuf_addstr(&verify_message
, sigc
.gpg_output
);
584 strbuf_addstr(&verify_message
, "No signature\n");
585 signature_check_clear(&sigc
);
586 /* otherwise we couldn't verify, which is shown as bad */
589 show_sig_lines(opt
, status
, verify_message
.buf
);
590 strbuf_release(&verify_message
);
594 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
596 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
599 static void next_commentary_block(struct rev_info
*opt
, struct strbuf
*sb
)
601 const char *x
= opt
->shown_dashes
? "\n" : "---\n";
603 strbuf_addstr(sb
, x
);
605 fputs(x
, opt
->diffopt
.file
);
606 opt
->shown_dashes
= 1;
609 void show_log(struct rev_info
*opt
)
611 struct strbuf msgbuf
= STRBUF_INIT
;
612 struct log_info
*log
= opt
->loginfo
;
613 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
614 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: the_hash_algo
->hexsz
;
615 const char *extra_headers
= opt
->extra_headers
;
616 struct pretty_print_context ctx
= {0};
619 if (!opt
->verbose_header
) {
620 graph_show_commit(opt
->graph
);
623 put_revision_mark(opt
, commit
);
624 fputs(find_unique_abbrev(&commit
->object
.oid
, abbrev_commit
), opt
->diffopt
.file
);
625 if (opt
->print_parents
)
626 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
627 if (opt
->children
.name
)
628 show_children(opt
, commit
, abbrev_commit
);
629 show_decorations(opt
, commit
);
630 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
631 putc('\n', opt
->diffopt
.file
);
632 graph_show_remainder(opt
->graph
);
634 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
639 * If use_terminator is set, we already handled any record termination
640 * at the end of the last record.
641 * Otherwise, add a diffopt.line_termination character before all
642 * entries but the first. (IOW, as a separator between entries)
644 if (opt
->shown_one
&& !opt
->use_terminator
) {
646 * If entries are separated by a newline, the output
647 * should look human-readable. If the last entry ended
648 * with a newline, print the graph output before this
649 * newline. Otherwise it will end up as a completely blank
650 * line and will look like a gap in the graph.
652 * If the entry separator is not a newline, the output is
653 * primarily intended for programmatic consumption, and we
654 * never want the extra graph output before the entry
657 if (opt
->diffopt
.line_termination
== '\n' &&
658 !opt
->missing_newline
)
659 graph_show_padding(opt
->graph
);
660 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
665 * If the history graph was requested,
666 * print the graph, up to this commit's line
668 graph_show_commit(opt
->graph
);
671 * Print header line of header..
674 if (cmit_fmt_is_mail(opt
->commit_format
)) {
675 log_write_email_headers(opt
, commit
, &extra_headers
,
676 &ctx
.need_8bit_cte
, 1);
678 ctx
.print_email_subject
= 1;
679 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
680 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
681 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
682 fputs("commit ", opt
->diffopt
.file
);
685 put_revision_mark(opt
, commit
);
686 fputs(find_unique_abbrev(&commit
->object
.oid
,
689 if (opt
->print_parents
)
690 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
691 if (opt
->children
.name
)
692 show_children(opt
, commit
, abbrev_commit
);
694 fprintf(opt
->diffopt
.file
, " (from %s)",
695 find_unique_abbrev(&parent
->object
.oid
, abbrev_commit
));
696 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
697 show_decorations(opt
, commit
);
698 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
699 putc(' ', opt
->diffopt
.file
);
701 putc('\n', opt
->diffopt
.file
);
702 graph_show_oneline(opt
->graph
);
704 if (opt
->reflog_info
) {
706 * setup_revisions() ensures that opt->reflog_info
707 * and opt->graph cannot both be set,
708 * so we don't need to worry about printing the
711 show_reflog_message(opt
->reflog_info
,
712 opt
->commit_format
== CMIT_FMT_ONELINE
,
714 opt
->date_mode_explicit
);
715 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
720 if (opt
->show_signature
) {
721 show_signature(opt
, commit
);
722 show_mergetag(opt
, commit
);
725 if (opt
->show_notes
) {
727 struct strbuf notebuf
= STRBUF_INIT
;
729 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
730 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
731 get_log_output_encoding(), raw
);
732 ctx
.notes_message
= strbuf_detach(¬ebuf
, NULL
);
736 * And then the pretty-printed message itself
738 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
740 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT
));
741 ctx
.date_mode
= opt
->date_mode
;
742 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
743 ctx
.abbrev
= opt
->diffopt
.abbrev
;
744 ctx
.after_subject
= extra_headers
;
745 ctx
.preserve_subject
= opt
->preserve_subject
;
746 ctx
.encode_email_headers
= opt
->encode_email_headers
;
747 ctx
.reflog_info
= opt
->reflog_info
;
748 ctx
.fmt
= opt
->commit_format
;
749 ctx
.mailmap
= opt
->mailmap
;
750 ctx
.color
= opt
->diffopt
.use_color
;
751 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
752 ctx
.output_encoding
= get_log_output_encoding();
754 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
755 ctx
.from_ident
= &opt
->from_ident
;
757 ctx
.graph_width
= graph_width(opt
->graph
);
758 pretty_print_commit(&ctx
, commit
, &msgbuf
);
760 if (opt
->add_signoff
)
761 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
763 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
764 ctx
.notes_message
&& *ctx
.notes_message
) {
765 if (cmit_fmt_is_mail(ctx
.fmt
))
766 next_commentary_block(opt
, &msgbuf
);
767 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
770 if (opt
->show_log_size
) {
771 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
772 graph_show_oneline(opt
->graph
);
776 * Set opt->missing_newline if msgbuf doesn't
777 * end in a newline (including if it is empty)
779 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
780 opt
->missing_newline
= 1;
782 opt
->missing_newline
= 0;
784 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
785 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
786 if (!opt
->missing_newline
)
787 graph_show_padding(opt
->graph
);
788 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
791 strbuf_release(&msgbuf
);
792 free(ctx
.notes_message
);
794 if (cmit_fmt_is_mail(ctx
.fmt
) && opt
->idiff_oid1
) {
795 struct diff_queue_struct dq
;
797 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
798 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
800 next_commentary_block(opt
, NULL
);
801 fprintf_ln(opt
->diffopt
.file
, "%s", opt
->idiff_title
);
802 show_interdiff(opt
->idiff_oid1
, opt
->idiff_oid2
, 2,
805 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
808 if (cmit_fmt_is_mail(ctx
.fmt
) && opt
->rdiff1
) {
809 struct diff_queue_struct dq
;
810 struct diff_options opts
;
812 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
813 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
815 next_commentary_block(opt
, NULL
);
816 fprintf_ln(opt
->diffopt
.file
, "%s", opt
->rdiff_title
);
818 * Pass minimum required diff-options to range-diff; others
819 * can be added later if deemed desirable.
822 opts
.file
= opt
->diffopt
.file
;
823 opts
.use_color
= opt
->diffopt
.use_color
;
824 diff_setup_done(&opts
);
825 show_range_diff(opt
->rdiff1
, opt
->rdiff2
,
826 opt
->creation_factor
, 1, &opts
, NULL
);
828 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
832 int log_tree_diff_flush(struct rev_info
*opt
)
834 opt
->shown_dashes
= 0;
835 diffcore_std(&opt
->diffopt
);
837 if (diff_queue_is_empty()) {
838 int saved_fmt
= opt
->diffopt
.output_format
;
839 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
840 diff_flush(&opt
->diffopt
);
841 opt
->diffopt
.output_format
= saved_fmt
;
845 if (opt
->loginfo
&& !opt
->no_commit_id
) {
847 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
848 opt
->verbose_header
&&
849 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
850 !commit_format_is_empty(opt
->commit_format
)) {
852 * When showing a verbose header (i.e. log message),
853 * and not in --pretty=oneline format, we would want
854 * an extra newline between the end of log and the
855 * diff/diffstat output for readability.
857 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
858 if (opt
->diffopt
.output_prefix
) {
859 struct strbuf
*msg
= NULL
;
860 msg
= opt
->diffopt
.output_prefix(&opt
->diffopt
,
861 opt
->diffopt
.output_prefix_data
);
862 fwrite(msg
->buf
, msg
->len
, 1, opt
->diffopt
.file
);
866 * We may have shown three-dashes line early
867 * between generated commentary (notes, etc.)
868 * and the log message, in which case we only
869 * want a blank line after the commentary
870 * without (an extra) three-dashes line.
871 * Otherwise, we show the three-dashes line if
872 * we are showing the patch with diffstat, but
873 * in that case, there is no extra blank line
874 * after the three-dashes line.
876 if (!opt
->shown_dashes
&&
877 (pch
& opt
->diffopt
.output_format
) == pch
)
878 fprintf(opt
->diffopt
.file
, "---");
879 putc('\n', opt
->diffopt
.file
);
882 diff_flush(&opt
->diffopt
);
886 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
888 diff_tree_combined_merge(commit
, opt
);
889 return !opt
->loginfo
;
893 * Show the diff of a commit.
895 * Return true if we printed any log info messages
897 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
900 struct commit_list
*parents
;
901 struct object_id
*oid
;
903 int all_need_diff
= opt
->diff
|| opt
->diffopt
.flags
.exit_with_status
;
905 if (!all_need_diff
&& !opt
->merges_need_diff
)
908 parse_commit_or_die(commit
);
909 oid
= get_commit_tree_oid(commit
);
911 parents
= get_saved_parents(opt
, commit
);
912 is_merge
= parents
&& parents
->next
;
913 if (!is_merge
&& !all_need_diff
)
918 if (opt
->show_root_diff
) {
919 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
920 log_tree_diff_flush(opt
);
922 return !opt
->loginfo
;
926 if (opt
->combine_merges
)
927 return do_diff_combined(opt
, commit
);
928 if (opt
->separate_merges
) {
929 if (!opt
->first_parent_merges
) {
930 /* Show parent info for multiple diffs */
931 log
->parent
= parents
->item
;
939 struct commit
*parent
= parents
->item
;
941 parse_commit_or_die(parent
);
942 diff_tree_oid(get_commit_tree_oid(parent
),
943 oid
, "", &opt
->diffopt
);
944 log_tree_diff_flush(opt
);
946 showed_log
|= !opt
->loginfo
;
948 /* Set up the log info for the next parent, if any.. */
949 parents
= parents
->next
;
950 if (!parents
|| opt
->first_parent_merges
)
952 log
->parent
= parents
->item
;
958 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
961 int shown
, close_file
= opt
->diffopt
.close_file
;
966 opt
->diffopt
.close_file
= 0;
968 if (opt
->line_level_traverse
)
969 return line_log_print(opt
, commit
);
971 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
972 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
973 shown
= log_tree_diff(opt
, commit
, &log
);
974 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
979 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
980 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
982 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
984 fclose(opt
->diffopt
.file
);