1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "commit-reach.h"
8 #include "environment.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "repository.h"
13 #include "tmp-objdir.h"
18 #include "merge-ort.h"
19 #include "reflog-walk.h"
21 #include "replace-object.h"
23 #include "string-list.h"
25 #include "gpg-interface.h"
26 #include "sequencer.h"
29 #include "range-diff.h"
32 #include "wildmatch.h"
33 #include "write-or-die.h"
36 static struct decoration name_decoration
= { "object names" };
37 static int decoration_loaded
;
38 static int decoration_flags
;
40 static char decoration_colors
[][COLOR_MAXLEN
] = {
42 GIT_COLOR_BOLD_GREEN
, /* REF_LOCAL */
43 GIT_COLOR_BOLD_RED
, /* REF_REMOTE */
44 GIT_COLOR_BOLD_YELLOW
, /* REF_TAG */
45 GIT_COLOR_BOLD_MAGENTA
, /* REF_STASH */
46 GIT_COLOR_BOLD_CYAN
, /* REF_HEAD */
47 GIT_COLOR_BOLD_BLUE
, /* GRAFTED */
50 static const char *color_decorate_slots
[] = {
51 [DECORATION_REF_LOCAL
] = "branch",
52 [DECORATION_REF_REMOTE
] = "remoteBranch",
53 [DECORATION_REF_TAG
] = "tag",
54 [DECORATION_REF_STASH
] = "stash",
55 [DECORATION_REF_HEAD
] = "HEAD",
56 [DECORATION_GRAFTED
] = "grafted",
59 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
61 if (want_color(decorate_use_color
))
62 return decoration_colors
[ix
];
66 define_list_config_array(color_decorate_slots
);
68 int parse_decorate_color_config(const char *var
, const char *slot_name
, const char *value
)
70 int slot
= LOOKUP_CONFIG(color_decorate_slots
, slot_name
);
74 return config_error_nonbool(var
);
75 return color_parse(value
, decoration_colors
[slot
]);
79 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
80 * for showing the commit sha1, use the same check for --decorate
82 #define decorate_get_color_opt(o, ix) \
83 decorate_get_color((o)->use_color, ix)
85 void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
87 struct name_decoration
*res
;
88 FLEX_ALLOC_STR(res
, name
, name
);
90 res
->next
= add_decoration(&name_decoration
, obj
, res
);
93 const struct name_decoration
*get_name_decoration(const struct object
*obj
)
95 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
96 return lookup_decoration(&name_decoration
, obj
);
99 static int match_ref_pattern(const char *refname
,
100 const struct string_list_item
*item
)
104 if (!wildmatch(item
->string
, refname
, 0))
108 if (skip_prefix(refname
, item
->string
, &rest
) &&
109 (!*rest
|| *rest
== '/'))
115 static int ref_filter_match(const char *refname
,
116 const struct decoration_filter
*filter
)
118 struct string_list_item
*item
;
119 const struct string_list
*exclude_patterns
= filter
->exclude_ref_pattern
;
120 const struct string_list
*include_patterns
= filter
->include_ref_pattern
;
121 const struct string_list
*exclude_patterns_config
=
122 filter
->exclude_ref_config_pattern
;
124 if (exclude_patterns
&& exclude_patterns
->nr
) {
125 for_each_string_list_item(item
, exclude_patterns
) {
126 if (match_ref_pattern(refname
, item
))
131 if (include_patterns
&& include_patterns
->nr
) {
132 for_each_string_list_item(item
, include_patterns
) {
133 if (match_ref_pattern(refname
, item
))
139 if (exclude_patterns_config
&& exclude_patterns_config
->nr
) {
140 for_each_string_list_item(item
, exclude_patterns_config
) {
141 if (match_ref_pattern(refname
, item
))
149 static int add_ref_decoration(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
155 enum object_type objtype
;
156 enum decoration_type deco_type
= DECORATION_NONE
;
157 struct decoration_filter
*filter
= (struct decoration_filter
*)cb_data
;
158 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
160 if (filter
&& !ref_filter_match(refname
, filter
))
163 if (starts_with(refname
, git_replace_ref_base
)) {
164 struct object_id original_oid
;
165 if (!replace_refs_enabled(the_repository
))
167 if (get_oid_hex(refname
+ strlen(git_replace_ref_base
),
169 warning("invalid replace ref %s", refname
);
172 obj
= parse_object(the_repository
, &original_oid
);
174 add_name_decoration(DECORATION_GRAFTED
, "replaced", obj
);
178 objtype
= oid_object_info(the_repository
, oid
, NULL
);
181 obj
= lookup_object_by_type(the_repository
, oid
, objtype
);
183 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
184 struct ref_namespace_info
*info
= &ref_namespace
[i
];
186 if (!info
->decoration
)
189 if (!strcmp(refname
, info
->ref
)) {
190 deco_type
= info
->decoration
;
193 } else if (starts_with(refname
, info
->ref
)) {
194 deco_type
= info
->decoration
;
199 add_name_decoration(deco_type
, refname
, obj
);
200 while (obj
->type
== OBJ_TAG
) {
202 parse_object(the_repository
, &obj
->oid
);
203 obj
= ((struct tag
*)obj
)->tagged
;
206 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
211 static int add_graft_decoration(const struct commit_graft
*graft
,
212 void *cb_data UNUSED
)
214 struct commit
*commit
= lookup_commit(the_repository
, &graft
->oid
);
217 add_name_decoration(DECORATION_GRAFTED
, "grafted", &commit
->object
);
221 void load_ref_decorations(struct decoration_filter
*filter
, int flags
)
223 if (!decoration_loaded
) {
225 struct string_list_item
*item
;
226 for_each_string_list_item(item
, filter
->exclude_ref_pattern
) {
227 normalize_glob_ref(item
, NULL
, item
->string
);
229 for_each_string_list_item(item
, filter
->include_ref_pattern
) {
230 normalize_glob_ref(item
, NULL
, item
->string
);
232 for_each_string_list_item(item
, filter
->exclude_ref_config_pattern
) {
233 normalize_glob_ref(item
, NULL
, item
->string
);
236 decoration_loaded
= 1;
237 decoration_flags
= flags
;
238 refs_for_each_ref(get_main_ref_store(the_repository
),
239 add_ref_decoration
, filter
);
240 refs_head_ref(get_main_ref_store(the_repository
),
241 add_ref_decoration
, filter
);
242 for_each_commit_graft(add_graft_decoration
, filter
);
246 static void show_parents(struct commit
*commit
, int abbrev
, FILE *file
)
248 struct commit_list
*p
;
249 for (p
= commit
->parents
; p
; p
= p
->next
) {
250 struct commit
*parent
= p
->item
;
252 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev
));
256 static void show_children(struct rev_info
*opt
, struct commit
*commit
, int abbrev
)
258 struct commit_list
*p
= lookup_decoration(&opt
->children
, &commit
->object
);
259 for ( ; p
; p
= p
->next
) {
260 fprintf(opt
->diffopt
.file
, " %s",
261 repo_find_unique_abbrev(the_repository
, &p
->item
->object
.oid
, abbrev
));
266 * Do we have HEAD in the output, and also the branch it points at?
267 * If so, find that decoration entry for that current branch.
269 static const struct name_decoration
*current_pointed_by_HEAD(const struct name_decoration
*decoration
)
271 const struct name_decoration
*list
, *head
= NULL
;
272 const char *branch_name
= NULL
;
275 /* First find HEAD */
276 for (list
= decoration
; list
; list
= list
->next
)
277 if (list
->type
== DECORATION_REF_HEAD
) {
284 /* Now resolve and find the matching current branch */
285 branch_name
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
286 "HEAD", 0, NULL
, &rru_flags
);
287 if (!branch_name
|| !(rru_flags
& REF_ISSYMREF
))
290 if (!starts_with(branch_name
, "refs/"))
293 /* OK, do we have that ref in the list? */
294 for (list
= decoration
; list
; list
= list
->next
)
295 if ((list
->type
== DECORATION_REF_LOCAL
) &&
296 !strcmp(branch_name
, list
->name
)) {
303 static void show_name(struct strbuf
*sb
, const struct name_decoration
*decoration
)
305 if (decoration_flags
== DECORATE_SHORT_REFS
)
306 strbuf_addstr(sb
, prettify_refname(decoration
->name
));
308 strbuf_addstr(sb
, decoration
->name
);
312 * The caller makes sure there is no funny color before calling.
313 * format_decorations ensures the same after return.
315 void format_decorations(struct strbuf
*sb
,
316 const struct commit
*commit
,
318 const struct decoration_options
*opts
)
320 const struct name_decoration
*decoration
;
321 const struct name_decoration
*current_and_HEAD
;
322 const char *color_commit
, *color_reset
;
324 const char *prefix
= " (";
325 const char *suffix
= ")";
326 const char *separator
= ", ";
327 const char *pointer
= " -> ";
328 const char *tag
= "tag: ";
330 decoration
= get_name_decoration(&commit
->object
);
336 prefix
= opts
->prefix
;
338 suffix
= opts
->suffix
;
340 separator
= opts
->separator
;
342 pointer
= opts
->pointer
;
347 color_commit
= diff_get_color(use_color
, DIFF_COMMIT
);
348 color_reset
= decorate_get_color(use_color
, DECORATION_NONE
);
350 current_and_HEAD
= current_pointed_by_HEAD(decoration
);
353 * When both current and HEAD are there, only
354 * show HEAD->current where HEAD would have
355 * appeared, skipping the entry for current.
357 if (decoration
!= current_and_HEAD
) {
359 decorate_get_color(use_color
, decoration
->type
);
362 strbuf_addstr(sb
, color_commit
);
363 strbuf_addstr(sb
, prefix
);
364 strbuf_addstr(sb
, color_reset
);
367 if (*tag
&& decoration
->type
== DECORATION_REF_TAG
) {
368 strbuf_addstr(sb
, color
);
369 strbuf_addstr(sb
, tag
);
370 strbuf_addstr(sb
, color_reset
);
373 strbuf_addstr(sb
, color
);
374 show_name(sb
, decoration
);
375 strbuf_addstr(sb
, color_reset
);
377 if (current_and_HEAD
&&
378 decoration
->type
== DECORATION_REF_HEAD
) {
379 strbuf_addstr(sb
, color_commit
);
380 strbuf_addstr(sb
, pointer
);
381 strbuf_addstr(sb
, color_reset
);
382 strbuf_addstr(sb
, decorate_get_color(use_color
, current_and_HEAD
->type
));
383 show_name(sb
, current_and_HEAD
);
384 strbuf_addstr(sb
, color_reset
);
389 decoration
= decoration
->next
;
392 strbuf_addstr(sb
, color_commit
);
393 strbuf_addstr(sb
, suffix
);
394 strbuf_addstr(sb
, color_reset
);
398 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
400 struct strbuf sb
= STRBUF_INIT
;
403 char **slot
= revision_sources_peek(opt
->sources
, commit
);
406 fprintf(opt
->diffopt
.file
, "\t%s", *slot
);
408 if (!opt
->show_decorations
)
410 format_decorations(&sb
, commit
, opt
->diffopt
.use_color
, NULL
);
411 fputs(sb
.buf
, opt
->diffopt
.file
);
415 void fmt_output_subject(struct strbuf
*filename
,
417 struct rev_info
*info
)
419 const char *suffix
= info
->patch_suffix
;
421 int start_len
= filename
->len
;
422 int max_len
= start_len
+ info
->patch_name_max
- (strlen(suffix
) + 1);
424 if (info
->reroll_count
) {
425 struct strbuf temp
= STRBUF_INIT
;
427 strbuf_addf(&temp
, "v%s", info
->reroll_count
);
428 format_sanitized_subject(filename
, temp
.buf
, temp
.len
);
429 strbuf_addstr(filename
, "-");
430 strbuf_release(&temp
);
432 strbuf_addf(filename
, "%04d-%s", nr
, subject
);
434 if (max_len
< filename
->len
)
435 strbuf_setlen(filename
, max_len
);
436 strbuf_addstr(filename
, suffix
);
439 void fmt_output_commit(struct strbuf
*filename
,
440 struct commit
*commit
,
441 struct rev_info
*info
)
443 struct pretty_print_context ctx
= {0};
444 struct strbuf subject
= STRBUF_INIT
;
446 repo_format_commit_message(the_repository
, commit
, "%f", &subject
,
448 fmt_output_subject(filename
, subject
.buf
, info
);
449 strbuf_release(&subject
);
452 void fmt_output_email_subject(struct strbuf
*sb
, struct rev_info
*opt
)
454 if (opt
->total
> 0) {
455 strbuf_addf(sb
, "Subject: [%s%s%0*d/%d] ",
457 *opt
->subject_prefix
? " " : "",
458 decimal_width(opt
->total
),
459 opt
->nr
, opt
->total
);
460 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
461 strbuf_addf(sb
, "Subject: [%s] ",
462 opt
->subject_prefix
);
464 strbuf_addstr(sb
, "Subject: ");
468 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
469 char **extra_headers_p
,
470 int *need_8bit_cte_p
,
473 struct strbuf headers
= STRBUF_INIT
;
474 const char *name
= oid_to_hex(opt
->zero_commit
?
475 null_oid() : &commit
->object
.oid
);
477 *need_8bit_cte_p
= 0; /* unknown */
479 if (opt
->extra_headers
&& *opt
->extra_headers
)
480 strbuf_addstr(&headers
, opt
->extra_headers
);
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 buffer
= STRBUF_INIT
;
499 struct strbuf filename
= STRBUF_INIT
;
500 *need_8bit_cte_p
= -1; /* NEVER */
502 strbuf_reset(&buffer
);
504 strbuf_addf(&headers
,
505 "MIME-Version: 1.0\n"
506 "Content-Type: multipart/mixed;"
507 " boundary=\"%s%s\"\n"
509 "This is a multi-part message in MIME "
512 "Content-Type: text/plain; "
513 "charset=UTF-8; format=fixed\n"
514 "Content-Transfer-Encoding: 8bit\n\n",
515 mime_boundary_leader
, opt
->mime_boundary
,
516 mime_boundary_leader
, opt
->mime_boundary
);
518 if (opt
->numbered_files
)
519 strbuf_addf(&filename
, "%d", opt
->nr
);
521 fmt_output_commit(&filename
, commit
, opt
);
524 "Content-Type: text/x-patch;"
526 "Content-Transfer-Encoding: 8bit\n"
527 "Content-Disposition: %s;"
528 " filename=\"%s\"\n\n",
529 mime_boundary_leader
, opt
->mime_boundary
,
531 opt
->no_inline
? "attachment" : "inline",
533 opt
->diffopt
.stat_sep
= buffer
.buf
;
534 strbuf_release(&filename
);
536 *extra_headers_p
= headers
.len
? strbuf_detach(&headers
, NULL
) : NULL
;
539 static void show_sig_lines(struct rev_info
*opt
, int status
, const char *bol
)
541 const char *color
, *reset
, *eol
;
543 color
= diff_get_color_opt(&opt
->diffopt
,
544 status
? DIFF_WHITESPACE
: DIFF_FRAGINFO
);
545 reset
= diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
);
547 eol
= strchrnul(bol
, '\n');
548 fprintf(opt
->diffopt
.file
, "%s%.*s%s%s", color
, (int)(eol
- bol
), bol
, reset
,
550 graph_show_oneline(opt
->graph
);
551 bol
= (*eol
) ? (eol
+ 1) : eol
;
555 static void show_signature(struct rev_info
*opt
, struct commit
*commit
)
557 struct strbuf payload
= STRBUF_INIT
;
558 struct strbuf signature
= STRBUF_INIT
;
559 struct signature_check sigc
= { 0 };
562 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
565 sigc
.payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
566 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
567 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
568 if (status
&& !sigc
.output
)
569 show_sig_lines(opt
, status
, "No signature\n");
571 show_sig_lines(opt
, status
, sigc
.output
);
572 signature_check_clear(&sigc
);
575 strbuf_release(&payload
);
576 strbuf_release(&signature
);
579 static int which_parent(const struct object_id
*oid
, const struct commit
*commit
)
582 const struct commit_list
*parent
;
584 for (nth
= 0, parent
= commit
->parents
; parent
; parent
= parent
->next
) {
585 if (oideq(&parent
->item
->object
.oid
, oid
))
592 static int is_common_merge(const struct commit
*commit
)
594 return (commit
->parents
595 && commit
->parents
->next
596 && !commit
->parents
->next
->next
);
599 static int show_one_mergetag(struct commit
*commit
,
600 struct commit_extra_header
*extra
,
603 struct rev_info
*opt
= (struct rev_info
*)data
;
604 struct object_id oid
;
606 struct strbuf verify_message
;
607 struct signature_check sigc
= { 0 };
609 struct strbuf payload
= STRBUF_INIT
;
610 struct strbuf signature
= STRBUF_INIT
;
612 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
614 tag
= lookup_tag(the_repository
, &oid
);
616 return -1; /* error message already given */
618 strbuf_init(&verify_message
, 256);
619 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
620 strbuf_addstr(&verify_message
, "malformed mergetag\n");
621 else if (is_common_merge(commit
) &&
622 oideq(&tag
->tagged
->oid
,
623 &commit
->parents
->next
->item
->object
.oid
))
624 strbuf_addf(&verify_message
,
625 "merged tag '%s'\n", tag
->tag
);
626 else if ((nth
= which_parent(&tag
->tagged
->oid
, commit
)) < 0)
627 strbuf_addf(&verify_message
, "tag %s names a non-parent %s\n",
628 tag
->tag
, oid_to_hex(&tag
->tagged
->oid
));
630 strbuf_addf(&verify_message
,
631 "parent #%d, tagged '%s'\n", nth
+ 1, tag
->tag
);
634 if (parse_signature(extra
->value
, extra
->len
, &payload
, &signature
)) {
635 /* could have a good signature */
636 sigc
.payload_type
= SIGNATURE_PAYLOAD_TAG
;
637 sigc
.payload
= strbuf_detach(&payload
, &sigc
.payload_len
);
638 status
= check_signature(&sigc
, signature
.buf
, signature
.len
);
640 strbuf_addstr(&verify_message
, sigc
.output
);
642 strbuf_addstr(&verify_message
, "No signature\n");
643 signature_check_clear(&sigc
);
644 /* otherwise we couldn't verify, which is shown as bad */
647 show_sig_lines(opt
, status
, verify_message
.buf
);
648 strbuf_release(&verify_message
);
649 strbuf_release(&payload
);
650 strbuf_release(&signature
);
654 static int show_mergetag(struct rev_info
*opt
, struct commit
*commit
)
656 return for_each_mergetag(show_one_mergetag
, commit
, opt
);
659 static void next_commentary_block(struct rev_info
*opt
, struct strbuf
*sb
)
661 const char *x
= opt
->shown_dashes
? "\n" : "---\n";
663 strbuf_addstr(sb
, x
);
665 fputs(x
, opt
->diffopt
.file
);
666 opt
->shown_dashes
= 1;
669 static void show_diff_of_diff(struct rev_info
*opt
)
671 if (!cmit_fmt_is_mail(opt
->commit_format
))
674 if (opt
->idiff_oid1
) {
675 struct diff_queue_struct dq
;
677 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
678 diff_queue_init(&diff_queued_diff
);
680 fprintf_ln(opt
->diffopt
.file
, "\n%s", opt
->idiff_title
);
681 show_interdiff(opt
->idiff_oid1
, opt
->idiff_oid2
, 2,
684 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
688 struct diff_queue_struct dq
;
689 struct diff_options opts
;
690 struct range_diff_options range_diff_opts
= {
691 .creation_factor
= opt
->creation_factor
,
696 memcpy(&dq
, &diff_queued_diff
, sizeof(diff_queued_diff
));
697 diff_queue_init(&diff_queued_diff
);
699 fprintf_ln(opt
->diffopt
.file
, "\n%s", opt
->rdiff_title
);
701 * Pass minimum required diff-options to range-diff; others
702 * can be added later if deemed desirable.
704 repo_diff_setup(the_repository
, &opts
);
705 opts
.file
= opt
->diffopt
.file
;
706 opts
.use_color
= opt
->diffopt
.use_color
;
707 diff_setup_done(&opts
);
708 show_range_diff(opt
->rdiff1
, opt
->rdiff2
, &range_diff_opts
);
710 memcpy(&diff_queued_diff
, &dq
, sizeof(diff_queued_diff
));
714 void show_log(struct rev_info
*opt
)
716 struct strbuf msgbuf
= STRBUF_INIT
;
717 struct log_info
*log
= opt
->loginfo
;
718 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
719 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: the_hash_algo
->hexsz
;
720 struct pretty_print_context ctx
= {0};
723 if (!opt
->verbose_header
) {
724 graph_show_commit(opt
->graph
);
727 put_revision_mark(opt
, commit
);
728 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev_commit
),
730 if (opt
->print_parents
)
731 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
732 if (opt
->children
.name
)
733 show_children(opt
, commit
, abbrev_commit
);
734 show_decorations(opt
, commit
);
735 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
736 putc('\n', opt
->diffopt
.file
);
737 graph_show_remainder(opt
->graph
);
739 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
744 * If use_terminator is set, we already handled any record termination
745 * at the end of the last record.
746 * Otherwise, add a diffopt.line_termination character before all
747 * entries but the first. (IOW, as a separator between entries)
749 if (opt
->shown_one
&& !opt
->use_terminator
) {
751 * If entries are separated by a newline, the output
752 * should look human-readable. If the last entry ended
753 * with a newline, print the graph output before this
754 * newline. Otherwise it will end up as a completely blank
755 * line and will look like a gap in the graph.
757 * If the entry separator is not a newline, the output is
758 * primarily intended for programmatic consumption, and we
759 * never want the extra graph output before the entry
762 if (opt
->diffopt
.line_termination
== '\n' &&
763 !opt
->missing_newline
)
764 graph_show_padding(opt
->graph
);
765 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
770 * If the history graph was requested,
771 * print the graph, up to this commit's line
773 graph_show_commit(opt
->graph
);
776 * Print header line of header..
779 if (cmit_fmt_is_mail(opt
->commit_format
)) {
780 log_write_email_headers(opt
, commit
, &ctx
.after_subject
,
781 &ctx
.need_8bit_cte
, 1);
783 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
784 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), opt
->diffopt
.file
);
785 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
786 fputs("commit ", opt
->diffopt
.file
);
789 put_revision_mark(opt
, commit
);
790 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
,
793 if (opt
->print_parents
)
794 show_parents(commit
, abbrev_commit
, opt
->diffopt
.file
);
795 if (opt
->children
.name
)
796 show_children(opt
, commit
, abbrev_commit
);
798 fprintf(opt
->diffopt
.file
, " (from %s)",
799 repo_find_unique_abbrev(the_repository
, &parent
->object
.oid
, abbrev_commit
));
800 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
), opt
->diffopt
.file
);
801 show_decorations(opt
, commit
);
802 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
803 putc(' ', opt
->diffopt
.file
);
805 putc('\n', opt
->diffopt
.file
);
806 graph_show_oneline(opt
->graph
);
808 if (opt
->reflog_info
) {
810 * setup_revisions() ensures that opt->reflog_info
811 * and opt->graph cannot both be set,
812 * so we don't need to worry about printing the
815 show_reflog_message(opt
->reflog_info
,
816 opt
->commit_format
== CMIT_FMT_ONELINE
,
818 opt
->date_mode_explicit
);
819 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
824 if (opt
->show_signature
) {
825 show_signature(opt
, commit
);
826 show_mergetag(opt
, commit
);
829 if (opt
->show_notes
) {
831 struct strbuf notebuf
= STRBUF_INIT
;
833 raw
= (opt
->commit_format
== CMIT_FMT_USERFORMAT
);
834 format_display_notes(&commit
->object
.oid
, ¬ebuf
,
835 get_log_output_encoding(), raw
);
836 ctx
.notes_message
= strbuf_detach(¬ebuf
, NULL
);
840 * And then the pretty-printed message itself
842 if (ctx
.need_8bit_cte
>= 0 && opt
->add_signoff
)
844 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT
));
845 ctx
.date_mode
= opt
->date_mode
;
846 ctx
.date_mode_explicit
= opt
->date_mode_explicit
;
847 ctx
.abbrev
= opt
->diffopt
.abbrev
;
848 ctx
.preserve_subject
= opt
->preserve_subject
;
849 ctx
.encode_email_headers
= opt
->encode_email_headers
;
850 ctx
.reflog_info
= opt
->reflog_info
;
851 ctx
.fmt
= opt
->commit_format
;
852 ctx
.mailmap
= opt
->mailmap
;
853 ctx
.color
= opt
->diffopt
.use_color
;
854 ctx
.expand_tabs_in_log
= opt
->expand_tabs_in_log
;
855 ctx
.output_encoding
= get_log_output_encoding();
857 if (opt
->from_ident
.mail_begin
&& opt
->from_ident
.name_begin
)
858 ctx
.from_ident
= &opt
->from_ident
;
860 ctx
.graph_width
= graph_width(opt
->graph
);
861 pretty_print_commit(&ctx
, commit
, &msgbuf
);
863 if (opt
->add_signoff
)
864 append_signoff(&msgbuf
, 0, APPEND_SIGNOFF_DEDUP
);
866 if ((ctx
.fmt
!= CMIT_FMT_USERFORMAT
) &&
867 ctx
.notes_message
&& *ctx
.notes_message
) {
868 if (cmit_fmt_is_mail(ctx
.fmt
))
869 next_commentary_block(opt
, &msgbuf
);
870 strbuf_addstr(&msgbuf
, ctx
.notes_message
);
873 if (opt
->show_log_size
) {
874 fprintf(opt
->diffopt
.file
, "log size %i\n", (int)msgbuf
.len
);
875 graph_show_oneline(opt
->graph
);
879 * Set opt->missing_newline if msgbuf doesn't
880 * end in a newline (including if it is empty)
882 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
883 opt
->missing_newline
= 1;
885 opt
->missing_newline
= 0;
887 graph_show_commit_msg(opt
->graph
, opt
->diffopt
.file
, &msgbuf
);
888 if (opt
->use_terminator
&& !commit_format_is_empty(opt
->commit_format
)) {
889 if (!opt
->missing_newline
)
890 graph_show_padding(opt
->graph
);
891 putc(opt
->diffopt
.line_termination
, opt
->diffopt
.file
);
894 strbuf_release(&msgbuf
);
895 free(ctx
.notes_message
);
896 free(ctx
.after_subject
);
899 int log_tree_diff_flush(struct rev_info
*opt
)
901 opt
->shown_dashes
= 0;
902 diffcore_std(&opt
->diffopt
);
904 if (diff_queue_is_empty(&opt
->diffopt
)) {
905 int saved_fmt
= opt
->diffopt
.output_format
;
906 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
907 diff_flush(&opt
->diffopt
);
908 opt
->diffopt
.output_format
= saved_fmt
;
912 if (opt
->loginfo
&& !opt
->no_commit_id
) {
914 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
915 opt
->verbose_header
&&
916 opt
->commit_format
!= CMIT_FMT_ONELINE
&&
917 !commit_format_is_empty(opt
->commit_format
)) {
919 * When showing a verbose header (i.e. log message),
920 * and not in --pretty=oneline format, we would want
921 * an extra newline between the end of log and the
922 * diff/diffstat output for readability.
924 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
925 fputs(diff_line_prefix(&opt
->diffopt
), opt
->diffopt
.file
);
928 * We may have shown three-dashes line early
929 * between generated commentary (notes, etc.)
930 * and the log message, in which case we only
931 * want a blank line after the commentary
932 * without (an extra) three-dashes line.
933 * Otherwise, we show the three-dashes line if
934 * we are showing the patch with diffstat, but
935 * in that case, there is no extra blank line
936 * after the three-dashes line.
938 if (!opt
->shown_dashes
&&
939 (pch
& opt
->diffopt
.output_format
) == pch
)
940 fprintf(opt
->diffopt
.file
, "---");
941 putc('\n', opt
->diffopt
.file
);
944 diff_flush(&opt
->diffopt
);
948 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
950 diff_tree_combined_merge(commit
, opt
);
951 return !opt
->loginfo
;
954 static void setup_additional_headers(struct diff_options
*o
,
955 struct strmap
*all_headers
)
957 struct hashmap_iter iter
;
958 struct strmap_entry
*entry
;
961 * Make o->additional_path_headers contain the subset of all_headers
962 * that match o->pathspec. If there aren't any that match o->pathspec,
963 * then make o->additional_path_headers be NULL.
966 if (!o
->pathspec
.nr
) {
967 o
->additional_path_headers
= all_headers
;
971 o
->additional_path_headers
= xmalloc(sizeof(struct strmap
));
972 strmap_init_with_options(o
->additional_path_headers
, NULL
, 0);
973 strmap_for_each_entry(all_headers
, &iter
, entry
) {
974 if (match_pathspec(the_repository
->index
, &o
->pathspec
,
975 entry
->key
, strlen(entry
->key
),
976 0 /* prefix */, NULL
/* seen */,
978 strmap_put(o
->additional_path_headers
,
979 entry
->key
, entry
->value
);
981 if (!strmap_get_size(o
->additional_path_headers
)) {
982 strmap_clear(o
->additional_path_headers
, 0);
983 FREE_AND_NULL(o
->additional_path_headers
);
987 static void cleanup_additional_headers(struct diff_options
*o
)
989 if (!o
->pathspec
.nr
) {
990 o
->additional_path_headers
= NULL
;
993 if (!o
->additional_path_headers
)
996 strmap_clear(o
->additional_path_headers
, 0);
997 FREE_AND_NULL(o
->additional_path_headers
);
1000 static int do_remerge_diff(struct rev_info
*opt
,
1001 struct commit_list
*parents
,
1002 struct object_id
*oid
)
1004 struct merge_options o
;
1005 struct commit_list
*bases
= NULL
;
1006 struct merge_result res
= {0};
1007 struct pretty_print_context ctx
= {0};
1008 struct commit
*parent1
= parents
->item
;
1009 struct commit
*parent2
= parents
->next
->item
;
1010 struct strbuf parent1_desc
= STRBUF_INIT
;
1011 struct strbuf parent2_desc
= STRBUF_INIT
;
1014 * Lazily prepare a temporary object directory and rotate it
1015 * into the alternative object store list as the primary.
1017 if (opt
->remerge_diff
&& !opt
->remerge_objdir
) {
1018 opt
->remerge_objdir
= tmp_objdir_create("remerge-diff");
1019 if (!opt
->remerge_objdir
)
1020 return error(_("unable to create temporary object directory"));
1021 tmp_objdir_replace_primary_odb(opt
->remerge_objdir
, 1);
1024 /* Setup merge options */
1025 init_ui_merge_options(&o
, the_repository
);
1026 o
.show_rename_progress
= 0;
1027 o
.record_conflict_msgs_as_headers
= 1;
1028 o
.msg_header_prefix
= "remerge";
1030 ctx
.abbrev
= DEFAULT_ABBREV
;
1031 repo_format_commit_message(the_repository
, parent1
, "%h (%s)",
1032 &parent1_desc
, &ctx
);
1033 repo_format_commit_message(the_repository
, parent2
, "%h (%s)",
1034 &parent2_desc
, &ctx
);
1035 o
.branch1
= parent1_desc
.buf
;
1036 o
.branch2
= parent2_desc
.buf
;
1038 /* Parse the relevant commits and get the merge bases */
1039 parse_commit_or_die(parent1
);
1040 parse_commit_or_die(parent2
);
1041 if (repo_get_merge_bases(the_repository
, parent1
, parent2
, &bases
) < 0)
1044 /* Re-merge the parents */
1045 merge_incore_recursive(&o
, bases
, parent1
, parent2
, &res
);
1048 setup_additional_headers(&opt
->diffopt
, res
.path_messages
);
1049 diff_tree_oid(&res
.tree
->object
.oid
, oid
, "", &opt
->diffopt
);
1050 log_tree_diff_flush(opt
);
1053 free_commit_list(bases
);
1054 cleanup_additional_headers(&opt
->diffopt
);
1055 strbuf_release(&parent1_desc
);
1056 strbuf_release(&parent2_desc
);
1057 merge_finalize(&o
, &res
);
1059 /* Clean up the contents of the temporary object directory */
1060 tmp_objdir_discard_objects(opt
->remerge_objdir
);
1062 return !opt
->loginfo
;
1066 * Show the diff of a commit.
1068 * Return true if we printed any log info messages
1070 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
1073 struct commit_list
*parents
;
1074 struct object_id
*oid
;
1076 int all_need_diff
= opt
->diff
|| opt
->diffopt
.flags
.exit_with_status
;
1078 if (!all_need_diff
&& !opt
->merges_need_diff
)
1081 parse_commit_or_die(commit
);
1082 oid
= get_commit_tree_oid(commit
);
1084 parents
= get_saved_parents(opt
, commit
);
1085 is_merge
= parents
&& parents
->next
;
1086 if (!is_merge
&& !all_need_diff
)
1091 if (opt
->show_root_diff
) {
1092 diff_root_tree_oid(oid
, "", &opt
->diffopt
);
1093 log_tree_diff_flush(opt
);
1095 return !opt
->loginfo
;
1099 int octopus
= (parents
->next
->next
!= NULL
);
1101 if (opt
->remerge_diff
) {
1104 fprintf(opt
->diffopt
.file
,
1105 "diff: warning: Skipping remerge-diff "
1106 "for octopus merges.\n");
1109 return do_remerge_diff(opt
, parents
, oid
);
1111 if (opt
->combine_merges
)
1112 return do_diff_combined(opt
, commit
);
1113 if (opt
->separate_merges
) {
1114 if (!opt
->first_parent_merges
) {
1115 /* Show parent info for multiple diffs */
1116 log
->parent
= parents
->item
;
1124 struct commit
*parent
= parents
->item
;
1126 parse_commit_or_die(parent
);
1127 diff_tree_oid(get_commit_tree_oid(parent
),
1128 oid
, "", &opt
->diffopt
);
1129 log_tree_diff_flush(opt
);
1131 showed_log
|= !opt
->loginfo
;
1133 /* Set up the log info for the next parent, if any.. */
1134 parents
= parents
->next
;
1135 if (!parents
|| opt
->first_parent_merges
)
1137 log
->parent
= parents
->item
;
1143 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
1145 struct log_info log
;
1147 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1148 int no_free
= opt
->diffopt
.no_free
;
1150 log
.commit
= commit
;
1152 opt
->loginfo
= &log
;
1153 opt
->diffopt
.no_free
= 1;
1155 /* NEEDSWORK: no restoring of no_free? Why? */
1156 if (opt
->line_level_traverse
)
1157 return line_log_print(opt
, commit
);
1159 if (opt
->track_linear
&& !opt
->linear
&& !opt
->reverse_output_stage
)
1160 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1161 shown
= log_tree_diff(opt
, commit
, &log
);
1162 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
1167 if (opt
->track_linear
&& !opt
->linear
&& opt
->reverse_output_stage
)
1168 fprintf(opt
->diffopt
.file
, "\n%s\n", opt
->break_bar
);
1170 show_diff_of_diff(opt
);
1171 opt
->loginfo
= NULL
;
1172 maybe_flush_or_die(opt
->diffopt
.file
, "stdout");
1173 opt
->diffopt
.no_free
= no_free
;
1175 diff_free(&opt
->diffopt
);