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"
15 #include "diff-merges.h"
20 #include "reflog-walk.h"
21 #include "patch-ids.h"
22 #include "run-command.h"
25 #include "string-list.h"
26 #include "parse-options.h"
29 #include "streaming.h"
32 #include "gpg-interface.h"
34 #include "commit-slab.h"
35 #include "repository.h"
36 #include "commit-reach.h"
37 #include "range-diff.h"
39 #define MAIL_DEFAULT_WRAP 72
40 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
41 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
43 /* Set a default date-time format for git log ("log.date" config variable) */
44 static const char *default_date_mode
= NULL
;
46 static int default_abbrev_commit
;
47 static int default_show_root
= 1;
48 static int default_follow
;
49 static int default_show_signature
;
50 static int default_encode_email_headers
= 1;
51 static int decoration_style
;
52 static int decoration_given
;
53 static int use_mailmap_config
= 1;
54 static const char *fmt_patch_subject_prefix
= "PATCH";
55 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
56 static const char *fmt_pretty
;
58 static const char * const builtin_log_usage
[] = {
59 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
60 N_("git show [<options>] <object>..."),
64 struct line_opt_callback_data
{
67 struct string_list args
;
70 static int session_is_interactive(void)
72 return isatty(1) || pager_in_use();
75 static int auto_decoration_style(void)
77 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
80 static int parse_decoration_style(const char *value
)
82 switch (git_parse_maybe_bool(value
)) {
84 return DECORATE_SHORT_REFS
;
90 if (!strcmp(value
, "full"))
91 return DECORATE_FULL_REFS
;
92 else if (!strcmp(value
, "short"))
93 return DECORATE_SHORT_REFS
;
94 else if (!strcmp(value
, "auto"))
95 return auto_decoration_style();
97 * Please update _git_log() in git-completion.bash when you
98 * add new decoration styles.
103 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
106 decoration_style
= 0;
108 decoration_style
= parse_decoration_style(arg
);
110 decoration_style
= DECORATE_SHORT_REFS
;
112 if (decoration_style
< 0)
113 die(_("invalid --decorate option: %s"), arg
);
115 decoration_given
= 1;
120 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
122 struct line_opt_callback_data
*data
= option
->value
;
124 BUG_ON_OPT_NEG(unset
);
129 data
->rev
->line_level_traverse
= 1;
130 string_list_append(&data
->args
, arg
);
135 static void init_log_defaults(void)
137 init_diff_ui_defaults();
139 decoration_style
= auto_decoration_style();
142 static void cmd_log_init_defaults(struct rev_info
*rev
)
145 get_commit_format(fmt_pretty
, rev
);
147 rev
->diffopt
.flags
.default_follow_renames
= 1;
148 rev
->verbose_header
= 1;
149 rev
->diffopt
.flags
.recursive
= 1;
150 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
151 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
152 rev
->abbrev_commit
= default_abbrev_commit
;
153 rev
->show_root_diff
= default_show_root
;
154 rev
->subject_prefix
= fmt_patch_subject_prefix
;
155 rev
->patch_name_max
= fmt_patch_name_max
;
156 rev
->show_signature
= default_show_signature
;
157 rev
->encode_email_headers
= default_encode_email_headers
;
158 rev
->diffopt
.flags
.allow_textconv
= 1;
160 if (default_date_mode
)
161 parse_date_format(default_date_mode
, &rev
->date_mode
);
164 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
165 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
167 struct userformat_want w
;
168 int quiet
= 0, source
= 0, mailmap
;
169 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
170 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
171 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
172 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
173 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
174 &decorate_refs_exclude
,
175 &decorate_refs_exclude_config
};
176 static struct revision_sources revision_sources
;
178 const struct option builtin_log_options
[] = {
179 OPT__QUIET(&quiet
, N_("suppress diff output")),
180 OPT_BOOL(0, "source", &source
, N_("show source")),
181 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
182 OPT_ALIAS(0, "mailmap", "use-mailmap"),
183 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
184 N_("pattern"), N_("only decorate refs that match <pattern>")),
185 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
186 N_("pattern"), N_("do not decorate refs that match <pattern>")),
187 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
188 PARSE_OPT_OPTARG
, decorate_callback
),
189 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
190 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
191 log_line_range_callback
),
196 line_cb
.prefix
= prefix
;
198 mailmap
= use_mailmap_config
;
199 argc
= parse_options(argc
, argv
, prefix
,
200 builtin_log_options
, builtin_log_usage
,
201 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
202 PARSE_OPT_KEEP_DASHDASH
);
205 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
206 argc
= setup_revisions(argc
, argv
, rev
, opt
);
208 /* Any arguments at this point are not recognized */
210 die(_("unrecognized argument: %s"), argv
[1]);
212 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
213 die(_("-L<range>:<file> cannot be used with pathspec"));
215 memset(&w
, 0, sizeof(w
));
216 userformat_find_requirements(NULL
, &w
);
218 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
221 load_display_notes(&rev
->notes_opt
);
223 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
224 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
225 rev
->always_show_header
= 0;
227 if (source
|| w
.source
) {
228 init_revision_sources(&revision_sources
);
229 rev
->sources
= &revision_sources
;
233 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
234 read_mailmap(rev
->mailmap
);
237 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
239 * "log --pretty=raw" is special; ignore UI oriented
240 * configuration variables such as decoration.
242 if (!decoration_given
)
243 decoration_style
= 0;
244 if (!rev
->abbrev_commit_given
)
245 rev
->abbrev_commit
= 0;
248 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
251 * Disable decoration loading if the format will not
254 decoration_style
= 0;
255 } else if (!decoration_style
) {
257 * If we are going to show them, make sure we do load
258 * them here, but taking care not to override a
259 * specific style set by config or --decorate.
261 decoration_style
= DECORATE_SHORT_REFS
;
265 if (decoration_style
|| rev
->simplify_by_decoration
) {
266 const struct string_list
*config_exclude
=
267 repo_config_get_value_multi(the_repository
,
268 "log.excludeDecoration");
270 if (config_exclude
) {
271 struct string_list_item
*item
;
272 for_each_string_list_item(item
, config_exclude
)
273 string_list_append(&decorate_refs_exclude_config
,
277 if (decoration_style
)
278 rev
->show_decorations
= 1;
280 load_ref_decorations(&decoration_filter
, decoration_style
);
283 if (rev
->line_level_traverse
)
284 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
289 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
290 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
292 cmd_log_init_defaults(rev
);
293 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
297 * This gives a rough estimate for how many commits we
298 * will print out in the list.
300 static int estimate_commit_count(struct commit_list
*list
)
305 struct commit
*commit
= list
->item
;
306 unsigned int flags
= commit
->object
.flags
;
308 if (!(flags
& (TREESAME
| UNINTERESTING
)))
314 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
316 if (rev
->shown_one
) {
318 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
319 putchar(rev
->diffopt
.line_termination
);
321 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
324 static struct itimerval early_output_timer
;
326 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
328 int i
= revs
->early_output
;
330 int no_free
= revs
->diffopt
.no_free
;
332 revs
->diffopt
.no_free
= 0;
333 sort_in_topological_order(&list
, revs
->sort_order
);
335 struct commit
*commit
= list
->item
;
336 switch (simplify_commit(revs
, commit
)) {
339 int n
= estimate_commit_count(list
);
340 show_early_header(revs
, "incomplete", n
);
343 log_tree_commit(revs
, commit
);
349 revs
->diffopt
.no_free
= no_free
;
350 diff_free(&revs
->diffopt
);
356 /* Did we already get enough commits for the early output? */
358 revs
->diffopt
.no_free
= 0;
359 diff_free(&revs
->diffopt
);
364 * ..if no, then repeat it twice a second until we
367 * NOTE! We don't use "it_interval", because if the
368 * reader isn't listening, we want our output to be
369 * throttled by the writing, and not have the timer
370 * trigger every second even if we're blocked on a
373 early_output_timer
.it_value
.tv_sec
= 0;
374 early_output_timer
.it_value
.tv_usec
= 500000;
375 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
378 static void early_output(int signal
)
380 show_early_output
= log_show_early
;
383 static void setup_early_output(void)
388 * Set up the signal handler, minimally intrusively:
389 * we only set a single volatile integer word (not
390 * using sigatomic_t - trying to avoid unnecessary
391 * system dependencies and headers), and using
394 memset(&sa
, 0, sizeof(sa
));
395 sa
.sa_handler
= early_output
;
396 sigemptyset(&sa
.sa_mask
);
397 sa
.sa_flags
= SA_RESTART
;
398 sigaction(SIGALRM
, &sa
, NULL
);
401 * If we can get the whole output in less than a
402 * tenth of a second, don't even bother doing the
403 * early-output thing..
405 * This is a one-time-only trigger.
407 early_output_timer
.it_value
.tv_sec
= 0;
408 early_output_timer
.it_value
.tv_usec
= 100000;
409 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
412 static void finish_early_output(struct rev_info
*rev
)
414 int n
= estimate_commit_count(rev
->commits
);
415 signal(SIGALRM
, SIG_IGN
);
416 show_early_header(rev
, "done", n
);
419 static int cmd_log_walk(struct rev_info
*rev
)
421 struct commit
*commit
;
425 if (rev
->early_output
)
426 setup_early_output();
428 if (prepare_revision_walk(rev
))
429 die(_("revision walk setup failed"));
431 if (rev
->early_output
)
432 finish_early_output(rev
);
435 * For --check and --exit-code, the exit code is based on CHECK_FAILED
436 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
437 * retain that state information if replacing rev->diffopt in this loop
439 rev
->diffopt
.no_free
= 1;
440 while ((commit
= get_revision(rev
)) != NULL
) {
441 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
443 * We decremented max_count in get_revision,
444 * but we didn't actually show the commit.
447 if (!rev
->reflog_info
) {
449 * We may show a given commit multiple times when
450 * walking the reflogs.
452 free_commit_buffer(the_repository
->parsed_objects
,
454 free_commit_list(commit
->parents
);
455 commit
->parents
= NULL
;
457 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
458 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
459 if (rev
->diffopt
.degraded_cc_to_c
)
462 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
463 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
464 rev
->diffopt
.no_free
= 0;
465 diff_free(&rev
->diffopt
);
467 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
468 rev
->diffopt
.flags
.check_failed
) {
471 return diff_result_code(&rev
->diffopt
, 0);
474 static int git_log_config(const char *var
, const char *value
, void *cb
)
476 const char *slot_name
;
478 if (!strcmp(var
, "format.pretty"))
479 return git_config_string(&fmt_pretty
, var
, value
);
480 if (!strcmp(var
, "format.subjectprefix"))
481 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
482 if (!strcmp(var
, "format.filenamemaxlength")) {
483 fmt_patch_name_max
= git_config_int(var
, value
);
486 if (!strcmp(var
, "format.encodeemailheaders")) {
487 default_encode_email_headers
= git_config_bool(var
, value
);
490 if (!strcmp(var
, "log.abbrevcommit")) {
491 default_abbrev_commit
= git_config_bool(var
, value
);
494 if (!strcmp(var
, "log.date"))
495 return git_config_string(&default_date_mode
, var
, value
);
496 if (!strcmp(var
, "log.decorate")) {
497 decoration_style
= parse_decoration_style(value
);
498 if (decoration_style
< 0)
499 decoration_style
= 0; /* maybe warn? */
502 if (!strcmp(var
, "log.diffmerges"))
503 return diff_merges_config(value
);
504 if (!strcmp(var
, "log.showroot")) {
505 default_show_root
= git_config_bool(var
, value
);
508 if (!strcmp(var
, "log.follow")) {
509 default_follow
= git_config_bool(var
, value
);
512 if (skip_prefix(var
, "color.decorate.", &slot_name
))
513 return parse_decorate_color_config(var
, slot_name
, value
);
514 if (!strcmp(var
, "log.mailmap")) {
515 use_mailmap_config
= git_config_bool(var
, value
);
518 if (!strcmp(var
, "log.showsignature")) {
519 default_show_signature
= git_config_bool(var
, value
);
523 if (grep_config(var
, value
, cb
) < 0)
525 if (git_gpg_config(var
, value
, cb
) < 0)
527 return git_diff_ui_config(var
, value
, cb
);
530 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
533 struct setup_revision_opt opt
;
536 git_config(git_log_config
, NULL
);
538 repo_init_revisions(the_repository
, &rev
, prefix
);
540 rev
.simplify_history
= 0;
541 memset(&opt
, 0, sizeof(opt
));
543 opt
.revarg_opt
= REVARG_COMMITTISH
;
544 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
545 if (!rev
.diffopt
.output_format
)
546 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
547 return cmd_log_walk(&rev
);
550 static void show_tagger(const char *buf
, struct rev_info
*rev
)
552 struct strbuf out
= STRBUF_INIT
;
553 struct pretty_print_context pp
= {0};
555 pp
.fmt
= rev
->commit_format
;
556 pp
.date_mode
= rev
->date_mode
;
557 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
558 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
559 strbuf_release(&out
);
562 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
564 struct object_id oidc
;
565 struct object_context obj_context
;
569 fflush(rev
->diffopt
.file
);
570 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
571 !rev
->diffopt
.flags
.allow_textconv
)
572 return stream_blob_to_fd(1, oid
, NULL
, 0);
574 if (get_oid_with_context(the_repository
, obj_name
,
576 &oidc
, &obj_context
))
577 die(_("not a valid object name %s"), obj_name
);
578 if (!obj_context
.path
||
579 !textconv_object(the_repository
, obj_context
.path
,
580 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
581 free(obj_context
.path
);
582 return stream_blob_to_fd(1, oid
, NULL
, 0);
586 die(_("git show %s: bad file"), obj_name
);
588 write_or_die(1, buf
, size
);
589 free(obj_context
.path
);
593 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
596 enum object_type type
;
597 char *buf
= read_object_file(oid
, &type
, &size
);
601 return error(_("could not read object %s"), oid_to_hex(oid
));
603 assert(type
== OBJ_TAG
);
604 while (offset
< size
&& buf
[offset
] != '\n') {
605 int new_offset
= offset
+ 1;
607 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
609 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
610 show_tagger(ident
, rev
);
615 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
620 static int show_tree_object(const struct object_id
*oid
,
622 const char *pathname
, unsigned mode
, void *context
)
624 FILE *file
= context
;
625 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
629 static void show_setup_revisions_tweak(struct rev_info
*rev
,
630 struct setup_revision_opt
*opt
)
632 if (rev
->first_parent_only
)
633 diff_merges_default_to_first_parent(rev
);
635 diff_merges_default_to_dense_combined(rev
);
636 if (!rev
->diffopt
.output_format
)
637 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
640 int cmd_show(int argc
, const char **argv
, const char *prefix
)
643 struct object_array_entry
*objects
;
644 struct setup_revision_opt opt
;
645 struct pathspec match_all
;
646 int i
, count
, ret
= 0;
649 git_config(git_log_config
, NULL
);
651 memset(&match_all
, 0, sizeof(match_all
));
652 repo_init_revisions(the_repository
, &rev
, prefix
);
654 rev
.always_show_header
= 1;
656 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
658 memset(&opt
, 0, sizeof(opt
));
660 opt
.tweak
= show_setup_revisions_tweak
;
661 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
664 return cmd_log_walk(&rev
);
666 count
= rev
.pending
.nr
;
667 objects
= rev
.pending
.objects
;
668 for (i
= 0; i
< count
&& !ret
; i
++) {
669 struct object
*o
= objects
[i
].item
;
670 const char *name
= objects
[i
].name
;
673 ret
= show_blob_object(&o
->oid
, &rev
, name
);
676 struct tag
*t
= (struct tag
*)o
;
677 struct object_id
*oid
= get_tagged_oid(t
);
681 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
682 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
684 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
685 ret
= show_tag_object(&o
->oid
, &rev
);
689 o
= parse_object(the_repository
, oid
);
691 ret
= error(_("could not read object %s"),
700 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
701 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
703 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
704 read_tree(the_repository
, (struct tree
*)o
,
705 &match_all
, show_tree_object
,
710 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
711 rev
.pending
.objects
= NULL
;
712 add_object_array(o
, name
, &rev
.pending
);
713 ret
= cmd_log_walk(&rev
);
716 ret
= error(_("unknown type: %d"), o
->type
);
724 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
726 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
729 struct setup_revision_opt opt
;
732 git_config(git_log_config
, NULL
);
734 repo_init_revisions(the_repository
, &rev
, prefix
);
735 init_reflog_walk(&rev
.reflog_info
);
736 rev
.verbose_header
= 1;
737 memset(&opt
, 0, sizeof(opt
));
739 cmd_log_init_defaults(&rev
);
740 rev
.abbrev_commit
= 1;
741 rev
.commit_format
= CMIT_FMT_ONELINE
;
742 rev
.use_terminator
= 1;
743 rev
.always_show_header
= 1;
744 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
746 return cmd_log_walk(&rev
);
749 static void log_setup_revisions_tweak(struct rev_info
*rev
,
750 struct setup_revision_opt
*opt
)
752 if (rev
->diffopt
.flags
.default_follow_renames
&&
753 rev
->prune_data
.nr
== 1)
754 rev
->diffopt
.flags
.follow_renames
= 1;
756 if (rev
->first_parent_only
)
757 diff_merges_default_to_first_parent(rev
);
760 int cmd_log(int argc
, const char **argv
, const char *prefix
)
763 struct setup_revision_opt opt
;
766 git_config(git_log_config
, NULL
);
768 repo_init_revisions(the_repository
, &rev
, prefix
);
769 rev
.always_show_header
= 1;
770 memset(&opt
, 0, sizeof(opt
));
772 opt
.revarg_opt
= REVARG_COMMITTISH
;
773 opt
.tweak
= log_setup_revisions_tweak
;
774 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
775 return cmd_log_walk(&rev
);
780 static const char *fmt_patch_suffix
= ".patch";
781 static int numbered
= 0;
782 static int auto_number
= 1;
784 static char *default_attach
= NULL
;
786 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
787 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
788 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
790 static void add_header(const char *value
)
792 struct string_list_item
*item
;
793 int len
= strlen(value
);
794 while (len
&& value
[len
- 1] == '\n')
797 if (!strncasecmp(value
, "to: ", 4)) {
798 item
= string_list_append(&extra_to
, value
+ 4);
800 } else if (!strncasecmp(value
, "cc: ", 4)) {
801 item
= string_list_append(&extra_cc
, value
+ 4);
804 item
= string_list_append(&extra_hdr
, value
);
807 item
->string
[len
] = '\0';
823 enum cover_from_description
{
830 enum auto_base_setting
{
836 static enum thread_level thread
;
837 static int do_signoff
;
838 static enum auto_base_setting auto_base
;
840 static const char *signature
= git_version_string
;
841 static const char *signature_file
;
842 static enum cover_setting config_cover_letter
;
843 static const char *config_output_directory
;
844 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
845 static int show_notes
;
846 static struct display_notes_opt notes_opt
;
848 static enum cover_from_description
parse_cover_from_description(const char *arg
)
850 if (!arg
|| !strcmp(arg
, "default"))
851 return COVER_FROM_MESSAGE
;
852 else if (!strcmp(arg
, "none"))
853 return COVER_FROM_NONE
;
854 else if (!strcmp(arg
, "message"))
855 return COVER_FROM_MESSAGE
;
856 else if (!strcmp(arg
, "subject"))
857 return COVER_FROM_SUBJECT
;
858 else if (!strcmp(arg
, "auto"))
859 return COVER_FROM_AUTO
;
861 die(_("%s: invalid cover from description mode"), arg
);
864 static int git_format_config(const char *var
, const char *value
, void *cb
)
866 if (!strcmp(var
, "format.headers")) {
868 die(_("format.headers without value"));
872 if (!strcmp(var
, "format.suffix"))
873 return git_config_string(&fmt_patch_suffix
, var
, value
);
874 if (!strcmp(var
, "format.to")) {
876 return config_error_nonbool(var
);
877 string_list_append(&extra_to
, value
);
880 if (!strcmp(var
, "format.cc")) {
882 return config_error_nonbool(var
);
883 string_list_append(&extra_cc
, value
);
886 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
887 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
890 if (!strcmp(var
, "format.numbered")) {
891 if (value
&& !strcasecmp(value
, "auto")) {
895 numbered
= git_config_bool(var
, value
);
896 auto_number
= auto_number
&& numbered
;
899 if (!strcmp(var
, "format.attach")) {
901 default_attach
= xstrdup(value
);
903 default_attach
= xstrdup(git_version_string
);
906 if (!strcmp(var
, "format.thread")) {
907 if (value
&& !strcasecmp(value
, "deep")) {
908 thread
= THREAD_DEEP
;
911 if (value
&& !strcasecmp(value
, "shallow")) {
912 thread
= THREAD_SHALLOW
;
915 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
918 if (!strcmp(var
, "format.signoff")) {
919 do_signoff
= git_config_bool(var
, value
);
922 if (!strcmp(var
, "format.signature"))
923 return git_config_string(&signature
, var
, value
);
924 if (!strcmp(var
, "format.signaturefile"))
925 return git_config_pathname(&signature_file
, var
, value
);
926 if (!strcmp(var
, "format.coverletter")) {
927 if (value
&& !strcasecmp(value
, "auto")) {
928 config_cover_letter
= COVER_AUTO
;
931 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
934 if (!strcmp(var
, "format.outputdirectory"))
935 return git_config_string(&config_output_directory
, var
, value
);
936 if (!strcmp(var
, "format.useautobase")) {
937 if (value
&& !strcasecmp(value
, "whenAble")) {
938 auto_base
= AUTO_BASE_WHEN_ABLE
;
941 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
944 if (!strcmp(var
, "format.from")) {
945 int b
= git_parse_maybe_bool(value
);
948 from
= xstrdup(value
);
950 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
955 if (!strcmp(var
, "format.notes")) {
956 int b
= git_parse_maybe_bool(value
);
958 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
960 enable_default_display_notes(¬es_opt
, &show_notes
);
962 disable_display_notes(¬es_opt
, &show_notes
);
965 if (!strcmp(var
, "format.coverfromdescription")) {
966 cover_from_description_mode
= parse_cover_from_description(value
);
970 return git_log_config(var
, value
, cb
);
973 static const char *output_directory
= NULL
;
974 static int outdir_offset
;
976 static int open_next_file(struct commit
*commit
, const char *subject
,
977 struct rev_info
*rev
, int quiet
)
979 struct strbuf filename
= STRBUF_INIT
;
981 if (output_directory
) {
982 strbuf_addstr(&filename
, output_directory
);
983 strbuf_complete(&filename
, '/');
986 if (rev
->numbered_files
)
987 strbuf_addf(&filename
, "%d", rev
->nr
);
989 fmt_output_commit(&filename
, commit
, rev
);
991 fmt_output_subject(&filename
, subject
, rev
);
994 printf("%s\n", filename
.buf
+ outdir_offset
);
996 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
997 error_errno(_("cannot open patch file %s"), filename
.buf
);
998 strbuf_release(&filename
);
1002 strbuf_release(&filename
);
1006 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1008 struct rev_info check_rev
;
1009 struct commit
*commit
, *c1
, *c2
;
1010 struct object
*o1
, *o2
;
1011 unsigned flags1
, flags2
;
1013 if (rev
->pending
.nr
!= 2)
1014 die(_("need exactly one range"));
1016 o1
= rev
->pending
.objects
[0].item
;
1017 o2
= rev
->pending
.objects
[1].item
;
1020 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1021 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1023 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1024 die(_("not a range"));
1026 init_patch_ids(the_repository
, ids
);
1028 /* given a range a..b get all patch ids for b..a */
1029 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1030 check_rev
.max_parents
= 1;
1031 o1
->flags
^= UNINTERESTING
;
1032 o2
->flags
^= UNINTERESTING
;
1033 add_pending_object(&check_rev
, o1
, "o1");
1034 add_pending_object(&check_rev
, o2
, "o2");
1035 if (prepare_revision_walk(&check_rev
))
1036 die(_("revision walk setup failed"));
1038 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1039 add_commit_patch_id(commit
, ids
);
1042 /* reset for next revision walk */
1043 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1044 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1049 static void gen_message_id(struct rev_info
*info
, char *base
)
1051 struct strbuf buf
= STRBUF_INIT
;
1052 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1053 (timestamp_t
) time(NULL
),
1054 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1055 info
->message_id
= strbuf_detach(&buf
, NULL
);
1058 static void print_signature(FILE *file
)
1060 if (!signature
|| !*signature
)
1063 fprintf(file
, "-- \n%s", signature
);
1064 if (signature
[strlen(signature
)-1] != '\n')
1069 static char *find_branch_name(struct rev_info
*rev
)
1071 int i
, positive
= -1;
1072 struct object_id branch_oid
;
1073 const struct object_id
*tip_oid
;
1074 const char *ref
, *v
;
1075 char *full_ref
, *branch
= NULL
;
1077 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1078 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1087 ref
= rev
->cmdline
.rev
[positive
].name
;
1088 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1089 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1090 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1091 oideq(tip_oid
, &branch_oid
))
1092 branch
= xstrdup(v
);
1097 static void show_diffstat(struct rev_info
*rev
,
1098 struct commit
*origin
, struct commit
*head
)
1100 struct diff_options opts
;
1102 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1103 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1104 diff_setup_done(&opts
);
1106 diff_tree_oid(get_commit_tree_oid(origin
),
1107 get_commit_tree_oid(head
),
1109 diffcore_std(&opts
);
1112 fprintf(rev
->diffopt
.file
, "\n");
1115 static void prepare_cover_text(struct pretty_print_context
*pp
,
1116 const char *branch_name
,
1118 const char *encoding
,
1121 const char *subject
= "*** SUBJECT HERE ***";
1122 const char *body
= "*** BLURB HERE ***";
1123 struct strbuf description_sb
= STRBUF_INIT
;
1124 struct strbuf subject_sb
= STRBUF_INIT
;
1126 if (cover_from_description_mode
== COVER_FROM_NONE
)
1129 if (branch_name
&& *branch_name
)
1130 read_branch_desc(&description_sb
, branch_name
);
1131 if (!description_sb
.len
)
1134 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1135 cover_from_description_mode
== COVER_FROM_AUTO
)
1136 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1138 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1139 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1140 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1141 body
= description_sb
.buf
;
1143 subject
= subject_sb
.buf
;
1146 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1147 pp_remainder(pp
, &body
, sb
, 0);
1149 strbuf_release(&description_sb
);
1150 strbuf_release(&subject_sb
);
1153 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1155 strvec_pushf(arg
, "--notes=%s", item
->string
);
1159 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1161 if (!rev
->show_notes
) {
1162 strvec_push(arg
, "--no-notes");
1163 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1164 (rev
->notes_opt
.use_default_notes
== -1 &&
1165 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1166 strvec_push(arg
, "--notes");
1168 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1172 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1173 struct commit
*origin
,
1174 int nr
, struct commit
**list
,
1175 const char *branch_name
,
1178 const char *committer
;
1179 struct shortlog log
;
1180 struct strbuf sb
= STRBUF_INIT
;
1182 const char *encoding
= "UTF-8";
1183 int need_8bit_cte
= 0;
1184 struct pretty_print_context pp
= {0};
1185 struct commit
*head
= list
[0];
1187 if (!cmit_fmt_is_mail(rev
->commit_format
))
1188 die(_("cover letter needs email format"));
1190 committer
= git_committer_info(0);
1192 if (use_separate_file
&&
1193 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1194 die(_("failed to create cover-letter file"));
1196 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1198 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1199 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1200 if (has_non_ascii(buf
))
1202 unuse_commit_buffer(list
[i
], buf
);
1206 branch_name
= find_branch_name(rev
);
1208 pp
.fmt
= CMIT_FMT_EMAIL
;
1209 pp
.date_mode
.type
= DATE_RFC2822
;
1211 pp
.print_email_subject
= 1;
1212 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1213 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1214 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1216 strbuf_release(&sb
);
1218 shortlog_init(&log
);
1220 log
.wrap
= MAIL_DEFAULT_WRAP
;
1223 log
.file
= rev
->diffopt
.file
;
1224 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1225 for (i
= 0; i
< nr
; i
++)
1226 shortlog_add_commit(&log
, list
[i
]);
1228 shortlog_output(&log
);
1230 /* We can only do diffstat with a unique reference point */
1232 show_diffstat(rev
, origin
, head
);
1234 if (rev
->idiff_oid1
) {
1235 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1236 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1242 * Pass minimum required diff-options to range-diff; others
1243 * can be added later if deemed desirable.
1245 struct diff_options opts
;
1246 struct strvec other_arg
= STRVEC_INIT
;
1247 struct range_diff_options range_diff_opts
= {
1248 .creation_factor
= rev
->creation_factor
,
1251 .other_arg
= &other_arg
1255 opts
.file
= rev
->diffopt
.file
;
1256 opts
.use_color
= rev
->diffopt
.use_color
;
1257 diff_setup_done(&opts
);
1258 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1259 get_notes_args(&other_arg
, rev
);
1260 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1261 strvec_clear(&other_arg
);
1265 static const char *clean_message_id(const char *msg_id
)
1268 const char *a
, *z
, *m
;
1271 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1276 if (!isspace(ch
) && (ch
!= '>'))
1281 die(_("insane in-reply-to: %s"), msg_id
);
1284 return xmemdupz(a
, z
- a
);
1287 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1289 if (output_directory
&& is_absolute_path(output_directory
))
1290 return output_directory
;
1292 if (!prefix
|| !*prefix
) {
1293 if (output_directory
)
1294 return output_directory
;
1295 /* The user did not explicitly ask for "./" */
1300 outdir_offset
= strlen(prefix
);
1301 if (!output_directory
)
1304 return prefix_filename(prefix
, output_directory
);
1307 static const char * const builtin_format_patch_usage
[] = {
1308 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1312 static int keep_subject
= 0;
1314 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1316 BUG_ON_OPT_NEG(unset
);
1317 BUG_ON_OPT_ARG(arg
);
1318 ((struct rev_info
*)opt
->value
)->total
= -1;
1323 static int subject_prefix
= 0;
1325 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1328 BUG_ON_OPT_NEG(unset
);
1330 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1334 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1336 BUG_ON_OPT_NEG(unset
);
1337 BUG_ON_OPT_ARG(arg
);
1338 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1341 static int numbered_cmdline_opt
= 0;
1343 static int numbered_callback(const struct option
*opt
, const char *arg
,
1346 BUG_ON_OPT_ARG(arg
);
1347 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1353 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1356 BUG_ON_OPT_NEG(unset
);
1357 return numbered_callback(opt
, arg
, 1);
1360 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1363 const char **dir
= (const char **)opt
->value
;
1364 BUG_ON_OPT_NEG(unset
);
1366 die(_("two output directories?"));
1371 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1373 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1375 *thread
= THREAD_UNSET
;
1376 else if (!arg
|| !strcmp(arg
, "shallow"))
1377 *thread
= THREAD_SHALLOW
;
1378 else if (!strcmp(arg
, "deep"))
1379 *thread
= THREAD_DEEP
;
1381 * Please update _git_formatpatch() in git-completion.bash
1382 * when you add new options.
1389 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1391 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1393 rev
->mime_boundary
= NULL
;
1395 rev
->mime_boundary
= arg
;
1397 rev
->mime_boundary
= git_version_string
;
1398 rev
->no_inline
= unset
? 0 : 1;
1402 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1404 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1406 rev
->mime_boundary
= NULL
;
1408 rev
->mime_boundary
= arg
;
1410 rev
->mime_boundary
= git_version_string
;
1415 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1418 string_list_clear(&extra_hdr
, 0);
1419 string_list_clear(&extra_to
, 0);
1420 string_list_clear(&extra_cc
, 0);
1427 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1430 string_list_clear(&extra_to
, 0);
1432 string_list_append(&extra_to
, arg
);
1436 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1439 string_list_clear(&extra_cc
, 0);
1441 string_list_append(&extra_cc
, arg
);
1445 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1447 char **from
= opt
->value
;
1454 *from
= xstrdup(arg
);
1456 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1460 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1462 const char **base_commit
= opt
->value
;
1465 auto_base
= AUTO_BASE_NEVER
;
1466 *base_commit
= NULL
;
1467 } else if (!strcmp(arg
, "auto")) {
1468 auto_base
= AUTO_BASE_ALWAYS
;
1469 *base_commit
= NULL
;
1471 auto_base
= AUTO_BASE_NEVER
;
1477 struct base_tree_info
{
1478 struct object_id base_commit
;
1479 int nr_patch_id
, alloc_patch_id
;
1480 struct object_id
*patch_id
;
1483 static struct commit
*get_base_commit(const char *base_commit
,
1484 struct commit
**list
,
1487 struct commit
*base
= NULL
;
1488 struct commit
**rev
;
1489 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1491 switch (auto_base
) {
1492 case AUTO_BASE_NEVER
:
1497 /* no base information is requested */
1501 case AUTO_BASE_ALWAYS
:
1502 case AUTO_BASE_WHEN_ABLE
:
1504 BUG("requested automatic base selection but a commit was provided");
1507 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1511 BUG("unexpected automatic base selection method");
1515 base
= lookup_commit_reference_by_name(base_commit
);
1517 die(_("unknown commit %s"), base_commit
);
1519 struct branch
*curr_branch
= branch_get(NULL
);
1520 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1522 struct commit_list
*base_list
;
1523 struct commit
*commit
;
1524 struct object_id oid
;
1526 if (get_oid(upstream
, &oid
)) {
1528 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1532 commit
= lookup_commit_or_die(&oid
, "upstream base");
1533 base_list
= get_merge_bases_many(commit
, total
, list
);
1534 /* There should be one and only one merge base. */
1535 if (!base_list
|| base_list
->next
) {
1536 if (die_on_failure
) {
1537 die(_("could not find exact merge base"));
1539 free_commit_list(base_list
);
1543 base
= base_list
->item
;
1544 free_commit_list(base_list
);
1547 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1548 "please use git branch --set-upstream-to to track a remote branch.\n"
1549 "Or you could specify base commit by --base=<base-commit-id> manually"));
1555 ALLOC_ARRAY(rev
, total
);
1556 for (i
= 0; i
< total
; i
++)
1561 * Get merge base through pair-wise computations
1562 * and store it in rev[0].
1564 while (rev_nr
> 1) {
1565 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1566 struct commit_list
*merge_base
;
1567 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1568 if (!merge_base
|| merge_base
->next
) {
1569 if (die_on_failure
) {
1570 die(_("failed to find exact merge base"));
1577 rev
[i
] = merge_base
->item
;
1581 rev
[i
] = rev
[2 * i
];
1582 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1585 if (!in_merge_bases(base
, rev
[0])) {
1586 if (die_on_failure
) {
1587 die(_("base commit should be the ancestor of revision list"));
1594 for (i
= 0; i
< total
; i
++) {
1595 if (base
== list
[i
]) {
1596 if (die_on_failure
) {
1597 die(_("base commit shouldn't be in revision list"));
1609 define_commit_slab(commit_base
, int);
1611 static void prepare_bases(struct base_tree_info
*bases
,
1612 struct commit
*base
,
1613 struct commit
**list
,
1616 struct commit
*commit
;
1617 struct rev_info revs
;
1618 struct diff_options diffopt
;
1619 struct commit_base commit_base
;
1625 init_commit_base(&commit_base
);
1626 repo_diff_setup(the_repository
, &diffopt
);
1627 diffopt
.flags
.recursive
= 1;
1628 diff_setup_done(&diffopt
);
1630 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1632 repo_init_revisions(the_repository
, &revs
, NULL
);
1633 revs
.max_parents
= 1;
1634 revs
.topo_order
= 1;
1635 for (i
= 0; i
< total
; i
++) {
1636 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1637 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1638 *commit_base_at(&commit_base
, list
[i
]) = 1;
1640 base
->object
.flags
|= UNINTERESTING
;
1641 add_pending_object(&revs
, &base
->object
, "base");
1643 if (prepare_revision_walk(&revs
))
1644 die(_("revision walk setup failed"));
1646 * Traverse the commits list, get prerequisite patch ids
1647 * and stuff them in bases structure.
1649 while ((commit
= get_revision(&revs
)) != NULL
) {
1650 struct object_id oid
;
1651 struct object_id
*patch_id
;
1652 if (*commit_base_at(&commit_base
, commit
))
1654 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1655 die(_("cannot get patch id"));
1656 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1657 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1658 oidcpy(patch_id
, &oid
);
1659 bases
->nr_patch_id
++;
1661 clear_commit_base(&commit_base
);
1664 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1668 /* Only do this once, either for the cover or for the first one */
1669 if (is_null_oid(&bases
->base_commit
))
1672 /* Show the base commit */
1673 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1675 /* Show the prerequisite patches */
1676 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1677 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1679 free(bases
->patch_id
);
1680 bases
->nr_patch_id
= 0;
1681 bases
->alloc_patch_id
= 0;
1682 oidclr(&bases
->base_commit
);
1685 static const char *diff_title(struct strbuf
*sb
,
1686 const char *reroll_count
,
1687 const char *generic
,
1688 const char *rerolled
)
1692 /* RFC may be v0, so allow -v1 to diff against v0 */
1693 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1695 strbuf_addf(sb
, rerolled
, v
- 1);
1697 strbuf_addstr(sb
, generic
);
1701 static void infer_range_diff_ranges(struct strbuf
*r1
,
1704 struct commit
*origin
,
1705 struct commit
*head
)
1707 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1708 int prev_is_range
= is_range_diff_range(prev
);
1711 strbuf_addstr(r1
, prev
);
1713 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1716 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1717 else if (prev_is_range
)
1718 die(_("failed to infer range-diff origin of current series"));
1720 warning(_("using '%s' as range-diff origin of current series"), prev
);
1721 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1725 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1727 struct commit
*commit
;
1728 struct commit
**list
= NULL
;
1729 struct rev_info rev
;
1730 struct setup_revision_opt s_r_opt
;
1731 int nr
= 0, total
, i
;
1733 int start_number
= -1;
1734 int just_numbers
= 0;
1735 int ignore_if_in_upstream
= 0;
1736 int cover_letter
= -1;
1737 int boundary_count
= 0;
1738 int no_binary_diff
= 0;
1739 int zero_commit
= 0;
1740 struct commit
*origin
= NULL
;
1741 const char *in_reply_to
= NULL
;
1742 struct patch_ids ids
;
1743 struct strbuf buf
= STRBUF_INIT
;
1744 int use_patch_format
= 0;
1746 const char *reroll_count
= NULL
;
1747 char *cover_from_description_arg
= NULL
;
1748 char *branch_name
= NULL
;
1749 char *base_commit
= NULL
;
1750 struct base_tree_info bases
;
1751 struct commit
*base
;
1752 int show_progress
= 0;
1753 struct progress
*progress
= NULL
;
1754 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1755 struct strbuf idiff_title
= STRBUF_INIT
;
1756 const char *rdiff_prev
= NULL
;
1757 struct strbuf rdiff1
= STRBUF_INIT
;
1758 struct strbuf rdiff2
= STRBUF_INIT
;
1759 struct strbuf rdiff_title
= STRBUF_INIT
;
1760 int creation_factor
= -1;
1762 const struct option builtin_format_patch_options
[] = {
1763 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1764 N_("use [PATCH n/m] even with a single patch"),
1765 PARSE_OPT_NOARG
, numbered_callback
),
1766 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1767 N_("use [PATCH] even with multiple patches"),
1768 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1769 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1770 OPT_BOOL(0, "stdout", &use_stdout
,
1771 N_("print patches to standard out")),
1772 OPT_BOOL(0, "cover-letter", &cover_letter
,
1773 N_("generate a cover letter")),
1774 OPT_BOOL(0, "numbered-files", &just_numbers
,
1775 N_("use simple number sequence for output file names")),
1776 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1777 N_("use <sfx> instead of '.patch'")),
1778 OPT_INTEGER(0, "start-number", &start_number
,
1779 N_("start numbering patches at <n> instead of 1")),
1780 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1781 N_("mark the series as Nth re-roll")),
1782 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1783 N_("max length of output filename")),
1784 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1785 N_("use [RFC PATCH] instead of [PATCH]"),
1786 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1787 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1788 N_("cover-from-description-mode"),
1789 N_("generate parts of a cover letter based on a branch's description")),
1790 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1791 N_("use [<prefix>] instead of [PATCH]"),
1792 PARSE_OPT_NONEG
, subject_prefix_callback
),
1793 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1794 N_("dir"), N_("store resulting files in <dir>"),
1795 PARSE_OPT_NONEG
, output_directory_callback
),
1796 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1797 N_("don't strip/add [PATCH]"),
1798 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1799 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1800 N_("don't output binary diffs")),
1801 OPT_BOOL(0, "zero-commit", &zero_commit
,
1802 N_("output all-zero hash in From header")),
1803 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1804 N_("don't include a patch matching a commit upstream")),
1805 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1806 N_("show patch format instead of default (patch + stat)"),
1807 1, PARSE_OPT_NONEG
),
1808 OPT_GROUP(N_("Messaging")),
1809 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1810 N_("add email header"), header_callback
),
1811 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1812 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1813 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1814 N_("set From address to <ident> (or committer ident if absent)"),
1815 PARSE_OPT_OPTARG
, from_callback
),
1816 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1817 N_("make first mail a reply to <message-id>")),
1818 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1819 N_("attach the patch"), PARSE_OPT_OPTARG
,
1821 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1822 N_("inline the patch"),
1823 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1825 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1826 N_("enable message threading, styles: shallow, deep"),
1827 PARSE_OPT_OPTARG
, thread_callback
),
1828 OPT_STRING(0, "signature", &signature
, N_("signature"),
1829 N_("add a signature")),
1830 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1831 N_("add prerequisite tree info to the patch series"),
1833 OPT_FILENAME(0, "signature-file", &signature_file
,
1834 N_("add a signature from a file")),
1835 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1836 OPT_BOOL(0, "progress", &show_progress
,
1837 N_("show progress while generating patches")),
1838 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1839 N_("show changes against <rev> in cover letter or single patch"),
1840 parse_opt_object_name
),
1841 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1842 N_("show changes against <refspec> in cover letter or single patch")),
1843 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1844 N_("percentage by which creation is weighted")),
1848 extra_hdr
.strdup_strings
= 1;
1849 extra_to
.strdup_strings
= 1;
1850 extra_cc
.strdup_strings
= 1;
1851 init_log_defaults();
1852 init_display_notes(¬es_opt
);
1853 git_config(git_format_config
, NULL
);
1854 repo_init_revisions(the_repository
, &rev
, prefix
);
1855 rev
.show_notes
= show_notes
;
1856 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1857 rev
.commit_format
= CMIT_FMT_EMAIL
;
1858 rev
.encode_email_headers
= default_encode_email_headers
;
1859 rev
.expand_tabs_in_log_default
= 0;
1860 rev
.verbose_header
= 1;
1862 rev
.max_parents
= 1;
1863 rev
.diffopt
.flags
.recursive
= 1;
1864 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1865 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1866 s_r_opt
.def
= "HEAD";
1867 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1869 if (default_attach
) {
1870 rev
.mime_boundary
= default_attach
;
1875 * Parse the arguments before setup_revisions(), or something
1876 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1877 * possibly a valid SHA1.
1879 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1880 builtin_format_patch_usage
,
1881 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1882 PARSE_OPT_KEEP_DASHDASH
);
1884 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1885 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
1886 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
1888 if (cover_from_description_arg
)
1889 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
1892 struct strbuf sprefix
= STRBUF_INIT
;
1894 strbuf_addf(&sprefix
, "%s v%s",
1895 rev
.subject_prefix
, reroll_count
);
1896 rev
.reroll_count
= reroll_count
;
1897 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1900 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1901 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1902 strbuf_addch(&buf
, '\n');
1906 strbuf_addstr(&buf
, "To: ");
1907 for (i
= 0; i
< extra_to
.nr
; i
++) {
1909 strbuf_addstr(&buf
, " ");
1910 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1911 if (i
+ 1 < extra_to
.nr
)
1912 strbuf_addch(&buf
, ',');
1913 strbuf_addch(&buf
, '\n');
1917 strbuf_addstr(&buf
, "Cc: ");
1918 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1920 strbuf_addstr(&buf
, " ");
1921 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1922 if (i
+ 1 < extra_cc
.nr
)
1923 strbuf_addch(&buf
, ',');
1924 strbuf_addch(&buf
, '\n');
1927 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1930 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1931 die(_("invalid ident line: %s"), from
);
1934 if (start_number
< 0)
1938 * If numbered is set solely due to format.numbered in config,
1939 * and it would conflict with --keep-subject (-k) from the
1940 * command line, reset "numbered".
1942 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1945 if (numbered
&& keep_subject
)
1946 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
1947 if (keep_subject
&& subject_prefix
)
1948 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
1949 rev
.preserve_subject
= keep_subject
;
1951 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1953 die(_("unrecognized argument: %s"), argv
[1]);
1955 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1956 die(_("--name-only does not make sense"));
1957 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1958 die(_("--name-status does not make sense"));
1959 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1960 die(_("--check does not make sense"));
1962 if (!use_patch_format
&&
1963 (!rev
.diffopt
.output_format
||
1964 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1965 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1966 if (!rev
.diffopt
.stat_width
)
1967 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1969 /* Always generate a patch */
1970 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1972 rev
.zero_commit
= zero_commit
;
1973 rev
.patch_name_max
= fmt_patch_name_max
;
1975 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1976 rev
.diffopt
.flags
.binary
= 1;
1979 load_display_notes(&rev
.notes_opt
);
1981 if (use_stdout
+ rev
.diffopt
.close_file
+ !!output_directory
> 1)
1982 die(_("options '%s', '%s', and '%s' cannot be used together"), "--stdout", "--output", "--output-directory");
1986 } else if (rev
.diffopt
.close_file
) {
1988 * The diff code parsed --output; it has already opened the
1989 * file, but we must instruct it not to close after each diff.
1991 rev
.diffopt
.no_free
= 1;
1995 if (!output_directory
)
1996 output_directory
= config_output_directory
;
1997 output_directory
= set_outdir(prefix
, output_directory
);
1999 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2000 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2002 * We consider <outdir> as 'outside of gitdir', therefore avoid
2003 * applying adjust_shared_perm in s-c-l-d.
2005 saved
= get_shared_repository();
2006 set_shared_repository(0);
2007 switch (safe_create_leading_directories_const(output_directory
)) {
2012 die(_("could not create leading directories "
2013 "of '%s'"), output_directory
);
2015 set_shared_repository(saved
);
2016 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2017 die_errno(_("could not create directory '%s'"),
2021 if (rev
.pending
.nr
== 1) {
2024 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2026 * This is traditional behaviour of "git format-patch
2027 * origin" that prepares what the origin side still
2030 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2031 add_head_to_pending(&rev
);
2035 * Otherwise, it is "format-patch -22 HEAD", and/or
2036 * "format-patch --root HEAD". The user wants
2037 * get_revision() to do the usual traversal.
2040 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2044 const char *ref
, *v
;
2045 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2047 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2048 branch_name
= xstrdup(v
);
2050 branch_name
= xstrdup(""); /* no branch */
2055 * We cannot move this anywhere earlier because we do want to
2056 * know if --root was given explicitly from the command line.
2058 rev
.show_root_diff
= 1;
2060 if (ignore_if_in_upstream
) {
2061 /* Don't say anything if head and upstream are the same. */
2062 if (rev
.pending
.nr
== 2) {
2063 struct object_array_entry
*o
= rev
.pending
.objects
;
2064 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2067 get_patch_ids(&rev
, &ids
);
2070 if (prepare_revision_walk(&rev
))
2071 die(_("revision walk setup failed"));
2073 while ((commit
= get_revision(&rev
)) != NULL
) {
2074 if (commit
->object
.flags
& BOUNDARY
) {
2076 origin
= (boundary_count
== 1) ? commit
: NULL
;
2080 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2084 REALLOC_ARRAY(list
, nr
);
2085 list
[nr
- 1] = commit
;
2091 if (cover_letter
== -1) {
2092 if (config_cover_letter
== COVER_AUTO
)
2093 cover_letter
= (total
> 1);
2095 cover_letter
= (config_cover_letter
== COVER_ON
);
2097 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2100 rev
.total
= total
+ start_number
- 1;
2102 if (idiff_prev
.nr
) {
2103 if (!cover_letter
&& total
!= 1)
2104 die(_("--interdiff requires --cover-letter or single patch"));
2105 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2106 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2107 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2109 _("Interdiff against v%d:"));
2112 if (creation_factor
< 0)
2113 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2114 else if (!rdiff_prev
)
2115 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2118 if (!cover_letter
&& total
!= 1)
2119 die(_("--range-diff requires --cover-letter or single patch"));
2121 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2123 rev
.rdiff1
= rdiff1
.buf
;
2124 rev
.rdiff2
= rdiff2
.buf
;
2125 rev
.creation_factor
= creation_factor
;
2126 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2128 _("Range-diff against v%d:"));
2132 ; /* --no-signature inhibits all signatures */
2133 } else if (signature
&& signature
!= git_version_string
) {
2134 ; /* non-default signature already set */
2135 } else if (signature_file
) {
2136 struct strbuf buf
= STRBUF_INIT
;
2138 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2139 die_errno(_("unable to read signature file '%s'"), signature_file
);
2140 signature
= strbuf_detach(&buf
, NULL
);
2143 memset(&bases
, 0, sizeof(bases
));
2144 base
= get_base_commit(base_commit
, list
, nr
);
2146 reset_revision_walk();
2147 clear_object_flags(UNINTERESTING
);
2148 prepare_bases(&bases
, base
, list
, nr
);
2151 if (in_reply_to
|| thread
|| cover_letter
)
2152 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
2154 const char *msgid
= clean_message_id(in_reply_to
);
2155 string_list_append(rev
.ref_message_ids
, msgid
);
2157 rev
.numbered_files
= just_numbers
;
2158 rev
.patch_suffix
= fmt_patch_suffix
;
2161 gen_message_id(&rev
, "cover");
2162 make_cover_letter(&rev
, !!output_directory
,
2163 origin
, nr
, list
, branch_name
, quiet
);
2164 print_bases(&bases
, rev
.diffopt
.file
);
2165 print_signature(rev
.diffopt
.file
);
2168 /* interdiff/range-diff in cover-letter; omit from patches */
2169 rev
.idiff_oid1
= NULL
;
2172 rev
.add_signoff
= do_signoff
;
2175 progress
= start_delayed_progress(_("Generating patches"), total
);
2178 display_progress(progress
, total
- nr
);
2180 rev
.nr
= total
- nr
+ (start_number
- 1);
2181 /* Make the second and subsequent mails replies to the first */
2183 /* Have we already had a message ID? */
2184 if (rev
.message_id
) {
2186 * For deep threading: make every mail
2187 * a reply to the previous one, no
2188 * matter what other options are set.
2190 * For shallow threading:
2192 * Without --cover-letter and
2193 * --in-reply-to, make every mail a
2194 * reply to the one before.
2196 * With --in-reply-to but no
2197 * --cover-letter, make every mail a
2198 * reply to the <reply-to>.
2200 * With --cover-letter, make every
2201 * mail but the cover letter a reply
2202 * to the cover letter. The cover
2203 * letter is a reply to the
2204 * --in-reply-to, if specified.
2206 if (thread
== THREAD_SHALLOW
2207 && rev
.ref_message_ids
->nr
> 0
2208 && (!cover_letter
|| rev
.nr
> 1))
2209 free(rev
.message_id
);
2211 string_list_append(rev
.ref_message_ids
,
2214 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2217 if (output_directory
&&
2218 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2219 die(_("failed to create output files"));
2220 shown
= log_tree_commit(&rev
, commit
);
2221 free_commit_buffer(the_repository
->parsed_objects
,
2224 /* We put one extra blank line between formatted
2225 * patches and this flag is used by log-tree code
2226 * to see if it needs to emit a LF before showing
2227 * the log; when using one file per patch, we do
2228 * not want the extra blank line.
2230 if (output_directory
)
2233 print_bases(&bases
, rev
.diffopt
.file
);
2234 if (rev
.mime_boundary
)
2235 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2236 mime_boundary_leader
,
2239 print_signature(rev
.diffopt
.file
);
2241 if (output_directory
)
2242 fclose(rev
.diffopt
.file
);
2244 stop_progress(&progress
);
2247 string_list_clear(&extra_to
, 0);
2248 string_list_clear(&extra_cc
, 0);
2249 string_list_clear(&extra_hdr
, 0);
2250 if (ignore_if_in_upstream
)
2251 free_patch_ids(&ids
);
2254 oid_array_clear(&idiff_prev
);
2255 strbuf_release(&idiff_title
);
2256 strbuf_release(&rdiff1
);
2257 strbuf_release(&rdiff2
);
2258 strbuf_release(&rdiff_title
);
2263 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2265 struct object_id oid
;
2266 if (get_oid(arg
, &oid
) == 0) {
2267 struct commit
*commit
= lookup_commit_reference(the_repository
,
2270 commit
->object
.flags
|= flags
;
2271 add_pending_object(revs
, &commit
->object
, arg
);
2278 static const char * const cherry_usage
[] = {
2279 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2283 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2284 int abbrev
, FILE *file
)
2287 fprintf(file
, "%c %s\n", sign
,
2288 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2290 struct strbuf buf
= STRBUF_INIT
;
2291 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2292 fprintf(file
, "%c %s %s\n", sign
,
2293 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2295 strbuf_release(&buf
);
2299 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2301 struct rev_info revs
;
2302 struct patch_ids ids
;
2303 struct commit
*commit
;
2304 struct commit_list
*list
= NULL
;
2305 struct branch
*current_branch
;
2306 const char *upstream
;
2307 const char *head
= "HEAD";
2308 const char *limit
= NULL
;
2309 int verbose
= 0, abbrev
= 0;
2311 struct option options
[] = {
2312 OPT__ABBREV(&abbrev
),
2313 OPT__VERBOSE(&verbose
, N_("be verbose")),
2317 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2330 current_branch
= branch_get(NULL
);
2331 upstream
= branch_get_upstream(current_branch
, NULL
);
2333 fprintf(stderr
, _("Could not find a tracked"
2334 " remote branch, please"
2335 " specify <upstream> manually.\n"));
2336 usage_with_options(cherry_usage
, options
);
2340 repo_init_revisions(the_repository
, &revs
, prefix
);
2341 revs
.max_parents
= 1;
2343 if (add_pending_commit(head
, &revs
, 0))
2344 die(_("unknown commit %s"), head
);
2345 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2346 die(_("unknown commit %s"), upstream
);
2348 /* Don't say anything if head and upstream are the same. */
2349 if (revs
.pending
.nr
== 2) {
2350 struct object_array_entry
*o
= revs
.pending
.objects
;
2351 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2355 get_patch_ids(&revs
, &ids
);
2357 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2358 die(_("unknown commit %s"), limit
);
2360 /* reverse the list of commits */
2361 if (prepare_revision_walk(&revs
))
2362 die(_("revision walk setup failed"));
2363 while ((commit
= get_revision(&revs
)) != NULL
) {
2364 commit_list_insert(commit
, &list
);
2370 commit
= list
->item
;
2371 if (has_commit_patch_id(commit
, &ids
))
2373 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2377 free_patch_ids(&ids
);