1 #include "git-compat-util.h"
2 #include "commit-reach.h"
5 #include "environment.h"
7 #include "object-name.h"
8 #include "object-store-ll.h"
9 #include "repository.h"
10 #include "tmp-objdir.h"
15 #include "merge-ort.h"
16 #include "reflog-walk.h"
18 #include "replace-object.h"
20 #include "string-list.h"
22 #include "gpg-interface.h"
23 #include "sequencer.h"
26 #include "range-diff.h"
29 #include "wildmatch.h"
30 #include "write-or-die.h"
32 static struct decoration name_decoration
= { "object names" };
33 static int decoration_loaded
;
34 static int decoration_flags
;
36 static char decoration_colors
[][COLOR_MAXLEN
] = {
38 GIT_COLOR_BOLD_GREEN
, /* REF_LOCAL */
39 GIT_COLOR_BOLD_RED
, /* REF_REMOTE */
40 GIT_COLOR_BOLD_YELLOW
, /* REF_TAG */
41 GIT_COLOR_BOLD_MAGENTA
, /* REF_STASH */
42 GIT_COLOR_BOLD_CYAN
, /* REF_HEAD */
43 GIT_COLOR_BOLD_BLUE
, /* GRAFTED */
46 static const char *color_decorate_slots
[] = {
47 [DECORATION_REF_LOCAL
] = "branch",
48 [DECORATION_REF_REMOTE
] = "remoteBranch",
49 [DECORATION_REF_TAG
] = "tag",
50 [DECORATION_REF_STASH
] = "stash",
51 [DECORATION_REF_HEAD
] = "HEAD",
52 [DECORATION_GRAFTED
] = "grafted",
55 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
57 if (want_color(decorate_use_color
))
58 return decoration_colors
[ix
];
62 define_list_config_array(color_decorate_slots
);
64 int parse_decorate_color_config(const char *var
, const char *slot_name
, const char *value
)
66 int slot
= LOOKUP_CONFIG(color_decorate_slots
, slot_name
);
70 return config_error_nonbool(var
);
71 return color_parse(value
, decoration_colors
[slot
]);
75 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
76 * for showing the commit sha1, use the same check for --decorate
78 #define decorate_get_color_opt(o, ix) \
79 decorate_get_color((o)->use_color, ix)
81 void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
83 struct name_decoration
*res
;
84 FLEX_ALLOC_STR(res
, name
, name
);
86 res
->next
= add_decoration(&name_decoration
, obj
, res
);
89 const struct name_decoration
*get_name_decoration(const struct object
*obj
)
91 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
92 return lookup_decoration(&name_decoration
, obj
);
95 static int match_ref_pattern(const char *refname
,
96 const struct string_list_item
*item
)
100 if (!wildmatch(item
->string
, refname
, 0))
104 if (skip_prefix(refname
, item
->string
, &rest
) &&
105 (!*rest
|| *rest
== '/'))
111 static int ref_filter_match(const char *refname
,
112 const struct decoration_filter
*filter
)
114 struct string_list_item
*item
;
115 const struct string_list
*exclude_patterns
= filter
->exclude_ref_pattern
;
116 const struct string_list
*include_patterns
= filter
->include_ref_pattern
;
117 const struct string_list
*exclude_patterns_config
=
118 filter
->exclude_ref_config_pattern
;
120 if (exclude_patterns
&& exclude_patterns
->nr
) {
121 for_each_string_list_item(item
, exclude_patterns
) {
122 if (match_ref_pattern(refname
, item
))
127 if (include_patterns
&& include_patterns
->nr
) {
128 for_each_string_list_item(item
, include_patterns
) {
129 if (match_ref_pattern(refname
, item
))
135 if (exclude_patterns_config
&& exclude_patterns_config
->nr
) {
136 for_each_string_list_item(item
, exclude_patterns_config
) {
137 if (match_ref_pattern(refname
, item
))
145 static int add_ref_decoration(const char *refname
, const struct object_id
*oid
,
151 enum object_type objtype
;
152 enum decoration_type deco_type
= DECORATION_NONE
;
153 struct decoration_filter
*filter
= (struct decoration_filter
*)cb_data
;
154 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
156 if (filter
&& !ref_filter_match(refname
, filter
))
159 if (starts_with(refname
, git_replace_ref_base
)) {
160 struct object_id original_oid
;
161 if (!replace_refs_enabled(the_repository
))
163 if (get_oid_hex(refname
+ strlen(git_replace_ref_base
),
165 warning("invalid replace ref %s", refname
);
168 obj
= parse_object(the_repository
, &original_oid
);
170 add_name_decoration(DECORATION_GRAFTED
, "replaced", obj
);
174 objtype
= oid_object_info(the_repository
, oid
, NULL
);
177 obj
= lookup_object_by_type(the_repository
, oid
, objtype
);
179 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
180 struct ref_namespace_info
*info
= &ref_namespace
[i
];
182 if (!info
->decoration
)
185 if (!strcmp(refname
, info
->ref
)) {
186 deco_type
= info
->decoration
;
189 } else if (starts_with(refname
, info
->ref
)) {
190 deco_type
= info
->decoration
;
195 add_name_decoration(deco_type
, refname
, obj
);
196 while (obj
->type
== OBJ_TAG
) {
198 parse_object(the_repository
, &obj
->oid
);
199 obj
= ((struct tag
*)obj
)->tagged
;
202 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
207 static int add_graft_decoration(const struct commit_graft
*graft
,
208 void *cb_data UNUSED
)
210 struct commit
*commit
= lookup_commit(the_repository
, &graft
->oid
);
213 add_name_decoration(DECORATION_GRAFTED
, "grafted", &commit
->object
);
217 void load_ref_decorations(struct decoration_filter
*filter
, int flags
)
219 if (!decoration_loaded
) {
221 struct string_list_item
*item
;
222 for_each_string_list_item(item
, filter
->exclude_ref_pattern
) {
223 normalize_glob_ref(item
, NULL
, item
->string
);
225 for_each_string_list_item(item
, filter
->include_ref_pattern
) {
226 normalize_glob_ref(item
, NULL
, item
->string
);
228 for_each_string_list_item(item
, filter
->exclude_ref_config_pattern
) {
229 normalize_glob_ref(item
, NULL
, item
->string
);
232 decoration_loaded
= 1;
233 decoration_flags
= flags
;
234 for_each_ref(add_ref_decoration
, filter
);
235 head_ref(add_ref_decoration
, filter
);
236 for_each_commit_graft(add_graft_decoration
, filter
);
240 static void show_parents(struct commit
*commit
, int abbrev
, FILE *file
)
242 struct commit_list
*p
;
243 for (p
= commit
->parents
; p
; p
= p
->next
) {
244 struct commit
*parent
= p
->item
;
246 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev
));
250 static void show_children(struct rev_info
*opt
, struct commit
*commit
, int abbrev
)
252 struct commit_list
*p
= lookup_decoration(&opt
->children
, &commit
->object
);
253 for ( ; p
; p
= p
->next
) {
254 fprintf(opt
->diffopt
.file
, " %s",
255 repo_find_unique_abbrev(the_repository
, &p
->item
->object
.oid
, abbrev
));
260 * Do we have HEAD in the output, and also the branch it points at?
261 * If so, find that decoration entry for that current branch.
263 static const struct name_decoration
*current_pointed_by_HEAD(const struct name_decoration
*decoration
)
265 const struct name_decoration
*list
, *head
= NULL
;
266 const char *branch_name
= NULL
;
269 /* First find HEAD */
270 for (list
= decoration
; list
; list
= list
->next
)
271 if (list
->type
== DECORATION_REF_HEAD
) {
278 /* Now resolve and find the matching current branch */
279 branch_name
= resolve_ref_unsafe("HEAD", 0, NULL
, &rru_flags
);
280 if (!branch_name
|| !(rru_flags
& REF_ISSYMREF
))
283 if (!starts_with(branch_name
, "refs/"))
286 /* OK, do we have that ref in the list? */
287 for (list
= decoration
; list
; list
= list
->next
)
288 if ((list
->type
== DECORATION_REF_LOCAL
) &&
289 !strcmp(branch_name
, list
->name
)) {
296 static void show_name(struct strbuf
*sb
, const struct name_decoration
*decoration
)
298 if (decoration_flags
== DECORATE_SHORT_REFS
)
299 strbuf_addstr(sb
, prettify_refname(decoration
->name
));
301 strbuf_addstr(sb
, decoration
->name
);
305 * The caller makes sure there is no funny color before calling.
306 * format_decorations ensures the same after return.
308 void format_decorations(struct strbuf
*sb
,
309 const struct commit
*commit
,
311 const struct decoration_options
*opts
)
313 const struct name_decoration
*decoration
;
314 const struct name_decoration
*current_and_HEAD
;
315 const char *color_commit
=
316 diff_get_color(use_color
, DIFF_COMMIT
);
317 const char *color_reset
=
318 decorate_get_color(use_color
, DECORATION_NONE
);
320 const char *prefix
= " (";
321 const char *suffix
= ")";
322 const char *separator
= ", ";
324 decoration
= get_name_decoration(&commit
->object
);
330 prefix
= opts
->prefix
;
332 suffix
= opts
->suffix
;
334 separator
= opts
->separator
;
337 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
340 * When both current and HEAD are there, only
341 * show HEAD->current where HEAD would have
342 * appeared, skipping the entry for current.
344 if (decoration
!= current_and_HEAD
) {
345 strbuf_addstr(sb
, color_commit
);
346 strbuf_addstr(sb
, prefix
);
347 strbuf_addstr(sb
, color_reset
);
348 strbuf_addstr(sb
, decorate_get_color(use_color
, decoration
->type
));
349 if (decoration
->type
== DECORATION_REF_TAG
)
350 strbuf_addstr(sb
, "tag: ");
352 show_name(sb
, decoration
);
354 if (current_and_HEAD
&&
355 decoration
->type
== DECORATION_REF_HEAD
) {
356 strbuf_addstr(sb
, " -> ");
357 strbuf_addstr(sb
, color_reset
);
358 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
359 show_name(sb
, current_and_HEAD
);
361 strbuf_addstr(sb
, color_reset
);
365 decoration
= decoration
->next
;
367 strbuf_addstr(sb
, color_commit
);
368 strbuf_addstr(sb
, suffix
);
369 strbuf_addstr(sb
, color_reset
);
372 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
374 struct strbuf sb
= STRBUF_INIT
;
377 char **slot
= revision_sources_peek(opt
->sources
, commit
);
380 fprintf(opt
->diffopt
.file
, "\t%s", *slot
);
382 if (!opt
->show_decorations
)
384 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
, NULL
);
385 fputs(sb
.buf
, opt
->diffopt
.file
);
389 static unsigned int digits_in_number(unsigned int number
)
391 unsigned int i
= 10, result
= 1;
392 while (i
<= number
) {
399 void fmt_output_subject(struct strbuf
*filename
,
401 struct rev_info
*info
)
403 const char *suffix
= info
->patch_suffix
;
405 int start_len
= filename
->len
;
406 int max_len
= start_len
+ info
->patch_name_max
- (strlen(suffix
) + 1);
408 if (info
->reroll_count
) {
409 struct strbuf temp
= STRBUF_INIT
;
411 strbuf_addf(&temp
, "v%s", info
->reroll_count
);
412 format_sanitized_subject(filename
, temp
.buf
, temp
.len
);
413 strbuf_addstr(filename
, "-");
414 strbuf_release(&temp
);
416 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
418 if (max_len
< filename
->len
)
419 strbuf_setlen(filename
, max_len
);
420 strbuf_addstr(filename
, suffix
);
423 void fmt_output_commit(struct strbuf
*filename
,
424 struct commit
*commit
,
425 struct rev_info
*info
)
427 struct pretty_print_context ctx
= {0};
428 struct strbuf subject
= STRBUF_INIT
;
430 repo_format_commit_message(the_repository
, commit
, "%f", &subject
,
432 fmt_output_subject(filename
, subject
.buf
, info
);
433 strbuf_release(&subject
);
436 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
438 if (opt
->total
> 0) {
439 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
441 *opt
->subject_prefix
? " " : "",
442 digits_in_number(opt
->total
),
443 opt
->nr
, opt
->total
);
444 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
445 strbuf_addf(sb
, "Subject: [%s] ",
446 opt
->subject_prefix
);
448 strbuf_addstr(sb
, "Subject: ");
452 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
453 const char **extra_headers_p
,
454 int *need_8bit_cte_p
,
457 const char *extra_headers
= opt
->extra_headers
;
458 const char *name
= oid_to_hex(opt
->zero_commit
?
459 null_oid() : &commit
->object
.oid
);
461 *need_8bit_cte_p
= 0; /* unknown */
463 fprintf(opt
->diffopt
.file
, "From %s Mon Sep 17 00:00:00 2001\n", name
);
464 graph_show_oneline(opt
->graph
);
465 if (opt
->message_id
) {
466 fprintf(opt
->diffopt
.file
, "Message-ID: <%s>\n", opt
->message_id
);
467 graph_show_oneline(opt
->graph
);
469 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
471 n
= opt
->ref_message_ids
->nr
;
472 fprintf(opt
->diffopt
.file
, "In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
473 for (i
= 0; i
< n
; i
++)
474 fprintf(opt
->diffopt
.file
, "%s<%s>\n", (i
> 0 ? "\t" : "References: "),
475 opt
->ref_message_ids
->items
[i
].string
);
476 graph_show_oneline(opt
->graph
);
478 if (opt
->mime_boundary
&& maybe_multipart
) {
479 static struct strbuf subject_buffer
= STRBUF_INIT
;
480 static struct strbuf buffer
= STRBUF_INIT
;
481 struct strbuf filename
= STRBUF_INIT
;
482 *need_8bit_cte_p
= -1; /* NEVER */
484 strbuf_reset(&subject_buffer
);
485 strbuf_reset(&buffer
);
487 strbuf_addf(&subject_buffer
,
489 "MIME-Version: 1.0\n"
490 "Content-Type: multipart/mixed;"
491 " boundary=\"%s%s\"\n"
493 "This is a multi-part message in MIME "
496 "Content-Type: text/plain; "
497 "charset=UTF-8; format=fixed\n"
498 "Content-Transfer-Encoding: 8bit\n\n",
499 extra_headers
? extra_headers
: "",
500 mime_boundary_leader
, opt
->mime_boundary
,
501 mime_boundary_leader
, opt
->mime_boundary
);
502 extra_headers
= subject_buffer
.buf
;
504 if (opt
->numbered_files
)
505 strbuf_addf(&filename
, "%d", opt
->nr
);
507 fmt_output_commit(&filename
, commit
, opt
);
510 "Content-Type: text/x-patch;"
512 "Content-Transfer-Encoding: 8bit\n"
513 "Content-Disposition: %s;"
514 " filename=\"%s\"\n\n",
515 mime_boundary_leader
, opt
->mime_boundary
,
517 opt
->no_inline
? "attachment" : "inline",
519 opt
->diffopt
.stat_sep
= buffer
.buf
;
520 strbuf_release(&filename
);
522 *extra_headers_p
= extra_headers
;
525 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
527 const char *color
, *reset
, *eol
;
529 color
= diff_get_color_opt(&opt
->diffopt
,
530 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
531 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
533 eol
= strchrnul(bol
, '\n');
534 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
536 graph_show_oneline(opt
->graph
);
537 bol
= (*eol
) ? (eol
+ 1) : eol
;
541 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
543 struct strbuf payload
= STRBUF_INIT
;
544 struct strbuf signature
= STRBUF_INIT
;
545 struct signature_check sigc
= { 0 };
548 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
551 sigc
.payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
552 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
553 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
554 if (status
&& !sigc
.output
)
555 show_sig_lines(opt
, status
, "No signature\n");
557 show_sig_lines(opt
, status
, sigc
.output
);
558 signature_check_clear(&sigc
);
561 strbuf_release(&payload
);
562 strbuf_release(&signature
);
565 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
568 const struct commit_list
*parent
;
570 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
571 if (oideq(&parent
->item
->object
.oid
, oid
))
578 static int is_common_merge(const struct commit
*commit
)
580 return (commit
->parents
581 && commit
->parents
->next
582 && !commit
->parents
->next
->next
);
585 static int show_one_mergetag(struct commit
*commit
,
586 struct commit_extra_header
*extra
,
589 struct rev_info
*opt
= (struct rev_info
*)data
;
590 struct object_id oid
;
592 struct strbuf verify_message
;
593 struct signature_check sigc
= { 0 };
595 struct strbuf payload
= STRBUF_INIT
;
596 struct strbuf signature
= STRBUF_INIT
;
598 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
600 tag
= lookup_tag(the_repository
, &oid
);
602 return -1; /* error message already given */
604 strbuf_init(&verify_message
, 256);
605 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
606 strbuf_addstr(&verify_message
, "malformed mergetag\n");
607 else if (is_common_merge(commit
) &&
608 oideq(&tag
->tagged
->oid
,
609 &commit
->parents
->next
->item
->object
.oid
))
610 strbuf_addf(&verify_message
,
611 "merged tag '%s'\n", tag
->tag
);
612 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
613 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
614 tag
->tag
, oid_to_hex(&tag
->tagged
->oid
));
616 strbuf_addf(&verify_message
,
617 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
620 if (parse_signature(extra
->value
, extra
->len
, &payload
, &signature
)) {
621 /* could have a good signature */
622 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
623 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
624 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
626 strbuf_addstr(&verify_message
, sigc
.output
);
628 strbuf_addstr(&verify_message
, "No signature\n");
629 signature_check_clear(&sigc
);
630 /* otherwise we couldn't verify, which is shown as bad */
633 show_sig_lines(opt
, status
, verify_message
.buf
);
634 strbuf_release(&verify_message
);
635 strbuf_release(&payload
);
636 strbuf_release(&signature
);
640 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
642 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
645 static void next_commentary_block(struct rev_info
*opt
, struct strbuf
*sb
)
647 const char *x
= opt
->shown_dashes
? "\n" : "---\n";
649 strbuf_addstr(sb
, x
);
651 fputs(x
, opt
->diffopt
.file
);
652 opt
->shown_dashes
= 1;
655 void show_log(struct rev_info
*opt
)
657 struct strbuf msgbuf
= STRBUF_INIT
;
658 struct log_info
*log
= opt
->loginfo
;
659 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
660 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: the_hash_algo
->hexsz
;
661 const char *extra_headers
= opt
->extra_headers
;
662 struct pretty_print_context ctx
= {0};
665 if (!opt
->verbose_header
) {
666 graph_show_commit(opt
->graph
);
669 put_revision_mark(opt
, commit
);
670 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev_commit
),
672 if (opt
->print_parents
)
673 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
674 if (opt
->children
.name
)
675 show_children(opt
, commit
, abbrev_commit
);
676 show_decorations(opt
, commit
);
677 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
678 putc('\n', opt
->diffopt
.file
);
679 graph_show_remainder(opt
->graph
);
681 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
686 * If use_terminator is set, we already handled any record termination
687 * at the end of the last record.
688 * Otherwise, add a diffopt.line_termination character before all
689 * entries but the first. (IOW, as a separator between entries)
691 if (opt
->shown_one
&& !opt
->use_terminator
) {
693 * If entries are separated by a newline, the output
694 * should look human-readable. If the last entry ended
695 * with a newline, print the graph output before this
696 * newline. Otherwise it will end up as a completely blank
697 * line and will look like a gap in the graph.
699 * If the entry separator is not a newline, the output is
700 * primarily intended for programmatic consumption, and we
701 * never want the extra graph output before the entry
704 if (opt
->diffopt
.line_termination
== '\n' &&
705 !opt
->missing_newline
)
706 graph_show_padding(opt
->graph
);
707 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
712 * If the history graph was requested,
713 * print the graph, up to this commit's line
715 graph_show_commit(opt
->graph
);
718 * Print header line of header..
721 if (cmit_fmt_is_mail(opt
->commit_format
)) {
722 log_write_email_headers(opt
, commit
, &extra_headers
,
723 &ctx
.need_8bit_cte
, 1);
725 ctx
.print_email_subject
= 1;
726 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
727 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
728 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
729 fputs("commit ", opt
->diffopt
.file
);
732 put_revision_mark(opt
, commit
);
733 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
,
736 if (opt
->print_parents
)
737 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
738 if (opt
->children
.name
)
739 show_children(opt
, commit
, abbrev_commit
);
741 fprintf(opt
->diffopt
.file
, " (from %s)",
742 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev_commit
));
743 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
744 show_decorations(opt
, commit
);
745 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
746 putc(' ', opt
->diffopt
.file
);
748 putc('\n', opt
->diffopt
.file
);
749 graph_show_oneline(opt
->graph
);
751 if (opt
->reflog_info
) {
753 * setup_revisions() ensures that opt->reflog_info
754 * and opt->graph cannot both be set,
755 * so we don't need to worry about printing the
758 show_reflog_message(opt
->reflog_info
,
759 opt
->commit_format
== CMIT_FMT_ONELINE
,
761 opt
->date_mode_explicit
);
762 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
767 if (opt
->show_signature
) {
768 show_signature(opt
, commit
);
769 show_mergetag(opt
, commit
);
772 if (opt
->show_notes
) {
774 struct strbuf notebuf
= STRBUF_INIT
;
776 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
777 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
778 get_log_output_encoding(), raw
);
779 ctx
.notes_message
= strbuf_detach(¬ebuf
, NULL
);
783 * And then the pretty-printed message itself
785 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
787 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT
));
788 ctx
.date_mode
= opt
->date_mode
;
789 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
790 ctx
.abbrev
= opt
->diffopt
.abbrev
;
791 ctx
.after_subject
= extra_headers
;
792 ctx
.preserve_subject
= opt
->preserve_subject
;
793 ctx
.encode_email_headers
= opt
->encode_email_headers
;
794 ctx
.reflog_info
= opt
->reflog_info
;
795 ctx
.fmt
= opt
->commit_format
;
796 ctx
.mailmap
= opt
->mailmap
;
797 ctx
.color
= opt
->diffopt
.use_color
;
798 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
799 ctx
.output_encoding
= get_log_output_encoding();
801 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
802 ctx
.from_ident
= &opt
->from_ident
;
804 ctx
.graph_width
= graph_width(opt
->graph
);
805 pretty_print_commit(&ctx
, commit
, &msgbuf
);
807 if (opt
->add_signoff
)
808 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
810 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
811 ctx
.notes_message
&& *ctx
.notes_message
) {
812 if (cmit_fmt_is_mail(ctx
.fmt
))
813 next_commentary_block(opt
, &msgbuf
);
814 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
817 if (opt
->show_log_size
) {
818 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
819 graph_show_oneline(opt
->graph
);
823 * Set opt->missing_newline if msgbuf doesn't
824 * end in a newline (including if it is empty)
826 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
827 opt
->missing_newline
= 1;
829 opt
->missing_newline
= 0;
831 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
832 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
833 if (!opt
->missing_newline
)
834 graph_show_padding(opt
->graph
);
835 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
838 strbuf_release(&msgbuf
);
839 free(ctx
.notes_message
);
841 if (cmit_fmt_is_mail(ctx
.fmt
) && opt
->idiff_oid1
) {
842 struct diff_queue_struct dq
;
844 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
845 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
847 next_commentary_block(opt
, NULL
);
848 fprintf_ln(opt
->diffopt
.file
, "%s", opt
->idiff_title
);
849 show_interdiff(opt
->idiff_oid1
, opt
->idiff_oid2
, 2,
852 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
855 if (cmit_fmt_is_mail(ctx
.fmt
) && opt
->rdiff1
) {
856 struct diff_queue_struct dq
;
857 struct diff_options opts
;
858 struct range_diff_options range_diff_opts
= {
859 .creation_factor
= opt
->creation_factor
,
864 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
865 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
867 next_commentary_block(opt
, NULL
);
868 fprintf_ln(opt
->diffopt
.file
, "%s", opt
->rdiff_title
);
870 * Pass minimum required diff-options to range-diff; others
871 * can be added later if deemed desirable.
873 repo_diff_setup(the_repository
, &opts
);
874 opts
.file
= opt
->diffopt
.file
;
875 opts
.use_color
= opt
->diffopt
.use_color
;
876 diff_setup_done(&opts
);
877 show_range_diff(opt
->rdiff1
, opt
->rdiff2
, &range_diff_opts
);
879 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
883 int log_tree_diff_flush(struct rev_info
*opt
)
885 opt
->shown_dashes
= 0;
886 diffcore_std(&opt
->diffopt
);
888 if (diff_queue_is_empty(&opt
->diffopt
)) {
889 int saved_fmt
= opt
->diffopt
.output_format
;
890 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
891 diff_flush(&opt
->diffopt
);
892 opt
->diffopt
.output_format
= saved_fmt
;
896 if (opt
->loginfo
&& !opt
->no_commit_id
) {
898 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
899 opt
->verbose_header
&&
900 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
901 !commit_format_is_empty(opt
->commit_format
)) {
903 * When showing a verbose header (i.e. log message),
904 * and not in --pretty=oneline format, we would want
905 * an extra newline between the end of log and the
906 * diff/diffstat output for readability.
908 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
909 if (opt
->diffopt
.output_prefix
) {
910 struct strbuf
*msg
= NULL
;
911 msg
= opt
->diffopt
.output_prefix(&opt
->diffopt
,
912 opt
->diffopt
.output_prefix_data
);
913 fwrite(msg
->buf
, msg
->len
, 1, opt
->diffopt
.file
);
917 * We may have shown three-dashes line early
918 * between generated commentary (notes, etc.)
919 * and the log message, in which case we only
920 * want a blank line after the commentary
921 * without (an extra) three-dashes line.
922 * Otherwise, we show the three-dashes line if
923 * we are showing the patch with diffstat, but
924 * in that case, there is no extra blank line
925 * after the three-dashes line.
927 if (!opt
->shown_dashes
&&
928 (pch
& opt
->diffopt
.output_format
) == pch
)
929 fprintf(opt
->diffopt
.file
, "---");
930 putc('\n', opt
->diffopt
.file
);
933 diff_flush(&opt
->diffopt
);
937 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
939 diff_tree_combined_merge(commit
, opt
);
940 return !opt
->loginfo
;
943 static void setup_additional_headers(struct diff_options
*o
,
944 struct strmap
*all_headers
)
946 struct hashmap_iter iter
;
947 struct strmap_entry
*entry
;
950 * Make o->additional_path_headers contain the subset of all_headers
951 * that match o->pathspec. If there aren't any that match o->pathspec,
952 * then make o->additional_path_headers be NULL.
955 if (!o
->pathspec
.nr
) {
956 o
->additional_path_headers
= all_headers
;
960 o
->additional_path_headers
= xmalloc(sizeof(struct strmap
));
961 strmap_init_with_options(o
->additional_path_headers
, NULL
, 0);
962 strmap_for_each_entry(all_headers
, &iter
, entry
) {
963 if (match_pathspec(the_repository
->index
, &o
->pathspec
,
964 entry
->key
, strlen(entry
->key
),
965 0 /* prefix */, NULL
/* seen */,
967 strmap_put(o
->additional_path_headers
,
968 entry
->key
, entry
->value
);
970 if (!strmap_get_size(o
->additional_path_headers
)) {
971 strmap_clear(o
->additional_path_headers
, 0);
972 FREE_AND_NULL(o
->additional_path_headers
);
976 static void cleanup_additional_headers(struct diff_options
*o
)
978 if (!o
->pathspec
.nr
) {
979 o
->additional_path_headers
= NULL
;
982 if (!o
->additional_path_headers
)
985 strmap_clear(o
->additional_path_headers
, 0);
986 FREE_AND_NULL(o
->additional_path_headers
);
989 static int do_remerge_diff(struct rev_info
*opt
,
990 struct commit_list
*parents
,
991 struct object_id
*oid
)
993 struct merge_options o
;
994 struct commit_list
*bases
;
995 struct merge_result res
= {0};
996 struct pretty_print_context ctx
= {0};
997 struct commit
*parent1
= parents
->item
;
998 struct commit
*parent2
= parents
->next
->item
;
999 struct strbuf parent1_desc
= STRBUF_INIT
;
1000 struct strbuf parent2_desc
= STRBUF_INIT
;
1002 /* Setup merge options */
1003 init_merge_options(&o
, the_repository
);
1004 o
.show_rename_progress
= 0;
1005 o
.record_conflict_msgs_as_headers
= 1;
1006 o
.msg_header_prefix
= "remerge";
1008 ctx
.abbrev
= DEFAULT_ABBREV
;
1009 repo_format_commit_message(the_repository
, parent1
, "%h (%s)",
1010 &parent1_desc
, &ctx
);
1011 repo_format_commit_message(the_repository
, parent2
, "%h (%s)",
1012 &parent2_desc
, &ctx
);
1013 o
.branch1
= parent1_desc
.buf
;
1014 o
.branch2
= parent2_desc
.buf
;
1016 /* Parse the relevant commits and get the merge bases */
1017 parse_commit_or_die(parent1
);
1018 parse_commit_or_die(parent2
);
1019 bases
= repo_get_merge_bases(the_repository
, parent1
, parent2
);
1021 /* Re-merge the parents */
1022 merge_incore_recursive(&o
, bases
, parent1
, parent2
, &res
);
1025 setup_additional_headers(&opt
->diffopt
, res
.path_messages
);
1026 diff_tree_oid(&res
.tree
->object
.oid
, oid
, "", &opt
->diffopt
);
1027 log_tree_diff_flush(opt
);
1030 cleanup_additional_headers(&opt
->diffopt
);
1031 strbuf_release(&parent1_desc
);
1032 strbuf_release(&parent2_desc
);
1033 merge_finalize(&o
, &res
);
1035 /* Clean up the contents of the temporary object directory */
1036 if (opt
->remerge_objdir
)
1037 tmp_objdir_discard_objects(opt
->remerge_objdir
);
1039 BUG("did a remerge diff without remerge_objdir?!?");
1041 return !opt
->loginfo
;
1045 * Show the diff of a commit.
1047 * Return true if we printed any log info messages
1049 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
1052 struct commit_list
*parents
;
1053 struct object_id
*oid
;
1055 int all_need_diff
= opt
->diff
|| opt
->diffopt
.flags
.exit_with_status
;
1057 if (!all_need_diff
&& !opt
->merges_need_diff
)
1060 parse_commit_or_die(commit
);
1061 oid
= get_commit_tree_oid(commit
);
1063 parents
= get_saved_parents(opt
, commit
);
1064 is_merge
= parents
&& parents
->next
;
1065 if (!is_merge
&& !all_need_diff
)
1070 if (opt
->show_root_diff
) {
1071 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
1072 log_tree_diff_flush(opt
);
1074 return !opt
->loginfo
;
1078 int octopus
= (parents
->next
->next
!= NULL
);
1080 if (opt
->remerge_diff
) {
1083 fprintf(opt
->diffopt
.file
,
1084 "diff: warning: Skipping remerge-diff "
1085 "for octopus merges.\n");
1088 return do_remerge_diff(opt
, parents
, oid
);
1090 if (opt
->combine_merges
)
1091 return do_diff_combined(opt
, commit
);
1092 if (opt
->separate_merges
) {
1093 if (!opt
->first_parent_merges
) {
1094 /* Show parent info for multiple diffs */
1095 log
->parent
= parents
->item
;
1103 struct commit
*parent
= parents
->item
;
1105 parse_commit_or_die(parent
);
1106 diff_tree_oid(get_commit_tree_oid(parent
),
1107 oid
, "", &opt
->diffopt
);
1108 log_tree_diff_flush(opt
);
1110 showed_log
|= !opt
->loginfo
;
1112 /* Set up the log info for the next parent, if any.. */
1113 parents
= parents
->next
;
1114 if (!parents
|| opt
->first_parent_merges
)
1116 log
->parent
= parents
->item
;
1122 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
1124 struct log_info log
;
1126 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1127 int no_free
= opt
->diffopt
.no_free
;
1129 log
.commit
= commit
;
1131 opt
->loginfo
= &log
;
1132 opt
->diffopt
.no_free
= 1;
1134 /* NEEDSWORK: no restoring of no_free? Why? */
1135 if (opt
->line_level_traverse
)
1136 return line_log_print(opt
, commit
);
1138 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
1139 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1140 shown
= log_tree_diff(opt
, commit
, &log
);
1141 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
1146 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
1147 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1148 opt
->loginfo
= NULL
;
1149 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
1150 opt
->diffopt
.no_free
= no_free
;
1151 diff_free(&opt
->diffopt
);