2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
10 #include "environment.h"
14 #include "object-file.h"
15 #include "object-name.h"
16 #include "object-store-ll.h"
21 #include "diff-merges.h"
25 #include "oid-array.h"
27 #include "reflog-walk.h"
28 #include "patch-ids.h"
31 #include "string-list.h"
32 #include "parse-options.h"
35 #include "streaming.h"
39 #include "commit-slab.h"
40 #include "repository.h"
41 #include "commit-reach.h"
42 #include "range-diff.h"
43 #include "tmp-objdir.h"
45 #include "write-or-die.h"
47 #define MAIL_DEFAULT_WRAP 72
48 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
49 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
51 static unsigned int force_in_body_from
;
52 static int stdout_mboxrd
;
53 static int format_no_prefix
;
55 static const char * const builtin_log_usage
[] = {
56 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
57 N_("git show [<options>] <object>..."),
61 struct line_opt_callback_data
{
64 struct string_list args
;
67 static int session_is_interactive(void)
69 return isatty(1) || pager_in_use();
72 static int auto_decoration_style(void)
74 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
77 static int parse_decoration_style(const char *value
)
79 switch (git_parse_maybe_bool(value
)) {
81 return DECORATE_SHORT_REFS
;
87 if (!strcmp(value
, "full"))
88 return DECORATE_FULL_REFS
;
89 else if (!strcmp(value
, "short"))
90 return DECORATE_SHORT_REFS
;
91 else if (!strcmp(value
, "auto"))
92 return auto_decoration_style();
94 * Please update _git_log() in git-completion.bash when you
95 * add new decoration styles.
101 int default_abbrev_commit
;
102 int default_show_root
;
104 int default_show_signature
;
105 int default_encode_email_headers
;
106 int decoration_style
;
107 int decoration_given
;
108 int use_mailmap_config
;
109 char *fmt_patch_subject_prefix
;
110 int fmt_patch_name_max
;
112 char *default_date_mode
;
115 static void log_config_init(struct log_config
*cfg
)
117 memset(cfg
, 0, sizeof(*cfg
));
118 cfg
->default_show_root
= 1;
119 cfg
->default_encode_email_headers
= 1;
120 cfg
->use_mailmap_config
= 1;
121 cfg
->fmt_patch_subject_prefix
= xstrdup("PATCH");
122 cfg
->fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
123 cfg
->decoration_style
= auto_decoration_style();
126 static void log_config_release(struct log_config
*cfg
)
128 free(cfg
->default_date_mode
);
129 free(cfg
->fmt_pretty
);
130 free(cfg
->fmt_patch_subject_prefix
);
133 static int use_default_decoration_filter
= 1;
134 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
135 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
136 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
138 static int clear_decorations_callback(const struct option
*opt UNUSED
,
139 const char *arg
, int unset
)
141 BUG_ON_OPT_NEG(unset
);
143 string_list_clear(&decorate_refs_include
, 0);
144 string_list_clear(&decorate_refs_exclude
, 0);
145 use_default_decoration_filter
= 0;
149 static int decorate_callback(const struct option
*opt
, const char *arg
,
152 struct log_config
*cfg
= opt
->value
;
155 cfg
->decoration_style
= 0;
157 cfg
->decoration_style
= parse_decoration_style(arg
);
159 cfg
->decoration_style
= DECORATE_SHORT_REFS
;
161 if (cfg
->decoration_style
< 0)
162 die(_("invalid --decorate option: %s"), arg
);
164 cfg
->decoration_given
= 1;
169 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
171 struct line_opt_callback_data
*data
= option
->value
;
173 BUG_ON_OPT_NEG(unset
);
178 data
->rev
->line_level_traverse
= 1;
179 string_list_append(&data
->args
, arg
);
184 static void cmd_log_init_defaults(struct rev_info
*rev
,
185 struct log_config
*cfg
)
188 get_commit_format(cfg
->fmt_pretty
, rev
);
189 if (cfg
->default_follow
)
190 rev
->diffopt
.flags
.default_follow_renames
= 1;
191 rev
->verbose_header
= 1;
192 init_diffstat_widths(&rev
->diffopt
);
193 rev
->diffopt
.flags
.recursive
= 1;
194 rev
->diffopt
.flags
.allow_textconv
= 1;
195 rev
->abbrev_commit
= cfg
->default_abbrev_commit
;
196 rev
->show_root_diff
= cfg
->default_show_root
;
197 rev
->subject_prefix
= cfg
->fmt_patch_subject_prefix
;
198 rev
->patch_name_max
= cfg
->fmt_patch_name_max
;
199 rev
->show_signature
= cfg
->default_show_signature
;
200 rev
->encode_email_headers
= cfg
->default_encode_email_headers
;
202 if (cfg
->default_date_mode
)
203 parse_date_format(cfg
->default_date_mode
, &rev
->date_mode
);
206 static void set_default_decoration_filter(struct decoration_filter
*decoration_filter
)
210 struct string_list
*include
= decoration_filter
->include_ref_pattern
;
211 const struct string_list
*config_exclude
;
213 if (!git_config_get_string_multi("log.excludeDecoration",
215 struct string_list_item
*item
;
216 for_each_string_list_item(item
, config_exclude
)
217 string_list_append(decoration_filter
->exclude_ref_config_pattern
,
222 * By default, decorate_all is disabled. Enable it if
223 * log.initialDecorationSet=all. Don't ever disable it by config,
224 * since the command-line takes precedent.
226 if (use_default_decoration_filter
&&
227 !git_config_get_string("log.initialdecorationset", &value
) &&
228 !strcmp("all", value
))
229 use_default_decoration_filter
= 0;
232 if (!use_default_decoration_filter
||
233 decoration_filter
->exclude_ref_pattern
->nr
||
234 decoration_filter
->include_ref_pattern
->nr
||
235 decoration_filter
->exclude_ref_config_pattern
->nr
)
239 * No command-line or config options were given, so
240 * populate with sensible defaults.
242 for (i
= 0; i
< ARRAY_SIZE(ref_namespace
); i
++) {
243 if (!ref_namespace
[i
].decoration
)
246 string_list_append(include
, ref_namespace
[i
].ref
);
250 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
251 struct rev_info
*rev
, struct setup_revision_opt
*opt
,
252 struct log_config
*cfg
)
254 struct userformat_want w
;
255 int quiet
= 0, source
= 0, mailmap
;
256 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
257 struct decoration_filter decoration_filter
= {
258 .exclude_ref_pattern
= &decorate_refs_exclude
,
259 .include_ref_pattern
= &decorate_refs_include
,
260 .exclude_ref_config_pattern
= &decorate_refs_exclude_config
,
262 static struct revision_sources revision_sources
;
264 const struct option builtin_log_options
[] = {
265 OPT__QUIET(&quiet
, N_("suppress diff output")),
266 OPT_BOOL(0, "source", &source
, N_("show source")),
267 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
268 OPT_ALIAS(0, "mailmap", "use-mailmap"),
269 OPT_CALLBACK_F(0, "clear-decorations", NULL
, NULL
,
270 N_("clear all previously-defined decoration filters"),
271 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
272 clear_decorations_callback
),
273 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
274 N_("pattern"), N_("only decorate refs that match <pattern>")),
275 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
276 N_("pattern"), N_("do not decorate refs that match <pattern>")),
277 OPT_CALLBACK_F(0, "decorate", cfg
, NULL
, N_("decorate options"),
278 PARSE_OPT_OPTARG
, decorate_callback
),
279 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
280 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
281 log_line_range_callback
),
286 line_cb
.prefix
= prefix
;
288 mailmap
= cfg
->use_mailmap_config
;
289 argc
= parse_options(argc
, argv
, prefix
,
290 builtin_log_options
, builtin_log_usage
,
291 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
292 PARSE_OPT_KEEP_DASHDASH
);
295 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
296 argc
= setup_revisions(argc
, argv
, rev
, opt
);
298 /* Any arguments at this point are not recognized */
300 die(_("unrecognized argument: %s"), argv
[1]);
302 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
303 die(_("-L<range>:<file> cannot be used with pathspec"));
305 memset(&w
, 0, sizeof(w
));
306 userformat_find_requirements(NULL
, &w
);
308 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
311 load_display_notes(&rev
->notes_opt
);
313 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
314 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
315 rev
->always_show_header
= 0;
317 if (source
|| w
.source
) {
318 init_revision_sources(&revision_sources
);
319 rev
->sources
= &revision_sources
;
323 rev
->mailmap
= xmalloc(sizeof(struct string_list
));
324 string_list_init_nodup(rev
->mailmap
);
325 read_mailmap(rev
->mailmap
);
328 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
330 * "log --pretty=raw" is special; ignore UI oriented
331 * configuration variables such as decoration.
333 if (!cfg
->decoration_given
)
334 cfg
->decoration_style
= 0;
335 if (!rev
->abbrev_commit_given
)
336 rev
->abbrev_commit
= 0;
339 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
342 * Disable decoration loading if the format will not
345 cfg
->decoration_style
= 0;
346 } else if (!cfg
->decoration_style
) {
348 * If we are going to show them, make sure we do load
349 * them here, but taking care not to override a
350 * specific style set by config or --decorate.
352 cfg
->decoration_style
= DECORATE_SHORT_REFS
;
356 if (cfg
->decoration_style
|| rev
->simplify_by_decoration
) {
357 set_default_decoration_filter(&decoration_filter
);
359 if (cfg
->decoration_style
)
360 rev
->show_decorations
= 1;
362 load_ref_decorations(&decoration_filter
, cfg
->decoration_style
);
365 if (rev
->line_level_traverse
)
366 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
371 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
372 struct rev_info
*rev
, struct setup_revision_opt
*opt
,
373 struct log_config
*cfg
)
375 cmd_log_init_defaults(rev
, cfg
);
376 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
, cfg
);
380 * This gives a rough estimate for how many commits we
381 * will print out in the list.
383 static int estimate_commit_count(struct commit_list
*list
)
388 struct commit
*commit
= list
->item
;
389 unsigned int flags
= commit
->object
.flags
;
391 if (!(flags
& (TREESAME
| UNINTERESTING
)))
397 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
399 if (rev
->shown_one
) {
401 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
402 putchar(rev
->diffopt
.line_termination
);
404 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
407 static struct itimerval early_output_timer
;
409 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
411 int i
= revs
->early_output
;
413 int no_free
= revs
->diffopt
.no_free
;
415 revs
->diffopt
.no_free
= 0;
416 sort_in_topological_order(&list
, revs
->sort_order
);
418 struct commit
*commit
= list
->item
;
419 switch (simplify_commit(revs
, commit
)) {
422 int n
= estimate_commit_count(list
);
423 show_early_header(revs
, "incomplete", n
);
426 log_tree_commit(revs
, commit
);
432 revs
->diffopt
.no_free
= no_free
;
433 diff_free(&revs
->diffopt
);
439 /* Did we already get enough commits for the early output? */
441 revs
->diffopt
.no_free
= 0;
442 diff_free(&revs
->diffopt
);
447 * ..if no, then repeat it twice a second until we
450 * NOTE! We don't use "it_interval", because if the
451 * reader isn't listening, we want our output to be
452 * throttled by the writing, and not have the timer
453 * trigger every second even if we're blocked on a
456 early_output_timer
.it_value
.tv_sec
= 0;
457 early_output_timer
.it_value
.tv_usec
= 500000;
458 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
461 static void early_output(int signal UNUSED
)
463 show_early_output
= log_show_early
;
466 static void setup_early_output(void)
471 * Set up the signal handler, minimally intrusively:
472 * we only set a single volatile integer word (not
473 * using sigatomic_t - trying to avoid unnecessary
474 * system dependencies and headers), and using
477 memset(&sa
, 0, sizeof(sa
));
478 sa
.sa_handler
= early_output
;
479 sigemptyset(&sa
.sa_mask
);
480 sa
.sa_flags
= SA_RESTART
;
481 sigaction(SIGALRM
, &sa
, NULL
);
484 * If we can get the whole output in less than a
485 * tenth of a second, don't even bother doing the
486 * early-output thing..
488 * This is a one-time-only trigger.
490 early_output_timer
.it_value
.tv_sec
= 0;
491 early_output_timer
.it_value
.tv_usec
= 100000;
492 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
495 static void finish_early_output(struct rev_info
*rev
)
497 int n
= estimate_commit_count(rev
->commits
);
498 signal(SIGALRM
, SIG_IGN
);
499 show_early_header(rev
, "done", n
);
502 static int cmd_log_walk_no_free(struct rev_info
*rev
)
504 struct commit
*commit
;
508 if (rev
->remerge_diff
) {
509 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
510 if (!rev
->remerge_objdir
)
511 die(_("unable to create temporary object directory"));
512 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
515 if (rev
->early_output
)
516 setup_early_output();
518 if (prepare_revision_walk(rev
))
519 die(_("revision walk setup failed"));
521 if (rev
->early_output
)
522 finish_early_output(rev
);
525 * For --check and --exit-code, the exit code is based on CHECK_FAILED
526 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
527 * retain that state information if replacing rev->diffopt in this loop
529 while ((commit
= get_revision(rev
)) != NULL
) {
530 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
532 * We decremented max_count in get_revision,
533 * but we didn't actually show the commit.
536 if (!rev
->reflog_info
) {
538 * We may show a given commit multiple times when
539 * walking the reflogs.
541 free_commit_buffer(the_repository
->parsed_objects
,
543 free_commit_list(commit
->parents
);
544 commit
->parents
= NULL
;
546 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
547 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
548 if (rev
->diffopt
.degraded_cc_to_c
)
551 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
552 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
554 if (rev
->remerge_diff
) {
555 tmp_objdir_destroy(rev
->remerge_objdir
);
556 rev
->remerge_objdir
= NULL
;
559 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
560 rev
->diffopt
.flags
.check_failed
) {
563 return diff_result_code(&rev
->diffopt
);
566 static int cmd_log_walk(struct rev_info
*rev
)
570 rev
->diffopt
.no_free
= 1;
571 retval
= cmd_log_walk_no_free(rev
);
572 rev
->diffopt
.no_free
= 0;
573 diff_free(&rev
->diffopt
);
577 static int git_log_config(const char *var
, const char *value
,
578 const struct config_context
*ctx
, void *cb
)
580 struct log_config
*cfg
= cb
;
581 const char *slot_name
;
583 if (!strcmp(var
, "format.pretty")) {
584 FREE_AND_NULL(cfg
->fmt_pretty
);
585 return git_config_string(&cfg
->fmt_pretty
, var
, value
);
587 if (!strcmp(var
, "format.subjectprefix")) {
588 FREE_AND_NULL(cfg
->fmt_patch_subject_prefix
);
589 return git_config_string(&cfg
->fmt_patch_subject_prefix
, var
, value
);
591 if (!strcmp(var
, "format.filenamemaxlength")) {
592 cfg
->fmt_patch_name_max
= git_config_int(var
, value
, ctx
->kvi
);
595 if (!strcmp(var
, "format.encodeemailheaders")) {
596 cfg
->default_encode_email_headers
= git_config_bool(var
, value
);
599 if (!strcmp(var
, "log.abbrevcommit")) {
600 cfg
->default_abbrev_commit
= git_config_bool(var
, value
);
603 if (!strcmp(var
, "log.date")) {
604 FREE_AND_NULL(cfg
->default_date_mode
);
605 return git_config_string(&cfg
->default_date_mode
, var
, value
);
607 if (!strcmp(var
, "log.decorate")) {
608 cfg
->decoration_style
= parse_decoration_style(value
);
609 if (cfg
->decoration_style
< 0)
610 cfg
->decoration_style
= 0; /* maybe warn? */
613 if (!strcmp(var
, "log.diffmerges")) {
615 return config_error_nonbool(var
);
616 return diff_merges_config(value
);
618 if (!strcmp(var
, "log.showroot")) {
619 cfg
->default_show_root
= git_config_bool(var
, value
);
622 if (!strcmp(var
, "log.follow")) {
623 cfg
->default_follow
= git_config_bool(var
, value
);
626 if (skip_prefix(var
, "color.decorate.", &slot_name
))
627 return parse_decorate_color_config(var
, slot_name
, value
);
628 if (!strcmp(var
, "log.mailmap")) {
629 cfg
->use_mailmap_config
= git_config_bool(var
, value
);
632 if (!strcmp(var
, "log.showsignature")) {
633 cfg
->default_show_signature
= git_config_bool(var
, value
);
637 return git_diff_ui_config(var
, value
, ctx
, cb
);
640 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
642 struct log_config cfg
;
644 struct setup_revision_opt opt
;
647 log_config_init(&cfg
);
648 init_diff_ui_defaults();
649 git_config(git_log_config
, &cfg
);
651 repo_init_revisions(the_repository
, &rev
, prefix
);
652 git_config(grep_config
, &rev
.grep_filter
);
655 rev
.simplify_history
= 0;
656 memset(&opt
, 0, sizeof(opt
));
658 opt
.revarg_opt
= REVARG_COMMITTISH
;
659 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
660 if (!rev
.diffopt
.output_format
)
661 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
663 ret
= cmd_log_walk(&rev
);
665 release_revisions(&rev
);
666 log_config_release(&cfg
);
670 static void show_tagger(const char *buf
, struct rev_info
*rev
)
672 struct strbuf out
= STRBUF_INIT
;
673 struct pretty_print_context pp
= {0};
675 pp
.fmt
= rev
->commit_format
;
676 pp
.date_mode
= rev
->date_mode
;
677 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
678 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
679 strbuf_release(&out
);
682 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
684 struct object_id oidc
;
685 struct object_context obj_context
= {0};
689 fflush(rev
->diffopt
.file
);
690 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
691 !rev
->diffopt
.flags
.allow_textconv
)
692 return stream_blob_to_fd(1, oid
, NULL
, 0);
694 if (get_oid_with_context(the_repository
, obj_name
,
696 &oidc
, &obj_context
))
697 die(_("not a valid object name %s"), obj_name
);
698 if (!obj_context
.path
||
699 !textconv_object(the_repository
, obj_context
.path
,
700 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
701 object_context_release(&obj_context
);
702 return stream_blob_to_fd(1, oid
, NULL
, 0);
706 die(_("git show %s: bad file"), obj_name
);
708 write_or_die(1, buf
, size
);
709 object_context_release(&obj_context
);
714 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
717 enum object_type type
;
718 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
722 return error(_("could not read object %s"), oid_to_hex(oid
));
724 assert(type
== OBJ_TAG
);
725 while (offset
< size
&& buf
[offset
] != '\n') {
726 int new_offset
= offset
+ 1;
728 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
730 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
731 show_tagger(ident
, rev
);
736 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
741 static int show_tree_object(const struct object_id
*oid UNUSED
,
742 struct strbuf
*base UNUSED
,
743 const char *pathname
, unsigned mode
,
746 FILE *file
= context
;
747 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
751 static void show_setup_revisions_tweak(struct rev_info
*rev
)
753 if (rev
->first_parent_only
)
754 diff_merges_default_to_first_parent(rev
);
756 diff_merges_default_to_dense_combined(rev
);
757 if (!rev
->diffopt
.output_format
)
758 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
761 int cmd_show(int argc
, const char **argv
, const char *prefix
)
763 struct log_config cfg
;
766 struct setup_revision_opt opt
;
767 struct pathspec match_all
;
770 log_config_init(&cfg
);
771 init_diff_ui_defaults();
772 git_config(git_log_config
, &cfg
);
774 if (the_repository
->gitdir
) {
775 prepare_repo_settings(the_repository
);
776 the_repository
->settings
.command_requires_full_index
= 0;
779 memset(&match_all
, 0, sizeof(match_all
));
780 repo_init_revisions(the_repository
, &rev
, prefix
);
781 git_config(grep_config
, &rev
.grep_filter
);
784 rev
.always_show_header
= 1;
786 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
788 memset(&opt
, 0, sizeof(opt
));
790 opt
.tweak
= show_setup_revisions_tweak
;
791 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
794 ret
= cmd_log_walk(&rev
);
795 release_revisions(&rev
);
796 log_config_release(&cfg
);
800 rev
.diffopt
.no_free
= 1;
801 for (i
= 0; i
< rev
.pending
.nr
&& !ret
; i
++) {
802 struct object
*o
= rev
.pending
.objects
[i
].item
;
803 const char *name
= rev
.pending
.objects
[i
].name
;
806 ret
= show_blob_object(&o
->oid
, &rev
, name
);
809 struct tag
*t
= (struct tag
*)o
;
810 struct object_id
*oid
= get_tagged_oid(t
);
814 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
815 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
817 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
818 ret
= show_tag_object(&o
->oid
, &rev
);
822 o
= parse_object(the_repository
, oid
);
824 ret
= error(_("could not read object %s"),
826 rev
.pending
.objects
[i
].item
= o
;
833 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
834 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
836 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
837 read_tree(the_repository
, (struct tree
*)o
,
838 &match_all
, show_tree_object
,
844 struct object_array old
;
845 struct object_array blank
= OBJECT_ARRAY_INIT
;
847 memcpy(&old
, &rev
.pending
, sizeof(old
));
848 memcpy(&rev
.pending
, &blank
, sizeof(rev
.pending
));
850 add_object_array(o
, name
, &rev
.pending
);
851 ret
= cmd_log_walk_no_free(&rev
);
855 * object_array_clear(&pending). It was
856 * cleared already in prepare_revision_walk()
858 memcpy(&rev
.pending
, &old
, sizeof(rev
.pending
));
862 ret
= error(_("unknown type: %d"), o
->type
);
866 rev
.diffopt
.no_free
= 0;
867 diff_free(&rev
.diffopt
);
868 release_revisions(&rev
);
869 log_config_release(&cfg
);
875 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
877 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
879 struct log_config cfg
;
881 struct setup_revision_opt opt
;
884 log_config_init(&cfg
);
885 init_diff_ui_defaults();
886 git_config(git_log_config
, &cfg
);
888 repo_init_revisions(the_repository
, &rev
, prefix
);
889 init_reflog_walk(&rev
.reflog_info
);
890 git_config(grep_config
, &rev
.grep_filter
);
892 rev
.verbose_header
= 1;
893 memset(&opt
, 0, sizeof(opt
));
895 cmd_log_init_defaults(&rev
, &cfg
);
896 rev
.abbrev_commit
= 1;
897 rev
.commit_format
= CMIT_FMT_ONELINE
;
898 rev
.use_terminator
= 1;
899 rev
.always_show_header
= 1;
900 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
902 ret
= cmd_log_walk(&rev
);
904 release_revisions(&rev
);
905 log_config_release(&cfg
);
909 static void log_setup_revisions_tweak(struct rev_info
*rev
)
911 if (rev
->diffopt
.flags
.default_follow_renames
&&
912 diff_check_follow_pathspec(&rev
->prune_data
, 0))
913 rev
->diffopt
.flags
.follow_renames
= 1;
915 if (rev
->first_parent_only
)
916 diff_merges_default_to_first_parent(rev
);
919 int cmd_log(int argc
, const char **argv
, const char *prefix
)
921 struct log_config cfg
;
923 struct setup_revision_opt opt
;
926 log_config_init(&cfg
);
927 init_diff_ui_defaults();
928 git_config(git_log_config
, &cfg
);
930 repo_init_revisions(the_repository
, &rev
, prefix
);
931 git_config(grep_config
, &rev
.grep_filter
);
933 rev
.always_show_header
= 1;
934 memset(&opt
, 0, sizeof(opt
));
936 opt
.revarg_opt
= REVARG_COMMITTISH
;
937 opt
.tweak
= log_setup_revisions_tweak
;
938 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
, &cfg
);
940 ret
= cmd_log_walk(&rev
);
942 release_revisions(&rev
);
943 log_config_release(&cfg
);
962 enum cover_from_description
{
969 enum auto_base_setting
{
975 struct format_config
{
976 struct log_config log
;
977 enum thread_level thread
;
979 enum auto_base_setting auto_base
;
983 char *signature_file
;
984 enum cover_setting config_cover_letter
;
985 char *config_output_directory
;
986 enum cover_from_description cover_from_description_mode
;
988 struct display_notes_opt notes_opt
;
989 int numbered_cmdline_opt
;
992 char *default_attach
;
993 struct string_list extra_hdr
;
994 struct string_list extra_to
;
995 struct string_list extra_cc
;
998 struct strbuf sprefix
;
999 char *fmt_patch_suffix
;
1002 static void format_config_init(struct format_config
*cfg
)
1004 memset(cfg
, 0, sizeof(*cfg
));
1005 log_config_init(&cfg
->log
);
1006 cfg
->cover_from_description_mode
= COVER_FROM_MESSAGE
;
1007 cfg
->auto_number
= 1;
1008 string_list_init_dup(&cfg
->extra_hdr
);
1009 string_list_init_dup(&cfg
->extra_to
);
1010 string_list_init_dup(&cfg
->extra_cc
);
1011 strbuf_init(&cfg
->sprefix
, 0);
1012 cfg
->fmt_patch_suffix
= xstrdup(".patch");
1015 static void format_config_release(struct format_config
*cfg
)
1017 log_config_release(&cfg
->log
);
1018 free(cfg
->base_commit
);
1020 free(cfg
->signature
);
1021 free(cfg
->signature_file
);
1022 free(cfg
->config_output_directory
);
1023 free(cfg
->default_attach
);
1024 string_list_clear(&cfg
->extra_hdr
, 0);
1025 string_list_clear(&cfg
->extra_to
, 0);
1026 string_list_clear(&cfg
->extra_cc
, 0);
1027 strbuf_release(&cfg
->sprefix
);
1028 free(cfg
->fmt_patch_suffix
);
1031 static enum cover_from_description
parse_cover_from_description(const char *arg
)
1033 if (!arg
|| !strcmp(arg
, "default"))
1034 return COVER_FROM_MESSAGE
;
1035 else if (!strcmp(arg
, "none"))
1036 return COVER_FROM_NONE
;
1037 else if (!strcmp(arg
, "message"))
1038 return COVER_FROM_MESSAGE
;
1039 else if (!strcmp(arg
, "subject"))
1040 return COVER_FROM_SUBJECT
;
1041 else if (!strcmp(arg
, "auto"))
1042 return COVER_FROM_AUTO
;
1044 die(_("%s: invalid cover from description mode"), arg
);
1047 static void add_header(struct format_config
*cfg
, const char *value
)
1049 struct string_list_item
*item
;
1050 int len
= strlen(value
);
1051 while (len
&& value
[len
- 1] == '\n')
1054 if (!strncasecmp(value
, "to: ", 4)) {
1055 item
= string_list_append(&cfg
->extra_to
, value
+ 4);
1057 } else if (!strncasecmp(value
, "cc: ", 4)) {
1058 item
= string_list_append(&cfg
->extra_cc
, value
+ 4);
1061 item
= string_list_append(&cfg
->extra_hdr
, value
);
1064 item
->string
[len
] = '\0';
1067 static int git_format_config(const char *var
, const char *value
,
1068 const struct config_context
*ctx
, void *cb
)
1070 struct format_config
*cfg
= cb
;
1072 if (!strcmp(var
, "format.headers")) {
1074 die(_("format.headers without value"));
1075 add_header(cfg
, value
);
1078 if (!strcmp(var
, "format.suffix")) {
1079 FREE_AND_NULL(cfg
->fmt_patch_suffix
);
1080 return git_config_string(&cfg
->fmt_patch_suffix
, var
, value
);
1082 if (!strcmp(var
, "format.to")) {
1084 return config_error_nonbool(var
);
1085 string_list_append(&cfg
->extra_to
, value
);
1088 if (!strcmp(var
, "format.cc")) {
1090 return config_error_nonbool(var
);
1091 string_list_append(&cfg
->extra_cc
, value
);
1094 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
1095 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
1098 if (!strcmp(var
, "format.numbered")) {
1099 if (value
&& !strcasecmp(value
, "auto")) {
1100 cfg
->auto_number
= 1;
1103 cfg
->numbered
= git_config_bool(var
, value
);
1104 cfg
->auto_number
= cfg
->auto_number
&& cfg
->numbered
;
1107 if (!strcmp(var
, "format.attach")) {
1108 if (value
&& *value
) {
1109 FREE_AND_NULL(cfg
->default_attach
);
1110 cfg
->default_attach
= xstrdup(value
);
1111 } else if (value
&& !*value
) {
1112 FREE_AND_NULL(cfg
->default_attach
);
1114 FREE_AND_NULL(cfg
->default_attach
);
1115 cfg
->default_attach
= xstrdup(git_version_string
);
1119 if (!strcmp(var
, "format.thread")) {
1120 if (value
&& !strcasecmp(value
, "deep")) {
1121 cfg
->thread
= THREAD_DEEP
;
1124 if (value
&& !strcasecmp(value
, "shallow")) {
1125 cfg
->thread
= THREAD_SHALLOW
;
1128 cfg
->thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
1131 if (!strcmp(var
, "format.signoff")) {
1132 cfg
->do_signoff
= git_config_bool(var
, value
);
1135 if (!strcmp(var
, "format.signature")) {
1136 FREE_AND_NULL(cfg
->signature
);
1137 return git_config_string(&cfg
->signature
, var
, value
);
1139 if (!strcmp(var
, "format.signaturefile")) {
1140 FREE_AND_NULL(cfg
->signature_file
);
1141 return git_config_pathname(&cfg
->signature_file
, var
, value
);
1143 if (!strcmp(var
, "format.coverletter")) {
1144 if (value
&& !strcasecmp(value
, "auto")) {
1145 cfg
->config_cover_letter
= COVER_AUTO
;
1148 cfg
->config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
1151 if (!strcmp(var
, "format.outputdirectory")) {
1152 FREE_AND_NULL(cfg
->config_output_directory
);
1153 return git_config_string(&cfg
->config_output_directory
, var
, value
);
1155 if (!strcmp(var
, "format.useautobase")) {
1156 if (value
&& !strcasecmp(value
, "whenAble")) {
1157 cfg
->auto_base
= AUTO_BASE_WHEN_ABLE
;
1160 cfg
->auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
1163 if (!strcmp(var
, "format.from")) {
1164 int b
= git_parse_maybe_bool(value
);
1165 FREE_AND_NULL(cfg
->from
);
1167 cfg
->from
= xstrdup(value
);
1169 cfg
->from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1172 if (!strcmp(var
, "format.forceinbodyfrom")) {
1173 force_in_body_from
= git_config_bool(var
, value
);
1176 if (!strcmp(var
, "format.notes")) {
1177 int b
= git_parse_maybe_bool(value
);
1179 enable_ref_display_notes(&cfg
->notes_opt
, &cfg
->show_notes
, value
);
1181 enable_default_display_notes(&cfg
->notes_opt
, &cfg
->show_notes
);
1183 disable_display_notes(&cfg
->notes_opt
, &cfg
->show_notes
);
1186 if (!strcmp(var
, "format.coverfromdescription")) {
1187 cfg
->cover_from_description_mode
= parse_cover_from_description(value
);
1190 if (!strcmp(var
, "format.mboxrd")) {
1191 stdout_mboxrd
= git_config_bool(var
, value
);
1194 if (!strcmp(var
, "format.noprefix")) {
1195 format_no_prefix
= 1;
1200 * ignore some porcelain config which would otherwise be parsed by
1201 * git_diff_ui_config(), via git_log_config(); we can't just avoid
1202 * diff_ui_config completely, because we do care about some ui options
1205 if (!strcmp(var
, "diff.noprefix"))
1208 return git_log_config(var
, value
, ctx
, &cfg
->log
);
1211 static const char *output_directory
= NULL
;
1212 static int outdir_offset
;
1214 static int open_next_file(struct commit
*commit
, const char *subject
,
1215 struct rev_info
*rev
, int quiet
)
1217 struct strbuf filename
= STRBUF_INIT
;
1219 if (output_directory
) {
1220 strbuf_addstr(&filename
, output_directory
);
1221 strbuf_complete(&filename
, '/');
1224 if (rev
->numbered_files
)
1225 strbuf_addf(&filename
, "%d", rev
->nr
);
1227 fmt_output_commit(&filename
, commit
, rev
);
1229 fmt_output_subject(&filename
, subject
, rev
);
1232 printf("%s\n", filename
.buf
+ outdir_offset
);
1234 if (!(rev
->diffopt
.file
= fopen(filename
.buf
, "w"))) {
1235 error_errno(_("cannot open patch file %s"), filename
.buf
);
1236 strbuf_release(&filename
);
1240 strbuf_release(&filename
);
1244 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1246 struct rev_info check_rev
;
1247 struct commit
*commit
, *c1
, *c2
;
1248 struct object
*o1
, *o2
;
1249 unsigned flags1
, flags2
;
1251 if (rev
->pending
.nr
!= 2)
1252 die(_("need exactly one range"));
1254 o1
= rev
->pending
.objects
[0].item
;
1255 o2
= rev
->pending
.objects
[1].item
;
1258 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1259 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1261 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1262 die(_("not a range"));
1264 init_patch_ids(the_repository
, ids
);
1266 /* given a range a..b get all patch ids for b..a */
1267 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1268 check_rev
.max_parents
= 1;
1269 o1
->flags
^= UNINTERESTING
;
1270 o2
->flags
^= UNINTERESTING
;
1271 add_pending_object(&check_rev
, o1
, "o1");
1272 add_pending_object(&check_rev
, o2
, "o2");
1273 if (prepare_revision_walk(&check_rev
))
1274 die(_("revision walk setup failed"));
1276 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1277 add_commit_patch_id(commit
, ids
);
1280 /* reset for next revision walk */
1281 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1282 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1287 static void gen_message_id(struct rev_info
*info
, const char *base
)
1289 struct strbuf buf
= STRBUF_INIT
;
1290 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1291 (timestamp_t
) time(NULL
),
1292 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1293 info
->message_id
= strbuf_detach(&buf
, NULL
);
1296 static void print_signature(const char *signature
, FILE *file
)
1298 if (!signature
|| !*signature
)
1301 fprintf(file
, "-- \n%s", signature
);
1302 if (signature
[strlen(signature
)-1] != '\n')
1307 static char *find_branch_name(struct rev_info
*rev
)
1309 int i
, positive
= -1;
1310 struct object_id branch_oid
;
1311 const struct object_id
*tip_oid
;
1312 const char *ref
, *v
;
1313 char *full_ref
, *branch
= NULL
;
1315 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1316 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1325 ref
= rev
->cmdline
.rev
[positive
].name
;
1326 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1327 if (repo_dwim_ref(the_repository
, ref
, strlen(ref
), &branch_oid
,
1329 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1330 oideq(tip_oid
, &branch_oid
))
1331 branch
= xstrdup(v
);
1336 static void show_diffstat(struct rev_info
*rev
,
1337 struct commit
*origin
, struct commit
*head
)
1339 struct diff_options opts
;
1341 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1342 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1343 diff_setup_done(&opts
);
1345 diff_tree_oid(get_commit_tree_oid(origin
),
1346 get_commit_tree_oid(head
),
1348 diffcore_std(&opts
);
1351 fprintf(rev
->diffopt
.file
, "\n");
1354 static void read_desc_file(struct strbuf
*buf
, const char *desc_file
)
1356 if (strbuf_read_file(buf
, desc_file
, 0) < 0)
1357 die_errno(_("unable to read branch description file '%s'"),
1361 static void prepare_cover_text(struct pretty_print_context
*pp
,
1362 const char *description_file
,
1363 const char *branch_name
,
1365 const char *encoding
,
1367 const struct format_config
*cfg
)
1369 const char *subject
= "*** SUBJECT HERE ***";
1370 const char *body
= "*** BLURB HERE ***";
1371 struct strbuf description_sb
= STRBUF_INIT
;
1372 struct strbuf subject_sb
= STRBUF_INIT
;
1374 if (cfg
->cover_from_description_mode
== COVER_FROM_NONE
)
1377 if (description_file
&& *description_file
)
1378 read_desc_file(&description_sb
, description_file
);
1379 else if (branch_name
&& *branch_name
)
1380 read_branch_desc(&description_sb
, branch_name
);
1381 if (!description_sb
.len
)
1384 if (cfg
->cover_from_description_mode
== COVER_FROM_SUBJECT
||
1385 cfg
->cover_from_description_mode
== COVER_FROM_AUTO
)
1386 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1388 if (cfg
->cover_from_description_mode
== COVER_FROM_MESSAGE
||
1389 (cfg
->cover_from_description_mode
== COVER_FROM_AUTO
&&
1390 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1391 body
= description_sb
.buf
;
1393 subject
= subject_sb
.buf
;
1396 pp_email_subject(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1397 pp_remainder(pp
, &body
, sb
, 0);
1399 strbuf_release(&description_sb
);
1400 strbuf_release(&subject_sb
);
1403 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1405 strvec_pushf(arg
, "--notes=%s", item
->string
);
1409 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1411 if (!rev
->show_notes
) {
1412 strvec_push(arg
, "--no-notes");
1413 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1414 (rev
->notes_opt
.use_default_notes
== -1 &&
1415 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1416 strvec_push(arg
, "--notes");
1418 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1422 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1423 struct commit
*origin
,
1424 int nr
, struct commit
**list
,
1425 const char *description_file
,
1426 const char *branch_name
,
1428 const struct format_config
*cfg
)
1430 const char *committer
;
1431 struct shortlog log
;
1432 struct strbuf sb
= STRBUF_INIT
;
1434 const char *encoding
= "UTF-8";
1435 int need_8bit_cte
= 0;
1436 struct pretty_print_context pp
= {0};
1437 struct commit
*head
= list
[0];
1438 char *to_free
= NULL
;
1440 if (!cmit_fmt_is_mail(rev
->commit_format
))
1441 die(_("cover letter needs email format"));
1443 committer
= git_committer_info(0);
1445 if (use_separate_file
&&
1446 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1447 die(_("failed to create cover-letter file"));
1449 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1451 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1452 const char *buf
= repo_get_commit_buffer(the_repository
,
1454 if (has_non_ascii(buf
))
1456 repo_unuse_commit_buffer(the_repository
, list
[i
], buf
);
1460 branch_name
= to_free
= find_branch_name(rev
);
1462 pp
.fmt
= CMIT_FMT_EMAIL
;
1463 pp
.date_mode
.type
= DATE_RFC2822
;
1465 pp
.encode_email_headers
= rev
->encode_email_headers
;
1466 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1467 prepare_cover_text(&pp
, description_file
, branch_name
, &sb
,
1468 encoding
, need_8bit_cte
, cfg
);
1469 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1472 free(pp
.after_subject
);
1473 strbuf_release(&sb
);
1475 shortlog_init(&log
);
1477 log
.wrap
= MAIL_DEFAULT_WRAP
;
1480 log
.file
= rev
->diffopt
.file
;
1481 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1482 shortlog_finish_setup(&log
);
1483 for (i
= 0; i
< nr
; i
++)
1484 shortlog_add_commit(&log
, list
[i
]);
1486 shortlog_output(&log
);
1488 /* We can only do diffstat with a unique reference point */
1490 show_diffstat(rev
, origin
, head
);
1492 if (rev
->idiff_oid1
) {
1493 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1494 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1500 * Pass minimum required diff-options to range-diff; others
1501 * can be added later if deemed desirable.
1503 struct diff_options opts
;
1504 struct strvec other_arg
= STRVEC_INIT
;
1505 struct range_diff_options range_diff_opts
= {
1506 .creation_factor
= rev
->creation_factor
,
1509 .other_arg
= &other_arg
1512 repo_diff_setup(the_repository
, &opts
);
1513 opts
.file
= rev
->diffopt
.file
;
1514 opts
.use_color
= rev
->diffopt
.use_color
;
1515 diff_setup_done(&opts
);
1516 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1517 get_notes_args(&other_arg
, rev
);
1518 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1519 strvec_clear(&other_arg
);
1523 static char *clean_message_id(const char *msg_id
)
1526 const char *a
, *z
, *m
;
1529 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1534 if (!isspace(ch
) && (ch
!= '>'))
1539 die(_("insane in-reply-to: %s"), msg_id
);
1542 return xmemdupz(a
, z
- a
);
1545 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1547 if (output_directory
&& is_absolute_path(output_directory
))
1548 return output_directory
;
1550 if (!prefix
|| !*prefix
) {
1551 if (output_directory
)
1552 return output_directory
;
1553 /* The user did not explicitly ask for "./" */
1558 outdir_offset
= strlen(prefix
);
1559 if (!output_directory
)
1562 return prefix_filename(prefix
, output_directory
);
1565 static const char * const builtin_format_patch_usage
[] = {
1566 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1570 struct keep_callback_data
{
1571 struct format_config
*cfg
;
1572 struct rev_info
*revs
;
1575 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1577 struct keep_callback_data
*data
= opt
->value
;
1578 BUG_ON_OPT_NEG(unset
);
1579 BUG_ON_OPT_ARG(arg
);
1580 data
->revs
->total
= -1;
1581 data
->cfg
->keep_subject
= 1;
1585 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1588 struct format_config
*cfg
= opt
->value
;
1590 BUG_ON_OPT_NEG(unset
);
1591 cfg
->subject_prefix
= 1;
1592 strbuf_reset(&cfg
->sprefix
);
1593 strbuf_addstr(&cfg
->sprefix
, arg
);
1597 static int rfc_callback(const struct option
*opt
, const char *arg
,
1600 const char **rfc
= opt
->value
;
1606 *rfc
= arg
? arg
: "RFC";
1610 static int numbered_callback(const struct option
*opt
, const char *arg
,
1613 struct format_config
*cfg
= opt
->value
;
1614 BUG_ON_OPT_ARG(arg
);
1615 cfg
->numbered
= cfg
->numbered_cmdline_opt
= unset
? 0 : 1;
1617 cfg
->auto_number
= 0;
1621 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1624 BUG_ON_OPT_NEG(unset
);
1625 return numbered_callback(opt
, arg
, 1);
1628 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1631 const char **dir
= (const char **)opt
->value
;
1632 BUG_ON_OPT_NEG(unset
);
1634 die(_("two output directories?"));
1639 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1641 struct format_config
*cfg
= opt
->value
;
1644 cfg
->thread
= THREAD_UNSET
;
1645 else if (!arg
|| !strcmp(arg
, "shallow"))
1646 cfg
->thread
= THREAD_SHALLOW
;
1647 else if (!strcmp(arg
, "deep"))
1648 cfg
->thread
= THREAD_DEEP
;
1650 * Please update _git_formatpatch() in git-completion.bash
1651 * when you add new options.
1658 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1660 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1662 rev
->mime_boundary
= NULL
;
1664 rev
->mime_boundary
= arg
;
1666 rev
->mime_boundary
= git_version_string
;
1667 rev
->no_inline
= unset
? 0 : 1;
1671 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1673 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1675 rev
->mime_boundary
= NULL
;
1677 rev
->mime_boundary
= arg
;
1679 rev
->mime_boundary
= git_version_string
;
1684 static int header_callback(const struct option
*opt
, const char *arg
,
1687 struct format_config
*cfg
= opt
->value
;
1690 string_list_clear(&cfg
->extra_hdr
, 0);
1691 string_list_clear(&cfg
->extra_to
, 0);
1692 string_list_clear(&cfg
->extra_cc
, 0);
1694 add_header(cfg
, arg
);
1699 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1701 char **from
= opt
->value
;
1708 *from
= xstrdup(arg
);
1710 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1714 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1716 struct format_config
*cfg
= opt
->value
;
1719 cfg
->auto_base
= AUTO_BASE_NEVER
;
1720 FREE_AND_NULL(cfg
->base_commit
);
1721 } else if (!strcmp(arg
, "auto")) {
1722 cfg
->auto_base
= AUTO_BASE_ALWAYS
;
1723 FREE_AND_NULL(cfg
->base_commit
);
1725 cfg
->auto_base
= AUTO_BASE_NEVER
;
1726 cfg
->base_commit
= xstrdup(arg
);
1731 struct base_tree_info
{
1732 struct object_id base_commit
;
1733 int nr_patch_id
, alloc_patch_id
;
1734 struct object_id
*patch_id
;
1737 static struct commit
*get_base_commit(const struct format_config
*cfg
,
1738 struct commit
**list
,
1741 struct commit
*base
= NULL
;
1742 struct commit
**rev
;
1743 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
, ret
;
1745 switch (cfg
->auto_base
) {
1746 case AUTO_BASE_NEVER
:
1747 if (cfg
->base_commit
) {
1751 /* no base information is requested */
1755 case AUTO_BASE_ALWAYS
:
1756 case AUTO_BASE_WHEN_ABLE
:
1757 if (cfg
->base_commit
) {
1758 BUG("requested automatic base selection but a commit was provided");
1761 die_on_failure
= cfg
->auto_base
== AUTO_BASE_ALWAYS
;
1765 BUG("unexpected automatic base selection method");
1769 base
= lookup_commit_reference_by_name(cfg
->base_commit
);
1771 die(_("unknown commit %s"), cfg
->base_commit
);
1773 struct branch
*curr_branch
= branch_get(NULL
);
1774 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1776 struct commit_list
*base_list
= NULL
;
1777 struct commit
*commit
;
1778 struct object_id oid
;
1780 if (repo_get_oid(the_repository
, upstream
, &oid
)) {
1782 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1786 commit
= lookup_commit_or_die(&oid
, "upstream base");
1787 if (repo_get_merge_bases_many(the_repository
,
1791 /* There should be one and only one merge base. */
1792 !base_list
|| base_list
->next
) {
1793 if (die_on_failure
) {
1794 die(_("could not find exact merge base"));
1796 free_commit_list(base_list
);
1800 base
= base_list
->item
;
1801 free_commit_list(base_list
);
1804 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1805 "please use git branch --set-upstream-to to track a remote branch.\n"
1806 "Or you could specify base commit by --base=<base-commit-id> manually"));
1812 ALLOC_ARRAY(rev
, total
);
1813 for (i
= 0; i
< total
; i
++)
1818 * Get merge base through pair-wise computations
1819 * and store it in rev[0].
1821 while (rev_nr
> 1) {
1822 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1823 struct commit_list
*merge_base
= NULL
;
1824 if (repo_get_merge_bases(the_repository
,
1826 rev
[2 * i
+ 1], &merge_base
) < 0 ||
1827 !merge_base
|| merge_base
->next
) {
1828 if (die_on_failure
) {
1829 die(_("failed to find exact merge base"));
1831 free_commit_list(merge_base
);
1837 rev
[i
] = merge_base
->item
;
1838 free_commit_list(merge_base
);
1842 rev
[i
] = rev
[2 * i
];
1843 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1846 ret
= repo_in_merge_bases(the_repository
, base
, rev
[0]);
1850 if (die_on_failure
) {
1851 die(_("base commit should be the ancestor of revision list"));
1858 for (i
= 0; i
< total
; i
++) {
1859 if (base
== list
[i
]) {
1860 if (die_on_failure
) {
1861 die(_("base commit shouldn't be in revision list"));
1873 define_commit_slab(commit_base
, int);
1875 static void prepare_bases(struct base_tree_info
*bases
,
1876 struct commit
*base
,
1877 struct commit
**list
,
1880 struct commit
*commit
;
1881 struct rev_info revs
;
1882 struct diff_options diffopt
;
1883 struct commit_base commit_base
;
1889 init_commit_base(&commit_base
);
1890 repo_diff_setup(the_repository
, &diffopt
);
1891 diffopt
.flags
.recursive
= 1;
1892 diff_setup_done(&diffopt
);
1894 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1896 repo_init_revisions(the_repository
, &revs
, NULL
);
1897 revs
.max_parents
= 1;
1898 revs
.topo_order
= 1;
1899 for (i
= 0; i
< total
; i
++) {
1900 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1901 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1902 *commit_base_at(&commit_base
, list
[i
]) = 1;
1904 base
->object
.flags
|= UNINTERESTING
;
1905 add_pending_object(&revs
, &base
->object
, "base");
1907 if (prepare_revision_walk(&revs
))
1908 die(_("revision walk setup failed"));
1910 * Traverse the commits list, get prerequisite patch ids
1911 * and stuff them in bases structure.
1913 while ((commit
= get_revision(&revs
)) != NULL
) {
1914 struct object_id oid
;
1915 struct object_id
*patch_id
;
1916 if (*commit_base_at(&commit_base
, commit
))
1918 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1919 die(_("cannot get patch id"));
1920 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1921 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1922 oidcpy(patch_id
, &oid
);
1923 bases
->nr_patch_id
++;
1925 clear_commit_base(&commit_base
);
1928 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1932 /* Only do this once, either for the cover or for the first one */
1933 if (is_null_oid(&bases
->base_commit
))
1936 /* Show the base commit */
1937 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1939 /* Show the prerequisite patches */
1940 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1941 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1943 free(bases
->patch_id
);
1944 bases
->nr_patch_id
= 0;
1945 bases
->alloc_patch_id
= 0;
1946 oidclr(&bases
->base_commit
, the_repository
->hash_algo
);
1949 static const char *diff_title(struct strbuf
*sb
,
1950 const char *reroll_count
,
1951 const char *generic
,
1952 const char *rerolled
)
1956 /* RFC may be v0, so allow -v1 to diff against v0 */
1957 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1959 strbuf_addf(sb
, rerolled
, v
- 1);
1961 strbuf_addstr(sb
, generic
);
1965 static void infer_range_diff_ranges(struct strbuf
*r1
,
1968 struct commit
*origin
,
1969 struct commit
*head
)
1971 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1972 int prev_is_range
= is_range_diff_range(prev
);
1975 strbuf_addstr(r1
, prev
);
1977 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1980 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1981 else if (prev_is_range
)
1982 die(_("failed to infer range-diff origin of current series"));
1984 warning(_("using '%s' as range-diff origin of current series"), prev
);
1985 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1989 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1991 struct format_config cfg
;
1992 struct commit
*commit
;
1993 struct commit
**list
= NULL
;
1994 struct rev_info rev
;
1995 char *to_free
= NULL
;
1996 struct setup_revision_opt s_r_opt
;
1997 int nr
= 0, total
, i
;
1999 int start_number
= -1;
2000 int just_numbers
= 0;
2001 int ignore_if_in_upstream
= 0;
2002 int cover_letter
= -1;
2003 int boundary_count
= 0;
2004 int no_binary_diff
= 0;
2005 int zero_commit
= 0;
2006 struct commit
*origin
= NULL
;
2007 const char *in_reply_to
= NULL
;
2008 struct patch_ids ids
;
2009 struct strbuf buf
= STRBUF_INIT
;
2010 int use_patch_format
= 0;
2012 const char *reroll_count
= NULL
;
2013 char *cover_from_description_arg
= NULL
;
2014 char *description_file
= NULL
;
2015 char *branch_name
= NULL
;
2016 struct base_tree_info bases
;
2017 struct commit
*base
;
2018 int show_progress
= 0;
2019 struct progress
*progress
= NULL
;
2020 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
2021 struct strbuf idiff_title
= STRBUF_INIT
;
2022 const char *rdiff_prev
= NULL
;
2023 struct strbuf rdiff1
= STRBUF_INIT
;
2024 struct strbuf rdiff2
= STRBUF_INIT
;
2025 struct strbuf rdiff_title
= STRBUF_INIT
;
2026 const char *rfc
= NULL
;
2027 int creation_factor
= -1;
2028 const char *signature
= git_version_string
;
2029 char *signature_to_free
= NULL
;
2030 char *signature_file_arg
= NULL
;
2031 struct keep_callback_data keep_callback_data
= {
2035 const char *fmt_patch_suffix
= NULL
;
2037 const struct option builtin_format_patch_options
[] = {
2038 OPT_CALLBACK_F('n', "numbered", &cfg
, NULL
,
2039 N_("use [PATCH n/m] even with a single patch"),
2040 PARSE_OPT_NOARG
, numbered_callback
),
2041 OPT_CALLBACK_F('N', "no-numbered", &cfg
, NULL
,
2042 N_("use [PATCH] even with multiple patches"),
2043 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
2044 OPT_BOOL('s', "signoff", &cfg
.do_signoff
, N_("add a Signed-off-by trailer")),
2045 OPT_BOOL(0, "stdout", &use_stdout
,
2046 N_("print patches to standard out")),
2047 OPT_BOOL(0, "cover-letter", &cover_letter
,
2048 N_("generate a cover letter")),
2049 OPT_BOOL(0, "numbered-files", &just_numbers
,
2050 N_("use simple number sequence for output file names")),
2051 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
2052 N_("use <sfx> instead of '.patch'")),
2053 OPT_INTEGER(0, "start-number", &start_number
,
2054 N_("start numbering patches at <n> instead of 1")),
2055 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
2056 N_("mark the series as Nth re-roll")),
2057 OPT_INTEGER(0, "filename-max-length", &cfg
.log
.fmt_patch_name_max
,
2058 N_("max length of output filename")),
2059 OPT_CALLBACK_F(0, "rfc", &rfc
, N_("rfc"),
2060 N_("add <rfc> (default 'RFC') before 'PATCH'"),
2061 PARSE_OPT_OPTARG
, rfc_callback
),
2062 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
2063 N_("cover-from-description-mode"),
2064 N_("generate parts of a cover letter based on a branch's description")),
2065 OPT_FILENAME(0, "description-file", &description_file
,
2066 N_("use branch description from file")),
2067 OPT_CALLBACK_F(0, "subject-prefix", &cfg
, N_("prefix"),
2068 N_("use [<prefix>] instead of [PATCH]"),
2069 PARSE_OPT_NONEG
, subject_prefix_callback
),
2070 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
2071 N_("dir"), N_("store resulting files in <dir>"),
2072 PARSE_OPT_NONEG
, output_directory_callback
),
2073 OPT_CALLBACK_F('k', "keep-subject", &keep_callback_data
, NULL
,
2074 N_("don't strip/add [PATCH]"),
2075 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
2076 OPT_BOOL(0, "no-binary", &no_binary_diff
,
2077 N_("don't output binary diffs")),
2078 OPT_BOOL(0, "zero-commit", &zero_commit
,
2079 N_("output all-zero hash in From header")),
2080 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
2081 N_("don't include a patch matching a commit upstream")),
2082 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
2083 N_("show patch format instead of default (patch + stat)"),
2084 1, PARSE_OPT_NONEG
),
2085 OPT_GROUP(N_("Messaging")),
2086 OPT_CALLBACK(0, "add-header", &cfg
, N_("header"),
2087 N_("add email header"), header_callback
),
2088 OPT_STRING_LIST(0, "to", &cfg
.extra_to
, N_("email"), N_("add To: header")),
2089 OPT_STRING_LIST(0, "cc", &cfg
.extra_cc
, N_("email"), N_("add Cc: header")),
2090 OPT_CALLBACK_F(0, "from", &cfg
.from
, N_("ident"),
2091 N_("set From address to <ident> (or committer ident if absent)"),
2092 PARSE_OPT_OPTARG
, from_callback
),
2093 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
2094 N_("make first mail a reply to <message-id>")),
2095 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
2096 N_("attach the patch"), PARSE_OPT_OPTARG
,
2098 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
2099 N_("inline the patch"),
2100 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
2102 OPT_CALLBACK_F(0, "thread", &cfg
, N_("style"),
2103 N_("enable message threading, styles: shallow, deep"),
2104 PARSE_OPT_OPTARG
, thread_callback
),
2105 OPT_STRING(0, "signature", &signature
, N_("signature"),
2106 N_("add a signature")),
2107 OPT_CALLBACK_F(0, "base", &cfg
, N_("base-commit"),
2108 N_("add prerequisite tree info to the patch series"),
2110 OPT_FILENAME(0, "signature-file", &signature_file_arg
,
2111 N_("add a signature from a file")),
2112 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
2113 OPT_BOOL(0, "progress", &show_progress
,
2114 N_("show progress while generating patches")),
2115 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
2116 N_("show changes against <rev> in cover letter or single patch"),
2117 parse_opt_object_name
),
2118 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
2119 N_("show changes against <refspec> in cover letter or single patch")),
2120 OPT_INTEGER(0, "creation-factor", &creation_factor
,
2121 N_("percentage by which creation is weighted")),
2122 OPT_BOOL(0, "force-in-body-from", &force_in_body_from
,
2123 N_("show in-body From: even if identical to the e-mail header")),
2127 format_config_init(&cfg
);
2128 init_diff_ui_defaults();
2129 init_display_notes(&cfg
.notes_opt
);
2130 git_config(git_format_config
, &cfg
);
2131 repo_init_revisions(the_repository
, &rev
, prefix
);
2132 git_config(grep_config
, &rev
.grep_filter
);
2134 rev
.show_notes
= cfg
.show_notes
;
2135 memcpy(&rev
.notes_opt
, &cfg
.notes_opt
, sizeof(cfg
.notes_opt
));
2136 rev
.commit_format
= CMIT_FMT_EMAIL
;
2137 rev
.encode_email_headers
= cfg
.log
.default_encode_email_headers
;
2138 rev
.expand_tabs_in_log_default
= 0;
2139 rev
.verbose_header
= 1;
2141 rev
.max_parents
= 1;
2142 rev
.diffopt
.flags
.recursive
= 1;
2143 rev
.diffopt
.no_free
= 1;
2144 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
2145 s_r_opt
.def
= "HEAD";
2146 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
2148 strbuf_addstr(&cfg
.sprefix
, cfg
.log
.fmt_patch_subject_prefix
);
2149 if (format_no_prefix
)
2150 diff_set_noprefix(&rev
.diffopt
);
2152 if (cfg
.default_attach
) {
2153 rev
.mime_boundary
= cfg
.default_attach
;
2158 * Parse the arguments before setup_revisions(), or something
2159 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2160 * possibly a valid SHA1.
2162 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
2163 builtin_format_patch_usage
,
2164 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
2165 PARSE_OPT_KEEP_DASHDASH
);
2167 rev
.force_in_body_from
= force_in_body_from
;
2169 if (!fmt_patch_suffix
)
2170 fmt_patch_suffix
= cfg
.fmt_patch_suffix
;
2172 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
2173 if (cfg
.log
.fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
2174 cfg
.log
.fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
2176 if (cover_from_description_arg
)
2177 cfg
.cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
2179 if (rfc
&& rfc
[0]) {
2180 cfg
.subject_prefix
= 1;
2182 strbuf_addf(&cfg
.sprefix
, " %s", rfc
+ 1);
2184 strbuf_insertf(&cfg
.sprefix
, 0, "%s ", rfc
);
2188 strbuf_addf(&cfg
.sprefix
, " v%s", reroll_count
);
2189 rev
.reroll_count
= reroll_count
;
2192 rev
.subject_prefix
= cfg
.sprefix
.buf
;
2194 for (i
= 0; i
< cfg
.extra_hdr
.nr
; i
++) {
2195 strbuf_addstr(&buf
, cfg
.extra_hdr
.items
[i
].string
);
2196 strbuf_addch(&buf
, '\n');
2199 if (cfg
.extra_to
.nr
)
2200 strbuf_addstr(&buf
, "To: ");
2201 for (i
= 0; i
< cfg
.extra_to
.nr
; i
++) {
2203 strbuf_addstr(&buf
, " ");
2204 strbuf_addstr(&buf
, cfg
.extra_to
.items
[i
].string
);
2205 if (i
+ 1 < cfg
.extra_to
.nr
)
2206 strbuf_addch(&buf
, ',');
2207 strbuf_addch(&buf
, '\n');
2210 if (cfg
.extra_cc
.nr
)
2211 strbuf_addstr(&buf
, "Cc: ");
2212 for (i
= 0; i
< cfg
.extra_cc
.nr
; i
++) {
2214 strbuf_addstr(&buf
, " ");
2215 strbuf_addstr(&buf
, cfg
.extra_cc
.items
[i
].string
);
2216 if (i
+ 1 < cfg
.extra_cc
.nr
)
2217 strbuf_addch(&buf
, ',');
2218 strbuf_addch(&buf
, '\n');
2221 rev
.extra_headers
= to_free
= strbuf_detach(&buf
, NULL
);
2224 if (split_ident_line(&rev
.from_ident
, cfg
.from
, strlen(cfg
.from
)))
2225 die(_("invalid ident line: %s"), cfg
.from
);
2228 if (start_number
< 0)
2232 * If numbered is set solely due to format.numbered in config,
2233 * and it would conflict with --keep-subject (-k) from the
2234 * command line, reset "numbered".
2236 if (cfg
.numbered
&& cfg
.keep_subject
&& !cfg
.numbered_cmdline_opt
)
2239 if (cfg
.numbered
&& cfg
.keep_subject
)
2240 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
2241 if (cfg
.keep_subject
&& cfg
.subject_prefix
)
2242 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
2243 rev
.preserve_subject
= cfg
.keep_subject
;
2245 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
2247 die(_("unrecognized argument: %s"), argv
[1]);
2249 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
2250 die(_("--name-only does not make sense"));
2251 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
2252 die(_("--name-status does not make sense"));
2253 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
2254 die(_("--check does not make sense"));
2255 if (rev
.remerge_diff
)
2256 die(_("--remerge-diff does not make sense"));
2258 if (!use_patch_format
&&
2259 (!rev
.diffopt
.output_format
||
2260 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2261 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2262 if (!rev
.diffopt
.stat_width
)
2263 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2265 /* Always generate a patch */
2266 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2267 rev
.always_show_header
= 1;
2269 rev
.zero_commit
= zero_commit
;
2270 rev
.patch_name_max
= cfg
.log
.fmt_patch_name_max
;
2272 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2273 rev
.diffopt
.flags
.binary
= 1;
2276 load_display_notes(&rev
.notes_opt
);
2278 die_for_incompatible_opt3(use_stdout
, "--stdout",
2279 rev
.diffopt
.close_file
, "--output",
2280 !!output_directory
, "--output-directory");
2282 if (use_stdout
&& stdout_mboxrd
)
2283 rev
.commit_format
= CMIT_FMT_MBOXRD
;
2287 } else if (!rev
.diffopt
.close_file
) {
2290 if (!output_directory
)
2291 output_directory
= cfg
.config_output_directory
;
2292 output_directory
= set_outdir(prefix
, output_directory
);
2294 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2295 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2297 * We consider <outdir> as 'outside of gitdir', therefore avoid
2298 * applying adjust_shared_perm in s-c-l-d.
2300 saved
= get_shared_repository();
2301 set_shared_repository(0);
2302 switch (safe_create_leading_directories_const(output_directory
)) {
2307 die(_("could not create leading directories "
2308 "of '%s'"), output_directory
);
2310 set_shared_repository(saved
);
2311 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2312 die_errno(_("could not create directory '%s'"),
2316 if (rev
.pending
.nr
== 1) {
2319 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2321 * This is traditional behaviour of "git format-patch
2322 * origin" that prepares what the origin side still
2325 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2326 add_head_to_pending(&rev
);
2330 * Otherwise, it is "format-patch -22 HEAD", and/or
2331 * "format-patch --root HEAD". The user wants
2332 * get_revision() to do the usual traversal.
2335 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2339 const char *ref
, *v
;
2340 ref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2342 RESOLVE_REF_READING
,
2344 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2345 branch_name
= xstrdup(v
);
2347 branch_name
= xstrdup(""); /* no branch */
2352 * We cannot move this anywhere earlier because we do want to
2353 * know if --root was given explicitly from the command line.
2355 rev
.show_root_diff
= 1;
2357 if (ignore_if_in_upstream
) {
2358 /* Don't say anything if head and upstream are the same. */
2359 if (rev
.pending
.nr
== 2) {
2360 struct object_array_entry
*o
= rev
.pending
.objects
;
2361 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2364 get_patch_ids(&rev
, &ids
);
2367 if (prepare_revision_walk(&rev
))
2368 die(_("revision walk setup failed"));
2370 while ((commit
= get_revision(&rev
)) != NULL
) {
2371 if (commit
->object
.flags
& BOUNDARY
) {
2373 origin
= (boundary_count
== 1) ? commit
: NULL
;
2377 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2381 REALLOC_ARRAY(list
, nr
);
2382 list
[nr
- 1] = commit
;
2388 if (cover_letter
== -1) {
2389 if (cfg
.config_cover_letter
== COVER_AUTO
)
2390 cover_letter
= (total
> 1);
2391 else if ((idiff_prev
.nr
|| rdiff_prev
) && (total
> 1))
2392 cover_letter
= (cfg
.config_cover_letter
!= COVER_OFF
);
2394 cover_letter
= (cfg
.config_cover_letter
== COVER_ON
);
2396 if (!cfg
.keep_subject
&& cfg
.auto_number
&& (total
> 1 || cover_letter
))
2399 rev
.total
= total
+ start_number
- 1;
2401 if (idiff_prev
.nr
) {
2402 if (!cover_letter
&& total
!= 1)
2403 die(_("--interdiff requires --cover-letter or single patch"));
2404 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2405 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2406 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2408 _("Interdiff against v%d:"));
2411 if (creation_factor
< 0)
2412 creation_factor
= CREATION_FACTOR_FOR_THE_SAME_SERIES
;
2413 else if (!rdiff_prev
)
2414 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2417 if (!cover_letter
&& total
!= 1)
2418 die(_("--range-diff requires --cover-letter or single patch"));
2420 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2422 rev
.rdiff1
= rdiff1
.buf
;
2423 rev
.rdiff2
= rdiff2
.buf
;
2424 rev
.creation_factor
= creation_factor
;
2425 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2427 _("Range-diff against v%d:"));
2431 * The order of precedence is:
2433 * 1. The `--signature` and `--no-signature` options.
2434 * 2. The `--signature-file` option.
2435 * 3. The `format.signature` config.
2436 * 4. The `format.signatureFile` config.
2437 * 5. Default `git_version_string`.
2440 ; /* --no-signature inhibits all signatures */
2441 } else if (signature
&& signature
!= git_version_string
) {
2442 ; /* non-default signature already set */
2443 } else if (signature_file_arg
|| (cfg
.signature_file
&& !cfg
.signature
)) {
2444 struct strbuf buf
= STRBUF_INIT
;
2445 const char *signature_file
= signature_file_arg
?
2446 signature_file_arg
: cfg
.signature_file
;
2448 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2449 die_errno(_("unable to read signature file '%s'"), signature_file
);
2450 signature
= signature_to_free
= strbuf_detach(&buf
, NULL
);
2451 } else if (cfg
.signature
) {
2452 signature
= cfg
.signature
;
2455 memset(&bases
, 0, sizeof(bases
));
2456 base
= get_base_commit(&cfg
, list
, nr
);
2458 reset_revision_walk();
2459 clear_object_flags(UNINTERESTING
);
2460 prepare_bases(&bases
, base
, list
, nr
);
2463 if (in_reply_to
|| cfg
.thread
|| cover_letter
) {
2464 rev
.ref_message_ids
= xmalloc(sizeof(*rev
.ref_message_ids
));
2465 string_list_init_dup(rev
.ref_message_ids
);
2468 char *msgid
= clean_message_id(in_reply_to
);
2469 string_list_append_nodup(rev
.ref_message_ids
, msgid
);
2471 rev
.numbered_files
= just_numbers
;
2472 rev
.patch_suffix
= fmt_patch_suffix
;
2475 gen_message_id(&rev
, "cover");
2476 make_cover_letter(&rev
, !!output_directory
,
2477 origin
, nr
, list
, description_file
, branch_name
, quiet
, &cfg
);
2478 print_bases(&bases
, rev
.diffopt
.file
);
2479 print_signature(signature
, rev
.diffopt
.file
);
2482 /* interdiff/range-diff in cover-letter; omit from patches */
2483 rev
.idiff_oid1
= NULL
;
2486 rev
.add_signoff
= cfg
.do_signoff
;
2489 progress
= start_delayed_progress(_("Generating patches"), total
);
2492 display_progress(progress
, total
- nr
);
2494 rev
.nr
= total
- nr
+ (start_number
- 1);
2495 /* Make the second and subsequent mails replies to the first */
2497 /* Have we already had a message ID? */
2498 if (rev
.message_id
) {
2500 * For deep threading: make every mail
2501 * a reply to the previous one, no
2502 * matter what other options are set.
2504 * For shallow threading:
2506 * Without --cover-letter and
2507 * --in-reply-to, make every mail a
2508 * reply to the one before.
2510 * With --in-reply-to but no
2511 * --cover-letter, make every mail a
2512 * reply to the <reply-to>.
2514 * With --cover-letter, make every
2515 * mail but the cover letter a reply
2516 * to the cover letter. The cover
2517 * letter is a reply to the
2518 * --in-reply-to, if specified.
2520 if (cfg
.thread
== THREAD_SHALLOW
2521 && rev
.ref_message_ids
->nr
> 0
2522 && (!cover_letter
|| rev
.nr
> 1))
2523 free(rev
.message_id
);
2525 string_list_append_nodup(rev
.ref_message_ids
,
2528 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2531 if (output_directory
&&
2532 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2533 die(_("failed to create output files"));
2534 shown
= log_tree_commit(&rev
, commit
);
2535 free_commit_buffer(the_repository
->parsed_objects
,
2538 /* We put one extra blank line between formatted
2539 * patches and this flag is used by log-tree code
2540 * to see if it needs to emit a LF before showing
2541 * the log; when using one file per patch, we do
2542 * not want the extra blank line.
2544 if (output_directory
)
2547 print_bases(&bases
, rev
.diffopt
.file
);
2548 if (rev
.mime_boundary
)
2549 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2550 mime_boundary_leader
,
2553 print_signature(signature
, rev
.diffopt
.file
);
2555 if (output_directory
) {
2556 fclose(rev
.diffopt
.file
);
2557 rev
.diffopt
.file
= NULL
;
2560 stop_progress(&progress
);
2562 if (ignore_if_in_upstream
)
2563 free_patch_ids(&ids
);
2566 oid_array_clear(&idiff_prev
);
2567 strbuf_release(&idiff_title
);
2568 strbuf_release(&rdiff1
);
2569 strbuf_release(&rdiff2
);
2570 strbuf_release(&rdiff_title
);
2571 free(description_file
);
2572 free(signature_file_arg
);
2573 free(signature_to_free
);
2576 free(rev
.message_id
);
2577 if (rev
.ref_message_ids
)
2578 string_list_clear(rev
.ref_message_ids
, 0);
2579 free(rev
.ref_message_ids
);
2580 rev
.diffopt
.no_free
= 0;
2581 release_revisions(&rev
);
2582 format_config_release(&cfg
);
2586 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2588 struct object_id oid
;
2589 if (repo_get_oid(the_repository
, arg
, &oid
) == 0) {
2590 struct commit
*commit
= lookup_commit_reference(the_repository
,
2593 commit
->object
.flags
|= flags
;
2594 add_pending_object(revs
, &commit
->object
, arg
);
2601 static const char * const cherry_usage
[] = {
2602 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2606 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2607 int abbrev
, FILE *file
)
2610 fprintf(file
, "%c %s\n", sign
,
2611 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
));
2613 struct strbuf buf
= STRBUF_INIT
;
2614 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2615 fprintf(file
, "%c %s %s\n", sign
,
2616 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, abbrev
),
2618 strbuf_release(&buf
);
2622 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2624 struct rev_info revs
;
2625 struct patch_ids ids
;
2626 struct commit
*commit
;
2627 struct commit_list
*list
= NULL
;
2628 struct branch
*current_branch
;
2629 const char *upstream
;
2630 const char *head
= "HEAD";
2631 const char *limit
= NULL
;
2632 int verbose
= 0, abbrev
= 0;
2634 struct option options
[] = {
2635 OPT__ABBREV(&abbrev
),
2636 OPT__VERBOSE(&verbose
, N_("be verbose")),
2640 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2653 current_branch
= branch_get(NULL
);
2654 upstream
= branch_get_upstream(current_branch
, NULL
);
2656 fprintf(stderr
, _("Could not find a tracked"
2657 " remote branch, please"
2658 " specify <upstream> manually.\n"));
2659 usage_with_options(cherry_usage
, options
);
2663 repo_init_revisions(the_repository
, &revs
, prefix
);
2664 revs
.max_parents
= 1;
2666 if (add_pending_commit(head
, &revs
, 0))
2667 die(_("unknown commit %s"), head
);
2668 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2669 die(_("unknown commit %s"), upstream
);
2671 /* Don't say anything if head and upstream are the same. */
2672 if (revs
.pending
.nr
== 2) {
2673 struct object_array_entry
*o
= revs
.pending
.objects
;
2674 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2678 get_patch_ids(&revs
, &ids
);
2680 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2681 die(_("unknown commit %s"), limit
);
2683 /* reverse the list of commits */
2684 if (prepare_revision_walk(&revs
))
2685 die(_("revision walk setup failed"));
2686 while ((commit
= get_revision(&revs
)) != NULL
) {
2687 commit_list_insert(commit
, &list
);
2690 for (struct commit_list
*l
= list
; l
; l
= l
->next
) {
2694 if (has_commit_patch_id(commit
, &ids
))
2696 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2699 free_commit_list(list
);
2700 free_patch_ids(&ids
);