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
= 0;
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
);
210 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
213 init_display_notes(&rev
->notes_opt
);
215 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
216 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
217 rev
->always_show_header
= 0;
219 if (source
|| w
.source
) {
220 init_revision_sources(&revision_sources
);
221 rev
->sources
= &revision_sources
;
225 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
226 read_mailmap(rev
->mailmap
, NULL
);
229 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
231 * "log --pretty=raw" is special; ignore UI oriented
232 * configuration variables such as decoration.
234 if (!decoration_given
)
235 decoration_style
= 0;
236 if (!rev
->abbrev_commit_given
)
237 rev
->abbrev_commit
= 0;
240 if (decoration_style
) {
241 rev
->show_decorations
= 1;
242 load_ref_decorations(&decoration_filter
, decoration_style
);
245 if (rev
->line_level_traverse
)
246 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
251 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
252 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
254 cmd_log_init_defaults(rev
);
255 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
259 * This gives a rough estimate for how many commits we
260 * will print out in the list.
262 static int estimate_commit_count(struct commit_list
*list
)
267 struct commit
*commit
= list
->item
;
268 unsigned int flags
= commit
->object
.flags
;
270 if (!(flags
& (TREESAME
| UNINTERESTING
)))
276 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
278 if (rev
->shown_one
) {
280 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
281 putchar(rev
->diffopt
.line_termination
);
283 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
286 static struct itimerval early_output_timer
;
288 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
290 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
293 revs
->diffopt
.close_file
= 0;
294 sort_in_topological_order(&list
, revs
->sort_order
);
296 struct commit
*commit
= list
->item
;
297 switch (simplify_commit(revs
, commit
)) {
300 int n
= estimate_commit_count(list
);
301 show_early_header(revs
, "incomplete", n
);
304 log_tree_commit(revs
, commit
);
311 fclose(revs
->diffopt
.file
);
317 /* Did we already get enough commits for the early output? */
320 fclose(revs
->diffopt
.file
);
325 * ..if no, then repeat it twice a second until we
328 * NOTE! We don't use "it_interval", because if the
329 * reader isn't listening, we want our output to be
330 * throttled by the writing, and not have the timer
331 * trigger every second even if we're blocked on a
334 early_output_timer
.it_value
.tv_sec
= 0;
335 early_output_timer
.it_value
.tv_usec
= 500000;
336 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
339 static void early_output(int signal
)
341 show_early_output
= log_show_early
;
344 static void setup_early_output(void)
349 * Set up the signal handler, minimally intrusively:
350 * we only set a single volatile integer word (not
351 * using sigatomic_t - trying to avoid unnecessary
352 * system dependencies and headers), and using
355 memset(&sa
, 0, sizeof(sa
));
356 sa
.sa_handler
= early_output
;
357 sigemptyset(&sa
.sa_mask
);
358 sa
.sa_flags
= SA_RESTART
;
359 sigaction(SIGALRM
, &sa
, NULL
);
362 * If we can get the whole output in less than a
363 * tenth of a second, don't even bother doing the
364 * early-output thing..
366 * This is a one-time-only trigger.
368 early_output_timer
.it_value
.tv_sec
= 0;
369 early_output_timer
.it_value
.tv_usec
= 100000;
370 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
373 static void finish_early_output(struct rev_info
*rev
)
375 int n
= estimate_commit_count(rev
->commits
);
376 signal(SIGALRM
, SIG_IGN
);
377 show_early_header(rev
, "done", n
);
380 static int cmd_log_walk(struct rev_info
*rev
)
382 struct commit
*commit
;
384 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
386 if (rev
->early_output
)
387 setup_early_output();
389 if (prepare_revision_walk(rev
))
390 die(_("revision walk setup failed"));
392 if (rev
->early_output
)
393 finish_early_output(rev
);
396 * For --check and --exit-code, the exit code is based on CHECK_FAILED
397 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
398 * retain that state information if replacing rev->diffopt in this loop
400 rev
->diffopt
.close_file
= 0;
401 while ((commit
= get_revision(rev
)) != NULL
) {
402 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
404 * We decremented max_count in get_revision,
405 * but we didn't actually show the commit.
408 if (!rev
->reflog_info
) {
410 * We may show a given commit multiple times when
411 * walking the reflogs.
413 free_commit_buffer(the_repository
->parsed_objects
,
415 free_commit_list(commit
->parents
);
416 commit
->parents
= NULL
;
418 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
419 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
420 if (rev
->diffopt
.degraded_cc_to_c
)
423 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
424 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
426 fclose(rev
->diffopt
.file
);
428 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
429 rev
->diffopt
.flags
.check_failed
) {
432 return diff_result_code(&rev
->diffopt
, 0);
435 static int git_log_config(const char *var
, const char *value
, void *cb
)
437 const char *slot_name
;
439 if (!strcmp(var
, "format.pretty"))
440 return git_config_string(&fmt_pretty
, var
, value
);
441 if (!strcmp(var
, "format.subjectprefix"))
442 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
443 if (!strcmp(var
, "log.abbrevcommit")) {
444 default_abbrev_commit
= git_config_bool(var
, value
);
447 if (!strcmp(var
, "log.date"))
448 return git_config_string(&default_date_mode
, var
, value
);
449 if (!strcmp(var
, "log.decorate")) {
450 decoration_style
= parse_decoration_style(value
);
451 if (decoration_style
< 0)
452 decoration_style
= 0; /* maybe warn? */
455 if (!strcmp(var
, "log.showroot")) {
456 default_show_root
= git_config_bool(var
, value
);
459 if (!strcmp(var
, "log.follow")) {
460 default_follow
= git_config_bool(var
, value
);
463 if (skip_prefix(var
, "color.decorate.", &slot_name
))
464 return parse_decorate_color_config(var
, slot_name
, value
);
465 if (!strcmp(var
, "log.mailmap")) {
466 use_mailmap_config
= git_config_bool(var
, value
);
469 if (!strcmp(var
, "log.showsignature")) {
470 default_show_signature
= git_config_bool(var
, value
);
474 if (grep_config(var
, value
, cb
) < 0)
476 if (git_gpg_config(var
, value
, cb
) < 0)
478 return git_diff_ui_config(var
, value
, cb
);
481 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
484 struct setup_revision_opt opt
;
487 git_config(git_log_config
, NULL
);
489 repo_init_revisions(the_repository
, &rev
, prefix
);
491 rev
.simplify_history
= 0;
492 memset(&opt
, 0, sizeof(opt
));
494 opt
.revarg_opt
= REVARG_COMMITTISH
;
495 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
496 if (!rev
.diffopt
.output_format
)
497 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
498 return cmd_log_walk(&rev
);
501 static void show_tagger(const char *buf
, struct rev_info
*rev
)
503 struct strbuf out
= STRBUF_INIT
;
504 struct pretty_print_context pp
= {0};
506 pp
.fmt
= rev
->commit_format
;
507 pp
.date_mode
= rev
->date_mode
;
508 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
509 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
510 strbuf_release(&out
);
513 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
515 struct object_id oidc
;
516 struct object_context obj_context
;
520 fflush(rev
->diffopt
.file
);
521 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
522 !rev
->diffopt
.flags
.allow_textconv
)
523 return stream_blob_to_fd(1, oid
, NULL
, 0);
525 if (get_oid_with_context(the_repository
, obj_name
,
527 &oidc
, &obj_context
))
528 die(_("not a valid object name %s"), obj_name
);
529 if (!obj_context
.path
||
530 !textconv_object(the_repository
, obj_context
.path
,
531 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
532 free(obj_context
.path
);
533 return stream_blob_to_fd(1, oid
, NULL
, 0);
537 die(_("git show %s: bad file"), obj_name
);
539 write_or_die(1, buf
, size
);
540 free(obj_context
.path
);
544 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
547 enum object_type type
;
548 char *buf
= read_object_file(oid
, &type
, &size
);
552 return error(_("could not read object %s"), oid_to_hex(oid
));
554 assert(type
== OBJ_TAG
);
555 while (offset
< size
&& buf
[offset
] != '\n') {
556 int new_offset
= offset
+ 1;
558 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
560 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
561 show_tagger(ident
, rev
);
566 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
571 static int show_tree_object(const struct object_id
*oid
,
573 const char *pathname
, unsigned mode
, int stage
, void *context
)
575 FILE *file
= context
;
576 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
580 static void show_setup_revisions_tweak(struct rev_info
*rev
,
581 struct setup_revision_opt
*opt
)
583 if (rev
->ignore_merges
) {
584 /* There was no "-m" on the command line */
585 rev
->ignore_merges
= 0;
586 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
587 /* No "--first-parent", "-c", or "--cc" */
588 rev
->combine_merges
= 1;
589 rev
->dense_combined_merges
= 1;
592 if (!rev
->diffopt
.output_format
)
593 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
596 int cmd_show(int argc
, const char **argv
, const char *prefix
)
599 struct object_array_entry
*objects
;
600 struct setup_revision_opt opt
;
601 struct pathspec match_all
;
602 int i
, count
, ret
= 0;
605 git_config(git_log_config
, NULL
);
607 memset(&match_all
, 0, sizeof(match_all
));
608 repo_init_revisions(the_repository
, &rev
, prefix
);
610 rev
.always_show_header
= 1;
611 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
612 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
614 memset(&opt
, 0, sizeof(opt
));
616 opt
.tweak
= show_setup_revisions_tweak
;
617 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
620 return cmd_log_walk(&rev
);
622 count
= rev
.pending
.nr
;
623 objects
= rev
.pending
.objects
;
624 for (i
= 0; i
< count
&& !ret
; i
++) {
625 struct object
*o
= objects
[i
].item
;
626 const char *name
= objects
[i
].name
;
629 ret
= show_blob_object(&o
->oid
, &rev
, name
);
632 struct tag
*t
= (struct tag
*)o
;
636 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
637 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
639 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
640 ret
= show_tag_object(&o
->oid
, &rev
);
644 o
= parse_object(the_repository
, &t
->tagged
->oid
);
646 ret
= error(_("could not read object %s"),
647 oid_to_hex(&t
->tagged
->oid
));
655 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
656 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
658 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
659 read_tree_recursive(the_repository
, (struct tree
*)o
, "",
660 0, 0, &match_all
, show_tree_object
,
665 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
666 rev
.pending
.objects
= NULL
;
667 add_object_array(o
, name
, &rev
.pending
);
668 ret
= cmd_log_walk(&rev
);
671 ret
= error(_("unknown type: %d"), o
->type
);
679 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
681 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
684 struct setup_revision_opt opt
;
687 git_config(git_log_config
, NULL
);
689 repo_init_revisions(the_repository
, &rev
, prefix
);
690 init_reflog_walk(&rev
.reflog_info
);
691 rev
.verbose_header
= 1;
692 memset(&opt
, 0, sizeof(opt
));
694 cmd_log_init_defaults(&rev
);
695 rev
.abbrev_commit
= 1;
696 rev
.commit_format
= CMIT_FMT_ONELINE
;
697 rev
.use_terminator
= 1;
698 rev
.always_show_header
= 1;
699 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
701 return cmd_log_walk(&rev
);
704 static void log_setup_revisions_tweak(struct rev_info
*rev
,
705 struct setup_revision_opt
*opt
)
707 if (rev
->diffopt
.flags
.default_follow_renames
&&
708 rev
->prune_data
.nr
== 1)
709 rev
->diffopt
.flags
.follow_renames
= 1;
711 /* Turn --cc/-c into -p --cc/-c when -p was not given */
712 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
713 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
715 /* Turn -m on when --cc/-c was given */
716 if (rev
->combine_merges
)
717 rev
->ignore_merges
= 0;
720 int cmd_log(int argc
, const char **argv
, const char *prefix
)
723 struct setup_revision_opt opt
;
726 git_config(git_log_config
, NULL
);
728 repo_init_revisions(the_repository
, &rev
, prefix
);
729 rev
.always_show_header
= 1;
730 memset(&opt
, 0, sizeof(opt
));
732 opt
.revarg_opt
= REVARG_COMMITTISH
;
733 opt
.tweak
= log_setup_revisions_tweak
;
734 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
735 return cmd_log_walk(&rev
);
740 static const char *fmt_patch_suffix
= ".patch";
741 static int numbered
= 0;
742 static int auto_number
= 1;
744 static char *default_attach
= NULL
;
746 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
747 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
748 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
750 static void add_header(const char *value
)
752 struct string_list_item
*item
;
753 int len
= strlen(value
);
754 while (len
&& value
[len
- 1] == '\n')
757 if (!strncasecmp(value
, "to: ", 4)) {
758 item
= string_list_append(&extra_to
, value
+ 4);
760 } else if (!strncasecmp(value
, "cc: ", 4)) {
761 item
= string_list_append(&extra_cc
, value
+ 4);
764 item
= string_list_append(&extra_hdr
, value
);
767 item
->string
[len
] = '\0';
770 #define THREAD_SHALLOW 1
771 #define THREAD_DEEP 2
773 static int do_signoff
;
774 static int base_auto
;
776 static const char *signature
= git_version_string
;
777 static const char *signature_file
;
778 static int config_cover_letter
;
779 static const char *config_output_directory
;
788 static int git_format_config(const char *var
, const char *value
, void *cb
)
790 struct rev_info
*rev
= cb
;
792 if (!strcmp(var
, "format.headers")) {
794 die(_("format.headers without value"));
798 if (!strcmp(var
, "format.suffix"))
799 return git_config_string(&fmt_patch_suffix
, var
, value
);
800 if (!strcmp(var
, "format.to")) {
802 return config_error_nonbool(var
);
803 string_list_append(&extra_to
, value
);
806 if (!strcmp(var
, "format.cc")) {
808 return config_error_nonbool(var
);
809 string_list_append(&extra_cc
, value
);
812 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
813 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
816 if (!strcmp(var
, "format.numbered")) {
817 if (value
&& !strcasecmp(value
, "auto")) {
821 numbered
= git_config_bool(var
, value
);
822 auto_number
= auto_number
&& numbered
;
825 if (!strcmp(var
, "format.attach")) {
827 default_attach
= xstrdup(value
);
829 default_attach
= xstrdup(git_version_string
);
832 if (!strcmp(var
, "format.thread")) {
833 if (value
&& !strcasecmp(value
, "deep")) {
834 thread
= THREAD_DEEP
;
837 if (value
&& !strcasecmp(value
, "shallow")) {
838 thread
= THREAD_SHALLOW
;
841 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
844 if (!strcmp(var
, "format.signoff")) {
845 do_signoff
= git_config_bool(var
, value
);
848 if (!strcmp(var
, "format.signature"))
849 return git_config_string(&signature
, var
, value
);
850 if (!strcmp(var
, "format.signaturefile"))
851 return git_config_pathname(&signature_file
, var
, value
);
852 if (!strcmp(var
, "format.coverletter")) {
853 if (value
&& !strcasecmp(value
, "auto")) {
854 config_cover_letter
= COVER_AUTO
;
857 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
860 if (!strcmp(var
, "format.outputdirectory"))
861 return git_config_string(&config_output_directory
, var
, value
);
862 if (!strcmp(var
, "format.useautobase")) {
863 base_auto
= git_config_bool(var
, value
);
866 if (!strcmp(var
, "format.from")) {
867 int b
= git_parse_maybe_bool(value
);
870 from
= xstrdup(value
);
872 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
877 if (!strcmp(var
, "format.notes")) {
878 struct strbuf buf
= STRBUF_INIT
;
879 int b
= git_parse_maybe_bool(value
);
884 strbuf_addstr(&buf
, value
);
885 expand_notes_ref(&buf
);
886 string_list_append(&rev
->notes_opt
.extra_notes_refs
,
887 strbuf_detach(&buf
, NULL
));
889 rev
->notes_opt
.use_default_notes
= 1;
894 return git_log_config(var
, value
, cb
);
897 static const char *output_directory
= NULL
;
898 static int outdir_offset
;
900 static int open_next_file(struct commit
*commit
, const char *subject
,
901 struct rev_info
*rev
, int quiet
)
903 struct strbuf filename
= STRBUF_INIT
;
904 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
906 if (output_directory
) {
907 strbuf_addstr(&filename
, output_directory
);
909 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
910 strbuf_release(&filename
);
911 return error(_("name of output directory is too long"));
913 strbuf_complete(&filename
, '/');
916 if (rev
->numbered_files
)
917 strbuf_addf(&filename
, "%d", rev
->nr
);
919 fmt_output_commit(&filename
, commit
, rev
);
921 fmt_output_subject(&filename
, subject
, rev
);
924 printf("%s\n", filename
.buf
+ outdir_offset
);
926 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
927 error_errno(_("cannot open patch file %s"), filename
.buf
);
928 strbuf_release(&filename
);
932 strbuf_release(&filename
);
936 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
938 struct rev_info check_rev
;
939 struct commit
*commit
, *c1
, *c2
;
940 struct object
*o1
, *o2
;
941 unsigned flags1
, flags2
;
943 if (rev
->pending
.nr
!= 2)
944 die(_("need exactly one range"));
946 o1
= rev
->pending
.objects
[0].item
;
947 o2
= rev
->pending
.objects
[1].item
;
950 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
951 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
953 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
954 die(_("not a range"));
956 init_patch_ids(the_repository
, ids
);
958 /* given a range a..b get all patch ids for b..a */
959 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
960 check_rev
.max_parents
= 1;
961 o1
->flags
^= UNINTERESTING
;
962 o2
->flags
^= UNINTERESTING
;
963 add_pending_object(&check_rev
, o1
, "o1");
964 add_pending_object(&check_rev
, o2
, "o2");
965 if (prepare_revision_walk(&check_rev
))
966 die(_("revision walk setup failed"));
968 while ((commit
= get_revision(&check_rev
)) != NULL
) {
969 add_commit_patch_id(commit
, ids
);
972 /* reset for next revision walk */
973 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
974 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
979 static void gen_message_id(struct rev_info
*info
, char *base
)
981 struct strbuf buf
= STRBUF_INIT
;
982 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
983 (timestamp_t
) time(NULL
),
984 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
985 info
->message_id
= strbuf_detach(&buf
, NULL
);
988 static void print_signature(FILE *file
)
990 if (!signature
|| !*signature
)
993 fprintf(file
, "-- \n%s", signature
);
994 if (signature
[strlen(signature
)-1] != '\n')
999 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
1001 struct strbuf desc
= STRBUF_INIT
;
1002 if (!branch_name
|| !*branch_name
)
1004 read_branch_desc(&desc
, branch_name
);
1006 strbuf_addch(buf
, '\n');
1007 strbuf_addbuf(buf
, &desc
);
1008 strbuf_addch(buf
, '\n');
1010 strbuf_release(&desc
);
1013 static char *find_branch_name(struct rev_info
*rev
)
1015 int i
, positive
= -1;
1016 struct object_id branch_oid
;
1017 const struct object_id
*tip_oid
;
1018 const char *ref
, *v
;
1019 char *full_ref
, *branch
= NULL
;
1021 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1022 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1031 ref
= rev
->cmdline
.rev
[positive
].name
;
1032 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1033 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
) &&
1034 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1035 oideq(tip_oid
, &branch_oid
))
1036 branch
= xstrdup(v
);
1041 static void show_diffstat(struct rev_info
*rev
,
1042 struct commit
*origin
, struct commit
*head
)
1044 struct diff_options opts
;
1046 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1047 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1048 diff_setup_done(&opts
);
1050 diff_tree_oid(get_commit_tree_oid(origin
),
1051 get_commit_tree_oid(head
),
1053 diffcore_std(&opts
);
1056 fprintf(rev
->diffopt
.file
, "\n");
1059 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1060 struct commit
*origin
,
1061 int nr
, struct commit
**list
,
1062 const char *branch_name
,
1065 const char *committer
;
1066 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1068 struct shortlog log
;
1069 struct strbuf sb
= STRBUF_INIT
;
1071 const char *encoding
= "UTF-8";
1072 int need_8bit_cte
= 0;
1073 struct pretty_print_context pp
= {0};
1074 struct commit
*head
= list
[0];
1076 if (!cmit_fmt_is_mail(rev
->commit_format
))
1077 die(_("cover letter needs email format"));
1079 committer
= git_committer_info(0);
1082 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1083 die(_("failed to create cover-letter file"));
1085 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1087 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1088 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1089 if (has_non_ascii(buf
))
1091 unuse_commit_buffer(list
[i
], buf
);
1095 branch_name
= find_branch_name(rev
);
1098 pp
.fmt
= CMIT_FMT_EMAIL
;
1099 pp
.date_mode
.type
= DATE_RFC2822
;
1101 pp
.print_email_subject
= 1;
1102 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1103 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1104 pp_remainder(&pp
, &msg
, &sb
, 0);
1105 add_branch_description(&sb
, branch_name
);
1106 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1108 strbuf_release(&sb
);
1110 shortlog_init(&log
);
1112 log
.wrap
= MAIL_DEFAULT_WRAP
;
1115 log
.file
= rev
->diffopt
.file
;
1116 for (i
= 0; i
< nr
; i
++)
1117 shortlog_add_commit(&log
, list
[i
]);
1119 shortlog_output(&log
);
1121 /* We can only do diffstat with a unique reference point */
1123 show_diffstat(rev
, origin
, head
);
1125 if (rev
->idiff_oid1
) {
1126 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1127 show_interdiff(rev
, 0);
1132 * Pass minimum required diff-options to range-diff; others
1133 * can be added later if deemed desirable.
1135 struct diff_options opts
;
1137 opts
.file
= rev
->diffopt
.file
;
1138 opts
.use_color
= rev
->diffopt
.use_color
;
1139 diff_setup_done(&opts
);
1140 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1141 show_range_diff(rev
->rdiff1
, rev
->rdiff2
,
1142 rev
->creation_factor
, 1, &opts
);
1146 static const char *clean_message_id(const char *msg_id
)
1149 const char *a
, *z
, *m
;
1152 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1157 if (!isspace(ch
) && (ch
!= '>'))
1162 die(_("insane in-reply-to: %s"), msg_id
);
1165 return xmemdupz(a
, z
- a
);
1168 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1170 if (output_directory
&& is_absolute_path(output_directory
))
1171 return output_directory
;
1173 if (!prefix
|| !*prefix
) {
1174 if (output_directory
)
1175 return output_directory
;
1176 /* The user did not explicitly ask for "./" */
1181 outdir_offset
= strlen(prefix
);
1182 if (!output_directory
)
1185 return prefix_filename(prefix
, output_directory
);
1188 static const char * const builtin_format_patch_usage
[] = {
1189 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1193 static int keep_subject
= 0;
1195 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1197 BUG_ON_OPT_NEG(unset
);
1198 BUG_ON_OPT_ARG(arg
);
1199 ((struct rev_info
*)opt
->value
)->total
= -1;
1204 static int subject_prefix
= 0;
1206 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1209 BUG_ON_OPT_NEG(unset
);
1211 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1215 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1217 BUG_ON_OPT_NEG(unset
);
1218 BUG_ON_OPT_ARG(arg
);
1219 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1222 static int numbered_cmdline_opt
= 0;
1224 static int numbered_callback(const struct option
*opt
, const char *arg
,
1227 BUG_ON_OPT_ARG(arg
);
1228 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1234 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1237 BUG_ON_OPT_NEG(unset
);
1238 return numbered_callback(opt
, arg
, 1);
1241 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1244 const char **dir
= (const char **)opt
->value
;
1245 BUG_ON_OPT_NEG(unset
);
1247 die(_("two output directories?"));
1252 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1254 int *thread
= (int *)opt
->value
;
1257 else if (!arg
|| !strcmp(arg
, "shallow"))
1258 *thread
= THREAD_SHALLOW
;
1259 else if (!strcmp(arg
, "deep"))
1260 *thread
= THREAD_DEEP
;
1262 * Please update _git_formatpatch() in git-completion.bash
1263 * when you add new options.
1270 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1272 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1274 rev
->mime_boundary
= NULL
;
1276 rev
->mime_boundary
= arg
;
1278 rev
->mime_boundary
= git_version_string
;
1279 rev
->no_inline
= unset
? 0 : 1;
1283 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1285 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1287 rev
->mime_boundary
= NULL
;
1289 rev
->mime_boundary
= arg
;
1291 rev
->mime_boundary
= git_version_string
;
1296 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1299 string_list_clear(&extra_hdr
, 0);
1300 string_list_clear(&extra_to
, 0);
1301 string_list_clear(&extra_cc
, 0);
1308 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1311 string_list_clear(&extra_to
, 0);
1313 string_list_append(&extra_to
, arg
);
1317 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1320 string_list_clear(&extra_cc
, 0);
1322 string_list_append(&extra_cc
, arg
);
1326 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1328 char **from
= opt
->value
;
1335 *from
= xstrdup(arg
);
1337 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1341 struct base_tree_info
{
1342 struct object_id base_commit
;
1343 int nr_patch_id
, alloc_patch_id
;
1344 struct object_id
*patch_id
;
1347 static struct commit
*get_base_commit(const char *base_commit
,
1348 struct commit
**list
,
1351 struct commit
*base
= NULL
;
1352 struct commit
**rev
;
1353 int i
= 0, rev_nr
= 0;
1355 if (base_commit
&& strcmp(base_commit
, "auto")) {
1356 base
= lookup_commit_reference_by_name(base_commit
);
1358 die(_("unknown commit %s"), base_commit
);
1359 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1360 struct branch
*curr_branch
= branch_get(NULL
);
1361 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1363 struct commit_list
*base_list
;
1364 struct commit
*commit
;
1365 struct object_id oid
;
1367 if (get_oid(upstream
, &oid
))
1368 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1369 commit
= lookup_commit_or_die(&oid
, "upstream base");
1370 base_list
= get_merge_bases_many(commit
, total
, list
);
1371 /* There should be one and only one merge base. */
1372 if (!base_list
|| base_list
->next
)
1373 die(_("could not find exact merge base"));
1374 base
= base_list
->item
;
1375 free_commit_list(base_list
);
1377 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1378 "please use git branch --set-upstream-to to track a remote branch.\n"
1379 "Or you could specify base commit by --base=<base-commit-id> manually"));
1383 ALLOC_ARRAY(rev
, total
);
1384 for (i
= 0; i
< total
; i
++)
1389 * Get merge base through pair-wise computations
1390 * and store it in rev[0].
1392 while (rev_nr
> 1) {
1393 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1394 struct commit_list
*merge_base
;
1395 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1396 if (!merge_base
|| merge_base
->next
)
1397 die(_("failed to find exact merge base"));
1399 rev
[i
] = merge_base
->item
;
1403 rev
[i
] = rev
[2 * i
];
1404 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1407 if (!in_merge_bases(base
, rev
[0]))
1408 die(_("base commit should be the ancestor of revision list"));
1410 for (i
= 0; i
< total
; i
++) {
1411 if (base
== list
[i
])
1412 die(_("base commit shouldn't be in revision list"));
1419 define_commit_slab(commit_base
, int);
1421 static void prepare_bases(struct base_tree_info
*bases
,
1422 struct commit
*base
,
1423 struct commit
**list
,
1426 struct commit
*commit
;
1427 struct rev_info revs
;
1428 struct diff_options diffopt
;
1429 struct commit_base commit_base
;
1435 init_commit_base(&commit_base
);
1436 repo_diff_setup(the_repository
, &diffopt
);
1437 diffopt
.flags
.recursive
= 1;
1438 diff_setup_done(&diffopt
);
1440 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1442 repo_init_revisions(the_repository
, &revs
, NULL
);
1443 revs
.max_parents
= 1;
1444 revs
.topo_order
= 1;
1445 for (i
= 0; i
< total
; i
++) {
1446 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1447 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1448 *commit_base_at(&commit_base
, list
[i
]) = 1;
1450 base
->object
.flags
|= UNINTERESTING
;
1451 add_pending_object(&revs
, &base
->object
, "base");
1453 if (prepare_revision_walk(&revs
))
1454 die(_("revision walk setup failed"));
1456 * Traverse the commits list, get prerequisite patch ids
1457 * and stuff them in bases structure.
1459 while ((commit
= get_revision(&revs
)) != NULL
) {
1460 struct object_id oid
;
1461 struct object_id
*patch_id
;
1462 if (*commit_base_at(&commit_base
, commit
))
1464 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1465 die(_("cannot get patch id"));
1466 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1467 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1468 oidcpy(patch_id
, &oid
);
1469 bases
->nr_patch_id
++;
1471 clear_commit_base(&commit_base
);
1474 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1478 /* Only do this once, either for the cover or for the first one */
1479 if (is_null_oid(&bases
->base_commit
))
1482 /* Show the base commit */
1483 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1485 /* Show the prerequisite patches */
1486 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1487 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1489 free(bases
->patch_id
);
1490 bases
->nr_patch_id
= 0;
1491 bases
->alloc_patch_id
= 0;
1492 oidclr(&bases
->base_commit
);
1495 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1496 const char *generic
, const char *rerolled
)
1498 if (reroll_count
<= 0)
1499 strbuf_addstr(sb
, generic
);
1500 else /* RFC may be v0, so allow -v1 to diff against v0 */
1501 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1505 static void infer_range_diff_ranges(struct strbuf
*r1
,
1508 struct commit
*origin
,
1509 struct commit
*head
)
1511 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1513 if (!strstr(prev
, "..")) {
1514 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1515 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1516 } else if (!origin
) {
1517 die(_("failed to infer range-diff ranges"));
1519 strbuf_addstr(r1
, prev
);
1520 strbuf_addf(r2
, "%s..%s",
1521 oid_to_hex(&origin
->object
.oid
), head_oid
);
1525 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1527 struct commit
*commit
;
1528 struct commit
**list
= NULL
;
1529 struct rev_info rev
;
1530 struct setup_revision_opt s_r_opt
;
1531 int nr
= 0, total
, i
;
1533 int start_number
= -1;
1534 int just_numbers
= 0;
1535 int ignore_if_in_upstream
= 0;
1536 int cover_letter
= -1;
1537 int boundary_count
= 0;
1538 int no_binary_diff
= 0;
1539 int zero_commit
= 0;
1540 struct commit
*origin
= NULL
;
1541 const char *in_reply_to
= NULL
;
1542 struct patch_ids ids
;
1543 struct strbuf buf
= STRBUF_INIT
;
1544 int use_patch_format
= 0;
1546 int reroll_count
= -1;
1547 char *branch_name
= NULL
;
1548 char *base_commit
= NULL
;
1549 struct base_tree_info bases
;
1550 int show_progress
= 0;
1551 struct progress
*progress
= NULL
;
1552 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1553 struct strbuf idiff_title
= STRBUF_INIT
;
1554 const char *rdiff_prev
= NULL
;
1555 struct strbuf rdiff1
= STRBUF_INIT
;
1556 struct strbuf rdiff2
= STRBUF_INIT
;
1557 struct strbuf rdiff_title
= STRBUF_INIT
;
1558 int creation_factor
= -1;
1560 const struct option builtin_format_patch_options
[] = {
1561 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1562 N_("use [PATCH n/m] even with a single patch"),
1563 PARSE_OPT_NOARG
, numbered_callback
},
1564 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1565 N_("use [PATCH] even with multiple patches"),
1566 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
},
1567 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1568 OPT_BOOL(0, "stdout", &use_stdout
,
1569 N_("print patches to standard out")),
1570 OPT_BOOL(0, "cover-letter", &cover_letter
,
1571 N_("generate a cover letter")),
1572 OPT_BOOL(0, "numbered-files", &just_numbers
,
1573 N_("use simple number sequence for output file names")),
1574 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1575 N_("use <sfx> instead of '.patch'")),
1576 OPT_INTEGER(0, "start-number", &start_number
,
1577 N_("start numbering patches at <n> instead of 1")),
1578 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1579 N_("mark the series as Nth re-roll")),
1580 { OPTION_CALLBACK
, 0, "rfc", &rev
, NULL
,
1581 N_("Use [RFC PATCH] instead of [PATCH]"),
1582 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
},
1583 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1584 N_("Use [<prefix>] instead of [PATCH]"),
1585 PARSE_OPT_NONEG
, subject_prefix_callback
},
1586 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1587 N_("dir"), N_("store resulting files in <dir>"),
1588 PARSE_OPT_NONEG
, output_directory_callback
},
1589 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1590 N_("don't strip/add [PATCH]"),
1591 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1592 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1593 N_("don't output binary diffs")),
1594 OPT_BOOL(0, "zero-commit", &zero_commit
,
1595 N_("output all-zero hash in From header")),
1596 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1597 N_("don't include a patch matching a commit upstream")),
1598 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1599 N_("show patch format instead of default (patch + stat)"),
1600 1, PARSE_OPT_NONEG
),
1601 OPT_GROUP(N_("Messaging")),
1602 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1603 N_("add email header"), 0, header_callback
},
1604 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1606 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1608 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1609 N_("set From address to <ident> (or committer ident if absent)"),
1610 PARSE_OPT_OPTARG
, from_callback
},
1611 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1612 N_("make first mail a reply to <message-id>")),
1613 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1614 N_("attach the patch"), PARSE_OPT_OPTARG
,
1616 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1617 N_("inline the patch"),
1618 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1620 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1621 N_("enable message threading, styles: shallow, deep"),
1622 PARSE_OPT_OPTARG
, thread_callback
},
1623 OPT_STRING(0, "signature", &signature
, N_("signature"),
1624 N_("add a signature")),
1625 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1626 N_("add prerequisite tree info to the patch series")),
1627 OPT_FILENAME(0, "signature-file", &signature_file
,
1628 N_("add a signature from a file")),
1629 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1630 OPT_BOOL(0, "progress", &show_progress
,
1631 N_("show progress while generating patches")),
1632 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1633 N_("show changes against <rev> in cover letter or single patch"),
1634 parse_opt_object_name
),
1635 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1636 N_("show changes against <refspec> in cover letter or single patch")),
1637 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1638 N_("percentage by which creation is weighted")),
1642 extra_hdr
.strdup_strings
= 1;
1643 extra_to
.strdup_strings
= 1;
1644 extra_cc
.strdup_strings
= 1;
1645 init_log_defaults();
1646 repo_init_revisions(the_repository
, &rev
, prefix
);
1647 git_config(git_format_config
, &rev
);
1648 rev
.commit_format
= CMIT_FMT_EMAIL
;
1649 rev
.expand_tabs_in_log_default
= 0;
1650 rev
.verbose_header
= 1;
1652 rev
.max_parents
= 1;
1653 rev
.diffopt
.flags
.recursive
= 1;
1654 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1655 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1656 s_r_opt
.def
= "HEAD";
1657 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1659 if (default_attach
) {
1660 rev
.mime_boundary
= default_attach
;
1665 * Parse the arguments before setup_revisions(), or something
1666 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1667 * possibly a valid SHA1.
1669 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1670 builtin_format_patch_usage
,
1671 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1672 PARSE_OPT_KEEP_DASHDASH
);
1674 if (0 < reroll_count
) {
1675 struct strbuf sprefix
= STRBUF_INIT
;
1676 strbuf_addf(&sprefix
, "%s v%d",
1677 rev
.subject_prefix
, reroll_count
);
1678 rev
.reroll_count
= reroll_count
;
1679 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1682 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1683 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1684 strbuf_addch(&buf
, '\n');
1688 strbuf_addstr(&buf
, "To: ");
1689 for (i
= 0; i
< extra_to
.nr
; i
++) {
1691 strbuf_addstr(&buf
, " ");
1692 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1693 if (i
+ 1 < extra_to
.nr
)
1694 strbuf_addch(&buf
, ',');
1695 strbuf_addch(&buf
, '\n');
1699 strbuf_addstr(&buf
, "Cc: ");
1700 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1702 strbuf_addstr(&buf
, " ");
1703 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1704 if (i
+ 1 < extra_cc
.nr
)
1705 strbuf_addch(&buf
, ',');
1706 strbuf_addch(&buf
, '\n');
1709 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1712 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1713 die(_("invalid ident line: %s"), from
);
1716 if (start_number
< 0)
1720 * If numbered is set solely due to format.numbered in config,
1721 * and it would conflict with --keep-subject (-k) from the
1722 * command line, reset "numbered".
1724 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1727 if (numbered
&& keep_subject
)
1728 die(_("-n and -k are mutually exclusive"));
1729 if (keep_subject
&& subject_prefix
)
1730 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1731 rev
.preserve_subject
= keep_subject
;
1733 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1735 die(_("unrecognized argument: %s"), argv
[1]);
1737 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1738 die(_("--name-only does not make sense"));
1739 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1740 die(_("--name-status does not make sense"));
1741 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1742 die(_("--check does not make sense"));
1744 if (!use_patch_format
&&
1745 (!rev
.diffopt
.output_format
||
1746 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1747 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1748 if (!rev
.diffopt
.stat_width
)
1749 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1751 /* Always generate a patch */
1752 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1754 rev
.zero_commit
= zero_commit
;
1756 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1757 rev
.diffopt
.flags
.binary
= 1;
1760 init_display_notes(&rev
.notes_opt
);
1762 if (!output_directory
&& !use_stdout
)
1763 output_directory
= config_output_directory
;
1766 output_directory
= set_outdir(prefix
, output_directory
);
1770 if (output_directory
) {
1771 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1772 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1774 die(_("standard output, or directory, which one?"));
1775 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1776 die_errno(_("could not create directory '%s'"),
1780 if (rev
.pending
.nr
== 1) {
1783 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1785 * This is traditional behaviour of "git format-patch
1786 * origin" that prepares what the origin side still
1789 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1790 add_head_to_pending(&rev
);
1794 * Otherwise, it is "format-patch -22 HEAD", and/or
1795 * "format-patch --root HEAD". The user wants
1796 * get_revision() to do the usual traversal.
1799 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1803 const char *ref
, *v
;
1804 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1806 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1807 branch_name
= xstrdup(v
);
1809 branch_name
= xstrdup(""); /* no branch */
1814 * We cannot move this anywhere earlier because we do want to
1815 * know if --root was given explicitly from the command line.
1817 rev
.show_root_diff
= 1;
1819 if (ignore_if_in_upstream
) {
1820 /* Don't say anything if head and upstream are the same. */
1821 if (rev
.pending
.nr
== 2) {
1822 struct object_array_entry
*o
= rev
.pending
.objects
;
1823 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
1826 get_patch_ids(&rev
, &ids
);
1829 if (prepare_revision_walk(&rev
))
1830 die(_("revision walk setup failed"));
1832 while ((commit
= get_revision(&rev
)) != NULL
) {
1833 if (commit
->object
.flags
& BOUNDARY
) {
1835 origin
= (boundary_count
== 1) ? commit
: NULL
;
1839 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1843 REALLOC_ARRAY(list
, nr
);
1844 list
[nr
- 1] = commit
;
1850 if (cover_letter
== -1) {
1851 if (config_cover_letter
== COVER_AUTO
)
1852 cover_letter
= (total
> 1);
1854 cover_letter
= (config_cover_letter
== COVER_ON
);
1856 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1859 rev
.total
= total
+ start_number
- 1;
1861 if (idiff_prev
.nr
) {
1862 if (!cover_letter
&& total
!= 1)
1863 die(_("--interdiff requires --cover-letter or single patch"));
1864 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
1865 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
1866 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
1868 _("Interdiff against v%d:"));
1871 if (creation_factor
< 0)
1872 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
1873 else if (!rdiff_prev
)
1874 die(_("--creation-factor requires --range-diff"));
1877 if (!cover_letter
&& total
!= 1)
1878 die(_("--range-diff requires --cover-letter or single patch"));
1880 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
1882 rev
.rdiff1
= rdiff1
.buf
;
1883 rev
.rdiff2
= rdiff2
.buf
;
1884 rev
.creation_factor
= creation_factor
;
1885 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
1887 _("Range-diff against v%d:"));
1891 ; /* --no-signature inhibits all signatures */
1892 } else if (signature
&& signature
!= git_version_string
) {
1893 ; /* non-default signature already set */
1894 } else if (signature_file
) {
1895 struct strbuf buf
= STRBUF_INIT
;
1897 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1898 die_errno(_("unable to read signature file '%s'"), signature_file
);
1899 signature
= strbuf_detach(&buf
, NULL
);
1902 memset(&bases
, 0, sizeof(bases
));
1903 if (base_commit
|| base_auto
) {
1904 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1905 reset_revision_walk();
1906 clear_object_flags(UNINTERESTING
);
1907 prepare_bases(&bases
, base
, list
, nr
);
1910 if (in_reply_to
|| thread
|| cover_letter
)
1911 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1913 const char *msgid
= clean_message_id(in_reply_to
);
1914 string_list_append(rev
.ref_message_ids
, msgid
);
1916 rev
.numbered_files
= just_numbers
;
1917 rev
.patch_suffix
= fmt_patch_suffix
;
1920 gen_message_id(&rev
, "cover");
1921 make_cover_letter(&rev
, use_stdout
,
1922 origin
, nr
, list
, branch_name
, quiet
);
1923 print_bases(&bases
, rev
.diffopt
.file
);
1924 print_signature(rev
.diffopt
.file
);
1927 /* interdiff/range-diff in cover-letter; omit from patches */
1928 rev
.idiff_oid1
= NULL
;
1931 rev
.add_signoff
= do_signoff
;
1934 progress
= start_delayed_progress(_("Generating patches"), total
);
1937 display_progress(progress
, total
- nr
);
1939 rev
.nr
= total
- nr
+ (start_number
- 1);
1940 /* Make the second and subsequent mails replies to the first */
1942 /* Have we already had a message ID? */
1943 if (rev
.message_id
) {
1945 * For deep threading: make every mail
1946 * a reply to the previous one, no
1947 * matter what other options are set.
1949 * For shallow threading:
1951 * Without --cover-letter and
1952 * --in-reply-to, make every mail a
1953 * reply to the one before.
1955 * With --in-reply-to but no
1956 * --cover-letter, make every mail a
1957 * reply to the <reply-to>.
1959 * With --cover-letter, make every
1960 * mail but the cover letter a reply
1961 * to the cover letter. The cover
1962 * letter is a reply to the
1963 * --in-reply-to, if specified.
1965 if (thread
== THREAD_SHALLOW
1966 && rev
.ref_message_ids
->nr
> 0
1967 && (!cover_letter
|| rev
.nr
> 1))
1968 free(rev
.message_id
);
1970 string_list_append(rev
.ref_message_ids
,
1973 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1977 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1978 die(_("failed to create output files"));
1979 shown
= log_tree_commit(&rev
, commit
);
1980 free_commit_buffer(the_repository
->parsed_objects
,
1983 /* We put one extra blank line between formatted
1984 * patches and this flag is used by log-tree code
1985 * to see if it needs to emit a LF before showing
1986 * the log; when using one file per patch, we do
1987 * not want the extra blank line.
1992 print_bases(&bases
, rev
.diffopt
.file
);
1993 if (rev
.mime_boundary
)
1994 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
1995 mime_boundary_leader
,
1998 print_signature(rev
.diffopt
.file
);
2001 fclose(rev
.diffopt
.file
);
2003 stop_progress(&progress
);
2006 string_list_clear(&extra_to
, 0);
2007 string_list_clear(&extra_cc
, 0);
2008 string_list_clear(&extra_hdr
, 0);
2009 if (ignore_if_in_upstream
)
2010 free_patch_ids(&ids
);
2013 oid_array_clear(&idiff_prev
);
2014 strbuf_release(&idiff_title
);
2015 strbuf_release(&rdiff1
);
2016 strbuf_release(&rdiff2
);
2017 strbuf_release(&rdiff_title
);
2021 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2023 struct object_id oid
;
2024 if (get_oid(arg
, &oid
) == 0) {
2025 struct commit
*commit
= lookup_commit_reference(the_repository
,
2028 commit
->object
.flags
|= flags
;
2029 add_pending_object(revs
, &commit
->object
, arg
);
2036 static const char * const cherry_usage
[] = {
2037 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2041 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2042 int abbrev
, FILE *file
)
2045 fprintf(file
, "%c %s\n", sign
,
2046 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2048 struct strbuf buf
= STRBUF_INIT
;
2049 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2050 fprintf(file
, "%c %s %s\n", sign
,
2051 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2053 strbuf_release(&buf
);
2057 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2059 struct rev_info revs
;
2060 struct patch_ids ids
;
2061 struct commit
*commit
;
2062 struct commit_list
*list
= NULL
;
2063 struct branch
*current_branch
;
2064 const char *upstream
;
2065 const char *head
= "HEAD";
2066 const char *limit
= NULL
;
2067 int verbose
= 0, abbrev
= 0;
2069 struct option options
[] = {
2070 OPT__ABBREV(&abbrev
),
2071 OPT__VERBOSE(&verbose
, N_("be verbose")),
2075 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2088 current_branch
= branch_get(NULL
);
2089 upstream
= branch_get_upstream(current_branch
, NULL
);
2091 fprintf(stderr
, _("Could not find a tracked"
2092 " remote branch, please"
2093 " specify <upstream> manually.\n"));
2094 usage_with_options(cherry_usage
, options
);
2098 repo_init_revisions(the_repository
, &revs
, prefix
);
2099 revs
.max_parents
= 1;
2101 if (add_pending_commit(head
, &revs
, 0))
2102 die(_("unknown commit %s"), head
);
2103 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2104 die(_("unknown commit %s"), upstream
);
2106 /* Don't say anything if head and upstream are the same. */
2107 if (revs
.pending
.nr
== 2) {
2108 struct object_array_entry
*o
= revs
.pending
.objects
;
2109 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2113 get_patch_ids(&revs
, &ids
);
2115 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2116 die(_("unknown commit %s"), limit
);
2118 /* reverse the list of commits */
2119 if (prepare_revision_walk(&revs
))
2120 die(_("revision walk setup failed"));
2121 while ((commit
= get_revision(&revs
)) != NULL
) {
2122 commit_list_insert(commit
, &list
);
2128 commit
= list
->item
;
2129 if (has_commit_patch_id(commit
, &ids
))
2131 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2135 free_patch_ids(&ids
);