1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "commit-reach.h"
9 #include "environment.h"
11 #include "object-name.h"
12 #include "object-file.h"
13 #include "repository.h"
14 #include "tmp-objdir.h"
19 #include "merge-ort.h"
20 #include "reflog-walk.h"
22 #include "replace-object.h"
24 #include "string-list.h"
26 #include "gpg-interface.h"
27 #include "sequencer.h"
30 #include "range-diff.h"
33 #include "wildmatch.h"
34 #include "write-or-die.h"
37 static struct decoration name_decoration
= { "object names" };
38 static int decoration_loaded
;
39 static int decoration_flags
;
41 static char decoration_colors
[][COLOR_MAXLEN
] = {
43 GIT_COLOR_BOLD_GREEN
, /* REF_LOCAL */
44 GIT_COLOR_BOLD_RED
, /* REF_REMOTE */
45 GIT_COLOR_BOLD_YELLOW
, /* REF_TAG */
46 GIT_COLOR_BOLD_MAGENTA
, /* REF_STASH */
47 GIT_COLOR_BOLD_CYAN
, /* REF_HEAD */
48 GIT_COLOR_BOLD_BLUE
, /* GRAFTED */
51 static const char *color_decorate_slots
[] = {
52 [DECORATION_REF_LOCAL
] = "branch",
53 [DECORATION_REF_REMOTE
] = "remoteBranch",
54 [DECORATION_REF_TAG
] = "tag",
55 [DECORATION_REF_STASH
] = "stash",
56 [DECORATION_REF_HEAD
] = "HEAD",
57 [DECORATION_GRAFTED
] = "grafted",
60 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
62 if (want_color(decorate_use_color
))
63 return decoration_colors
[ix
];
67 define_list_config_array(color_decorate_slots
);
69 int parse_decorate_color_config(const char *var
, const char *slot_name
, const char *value
)
71 int slot
= LOOKUP_CONFIG(color_decorate_slots
, slot_name
);
75 return config_error_nonbool(var
);
76 return color_parse(value
, decoration_colors
[slot
]);
80 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
81 * for showing the commit sha1, use the same check for --decorate
83 #define decorate_get_color_opt(o, ix) \
84 decorate_get_color((o)->use_color, ix)
86 void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
88 struct name_decoration
*res
;
89 FLEX_ALLOC_STR(res
, name
, name
);
91 res
->next
= add_decoration(&name_decoration
, obj
, res
);
94 const struct name_decoration
*get_name_decoration(const struct object
*obj
)
96 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
97 return lookup_decoration(&name_decoration
, obj
);
100 static int match_ref_pattern(const char *refname
,
101 const struct string_list_item
*item
)
105 if (!wildmatch(item
->string
, refname
, 0))
109 if (skip_prefix(refname
, item
->string
, &rest
) &&
110 (!*rest
|| *rest
== '/'))
116 static int ref_filter_match(const char *refname
,
117 const struct decoration_filter
*filter
)
119 struct string_list_item
*item
;
120 const struct string_list
*exclude_patterns
= filter
->exclude_ref_pattern
;
121 const struct string_list
*include_patterns
= filter
->include_ref_pattern
;
122 const struct string_list
*exclude_patterns_config
=
123 filter
->exclude_ref_config_pattern
;
125 if (exclude_patterns
&& exclude_patterns
->nr
) {
126 for_each_string_list_item(item
, exclude_patterns
) {
127 if (match_ref_pattern(refname
, item
))
132 if (include_patterns
&& include_patterns
->nr
) {
133 for_each_string_list_item(item
, include_patterns
) {
134 if (match_ref_pattern(refname
, item
))
140 if (exclude_patterns_config
&& exclude_patterns_config
->nr
) {
141 for_each_string_list_item(item
, exclude_patterns_config
) {
142 if (match_ref_pattern(refname
, item
))
150 static int add_ref_decoration(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
156 enum object_type objtype
;
157 enum decoration_type deco_type
= DECORATION_NONE
;
158 struct decoration_filter
*filter
= (struct decoration_filter
*)cb_data
;
159 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
161 if (filter
&& !ref_filter_match(refname
, filter
))
164 if (starts_with(refname
, git_replace_ref_base
)) {
165 struct object_id original_oid
;
166 if (!replace_refs_enabled(the_repository
))
168 if (get_oid_hex(refname
+ strlen(git_replace_ref_base
),
170 warning("invalid replace ref %s", refname
);
173 obj
= parse_object(the_repository
, &original_oid
);
175 add_name_decoration(DECORATION_GRAFTED
, "replaced", obj
);
179 objtype
= oid_object_info(the_repository
, oid
, NULL
);
182 obj
= lookup_object_by_type(the_repository
, oid
, objtype
);
184 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
185 struct ref_namespace_info
*info
= &ref_namespace
[i
];
187 if (!info
->decoration
)
190 if (!strcmp(refname
, info
->ref
)) {
191 deco_type
= info
->decoration
;
194 } else if (starts_with(refname
, info
->ref
)) {
195 deco_type
= info
->decoration
;
200 add_name_decoration(deco_type
, refname
, obj
);
201 while (obj
->type
== OBJ_TAG
) {
203 parse_object(the_repository
, &obj
->oid
);
204 obj
= ((struct tag
*)obj
)->tagged
;
207 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
212 static int add_graft_decoration(const struct commit_graft
*graft
,
213 void *cb_data UNUSED
)
215 struct commit
*commit
= lookup_commit(the_repository
, &graft
->oid
);
218 add_name_decoration(DECORATION_GRAFTED
, "grafted", &commit
->object
);
222 void load_ref_decorations(struct decoration_filter
*filter
, int flags
)
224 if (!decoration_loaded
) {
226 struct string_list_item
*item
;
227 for_each_string_list_item(item
, filter
->exclude_ref_pattern
) {
228 normalize_glob_ref(item
, NULL
, item
->string
);
230 for_each_string_list_item(item
, filter
->include_ref_pattern
) {
231 normalize_glob_ref(item
, NULL
, item
->string
);
233 for_each_string_list_item(item
, filter
->exclude_ref_config_pattern
) {
234 normalize_glob_ref(item
, NULL
, item
->string
);
237 /* normalize_glob_ref duplicates the strings */
238 filter
->exclude_ref_pattern
->strdup_strings
= 1;
239 filter
->include_ref_pattern
->strdup_strings
= 1;
240 filter
->exclude_ref_config_pattern
->strdup_strings
= 1;
242 decoration_loaded
= 1;
243 decoration_flags
= flags
;
244 refs_for_each_ref(get_main_ref_store(the_repository
),
245 add_ref_decoration
, filter
);
246 refs_head_ref(get_main_ref_store(the_repository
),
247 add_ref_decoration
, filter
);
248 for_each_commit_graft(add_graft_decoration
, filter
);
252 void load_branch_decorations(void)
254 if (!decoration_loaded
) {
255 struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
256 struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
257 struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
258 struct decoration_filter decoration_filter
= {
259 .include_ref_pattern
= &decorate_refs_include
,
260 .exclude_ref_pattern
= &decorate_refs_exclude
,
261 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
264 string_list_append(&decorate_refs_include
, "refs/heads/");
265 load_ref_decorations(&decoration_filter
, 0);
267 string_list_clear(&decorate_refs_exclude
, 0);
268 string_list_clear(&decorate_refs_exclude_config
, 0);
269 string_list_clear(&decorate_refs_include
, 0);
273 static void show_parents(struct commit
*commit
, int abbrev
, FILE *file
)
275 struct commit_list
*p
;
276 for (p
= commit
->parents
; p
; p
= p
->next
) {
277 struct commit
*parent
= p
->item
;
279 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev
));
283 static void show_children(struct rev_info
*opt
, struct commit
*commit
, int abbrev
)
285 struct commit_list
*p
= lookup_decoration(&opt
->children
, &commit
->object
);
286 for ( ; p
; p
= p
->next
) {
287 fprintf(opt
->diffopt
.file
, " %s",
288 repo_find_unique_abbrev(the_repository
, &p
->item
->object
.oid
, abbrev
));
293 * Do we have HEAD in the output, and also the branch it points at?
294 * If so, find that decoration entry for that current branch.
296 static const struct name_decoration
*current_pointed_by_HEAD(const struct name_decoration
*decoration
)
298 const struct name_decoration
*list
, *head
= NULL
;
299 const char *branch_name
= NULL
;
302 /* First find HEAD */
303 for (list
= decoration
; list
; list
= list
->next
)
304 if (list
->type
== DECORATION_REF_HEAD
) {
311 /* Now resolve and find the matching current branch */
312 branch_name
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
313 "HEAD", 0, NULL
, &rru_flags
);
314 if (!branch_name
|| !(rru_flags
& REF_ISSYMREF
))
317 if (!starts_with(branch_name
, "refs/"))
320 /* OK, do we have that ref in the list? */
321 for (list
= decoration
; list
; list
= list
->next
)
322 if ((list
->type
== DECORATION_REF_LOCAL
) &&
323 !strcmp(branch_name
, list
->name
)) {
330 static void show_name(struct strbuf
*sb
, const struct name_decoration
*decoration
)
332 if (decoration_flags
== DECORATE_SHORT_REFS
)
333 strbuf_addstr(sb
, prettify_refname(decoration
->name
));
335 strbuf_addstr(sb
, decoration
->name
);
339 * The caller makes sure there is no funny color before calling.
340 * format_decorations ensures the same after return.
342 void format_decorations(struct strbuf
*sb
,
343 const struct commit
*commit
,
345 const struct decoration_options
*opts
)
347 const struct name_decoration
*decoration
;
348 const struct name_decoration
*current_and_HEAD
;
349 const char *color_commit
, *color_reset
;
351 const char *prefix
= " (";
352 const char *suffix
= ")";
353 const char *separator
= ", ";
354 const char *pointer
= " -> ";
355 const char *tag
= "tag: ";
357 decoration
= get_name_decoration(&commit
->object
);
363 prefix
= opts
->prefix
;
365 suffix
= opts
->suffix
;
367 separator
= opts
->separator
;
369 pointer
= opts
->pointer
;
374 color_commit
= diff_get_color(use_color
, DIFF_COMMIT
);
375 color_reset
= decorate_get_color(use_color
, DECORATION_NONE
);
377 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
380 * When both current and HEAD are there, only
381 * show HEAD->current where HEAD would have
382 * appeared, skipping the entry for current.
384 if (decoration
!= current_and_HEAD
) {
386 decorate_get_color(use_color
, decoration
->type
);
389 strbuf_addstr(sb
, color_commit
);
390 strbuf_addstr(sb
, prefix
);
391 strbuf_addstr(sb
, color_reset
);
394 if (*tag
&& decoration
->type
== DECORATION_REF_TAG
) {
395 strbuf_addstr(sb
, color
);
396 strbuf_addstr(sb
, tag
);
397 strbuf_addstr(sb
, color_reset
);
400 strbuf_addstr(sb
, color
);
401 show_name(sb
, decoration
);
402 strbuf_addstr(sb
, color_reset
);
404 if (current_and_HEAD
&&
405 decoration
->type
== DECORATION_REF_HEAD
) {
406 strbuf_addstr(sb
, color_commit
);
407 strbuf_addstr(sb
, pointer
);
408 strbuf_addstr(sb
, color_reset
);
409 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
410 show_name(sb
, current_and_HEAD
);
411 strbuf_addstr(sb
, color_reset
);
416 decoration
= decoration
->next
;
419 strbuf_addstr(sb
, color_commit
);
420 strbuf_addstr(sb
, suffix
);
421 strbuf_addstr(sb
, color_reset
);
425 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
427 struct strbuf sb
= STRBUF_INIT
;
430 char **slot
= revision_sources_peek(opt
->sources
, commit
);
433 fprintf(opt
->diffopt
.file
, "\t%s", *slot
);
435 if (!opt
->show_decorations
)
437 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
, NULL
);
438 fputs(sb
.buf
, opt
->diffopt
.file
);
442 void fmt_output_subject(struct strbuf
*filename
,
444 struct rev_info
*info
)
446 const char *suffix
= info
->patch_suffix
;
448 int start_len
= filename
->len
;
449 int max_len
= start_len
+ info
->patch_name_max
- (strlen(suffix
) + 1);
451 if (info
->reroll_count
) {
452 struct strbuf temp
= STRBUF_INIT
;
454 strbuf_addf(&temp
, "v%s", info
->reroll_count
);
455 format_sanitized_subject(filename
, temp
.buf
, temp
.len
);
456 strbuf_addstr(filename
, "-");
457 strbuf_release(&temp
);
459 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
461 if (max_len
< filename
->len
)
462 strbuf_setlen(filename
, max_len
);
463 strbuf_addstr(filename
, suffix
);
466 void fmt_output_commit(struct strbuf
*filename
,
467 struct commit
*commit
,
468 struct rev_info
*info
)
470 struct pretty_print_context ctx
= {0};
471 struct strbuf subject
= STRBUF_INIT
;
473 repo_format_commit_message(the_repository
, commit
, "%f", &subject
,
475 fmt_output_subject(filename
, subject
.buf
, info
);
476 strbuf_release(&subject
);
479 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
481 if (opt
->total
> 0) {
482 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
484 *opt
->subject_prefix
? " " : "",
485 decimal_width(opt
->total
),
486 opt
->nr
, opt
->total
);
487 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
488 strbuf_addf(sb
, "Subject: [%s] ",
489 opt
->subject_prefix
);
491 strbuf_addstr(sb
, "Subject: ");
495 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
496 char **extra_headers_p
,
497 int *need_8bit_cte_p
,
500 struct strbuf headers
= STRBUF_INIT
;
501 const char *name
= oid_to_hex(opt
->zero_commit
?
502 null_oid(the_hash_algo
) : &commit
->object
.oid
);
504 *need_8bit_cte_p
= 0; /* unknown */
506 if (opt
->extra_headers
&& *opt
->extra_headers
)
507 strbuf_addstr(&headers
, opt
->extra_headers
);
509 fprintf(opt
->diffopt
.file
, "From %s Mon Sep 17 00:00:00 2001\n", name
);
510 graph_show_oneline(opt
->graph
);
511 if (opt
->message_id
) {
512 fprintf(opt
->diffopt
.file
, "Message-ID: <%s>\n", opt
->message_id
);
513 graph_show_oneline(opt
->graph
);
515 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
517 n
= opt
->ref_message_ids
->nr
;
518 fprintf(opt
->diffopt
.file
, "In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
519 for (i
= 0; i
< n
; i
++)
520 fprintf(opt
->diffopt
.file
, "%s<%s>\n", (i
> 0 ? "\t" : "References: "),
521 opt
->ref_message_ids
->items
[i
].string
);
522 graph_show_oneline(opt
->graph
);
524 if (opt
->mime_boundary
&& maybe_multipart
) {
525 static struct strbuf buffer
= STRBUF_INIT
;
526 struct strbuf filename
= STRBUF_INIT
;
527 *need_8bit_cte_p
= -1; /* NEVER */
529 strbuf_reset(&buffer
);
531 strbuf_addf(&headers
,
532 "MIME-Version: 1.0\n"
533 "Content-Type: multipart/mixed;"
534 " boundary=\"%s%s\"\n"
536 "This is a multi-part message in MIME "
539 "Content-Type: text/plain; "
540 "charset=UTF-8; format=fixed\n"
541 "Content-Transfer-Encoding: 8bit\n\n",
542 mime_boundary_leader
, opt
->mime_boundary
,
543 mime_boundary_leader
, opt
->mime_boundary
);
545 if (opt
->numbered_files
)
546 strbuf_addf(&filename
, "%d", opt
->nr
);
548 fmt_output_commit(&filename
, commit
, opt
);
551 "Content-Type: text/x-patch;"
553 "Content-Transfer-Encoding: 8bit\n"
554 "Content-Disposition: %s;"
555 " filename=\"%s\"\n\n",
556 mime_boundary_leader
, opt
->mime_boundary
,
558 opt
->no_inline
? "attachment" : "inline",
560 opt
->diffopt
.stat_sep
= buffer
.buf
;
561 strbuf_release(&filename
);
563 *extra_headers_p
= headers
.len
? strbuf_detach(&headers
, NULL
) : NULL
;
566 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
568 const char *color
, *reset
, *eol
;
570 color
= diff_get_color_opt(&opt
->diffopt
,
571 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
572 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
574 eol
= strchrnul(bol
, '\n');
575 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
577 graph_show_oneline(opt
->graph
);
578 bol
= (*eol
) ? (eol
+ 1) : eol
;
582 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
584 struct strbuf payload
= STRBUF_INIT
;
585 struct strbuf signature
= STRBUF_INIT
;
586 struct signature_check sigc
= { 0 };
589 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
592 sigc
.payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
593 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
594 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
595 if (status
&& !sigc
.output
)
596 show_sig_lines(opt
, status
, "No signature\n");
598 show_sig_lines(opt
, status
, sigc
.output
);
599 signature_check_clear(&sigc
);
602 strbuf_release(&payload
);
603 strbuf_release(&signature
);
606 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
609 const struct commit_list
*parent
;
611 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
612 if (oideq(&parent
->item
->object
.oid
, oid
))
619 static int is_common_merge(const struct commit
*commit
)
621 return (commit
->parents
622 && commit
->parents
->next
623 && !commit
->parents
->next
->next
);
626 static int show_one_mergetag(struct commit
*commit
,
627 struct commit_extra_header
*extra
,
630 struct rev_info
*opt
= (struct rev_info
*)data
;
631 struct object_id oid
;
633 struct strbuf verify_message
;
634 struct signature_check sigc
= { 0 };
636 struct strbuf payload
= STRBUF_INIT
;
637 struct strbuf signature
= STRBUF_INIT
;
639 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
641 tag
= lookup_tag(the_repository
, &oid
);
643 return -1; /* error message already given */
645 strbuf_init(&verify_message
, 256);
646 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
647 strbuf_addstr(&verify_message
, "malformed mergetag\n");
648 else if (is_common_merge(commit
) &&
649 oideq(&tag
->tagged
->oid
,
650 &commit
->parents
->next
->item
->object
.oid
))
651 strbuf_addf(&verify_message
,
652 "merged tag '%s'\n", tag
->tag
);
653 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
654 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
655 tag
->tag
, oid_to_hex(&tag
->tagged
->oid
));
657 strbuf_addf(&verify_message
,
658 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
661 if (parse_signature(extra
->value
, extra
->len
, &payload
, &signature
)) {
662 /* could have a good signature */
663 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
664 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
665 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
667 strbuf_addstr(&verify_message
, sigc
.output
);
669 strbuf_addstr(&verify_message
, "No signature\n");
670 signature_check_clear(&sigc
);
671 /* otherwise we couldn't verify, which is shown as bad */
674 show_sig_lines(opt
, status
, verify_message
.buf
);
675 strbuf_release(&verify_message
);
676 strbuf_release(&payload
);
677 strbuf_release(&signature
);
681 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
683 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
686 static void next_commentary_block(struct rev_info
*opt
, struct strbuf
*sb
)
688 const char *x
= opt
->shown_dashes
? "\n" : "---\n";
690 strbuf_addstr(sb
, x
);
692 fputs(x
, opt
->diffopt
.file
);
693 opt
->shown_dashes
= 1;
696 static void show_diff_of_diff(struct rev_info
*opt
)
698 if (!cmit_fmt_is_mail(opt
->commit_format
))
701 if (opt
->idiff_oid1
) {
702 struct diff_queue_struct dq
;
704 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
705 diff_queue_init(&diff_queued_diff
);
707 fprintf_ln(opt
->diffopt
.file
, "\n%s", opt
->idiff_title
);
708 show_interdiff(opt
->idiff_oid1
, opt
->idiff_oid2
, 2,
711 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
715 struct diff_queue_struct dq
;
716 struct diff_options opts
;
717 struct range_diff_options range_diff_opts
= {
718 .creation_factor
= opt
->creation_factor
,
723 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
724 diff_queue_init(&diff_queued_diff
);
726 fprintf_ln(opt
->diffopt
.file
, "\n%s", opt
->rdiff_title
);
728 * Pass minimum required diff-options to range-diff; others
729 * can be added later if deemed desirable.
731 repo_diff_setup(the_repository
, &opts
);
732 opts
.file
= opt
->diffopt
.file
;
733 opts
.use_color
= opt
->diffopt
.use_color
;
734 diff_setup_done(&opts
);
735 show_range_diff(opt
->rdiff1
, opt
->rdiff2
, &range_diff_opts
);
737 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
741 void show_log(struct rev_info
*opt
)
743 struct strbuf msgbuf
= STRBUF_INIT
;
744 struct log_info
*log
= opt
->loginfo
;
745 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
746 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: the_hash_algo
->hexsz
;
747 struct pretty_print_context ctx
= {0};
750 if (!opt
->verbose_header
) {
751 graph_show_commit(opt
->graph
);
754 put_revision_mark(opt
, commit
);
755 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev_commit
),
757 if (opt
->print_parents
)
758 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
759 if (opt
->children
.name
)
760 show_children(opt
, commit
, abbrev_commit
);
761 show_decorations(opt
, commit
);
762 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
763 putc('\n', opt
->diffopt
.file
);
764 graph_show_remainder(opt
->graph
);
766 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
771 * If use_terminator is set, we already handled any record termination
772 * at the end of the last record.
773 * Otherwise, add a diffopt.line_termination character before all
774 * entries but the first. (IOW, as a separator between entries)
776 if (opt
->shown_one
&& !opt
->use_terminator
) {
778 * If entries are separated by a newline, the output
779 * should look human-readable. If the last entry ended
780 * with a newline, print the graph output before this
781 * newline. Otherwise it will end up as a completely blank
782 * line and will look like a gap in the graph.
784 * If the entry separator is not a newline, the output is
785 * primarily intended for programmatic consumption, and we
786 * never want the extra graph output before the entry
789 if (opt
->diffopt
.line_termination
== '\n' &&
790 !opt
->missing_newline
)
791 graph_show_padding(opt
->graph
);
792 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
797 * If the history graph was requested,
798 * print the graph, up to this commit's line
800 graph_show_commit(opt
->graph
);
803 * Print header line of header..
806 if (cmit_fmt_is_mail(opt
->commit_format
)) {
807 log_write_email_headers(opt
, commit
, &ctx
.after_subject
,
808 &ctx
.need_8bit_cte
, 1);
810 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
811 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
812 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
813 fputs("commit ", opt
->diffopt
.file
);
816 put_revision_mark(opt
, commit
);
817 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
,
820 if (opt
->print_parents
)
821 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
822 if (opt
->children
.name
)
823 show_children(opt
, commit
, abbrev_commit
);
825 fprintf(opt
->diffopt
.file
, " (from %s)",
826 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev_commit
));
827 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
828 show_decorations(opt
, commit
);
829 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
830 putc(' ', opt
->diffopt
.file
);
832 putc('\n', opt
->diffopt
.file
);
833 graph_show_oneline(opt
->graph
);
835 if (opt
->reflog_info
) {
837 * setup_revisions() ensures that opt->reflog_info
838 * and opt->graph cannot both be set,
839 * so we don't need to worry about printing the
842 show_reflog_message(opt
->reflog_info
,
843 opt
->commit_format
== CMIT_FMT_ONELINE
,
845 opt
->date_mode_explicit
);
846 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
851 if (opt
->show_signature
) {
852 show_signature(opt
, commit
);
853 show_mergetag(opt
, commit
);
856 if (opt
->show_notes
) {
858 struct strbuf notebuf
= STRBUF_INIT
;
860 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
861 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
862 get_log_output_encoding(), raw
);
863 ctx
.notes_message
= strbuf_detach(¬ebuf
, NULL
);
867 * And then the pretty-printed message itself
869 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
871 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT
));
872 ctx
.date_mode
= opt
->date_mode
;
873 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
874 ctx
.abbrev
= opt
->diffopt
.abbrev
;
875 ctx
.preserve_subject
= opt
->preserve_subject
;
876 ctx
.encode_email_headers
= opt
->encode_email_headers
;
877 ctx
.reflog_info
= opt
->reflog_info
;
878 ctx
.fmt
= opt
->commit_format
;
879 ctx
.mailmap
= opt
->mailmap
;
880 ctx
.color
= opt
->diffopt
.use_color
;
881 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
882 ctx
.output_encoding
= get_log_output_encoding();
884 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
885 ctx
.from_ident
= &opt
->from_ident
;
887 ctx
.graph_width
= graph_width(opt
->graph
);
888 pretty_print_commit(&ctx
, commit
, &msgbuf
);
890 if (opt
->add_signoff
)
891 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
893 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
894 ctx
.notes_message
&& *ctx
.notes_message
) {
895 if (cmit_fmt_is_mail(ctx
.fmt
))
896 next_commentary_block(opt
, &msgbuf
);
897 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
900 if (opt
->show_log_size
) {
901 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
902 graph_show_oneline(opt
->graph
);
906 * Set opt->missing_newline if msgbuf doesn't
907 * end in a newline (including if it is empty)
909 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
910 opt
->missing_newline
= 1;
912 opt
->missing_newline
= 0;
914 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
915 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
916 if (!opt
->missing_newline
)
917 graph_show_padding(opt
->graph
);
918 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
921 strbuf_release(&msgbuf
);
922 free(ctx
.notes_message
);
923 free(ctx
.after_subject
);
926 int log_tree_diff_flush(struct rev_info
*opt
)
928 opt
->shown_dashes
= 0;
929 diffcore_std(&opt
->diffopt
);
931 if (diff_queue_is_empty(&opt
->diffopt
)) {
932 int saved_fmt
= opt
->diffopt
.output_format
;
933 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
934 diff_flush(&opt
->diffopt
);
935 opt
->diffopt
.output_format
= saved_fmt
;
939 if (opt
->loginfo
&& !opt
->no_commit_id
) {
941 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
942 opt
->verbose_header
&&
943 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
944 !commit_format_is_empty(opt
->commit_format
)) {
946 * When showing a verbose header (i.e. log message),
947 * and not in --pretty=oneline format, we would want
948 * an extra newline between the end of log and the
949 * diff/diffstat output for readability.
951 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
952 fputs(diff_line_prefix(&opt
->diffopt
), opt
->diffopt
.file
);
955 * We may have shown three-dashes line early
956 * between generated commentary (notes, etc.)
957 * and the log message, in which case we only
958 * want a blank line after the commentary
959 * without (an extra) three-dashes line.
960 * Otherwise, we show the three-dashes line if
961 * we are showing the patch with diffstat, but
962 * in that case, there is no extra blank line
963 * after the three-dashes line.
965 if (!opt
->shown_dashes
&&
966 (pch
& opt
->diffopt
.output_format
) == pch
)
967 fprintf(opt
->diffopt
.file
, "---");
968 putc('\n', opt
->diffopt
.file
);
971 diff_flush(&opt
->diffopt
);
975 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
977 diff_tree_combined_merge(commit
, opt
);
978 return !opt
->loginfo
;
981 static void setup_additional_headers(struct diff_options
*o
,
982 struct strmap
*all_headers
)
984 struct hashmap_iter iter
;
985 struct strmap_entry
*entry
;
988 * Make o->additional_path_headers contain the subset of all_headers
989 * that match o->pathspec. If there aren't any that match o->pathspec,
990 * then make o->additional_path_headers be NULL.
993 if (!o
->pathspec
.nr
) {
994 o
->additional_path_headers
= all_headers
;
998 o
->additional_path_headers
= xmalloc(sizeof(struct strmap
));
999 strmap_init_with_options(o
->additional_path_headers
, NULL
, 0);
1000 strmap_for_each_entry(all_headers
, &iter
, entry
) {
1001 if (match_pathspec(the_repository
->index
, &o
->pathspec
,
1002 entry
->key
, strlen(entry
->key
),
1003 0 /* prefix */, NULL
/* seen */,
1005 strmap_put(o
->additional_path_headers
,
1006 entry
->key
, entry
->value
);
1008 if (!strmap_get_size(o
->additional_path_headers
)) {
1009 strmap_clear(o
->additional_path_headers
, 0);
1010 FREE_AND_NULL(o
->additional_path_headers
);
1014 static void cleanup_additional_headers(struct diff_options
*o
)
1016 if (!o
->pathspec
.nr
) {
1017 o
->additional_path_headers
= NULL
;
1020 if (!o
->additional_path_headers
)
1023 strmap_clear(o
->additional_path_headers
, 0);
1024 FREE_AND_NULL(o
->additional_path_headers
);
1027 static int do_remerge_diff(struct rev_info
*opt
,
1028 struct commit_list
*parents
,
1029 struct object_id
*oid
)
1031 struct merge_options o
;
1032 struct commit_list
*bases
= NULL
;
1033 struct merge_result res
= {0};
1034 struct pretty_print_context ctx
= {0};
1035 struct commit
*parent1
= parents
->item
;
1036 struct commit
*parent2
= parents
->next
->item
;
1037 struct strbuf parent1_desc
= STRBUF_INIT
;
1038 struct strbuf parent2_desc
= STRBUF_INIT
;
1041 * Lazily prepare a temporary object directory and rotate it
1042 * into the alternative object store list as the primary.
1044 if (opt
->remerge_diff
&& !opt
->remerge_objdir
) {
1045 opt
->remerge_objdir
= tmp_objdir_create(the_repository
, "remerge-diff");
1046 if (!opt
->remerge_objdir
)
1047 return error(_("unable to create temporary object directory"));
1048 tmp_objdir_replace_primary_odb(opt
->remerge_objdir
, 1);
1051 /* Setup merge options */
1052 init_ui_merge_options(&o
, the_repository
);
1053 o
.show_rename_progress
= 0;
1054 o
.record_conflict_msgs_as_headers
= 1;
1055 o
.msg_header_prefix
= "remerge";
1057 ctx
.abbrev
= DEFAULT_ABBREV
;
1058 repo_format_commit_message(the_repository
, parent1
, "%h (%s)",
1059 &parent1_desc
, &ctx
);
1060 repo_format_commit_message(the_repository
, parent2
, "%h (%s)",
1061 &parent2_desc
, &ctx
);
1062 o
.branch1
= parent1_desc
.buf
;
1063 o
.branch2
= parent2_desc
.buf
;
1065 /* Parse the relevant commits and get the merge bases */
1066 parse_commit_or_die(parent1
);
1067 parse_commit_or_die(parent2
);
1068 if (repo_get_merge_bases(the_repository
, parent1
, parent2
, &bases
) < 0)
1071 /* Re-merge the parents */
1072 merge_incore_recursive(&o
, bases
, parent1
, parent2
, &res
);
1075 setup_additional_headers(&opt
->diffopt
, res
.path_messages
);
1076 diff_tree_oid(&res
.tree
->object
.oid
, oid
, "", &opt
->diffopt
);
1077 log_tree_diff_flush(opt
);
1080 free_commit_list(bases
);
1081 cleanup_additional_headers(&opt
->diffopt
);
1082 strbuf_release(&parent1_desc
);
1083 strbuf_release(&parent2_desc
);
1084 merge_finalize(&o
, &res
);
1086 /* Clean up the contents of the temporary object directory */
1087 tmp_objdir_discard_objects(opt
->remerge_objdir
);
1089 return !opt
->loginfo
;
1093 * Show the diff of a commit.
1095 * Return true if we printed any log info messages
1097 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
1100 struct commit_list
*parents
;
1101 struct object_id
*oid
;
1103 int all_need_diff
= opt
->diff
|| opt
->diffopt
.flags
.exit_with_status
;
1105 if (!all_need_diff
&& !opt
->merges_need_diff
)
1108 parse_commit_or_die(commit
);
1109 oid
= get_commit_tree_oid(commit
);
1111 parents
= get_saved_parents(opt
, commit
);
1112 is_merge
= parents
&& parents
->next
;
1113 if (!is_merge
&& !all_need_diff
)
1118 if (opt
->show_root_diff
) {
1119 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
1120 log_tree_diff_flush(opt
);
1122 return !opt
->loginfo
;
1126 int octopus
= (parents
->next
->next
!= NULL
);
1128 if (opt
->remerge_diff
) {
1131 fprintf(opt
->diffopt
.file
,
1132 "diff: warning: Skipping remerge-diff "
1133 "for octopus merges.\n");
1136 return do_remerge_diff(opt
, parents
, oid
);
1138 if (opt
->combine_merges
)
1139 return do_diff_combined(opt
, commit
);
1140 if (opt
->separate_merges
) {
1141 if (!opt
->first_parent_merges
) {
1142 /* Show parent info for multiple diffs */
1143 log
->parent
= parents
->item
;
1151 struct commit
*parent
= parents
->item
;
1153 parse_commit_or_die(parent
);
1154 diff_tree_oid(get_commit_tree_oid(parent
),
1155 oid
, "", &opt
->diffopt
);
1156 log_tree_diff_flush(opt
);
1158 showed_log
|= !opt
->loginfo
;
1160 /* Set up the log info for the next parent, if any.. */
1161 parents
= parents
->next
;
1162 if (!parents
|| opt
->first_parent_merges
)
1164 log
->parent
= parents
->item
;
1170 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
1172 struct log_info log
;
1174 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1175 int no_free
= opt
->diffopt
.no_free
;
1177 log
.commit
= commit
;
1179 opt
->loginfo
= &log
;
1180 opt
->diffopt
.no_free
= 1;
1182 /* NEEDSWORK: no restoring of no_free? Why? */
1183 if (opt
->line_level_traverse
)
1184 return line_log_print(opt
, commit
);
1186 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
1187 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1188 shown
= log_tree_diff(opt
, commit
, &log
);
1189 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
1194 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
1195 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1197 show_diff_of_diff(opt
);
1198 opt
->loginfo
= NULL
;
1199 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
1200 opt
->diffopt
.no_free
= no_free
;
1202 diff_free(&opt
->diffopt
);