2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "object-store.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
24 #include "string-list.h"
25 #include "parse-options.h"
28 #include "streaming.h"
31 #include "gpg-interface.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "interdiff.h"
37 #include "range-diff.h"
39 #define MAIL_DEFAULT_WRAP 72
41 /* Set a default date-time format for git log ("log.date" config variable) */
42 static const char *default_date_mode
= NULL
;
44 static int default_abbrev_commit
;
45 static int default_show_root
= 1;
46 static int default_follow
;
47 static int default_show_signature
;
48 static int decoration_style
;
49 static int decoration_given
;
50 static int use_mailmap_config
= 1;
51 static const char *fmt_patch_subject_prefix
= "PATCH";
52 static const char *fmt_pretty
;
54 static const char * const builtin_log_usage
[] = {
55 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
56 N_("git show [<options>] <object>..."),
60 struct line_opt_callback_data
{
63 struct string_list args
;
66 static int session_is_interactive(void)
68 return isatty(1) || pager_in_use();
71 static int auto_decoration_style(void)
73 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
76 static int parse_decoration_style(const char *value
)
78 switch (git_parse_maybe_bool(value
)) {
80 return DECORATE_SHORT_REFS
;
86 if (!strcmp(value
, "full"))
87 return DECORATE_FULL_REFS
;
88 else if (!strcmp(value
, "short"))
89 return DECORATE_SHORT_REFS
;
90 else if (!strcmp(value
, "auto"))
91 return auto_decoration_style();
93 * Please update _git_log() in git-completion.bash when you
94 * add new decoration styles.
99 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
102 decoration_style
= 0;
104 decoration_style
= parse_decoration_style(arg
);
106 decoration_style
= DECORATE_SHORT_REFS
;
108 if (decoration_style
< 0)
109 die(_("invalid --decorate option: %s"), arg
);
111 decoration_given
= 1;
116 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
118 struct line_opt_callback_data
*data
= option
->value
;
120 BUG_ON_OPT_NEG(unset
);
125 data
->rev
->line_level_traverse
= 1;
126 string_list_append(&data
->args
, arg
);
131 static void init_log_defaults(void)
133 init_grep_defaults(the_repository
);
134 init_diff_ui_defaults();
136 decoration_style
= auto_decoration_style();
139 static void cmd_log_init_defaults(struct rev_info
*rev
)
142 get_commit_format(fmt_pretty
, rev
);
144 rev
->diffopt
.flags
.default_follow_renames
= 1;
145 rev
->verbose_header
= 1;
146 rev
->diffopt
.flags
.recursive
= 1;
147 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
148 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
149 rev
->abbrev_commit
= default_abbrev_commit
;
150 rev
->show_root_diff
= default_show_root
;
151 rev
->subject_prefix
= fmt_patch_subject_prefix
;
152 rev
->show_signature
= default_show_signature
;
153 rev
->diffopt
.flags
.allow_textconv
= 1;
155 if (default_date_mode
)
156 parse_date_format(default_date_mode
, &rev
->date_mode
);
159 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
160 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
162 struct userformat_want w
;
163 int quiet
= 0, source
= 0, mailmap
;
164 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
165 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
166 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
167 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
168 &decorate_refs_exclude
};
169 static struct revision_sources revision_sources
;
171 const struct option builtin_log_options
[] = {
172 OPT__QUIET(&quiet
, N_("suppress diff output")),
173 OPT_BOOL(0, "source", &source
, N_("show source")),
174 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
175 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
176 N_("pattern"), N_("only decorate refs that match <pattern>")),
177 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
178 N_("pattern"), N_("do not decorate refs that match <pattern>")),
179 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
180 PARSE_OPT_OPTARG
, decorate_callback
},
181 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
182 N_("Process line range n,m in file, counting from 1"),
183 log_line_range_callback
),
188 line_cb
.prefix
= prefix
;
190 mailmap
= use_mailmap_config
;
191 argc
= parse_options(argc
, argv
, prefix
,
192 builtin_log_options
, builtin_log_usage
,
193 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
194 PARSE_OPT_KEEP_DASHDASH
);
197 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
198 argc
= setup_revisions(argc
, argv
, rev
, opt
);
200 /* Any arguments at this point are not recognized */
202 die(_("unrecognized argument: %s"), argv
[1]);
204 memset(&w
, 0, sizeof(w
));
205 userformat_find_requirements(NULL
, &w
);
207 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
210 init_display_notes(&rev
->notes_opt
);
212 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
213 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
214 rev
->always_show_header
= 0;
216 if (source
|| w
.source
) {
217 init_revision_sources(&revision_sources
);
218 rev
->sources
= &revision_sources
;
222 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
223 read_mailmap(rev
->mailmap
, NULL
);
226 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
228 * "log --pretty=raw" is special; ignore UI oriented
229 * configuration variables such as decoration.
231 if (!decoration_given
)
232 decoration_style
= 0;
233 if (!rev
->abbrev_commit_given
)
234 rev
->abbrev_commit
= 0;
237 if (decoration_style
) {
238 rev
->show_decorations
= 1;
239 load_ref_decorations(&decoration_filter
, decoration_style
);
242 if (rev
->line_level_traverse
)
243 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
248 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
249 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
251 cmd_log_init_defaults(rev
);
252 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
256 * This gives a rough estimate for how many commits we
257 * will print out in the list.
259 static int estimate_commit_count(struct commit_list
*list
)
264 struct commit
*commit
= list
->item
;
265 unsigned int flags
= commit
->object
.flags
;
267 if (!(flags
& (TREESAME
| UNINTERESTING
)))
273 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
275 if (rev
->shown_one
) {
277 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
278 putchar(rev
->diffopt
.line_termination
);
280 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
283 static struct itimerval early_output_timer
;
285 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
287 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
290 revs
->diffopt
.close_file
= 0;
291 sort_in_topological_order(&list
, revs
->sort_order
);
293 struct commit
*commit
= list
->item
;
294 switch (simplify_commit(revs
, commit
)) {
297 int n
= estimate_commit_count(list
);
298 show_early_header(revs
, "incomplete", n
);
301 log_tree_commit(revs
, commit
);
308 fclose(revs
->diffopt
.file
);
314 /* Did we already get enough commits for the early output? */
317 fclose(revs
->diffopt
.file
);
322 * ..if no, then repeat it twice a second until we
325 * NOTE! We don't use "it_interval", because if the
326 * reader isn't listening, we want our output to be
327 * throttled by the writing, and not have the timer
328 * trigger every second even if we're blocked on a
331 early_output_timer
.it_value
.tv_sec
= 0;
332 early_output_timer
.it_value
.tv_usec
= 500000;
333 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
336 static void early_output(int signal
)
338 show_early_output
= log_show_early
;
341 static void setup_early_output(void)
346 * Set up the signal handler, minimally intrusively:
347 * we only set a single volatile integer word (not
348 * using sigatomic_t - trying to avoid unnecessary
349 * system dependencies and headers), and using
352 memset(&sa
, 0, sizeof(sa
));
353 sa
.sa_handler
= early_output
;
354 sigemptyset(&sa
.sa_mask
);
355 sa
.sa_flags
= SA_RESTART
;
356 sigaction(SIGALRM
, &sa
, NULL
);
359 * If we can get the whole output in less than a
360 * tenth of a second, don't even bother doing the
361 * early-output thing..
363 * This is a one-time-only trigger.
365 early_output_timer
.it_value
.tv_sec
= 0;
366 early_output_timer
.it_value
.tv_usec
= 100000;
367 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
370 static void finish_early_output(struct rev_info
*rev
)
372 int n
= estimate_commit_count(rev
->commits
);
373 signal(SIGALRM
, SIG_IGN
);
374 show_early_header(rev
, "done", n
);
377 static int cmd_log_walk(struct rev_info
*rev
)
379 struct commit
*commit
;
381 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
383 if (rev
->early_output
)
384 setup_early_output();
386 if (prepare_revision_walk(rev
))
387 die(_("revision walk setup failed"));
389 if (rev
->early_output
)
390 finish_early_output(rev
);
393 * For --check and --exit-code, the exit code is based on CHECK_FAILED
394 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
395 * retain that state information if replacing rev->diffopt in this loop
397 rev
->diffopt
.close_file
= 0;
398 while ((commit
= get_revision(rev
)) != NULL
) {
399 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
401 * We decremented max_count in get_revision,
402 * but we didn't actually show the commit.
405 if (!rev
->reflog_info
) {
407 * We may show a given commit multiple times when
408 * walking the reflogs.
410 free_commit_buffer(the_repository
->parsed_objects
,
412 free_commit_list(commit
->parents
);
413 commit
->parents
= NULL
;
415 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
416 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
417 if (rev
->diffopt
.degraded_cc_to_c
)
420 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
421 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
423 fclose(rev
->diffopt
.file
);
425 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
426 rev
->diffopt
.flags
.check_failed
) {
429 return diff_result_code(&rev
->diffopt
, 0);
432 static int git_log_config(const char *var
, const char *value
, void *cb
)
434 const char *slot_name
;
436 if (!strcmp(var
, "format.pretty"))
437 return git_config_string(&fmt_pretty
, var
, value
);
438 if (!strcmp(var
, "format.subjectprefix"))
439 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
440 if (!strcmp(var
, "log.abbrevcommit")) {
441 default_abbrev_commit
= git_config_bool(var
, value
);
444 if (!strcmp(var
, "log.date"))
445 return git_config_string(&default_date_mode
, var
, value
);
446 if (!strcmp(var
, "log.decorate")) {
447 decoration_style
= parse_decoration_style(value
);
448 if (decoration_style
< 0)
449 decoration_style
= 0; /* maybe warn? */
452 if (!strcmp(var
, "log.showroot")) {
453 default_show_root
= git_config_bool(var
, value
);
456 if (!strcmp(var
, "log.follow")) {
457 default_follow
= git_config_bool(var
, value
);
460 if (skip_prefix(var
, "color.decorate.", &slot_name
))
461 return parse_decorate_color_config(var
, slot_name
, value
);
462 if (!strcmp(var
, "log.mailmap")) {
463 use_mailmap_config
= git_config_bool(var
, value
);
466 if (!strcmp(var
, "log.showsignature")) {
467 default_show_signature
= git_config_bool(var
, value
);
471 if (grep_config(var
, value
, cb
) < 0)
473 if (git_gpg_config(var
, value
, cb
) < 0)
475 return git_diff_ui_config(var
, value
, cb
);
478 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
481 struct setup_revision_opt opt
;
484 git_config(git_log_config
, NULL
);
486 repo_init_revisions(the_repository
, &rev
, prefix
);
488 rev
.simplify_history
= 0;
489 memset(&opt
, 0, sizeof(opt
));
491 opt
.revarg_opt
= REVARG_COMMITTISH
;
492 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
493 if (!rev
.diffopt
.output_format
)
494 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
495 return cmd_log_walk(&rev
);
498 static void show_tagger(const char *buf
, struct rev_info
*rev
)
500 struct strbuf out
= STRBUF_INIT
;
501 struct pretty_print_context pp
= {0};
503 pp
.fmt
= rev
->commit_format
;
504 pp
.date_mode
= rev
->date_mode
;
505 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
506 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
507 strbuf_release(&out
);
510 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
512 struct object_id oidc
;
513 struct object_context obj_context
;
517 fflush(rev
->diffopt
.file
);
518 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
519 !rev
->diffopt
.flags
.allow_textconv
)
520 return stream_blob_to_fd(1, oid
, NULL
, 0);
522 if (get_oid_with_context(the_repository
, obj_name
,
524 &oidc
, &obj_context
))
525 die(_("not a valid object name %s"), obj_name
);
526 if (!obj_context
.path
||
527 !textconv_object(the_repository
, obj_context
.path
,
528 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
529 free(obj_context
.path
);
530 return stream_blob_to_fd(1, oid
, NULL
, 0);
534 die(_("git show %s: bad file"), obj_name
);
536 write_or_die(1, buf
, size
);
537 free(obj_context
.path
);
541 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
544 enum object_type type
;
545 char *buf
= read_object_file(oid
, &type
, &size
);
549 return error(_("could not read object %s"), oid_to_hex(oid
));
551 assert(type
== OBJ_TAG
);
552 while (offset
< size
&& buf
[offset
] != '\n') {
553 int new_offset
= offset
+ 1;
555 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
557 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
558 show_tagger(ident
, rev
);
563 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
568 static int show_tree_object(const struct object_id
*oid
,
570 const char *pathname
, unsigned mode
, int stage
, void *context
)
572 FILE *file
= context
;
573 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
577 static void show_setup_revisions_tweak(struct rev_info
*rev
,
578 struct setup_revision_opt
*opt
)
580 if (rev
->ignore_merges
) {
581 /* There was no "-m" on the command line */
582 rev
->ignore_merges
= 0;
583 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
584 /* No "--first-parent", "-c", or "--cc" */
585 rev
->combine_merges
= 1;
586 rev
->dense_combined_merges
= 1;
589 if (!rev
->diffopt
.output_format
)
590 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
593 int cmd_show(int argc
, const char **argv
, const char *prefix
)
596 struct object_array_entry
*objects
;
597 struct setup_revision_opt opt
;
598 struct pathspec match_all
;
599 int i
, count
, ret
= 0;
602 git_config(git_log_config
, NULL
);
604 memset(&match_all
, 0, sizeof(match_all
));
605 repo_init_revisions(the_repository
, &rev
, prefix
);
607 rev
.always_show_header
= 1;
608 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
609 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
611 memset(&opt
, 0, sizeof(opt
));
613 opt
.tweak
= show_setup_revisions_tweak
;
614 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
617 return cmd_log_walk(&rev
);
619 count
= rev
.pending
.nr
;
620 objects
= rev
.pending
.objects
;
621 for (i
= 0; i
< count
&& !ret
; i
++) {
622 struct object
*o
= objects
[i
].item
;
623 const char *name
= objects
[i
].name
;
626 ret
= show_blob_object(&o
->oid
, &rev
, name
);
629 struct tag
*t
= (struct tag
*)o
;
633 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
634 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
636 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
637 ret
= show_tag_object(&o
->oid
, &rev
);
641 o
= parse_object(the_repository
, &t
->tagged
->oid
);
643 ret
= error(_("could not read object %s"),
644 oid_to_hex(&t
->tagged
->oid
));
652 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
653 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
655 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
656 read_tree_recursive(the_repository
, (struct tree
*)o
, "",
657 0, 0, &match_all
, show_tree_object
,
662 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
663 rev
.pending
.objects
= NULL
;
664 add_object_array(o
, name
, &rev
.pending
);
665 ret
= cmd_log_walk(&rev
);
668 ret
= error(_("unknown type: %d"), o
->type
);
676 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
678 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
681 struct setup_revision_opt opt
;
684 git_config(git_log_config
, NULL
);
686 repo_init_revisions(the_repository
, &rev
, prefix
);
687 init_reflog_walk(&rev
.reflog_info
);
688 rev
.verbose_header
= 1;
689 memset(&opt
, 0, sizeof(opt
));
691 cmd_log_init_defaults(&rev
);
692 rev
.abbrev_commit
= 1;
693 rev
.commit_format
= CMIT_FMT_ONELINE
;
694 rev
.use_terminator
= 1;
695 rev
.always_show_header
= 1;
696 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
698 return cmd_log_walk(&rev
);
701 static void log_setup_revisions_tweak(struct rev_info
*rev
,
702 struct setup_revision_opt
*opt
)
704 if (rev
->diffopt
.flags
.default_follow_renames
&&
705 rev
->prune_data
.nr
== 1)
706 rev
->diffopt
.flags
.follow_renames
= 1;
708 /* Turn --cc/-c into -p --cc/-c when -p was not given */
709 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
710 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
712 /* Turn -m on when --cc/-c was given */
713 if (rev
->combine_merges
)
714 rev
->ignore_merges
= 0;
717 int cmd_log(int argc
, const char **argv
, const char *prefix
)
720 struct setup_revision_opt opt
;
723 git_config(git_log_config
, NULL
);
725 repo_init_revisions(the_repository
, &rev
, prefix
);
726 rev
.always_show_header
= 1;
727 memset(&opt
, 0, sizeof(opt
));
729 opt
.revarg_opt
= REVARG_COMMITTISH
;
730 opt
.tweak
= log_setup_revisions_tweak
;
731 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
732 return cmd_log_walk(&rev
);
737 static const char *fmt_patch_suffix
= ".patch";
738 static int numbered
= 0;
739 static int auto_number
= 1;
741 static char *default_attach
= NULL
;
743 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
744 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
745 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
747 static void add_header(const char *value
)
749 struct string_list_item
*item
;
750 int len
= strlen(value
);
751 while (len
&& value
[len
- 1] == '\n')
754 if (!strncasecmp(value
, "to: ", 4)) {
755 item
= string_list_append(&extra_to
, value
+ 4);
757 } else if (!strncasecmp(value
, "cc: ", 4)) {
758 item
= string_list_append(&extra_cc
, value
+ 4);
761 item
= string_list_append(&extra_hdr
, value
);
764 item
->string
[len
] = '\0';
767 #define THREAD_SHALLOW 1
768 #define THREAD_DEEP 2
770 static int do_signoff
;
771 static int base_auto
;
773 static const char *signature
= git_version_string
;
774 static const char *signature_file
;
775 static int config_cover_letter
;
776 static const char *config_output_directory
;
785 static int git_format_config(const char *var
, const char *value
, void *cb
)
787 struct rev_info
*rev
= cb
;
789 if (!strcmp(var
, "format.headers")) {
791 die(_("format.headers without value"));
795 if (!strcmp(var
, "format.suffix"))
796 return git_config_string(&fmt_patch_suffix
, var
, value
);
797 if (!strcmp(var
, "format.to")) {
799 return config_error_nonbool(var
);
800 string_list_append(&extra_to
, value
);
803 if (!strcmp(var
, "format.cc")) {
805 return config_error_nonbool(var
);
806 string_list_append(&extra_cc
, value
);
809 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
810 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
813 if (!strcmp(var
, "format.numbered")) {
814 if (value
&& !strcasecmp(value
, "auto")) {
818 numbered
= git_config_bool(var
, value
);
819 auto_number
= auto_number
&& numbered
;
822 if (!strcmp(var
, "format.attach")) {
824 default_attach
= xstrdup(value
);
826 default_attach
= xstrdup(git_version_string
);
829 if (!strcmp(var
, "format.thread")) {
830 if (value
&& !strcasecmp(value
, "deep")) {
831 thread
= THREAD_DEEP
;
834 if (value
&& !strcasecmp(value
, "shallow")) {
835 thread
= THREAD_SHALLOW
;
838 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
841 if (!strcmp(var
, "format.signoff")) {
842 do_signoff
= git_config_bool(var
, value
);
845 if (!strcmp(var
, "format.signature"))
846 return git_config_string(&signature
, var
, value
);
847 if (!strcmp(var
, "format.signaturefile"))
848 return git_config_pathname(&signature_file
, var
, value
);
849 if (!strcmp(var
, "format.coverletter")) {
850 if (value
&& !strcasecmp(value
, "auto")) {
851 config_cover_letter
= COVER_AUTO
;
854 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
857 if (!strcmp(var
, "format.outputdirectory"))
858 return git_config_string(&config_output_directory
, var
, value
);
859 if (!strcmp(var
, "format.useautobase")) {
860 base_auto
= git_config_bool(var
, value
);
863 if (!strcmp(var
, "format.from")) {
864 int b
= git_parse_maybe_bool(value
);
867 from
= xstrdup(value
);
869 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
874 if (!strcmp(var
, "format.notes")) {
875 struct strbuf buf
= STRBUF_INIT
;
876 int b
= git_parse_maybe_bool(value
);
881 strbuf_addstr(&buf
, value
);
882 expand_notes_ref(&buf
);
883 string_list_append(&rev
->notes_opt
.extra_notes_refs
,
884 strbuf_detach(&buf
, NULL
));
886 rev
->notes_opt
.use_default_notes
= 1;
891 return git_log_config(var
, value
, cb
);
894 static const char *output_directory
= NULL
;
895 static int outdir_offset
;
897 static int open_next_file(struct commit
*commit
, const char *subject
,
898 struct rev_info
*rev
, int quiet
)
900 struct strbuf filename
= STRBUF_INIT
;
901 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
903 if (output_directory
) {
904 strbuf_addstr(&filename
, output_directory
);
906 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
907 strbuf_release(&filename
);
908 return error(_("name of output directory is too long"));
910 strbuf_complete(&filename
, '/');
913 if (rev
->numbered_files
)
914 strbuf_addf(&filename
, "%d", rev
->nr
);
916 fmt_output_commit(&filename
, commit
, rev
);
918 fmt_output_subject(&filename
, subject
, rev
);
921 printf("%s\n", filename
.buf
+ outdir_offset
);
923 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
924 error_errno(_("cannot open patch file %s"), filename
.buf
);
925 strbuf_release(&filename
);
929 strbuf_release(&filename
);
933 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
935 struct rev_info check_rev
;
936 struct commit
*commit
, *c1
, *c2
;
937 struct object
*o1
, *o2
;
938 unsigned flags1
, flags2
;
940 if (rev
->pending
.nr
!= 2)
941 die(_("need exactly one range"));
943 o1
= rev
->pending
.objects
[0].item
;
944 o2
= rev
->pending
.objects
[1].item
;
947 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
948 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
950 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
951 die(_("not a range"));
953 init_patch_ids(the_repository
, ids
);
955 /* given a range a..b get all patch ids for b..a */
956 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
957 check_rev
.max_parents
= 1;
958 o1
->flags
^= UNINTERESTING
;
959 o2
->flags
^= UNINTERESTING
;
960 add_pending_object(&check_rev
, o1
, "o1");
961 add_pending_object(&check_rev
, o2
, "o2");
962 if (prepare_revision_walk(&check_rev
))
963 die(_("revision walk setup failed"));
965 while ((commit
= get_revision(&check_rev
)) != NULL
) {
966 add_commit_patch_id(commit
, ids
);
969 /* reset for next revision walk */
970 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
971 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
976 static void gen_message_id(struct rev_info
*info
, char *base
)
978 struct strbuf buf
= STRBUF_INIT
;
979 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
980 (timestamp_t
) time(NULL
),
981 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
982 info
->message_id
= strbuf_detach(&buf
, NULL
);
985 static void print_signature(FILE *file
)
987 if (!signature
|| !*signature
)
990 fprintf(file
, "-- \n%s", signature
);
991 if (signature
[strlen(signature
)-1] != '\n')
996 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
998 struct strbuf desc
= STRBUF_INIT
;
999 if (!branch_name
|| !*branch_name
)
1001 read_branch_desc(&desc
, branch_name
);
1003 strbuf_addch(buf
, '\n');
1004 strbuf_addbuf(buf
, &desc
);
1005 strbuf_addch(buf
, '\n');
1007 strbuf_release(&desc
);
1010 static char *find_branch_name(struct rev_info
*rev
)
1012 int i
, positive
= -1;
1013 struct object_id branch_oid
;
1014 const struct object_id
*tip_oid
;
1015 const char *ref
, *v
;
1016 char *full_ref
, *branch
= NULL
;
1018 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1019 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1028 ref
= rev
->cmdline
.rev
[positive
].name
;
1029 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1030 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
) &&
1031 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1032 oideq(tip_oid
, &branch_oid
))
1033 branch
= xstrdup(v
);
1038 static void show_diffstat(struct rev_info
*rev
,
1039 struct commit
*origin
, struct commit
*head
)
1041 struct diff_options opts
;
1043 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1044 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1045 diff_setup_done(&opts
);
1047 diff_tree_oid(get_commit_tree_oid(origin
),
1048 get_commit_tree_oid(head
),
1050 diffcore_std(&opts
);
1053 fprintf(rev
->diffopt
.file
, "\n");
1056 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1057 struct commit
*origin
,
1058 int nr
, struct commit
**list
,
1059 const char *branch_name
,
1062 const char *committer
;
1063 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1065 struct shortlog log
;
1066 struct strbuf sb
= STRBUF_INIT
;
1068 const char *encoding
= "UTF-8";
1069 int need_8bit_cte
= 0;
1070 struct pretty_print_context pp
= {0};
1071 struct commit
*head
= list
[0];
1073 if (!cmit_fmt_is_mail(rev
->commit_format
))
1074 die(_("cover letter needs email format"));
1076 committer
= git_committer_info(0);
1079 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1080 die(_("failed to create cover-letter file"));
1082 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1084 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1085 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1086 if (has_non_ascii(buf
))
1088 unuse_commit_buffer(list
[i
], buf
);
1092 branch_name
= find_branch_name(rev
);
1095 pp
.fmt
= CMIT_FMT_EMAIL
;
1096 pp
.date_mode
.type
= DATE_RFC2822
;
1098 pp
.print_email_subject
= 1;
1099 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1100 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1101 pp_remainder(&pp
, &msg
, &sb
, 0);
1102 add_branch_description(&sb
, branch_name
);
1103 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1105 strbuf_release(&sb
);
1107 shortlog_init(&log
);
1109 log
.wrap
= MAIL_DEFAULT_WRAP
;
1112 log
.file
= rev
->diffopt
.file
;
1113 for (i
= 0; i
< nr
; i
++)
1114 shortlog_add_commit(&log
, list
[i
]);
1116 shortlog_output(&log
);
1118 /* We can only do diffstat with a unique reference point */
1120 show_diffstat(rev
, origin
, head
);
1122 if (rev
->idiff_oid1
) {
1123 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1124 show_interdiff(rev
, 0);
1129 * Pass minimum required diff-options to range-diff; others
1130 * can be added later if deemed desirable.
1132 struct diff_options opts
;
1134 opts
.file
= rev
->diffopt
.file
;
1135 opts
.use_color
= rev
->diffopt
.use_color
;
1136 diff_setup_done(&opts
);
1137 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1138 show_range_diff(rev
->rdiff1
, rev
->rdiff2
,
1139 rev
->creation_factor
, 1, &opts
);
1143 static const char *clean_message_id(const char *msg_id
)
1146 const char *a
, *z
, *m
;
1149 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1154 if (!isspace(ch
) && (ch
!= '>'))
1159 die(_("insane in-reply-to: %s"), msg_id
);
1162 return xmemdupz(a
, z
- a
);
1165 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1167 if (output_directory
&& is_absolute_path(output_directory
))
1168 return output_directory
;
1170 if (!prefix
|| !*prefix
) {
1171 if (output_directory
)
1172 return output_directory
;
1173 /* The user did not explicitly ask for "./" */
1178 outdir_offset
= strlen(prefix
);
1179 if (!output_directory
)
1182 return prefix_filename(prefix
, output_directory
);
1185 static const char * const builtin_format_patch_usage
[] = {
1186 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1190 static int keep_subject
= 0;
1192 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1194 BUG_ON_OPT_NEG(unset
);
1195 BUG_ON_OPT_ARG(arg
);
1196 ((struct rev_info
*)opt
->value
)->total
= -1;
1201 static int subject_prefix
= 0;
1203 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1206 BUG_ON_OPT_NEG(unset
);
1208 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1212 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1214 BUG_ON_OPT_NEG(unset
);
1215 BUG_ON_OPT_ARG(arg
);
1216 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1219 static int numbered_cmdline_opt
= 0;
1221 static int numbered_callback(const struct option
*opt
, const char *arg
,
1224 BUG_ON_OPT_ARG(arg
);
1225 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1231 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1234 BUG_ON_OPT_NEG(unset
);
1235 return numbered_callback(opt
, arg
, 1);
1238 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1241 const char **dir
= (const char **)opt
->value
;
1242 BUG_ON_OPT_NEG(unset
);
1244 die(_("two output directories?"));
1249 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1251 int *thread
= (int *)opt
->value
;
1254 else if (!arg
|| !strcmp(arg
, "shallow"))
1255 *thread
= THREAD_SHALLOW
;
1256 else if (!strcmp(arg
, "deep"))
1257 *thread
= THREAD_DEEP
;
1259 * Please update _git_formatpatch() in git-completion.bash
1260 * when you add new options.
1267 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1269 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1271 rev
->mime_boundary
= NULL
;
1273 rev
->mime_boundary
= arg
;
1275 rev
->mime_boundary
= git_version_string
;
1276 rev
->no_inline
= unset
? 0 : 1;
1280 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1282 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1284 rev
->mime_boundary
= NULL
;
1286 rev
->mime_boundary
= arg
;
1288 rev
->mime_boundary
= git_version_string
;
1293 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1296 string_list_clear(&extra_hdr
, 0);
1297 string_list_clear(&extra_to
, 0);
1298 string_list_clear(&extra_cc
, 0);
1305 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1308 string_list_clear(&extra_to
, 0);
1310 string_list_append(&extra_to
, arg
);
1314 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1317 string_list_clear(&extra_cc
, 0);
1319 string_list_append(&extra_cc
, arg
);
1323 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1325 char **from
= opt
->value
;
1332 *from
= xstrdup(arg
);
1334 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1338 struct base_tree_info
{
1339 struct object_id base_commit
;
1340 int nr_patch_id
, alloc_patch_id
;
1341 struct object_id
*patch_id
;
1344 static struct commit
*get_base_commit(const char *base_commit
,
1345 struct commit
**list
,
1348 struct commit
*base
= NULL
;
1349 struct commit
**rev
;
1350 int i
= 0, rev_nr
= 0;
1352 if (base_commit
&& strcmp(base_commit
, "auto")) {
1353 base
= lookup_commit_reference_by_name(base_commit
);
1355 die(_("unknown commit %s"), base_commit
);
1356 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1357 struct branch
*curr_branch
= branch_get(NULL
);
1358 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1360 struct commit_list
*base_list
;
1361 struct commit
*commit
;
1362 struct object_id oid
;
1364 if (get_oid(upstream
, &oid
))
1365 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1366 commit
= lookup_commit_or_die(&oid
, "upstream base");
1367 base_list
= get_merge_bases_many(commit
, total
, list
);
1368 /* There should be one and only one merge base. */
1369 if (!base_list
|| base_list
->next
)
1370 die(_("could not find exact merge base"));
1371 base
= base_list
->item
;
1372 free_commit_list(base_list
);
1374 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1375 "please use git branch --set-upstream-to to track a remote branch.\n"
1376 "Or you could specify base commit by --base=<base-commit-id> manually"));
1380 ALLOC_ARRAY(rev
, total
);
1381 for (i
= 0; i
< total
; i
++)
1386 * Get merge base through pair-wise computations
1387 * and store it in rev[0].
1389 while (rev_nr
> 1) {
1390 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1391 struct commit_list
*merge_base
;
1392 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1393 if (!merge_base
|| merge_base
->next
)
1394 die(_("failed to find exact merge base"));
1396 rev
[i
] = merge_base
->item
;
1400 rev
[i
] = rev
[2 * i
];
1401 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1404 if (!in_merge_bases(base
, rev
[0]))
1405 die(_("base commit should be the ancestor of revision list"));
1407 for (i
= 0; i
< total
; i
++) {
1408 if (base
== list
[i
])
1409 die(_("base commit shouldn't be in revision list"));
1416 define_commit_slab(commit_base
, int);
1418 static void prepare_bases(struct base_tree_info
*bases
,
1419 struct commit
*base
,
1420 struct commit
**list
,
1423 struct commit
*commit
;
1424 struct rev_info revs
;
1425 struct diff_options diffopt
;
1426 struct commit_base commit_base
;
1432 init_commit_base(&commit_base
);
1433 repo_diff_setup(the_repository
, &diffopt
);
1434 diffopt
.flags
.recursive
= 1;
1435 diff_setup_done(&diffopt
);
1437 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1439 repo_init_revisions(the_repository
, &revs
, NULL
);
1440 revs
.max_parents
= 1;
1441 revs
.topo_order
= 1;
1442 for (i
= 0; i
< total
; i
++) {
1443 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1444 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1445 *commit_base_at(&commit_base
, list
[i
]) = 1;
1447 base
->object
.flags
|= UNINTERESTING
;
1448 add_pending_object(&revs
, &base
->object
, "base");
1450 if (prepare_revision_walk(&revs
))
1451 die(_("revision walk setup failed"));
1453 * Traverse the commits list, get prerequisite patch ids
1454 * and stuff them in bases structure.
1456 while ((commit
= get_revision(&revs
)) != NULL
) {
1457 struct object_id oid
;
1458 struct object_id
*patch_id
;
1459 if (*commit_base_at(&commit_base
, commit
))
1461 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1462 die(_("cannot get patch id"));
1463 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1464 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1465 oidcpy(patch_id
, &oid
);
1466 bases
->nr_patch_id
++;
1468 clear_commit_base(&commit_base
);
1471 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1475 /* Only do this once, either for the cover or for the first one */
1476 if (is_null_oid(&bases
->base_commit
))
1479 /* Show the base commit */
1480 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1482 /* Show the prerequisite patches */
1483 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1484 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1486 free(bases
->patch_id
);
1487 bases
->nr_patch_id
= 0;
1488 bases
->alloc_patch_id
= 0;
1489 oidclr(&bases
->base_commit
);
1492 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1493 const char *generic
, const char *rerolled
)
1495 if (reroll_count
<= 0)
1496 strbuf_addstr(sb
, generic
);
1497 else /* RFC may be v0, so allow -v1 to diff against v0 */
1498 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1502 static void infer_range_diff_ranges(struct strbuf
*r1
,
1505 struct commit
*origin
,
1506 struct commit
*head
)
1508 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1510 if (!strstr(prev
, "..")) {
1511 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1512 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1513 } else if (!origin
) {
1514 die(_("failed to infer range-diff ranges"));
1516 strbuf_addstr(r1
, prev
);
1517 strbuf_addf(r2
, "%s..%s",
1518 oid_to_hex(&origin
->object
.oid
), head_oid
);
1522 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1524 struct commit
*commit
;
1525 struct commit
**list
= NULL
;
1526 struct rev_info rev
;
1527 struct setup_revision_opt s_r_opt
;
1528 int nr
= 0, total
, i
;
1530 int start_number
= -1;
1531 int just_numbers
= 0;
1532 int ignore_if_in_upstream
= 0;
1533 int cover_letter
= -1;
1534 int boundary_count
= 0;
1535 int no_binary_diff
= 0;
1536 int zero_commit
= 0;
1537 struct commit
*origin
= NULL
;
1538 const char *in_reply_to
= NULL
;
1539 struct patch_ids ids
;
1540 struct strbuf buf
= STRBUF_INIT
;
1541 int use_patch_format
= 0;
1543 int reroll_count
= -1;
1544 char *branch_name
= NULL
;
1545 char *base_commit
= NULL
;
1546 struct base_tree_info bases
;
1547 int show_progress
= 0;
1548 struct progress
*progress
= NULL
;
1549 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1550 struct strbuf idiff_title
= STRBUF_INIT
;
1551 const char *rdiff_prev
= NULL
;
1552 struct strbuf rdiff1
= STRBUF_INIT
;
1553 struct strbuf rdiff2
= STRBUF_INIT
;
1554 struct strbuf rdiff_title
= STRBUF_INIT
;
1555 int creation_factor
= -1;
1557 const struct option builtin_format_patch_options
[] = {
1558 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1559 N_("use [PATCH n/m] even with a single patch"),
1560 PARSE_OPT_NOARG
, numbered_callback
},
1561 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1562 N_("use [PATCH] even with multiple patches"),
1563 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
},
1564 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1565 OPT_BOOL(0, "stdout", &use_stdout
,
1566 N_("print patches to standard out")),
1567 OPT_BOOL(0, "cover-letter", &cover_letter
,
1568 N_("generate a cover letter")),
1569 OPT_BOOL(0, "numbered-files", &just_numbers
,
1570 N_("use simple number sequence for output file names")),
1571 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1572 N_("use <sfx> instead of '.patch'")),
1573 OPT_INTEGER(0, "start-number", &start_number
,
1574 N_("start numbering patches at <n> instead of 1")),
1575 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1576 N_("mark the series as Nth re-roll")),
1577 { OPTION_CALLBACK
, 0, "rfc", &rev
, NULL
,
1578 N_("Use [RFC PATCH] instead of [PATCH]"),
1579 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
},
1580 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1581 N_("Use [<prefix>] instead of [PATCH]"),
1582 PARSE_OPT_NONEG
, subject_prefix_callback
},
1583 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1584 N_("dir"), N_("store resulting files in <dir>"),
1585 PARSE_OPT_NONEG
, output_directory_callback
},
1586 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1587 N_("don't strip/add [PATCH]"),
1588 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1589 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1590 N_("don't output binary diffs")),
1591 OPT_BOOL(0, "zero-commit", &zero_commit
,
1592 N_("output all-zero hash in From header")),
1593 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1594 N_("don't include a patch matching a commit upstream")),
1595 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1596 N_("show patch format instead of default (patch + stat)"),
1597 1, PARSE_OPT_NONEG
),
1598 OPT_GROUP(N_("Messaging")),
1599 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1600 N_("add email header"), 0, header_callback
},
1601 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1603 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1605 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1606 N_("set From address to <ident> (or committer ident if absent)"),
1607 PARSE_OPT_OPTARG
, from_callback
},
1608 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1609 N_("make first mail a reply to <message-id>")),
1610 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1611 N_("attach the patch"), PARSE_OPT_OPTARG
,
1613 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1614 N_("inline the patch"),
1615 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1617 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1618 N_("enable message threading, styles: shallow, deep"),
1619 PARSE_OPT_OPTARG
, thread_callback
},
1620 OPT_STRING(0, "signature", &signature
, N_("signature"),
1621 N_("add a signature")),
1622 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1623 N_("add prerequisite tree info to the patch series")),
1624 OPT_FILENAME(0, "signature-file", &signature_file
,
1625 N_("add a signature from a file")),
1626 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1627 OPT_BOOL(0, "progress", &show_progress
,
1628 N_("show progress while generating patches")),
1629 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1630 N_("show changes against <rev> in cover letter or single patch"),
1631 parse_opt_object_name
),
1632 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1633 N_("show changes against <refspec> in cover letter or single patch")),
1634 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1635 N_("percentage by which creation is weighted")),
1639 extra_hdr
.strdup_strings
= 1;
1640 extra_to
.strdup_strings
= 1;
1641 extra_cc
.strdup_strings
= 1;
1642 init_log_defaults();
1643 repo_init_revisions(the_repository
, &rev
, prefix
);
1644 git_config(git_format_config
, &rev
);
1645 rev
.commit_format
= CMIT_FMT_EMAIL
;
1646 rev
.expand_tabs_in_log_default
= 0;
1647 rev
.verbose_header
= 1;
1649 rev
.max_parents
= 1;
1650 rev
.diffopt
.flags
.recursive
= 1;
1651 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1652 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1653 s_r_opt
.def
= "HEAD";
1654 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1656 if (default_attach
) {
1657 rev
.mime_boundary
= default_attach
;
1662 * Parse the arguments before setup_revisions(), or something
1663 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1664 * possibly a valid SHA1.
1666 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1667 builtin_format_patch_usage
,
1668 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1669 PARSE_OPT_KEEP_DASHDASH
);
1671 if (0 < reroll_count
) {
1672 struct strbuf sprefix
= STRBUF_INIT
;
1673 strbuf_addf(&sprefix
, "%s v%d",
1674 rev
.subject_prefix
, reroll_count
);
1675 rev
.reroll_count
= reroll_count
;
1676 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1679 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1680 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1681 strbuf_addch(&buf
, '\n');
1685 strbuf_addstr(&buf
, "To: ");
1686 for (i
= 0; i
< extra_to
.nr
; i
++) {
1688 strbuf_addstr(&buf
, " ");
1689 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1690 if (i
+ 1 < extra_to
.nr
)
1691 strbuf_addch(&buf
, ',');
1692 strbuf_addch(&buf
, '\n');
1696 strbuf_addstr(&buf
, "Cc: ");
1697 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1699 strbuf_addstr(&buf
, " ");
1700 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1701 if (i
+ 1 < extra_cc
.nr
)
1702 strbuf_addch(&buf
, ',');
1703 strbuf_addch(&buf
, '\n');
1706 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1709 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1710 die(_("invalid ident line: %s"), from
);
1713 if (start_number
< 0)
1717 * If numbered is set solely due to format.numbered in config,
1718 * and it would conflict with --keep-subject (-k) from the
1719 * command line, reset "numbered".
1721 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1724 if (numbered
&& keep_subject
)
1725 die(_("-n and -k are mutually exclusive"));
1726 if (keep_subject
&& subject_prefix
)
1727 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1728 rev
.preserve_subject
= keep_subject
;
1730 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1732 die(_("unrecognized argument: %s"), argv
[1]);
1734 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1735 die(_("--name-only does not make sense"));
1736 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1737 die(_("--name-status does not make sense"));
1738 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1739 die(_("--check does not make sense"));
1741 if (!use_patch_format
&&
1742 (!rev
.diffopt
.output_format
||
1743 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1744 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1745 if (!rev
.diffopt
.stat_width
)
1746 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1748 /* Always generate a patch */
1749 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1751 rev
.zero_commit
= zero_commit
;
1753 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1754 rev
.diffopt
.flags
.binary
= 1;
1757 init_display_notes(&rev
.notes_opt
);
1759 if (!output_directory
&& !use_stdout
)
1760 output_directory
= config_output_directory
;
1763 output_directory
= set_outdir(prefix
, output_directory
);
1767 if (output_directory
) {
1768 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1769 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1771 die(_("standard output, or directory, which one?"));
1772 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1773 die_errno(_("could not create directory '%s'"),
1777 if (rev
.pending
.nr
== 1) {
1780 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1782 * This is traditional behaviour of "git format-patch
1783 * origin" that prepares what the origin side still
1786 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1787 add_head_to_pending(&rev
);
1791 * Otherwise, it is "format-patch -22 HEAD", and/or
1792 * "format-patch --root HEAD". The user wants
1793 * get_revision() to do the usual traversal.
1796 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1800 const char *ref
, *v
;
1801 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1803 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1804 branch_name
= xstrdup(v
);
1806 branch_name
= xstrdup(""); /* no branch */
1811 * We cannot move this anywhere earlier because we do want to
1812 * know if --root was given explicitly from the command line.
1814 rev
.show_root_diff
= 1;
1816 if (ignore_if_in_upstream
) {
1817 /* Don't say anything if head and upstream are the same. */
1818 if (rev
.pending
.nr
== 2) {
1819 struct object_array_entry
*o
= rev
.pending
.objects
;
1820 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
1823 get_patch_ids(&rev
, &ids
);
1826 if (prepare_revision_walk(&rev
))
1827 die(_("revision walk setup failed"));
1829 while ((commit
= get_revision(&rev
)) != NULL
) {
1830 if (commit
->object
.flags
& BOUNDARY
) {
1832 origin
= (boundary_count
== 1) ? commit
: NULL
;
1836 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1840 REALLOC_ARRAY(list
, nr
);
1841 list
[nr
- 1] = commit
;
1847 if (cover_letter
== -1) {
1848 if (config_cover_letter
== COVER_AUTO
)
1849 cover_letter
= (total
> 1);
1851 cover_letter
= (config_cover_letter
== COVER_ON
);
1853 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1856 rev
.total
= total
+ start_number
- 1;
1858 if (idiff_prev
.nr
) {
1859 if (!cover_letter
&& total
!= 1)
1860 die(_("--interdiff requires --cover-letter or single patch"));
1861 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
1862 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
1863 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
1865 _("Interdiff against v%d:"));
1868 if (creation_factor
< 0)
1869 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
1870 else if (!rdiff_prev
)
1871 die(_("--creation-factor requires --range-diff"));
1874 if (!cover_letter
&& total
!= 1)
1875 die(_("--range-diff requires --cover-letter or single patch"));
1877 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
1879 rev
.rdiff1
= rdiff1
.buf
;
1880 rev
.rdiff2
= rdiff2
.buf
;
1881 rev
.creation_factor
= creation_factor
;
1882 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
1884 _("Range-diff against v%d:"));
1888 ; /* --no-signature inhibits all signatures */
1889 } else if (signature
&& signature
!= git_version_string
) {
1890 ; /* non-default signature already set */
1891 } else if (signature_file
) {
1892 struct strbuf buf
= STRBUF_INIT
;
1894 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1895 die_errno(_("unable to read signature file '%s'"), signature_file
);
1896 signature
= strbuf_detach(&buf
, NULL
);
1899 memset(&bases
, 0, sizeof(bases
));
1900 if (base_commit
|| base_auto
) {
1901 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1902 reset_revision_walk();
1903 clear_object_flags(UNINTERESTING
);
1904 prepare_bases(&bases
, base
, list
, nr
);
1907 if (in_reply_to
|| thread
|| cover_letter
)
1908 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1910 const char *msgid
= clean_message_id(in_reply_to
);
1911 string_list_append(rev
.ref_message_ids
, msgid
);
1913 rev
.numbered_files
= just_numbers
;
1914 rev
.patch_suffix
= fmt_patch_suffix
;
1917 gen_message_id(&rev
, "cover");
1918 make_cover_letter(&rev
, use_stdout
,
1919 origin
, nr
, list
, branch_name
, quiet
);
1920 print_bases(&bases
, rev
.diffopt
.file
);
1921 print_signature(rev
.diffopt
.file
);
1924 /* interdiff/range-diff in cover-letter; omit from patches */
1925 rev
.idiff_oid1
= NULL
;
1928 rev
.add_signoff
= do_signoff
;
1931 progress
= start_delayed_progress(_("Generating patches"), total
);
1934 display_progress(progress
, total
- nr
);
1936 rev
.nr
= total
- nr
+ (start_number
- 1);
1937 /* Make the second and subsequent mails replies to the first */
1939 /* Have we already had a message ID? */
1940 if (rev
.message_id
) {
1942 * For deep threading: make every mail
1943 * a reply to the previous one, no
1944 * matter what other options are set.
1946 * For shallow threading:
1948 * Without --cover-letter and
1949 * --in-reply-to, make every mail a
1950 * reply to the one before.
1952 * With --in-reply-to but no
1953 * --cover-letter, make every mail a
1954 * reply to the <reply-to>.
1956 * With --cover-letter, make every
1957 * mail but the cover letter a reply
1958 * to the cover letter. The cover
1959 * letter is a reply to the
1960 * --in-reply-to, if specified.
1962 if (thread
== THREAD_SHALLOW
1963 && rev
.ref_message_ids
->nr
> 0
1964 && (!cover_letter
|| rev
.nr
> 1))
1965 free(rev
.message_id
);
1967 string_list_append(rev
.ref_message_ids
,
1970 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1974 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1975 die(_("failed to create output files"));
1976 shown
= log_tree_commit(&rev
, commit
);
1977 free_commit_buffer(the_repository
->parsed_objects
,
1980 /* We put one extra blank line between formatted
1981 * patches and this flag is used by log-tree code
1982 * to see if it needs to emit a LF before showing
1983 * the log; when using one file per patch, we do
1984 * not want the extra blank line.
1989 print_bases(&bases
, rev
.diffopt
.file
);
1990 if (rev
.mime_boundary
)
1991 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
1992 mime_boundary_leader
,
1995 print_signature(rev
.diffopt
.file
);
1998 fclose(rev
.diffopt
.file
);
2000 stop_progress(&progress
);
2003 string_list_clear(&extra_to
, 0);
2004 string_list_clear(&extra_cc
, 0);
2005 string_list_clear(&extra_hdr
, 0);
2006 if (ignore_if_in_upstream
)
2007 free_patch_ids(&ids
);
2010 oid_array_clear(&idiff_prev
);
2011 strbuf_release(&idiff_title
);
2012 strbuf_release(&rdiff1
);
2013 strbuf_release(&rdiff2
);
2014 strbuf_release(&rdiff_title
);
2018 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2020 struct object_id oid
;
2021 if (get_oid(arg
, &oid
) == 0) {
2022 struct commit
*commit
= lookup_commit_reference(the_repository
,
2025 commit
->object
.flags
|= flags
;
2026 add_pending_object(revs
, &commit
->object
, arg
);
2033 static const char * const cherry_usage
[] = {
2034 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2038 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2039 int abbrev
, FILE *file
)
2042 fprintf(file
, "%c %s\n", sign
,
2043 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2045 struct strbuf buf
= STRBUF_INIT
;
2046 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2047 fprintf(file
, "%c %s %s\n", sign
,
2048 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2050 strbuf_release(&buf
);
2054 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2056 struct rev_info revs
;
2057 struct patch_ids ids
;
2058 struct commit
*commit
;
2059 struct commit_list
*list
= NULL
;
2060 struct branch
*current_branch
;
2061 const char *upstream
;
2062 const char *head
= "HEAD";
2063 const char *limit
= NULL
;
2064 int verbose
= 0, abbrev
= 0;
2066 struct option options
[] = {
2067 OPT__ABBREV(&abbrev
),
2068 OPT__VERBOSE(&verbose
, N_("be verbose")),
2072 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2085 current_branch
= branch_get(NULL
);
2086 upstream
= branch_get_upstream(current_branch
, NULL
);
2088 fprintf(stderr
, _("Could not find a tracked"
2089 " remote branch, please"
2090 " specify <upstream> manually.\n"));
2091 usage_with_options(cherry_usage
, options
);
2095 repo_init_revisions(the_repository
, &revs
, prefix
);
2096 revs
.max_parents
= 1;
2098 if (add_pending_commit(head
, &revs
, 0))
2099 die(_("unknown commit %s"), head
);
2100 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2101 die(_("unknown commit %s"), upstream
);
2103 /* Don't say anything if head and upstream are the same. */
2104 if (revs
.pending
.nr
== 2) {
2105 struct object_array_entry
*o
= revs
.pending
.objects
;
2106 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2110 get_patch_ids(&revs
, &ids
);
2112 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2113 die(_("unknown commit %s"), limit
);
2115 /* reverse the list of commits */
2116 if (prepare_revision_walk(&revs
))
2117 die(_("revision walk setup failed"));
2118 while ((commit
= get_revision(&revs
)) != NULL
) {
2119 commit_list_insert(commit
, &list
);
2125 commit
= list
->item
;
2126 if (has_commit_patch_id(commit
, &ids
))
2128 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2132 free_patch_ids(&ids
);