2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
10 #include "object-store.h"
18 #include "reflog-walk.h"
19 #include "patch-ids.h"
20 #include "run-command.h"
23 #include "string-list.h"
24 #include "parse-options.h"
27 #include "streaming.h"
30 #include "gpg-interface.h"
32 #include "commit-slab.h"
33 #include "interdiff.h"
35 #define MAIL_DEFAULT_WRAP 72
37 /* Set a default date-time format for git log ("log.date" config variable) */
38 static const char *default_date_mode
= NULL
;
40 static int default_abbrev_commit
;
41 static int default_show_root
= 1;
42 static int default_follow
;
43 static int default_show_signature
;
44 static int decoration_style
;
45 static int decoration_given
;
46 static int use_mailmap_config
;
47 static const char *fmt_patch_subject_prefix
= "PATCH";
48 static const char *fmt_pretty
;
50 static const char * const builtin_log_usage
[] = {
51 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
52 N_("git show [<options>] <object>..."),
56 struct line_opt_callback_data
{
59 struct string_list args
;
62 static int auto_decoration_style(void)
64 return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS
: 0;
67 static int parse_decoration_style(const char *value
)
69 switch (git_parse_maybe_bool(value
)) {
71 return DECORATE_SHORT_REFS
;
77 if (!strcmp(value
, "full"))
78 return DECORATE_FULL_REFS
;
79 else if (!strcmp(value
, "short"))
80 return DECORATE_SHORT_REFS
;
81 else if (!strcmp(value
, "auto"))
82 return auto_decoration_style();
86 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
91 decoration_style
= parse_decoration_style(arg
);
93 decoration_style
= DECORATE_SHORT_REFS
;
95 if (decoration_style
< 0)
96 die(_("invalid --decorate option: %s"), arg
);
103 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
105 struct line_opt_callback_data
*data
= option
->value
;
110 data
->rev
->line_level_traverse
= 1;
111 string_list_append(&data
->args
, arg
);
116 static void init_log_defaults(void)
118 init_grep_defaults();
119 init_diff_ui_defaults();
121 decoration_style
= auto_decoration_style();
124 static void cmd_log_init_defaults(struct rev_info
*rev
)
127 get_commit_format(fmt_pretty
, rev
);
129 rev
->diffopt
.flags
.default_follow_renames
= 1;
130 rev
->verbose_header
= 1;
131 rev
->diffopt
.flags
.recursive
= 1;
132 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
133 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
134 rev
->abbrev_commit
= default_abbrev_commit
;
135 rev
->show_root_diff
= default_show_root
;
136 rev
->subject_prefix
= fmt_patch_subject_prefix
;
137 rev
->show_signature
= default_show_signature
;
138 rev
->diffopt
.flags
.allow_textconv
= 1;
140 if (default_date_mode
)
141 parse_date_format(default_date_mode
, &rev
->date_mode
);
144 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
145 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
147 struct userformat_want w
;
148 int quiet
= 0, source
= 0, mailmap
= 0;
149 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
150 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
151 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
152 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
153 &decorate_refs_exclude
};
154 static struct revision_sources revision_sources
;
156 const struct option builtin_log_options
[] = {
157 OPT__QUIET(&quiet
, N_("suppress diff output")),
158 OPT_BOOL(0, "source", &source
, N_("show source")),
159 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
160 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
161 N_("pattern"), N_("only decorate refs that match <pattern>")),
162 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
163 N_("pattern"), N_("do not decorate refs that match <pattern>")),
164 { OPTION_CALLBACK
, 0, "decorate", NULL
, NULL
, N_("decorate options"),
165 PARSE_OPT_OPTARG
, decorate_callback
},
166 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
167 N_("Process line range n,m in file, counting from 1"),
168 log_line_range_callback
),
173 line_cb
.prefix
= prefix
;
175 mailmap
= use_mailmap_config
;
176 argc
= parse_options(argc
, argv
, prefix
,
177 builtin_log_options
, builtin_log_usage
,
178 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
179 PARSE_OPT_KEEP_DASHDASH
);
182 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
183 argc
= setup_revisions(argc
, argv
, rev
, opt
);
185 /* Any arguments at this point are not recognized */
187 die(_("unrecognized argument: %s"), argv
[1]);
189 memset(&w
, 0, sizeof(w
));
190 userformat_find_requirements(NULL
, &w
);
192 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
195 init_display_notes(&rev
->notes_opt
);
197 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
198 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
199 rev
->always_show_header
= 0;
202 init_revision_sources(&revision_sources
);
203 rev
->sources
= &revision_sources
;
207 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
208 read_mailmap(rev
->mailmap
, NULL
);
211 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
213 * "log --pretty=raw" is special; ignore UI oriented
214 * configuration variables such as decoration.
216 if (!decoration_given
)
217 decoration_style
= 0;
218 if (!rev
->abbrev_commit_given
)
219 rev
->abbrev_commit
= 0;
222 if (decoration_style
) {
223 rev
->show_decorations
= 1;
224 load_ref_decorations(&decoration_filter
, decoration_style
);
227 if (rev
->line_level_traverse
)
228 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
233 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
234 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
236 cmd_log_init_defaults(rev
);
237 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
241 * This gives a rough estimate for how many commits we
242 * will print out in the list.
244 static int estimate_commit_count(struct rev_info
*rev
, struct commit_list
*list
)
249 struct commit
*commit
= list
->item
;
250 unsigned int flags
= commit
->object
.flags
;
252 if (!(flags
& (TREESAME
| UNINTERESTING
)))
258 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
260 if (rev
->shown_one
) {
262 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
263 putchar(rev
->diffopt
.line_termination
);
265 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
268 static struct itimerval early_output_timer
;
270 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
272 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
275 revs
->diffopt
.close_file
= 0;
276 sort_in_topological_order(&list
, revs
->sort_order
);
278 struct commit
*commit
= list
->item
;
279 switch (simplify_commit(revs
, commit
)) {
282 int n
= estimate_commit_count(revs
, list
);
283 show_early_header(revs
, "incomplete", n
);
286 log_tree_commit(revs
, commit
);
293 fclose(revs
->diffopt
.file
);
299 /* Did we already get enough commits for the early output? */
302 fclose(revs
->diffopt
.file
);
307 * ..if no, then repeat it twice a second until we
310 * NOTE! We don't use "it_interval", because if the
311 * reader isn't listening, we want our output to be
312 * throttled by the writing, and not have the timer
313 * trigger every second even if we're blocked on a
316 early_output_timer
.it_value
.tv_sec
= 0;
317 early_output_timer
.it_value
.tv_usec
= 500000;
318 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
321 static void early_output(int signal
)
323 show_early_output
= log_show_early
;
326 static void setup_early_output(struct rev_info
*rev
)
331 * Set up the signal handler, minimally intrusively:
332 * we only set a single volatile integer word (not
333 * using sigatomic_t - trying to avoid unnecessary
334 * system dependencies and headers), and using
337 memset(&sa
, 0, sizeof(sa
));
338 sa
.sa_handler
= early_output
;
339 sigemptyset(&sa
.sa_mask
);
340 sa
.sa_flags
= SA_RESTART
;
341 sigaction(SIGALRM
, &sa
, NULL
);
344 * If we can get the whole output in less than a
345 * tenth of a second, don't even bother doing the
346 * early-output thing..
348 * This is a one-time-only trigger.
350 early_output_timer
.it_value
.tv_sec
= 0;
351 early_output_timer
.it_value
.tv_usec
= 100000;
352 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
355 static void finish_early_output(struct rev_info
*rev
)
357 int n
= estimate_commit_count(rev
, rev
->commits
);
358 signal(SIGALRM
, SIG_IGN
);
359 show_early_header(rev
, "done", n
);
362 static int cmd_log_walk(struct rev_info
*rev
)
364 struct commit
*commit
;
366 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
368 if (rev
->early_output
)
369 setup_early_output(rev
);
371 if (prepare_revision_walk(rev
))
372 die(_("revision walk setup failed"));
374 if (rev
->early_output
)
375 finish_early_output(rev
);
378 * For --check and --exit-code, the exit code is based on CHECK_FAILED
379 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
380 * retain that state information if replacing rev->diffopt in this loop
382 rev
->diffopt
.close_file
= 0;
383 while ((commit
= get_revision(rev
)) != NULL
) {
384 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
386 * We decremented max_count in get_revision,
387 * but we didn't actually show the commit.
390 if (!rev
->reflog_info
) {
392 * We may show a given commit multiple times when
393 * walking the reflogs.
395 free_commit_buffer(commit
);
396 free_commit_list(commit
->parents
);
397 commit
->parents
= NULL
;
399 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
400 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
401 if (rev
->diffopt
.degraded_cc_to_c
)
404 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
405 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
407 fclose(rev
->diffopt
.file
);
409 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
410 rev
->diffopt
.flags
.check_failed
) {
413 return diff_result_code(&rev
->diffopt
, 0);
416 static int git_log_config(const char *var
, const char *value
, void *cb
)
418 const char *slot_name
;
420 if (!strcmp(var
, "format.pretty"))
421 return git_config_string(&fmt_pretty
, var
, value
);
422 if (!strcmp(var
, "format.subjectprefix"))
423 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
424 if (!strcmp(var
, "log.abbrevcommit")) {
425 default_abbrev_commit
= git_config_bool(var
, value
);
428 if (!strcmp(var
, "log.date"))
429 return git_config_string(&default_date_mode
, var
, value
);
430 if (!strcmp(var
, "log.decorate")) {
431 decoration_style
= parse_decoration_style(value
);
432 if (decoration_style
< 0)
433 decoration_style
= 0; /* maybe warn? */
436 if (!strcmp(var
, "log.showroot")) {
437 default_show_root
= git_config_bool(var
, value
);
440 if (!strcmp(var
, "log.follow")) {
441 default_follow
= git_config_bool(var
, value
);
444 if (skip_prefix(var
, "color.decorate.", &slot_name
))
445 return parse_decorate_color_config(var
, slot_name
, value
);
446 if (!strcmp(var
, "log.mailmap")) {
447 use_mailmap_config
= git_config_bool(var
, value
);
450 if (!strcmp(var
, "log.showsignature")) {
451 default_show_signature
= git_config_bool(var
, value
);
455 if (grep_config(var
, value
, cb
) < 0)
457 if (git_gpg_config(var
, value
, cb
) < 0)
459 return git_diff_ui_config(var
, value
, cb
);
462 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
465 struct setup_revision_opt opt
;
468 git_config(git_log_config
, NULL
);
470 init_revisions(&rev
, prefix
);
472 rev
.simplify_history
= 0;
473 memset(&opt
, 0, sizeof(opt
));
475 opt
.revarg_opt
= REVARG_COMMITTISH
;
476 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
477 if (!rev
.diffopt
.output_format
)
478 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
479 return cmd_log_walk(&rev
);
482 static void show_tagger(char *buf
, int len
, struct rev_info
*rev
)
484 struct strbuf out
= STRBUF_INIT
;
485 struct pretty_print_context pp
= {0};
487 pp
.fmt
= rev
->commit_format
;
488 pp
.date_mode
= rev
->date_mode
;
489 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
490 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
491 strbuf_release(&out
);
494 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
496 struct object_id oidc
;
497 struct object_context obj_context
;
501 fflush(rev
->diffopt
.file
);
502 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
503 !rev
->diffopt
.flags
.allow_textconv
)
504 return stream_blob_to_fd(1, oid
, NULL
, 0);
506 if (get_oid_with_context(obj_name
, GET_OID_RECORD_PATH
,
507 &oidc
, &obj_context
))
508 die(_("Not a valid object name %s"), obj_name
);
509 if (!obj_context
.path
||
510 !textconv_object(obj_context
.path
, obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
511 free(obj_context
.path
);
512 return stream_blob_to_fd(1, oid
, NULL
, 0);
516 die(_("git show %s: bad file"), obj_name
);
518 write_or_die(1, buf
, size
);
519 free(obj_context
.path
);
523 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
526 enum object_type type
;
527 char *buf
= read_object_file(oid
, &type
, &size
);
531 return error(_("Could not read object %s"), oid_to_hex(oid
));
533 assert(type
== OBJ_TAG
);
534 while (offset
< size
&& buf
[offset
] != '\n') {
535 int new_offset
= offset
+ 1;
536 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
538 if (starts_with(buf
+ offset
, "tagger "))
539 show_tagger(buf
+ offset
+ 7,
540 new_offset
- offset
- 7, rev
);
545 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
550 static int show_tree_object(const struct object_id
*oid
,
552 const char *pathname
, unsigned mode
, int stage
, void *context
)
554 FILE *file
= context
;
555 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
559 static void show_setup_revisions_tweak(struct rev_info
*rev
,
560 struct setup_revision_opt
*opt
)
562 if (rev
->ignore_merges
) {
563 /* There was no "-m" on the command line */
564 rev
->ignore_merges
= 0;
565 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
566 /* No "--first-parent", "-c", or "--cc" */
567 rev
->combine_merges
= 1;
568 rev
->dense_combined_merges
= 1;
571 if (!rev
->diffopt
.output_format
)
572 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
575 int cmd_show(int argc
, const char **argv
, const char *prefix
)
578 struct object_array_entry
*objects
;
579 struct setup_revision_opt opt
;
580 struct pathspec match_all
;
581 int i
, count
, ret
= 0;
584 git_config(git_log_config
, NULL
);
586 memset(&match_all
, 0, sizeof(match_all
));
587 init_revisions(&rev
, prefix
);
589 rev
.always_show_header
= 1;
590 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
591 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
593 memset(&opt
, 0, sizeof(opt
));
595 opt
.tweak
= show_setup_revisions_tweak
;
596 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
599 return cmd_log_walk(&rev
);
601 count
= rev
.pending
.nr
;
602 objects
= rev
.pending
.objects
;
603 for (i
= 0; i
< count
&& !ret
; i
++) {
604 struct object
*o
= objects
[i
].item
;
605 const char *name
= objects
[i
].name
;
608 ret
= show_blob_object(&o
->oid
, &rev
, name
);
611 struct tag
*t
= (struct tag
*)o
;
615 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
616 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
618 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
619 ret
= show_tag_object(&o
->oid
, &rev
);
623 o
= parse_object(&t
->tagged
->oid
);
625 ret
= error(_("Could not read object %s"),
626 oid_to_hex(&t
->tagged
->oid
));
634 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
635 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
637 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
638 read_tree_recursive((struct tree
*)o
, "", 0, 0, &match_all
,
639 show_tree_object
, rev
.diffopt
.file
);
643 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
644 rev
.pending
.objects
= NULL
;
645 add_object_array(o
, name
, &rev
.pending
);
646 ret
= cmd_log_walk(&rev
);
649 ret
= error(_("Unknown type: %d"), o
->type
);
657 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
659 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
662 struct setup_revision_opt opt
;
665 git_config(git_log_config
, NULL
);
667 init_revisions(&rev
, prefix
);
668 init_reflog_walk(&rev
.reflog_info
);
669 rev
.verbose_header
= 1;
670 memset(&opt
, 0, sizeof(opt
));
672 cmd_log_init_defaults(&rev
);
673 rev
.abbrev_commit
= 1;
674 rev
.commit_format
= CMIT_FMT_ONELINE
;
675 rev
.use_terminator
= 1;
676 rev
.always_show_header
= 1;
677 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
679 return cmd_log_walk(&rev
);
682 static void log_setup_revisions_tweak(struct rev_info
*rev
,
683 struct setup_revision_opt
*opt
)
685 if (rev
->diffopt
.flags
.default_follow_renames
&&
686 rev
->prune_data
.nr
== 1)
687 rev
->diffopt
.flags
.follow_renames
= 1;
689 /* Turn --cc/-c into -p --cc/-c when -p was not given */
690 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
691 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
693 /* Turn -m on when --cc/-c was given */
694 if (rev
->combine_merges
)
695 rev
->ignore_merges
= 0;
698 int cmd_log(int argc
, const char **argv
, const char *prefix
)
701 struct setup_revision_opt opt
;
704 git_config(git_log_config
, NULL
);
706 init_revisions(&rev
, prefix
);
707 rev
.always_show_header
= 1;
708 memset(&opt
, 0, sizeof(opt
));
710 opt
.revarg_opt
= REVARG_COMMITTISH
;
711 opt
.tweak
= log_setup_revisions_tweak
;
712 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
713 return cmd_log_walk(&rev
);
718 static const char *fmt_patch_suffix
= ".patch";
719 static int numbered
= 0;
720 static int auto_number
= 1;
722 static char *default_attach
= NULL
;
724 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
725 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
726 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
728 static void add_header(const char *value
)
730 struct string_list_item
*item
;
731 int len
= strlen(value
);
732 while (len
&& value
[len
- 1] == '\n')
735 if (!strncasecmp(value
, "to: ", 4)) {
736 item
= string_list_append(&extra_to
, value
+ 4);
738 } else if (!strncasecmp(value
, "cc: ", 4)) {
739 item
= string_list_append(&extra_cc
, value
+ 4);
742 item
= string_list_append(&extra_hdr
, value
);
745 item
->string
[len
] = '\0';
748 #define THREAD_SHALLOW 1
749 #define THREAD_DEEP 2
751 static int do_signoff
;
752 static int base_auto
;
754 static const char *signature
= git_version_string
;
755 static const char *signature_file
;
756 static int config_cover_letter
;
757 static const char *config_output_directory
;
766 static int git_format_config(const char *var
, const char *value
, void *cb
)
768 if (!strcmp(var
, "format.headers")) {
770 die(_("format.headers without value"));
774 if (!strcmp(var
, "format.suffix"))
775 return git_config_string(&fmt_patch_suffix
, var
, value
);
776 if (!strcmp(var
, "format.to")) {
778 return config_error_nonbool(var
);
779 string_list_append(&extra_to
, value
);
782 if (!strcmp(var
, "format.cc")) {
784 return config_error_nonbool(var
);
785 string_list_append(&extra_cc
, value
);
788 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
789 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
792 if (!strcmp(var
, "format.numbered")) {
793 if (value
&& !strcasecmp(value
, "auto")) {
797 numbered
= git_config_bool(var
, value
);
798 auto_number
= auto_number
&& numbered
;
801 if (!strcmp(var
, "format.attach")) {
803 default_attach
= xstrdup(value
);
805 default_attach
= xstrdup(git_version_string
);
808 if (!strcmp(var
, "format.thread")) {
809 if (value
&& !strcasecmp(value
, "deep")) {
810 thread
= THREAD_DEEP
;
813 if (value
&& !strcasecmp(value
, "shallow")) {
814 thread
= THREAD_SHALLOW
;
817 thread
= git_config_bool(var
, value
) && THREAD_SHALLOW
;
820 if (!strcmp(var
, "format.signoff")) {
821 do_signoff
= git_config_bool(var
, value
);
824 if (!strcmp(var
, "format.signature"))
825 return git_config_string(&signature
, var
, value
);
826 if (!strcmp(var
, "format.signaturefile"))
827 return git_config_pathname(&signature_file
, var
, value
);
828 if (!strcmp(var
, "format.coverletter")) {
829 if (value
&& !strcasecmp(value
, "auto")) {
830 config_cover_letter
= COVER_AUTO
;
833 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
836 if (!strcmp(var
, "format.outputdirectory"))
837 return git_config_string(&config_output_directory
, var
, value
);
838 if (!strcmp(var
, "format.useautobase")) {
839 base_auto
= git_config_bool(var
, value
);
842 if (!strcmp(var
, "format.from")) {
843 int b
= git_parse_maybe_bool(value
);
846 from
= xstrdup(value
);
848 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
854 return git_log_config(var
, value
, cb
);
857 static const char *output_directory
= NULL
;
858 static int outdir_offset
;
860 static int open_next_file(struct commit
*commit
, const char *subject
,
861 struct rev_info
*rev
, int quiet
)
863 struct strbuf filename
= STRBUF_INIT
;
864 int suffix_len
= strlen(rev
->patch_suffix
) + 1;
866 if (output_directory
) {
867 strbuf_addstr(&filename
, output_directory
);
869 PATH_MAX
- FORMAT_PATCH_NAME_MAX
- suffix_len
) {
870 strbuf_release(&filename
);
871 return error(_("name of output directory is too long"));
873 strbuf_complete(&filename
, '/');
876 if (rev
->numbered_files
)
877 strbuf_addf(&filename
, "%d", rev
->nr
);
879 fmt_output_commit(&filename
, commit
, rev
);
881 fmt_output_subject(&filename
, subject
, rev
);
884 printf("%s\n", filename
.buf
+ outdir_offset
);
886 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
887 error_errno(_("Cannot open patch file %s"), filename
.buf
);
888 strbuf_release(&filename
);
892 strbuf_release(&filename
);
896 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
898 struct rev_info check_rev
;
899 struct commit
*commit
, *c1
, *c2
;
900 struct object
*o1
, *o2
;
901 unsigned flags1
, flags2
;
903 if (rev
->pending
.nr
!= 2)
904 die(_("Need exactly one range."));
906 o1
= rev
->pending
.objects
[0].item
;
907 o2
= rev
->pending
.objects
[1].item
;
910 c1
= lookup_commit_reference(&o1
->oid
);
911 c2
= lookup_commit_reference(&o2
->oid
);
913 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
914 die(_("Not a range."));
918 /* given a range a..b get all patch ids for b..a */
919 init_revisions(&check_rev
, rev
->prefix
);
920 check_rev
.max_parents
= 1;
921 o1
->flags
^= UNINTERESTING
;
922 o2
->flags
^= UNINTERESTING
;
923 add_pending_object(&check_rev
, o1
, "o1");
924 add_pending_object(&check_rev
, o2
, "o2");
925 if (prepare_revision_walk(&check_rev
))
926 die(_("revision walk setup failed"));
928 while ((commit
= get_revision(&check_rev
)) != NULL
) {
929 add_commit_patch_id(commit
, ids
);
932 /* reset for next revision walk */
933 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
934 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
939 static void gen_message_id(struct rev_info
*info
, char *base
)
941 struct strbuf buf
= STRBUF_INIT
;
942 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
943 (timestamp_t
) time(NULL
),
944 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
945 info
->message_id
= strbuf_detach(&buf
, NULL
);
948 static void print_signature(FILE *file
)
950 if (!signature
|| !*signature
)
953 fprintf(file
, "-- \n%s", signature
);
954 if (signature
[strlen(signature
)-1] != '\n')
959 static void add_branch_description(struct strbuf
*buf
, const char *branch_name
)
961 struct strbuf desc
= STRBUF_INIT
;
962 if (!branch_name
|| !*branch_name
)
964 read_branch_desc(&desc
, branch_name
);
966 strbuf_addch(buf
, '\n');
967 strbuf_addbuf(buf
, &desc
);
968 strbuf_addch(buf
, '\n');
970 strbuf_release(&desc
);
973 static char *find_branch_name(struct rev_info
*rev
)
975 int i
, positive
= -1;
976 struct object_id branch_oid
;
977 const struct object_id
*tip_oid
;
979 char *full_ref
, *branch
= NULL
;
981 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
982 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
991 ref
= rev
->cmdline
.rev
[positive
].name
;
992 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
993 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
) &&
994 skip_prefix(full_ref
, "refs/heads/", &v
) &&
995 !oidcmp(tip_oid
, &branch_oid
))
1001 static void show_diffstat(struct rev_info
*rev
,
1002 struct commit
*origin
, struct commit
*head
)
1004 struct diff_options opts
;
1006 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1007 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1008 opts
.stat_width
= MAIL_DEFAULT_WRAP
;
1010 diff_setup_done(&opts
);
1012 diff_tree_oid(get_commit_tree_oid(origin
),
1013 get_commit_tree_oid(head
),
1015 diffcore_std(&opts
);
1018 fprintf(rev
->diffopt
.file
, "\n");
1021 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1022 struct commit
*origin
,
1023 int nr
, struct commit
**list
,
1024 const char *branch_name
,
1027 const char *committer
;
1028 const char *body
= "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
1030 struct shortlog log
;
1031 struct strbuf sb
= STRBUF_INIT
;
1033 const char *encoding
= "UTF-8";
1034 int need_8bit_cte
= 0;
1035 struct pretty_print_context pp
= {0};
1036 struct commit
*head
= list
[0];
1038 if (!cmit_fmt_is_mail(rev
->commit_format
))
1039 die(_("Cover letter needs email format"));
1041 committer
= git_committer_info(0);
1044 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1047 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1049 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1050 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1051 if (has_non_ascii(buf
))
1053 unuse_commit_buffer(list
[i
], buf
);
1057 branch_name
= find_branch_name(rev
);
1060 pp
.fmt
= CMIT_FMT_EMAIL
;
1061 pp
.date_mode
.type
= DATE_RFC2822
;
1063 pp
.print_email_subject
= 1;
1064 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1065 pp_title_line(&pp
, &msg
, &sb
, encoding
, need_8bit_cte
);
1066 pp_remainder(&pp
, &msg
, &sb
, 0);
1067 add_branch_description(&sb
, branch_name
);
1068 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1070 strbuf_release(&sb
);
1072 shortlog_init(&log
);
1074 log
.wrap
= MAIL_DEFAULT_WRAP
;
1077 log
.file
= rev
->diffopt
.file
;
1078 for (i
= 0; i
< nr
; i
++)
1079 shortlog_add_commit(&log
, list
[i
]);
1081 shortlog_output(&log
);
1083 /* We can only do diffstat with a unique reference point */
1085 show_diffstat(rev
, origin
, head
);
1087 if (rev
->idiff_oid1
) {
1088 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1089 show_interdiff(rev
, 0);
1093 static const char *clean_message_id(const char *msg_id
)
1096 const char *a
, *z
, *m
;
1099 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1104 if (!isspace(ch
) && (ch
!= '>'))
1109 die(_("insane in-reply-to: %s"), msg_id
);
1112 return xmemdupz(a
, z
- a
);
1115 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1117 if (output_directory
&& is_absolute_path(output_directory
))
1118 return output_directory
;
1120 if (!prefix
|| !*prefix
) {
1121 if (output_directory
)
1122 return output_directory
;
1123 /* The user did not explicitly ask for "./" */
1128 outdir_offset
= strlen(prefix
);
1129 if (!output_directory
)
1132 return prefix_filename(prefix
, output_directory
);
1135 static const char * const builtin_format_patch_usage
[] = {
1136 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1140 static int keep_subject
= 0;
1142 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1144 ((struct rev_info
*)opt
->value
)->total
= -1;
1149 static int subject_prefix
= 0;
1151 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1155 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1159 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1161 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1164 static int numbered_cmdline_opt
= 0;
1166 static int numbered_callback(const struct option
*opt
, const char *arg
,
1169 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1175 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1178 return numbered_callback(opt
, arg
, 1);
1181 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1184 const char **dir
= (const char **)opt
->value
;
1186 die(_("Two output directories?"));
1191 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1193 int *thread
= (int *)opt
->value
;
1196 else if (!arg
|| !strcmp(arg
, "shallow"))
1197 *thread
= THREAD_SHALLOW
;
1198 else if (!strcmp(arg
, "deep"))
1199 *thread
= THREAD_DEEP
;
1205 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1207 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1209 rev
->mime_boundary
= NULL
;
1211 rev
->mime_boundary
= arg
;
1213 rev
->mime_boundary
= git_version_string
;
1214 rev
->no_inline
= unset
? 0 : 1;
1218 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1220 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1222 rev
->mime_boundary
= NULL
;
1224 rev
->mime_boundary
= arg
;
1226 rev
->mime_boundary
= git_version_string
;
1231 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1234 string_list_clear(&extra_hdr
, 0);
1235 string_list_clear(&extra_to
, 0);
1236 string_list_clear(&extra_cc
, 0);
1243 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1246 string_list_clear(&extra_to
, 0);
1248 string_list_append(&extra_to
, arg
);
1252 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1255 string_list_clear(&extra_cc
, 0);
1257 string_list_append(&extra_cc
, arg
);
1261 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1263 char **from
= opt
->value
;
1270 *from
= xstrdup(arg
);
1272 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1276 struct base_tree_info
{
1277 struct object_id base_commit
;
1278 int nr_patch_id
, alloc_patch_id
;
1279 struct object_id
*patch_id
;
1282 static struct commit
*get_base_commit(const char *base_commit
,
1283 struct commit
**list
,
1286 struct commit
*base
= NULL
;
1287 struct commit
**rev
;
1288 int i
= 0, rev_nr
= 0;
1290 if (base_commit
&& strcmp(base_commit
, "auto")) {
1291 base
= lookup_commit_reference_by_name(base_commit
);
1293 die(_("Unknown commit %s"), base_commit
);
1294 } else if ((base_commit
&& !strcmp(base_commit
, "auto")) || base_auto
) {
1295 struct branch
*curr_branch
= branch_get(NULL
);
1296 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1298 struct commit_list
*base_list
;
1299 struct commit
*commit
;
1300 struct object_id oid
;
1302 if (get_oid(upstream
, &oid
))
1303 die(_("Failed to resolve '%s' as a valid ref."), upstream
);
1304 commit
= lookup_commit_or_die(&oid
, "upstream base");
1305 base_list
= get_merge_bases_many(commit
, total
, list
);
1306 /* There should be one and only one merge base. */
1307 if (!base_list
|| base_list
->next
)
1308 die(_("Could not find exact merge base."));
1309 base
= base_list
->item
;
1310 free_commit_list(base_list
);
1312 die(_("Failed to get upstream, if you want to record base commit automatically,\n"
1313 "please use git branch --set-upstream-to to track a remote branch.\n"
1314 "Or you could specify base commit by --base=<base-commit-id> manually."));
1318 ALLOC_ARRAY(rev
, total
);
1319 for (i
= 0; i
< total
; i
++)
1324 * Get merge base through pair-wise computations
1325 * and store it in rev[0].
1327 while (rev_nr
> 1) {
1328 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1329 struct commit_list
*merge_base
;
1330 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1331 if (!merge_base
|| merge_base
->next
)
1332 die(_("Failed to find exact merge base"));
1334 rev
[i
] = merge_base
->item
;
1338 rev
[i
] = rev
[2 * i
];
1339 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1342 if (!in_merge_bases(base
, rev
[0]))
1343 die(_("base commit should be the ancestor of revision list"));
1345 for (i
= 0; i
< total
; i
++) {
1346 if (base
== list
[i
])
1347 die(_("base commit shouldn't be in revision list"));
1354 define_commit_slab(commit_base
, int);
1356 static void prepare_bases(struct base_tree_info
*bases
,
1357 struct commit
*base
,
1358 struct commit
**list
,
1361 struct commit
*commit
;
1362 struct rev_info revs
;
1363 struct diff_options diffopt
;
1364 struct commit_base commit_base
;
1370 init_commit_base(&commit_base
);
1371 diff_setup(&diffopt
);
1372 diffopt
.flags
.recursive
= 1;
1373 diff_setup_done(&diffopt
);
1375 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1377 init_revisions(&revs
, NULL
);
1378 revs
.max_parents
= 1;
1379 revs
.topo_order
= 1;
1380 for (i
= 0; i
< total
; i
++) {
1381 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1382 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1383 *commit_base_at(&commit_base
, list
[i
]) = 1;
1385 base
->object
.flags
|= UNINTERESTING
;
1386 add_pending_object(&revs
, &base
->object
, "base");
1388 if (prepare_revision_walk(&revs
))
1389 die(_("revision walk setup failed"));
1391 * Traverse the commits list, get prerequisite patch ids
1392 * and stuff them in bases structure.
1394 while ((commit
= get_revision(&revs
)) != NULL
) {
1395 struct object_id oid
;
1396 struct object_id
*patch_id
;
1397 if (*commit_base_at(&commit_base
, commit
))
1399 if (commit_patch_id(commit
, &diffopt
, &oid
, 0))
1400 die(_("cannot get patch id"));
1401 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1402 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1403 oidcpy(patch_id
, &oid
);
1404 bases
->nr_patch_id
++;
1406 clear_commit_base(&commit_base
);
1409 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1413 /* Only do this once, either for the cover or for the first one */
1414 if (is_null_oid(&bases
->base_commit
))
1417 /* Show the base commit */
1418 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1420 /* Show the prerequisite patches */
1421 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1422 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1424 free(bases
->patch_id
);
1425 bases
->nr_patch_id
= 0;
1426 bases
->alloc_patch_id
= 0;
1427 oidclr(&bases
->base_commit
);
1430 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1431 const char *generic
, const char *rerolled
)
1433 if (reroll_count
<= 0)
1434 strbuf_addstr(sb
, generic
);
1435 else /* RFC may be v0, so allow -v1 to diff against v0 */
1436 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1440 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1442 struct commit
*commit
;
1443 struct commit
**list
= NULL
;
1444 struct rev_info rev
;
1445 struct setup_revision_opt s_r_opt
;
1446 int nr
= 0, total
, i
;
1448 int start_number
= -1;
1449 int just_numbers
= 0;
1450 int ignore_if_in_upstream
= 0;
1451 int cover_letter
= -1;
1452 int boundary_count
= 0;
1453 int no_binary_diff
= 0;
1454 int zero_commit
= 0;
1455 struct commit
*origin
= NULL
;
1456 const char *in_reply_to
= NULL
;
1457 struct patch_ids ids
;
1458 struct strbuf buf
= STRBUF_INIT
;
1459 int use_patch_format
= 0;
1461 int reroll_count
= -1;
1462 char *branch_name
= NULL
;
1463 char *base_commit
= NULL
;
1464 struct base_tree_info bases
;
1465 int show_progress
= 0;
1466 struct progress
*progress
= NULL
;
1467 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1468 struct strbuf idiff_title
= STRBUF_INIT
;
1470 const struct option builtin_format_patch_options
[] = {
1471 { OPTION_CALLBACK
, 'n', "numbered", &numbered
, NULL
,
1472 N_("use [PATCH n/m] even with a single patch"),
1473 PARSE_OPT_NOARG
, numbered_callback
},
1474 { OPTION_CALLBACK
, 'N', "no-numbered", &numbered
, NULL
,
1475 N_("use [PATCH] even with multiple patches"),
1476 PARSE_OPT_NOARG
, no_numbered_callback
},
1477 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1478 OPT_BOOL(0, "stdout", &use_stdout
,
1479 N_("print patches to standard out")),
1480 OPT_BOOL(0, "cover-letter", &cover_letter
,
1481 N_("generate a cover letter")),
1482 OPT_BOOL(0, "numbered-files", &just_numbers
,
1483 N_("use simple number sequence for output file names")),
1484 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1485 N_("use <sfx> instead of '.patch'")),
1486 OPT_INTEGER(0, "start-number", &start_number
,
1487 N_("start numbering patches at <n> instead of 1")),
1488 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1489 N_("mark the series as Nth re-roll")),
1490 { OPTION_CALLBACK
, 0, "rfc", &rev
, NULL
,
1491 N_("Use [RFC PATCH] instead of [PATCH]"),
1492 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
},
1493 { OPTION_CALLBACK
, 0, "subject-prefix", &rev
, N_("prefix"),
1494 N_("Use [<prefix>] instead of [PATCH]"),
1495 PARSE_OPT_NONEG
, subject_prefix_callback
},
1496 { OPTION_CALLBACK
, 'o', "output-directory", &output_directory
,
1497 N_("dir"), N_("store resulting files in <dir>"),
1498 PARSE_OPT_NONEG
, output_directory_callback
},
1499 { OPTION_CALLBACK
, 'k', "keep-subject", &rev
, NULL
,
1500 N_("don't strip/add [PATCH]"),
1501 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
},
1502 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1503 N_("don't output binary diffs")),
1504 OPT_BOOL(0, "zero-commit", &zero_commit
,
1505 N_("output all-zero hash in From header")),
1506 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1507 N_("don't include a patch matching a commit upstream")),
1508 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1509 N_("show patch format instead of default (patch + stat)"),
1510 1, PARSE_OPT_NONEG
),
1511 OPT_GROUP(N_("Messaging")),
1512 { OPTION_CALLBACK
, 0, "add-header", NULL
, N_("header"),
1513 N_("add email header"), 0, header_callback
},
1514 { OPTION_CALLBACK
, 0, "to", NULL
, N_("email"), N_("add To: header"),
1516 { OPTION_CALLBACK
, 0, "cc", NULL
, N_("email"), N_("add Cc: header"),
1518 { OPTION_CALLBACK
, 0, "from", &from
, N_("ident"),
1519 N_("set From address to <ident> (or committer ident if absent)"),
1520 PARSE_OPT_OPTARG
, from_callback
},
1521 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1522 N_("make first mail a reply to <message-id>")),
1523 { OPTION_CALLBACK
, 0, "attach", &rev
, N_("boundary"),
1524 N_("attach the patch"), PARSE_OPT_OPTARG
,
1526 { OPTION_CALLBACK
, 0, "inline", &rev
, N_("boundary"),
1527 N_("inline the patch"),
1528 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1530 { OPTION_CALLBACK
, 0, "thread", &thread
, N_("style"),
1531 N_("enable message threading, styles: shallow, deep"),
1532 PARSE_OPT_OPTARG
, thread_callback
},
1533 OPT_STRING(0, "signature", &signature
, N_("signature"),
1534 N_("add a signature")),
1535 OPT_STRING(0, "base", &base_commit
, N_("base-commit"),
1536 N_("add prerequisite tree info to the patch series")),
1537 OPT_FILENAME(0, "signature-file", &signature_file
,
1538 N_("add a signature from a file")),
1539 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1540 OPT_BOOL(0, "progress", &show_progress
,
1541 N_("show progress while generating patches")),
1542 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1543 N_("show changes against <rev> in cover letter"),
1544 parse_opt_object_name
),
1548 extra_hdr
.strdup_strings
= 1;
1549 extra_to
.strdup_strings
= 1;
1550 extra_cc
.strdup_strings
= 1;
1551 init_log_defaults();
1552 git_config(git_format_config
, NULL
);
1553 init_revisions(&rev
, prefix
);
1554 rev
.commit_format
= CMIT_FMT_EMAIL
;
1555 rev
.expand_tabs_in_log_default
= 0;
1556 rev
.verbose_header
= 1;
1558 rev
.max_parents
= 1;
1559 rev
.diffopt
.flags
.recursive
= 1;
1560 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1561 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1562 s_r_opt
.def
= "HEAD";
1563 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1565 if (default_attach
) {
1566 rev
.mime_boundary
= default_attach
;
1571 * Parse the arguments before setup_revisions(), or something
1572 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1573 * possibly a valid SHA1.
1575 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1576 builtin_format_patch_usage
,
1577 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1578 PARSE_OPT_KEEP_DASHDASH
);
1580 if (0 < reroll_count
) {
1581 struct strbuf sprefix
= STRBUF_INIT
;
1582 strbuf_addf(&sprefix
, "%s v%d",
1583 rev
.subject_prefix
, reroll_count
);
1584 rev
.reroll_count
= reroll_count
;
1585 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1588 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1589 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1590 strbuf_addch(&buf
, '\n');
1594 strbuf_addstr(&buf
, "To: ");
1595 for (i
= 0; i
< extra_to
.nr
; i
++) {
1597 strbuf_addstr(&buf
, " ");
1598 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1599 if (i
+ 1 < extra_to
.nr
)
1600 strbuf_addch(&buf
, ',');
1601 strbuf_addch(&buf
, '\n');
1605 strbuf_addstr(&buf
, "Cc: ");
1606 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1608 strbuf_addstr(&buf
, " ");
1609 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1610 if (i
+ 1 < extra_cc
.nr
)
1611 strbuf_addch(&buf
, ',');
1612 strbuf_addch(&buf
, '\n');
1615 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1618 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1619 die(_("invalid ident line: %s"), from
);
1622 if (start_number
< 0)
1626 * If numbered is set solely due to format.numbered in config,
1627 * and it would conflict with --keep-subject (-k) from the
1628 * command line, reset "numbered".
1630 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1633 if (numbered
&& keep_subject
)
1634 die (_("-n and -k are mutually exclusive."));
1635 if (keep_subject
&& subject_prefix
)
1636 die (_("--subject-prefix/--rfc and -k are mutually exclusive."));
1637 rev
.preserve_subject
= keep_subject
;
1639 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1641 die (_("unrecognized argument: %s"), argv
[1]);
1643 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1644 die(_("--name-only does not make sense"));
1645 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1646 die(_("--name-status does not make sense"));
1647 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1648 die(_("--check does not make sense"));
1650 if (!use_patch_format
&&
1651 (!rev
.diffopt
.output_format
||
1652 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1653 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1654 if (!rev
.diffopt
.stat_width
)
1655 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1657 /* Always generate a patch */
1658 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1660 rev
.zero_commit
= zero_commit
;
1662 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1663 rev
.diffopt
.flags
.binary
= 1;
1666 init_display_notes(&rev
.notes_opt
);
1668 if (!output_directory
&& !use_stdout
)
1669 output_directory
= config_output_directory
;
1672 output_directory
= set_outdir(prefix
, output_directory
);
1676 if (output_directory
) {
1677 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1678 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1680 die(_("standard output, or directory, which one?"));
1681 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1682 die_errno(_("Could not create directory '%s'"),
1686 if (rev
.pending
.nr
== 1) {
1689 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1691 * This is traditional behaviour of "git format-patch
1692 * origin" that prepares what the origin side still
1695 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1696 add_head_to_pending(&rev
);
1700 * Otherwise, it is "format-patch -22 HEAD", and/or
1701 * "format-patch --root HEAD". The user wants
1702 * get_revision() to do the usual traversal.
1705 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
1709 const char *ref
, *v
;
1710 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1712 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
1713 branch_name
= xstrdup(v
);
1715 branch_name
= xstrdup(""); /* no branch */
1720 * We cannot move this anywhere earlier because we do want to
1721 * know if --root was given explicitly from the command line.
1723 rev
.show_root_diff
= 1;
1725 if (ignore_if_in_upstream
) {
1726 /* Don't say anything if head and upstream are the same. */
1727 if (rev
.pending
.nr
== 2) {
1728 struct object_array_entry
*o
= rev
.pending
.objects
;
1729 if (oidcmp(&o
[0].item
->oid
, &o
[1].item
->oid
) == 0)
1732 get_patch_ids(&rev
, &ids
);
1735 if (prepare_revision_walk(&rev
))
1736 die(_("revision walk setup failed"));
1738 while ((commit
= get_revision(&rev
)) != NULL
) {
1739 if (commit
->object
.flags
& BOUNDARY
) {
1741 origin
= (boundary_count
== 1) ? commit
: NULL
;
1745 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
1749 REALLOC_ARRAY(list
, nr
);
1750 list
[nr
- 1] = commit
;
1756 if (cover_letter
== -1) {
1757 if (config_cover_letter
== COVER_AUTO
)
1758 cover_letter
= (total
> 1);
1760 cover_letter
= (config_cover_letter
== COVER_ON
);
1762 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
1765 rev
.total
= total
+ start_number
- 1;
1767 if (idiff_prev
.nr
) {
1769 die(_("--interdiff requires --cover-letter"));
1770 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
1771 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
1772 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
1774 _("Interdiff against v%d:"));
1778 ; /* --no-signature inhibits all signatures */
1779 } else if (signature
&& signature
!= git_version_string
) {
1780 ; /* non-default signature already set */
1781 } else if (signature_file
) {
1782 struct strbuf buf
= STRBUF_INIT
;
1784 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
1785 die_errno(_("unable to read signature file '%s'"), signature_file
);
1786 signature
= strbuf_detach(&buf
, NULL
);
1789 memset(&bases
, 0, sizeof(bases
));
1790 if (base_commit
|| base_auto
) {
1791 struct commit
*base
= get_base_commit(base_commit
, list
, nr
);
1792 reset_revision_walk();
1793 clear_object_flags(UNINTERESTING
);
1794 prepare_bases(&bases
, base
, list
, nr
);
1797 if (in_reply_to
|| thread
|| cover_letter
)
1798 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
1800 const char *msgid
= clean_message_id(in_reply_to
);
1801 string_list_append(rev
.ref_message_ids
, msgid
);
1803 rev
.numbered_files
= just_numbers
;
1804 rev
.patch_suffix
= fmt_patch_suffix
;
1807 gen_message_id(&rev
, "cover");
1808 make_cover_letter(&rev
, use_stdout
,
1809 origin
, nr
, list
, branch_name
, quiet
);
1810 print_bases(&bases
, rev
.diffopt
.file
);
1811 print_signature(rev
.diffopt
.file
);
1815 rev
.add_signoff
= do_signoff
;
1818 progress
= start_delayed_progress(_("Generating patches"), total
);
1821 display_progress(progress
, total
- nr
);
1823 rev
.nr
= total
- nr
+ (start_number
- 1);
1824 /* Make the second and subsequent mails replies to the first */
1826 /* Have we already had a message ID? */
1827 if (rev
.message_id
) {
1829 * For deep threading: make every mail
1830 * a reply to the previous one, no
1831 * matter what other options are set.
1833 * For shallow threading:
1835 * Without --cover-letter and
1836 * --in-reply-to, make every mail a
1837 * reply to the one before.
1839 * With --in-reply-to but no
1840 * --cover-letter, make every mail a
1841 * reply to the <reply-to>.
1843 * With --cover-letter, make every
1844 * mail but the cover letter a reply
1845 * to the cover letter. The cover
1846 * letter is a reply to the
1847 * --in-reply-to, if specified.
1849 if (thread
== THREAD_SHALLOW
1850 && rev
.ref_message_ids
->nr
> 0
1851 && (!cover_letter
|| rev
.nr
> 1))
1852 free(rev
.message_id
);
1854 string_list_append(rev
.ref_message_ids
,
1857 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
1861 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
1862 die(_("Failed to create output files"));
1863 shown
= log_tree_commit(&rev
, commit
);
1864 free_commit_buffer(commit
);
1866 /* We put one extra blank line between formatted
1867 * patches and this flag is used by log-tree code
1868 * to see if it needs to emit a LF before showing
1869 * the log; when using one file per patch, we do
1870 * not want the extra blank line.
1875 print_bases(&bases
, rev
.diffopt
.file
);
1876 if (rev
.mime_boundary
)
1877 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
1878 mime_boundary_leader
,
1881 print_signature(rev
.diffopt
.file
);
1884 fclose(rev
.diffopt
.file
);
1886 stop_progress(&progress
);
1889 string_list_clear(&extra_to
, 0);
1890 string_list_clear(&extra_cc
, 0);
1891 string_list_clear(&extra_hdr
, 0);
1892 if (ignore_if_in_upstream
)
1893 free_patch_ids(&ids
);
1896 oid_array_clear(&idiff_prev
);
1897 strbuf_release(&idiff_title
);
1901 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
1903 struct object_id oid
;
1904 if (get_oid(arg
, &oid
) == 0) {
1905 struct commit
*commit
= lookup_commit_reference(&oid
);
1907 commit
->object
.flags
|= flags
;
1908 add_pending_object(revs
, &commit
->object
, arg
);
1915 static const char * const cherry_usage
[] = {
1916 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
1920 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
1921 int abbrev
, FILE *file
)
1924 fprintf(file
, "%c %s\n", sign
,
1925 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
1927 struct strbuf buf
= STRBUF_INIT
;
1928 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
1929 fprintf(file
, "%c %s %s\n", sign
,
1930 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
1932 strbuf_release(&buf
);
1936 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
1938 struct rev_info revs
;
1939 struct patch_ids ids
;
1940 struct commit
*commit
;
1941 struct commit_list
*list
= NULL
;
1942 struct branch
*current_branch
;
1943 const char *upstream
;
1944 const char *head
= "HEAD";
1945 const char *limit
= NULL
;
1946 int verbose
= 0, abbrev
= 0;
1948 struct option options
[] = {
1949 OPT__ABBREV(&abbrev
),
1950 OPT__VERBOSE(&verbose
, N_("be verbose")),
1954 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
1967 current_branch
= branch_get(NULL
);
1968 upstream
= branch_get_upstream(current_branch
, NULL
);
1970 fprintf(stderr
, _("Could not find a tracked"
1971 " remote branch, please"
1972 " specify <upstream> manually.\n"));
1973 usage_with_options(cherry_usage
, options
);
1977 init_revisions(&revs
, prefix
);
1978 revs
.max_parents
= 1;
1980 if (add_pending_commit(head
, &revs
, 0))
1981 die(_("Unknown commit %s"), head
);
1982 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
1983 die(_("Unknown commit %s"), upstream
);
1985 /* Don't say anything if head and upstream are the same. */
1986 if (revs
.pending
.nr
== 2) {
1987 struct object_array_entry
*o
= revs
.pending
.objects
;
1988 if (oidcmp(&o
[0].item
->oid
, &o
[1].item
->oid
) == 0)
1992 get_patch_ids(&revs
, &ids
);
1994 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
1995 die(_("Unknown commit %s"), limit
);
1997 /* reverse the list of commits */
1998 if (prepare_revision_walk(&revs
))
1999 die(_("revision walk setup failed"));
2000 while ((commit
= get_revision(&revs
)) != NULL
) {
2001 commit_list_insert(commit
, &list
);
2007 commit
= list
->item
;
2008 if (has_commit_patch_id(commit
, &ids
))
2010 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2014 free_patch_ids(&ids
);