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
, *color_reset
;
317 const char *prefix
= " (";
318 const char *suffix
= ")";
319 const char *separator
= ", ";
320 const char *pointer
= " -> ";
321 const char *tag
= "tag: ";
323 decoration
= get_name_decoration(&commit
->object
);
329 prefix
= opts
->prefix
;
331 suffix
= opts
->suffix
;
333 separator
= opts
->separator
;
335 pointer
= opts
->pointer
;
340 color_commit
= diff_get_color(use_color
, DIFF_COMMIT
);
341 color_reset
= decorate_get_color(use_color
, DECORATION_NONE
);
343 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
346 * When both current and HEAD are there, only
347 * show HEAD->current where HEAD would have
348 * appeared, skipping the entry for current.
350 if (decoration
!= current_and_HEAD
) {
352 decorate_get_color(use_color
, decoration
->type
);
355 strbuf_addstr(sb
, color_commit
);
356 strbuf_addstr(sb
, prefix
);
357 strbuf_addstr(sb
, color_reset
);
360 if (*tag
&& decoration
->type
== DECORATION_REF_TAG
) {
361 strbuf_addstr(sb
, color
);
362 strbuf_addstr(sb
, tag
);
363 strbuf_addstr(sb
, color_reset
);
366 strbuf_addstr(sb
, color
);
367 show_name(sb
, decoration
);
368 strbuf_addstr(sb
, color_reset
);
370 if (current_and_HEAD
&&
371 decoration
->type
== DECORATION_REF_HEAD
) {
372 strbuf_addstr(sb
, color_commit
);
373 strbuf_addstr(sb
, pointer
);
374 strbuf_addstr(sb
, color_reset
);
375 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
376 show_name(sb
, current_and_HEAD
);
377 strbuf_addstr(sb
, color_reset
);
382 decoration
= decoration
->next
;
385 strbuf_addstr(sb
, color_commit
);
386 strbuf_addstr(sb
, suffix
);
387 strbuf_addstr(sb
, color_reset
);
391 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
393 struct strbuf sb
= STRBUF_INIT
;
396 char **slot
= revision_sources_peek(opt
->sources
, commit
);
399 fprintf(opt
->diffopt
.file
, "\t%s", *slot
);
401 if (!opt
->show_decorations
)
403 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
, NULL
);
404 fputs(sb
.buf
, opt
->diffopt
.file
);
408 static unsigned int digits_in_number(unsigned int number
)
410 unsigned int i
= 10, result
= 1;
411 while (i
<= number
) {
418 void fmt_output_subject(struct strbuf
*filename
,
420 struct rev_info
*info
)
422 const char *suffix
= info
->patch_suffix
;
424 int start_len
= filename
->len
;
425 int max_len
= start_len
+ info
->patch_name_max
- (strlen(suffix
) + 1);
427 if (info
->reroll_count
) {
428 struct strbuf temp
= STRBUF_INIT
;
430 strbuf_addf(&temp
, "v%s", info
->reroll_count
);
431 format_sanitized_subject(filename
, temp
.buf
, temp
.len
);
432 strbuf_addstr(filename
, "-");
433 strbuf_release(&temp
);
435 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
437 if (max_len
< filename
->len
)
438 strbuf_setlen(filename
, max_len
);
439 strbuf_addstr(filename
, suffix
);
442 void fmt_output_commit(struct strbuf
*filename
,
443 struct commit
*commit
,
444 struct rev_info
*info
)
446 struct pretty_print_context ctx
= {0};
447 struct strbuf subject
= STRBUF_INIT
;
449 repo_format_commit_message(the_repository
, commit
, "%f", &subject
,
451 fmt_output_subject(filename
, subject
.buf
, info
);
452 strbuf_release(&subject
);
455 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
457 if (opt
->total
> 0) {
458 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
460 *opt
->subject_prefix
? " " : "",
461 digits_in_number(opt
->total
),
462 opt
->nr
, opt
->total
);
463 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
464 strbuf_addf(sb
, "Subject: [%s] ",
465 opt
->subject_prefix
);
467 strbuf_addstr(sb
, "Subject: ");
471 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
472 const char **extra_headers_p
,
473 int *need_8bit_cte_p
,
476 const char *extra_headers
= opt
->extra_headers
;
477 const char *name
= oid_to_hex(opt
->zero_commit
?
478 null_oid() : &commit
->object
.oid
);
480 *need_8bit_cte_p
= 0; /* unknown */
482 fprintf(opt
->diffopt
.file
, "From %s Mon Sep 17 00:00:00 2001\n", name
);
483 graph_show_oneline(opt
->graph
);
484 if (opt
->message_id
) {
485 fprintf(opt
->diffopt
.file
, "Message-ID: <%s>\n", opt
->message_id
);
486 graph_show_oneline(opt
->graph
);
488 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
490 n
= opt
->ref_message_ids
->nr
;
491 fprintf(opt
->diffopt
.file
, "In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
492 for (i
= 0; i
< n
; i
++)
493 fprintf(opt
->diffopt
.file
, "%s<%s>\n", (i
> 0 ? "\t" : "References: "),
494 opt
->ref_message_ids
->items
[i
].string
);
495 graph_show_oneline(opt
->graph
);
497 if (opt
->mime_boundary
&& maybe_multipart
) {
498 static struct strbuf subject_buffer
= STRBUF_INIT
;
499 static struct strbuf buffer
= STRBUF_INIT
;
500 struct strbuf filename
= STRBUF_INIT
;
501 *need_8bit_cte_p
= -1; /* NEVER */
503 strbuf_reset(&subject_buffer
);
504 strbuf_reset(&buffer
);
506 strbuf_addf(&subject_buffer
,
508 "MIME-Version: 1.0\n"
509 "Content-Type: multipart/mixed;"
510 " boundary=\"%s%s\"\n"
512 "This is a multi-part message in MIME "
515 "Content-Type: text/plain; "
516 "charset=UTF-8; format=fixed\n"
517 "Content-Transfer-Encoding: 8bit\n\n",
518 extra_headers
? extra_headers
: "",
519 mime_boundary_leader
, opt
->mime_boundary
,
520 mime_boundary_leader
, opt
->mime_boundary
);
521 extra_headers
= subject_buffer
.buf
;
523 if (opt
->numbered_files
)
524 strbuf_addf(&filename
, "%d", opt
->nr
);
526 fmt_output_commit(&filename
, commit
, opt
);
529 "Content-Type: text/x-patch;"
531 "Content-Transfer-Encoding: 8bit\n"
532 "Content-Disposition: %s;"
533 " filename=\"%s\"\n\n",
534 mime_boundary_leader
, opt
->mime_boundary
,
536 opt
->no_inline
? "attachment" : "inline",
538 opt
->diffopt
.stat_sep
= buffer
.buf
;
539 strbuf_release(&filename
);
541 *extra_headers_p
= extra_headers
;
544 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
546 const char *color
, *reset
, *eol
;
548 color
= diff_get_color_opt(&opt
->diffopt
,
549 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
550 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
552 eol
= strchrnul(bol
, '\n');
553 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
555 graph_show_oneline(opt
->graph
);
556 bol
= (*eol
) ? (eol
+ 1) : eol
;
560 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
562 struct strbuf payload
= STRBUF_INIT
;
563 struct strbuf signature
= STRBUF_INIT
;
564 struct signature_check sigc
= { 0 };
567 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
570 sigc
.payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
571 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
572 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
573 if (status
&& !sigc
.output
)
574 show_sig_lines(opt
, status
, "No signature\n");
576 show_sig_lines(opt
, status
, sigc
.output
);
577 signature_check_clear(&sigc
);
580 strbuf_release(&payload
);
581 strbuf_release(&signature
);
584 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
587 const struct commit_list
*parent
;
589 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
590 if (oideq(&parent
->item
->object
.oid
, oid
))
597 static int is_common_merge(const struct commit
*commit
)
599 return (commit
->parents
600 && commit
->parents
->next
601 && !commit
->parents
->next
->next
);
604 static int show_one_mergetag(struct commit
*commit
,
605 struct commit_extra_header
*extra
,
608 struct rev_info
*opt
= (struct rev_info
*)data
;
609 struct object_id oid
;
611 struct strbuf verify_message
;
612 struct signature_check sigc
= { 0 };
614 struct strbuf payload
= STRBUF_INIT
;
615 struct strbuf signature
= STRBUF_INIT
;
617 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
619 tag
= lookup_tag(the_repository
, &oid
);
621 return -1; /* error message already given */
623 strbuf_init(&verify_message
, 256);
624 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
625 strbuf_addstr(&verify_message
, "malformed mergetag\n");
626 else if (is_common_merge(commit
) &&
627 oideq(&tag
->tagged
->oid
,
628 &commit
->parents
->next
->item
->object
.oid
))
629 strbuf_addf(&verify_message
,
630 "merged tag '%s'\n", tag
->tag
);
631 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
632 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
633 tag
->tag
, oid_to_hex(&tag
->tagged
->oid
));
635 strbuf_addf(&verify_message
,
636 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
639 if (parse_signature(extra
->value
, extra
->len
, &payload
, &signature
)) {
640 /* could have a good signature */
641 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
642 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
643 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
645 strbuf_addstr(&verify_message
, sigc
.output
);
647 strbuf_addstr(&verify_message
, "No signature\n");
648 signature_check_clear(&sigc
);
649 /* otherwise we couldn't verify, which is shown as bad */
652 show_sig_lines(opt
, status
, verify_message
.buf
);
653 strbuf_release(&verify_message
);
654 strbuf_release(&payload
);
655 strbuf_release(&signature
);
659 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
661 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
664 static void next_commentary_block(struct rev_info
*opt
, struct strbuf
*sb
)
666 const char *x
= opt
->shown_dashes
? "\n" : "---\n";
668 strbuf_addstr(sb
, x
);
670 fputs(x
, opt
->diffopt
.file
);
671 opt
->shown_dashes
= 1;
674 void show_log(struct rev_info
*opt
)
676 struct strbuf msgbuf
= STRBUF_INIT
;
677 struct log_info
*log
= opt
->loginfo
;
678 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
679 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: the_hash_algo
->hexsz
;
680 const char *extra_headers
= opt
->extra_headers
;
681 struct pretty_print_context ctx
= {0};
684 if (!opt
->verbose_header
) {
685 graph_show_commit(opt
->graph
);
688 put_revision_mark(opt
, commit
);
689 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev_commit
),
691 if (opt
->print_parents
)
692 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
693 if (opt
->children
.name
)
694 show_children(opt
, commit
, abbrev_commit
);
695 show_decorations(opt
, commit
);
696 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
697 putc('\n', opt
->diffopt
.file
);
698 graph_show_remainder(opt
->graph
);
700 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
705 * If use_terminator is set, we already handled any record termination
706 * at the end of the last record.
707 * Otherwise, add a diffopt.line_termination character before all
708 * entries but the first. (IOW, as a separator between entries)
710 if (opt
->shown_one
&& !opt
->use_terminator
) {
712 * If entries are separated by a newline, the output
713 * should look human-readable. If the last entry ended
714 * with a newline, print the graph output before this
715 * newline. Otherwise it will end up as a completely blank
716 * line and will look like a gap in the graph.
718 * If the entry separator is not a newline, the output is
719 * primarily intended for programmatic consumption, and we
720 * never want the extra graph output before the entry
723 if (opt
->diffopt
.line_termination
== '\n' &&
724 !opt
->missing_newline
)
725 graph_show_padding(opt
->graph
);
726 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
731 * If the history graph was requested,
732 * print the graph, up to this commit's line
734 graph_show_commit(opt
->graph
);
737 * Print header line of header..
740 if (cmit_fmt_is_mail(opt
->commit_format
)) {
741 log_write_email_headers(opt
, commit
, &extra_headers
,
742 &ctx
.need_8bit_cte
, 1);
744 ctx
.print_email_subject
= 1;
745 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
746 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
747 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
748 fputs("commit ", opt
->diffopt
.file
);
751 put_revision_mark(opt
, commit
);
752 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
,
755 if (opt
->print_parents
)
756 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
757 if (opt
->children
.name
)
758 show_children(opt
, commit
, abbrev_commit
);
760 fprintf(opt
->diffopt
.file
, " (from %s)",
761 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev_commit
));
762 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
763 show_decorations(opt
, commit
);
764 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
765 putc(' ', opt
->diffopt
.file
);
767 putc('\n', opt
->diffopt
.file
);
768 graph_show_oneline(opt
->graph
);
770 if (opt
->reflog_info
) {
772 * setup_revisions() ensures that opt->reflog_info
773 * and opt->graph cannot both be set,
774 * so we don't need to worry about printing the
777 show_reflog_message(opt
->reflog_info
,
778 opt
->commit_format
== CMIT_FMT_ONELINE
,
780 opt
->date_mode_explicit
);
781 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
786 if (opt
->show_signature
) {
787 show_signature(opt
, commit
);
788 show_mergetag(opt
, commit
);
791 if (opt
->show_notes
) {
793 struct strbuf notebuf
= STRBUF_INIT
;
795 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
796 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
797 get_log_output_encoding(), raw
);
798 ctx
.notes_message
= strbuf_detach(¬ebuf
, NULL
);
802 * And then the pretty-printed message itself
804 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
806 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT
));
807 ctx
.date_mode
= opt
->date_mode
;
808 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
809 ctx
.abbrev
= opt
->diffopt
.abbrev
;
810 ctx
.after_subject
= extra_headers
;
811 ctx
.preserve_subject
= opt
->preserve_subject
;
812 ctx
.encode_email_headers
= opt
->encode_email_headers
;
813 ctx
.reflog_info
= opt
->reflog_info
;
814 ctx
.fmt
= opt
->commit_format
;
815 ctx
.mailmap
= opt
->mailmap
;
816 ctx
.color
= opt
->diffopt
.use_color
;
817 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
818 ctx
.output_encoding
= get_log_output_encoding();
820 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
821 ctx
.from_ident
= &opt
->from_ident
;
823 ctx
.graph_width
= graph_width(opt
->graph
);
824 pretty_print_commit(&ctx
, commit
, &msgbuf
);
826 if (opt
->add_signoff
)
827 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
829 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
830 ctx
.notes_message
&& *ctx
.notes_message
) {
831 if (cmit_fmt_is_mail(ctx
.fmt
))
832 next_commentary_block(opt
, &msgbuf
);
833 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
836 if (opt
->show_log_size
) {
837 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
838 graph_show_oneline(opt
->graph
);
842 * Set opt->missing_newline if msgbuf doesn't
843 * end in a newline (including if it is empty)
845 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
846 opt
->missing_newline
= 1;
848 opt
->missing_newline
= 0;
850 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
851 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
852 if (!opt
->missing_newline
)
853 graph_show_padding(opt
->graph
);
854 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
857 strbuf_release(&msgbuf
);
858 free(ctx
.notes_message
);
860 if (cmit_fmt_is_mail(ctx
.fmt
) && opt
->idiff_oid1
) {
861 struct diff_queue_struct dq
;
863 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
864 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
866 next_commentary_block(opt
, NULL
);
867 fprintf_ln(opt
->diffopt
.file
, "%s", opt
->idiff_title
);
868 show_interdiff(opt
->idiff_oid1
, opt
->idiff_oid2
, 2,
871 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
874 if (cmit_fmt_is_mail(ctx
.fmt
) && opt
->rdiff1
) {
875 struct diff_queue_struct dq
;
876 struct diff_options opts
;
877 struct range_diff_options range_diff_opts
= {
878 .creation_factor
= opt
->creation_factor
,
883 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
884 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
886 next_commentary_block(opt
, NULL
);
887 fprintf_ln(opt
->diffopt
.file
, "%s", opt
->rdiff_title
);
889 * Pass minimum required diff-options to range-diff; others
890 * can be added later if deemed desirable.
892 repo_diff_setup(the_repository
, &opts
);
893 opts
.file
= opt
->diffopt
.file
;
894 opts
.use_color
= opt
->diffopt
.use_color
;
895 diff_setup_done(&opts
);
896 show_range_diff(opt
->rdiff1
, opt
->rdiff2
, &range_diff_opts
);
898 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
902 int log_tree_diff_flush(struct rev_info
*opt
)
904 opt
->shown_dashes
= 0;
905 diffcore_std(&opt
->diffopt
);
907 if (diff_queue_is_empty(&opt
->diffopt
)) {
908 int saved_fmt
= opt
->diffopt
.output_format
;
909 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
910 diff_flush(&opt
->diffopt
);
911 opt
->diffopt
.output_format
= saved_fmt
;
915 if (opt
->loginfo
&& !opt
->no_commit_id
) {
917 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
918 opt
->verbose_header
&&
919 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
920 !commit_format_is_empty(opt
->commit_format
)) {
922 * When showing a verbose header (i.e. log message),
923 * and not in --pretty=oneline format, we would want
924 * an extra newline between the end of log and the
925 * diff/diffstat output for readability.
927 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
928 if (opt
->diffopt
.output_prefix
) {
929 struct strbuf
*msg
= NULL
;
930 msg
= opt
->diffopt
.output_prefix(&opt
->diffopt
,
931 opt
->diffopt
.output_prefix_data
);
932 fwrite(msg
->buf
, msg
->len
, 1, opt
->diffopt
.file
);
936 * We may have shown three-dashes line early
937 * between generated commentary (notes, etc.)
938 * and the log message, in which case we only
939 * want a blank line after the commentary
940 * without (an extra) three-dashes line.
941 * Otherwise, we show the three-dashes line if
942 * we are showing the patch with diffstat, but
943 * in that case, there is no extra blank line
944 * after the three-dashes line.
946 if (!opt
->shown_dashes
&&
947 (pch
& opt
->diffopt
.output_format
) == pch
)
948 fprintf(opt
->diffopt
.file
, "---");
949 putc('\n', opt
->diffopt
.file
);
952 diff_flush(&opt
->diffopt
);
956 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
958 diff_tree_combined_merge(commit
, opt
);
959 return !opt
->loginfo
;
962 static void setup_additional_headers(struct diff_options
*o
,
963 struct strmap
*all_headers
)
965 struct hashmap_iter iter
;
966 struct strmap_entry
*entry
;
969 * Make o->additional_path_headers contain the subset of all_headers
970 * that match o->pathspec. If there aren't any that match o->pathspec,
971 * then make o->additional_path_headers be NULL.
974 if (!o
->pathspec
.nr
) {
975 o
->additional_path_headers
= all_headers
;
979 o
->additional_path_headers
= xmalloc(sizeof(struct strmap
));
980 strmap_init_with_options(o
->additional_path_headers
, NULL
, 0);
981 strmap_for_each_entry(all_headers
, &iter
, entry
) {
982 if (match_pathspec(the_repository
->index
, &o
->pathspec
,
983 entry
->key
, strlen(entry
->key
),
984 0 /* prefix */, NULL
/* seen */,
986 strmap_put(o
->additional_path_headers
,
987 entry
->key
, entry
->value
);
989 if (!strmap_get_size(o
->additional_path_headers
)) {
990 strmap_clear(o
->additional_path_headers
, 0);
991 FREE_AND_NULL(o
->additional_path_headers
);
995 static void cleanup_additional_headers(struct diff_options
*o
)
997 if (!o
->pathspec
.nr
) {
998 o
->additional_path_headers
= NULL
;
1001 if (!o
->additional_path_headers
)
1004 strmap_clear(o
->additional_path_headers
, 0);
1005 FREE_AND_NULL(o
->additional_path_headers
);
1008 static int do_remerge_diff(struct rev_info
*opt
,
1009 struct commit_list
*parents
,
1010 struct object_id
*oid
)
1012 struct merge_options o
;
1013 struct commit_list
*bases
;
1014 struct merge_result res
= {0};
1015 struct pretty_print_context ctx
= {0};
1016 struct commit
*parent1
= parents
->item
;
1017 struct commit
*parent2
= parents
->next
->item
;
1018 struct strbuf parent1_desc
= STRBUF_INIT
;
1019 struct strbuf parent2_desc
= STRBUF_INIT
;
1021 /* Setup merge options */
1022 init_merge_options(&o
, the_repository
);
1023 o
.show_rename_progress
= 0;
1024 o
.record_conflict_msgs_as_headers
= 1;
1025 o
.msg_header_prefix
= "remerge";
1027 ctx
.abbrev
= DEFAULT_ABBREV
;
1028 repo_format_commit_message(the_repository
, parent1
, "%h (%s)",
1029 &parent1_desc
, &ctx
);
1030 repo_format_commit_message(the_repository
, parent2
, "%h (%s)",
1031 &parent2_desc
, &ctx
);
1032 o
.branch1
= parent1_desc
.buf
;
1033 o
.branch2
= parent2_desc
.buf
;
1035 /* Parse the relevant commits and get the merge bases */
1036 parse_commit_or_die(parent1
);
1037 parse_commit_or_die(parent2
);
1038 bases
= repo_get_merge_bases(the_repository
, parent1
, parent2
);
1040 /* Re-merge the parents */
1041 merge_incore_recursive(&o
, bases
, parent1
, parent2
, &res
);
1044 setup_additional_headers(&opt
->diffopt
, res
.path_messages
);
1045 diff_tree_oid(&res
.tree
->object
.oid
, oid
, "", &opt
->diffopt
);
1046 log_tree_diff_flush(opt
);
1049 cleanup_additional_headers(&opt
->diffopt
);
1050 strbuf_release(&parent1_desc
);
1051 strbuf_release(&parent2_desc
);
1052 merge_finalize(&o
, &res
);
1054 /* Clean up the contents of the temporary object directory */
1055 if (opt
->remerge_objdir
)
1056 tmp_objdir_discard_objects(opt
->remerge_objdir
);
1058 BUG("did a remerge diff without remerge_objdir?!?");
1060 return !opt
->loginfo
;
1064 * Show the diff of a commit.
1066 * Return true if we printed any log info messages
1068 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
1071 struct commit_list
*parents
;
1072 struct object_id
*oid
;
1074 int all_need_diff
= opt
->diff
|| opt
->diffopt
.flags
.exit_with_status
;
1076 if (!all_need_diff
&& !opt
->merges_need_diff
)
1079 parse_commit_or_die(commit
);
1080 oid
= get_commit_tree_oid(commit
);
1082 parents
= get_saved_parents(opt
, commit
);
1083 is_merge
= parents
&& parents
->next
;
1084 if (!is_merge
&& !all_need_diff
)
1089 if (opt
->show_root_diff
) {
1090 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
1091 log_tree_diff_flush(opt
);
1093 return !opt
->loginfo
;
1097 int octopus
= (parents
->next
->next
!= NULL
);
1099 if (opt
->remerge_diff
) {
1102 fprintf(opt
->diffopt
.file
,
1103 "diff: warning: Skipping remerge-diff "
1104 "for octopus merges.\n");
1107 return do_remerge_diff(opt
, parents
, oid
);
1109 if (opt
->combine_merges
)
1110 return do_diff_combined(opt
, commit
);
1111 if (opt
->separate_merges
) {
1112 if (!opt
->first_parent_merges
) {
1113 /* Show parent info for multiple diffs */
1114 log
->parent
= parents
->item
;
1122 struct commit
*parent
= parents
->item
;
1124 parse_commit_or_die(parent
);
1125 diff_tree_oid(get_commit_tree_oid(parent
),
1126 oid
, "", &opt
->diffopt
);
1127 log_tree_diff_flush(opt
);
1129 showed_log
|= !opt
->loginfo
;
1131 /* Set up the log info for the next parent, if any.. */
1132 parents
= parents
->next
;
1133 if (!parents
|| opt
->first_parent_merges
)
1135 log
->parent
= parents
->item
;
1141 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
1143 struct log_info log
;
1145 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1146 int no_free
= opt
->diffopt
.no_free
;
1148 log
.commit
= commit
;
1150 opt
->loginfo
= &log
;
1151 opt
->diffopt
.no_free
= 1;
1153 /* NEEDSWORK: no restoring of no_free? Why? */
1154 if (opt
->line_level_traverse
)
1155 return line_log_print(opt
, commit
);
1157 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
1158 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1159 shown
= log_tree_diff(opt
, commit
, &log
);
1160 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
1165 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
1166 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1167 opt
->loginfo
= NULL
;
1168 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
1169 opt
->diffopt
.no_free
= no_free
;
1170 diff_free(&opt
->diffopt
);