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
;
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 auto_decoration_style(void)
68 return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS
: 0;
71 static int parse_decoration_style(const char *value
)
73 switch (git_parse_maybe_bool(value
)) {
75 return DECORATE_SHORT_REFS
;
81 if (!strcmp(value
, "full"))
82 return DECORATE_FULL_REFS
;
83 else if (!strcmp(value
, "short"))
84 return DECORATE_SHORT_REFS
;
85 else if (!strcmp(value
, "auto"))
86 return auto_decoration_style();
88 * Please update _git_log() in git-completion.bash when you
89 * add new decoration styles.
94 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
99 decoration_style
= parse_decoration_style(arg
);
101 decoration_style
= DECORATE_SHORT_REFS
;
103 if (decoration_style
< 0)
104 die(_("invalid --decorate option: %s"), arg
);
106 decoration_given
= 1;
111 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
113 struct line_opt_callback_data
*data
= option
->value
;
115 BUG_ON_OPT_NEG(unset
);
120 data
->rev
->line_level_traverse
= 1;
121 string_list_append(&data
->args
, arg
);
126 static void init_log_defaults(void)
128 init_grep_defaults(the_repository
);
129 init_diff_ui_defaults();
131 decoration_style
= auto_decoration_style();
134 static void cmd_log_init_defaults(struct rev_info
*rev
)
137 get_commit_format(fmt_pretty
, rev
);
139 rev
->diffopt
.flags
.default_follow_renames
= 1;
140 rev
->verbose_header
= 1;
141 rev
->diffopt
.flags
.recursive
= 1;
142 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
143 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
144 rev
->abbrev_commit
= default_abbrev_commit
;
145 rev
->show_root_diff
= default_show_root
;
146 rev
->subject_prefix
= fmt_patch_subject_prefix
;
147 rev
->show_signature
= default_show_signature
;
148 rev
->diffopt
.flags
.allow_textconv
= 1;
150 if (default_date_mode
)
151 parse_date_format(default_date_mode
, &rev
->date_mode
);
154 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
155 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
157 struct userformat_want w
;
158 int quiet
= 0, source
= 0, mailmap
= 0;
159 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
160 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
161 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
162 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
163 &decorate_refs_exclude
};
164 static struct revision_sources revision_sources
;
166 const struct option builtin_log_options
[] = {
167 OPT__QUIET(&quiet
, N_("suppress diff output")),
168 OPT_BOOL(0, "source", &source
, N_("show source")),
169 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
170 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
171 N_("pattern"), N_("only decorate refs that match <pattern>")),
172 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
173 N_("pattern"), N_("do not decorate refs that match <pattern>")),
174 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
175 PARSE_OPT_OPTARG
, decorate_callback
},
176 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
177 N_("Process line range n,m in file, counting from 1"),
178 log_line_range_callback
),
183 line_cb
.prefix
= prefix
;
185 mailmap
= use_mailmap_config
;
186 argc
= parse_options(argc
, argv
, prefix
,
187 builtin_log_options
, builtin_log_usage
,
188 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
189 PARSE_OPT_KEEP_DASHDASH
);
192 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
193 argc
= setup_revisions(argc
, argv
, rev
, opt
);
195 /* Any arguments at this point are not recognized */
197 die(_("unrecognized argument: %s"), argv
[1]);
199 memset(&w
, 0, sizeof(w
));
200 userformat_find_requirements(NULL
, &w
);
202 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
205 load_display_notes(&rev
->notes_opt
);
207 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
208 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
209 rev
->always_show_header
= 0;
211 if (source
|| w
.source
) {
212 init_revision_sources(&revision_sources
);
213 rev
->sources
= &revision_sources
;
217 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
218 read_mailmap(rev
->mailmap
, NULL
);
221 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
223 * "log --pretty=raw" is special; ignore UI oriented
224 * configuration variables such as decoration.
226 if (!decoration_given
)
227 decoration_style
= 0;
228 if (!rev
->abbrev_commit_given
)
229 rev
->abbrev_commit
= 0;
232 if (decoration_style
) {
233 rev
->show_decorations
= 1;
234 load_ref_decorations(&decoration_filter
, decoration_style
);
237 if (rev
->line_level_traverse
)
238 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
243 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
244 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
246 cmd_log_init_defaults(rev
);
247 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
251 * This gives a rough estimate for how many commits we
252 * will print out in the list.
254 static int estimate_commit_count(struct commit_list
*list
)
259 struct commit
*commit
= list
->item
;
260 unsigned int flags
= commit
->object
.flags
;
262 if (!(flags
& (TREESAME
| UNINTERESTING
)))
268 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
270 if (rev
->shown_one
) {
272 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
273 putchar(rev
->diffopt
.line_termination
);
275 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
278 static struct itimerval early_output_timer
;
280 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
282 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
285 revs
->diffopt
.close_file
= 0;
286 sort_in_topological_order(&list
, revs
->sort_order
);
288 struct commit
*commit
= list
->item
;
289 switch (simplify_commit(revs
, commit
)) {
292 int n
= estimate_commit_count(list
);
293 show_early_header(revs
, "incomplete", n
);
296 log_tree_commit(revs
, commit
);
303 fclose(revs
->diffopt
.file
);
309 /* Did we already get enough commits for the early output? */
312 fclose(revs
->diffopt
.file
);
317 * ..if no, then repeat it twice a second until we
320 * NOTE! We don't use "it_interval", because if the
321 * reader isn't listening, we want our output to be
322 * throttled by the writing, and not have the timer
323 * trigger every second even if we're blocked on a
326 early_output_timer
.it_value
.tv_sec
= 0;
327 early_output_timer
.it_value
.tv_usec
= 500000;
328 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
331 static void early_output(int signal
)
333 show_early_output
= log_show_early
;
336 static void setup_early_output(void)
341 * Set up the signal handler, minimally intrusively:
342 * we only set a single volatile integer word (not
343 * using sigatomic_t - trying to avoid unnecessary
344 * system dependencies and headers), and using
347 memset(&sa
, 0, sizeof(sa
));
348 sa
.sa_handler
= early_output
;
349 sigemptyset(&sa
.sa_mask
);
350 sa
.sa_flags
= SA_RESTART
;
351 sigaction(SIGALRM
, &sa
, NULL
);
354 * If we can get the whole output in less than a
355 * tenth of a second, don't even bother doing the
356 * early-output thing..
358 * This is a one-time-only trigger.
360 early_output_timer
.it_value
.tv_sec
= 0;
361 early_output_timer
.it_value
.tv_usec
= 100000;
362 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
365 static void finish_early_output(struct rev_info
*rev
)
367 int n
= estimate_commit_count(rev
->commits
);
368 signal(SIGALRM
, SIG_IGN
);
369 show_early_header(rev
, "done", n
);
372 static int cmd_log_walk(struct rev_info
*rev
)
374 struct commit
*commit
;
376 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
378 if (rev
->early_output
)
379 setup_early_output();
381 if (prepare_revision_walk(rev
))
382 die(_("revision walk setup failed"));
384 if (rev
->early_output
)
385 finish_early_output(rev
);
388 * For --check and --exit-code, the exit code is based on CHECK_FAILED
389 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
390 * retain that state information if replacing rev->diffopt in this loop
392 rev
->diffopt
.close_file
= 0;
393 while ((commit
= get_revision(rev
)) != NULL
) {
394 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
396 * We decremented max_count in get_revision,
397 * but we didn't actually show the commit.
400 if (!rev
->reflog_info
) {
402 * We may show a given commit multiple times when
403 * walking the reflogs.
405 free_commit_buffer(the_repository
->parsed_objects
,
407 free_commit_list(commit
->parents
);
408 commit
->parents
= NULL
;
410 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
411 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
412 if (rev
->diffopt
.degraded_cc_to_c
)
415 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
416 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
418 fclose(rev
->diffopt
.file
);
420 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
421 rev
->diffopt
.flags
.check_failed
) {
424 return diff_result_code(&rev
->diffopt
, 0);
427 static int git_log_config(const char *var
, const char *value
, void *cb
)
429 const char *slot_name
;
431 if (!strcmp(var
, "format.pretty"))
432 return git_config_string(&fmt_pretty
, var
, value
);
433 if (!strcmp(var
, "format.subjectprefix"))
434 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
435 if (!strcmp(var
, "log.abbrevcommit")) {
436 default_abbrev_commit
= git_config_bool(var
, value
);
439 if (!strcmp(var
, "log.date"))
440 return git_config_string(&default_date_mode
, var
, value
);
441 if (!strcmp(var
, "log.decorate")) {
442 decoration_style
= parse_decoration_style(value
);
443 if (decoration_style
< 0)
444 decoration_style
= 0; /* maybe warn? */
447 if (!strcmp(var
, "log.showroot")) {
448 default_show_root
= git_config_bool(var
, value
);
451 if (!strcmp(var
, "log.follow")) {
452 default_follow
= git_config_bool(var
, value
);
455 if (skip_prefix(var
, "color.decorate.", &slot_name
))
456 return parse_decorate_color_config(var
, slot_name
, value
);
457 if (!strcmp(var
, "log.mailmap")) {
458 use_mailmap_config
= git_config_bool(var
, value
);
461 if (!strcmp(var
, "log.showsignature")) {
462 default_show_signature
= git_config_bool(var
, value
);
466 if (grep_config(var
, value
, cb
) < 0)
468 if (git_gpg_config(var
, value
, cb
) < 0)
470 return git_diff_ui_config(var
, value
, cb
);
473 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
476 struct setup_revision_opt opt
;
479 git_config(git_log_config
, NULL
);
481 repo_init_revisions(the_repository
, &rev
, prefix
);
483 rev
.simplify_history
= 0;
484 memset(&opt
, 0, sizeof(opt
));
486 opt
.revarg_opt
= REVARG_COMMITTISH
;
487 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
488 if (!rev
.diffopt
.output_format
)
489 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
490 return cmd_log_walk(&rev
);
493 static void show_tagger(const char *buf
, struct rev_info
*rev
)
495 struct strbuf out
= STRBUF_INIT
;
496 struct pretty_print_context pp
= {0};
498 pp
.fmt
= rev
->commit_format
;
499 pp
.date_mode
= rev
->date_mode
;
500 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
501 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
502 strbuf_release(&out
);
505 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
507 struct object_id oidc
;
508 struct object_context obj_context
;
512 fflush(rev
->diffopt
.file
);
513 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
514 !rev
->diffopt
.flags
.allow_textconv
)
515 return stream_blob_to_fd(1, oid
, NULL
, 0);
517 if (get_oid_with_context(the_repository
, obj_name
,
519 &oidc
, &obj_context
))
520 die(_("not a valid object name %s"), obj_name
);
521 if (!obj_context
.path
||
522 !textconv_object(the_repository
, obj_context
.path
,
523 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
524 free(obj_context
.path
);
525 return stream_blob_to_fd(1, oid
, NULL
, 0);
529 die(_("git show %s: bad file"), obj_name
);
531 write_or_die(1, buf
, size
);
532 free(obj_context
.path
);
536 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
539 enum object_type type
;
540 char *buf
= read_object_file(oid
, &type
, &size
);
544 return error(_("could not read object %s"), oid_to_hex(oid
));
546 assert(type
== OBJ_TAG
);
547 while (offset
< size
&& buf
[offset
] != '\n') {
548 int new_offset
= offset
+ 1;
550 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
552 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
553 show_tagger(ident
, rev
);
558 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
563 static int show_tree_object(const struct object_id
*oid
,
565 const char *pathname
, unsigned mode
, int stage
, void *context
)
567 FILE *file
= context
;
568 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
572 static void show_setup_revisions_tweak(struct rev_info
*rev
,
573 struct setup_revision_opt
*opt
)
575 if (rev
->ignore_merges
) {
576 /* There was no "-m" on the command line */
577 rev
->ignore_merges
= 0;
578 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
579 /* No "--first-parent", "-c", or "--cc" */
580 rev
->combine_merges
= 1;
581 rev
->dense_combined_merges
= 1;
584 if (!rev
->diffopt
.output_format
)
585 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
588 int cmd_show(int argc
, const char **argv
, const char *prefix
)
591 struct object_array_entry
*objects
;
592 struct setup_revision_opt opt
;
593 struct pathspec match_all
;
594 int i
, count
, ret
= 0;
597 git_config(git_log_config
, NULL
);
599 memset(&match_all
, 0, sizeof(match_all
));
600 repo_init_revisions(the_repository
, &rev
, prefix
);
602 rev
.always_show_header
= 1;
603 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
604 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
606 memset(&opt
, 0, sizeof(opt
));
608 opt
.tweak
= show_setup_revisions_tweak
;
609 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
612 return cmd_log_walk(&rev
);
614 count
= rev
.pending
.nr
;
615 objects
= rev
.pending
.objects
;
616 for (i
= 0; i
< count
&& !ret
; i
++) {
617 struct object
*o
= objects
[i
].item
;
618 const char *name
= objects
[i
].name
;
621 ret
= show_blob_object(&o
->oid
, &rev
, name
);
624 struct tag
*t
= (struct tag
*)o
;
628 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
629 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
631 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
632 ret
= show_tag_object(&o
->oid
, &rev
);
636 o
= parse_object(the_repository
, &t
->tagged
->oid
);
638 ret
= error(_("could not read object %s"),
639 oid_to_hex(&t
->tagged
->oid
));
647 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
648 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
650 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
651 read_tree_recursive(the_repository
, (struct tree
*)o
, "",
652 0, 0, &match_all
, show_tree_object
,
657 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
658 rev
.pending
.objects
= NULL
;
659 add_object_array(o
, name
, &rev
.pending
);
660 ret
= cmd_log_walk(&rev
);
663 ret
= error(_("unknown type: %d"), o
->type
);
671 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
673 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
676 struct setup_revision_opt opt
;
679 git_config(git_log_config
, NULL
);
681 repo_init_revisions(the_repository
, &rev
, prefix
);
682 init_reflog_walk(&rev
.reflog_info
);
683 rev
.verbose_header
= 1;
684 memset(&opt
, 0, sizeof(opt
));
686 cmd_log_init_defaults(&rev
);
687 rev
.abbrev_commit
= 1;
688 rev
.commit_format
= CMIT_FMT_ONELINE
;
689 rev
.use_terminator
= 1;
690 rev
.always_show_header
= 1;
691 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
693 return cmd_log_walk(&rev
);
696 static void log_setup_revisions_tweak(struct rev_info
*rev
,
697 struct setup_revision_opt
*opt
)
699 if (rev
->diffopt
.flags
.default_follow_renames
&&
700 rev
->prune_data
.nr
== 1)
701 rev
->diffopt
.flags
.follow_renames
= 1;
703 /* Turn --cc/-c into -p --cc/-c when -p was not given */
704 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
705 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
707 /* Turn -m on when --cc/-c was given */
708 if (rev
->combine_merges
)
709 rev
->ignore_merges
= 0;
712 int cmd_log(int argc
, const char **argv
, const char *prefix
)
715 struct setup_revision_opt opt
;
718 git_config(git_log_config
, NULL
);
720 repo_init_revisions(the_repository
, &rev
, prefix
);
721 rev
.always_show_header
= 1;
722 memset(&opt
, 0, sizeof(opt
));
724 opt
.revarg_opt
= REVARG_COMMITTISH
;
725 opt
.tweak
= log_setup_revisions_tweak
;
726 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
727 return cmd_log_walk(&rev
);
732 static const char *fmt_patch_suffix
= ".patch";
733 static int numbered
= 0;
734 static int auto_number
= 1;
736 static char *default_attach
= NULL
;
738 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
739 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
740 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
742 static void add_header(const char *value
)
744 struct string_list_item
*item
;
745 int len
= strlen(value
);
746 while (len
&& value
[len
- 1] == '\n')
749 if (!strncasecmp(value
, "to: ", 4)) {
750 item
= string_list_append(&extra_to
, value
+ 4);
752 } else if (!strncasecmp(value
, "cc: ", 4)) {
753 item
= string_list_append(&extra_cc
, value
+ 4);
756 item
= string_list_append(&extra_hdr
, value
);
759 item
->string
[len
] = '\0';
762 #define THREAD_SHALLOW 1
763 #define THREAD_DEEP 2
765 static int do_signoff
;
766 static int base_auto
;
768 static const char *signature
= git_version_string
;
769 static const char *signature_file
;
770 static int config_cover_letter
;
771 static const char *config_output_directory
;
772 static int show_notes
;
773 static struct display_notes_opt notes_opt
;
782 static int git_format_config(const char *var
, const char *value
, void *cb
)
784 if (!strcmp(var
, "format.headers")) {
786 die(_("format.headers without value"));
790 if (!strcmp(var
, "format.suffix"))
791 return git_config_string(&fmt_patch_suffix
, var
, value
);
792 if (!strcmp(var
, "format.to")) {
794 return config_error_nonbool(var
);
795 string_list_append(&extra_to
, value
);
798 if (!strcmp(var
, "format.cc")) {
800 return config_error_nonbool(var
);
801 string_list_append(&extra_cc
, value
);
804 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
805 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
808 if (!strcmp(var
, "format.numbered")) {
809 if (value
&& !strcasecmp(value
, "auto")) {
813 numbered
= git_config_bool(var
, value
);
814 auto_number
= auto_number
&& numbered
;
817 if (!strcmp(var
, "format.attach")) {
819 default_attach
= xstrdup(value
);
821 default_attach
= xstrdup(git_version_string
);
824 if (!strcmp(var
, "format.thread")) {
825 if (value
&& !strcasecmp(value
, "deep")) {
826 thread
= THREAD_DEEP
;
829 if (value
&& !strcasecmp(value
, "shallow")) {
830 thread
= THREAD_SHALLOW
;
833 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
836 if (!strcmp(var
, "format.signoff")) {
837 do_signoff
= git_config_bool(var
, value
);
840 if (!strcmp(var
, "format.signature"))
841 return git_config_string(&signature
, var
, value
);
842 if (!strcmp(var
, "format.signaturefile"))
843 return git_config_pathname(&signature_file
, var
, value
);
844 if (!strcmp(var
, "format.coverletter")) {
845 if (value
&& !strcasecmp(value
, "auto")) {
846 config_cover_letter
= COVER_AUTO
;
849 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
852 if (!strcmp(var
, "format.outputdirectory"))
853 return git_config_string(&config_output_directory
, var
, value
);
854 if (!strcmp(var
, "format.useautobase")) {
855 base_auto
= git_config_bool(var
, value
);
858 if (!strcmp(var
, "format.from")) {
859 int b
= git_parse_maybe_bool(value
);
862 from
= xstrdup(value
);
864 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
869 if (!strcmp(var
, "format.notes")) {
870 int b
= git_parse_maybe_bool(value
);
871 show_notes
= set_display_notes(¬es_opt
, b
, b
< 0 ? value
: NULL
);
875 return git_log_config(var
, value
, cb
);
878 static const char *output_directory
= NULL
;
879 static int outdir_offset
;
881 static int open_next_file(struct commit
*commit
, const char *subject
,
882 struct rev_info
*rev
, int quiet
)
884 struct strbuf filename
= STRBUF_INIT
;
885 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
887 if (output_directory
) {
888 strbuf_addstr(&filename
, output_directory
);
890 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
891 strbuf_release(&filename
);
892 return error(_("name of output directory is too long"));
894 strbuf_complete(&filename
, '/');
897 if (rev
->numbered_files
)
898 strbuf_addf(&filename
, "%d", rev
->nr
);
900 fmt_output_commit(&filename
, commit
, rev
);
902 fmt_output_subject(&filename
, subject
, rev
);
905 printf("%s\n", filename
.buf
+ outdir_offset
);
907 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
908 error_errno(_("cannot open patch file %s"), filename
.buf
);
909 strbuf_release(&filename
);
913 strbuf_release(&filename
);
917 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
919 struct rev_info check_rev
;
920 struct commit
*commit
, *c1
, *c2
;
921 struct object
*o1
, *o2
;
922 unsigned flags1
, flags2
;
924 if (rev
->pending
.nr
!= 2)
925 die(_("need exactly one range"));
927 o1
= rev
->pending
.objects
[0].item
;
928 o2
= rev
->pending
.objects
[1].item
;
931 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
932 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
934 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
935 die(_("not a range"));
937 init_patch_ids(the_repository
, ids
);
939 /* given a range a..b get all patch ids for b..a */
940 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
941 check_rev
.max_parents
= 1;
942 o1
->flags
^= UNINTERESTING
;
943 o2
->flags
^= UNINTERESTING
;
944 add_pending_object(&check_rev
, o1
, "o1");
945 add_pending_object(&check_rev
, o2
, "o2");
946 if (prepare_revision_walk(&check_rev
))
947 die(_("revision walk setup failed"));
949 while ((commit
= get_revision(&check_rev
)) != NULL
) {
950 add_commit_patch_id(commit
, ids
);
953 /* reset for next revision walk */
954 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
955 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
960 static void gen_message_id(struct rev_info
*info
, char *base
)
962 struct strbuf buf
= STRBUF_INIT
;
963 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
964 (timestamp_t
) time(NULL
),
965 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
966 info
->message_id
= strbuf_detach(&buf
, NULL
);
969 static void print_signature(FILE *file
)
971 if (!signature
|| !*signature
)
974 fprintf(file
, "-- \n%s", signature
);
975 if (signature
[strlen(signature
)-1] != '\n')
980 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
982 struct strbuf desc
= STRBUF_INIT
;
983 if (!branch_name
|| !*branch_name
)
985 read_branch_desc(&desc
, branch_name
);
987 strbuf_addch(buf
, '\n');
988 strbuf_addbuf(buf
, &desc
);
989 strbuf_addch(buf
, '\n');
991 strbuf_release(&desc
);
994 static char *find_branch_name(struct rev_info
*rev
)
996 int i
, positive
= -1;
997 struct object_id branch_oid
;
998 const struct object_id
*tip_oid
;
1000 char *full_ref
, *branch
= NULL
;
1002 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1003 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1012 ref
= rev
->cmdline
.rev
[positive
].name
;
1013 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1014 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
) &&
1015 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1016 oideq(tip_oid
, &branch_oid
))
1017 branch
= xstrdup(v
);
1022 static void show_diffstat(struct rev_info
*rev
,
1023 struct commit
*origin
, struct commit
*head
)
1025 struct diff_options opts
;
1027 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1028 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1029 diff_setup_done(&opts
);
1031 diff_tree_oid(get_commit_tree_oid(origin
),
1032 get_commit_tree_oid(head
),
1034 diffcore_std(&opts
);
1037 fprintf(rev
->diffopt
.file
, "\n");
1040 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1041 struct commit
*origin
,
1042 int nr
, struct commit
**list
,
1043 const char *branch_name
,
1046 const char *committer
;
1047 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1049 struct shortlog log
;
1050 struct strbuf sb
= STRBUF_INIT
;
1052 const char *encoding
= "UTF-8";
1053 int need_8bit_cte
= 0;
1054 struct pretty_print_context pp
= {0};
1055 struct commit
*head
= list
[0];
1057 if (!cmit_fmt_is_mail(rev
->commit_format
))
1058 die(_("cover letter needs email format"));
1060 committer
= git_committer_info(0);
1063 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1064 die(_("failed to create cover-letter file"));
1066 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1068 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1069 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1070 if (has_non_ascii(buf
))
1072 unuse_commit_buffer(list
[i
], buf
);
1076 branch_name
= find_branch_name(rev
);
1079 pp
.fmt
= CMIT_FMT_EMAIL
;
1080 pp
.date_mode
.type
= DATE_RFC2822
;
1082 pp
.print_email_subject
= 1;
1083 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1084 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1085 pp_remainder(&pp
, &msg
, &sb
, 0);
1086 add_branch_description(&sb
, branch_name
);
1087 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1089 strbuf_release(&sb
);
1091 shortlog_init(&log
);
1093 log
.wrap
= MAIL_DEFAULT_WRAP
;
1096 log
.file
= rev
->diffopt
.file
;
1097 for (i
= 0; i
< nr
; i
++)
1098 shortlog_add_commit(&log
, list
[i
]);
1100 shortlog_output(&log
);
1102 /* We can only do diffstat with a unique reference point */
1104 show_diffstat(rev
, origin
, head
);
1106 if (rev
->idiff_oid1
) {
1107 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1108 show_interdiff(rev
, 0);
1113 * Pass minimum required diff-options to range-diff; others
1114 * can be added later if deemed desirable.
1116 struct diff_options opts
;
1118 opts
.file
= rev
->diffopt
.file
;
1119 opts
.use_color
= rev
->diffopt
.use_color
;
1120 diff_setup_done(&opts
);
1121 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1122 show_range_diff(rev
->rdiff1
, rev
->rdiff2
,
1123 rev
->creation_factor
, 1, &opts
);
1127 static const char *clean_message_id(const char *msg_id
)
1130 const char *a
, *z
, *m
;
1133 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1138 if (!isspace(ch
) && (ch
!= '>'))
1143 die(_("insane in-reply-to: %s"), msg_id
);
1146 return xmemdupz(a
, z
- a
);
1149 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1151 if (output_directory
&& is_absolute_path(output_directory
))
1152 return output_directory
;
1154 if (!prefix
|| !*prefix
) {
1155 if (output_directory
)
1156 return output_directory
;
1157 /* The user did not explicitly ask for "./" */
1162 outdir_offset
= strlen(prefix
);
1163 if (!output_directory
)
1166 return prefix_filename(prefix
, output_directory
);
1169 static const char * const builtin_format_patch_usage
[] = {
1170 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1174 static int keep_subject
= 0;
1176 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1178 BUG_ON_OPT_NEG(unset
);
1179 BUG_ON_OPT_ARG(arg
);
1180 ((struct rev_info
*)opt
->value
)->total
= -1;
1185 static int subject_prefix
= 0;
1187 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1190 BUG_ON_OPT_NEG(unset
);
1192 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1196 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1198 BUG_ON_OPT_NEG(unset
);
1199 BUG_ON_OPT_ARG(arg
);
1200 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1203 static int numbered_cmdline_opt
= 0;
1205 static int numbered_callback(const struct option
*opt
, const char *arg
,
1208 BUG_ON_OPT_ARG(arg
);
1209 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1215 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1218 BUG_ON_OPT_NEG(unset
);
1219 return numbered_callback(opt
, arg
, 1);
1222 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1225 const char **dir
= (const char **)opt
->value
;
1226 BUG_ON_OPT_NEG(unset
);
1228 die(_("two output directories?"));
1233 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1235 int *thread
= (int *)opt
->value
;
1238 else if (!arg
|| !strcmp(arg
, "shallow"))
1239 *thread
= THREAD_SHALLOW
;
1240 else if (!strcmp(arg
, "deep"))
1241 *thread
= THREAD_DEEP
;
1243 * Please update _git_formatpatch() in git-completion.bash
1244 * when you add new options.
1251 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1253 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1255 rev
->mime_boundary
= NULL
;
1257 rev
->mime_boundary
= arg
;
1259 rev
->mime_boundary
= git_version_string
;
1260 rev
->no_inline
= unset
? 0 : 1;
1264 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1266 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1268 rev
->mime_boundary
= NULL
;
1270 rev
->mime_boundary
= arg
;
1272 rev
->mime_boundary
= git_version_string
;
1277 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1280 string_list_clear(&extra_hdr
, 0);
1281 string_list_clear(&extra_to
, 0);
1282 string_list_clear(&extra_cc
, 0);
1289 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1292 string_list_clear(&extra_to
, 0);
1294 string_list_append(&extra_to
, arg
);
1298 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1301 string_list_clear(&extra_cc
, 0);
1303 string_list_append(&extra_cc
, arg
);
1307 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1309 char **from
= opt
->value
;
1316 *from
= xstrdup(arg
);
1318 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1322 struct base_tree_info
{
1323 struct object_id base_commit
;
1324 int nr_patch_id
, alloc_patch_id
;
1325 struct object_id
*patch_id
;
1328 static struct commit
*get_base_commit(const char *base_commit
,
1329 struct commit
**list
,
1332 struct commit
*base
= NULL
;
1333 struct commit
**rev
;
1334 int i
= 0, rev_nr
= 0;
1336 if (base_commit
&& strcmp(base_commit
, "auto")) {
1337 base
= lookup_commit_reference_by_name(base_commit
);
1339 die(_("unknown commit %s"), base_commit
);
1340 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1341 struct branch
*curr_branch
= branch_get(NULL
);
1342 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1344 struct commit_list
*base_list
;
1345 struct commit
*commit
;
1346 struct object_id oid
;
1348 if (get_oid(upstream
, &oid
))
1349 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1350 commit
= lookup_commit_or_die(&oid
, "upstream base");
1351 base_list
= get_merge_bases_many(commit
, total
, list
);
1352 /* There should be one and only one merge base. */
1353 if (!base_list
|| base_list
->next
)
1354 die(_("could not find exact merge base"));
1355 base
= base_list
->item
;
1356 free_commit_list(base_list
);
1358 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1359 "please use git branch --set-upstream-to to track a remote branch.\n"
1360 "Or you could specify base commit by --base=<base-commit-id> manually"));
1364 ALLOC_ARRAY(rev
, total
);
1365 for (i
= 0; i
< total
; i
++)
1370 * Get merge base through pair-wise computations
1371 * and store it in rev[0].
1373 while (rev_nr
> 1) {
1374 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1375 struct commit_list
*merge_base
;
1376 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1377 if (!merge_base
|| merge_base
->next
)
1378 die(_("failed to find exact merge base"));
1380 rev
[i
] = merge_base
->item
;
1384 rev
[i
] = rev
[2 * i
];
1385 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1388 if (!in_merge_bases(base
, rev
[0]))
1389 die(_("base commit should be the ancestor of revision list"));
1391 for (i
= 0; i
< total
; i
++) {
1392 if (base
== list
[i
])
1393 die(_("base commit shouldn't be in revision list"));
1400 define_commit_slab(commit_base
, int);
1402 static void prepare_bases(struct base_tree_info
*bases
,
1403 struct commit
*base
,
1404 struct commit
**list
,
1407 struct commit
*commit
;
1408 struct rev_info revs
;
1409 struct diff_options diffopt
;
1410 struct commit_base commit_base
;
1416 init_commit_base(&commit_base
);
1417 repo_diff_setup(the_repository
, &diffopt
);
1418 diffopt
.flags
.recursive
= 1;
1419 diff_setup_done(&diffopt
);
1421 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1423 repo_init_revisions(the_repository
, &revs
, NULL
);
1424 revs
.max_parents
= 1;
1425 revs
.topo_order
= 1;
1426 for (i
= 0; i
< total
; i
++) {
1427 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1428 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1429 *commit_base_at(&commit_base
, list
[i
]) = 1;
1431 base
->object
.flags
|= UNINTERESTING
;
1432 add_pending_object(&revs
, &base
->object
, "base");
1434 if (prepare_revision_walk(&revs
))
1435 die(_("revision walk setup failed"));
1437 * Traverse the commits list, get prerequisite patch ids
1438 * and stuff them in bases structure.
1440 while ((commit
= get_revision(&revs
)) != NULL
) {
1441 struct object_id oid
;
1442 struct object_id
*patch_id
;
1443 if (*commit_base_at(&commit_base
, commit
))
1445 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1446 die(_("cannot get patch id"));
1447 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1448 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1449 oidcpy(patch_id
, &oid
);
1450 bases
->nr_patch_id
++;
1452 clear_commit_base(&commit_base
);
1455 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1459 /* Only do this once, either for the cover or for the first one */
1460 if (is_null_oid(&bases
->base_commit
))
1463 /* Show the base commit */
1464 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1466 /* Show the prerequisite patches */
1467 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1468 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1470 free(bases
->patch_id
);
1471 bases
->nr_patch_id
= 0;
1472 bases
->alloc_patch_id
= 0;
1473 oidclr(&bases
->base_commit
);
1476 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1477 const char *generic
, const char *rerolled
)
1479 if (reroll_count
<= 0)
1480 strbuf_addstr(sb
, generic
);
1481 else /* RFC may be v0, so allow -v1 to diff against v0 */
1482 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1486 static void infer_range_diff_ranges(struct strbuf
*r1
,
1489 struct commit
*origin
,
1490 struct commit
*head
)
1492 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1494 if (!strstr(prev
, "..")) {
1495 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1496 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1497 } else if (!origin
) {
1498 die(_("failed to infer range-diff ranges"));
1500 strbuf_addstr(r1
, prev
);
1501 strbuf_addf(r2
, "%s..%s",
1502 oid_to_hex(&origin
->object
.oid
), head_oid
);
1506 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1508 struct commit
*commit
;
1509 struct commit
**list
= NULL
;
1510 struct rev_info rev
;
1511 struct setup_revision_opt s_r_opt
;
1512 int nr
= 0, total
, i
;
1514 int start_number
= -1;
1515 int just_numbers
= 0;
1516 int ignore_if_in_upstream
= 0;
1517 int cover_letter
= -1;
1518 int boundary_count
= 0;
1519 int no_binary_diff
= 0;
1520 int zero_commit
= 0;
1521 struct commit
*origin
= NULL
;
1522 const char *in_reply_to
= NULL
;
1523 struct patch_ids ids
;
1524 struct strbuf buf
= STRBUF_INIT
;
1525 int use_patch_format
= 0;
1527 int reroll_count
= -1;
1528 char *branch_name
= NULL
;
1529 char *base_commit
= NULL
;
1530 struct base_tree_info bases
;
1531 int show_progress
= 0;
1532 struct progress
*progress
= NULL
;
1533 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1534 struct strbuf idiff_title
= STRBUF_INIT
;
1535 const char *rdiff_prev
= NULL
;
1536 struct strbuf rdiff1
= STRBUF_INIT
;
1537 struct strbuf rdiff2
= STRBUF_INIT
;
1538 struct strbuf rdiff_title
= STRBUF_INIT
;
1539 int creation_factor
= -1;
1541 const struct option builtin_format_patch_options
[] = {
1542 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1543 N_("use [PATCH n/m] even with a single patch"),
1544 PARSE_OPT_NOARG
, numbered_callback
},
1545 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1546 N_("use [PATCH] even with multiple patches"),
1547 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
},
1548 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1549 OPT_BOOL(0, "stdout", &use_stdout
,
1550 N_("print patches to standard out")),
1551 OPT_BOOL(0, "cover-letter", &cover_letter
,
1552 N_("generate a cover letter")),
1553 OPT_BOOL(0, "numbered-files", &just_numbers
,
1554 N_("use simple number sequence for output file names")),
1555 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1556 N_("use <sfx> instead of '.patch'")),
1557 OPT_INTEGER(0, "start-number", &start_number
,
1558 N_("start numbering patches at <n> instead of 1")),
1559 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1560 N_("mark the series as Nth re-roll")),
1561 { OPTION_CALLBACK
, 0, "rfc", &rev
, NULL
,
1562 N_("Use [RFC PATCH] instead of [PATCH]"),
1563 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
},
1564 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1565 N_("Use [<prefix>] instead of [PATCH]"),
1566 PARSE_OPT_NONEG
, subject_prefix_callback
},
1567 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1568 N_("dir"), N_("store resulting files in <dir>"),
1569 PARSE_OPT_NONEG
, output_directory_callback
},
1570 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1571 N_("don't strip/add [PATCH]"),
1572 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1573 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1574 N_("don't output binary diffs")),
1575 OPT_BOOL(0, "zero-commit", &zero_commit
,
1576 N_("output all-zero hash in From header")),
1577 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1578 N_("don't include a patch matching a commit upstream")),
1579 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1580 N_("show patch format instead of default (patch + stat)"),
1581 1, PARSE_OPT_NONEG
),
1582 OPT_GROUP(N_("Messaging")),
1583 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1584 N_("add email header"), 0, header_callback
},
1585 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1587 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1589 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1590 N_("set From address to <ident> (or committer ident if absent)"),
1591 PARSE_OPT_OPTARG
, from_callback
},
1592 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1593 N_("make first mail a reply to <message-id>")),
1594 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1595 N_("attach the patch"), PARSE_OPT_OPTARG
,
1597 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1598 N_("inline the patch"),
1599 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1601 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1602 N_("enable message threading, styles: shallow, deep"),
1603 PARSE_OPT_OPTARG
, thread_callback
},
1604 OPT_STRING(0, "signature", &signature
, N_("signature"),
1605 N_("add a signature")),
1606 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1607 N_("add prerequisite tree info to the patch series")),
1608 OPT_FILENAME(0, "signature-file", &signature_file
,
1609 N_("add a signature from a file")),
1610 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1611 OPT_BOOL(0, "progress", &show_progress
,
1612 N_("show progress while generating patches")),
1613 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1614 N_("show changes against <rev> in cover letter or single patch"),
1615 parse_opt_object_name
),
1616 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1617 N_("show changes against <refspec> in cover letter or single patch")),
1618 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1619 N_("percentage by which creation is weighted")),
1623 extra_hdr
.strdup_strings
= 1;
1624 extra_to
.strdup_strings
= 1;
1625 extra_cc
.strdup_strings
= 1;
1626 init_log_defaults();
1627 init_display_notes(¬es_opt
);
1628 git_config(git_format_config
, NULL
);
1629 repo_init_revisions(the_repository
, &rev
, prefix
);
1630 rev
.show_notes
= show_notes
;
1631 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1632 rev
.commit_format
= CMIT_FMT_EMAIL
;
1633 rev
.expand_tabs_in_log_default
= 0;
1634 rev
.verbose_header
= 1;
1636 rev
.max_parents
= 1;
1637 rev
.diffopt
.flags
.recursive
= 1;
1638 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1639 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1640 s_r_opt
.def
= "HEAD";
1641 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1643 if (default_attach
) {
1644 rev
.mime_boundary
= default_attach
;
1649 * Parse the arguments before setup_revisions(), or something
1650 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1651 * possibly a valid SHA1.
1653 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1654 builtin_format_patch_usage
,
1655 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1656 PARSE_OPT_KEEP_DASHDASH
);
1658 if (0 < reroll_count
) {
1659 struct strbuf sprefix
= STRBUF_INIT
;
1660 strbuf_addf(&sprefix
, "%s v%d",
1661 rev
.subject_prefix
, reroll_count
);
1662 rev
.reroll_count
= reroll_count
;
1663 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1666 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1667 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1668 strbuf_addch(&buf
, '\n');
1672 strbuf_addstr(&buf
, "To: ");
1673 for (i
= 0; i
< extra_to
.nr
; i
++) {
1675 strbuf_addstr(&buf
, " ");
1676 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1677 if (i
+ 1 < extra_to
.nr
)
1678 strbuf_addch(&buf
, ',');
1679 strbuf_addch(&buf
, '\n');
1683 strbuf_addstr(&buf
, "Cc: ");
1684 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1686 strbuf_addstr(&buf
, " ");
1687 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1688 if (i
+ 1 < extra_cc
.nr
)
1689 strbuf_addch(&buf
, ',');
1690 strbuf_addch(&buf
, '\n');
1693 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1696 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1697 die(_("invalid ident line: %s"), from
);
1700 if (start_number
< 0)
1704 * If numbered is set solely due to format.numbered in config,
1705 * and it would conflict with --keep-subject (-k) from the
1706 * command line, reset "numbered".
1708 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1711 if (numbered
&& keep_subject
)
1712 die(_("-n and -k are mutually exclusive"));
1713 if (keep_subject
&& subject_prefix
)
1714 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1715 rev
.preserve_subject
= keep_subject
;
1717 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1719 die(_("unrecognized argument: %s"), argv
[1]);
1721 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1722 die(_("--name-only does not make sense"));
1723 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1724 die(_("--name-status does not make sense"));
1725 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1726 die(_("--check does not make sense"));
1728 if (!use_patch_format
&&
1729 (!rev
.diffopt
.output_format
||
1730 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1731 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1732 if (!rev
.diffopt
.stat_width
)
1733 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1735 /* Always generate a patch */
1736 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1738 rev
.zero_commit
= zero_commit
;
1740 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1741 rev
.diffopt
.flags
.binary
= 1;
1744 load_display_notes(&rev
.notes_opt
);
1746 if (!output_directory
&& !use_stdout
)
1747 output_directory
= config_output_directory
;
1750 output_directory
= set_outdir(prefix
, output_directory
);
1754 if (output_directory
) {
1755 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1756 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1758 die(_("standard output, or directory, which one?"));
1759 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1760 die_errno(_("could not create directory '%s'"),
1764 if (rev
.pending
.nr
== 1) {
1767 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1769 * This is traditional behaviour of "git format-patch
1770 * origin" that prepares what the origin side still
1773 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1774 add_head_to_pending(&rev
);
1778 * Otherwise, it is "format-patch -22 HEAD", and/or
1779 * "format-patch --root HEAD". The user wants
1780 * get_revision() to do the usual traversal.
1783 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1787 const char *ref
, *v
;
1788 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1790 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1791 branch_name
= xstrdup(v
);
1793 branch_name
= xstrdup(""); /* no branch */
1798 * We cannot move this anywhere earlier because we do want to
1799 * know if --root was given explicitly from the command line.
1801 rev
.show_root_diff
= 1;
1803 if (ignore_if_in_upstream
) {
1804 /* Don't say anything if head and upstream are the same. */
1805 if (rev
.pending
.nr
== 2) {
1806 struct object_array_entry
*o
= rev
.pending
.objects
;
1807 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
1810 get_patch_ids(&rev
, &ids
);
1813 if (prepare_revision_walk(&rev
))
1814 die(_("revision walk setup failed"));
1816 while ((commit
= get_revision(&rev
)) != NULL
) {
1817 if (commit
->object
.flags
& BOUNDARY
) {
1819 origin
= (boundary_count
== 1) ? commit
: NULL
;
1823 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1827 REALLOC_ARRAY(list
, nr
);
1828 list
[nr
- 1] = commit
;
1834 if (cover_letter
== -1) {
1835 if (config_cover_letter
== COVER_AUTO
)
1836 cover_letter
= (total
> 1);
1838 cover_letter
= (config_cover_letter
== COVER_ON
);
1840 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1843 rev
.total
= total
+ start_number
- 1;
1845 if (idiff_prev
.nr
) {
1846 if (!cover_letter
&& total
!= 1)
1847 die(_("--interdiff requires --cover-letter or single patch"));
1848 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
1849 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
1850 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
1852 _("Interdiff against v%d:"));
1855 if (creation_factor
< 0)
1856 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
1857 else if (!rdiff_prev
)
1858 die(_("--creation-factor requires --range-diff"));
1861 if (!cover_letter
&& total
!= 1)
1862 die(_("--range-diff requires --cover-letter or single patch"));
1864 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
1866 rev
.rdiff1
= rdiff1
.buf
;
1867 rev
.rdiff2
= rdiff2
.buf
;
1868 rev
.creation_factor
= creation_factor
;
1869 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
1871 _("Range-diff against v%d:"));
1875 ; /* --no-signature inhibits all signatures */
1876 } else if (signature
&& signature
!= git_version_string
) {
1877 ; /* non-default signature already set */
1878 } else if (signature_file
) {
1879 struct strbuf buf
= STRBUF_INIT
;
1881 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1882 die_errno(_("unable to read signature file '%s'"), signature_file
);
1883 signature
= strbuf_detach(&buf
, NULL
);
1886 memset(&bases
, 0, sizeof(bases
));
1887 if (base_commit
|| base_auto
) {
1888 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1889 reset_revision_walk();
1890 clear_object_flags(UNINTERESTING
);
1891 prepare_bases(&bases
, base
, list
, nr
);
1894 if (in_reply_to
|| thread
|| cover_letter
)
1895 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1897 const char *msgid
= clean_message_id(in_reply_to
);
1898 string_list_append(rev
.ref_message_ids
, msgid
);
1900 rev
.numbered_files
= just_numbers
;
1901 rev
.patch_suffix
= fmt_patch_suffix
;
1904 gen_message_id(&rev
, "cover");
1905 make_cover_letter(&rev
, use_stdout
,
1906 origin
, nr
, list
, branch_name
, quiet
);
1907 print_bases(&bases
, rev
.diffopt
.file
);
1908 print_signature(rev
.diffopt
.file
);
1911 /* interdiff/range-diff in cover-letter; omit from patches */
1912 rev
.idiff_oid1
= NULL
;
1915 rev
.add_signoff
= do_signoff
;
1918 progress
= start_delayed_progress(_("Generating patches"), total
);
1921 display_progress(progress
, total
- nr
);
1923 rev
.nr
= total
- nr
+ (start_number
- 1);
1924 /* Make the second and subsequent mails replies to the first */
1926 /* Have we already had a message ID? */
1927 if (rev
.message_id
) {
1929 * For deep threading: make every mail
1930 * a reply to the previous one, no
1931 * matter what other options are set.
1933 * For shallow threading:
1935 * Without --cover-letter and
1936 * --in-reply-to, make every mail a
1937 * reply to the one before.
1939 * With --in-reply-to but no
1940 * --cover-letter, make every mail a
1941 * reply to the <reply-to>.
1943 * With --cover-letter, make every
1944 * mail but the cover letter a reply
1945 * to the cover letter. The cover
1946 * letter is a reply to the
1947 * --in-reply-to, if specified.
1949 if (thread
== THREAD_SHALLOW
1950 && rev
.ref_message_ids
->nr
> 0
1951 && (!cover_letter
|| rev
.nr
> 1))
1952 free(rev
.message_id
);
1954 string_list_append(rev
.ref_message_ids
,
1957 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1961 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1962 die(_("failed to create output files"));
1963 shown
= log_tree_commit(&rev
, commit
);
1964 free_commit_buffer(the_repository
->parsed_objects
,
1967 /* We put one extra blank line between formatted
1968 * patches and this flag is used by log-tree code
1969 * to see if it needs to emit a LF before showing
1970 * the log; when using one file per patch, we do
1971 * not want the extra blank line.
1976 print_bases(&bases
, rev
.diffopt
.file
);
1977 if (rev
.mime_boundary
)
1978 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
1979 mime_boundary_leader
,
1982 print_signature(rev
.diffopt
.file
);
1985 fclose(rev
.diffopt
.file
);
1987 stop_progress(&progress
);
1990 string_list_clear(&extra_to
, 0);
1991 string_list_clear(&extra_cc
, 0);
1992 string_list_clear(&extra_hdr
, 0);
1993 if (ignore_if_in_upstream
)
1994 free_patch_ids(&ids
);
1997 oid_array_clear(&idiff_prev
);
1998 strbuf_release(&idiff_title
);
1999 strbuf_release(&rdiff1
);
2000 strbuf_release(&rdiff2
);
2001 strbuf_release(&rdiff_title
);
2005 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2007 struct object_id oid
;
2008 if (get_oid(arg
, &oid
) == 0) {
2009 struct commit
*commit
= lookup_commit_reference(the_repository
,
2012 commit
->object
.flags
|= flags
;
2013 add_pending_object(revs
, &commit
->object
, arg
);
2020 static const char * const cherry_usage
[] = {
2021 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2025 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2026 int abbrev
, FILE *file
)
2029 fprintf(file
, "%c %s\n", sign
,
2030 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2032 struct strbuf buf
= STRBUF_INIT
;
2033 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2034 fprintf(file
, "%c %s %s\n", sign
,
2035 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2037 strbuf_release(&buf
);
2041 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2043 struct rev_info revs
;
2044 struct patch_ids ids
;
2045 struct commit
*commit
;
2046 struct commit_list
*list
= NULL
;
2047 struct branch
*current_branch
;
2048 const char *upstream
;
2049 const char *head
= "HEAD";
2050 const char *limit
= NULL
;
2051 int verbose
= 0, abbrev
= 0;
2053 struct option options
[] = {
2054 OPT__ABBREV(&abbrev
),
2055 OPT__VERBOSE(&verbose
, N_("be verbose")),
2059 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2072 current_branch
= branch_get(NULL
);
2073 upstream
= branch_get_upstream(current_branch
, NULL
);
2075 fprintf(stderr
, _("Could not find a tracked"
2076 " remote branch, please"
2077 " specify <upstream> manually.\n"));
2078 usage_with_options(cherry_usage
, options
);
2082 repo_init_revisions(the_repository
, &revs
, prefix
);
2083 revs
.max_parents
= 1;
2085 if (add_pending_commit(head
, &revs
, 0))
2086 die(_("unknown commit %s"), head
);
2087 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2088 die(_("unknown commit %s"), upstream
);
2090 /* Don't say anything if head and upstream are the same. */
2091 if (revs
.pending
.nr
== 2) {
2092 struct object_array_entry
*o
= revs
.pending
.objects
;
2093 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2097 get_patch_ids(&revs
, &ids
);
2099 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2100 die(_("unknown commit %s"), limit
);
2102 /* reverse the list of commits */
2103 if (prepare_revision_walk(&revs
))
2104 die(_("revision walk setup failed"));
2105 while ((commit
= get_revision(&revs
)) != NULL
) {
2106 commit_list_insert(commit
, &list
);
2112 commit
= list
->item
;
2113 if (has_commit_patch_id(commit
, &ids
))
2115 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2119 free_patch_ids(&ids
);