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"
38 #include "tmp-objdir.h"
40 #define MAIL_DEFAULT_WRAP 72
41 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
42 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
44 /* Set a default date-time format for git log ("log.date" config variable) */
45 static const char *default_date_mode
= NULL
;
47 static int default_abbrev_commit
;
48 static int default_show_root
= 1;
49 static int default_follow
;
50 static int default_show_signature
;
51 static int default_encode_email_headers
= 1;
52 static int decoration_style
;
53 static int decoration_given
;
54 static int use_mailmap_config
= 1;
55 static const char *fmt_patch_subject_prefix
= "PATCH";
56 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
57 static const char *fmt_pretty
;
59 static const char * const builtin_log_usage
[] = {
60 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
61 N_("git show [<options>] <object>..."),
65 struct line_opt_callback_data
{
68 struct string_list args
;
71 static int session_is_interactive(void)
73 return isatty(1) || pager_in_use();
76 static int auto_decoration_style(void)
78 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
81 static int parse_decoration_style(const char *value
)
83 switch (git_parse_maybe_bool(value
)) {
85 return DECORATE_SHORT_REFS
;
91 if (!strcmp(value
, "full"))
92 return DECORATE_FULL_REFS
;
93 else if (!strcmp(value
, "short"))
94 return DECORATE_SHORT_REFS
;
95 else if (!strcmp(value
, "auto"))
96 return auto_decoration_style();
98 * Please update _git_log() in git-completion.bash when you
99 * add new decoration styles.
104 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
107 decoration_style
= 0;
109 decoration_style
= parse_decoration_style(arg
);
111 decoration_style
= DECORATE_SHORT_REFS
;
113 if (decoration_style
< 0)
114 die(_("invalid --decorate option: %s"), arg
);
116 decoration_given
= 1;
121 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
123 struct line_opt_callback_data
*data
= option
->value
;
125 BUG_ON_OPT_NEG(unset
);
130 data
->rev
->line_level_traverse
= 1;
131 string_list_append(&data
->args
, arg
);
136 static void init_log_defaults(void)
138 init_diff_ui_defaults();
140 decoration_style
= auto_decoration_style();
143 static void cmd_log_init_defaults(struct rev_info
*rev
)
146 get_commit_format(fmt_pretty
, rev
);
148 rev
->diffopt
.flags
.default_follow_renames
= 1;
149 rev
->verbose_header
= 1;
150 rev
->diffopt
.flags
.recursive
= 1;
151 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
152 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
153 rev
->abbrev_commit
= default_abbrev_commit
;
154 rev
->show_root_diff
= default_show_root
;
155 rev
->subject_prefix
= fmt_patch_subject_prefix
;
156 rev
->patch_name_max
= fmt_patch_name_max
;
157 rev
->show_signature
= default_show_signature
;
158 rev
->encode_email_headers
= default_encode_email_headers
;
159 rev
->diffopt
.flags
.allow_textconv
= 1;
161 if (default_date_mode
)
162 parse_date_format(default_date_mode
, &rev
->date_mode
);
165 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
166 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
168 struct userformat_want w
;
169 int quiet
= 0, source
= 0, mailmap
;
170 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
171 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
172 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
173 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
174 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
175 &decorate_refs_exclude
,
176 &decorate_refs_exclude_config
};
177 static struct revision_sources revision_sources
;
179 const struct option builtin_log_options
[] = {
180 OPT__QUIET(&quiet
, N_("suppress diff output")),
181 OPT_BOOL(0, "source", &source
, N_("show source")),
182 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("use mail map file")),
183 OPT_ALIAS(0, "mailmap", "use-mailmap"),
184 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
185 N_("pattern"), N_("only decorate refs that match <pattern>")),
186 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
187 N_("pattern"), N_("do not decorate refs that match <pattern>")),
188 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
189 PARSE_OPT_OPTARG
, decorate_callback
),
190 OPT_CALLBACK('L', NULL
, &line_cb
, "range:file",
191 N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
192 log_line_range_callback
),
197 line_cb
.prefix
= prefix
;
199 mailmap
= use_mailmap_config
;
200 argc
= parse_options(argc
, argv
, prefix
,
201 builtin_log_options
, builtin_log_usage
,
202 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
203 PARSE_OPT_KEEP_DASHDASH
);
206 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
207 argc
= setup_revisions(argc
, argv
, rev
, opt
);
209 /* Any arguments at this point are not recognized */
211 die(_("unrecognized argument: %s"), argv
[1]);
213 if (rev
->line_level_traverse
&& rev
->prune_data
.nr
)
214 die(_("-L<range>:<file> cannot be used with pathspec"));
216 memset(&w
, 0, sizeof(w
));
217 userformat_find_requirements(NULL
, &w
);
219 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
222 load_display_notes(&rev
->notes_opt
);
224 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
225 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
226 rev
->always_show_header
= 0;
228 if (source
|| w
.source
) {
229 init_revision_sources(&revision_sources
);
230 rev
->sources
= &revision_sources
;
234 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
235 read_mailmap(rev
->mailmap
);
238 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
240 * "log --pretty=raw" is special; ignore UI oriented
241 * configuration variables such as decoration.
243 if (!decoration_given
)
244 decoration_style
= 0;
245 if (!rev
->abbrev_commit_given
)
246 rev
->abbrev_commit
= 0;
249 if (rev
->commit_format
== CMIT_FMT_USERFORMAT
) {
252 * Disable decoration loading if the format will not
255 decoration_style
= 0;
256 } else if (!decoration_style
) {
258 * If we are going to show them, make sure we do load
259 * them here, but taking care not to override a
260 * specific style set by config or --decorate.
262 decoration_style
= DECORATE_SHORT_REFS
;
266 if (decoration_style
|| rev
->simplify_by_decoration
) {
267 const struct string_list
*config_exclude
=
268 repo_config_get_value_multi(the_repository
,
269 "log.excludeDecoration");
271 if (config_exclude
) {
272 struct string_list_item
*item
;
273 for_each_string_list_item(item
, config_exclude
)
274 string_list_append(&decorate_refs_exclude_config
,
278 if (decoration_style
)
279 rev
->show_decorations
= 1;
281 load_ref_decorations(&decoration_filter
, decoration_style
);
284 if (rev
->line_level_traverse
)
285 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
290 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
291 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
293 cmd_log_init_defaults(rev
);
294 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
298 * This gives a rough estimate for how many commits we
299 * will print out in the list.
301 static int estimate_commit_count(struct commit_list
*list
)
306 struct commit
*commit
= list
->item
;
307 unsigned int flags
= commit
->object
.flags
;
309 if (!(flags
& (TREESAME
| UNINTERESTING
)))
315 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
317 if (rev
->shown_one
) {
319 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
320 putchar(rev
->diffopt
.line_termination
);
322 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
325 static struct itimerval early_output_timer
;
327 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
329 int i
= revs
->early_output
;
331 int no_free
= revs
->diffopt
.no_free
;
333 revs
->diffopt
.no_free
= 0;
334 sort_in_topological_order(&list
, revs
->sort_order
);
336 struct commit
*commit
= list
->item
;
337 switch (simplify_commit(revs
, commit
)) {
340 int n
= estimate_commit_count(list
);
341 show_early_header(revs
, "incomplete", n
);
344 log_tree_commit(revs
, commit
);
350 revs
->diffopt
.no_free
= no_free
;
351 diff_free(&revs
->diffopt
);
357 /* Did we already get enough commits for the early output? */
359 revs
->diffopt
.no_free
= 0;
360 diff_free(&revs
->diffopt
);
365 * ..if no, then repeat it twice a second until we
368 * NOTE! We don't use "it_interval", because if the
369 * reader isn't listening, we want our output to be
370 * throttled by the writing, and not have the timer
371 * trigger every second even if we're blocked on a
374 early_output_timer
.it_value
.tv_sec
= 0;
375 early_output_timer
.it_value
.tv_usec
= 500000;
376 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
379 static void early_output(int signal
)
381 show_early_output
= log_show_early
;
384 static void setup_early_output(void)
389 * Set up the signal handler, minimally intrusively:
390 * we only set a single volatile integer word (not
391 * using sigatomic_t - trying to avoid unnecessary
392 * system dependencies and headers), and using
395 memset(&sa
, 0, sizeof(sa
));
396 sa
.sa_handler
= early_output
;
397 sigemptyset(&sa
.sa_mask
);
398 sa
.sa_flags
= SA_RESTART
;
399 sigaction(SIGALRM
, &sa
, NULL
);
402 * If we can get the whole output in less than a
403 * tenth of a second, don't even bother doing the
404 * early-output thing..
406 * This is a one-time-only trigger.
408 early_output_timer
.it_value
.tv_sec
= 0;
409 early_output_timer
.it_value
.tv_usec
= 100000;
410 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
413 static void finish_early_output(struct rev_info
*rev
)
415 int n
= estimate_commit_count(rev
->commits
);
416 signal(SIGALRM
, SIG_IGN
);
417 show_early_header(rev
, "done", n
);
420 static int cmd_log_walk_no_free(struct rev_info
*rev
)
422 struct commit
*commit
;
426 if (rev
->remerge_diff
) {
427 rev
->remerge_objdir
= tmp_objdir_create("remerge-diff");
428 if (!rev
->remerge_objdir
)
429 die(_("unable to create temporary object directory"));
430 tmp_objdir_replace_primary_odb(rev
->remerge_objdir
, 1);
433 if (rev
->early_output
)
434 setup_early_output();
436 if (prepare_revision_walk(rev
))
437 die(_("revision walk setup failed"));
439 if (rev
->early_output
)
440 finish_early_output(rev
);
443 * For --check and --exit-code, the exit code is based on CHECK_FAILED
444 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
445 * retain that state information if replacing rev->diffopt in this loop
447 while ((commit
= get_revision(rev
)) != NULL
) {
448 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
450 * We decremented max_count in get_revision,
451 * but we didn't actually show the commit.
454 if (!rev
->reflog_info
) {
456 * We may show a given commit multiple times when
457 * walking the reflogs.
459 free_commit_buffer(the_repository
->parsed_objects
,
461 free_commit_list(commit
->parents
);
462 commit
->parents
= NULL
;
464 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
465 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
466 if (rev
->diffopt
.degraded_cc_to_c
)
469 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
470 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
472 if (rev
->remerge_diff
) {
473 tmp_objdir_destroy(rev
->remerge_objdir
);
474 rev
->remerge_objdir
= NULL
;
477 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
478 rev
->diffopt
.flags
.check_failed
) {
481 return diff_result_code(&rev
->diffopt
, 0);
484 static int cmd_log_walk(struct rev_info
*rev
)
488 rev
->diffopt
.no_free
= 1;
489 retval
= cmd_log_walk_no_free(rev
);
490 rev
->diffopt
.no_free
= 0;
491 diff_free(&rev
->diffopt
);
495 static int git_log_config(const char *var
, const char *value
, void *cb
)
497 const char *slot_name
;
499 if (!strcmp(var
, "format.pretty"))
500 return git_config_string(&fmt_pretty
, var
, value
);
501 if (!strcmp(var
, "format.subjectprefix"))
502 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
503 if (!strcmp(var
, "format.filenamemaxlength")) {
504 fmt_patch_name_max
= git_config_int(var
, value
);
507 if (!strcmp(var
, "format.encodeemailheaders")) {
508 default_encode_email_headers
= git_config_bool(var
, value
);
511 if (!strcmp(var
, "log.abbrevcommit")) {
512 default_abbrev_commit
= git_config_bool(var
, value
);
515 if (!strcmp(var
, "log.date"))
516 return git_config_string(&default_date_mode
, var
, value
);
517 if (!strcmp(var
, "log.decorate")) {
518 decoration_style
= parse_decoration_style(value
);
519 if (decoration_style
< 0)
520 decoration_style
= 0; /* maybe warn? */
523 if (!strcmp(var
, "log.diffmerges"))
524 return diff_merges_config(value
);
525 if (!strcmp(var
, "log.showroot")) {
526 default_show_root
= git_config_bool(var
, value
);
529 if (!strcmp(var
, "log.follow")) {
530 default_follow
= git_config_bool(var
, value
);
533 if (skip_prefix(var
, "color.decorate.", &slot_name
))
534 return parse_decorate_color_config(var
, slot_name
, value
);
535 if (!strcmp(var
, "log.mailmap")) {
536 use_mailmap_config
= git_config_bool(var
, value
);
539 if (!strcmp(var
, "log.showsignature")) {
540 default_show_signature
= git_config_bool(var
, value
);
544 if (git_gpg_config(var
, value
, cb
) < 0)
546 return git_diff_ui_config(var
, value
, cb
);
549 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
552 struct setup_revision_opt opt
;
555 git_config(git_log_config
, NULL
);
557 repo_init_revisions(the_repository
, &rev
, prefix
);
558 git_config(grep_config
, &rev
.grep_filter
);
561 rev
.simplify_history
= 0;
562 memset(&opt
, 0, sizeof(opt
));
564 opt
.revarg_opt
= REVARG_COMMITTISH
;
565 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
566 if (!rev
.diffopt
.output_format
)
567 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
568 return cmd_log_walk(&rev
);
571 static void show_tagger(const char *buf
, struct rev_info
*rev
)
573 struct strbuf out
= STRBUF_INIT
;
574 struct pretty_print_context pp
= {0};
576 pp
.fmt
= rev
->commit_format
;
577 pp
.date_mode
= rev
->date_mode
;
578 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
579 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
580 strbuf_release(&out
);
583 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
585 struct object_id oidc
;
586 struct object_context obj_context
;
590 fflush(rev
->diffopt
.file
);
591 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
592 !rev
->diffopt
.flags
.allow_textconv
)
593 return stream_blob_to_fd(1, oid
, NULL
, 0);
595 if (get_oid_with_context(the_repository
, obj_name
,
597 &oidc
, &obj_context
))
598 die(_("not a valid object name %s"), obj_name
);
599 if (!obj_context
.path
||
600 !textconv_object(the_repository
, obj_context
.path
,
601 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
602 free(obj_context
.path
);
603 return stream_blob_to_fd(1, oid
, NULL
, 0);
607 die(_("git show %s: bad file"), obj_name
);
609 write_or_die(1, buf
, size
);
610 free(obj_context
.path
);
614 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
617 enum object_type type
;
618 char *buf
= read_object_file(oid
, &type
, &size
);
622 return error(_("could not read object %s"), oid_to_hex(oid
));
624 assert(type
== OBJ_TAG
);
625 while (offset
< size
&& buf
[offset
] != '\n') {
626 int new_offset
= offset
+ 1;
628 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
630 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
631 show_tagger(ident
, rev
);
636 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
641 static int show_tree_object(const struct object_id
*oid
,
643 const char *pathname
, unsigned mode
, void *context
)
645 FILE *file
= context
;
646 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
650 static void show_setup_revisions_tweak(struct rev_info
*rev
,
651 struct setup_revision_opt
*opt
)
653 if (rev
->first_parent_only
)
654 diff_merges_default_to_first_parent(rev
);
656 diff_merges_default_to_dense_combined(rev
);
657 if (!rev
->diffopt
.output_format
)
658 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
661 int cmd_show(int argc
, const char **argv
, const char *prefix
)
664 struct object_array_entry
*objects
;
665 struct setup_revision_opt opt
;
666 struct pathspec match_all
;
667 int i
, count
, ret
= 0;
670 git_config(git_log_config
, NULL
);
672 memset(&match_all
, 0, sizeof(match_all
));
673 repo_init_revisions(the_repository
, &rev
, prefix
);
674 git_config(grep_config
, &rev
.grep_filter
);
677 rev
.always_show_header
= 1;
679 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
681 memset(&opt
, 0, sizeof(opt
));
683 opt
.tweak
= show_setup_revisions_tweak
;
684 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
687 return cmd_log_walk(&rev
);
689 count
= rev
.pending
.nr
;
690 objects
= rev
.pending
.objects
;
691 rev
.diffopt
.no_free
= 1;
692 for (i
= 0; i
< count
&& !ret
; i
++) {
693 struct object
*o
= objects
[i
].item
;
694 const char *name
= objects
[i
].name
;
697 ret
= show_blob_object(&o
->oid
, &rev
, name
);
700 struct tag
*t
= (struct tag
*)o
;
701 struct object_id
*oid
= get_tagged_oid(t
);
705 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
706 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
708 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
709 ret
= show_tag_object(&o
->oid
, &rev
);
713 o
= parse_object(the_repository
, oid
);
715 ret
= error(_("could not read object %s"),
724 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
725 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
727 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
728 read_tree(the_repository
, (struct tree
*)o
,
729 &match_all
, show_tree_object
,
734 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
735 rev
.pending
.objects
= NULL
;
736 add_object_array(o
, name
, &rev
.pending
);
737 ret
= cmd_log_walk_no_free(&rev
);
740 ret
= error(_("unknown type: %d"), o
->type
);
744 rev
.diffopt
.no_free
= 0;
745 diff_free(&rev
.diffopt
);
752 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
754 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
757 struct setup_revision_opt opt
;
760 git_config(git_log_config
, NULL
);
762 repo_init_revisions(the_repository
, &rev
, prefix
);
763 init_reflog_walk(&rev
.reflog_info
);
764 git_config(grep_config
, &rev
.grep_filter
);
766 rev
.verbose_header
= 1;
767 memset(&opt
, 0, sizeof(opt
));
769 cmd_log_init_defaults(&rev
);
770 rev
.abbrev_commit
= 1;
771 rev
.commit_format
= CMIT_FMT_ONELINE
;
772 rev
.use_terminator
= 1;
773 rev
.always_show_header
= 1;
774 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
776 return cmd_log_walk(&rev
);
779 static void log_setup_revisions_tweak(struct rev_info
*rev
,
780 struct setup_revision_opt
*opt
)
782 if (rev
->diffopt
.flags
.default_follow_renames
&&
783 rev
->prune_data
.nr
== 1)
784 rev
->diffopt
.flags
.follow_renames
= 1;
786 if (rev
->first_parent_only
)
787 diff_merges_default_to_first_parent(rev
);
790 int cmd_log(int argc
, const char **argv
, const char *prefix
)
793 struct setup_revision_opt opt
;
796 git_config(git_log_config
, NULL
);
798 repo_init_revisions(the_repository
, &rev
, prefix
);
799 git_config(grep_config
, &rev
.grep_filter
);
801 rev
.always_show_header
= 1;
802 memset(&opt
, 0, sizeof(opt
));
804 opt
.revarg_opt
= REVARG_COMMITTISH
;
805 opt
.tweak
= log_setup_revisions_tweak
;
806 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
807 return cmd_log_walk(&rev
);
812 static const char *fmt_patch_suffix
= ".patch";
813 static int numbered
= 0;
814 static int auto_number
= 1;
816 static char *default_attach
= NULL
;
818 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
819 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
820 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
822 static void add_header(const char *value
)
824 struct string_list_item
*item
;
825 int len
= strlen(value
);
826 while (len
&& value
[len
- 1] == '\n')
829 if (!strncasecmp(value
, "to: ", 4)) {
830 item
= string_list_append(&extra_to
, value
+ 4);
832 } else if (!strncasecmp(value
, "cc: ", 4)) {
833 item
= string_list_append(&extra_cc
, value
+ 4);
836 item
= string_list_append(&extra_hdr
, value
);
839 item
->string
[len
] = '\0';
855 enum cover_from_description
{
862 enum auto_base_setting
{
868 static enum thread_level thread
;
869 static int do_signoff
;
870 static enum auto_base_setting auto_base
;
872 static const char *signature
= git_version_string
;
873 static const char *signature_file
;
874 static enum cover_setting config_cover_letter
;
875 static const char *config_output_directory
;
876 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
877 static int show_notes
;
878 static struct display_notes_opt notes_opt
;
880 static enum cover_from_description
parse_cover_from_description(const char *arg
)
882 if (!arg
|| !strcmp(arg
, "default"))
883 return COVER_FROM_MESSAGE
;
884 else if (!strcmp(arg
, "none"))
885 return COVER_FROM_NONE
;
886 else if (!strcmp(arg
, "message"))
887 return COVER_FROM_MESSAGE
;
888 else if (!strcmp(arg
, "subject"))
889 return COVER_FROM_SUBJECT
;
890 else if (!strcmp(arg
, "auto"))
891 return COVER_FROM_AUTO
;
893 die(_("%s: invalid cover from description mode"), arg
);
896 static int git_format_config(const char *var
, const char *value
, void *cb
)
898 if (!strcmp(var
, "format.headers")) {
900 die(_("format.headers without value"));
904 if (!strcmp(var
, "format.suffix"))
905 return git_config_string(&fmt_patch_suffix
, var
, value
);
906 if (!strcmp(var
, "format.to")) {
908 return config_error_nonbool(var
);
909 string_list_append(&extra_to
, value
);
912 if (!strcmp(var
, "format.cc")) {
914 return config_error_nonbool(var
);
915 string_list_append(&extra_cc
, value
);
918 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
919 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
922 if (!strcmp(var
, "format.numbered")) {
923 if (value
&& !strcasecmp(value
, "auto")) {
927 numbered
= git_config_bool(var
, value
);
928 auto_number
= auto_number
&& numbered
;
931 if (!strcmp(var
, "format.attach")) {
933 default_attach
= xstrdup(value
);
935 default_attach
= xstrdup(git_version_string
);
938 if (!strcmp(var
, "format.thread")) {
939 if (value
&& !strcasecmp(value
, "deep")) {
940 thread
= THREAD_DEEP
;
943 if (value
&& !strcasecmp(value
, "shallow")) {
944 thread
= THREAD_SHALLOW
;
947 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
950 if (!strcmp(var
, "format.signoff")) {
951 do_signoff
= git_config_bool(var
, value
);
954 if (!strcmp(var
, "format.signature"))
955 return git_config_string(&signature
, var
, value
);
956 if (!strcmp(var
, "format.signaturefile"))
957 return git_config_pathname(&signature_file
, var
, value
);
958 if (!strcmp(var
, "format.coverletter")) {
959 if (value
&& !strcasecmp(value
, "auto")) {
960 config_cover_letter
= COVER_AUTO
;
963 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
966 if (!strcmp(var
, "format.outputdirectory"))
967 return git_config_string(&config_output_directory
, var
, value
);
968 if (!strcmp(var
, "format.useautobase")) {
969 if (value
&& !strcasecmp(value
, "whenAble")) {
970 auto_base
= AUTO_BASE_WHEN_ABLE
;
973 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
976 if (!strcmp(var
, "format.from")) {
977 int b
= git_parse_maybe_bool(value
);
980 from
= xstrdup(value
);
982 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
987 if (!strcmp(var
, "format.notes")) {
988 int b
= git_parse_maybe_bool(value
);
990 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
992 enable_default_display_notes(¬es_opt
, &show_notes
);
994 disable_display_notes(¬es_opt
, &show_notes
);
997 if (!strcmp(var
, "format.coverfromdescription")) {
998 cover_from_description_mode
= parse_cover_from_description(value
);
1002 return git_log_config(var
, value
, cb
);
1005 static const char *output_directory
= NULL
;
1006 static int outdir_offset
;
1008 static int open_next_file(struct commit
*commit
, const char *subject
,
1009 struct rev_info
*rev
, int quiet
)
1011 struct strbuf filename
= STRBUF_INIT
;
1013 if (output_directory
) {
1014 strbuf_addstr(&filename
, output_directory
);
1015 strbuf_complete(&filename
, '/');
1018 if (rev
->numbered_files
)
1019 strbuf_addf(&filename
, "%d", rev
->nr
);
1021 fmt_output_commit(&filename
, commit
, rev
);
1023 fmt_output_subject(&filename
, subject
, rev
);
1026 printf("%s\n", filename
.buf
+ outdir_offset
);
1028 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
1029 error_errno(_("cannot open patch file %s"), filename
.buf
);
1030 strbuf_release(&filename
);
1034 strbuf_release(&filename
);
1038 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1040 struct rev_info check_rev
;
1041 struct commit
*commit
, *c1
, *c2
;
1042 struct object
*o1
, *o2
;
1043 unsigned flags1
, flags2
;
1045 if (rev
->pending
.nr
!= 2)
1046 die(_("need exactly one range"));
1048 o1
= rev
->pending
.objects
[0].item
;
1049 o2
= rev
->pending
.objects
[1].item
;
1052 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1053 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1055 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1056 die(_("not a range"));
1058 init_patch_ids(the_repository
, ids
);
1060 /* given a range a..b get all patch ids for b..a */
1061 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1062 check_rev
.max_parents
= 1;
1063 o1
->flags
^= UNINTERESTING
;
1064 o2
->flags
^= UNINTERESTING
;
1065 add_pending_object(&check_rev
, o1
, "o1");
1066 add_pending_object(&check_rev
, o2
, "o2");
1067 if (prepare_revision_walk(&check_rev
))
1068 die(_("revision walk setup failed"));
1070 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1071 add_commit_patch_id(commit
, ids
);
1074 /* reset for next revision walk */
1075 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1076 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1081 static void gen_message_id(struct rev_info
*info
, char *base
)
1083 struct strbuf buf
= STRBUF_INIT
;
1084 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1085 (timestamp_t
) time(NULL
),
1086 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1087 info
->message_id
= strbuf_detach(&buf
, NULL
);
1090 static void print_signature(FILE *file
)
1092 if (!signature
|| !*signature
)
1095 fprintf(file
, "-- \n%s", signature
);
1096 if (signature
[strlen(signature
)-1] != '\n')
1101 static char *find_branch_name(struct rev_info
*rev
)
1103 int i
, positive
= -1;
1104 struct object_id branch_oid
;
1105 const struct object_id
*tip_oid
;
1106 const char *ref
, *v
;
1107 char *full_ref
, *branch
= NULL
;
1109 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1110 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1119 ref
= rev
->cmdline
.rev
[positive
].name
;
1120 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1121 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1122 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1123 oideq(tip_oid
, &branch_oid
))
1124 branch
= xstrdup(v
);
1129 static void show_diffstat(struct rev_info
*rev
,
1130 struct commit
*origin
, struct commit
*head
)
1132 struct diff_options opts
;
1134 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1135 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1136 diff_setup_done(&opts
);
1138 diff_tree_oid(get_commit_tree_oid(origin
),
1139 get_commit_tree_oid(head
),
1141 diffcore_std(&opts
);
1144 fprintf(rev
->diffopt
.file
, "\n");
1147 static void prepare_cover_text(struct pretty_print_context
*pp
,
1148 const char *branch_name
,
1150 const char *encoding
,
1153 const char *subject
= "*** SUBJECT HERE ***";
1154 const char *body
= "*** BLURB HERE ***";
1155 struct strbuf description_sb
= STRBUF_INIT
;
1156 struct strbuf subject_sb
= STRBUF_INIT
;
1158 if (cover_from_description_mode
== COVER_FROM_NONE
)
1161 if (branch_name
&& *branch_name
)
1162 read_branch_desc(&description_sb
, branch_name
);
1163 if (!description_sb
.len
)
1166 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1167 cover_from_description_mode
== COVER_FROM_AUTO
)
1168 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1170 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1171 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1172 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1173 body
= description_sb
.buf
;
1175 subject
= subject_sb
.buf
;
1178 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1179 pp_remainder(pp
, &body
, sb
, 0);
1181 strbuf_release(&description_sb
);
1182 strbuf_release(&subject_sb
);
1185 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1187 strvec_pushf(arg
, "--notes=%s", item
->string
);
1191 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1193 if (!rev
->show_notes
) {
1194 strvec_push(arg
, "--no-notes");
1195 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1196 (rev
->notes_opt
.use_default_notes
== -1 &&
1197 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1198 strvec_push(arg
, "--notes");
1200 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1204 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1205 struct commit
*origin
,
1206 int nr
, struct commit
**list
,
1207 const char *branch_name
,
1210 const char *committer
;
1211 struct shortlog log
;
1212 struct strbuf sb
= STRBUF_INIT
;
1214 const char *encoding
= "UTF-8";
1215 int need_8bit_cte
= 0;
1216 struct pretty_print_context pp
= {0};
1217 struct commit
*head
= list
[0];
1219 if (!cmit_fmt_is_mail(rev
->commit_format
))
1220 die(_("cover letter needs email format"));
1222 committer
= git_committer_info(0);
1224 if (use_separate_file
&&
1225 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1226 die(_("failed to create cover-letter file"));
1228 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1230 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1231 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1232 if (has_non_ascii(buf
))
1234 unuse_commit_buffer(list
[i
], buf
);
1238 branch_name
= find_branch_name(rev
);
1240 pp
.fmt
= CMIT_FMT_EMAIL
;
1241 pp
.date_mode
.type
= DATE_RFC2822
;
1243 pp
.print_email_subject
= 1;
1244 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1245 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1246 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1248 strbuf_release(&sb
);
1250 shortlog_init(&log
);
1252 log
.wrap
= MAIL_DEFAULT_WRAP
;
1255 log
.file
= rev
->diffopt
.file
;
1256 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1257 for (i
= 0; i
< nr
; i
++)
1258 shortlog_add_commit(&log
, list
[i
]);
1260 shortlog_output(&log
);
1262 /* We can only do diffstat with a unique reference point */
1264 show_diffstat(rev
, origin
, head
);
1266 if (rev
->idiff_oid1
) {
1267 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1268 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1274 * Pass minimum required diff-options to range-diff; others
1275 * can be added later if deemed desirable.
1277 struct diff_options opts
;
1278 struct strvec other_arg
= STRVEC_INIT
;
1279 struct range_diff_options range_diff_opts
= {
1280 .creation_factor
= rev
->creation_factor
,
1283 .other_arg
= &other_arg
1287 opts
.file
= rev
->diffopt
.file
;
1288 opts
.use_color
= rev
->diffopt
.use_color
;
1289 diff_setup_done(&opts
);
1290 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1291 get_notes_args(&other_arg
, rev
);
1292 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1293 strvec_clear(&other_arg
);
1297 static const char *clean_message_id(const char *msg_id
)
1300 const char *a
, *z
, *m
;
1303 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1308 if (!isspace(ch
) && (ch
!= '>'))
1313 die(_("insane in-reply-to: %s"), msg_id
);
1316 return xmemdupz(a
, z
- a
);
1319 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1321 if (output_directory
&& is_absolute_path(output_directory
))
1322 return output_directory
;
1324 if (!prefix
|| !*prefix
) {
1325 if (output_directory
)
1326 return output_directory
;
1327 /* The user did not explicitly ask for "./" */
1332 outdir_offset
= strlen(prefix
);
1333 if (!output_directory
)
1336 return prefix_filename(prefix
, output_directory
);
1339 static const char * const builtin_format_patch_usage
[] = {
1340 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1344 static int keep_subject
= 0;
1346 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1348 BUG_ON_OPT_NEG(unset
);
1349 BUG_ON_OPT_ARG(arg
);
1350 ((struct rev_info
*)opt
->value
)->total
= -1;
1355 static int subject_prefix
= 0;
1357 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1360 BUG_ON_OPT_NEG(unset
);
1362 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1366 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1368 BUG_ON_OPT_NEG(unset
);
1369 BUG_ON_OPT_ARG(arg
);
1370 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1373 static int numbered_cmdline_opt
= 0;
1375 static int numbered_callback(const struct option
*opt
, const char *arg
,
1378 BUG_ON_OPT_ARG(arg
);
1379 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1385 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1388 BUG_ON_OPT_NEG(unset
);
1389 return numbered_callback(opt
, arg
, 1);
1392 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1395 const char **dir
= (const char **)opt
->value
;
1396 BUG_ON_OPT_NEG(unset
);
1398 die(_("two output directories?"));
1403 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1405 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1407 *thread
= THREAD_UNSET
;
1408 else if (!arg
|| !strcmp(arg
, "shallow"))
1409 *thread
= THREAD_SHALLOW
;
1410 else if (!strcmp(arg
, "deep"))
1411 *thread
= THREAD_DEEP
;
1413 * Please update _git_formatpatch() in git-completion.bash
1414 * when you add new options.
1421 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1423 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1425 rev
->mime_boundary
= NULL
;
1427 rev
->mime_boundary
= arg
;
1429 rev
->mime_boundary
= git_version_string
;
1430 rev
->no_inline
= unset
? 0 : 1;
1434 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1436 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1438 rev
->mime_boundary
= NULL
;
1440 rev
->mime_boundary
= arg
;
1442 rev
->mime_boundary
= git_version_string
;
1447 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1450 string_list_clear(&extra_hdr
, 0);
1451 string_list_clear(&extra_to
, 0);
1452 string_list_clear(&extra_cc
, 0);
1459 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1462 string_list_clear(&extra_to
, 0);
1464 string_list_append(&extra_to
, arg
);
1468 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1471 string_list_clear(&extra_cc
, 0);
1473 string_list_append(&extra_cc
, arg
);
1477 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1479 char **from
= opt
->value
;
1486 *from
= xstrdup(arg
);
1488 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1492 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1494 const char **base_commit
= opt
->value
;
1497 auto_base
= AUTO_BASE_NEVER
;
1498 *base_commit
= NULL
;
1499 } else if (!strcmp(arg
, "auto")) {
1500 auto_base
= AUTO_BASE_ALWAYS
;
1501 *base_commit
= NULL
;
1503 auto_base
= AUTO_BASE_NEVER
;
1509 struct base_tree_info
{
1510 struct object_id base_commit
;
1511 int nr_patch_id
, alloc_patch_id
;
1512 struct object_id
*patch_id
;
1515 static struct commit
*get_base_commit(const char *base_commit
,
1516 struct commit
**list
,
1519 struct commit
*base
= NULL
;
1520 struct commit
**rev
;
1521 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1523 switch (auto_base
) {
1524 case AUTO_BASE_NEVER
:
1529 /* no base information is requested */
1533 case AUTO_BASE_ALWAYS
:
1534 case AUTO_BASE_WHEN_ABLE
:
1536 BUG("requested automatic base selection but a commit was provided");
1539 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1543 BUG("unexpected automatic base selection method");
1547 base
= lookup_commit_reference_by_name(base_commit
);
1549 die(_("unknown commit %s"), base_commit
);
1551 struct branch
*curr_branch
= branch_get(NULL
);
1552 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1554 struct commit_list
*base_list
;
1555 struct commit
*commit
;
1556 struct object_id oid
;
1558 if (get_oid(upstream
, &oid
)) {
1560 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1564 commit
= lookup_commit_or_die(&oid
, "upstream base");
1565 base_list
= get_merge_bases_many(commit
, total
, list
);
1566 /* There should be one and only one merge base. */
1567 if (!base_list
|| base_list
->next
) {
1568 if (die_on_failure
) {
1569 die(_("could not find exact merge base"));
1571 free_commit_list(base_list
);
1575 base
= base_list
->item
;
1576 free_commit_list(base_list
);
1579 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1580 "please use git branch --set-upstream-to to track a remote branch.\n"
1581 "Or you could specify base commit by --base=<base-commit-id> manually"));
1587 ALLOC_ARRAY(rev
, total
);
1588 for (i
= 0; i
< total
; i
++)
1593 * Get merge base through pair-wise computations
1594 * and store it in rev[0].
1596 while (rev_nr
> 1) {
1597 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1598 struct commit_list
*merge_base
;
1599 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1600 if (!merge_base
|| merge_base
->next
) {
1601 if (die_on_failure
) {
1602 die(_("failed to find exact merge base"));
1609 rev
[i
] = merge_base
->item
;
1613 rev
[i
] = rev
[2 * i
];
1614 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1617 if (!in_merge_bases(base
, rev
[0])) {
1618 if (die_on_failure
) {
1619 die(_("base commit should be the ancestor of revision list"));
1626 for (i
= 0; i
< total
; i
++) {
1627 if (base
== list
[i
]) {
1628 if (die_on_failure
) {
1629 die(_("base commit shouldn't be in revision list"));
1641 define_commit_slab(commit_base
, int);
1643 static void prepare_bases(struct base_tree_info
*bases
,
1644 struct commit
*base
,
1645 struct commit
**list
,
1648 struct commit
*commit
;
1649 struct rev_info revs
;
1650 struct diff_options diffopt
;
1651 struct commit_base commit_base
;
1657 init_commit_base(&commit_base
);
1658 repo_diff_setup(the_repository
, &diffopt
);
1659 diffopt
.flags
.recursive
= 1;
1660 diff_setup_done(&diffopt
);
1662 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1664 repo_init_revisions(the_repository
, &revs
, NULL
);
1665 revs
.max_parents
= 1;
1666 revs
.topo_order
= 1;
1667 for (i
= 0; i
< total
; i
++) {
1668 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1669 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1670 *commit_base_at(&commit_base
, list
[i
]) = 1;
1672 base
->object
.flags
|= UNINTERESTING
;
1673 add_pending_object(&revs
, &base
->object
, "base");
1675 if (prepare_revision_walk(&revs
))
1676 die(_("revision walk setup failed"));
1678 * Traverse the commits list, get prerequisite patch ids
1679 * and stuff them in bases structure.
1681 while ((commit
= get_revision(&revs
)) != NULL
) {
1682 struct object_id oid
;
1683 struct object_id
*patch_id
;
1684 if (*commit_base_at(&commit_base
, commit
))
1686 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1687 die(_("cannot get patch id"));
1688 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1689 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1690 oidcpy(patch_id
, &oid
);
1691 bases
->nr_patch_id
++;
1693 clear_commit_base(&commit_base
);
1696 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1700 /* Only do this once, either for the cover or for the first one */
1701 if (is_null_oid(&bases
->base_commit
))
1704 /* Show the base commit */
1705 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1707 /* Show the prerequisite patches */
1708 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1709 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1711 free(bases
->patch_id
);
1712 bases
->nr_patch_id
= 0;
1713 bases
->alloc_patch_id
= 0;
1714 oidclr(&bases
->base_commit
);
1717 static const char *diff_title(struct strbuf
*sb
,
1718 const char *reroll_count
,
1719 const char *generic
,
1720 const char *rerolled
)
1724 /* RFC may be v0, so allow -v1 to diff against v0 */
1725 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1727 strbuf_addf(sb
, rerolled
, v
- 1);
1729 strbuf_addstr(sb
, generic
);
1733 static void infer_range_diff_ranges(struct strbuf
*r1
,
1736 struct commit
*origin
,
1737 struct commit
*head
)
1739 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1740 int prev_is_range
= is_range_diff_range(prev
);
1743 strbuf_addstr(r1
, prev
);
1745 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1748 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1749 else if (prev_is_range
)
1750 die(_("failed to infer range-diff origin of current series"));
1752 warning(_("using '%s' as range-diff origin of current series"), prev
);
1753 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1757 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1759 struct commit
*commit
;
1760 struct commit
**list
= NULL
;
1761 struct rev_info rev
;
1762 struct setup_revision_opt s_r_opt
;
1763 int nr
= 0, total
, i
;
1765 int start_number
= -1;
1766 int just_numbers
= 0;
1767 int ignore_if_in_upstream
= 0;
1768 int cover_letter
= -1;
1769 int boundary_count
= 0;
1770 int no_binary_diff
= 0;
1771 int zero_commit
= 0;
1772 struct commit
*origin
= NULL
;
1773 const char *in_reply_to
= NULL
;
1774 struct patch_ids ids
;
1775 struct strbuf buf
= STRBUF_INIT
;
1776 int use_patch_format
= 0;
1778 const char *reroll_count
= NULL
;
1779 char *cover_from_description_arg
= NULL
;
1780 char *branch_name
= NULL
;
1781 char *base_commit
= NULL
;
1782 struct base_tree_info bases
;
1783 struct commit
*base
;
1784 int show_progress
= 0;
1785 struct progress
*progress
= NULL
;
1786 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1787 struct strbuf idiff_title
= STRBUF_INIT
;
1788 const char *rdiff_prev
= NULL
;
1789 struct strbuf rdiff1
= STRBUF_INIT
;
1790 struct strbuf rdiff2
= STRBUF_INIT
;
1791 struct strbuf rdiff_title
= STRBUF_INIT
;
1792 int creation_factor
= -1;
1794 const struct option builtin_format_patch_options
[] = {
1795 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1796 N_("use [PATCH n/m] even with a single patch"),
1797 PARSE_OPT_NOARG
, numbered_callback
),
1798 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1799 N_("use [PATCH] even with multiple patches"),
1800 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1801 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1802 OPT_BOOL(0, "stdout", &use_stdout
,
1803 N_("print patches to standard out")),
1804 OPT_BOOL(0, "cover-letter", &cover_letter
,
1805 N_("generate a cover letter")),
1806 OPT_BOOL(0, "numbered-files", &just_numbers
,
1807 N_("use simple number sequence for output file names")),
1808 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1809 N_("use <sfx> instead of '.patch'")),
1810 OPT_INTEGER(0, "start-number", &start_number
,
1811 N_("start numbering patches at <n> instead of 1")),
1812 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1813 N_("mark the series as Nth re-roll")),
1814 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1815 N_("max length of output filename")),
1816 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1817 N_("use [RFC PATCH] instead of [PATCH]"),
1818 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1819 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1820 N_("cover-from-description-mode"),
1821 N_("generate parts of a cover letter based on a branch's description")),
1822 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1823 N_("use [<prefix>] instead of [PATCH]"),
1824 PARSE_OPT_NONEG
, subject_prefix_callback
),
1825 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1826 N_("dir"), N_("store resulting files in <dir>"),
1827 PARSE_OPT_NONEG
, output_directory_callback
),
1828 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1829 N_("don't strip/add [PATCH]"),
1830 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1831 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1832 N_("don't output binary diffs")),
1833 OPT_BOOL(0, "zero-commit", &zero_commit
,
1834 N_("output all-zero hash in From header")),
1835 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1836 N_("don't include a patch matching a commit upstream")),
1837 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1838 N_("show patch format instead of default (patch + stat)"),
1839 1, PARSE_OPT_NONEG
),
1840 OPT_GROUP(N_("Messaging")),
1841 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1842 N_("add email header"), header_callback
),
1843 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1844 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1845 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1846 N_("set From address to <ident> (or committer ident if absent)"),
1847 PARSE_OPT_OPTARG
, from_callback
),
1848 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1849 N_("make first mail a reply to <message-id>")),
1850 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1851 N_("attach the patch"), PARSE_OPT_OPTARG
,
1853 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1854 N_("inline the patch"),
1855 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1857 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1858 N_("enable message threading, styles: shallow, deep"),
1859 PARSE_OPT_OPTARG
, thread_callback
),
1860 OPT_STRING(0, "signature", &signature
, N_("signature"),
1861 N_("add a signature")),
1862 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1863 N_("add prerequisite tree info to the patch series"),
1865 OPT_FILENAME(0, "signature-file", &signature_file
,
1866 N_("add a signature from a file")),
1867 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1868 OPT_BOOL(0, "progress", &show_progress
,
1869 N_("show progress while generating patches")),
1870 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1871 N_("show changes against <rev> in cover letter or single patch"),
1872 parse_opt_object_name
),
1873 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1874 N_("show changes against <refspec> in cover letter or single patch")),
1875 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1876 N_("percentage by which creation is weighted")),
1880 extra_hdr
.strdup_strings
= 1;
1881 extra_to
.strdup_strings
= 1;
1882 extra_cc
.strdup_strings
= 1;
1884 init_log_defaults();
1885 init_display_notes(¬es_opt
);
1886 git_config(git_format_config
, NULL
);
1887 repo_init_revisions(the_repository
, &rev
, prefix
);
1888 git_config(grep_config
, &rev
.grep_filter
);
1890 rev
.show_notes
= show_notes
;
1891 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1892 rev
.commit_format
= CMIT_FMT_EMAIL
;
1893 rev
.encode_email_headers
= default_encode_email_headers
;
1894 rev
.expand_tabs_in_log_default
= 0;
1895 rev
.verbose_header
= 1;
1897 rev
.max_parents
= 1;
1898 rev
.diffopt
.flags
.recursive
= 1;
1899 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1900 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1901 s_r_opt
.def
= "HEAD";
1902 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1904 if (default_attach
) {
1905 rev
.mime_boundary
= default_attach
;
1910 * Parse the arguments before setup_revisions(), or something
1911 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1912 * possibly a valid SHA1.
1914 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1915 builtin_format_patch_usage
,
1916 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1917 PARSE_OPT_KEEP_DASHDASH
);
1919 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1920 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
1921 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
1923 if (cover_from_description_arg
)
1924 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
1927 struct strbuf sprefix
= STRBUF_INIT
;
1929 strbuf_addf(&sprefix
, "%s v%s",
1930 rev
.subject_prefix
, reroll_count
);
1931 rev
.reroll_count
= reroll_count
;
1932 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1935 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1936 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1937 strbuf_addch(&buf
, '\n');
1941 strbuf_addstr(&buf
, "To: ");
1942 for (i
= 0; i
< extra_to
.nr
; i
++) {
1944 strbuf_addstr(&buf
, " ");
1945 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1946 if (i
+ 1 < extra_to
.nr
)
1947 strbuf_addch(&buf
, ',');
1948 strbuf_addch(&buf
, '\n');
1952 strbuf_addstr(&buf
, "Cc: ");
1953 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1955 strbuf_addstr(&buf
, " ");
1956 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1957 if (i
+ 1 < extra_cc
.nr
)
1958 strbuf_addch(&buf
, ',');
1959 strbuf_addch(&buf
, '\n');
1962 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1965 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1966 die(_("invalid ident line: %s"), from
);
1969 if (start_number
< 0)
1973 * If numbered is set solely due to format.numbered in config,
1974 * and it would conflict with --keep-subject (-k) from the
1975 * command line, reset "numbered".
1977 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1980 if (numbered
&& keep_subject
)
1981 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
1982 if (keep_subject
&& subject_prefix
)
1983 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
1984 rev
.preserve_subject
= keep_subject
;
1986 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1988 die(_("unrecognized argument: %s"), argv
[1]);
1990 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1991 die(_("--name-only does not make sense"));
1992 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1993 die(_("--name-status does not make sense"));
1994 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1995 die(_("--check does not make sense"));
1996 if (rev
.remerge_diff
)
1997 die(_("--remerge-diff does not make sense"));
1999 if (!use_patch_format
&&
2000 (!rev
.diffopt
.output_format
||
2001 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
2002 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
2003 if (!rev
.diffopt
.stat_width
)
2004 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
2006 /* Always generate a patch */
2007 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
2009 rev
.zero_commit
= zero_commit
;
2010 rev
.patch_name_max
= fmt_patch_name_max
;
2012 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
2013 rev
.diffopt
.flags
.binary
= 1;
2016 load_display_notes(&rev
.notes_opt
);
2018 die_for_incompatible_opt3(use_stdout
, "--stdout",
2019 rev
.diffopt
.close_file
, "--output",
2020 !!output_directory
, "--output-directory");
2024 } else if (rev
.diffopt
.close_file
) {
2026 * The diff code parsed --output; it has already opened the
2027 * file, but we must instruct it not to close after each diff.
2029 rev
.diffopt
.no_free
= 1;
2033 if (!output_directory
)
2034 output_directory
= config_output_directory
;
2035 output_directory
= set_outdir(prefix
, output_directory
);
2037 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2038 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2040 * We consider <outdir> as 'outside of gitdir', therefore avoid
2041 * applying adjust_shared_perm in s-c-l-d.
2043 saved
= get_shared_repository();
2044 set_shared_repository(0);
2045 switch (safe_create_leading_directories_const(output_directory
)) {
2050 die(_("could not create leading directories "
2051 "of '%s'"), output_directory
);
2053 set_shared_repository(saved
);
2054 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2055 die_errno(_("could not create directory '%s'"),
2059 if (rev
.pending
.nr
== 1) {
2062 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2064 * This is traditional behaviour of "git format-patch
2065 * origin" that prepares what the origin side still
2068 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2069 add_head_to_pending(&rev
);
2073 * Otherwise, it is "format-patch -22 HEAD", and/or
2074 * "format-patch --root HEAD". The user wants
2075 * get_revision() to do the usual traversal.
2078 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2082 const char *ref
, *v
;
2083 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2085 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2086 branch_name
= xstrdup(v
);
2088 branch_name
= xstrdup(""); /* no branch */
2093 * We cannot move this anywhere earlier because we do want to
2094 * know if --root was given explicitly from the command line.
2096 rev
.show_root_diff
= 1;
2098 if (ignore_if_in_upstream
) {
2099 /* Don't say anything if head and upstream are the same. */
2100 if (rev
.pending
.nr
== 2) {
2101 struct object_array_entry
*o
= rev
.pending
.objects
;
2102 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2105 get_patch_ids(&rev
, &ids
);
2108 if (prepare_revision_walk(&rev
))
2109 die(_("revision walk setup failed"));
2111 while ((commit
= get_revision(&rev
)) != NULL
) {
2112 if (commit
->object
.flags
& BOUNDARY
) {
2114 origin
= (boundary_count
== 1) ? commit
: NULL
;
2118 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2122 REALLOC_ARRAY(list
, nr
);
2123 list
[nr
- 1] = commit
;
2129 if (cover_letter
== -1) {
2130 if (config_cover_letter
== COVER_AUTO
)
2131 cover_letter
= (total
> 1);
2133 cover_letter
= (config_cover_letter
== COVER_ON
);
2135 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2138 rev
.total
= total
+ start_number
- 1;
2140 if (idiff_prev
.nr
) {
2141 if (!cover_letter
&& total
!= 1)
2142 die(_("--interdiff requires --cover-letter or single patch"));
2143 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2144 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2145 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2147 _("Interdiff against v%d:"));
2150 if (creation_factor
< 0)
2151 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2152 else if (!rdiff_prev
)
2153 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2156 if (!cover_letter
&& total
!= 1)
2157 die(_("--range-diff requires --cover-letter or single patch"));
2159 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2161 rev
.rdiff1
= rdiff1
.buf
;
2162 rev
.rdiff2
= rdiff2
.buf
;
2163 rev
.creation_factor
= creation_factor
;
2164 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2166 _("Range-diff against v%d:"));
2170 ; /* --no-signature inhibits all signatures */
2171 } else if (signature
&& signature
!= git_version_string
) {
2172 ; /* non-default signature already set */
2173 } else if (signature_file
) {
2174 struct strbuf buf
= STRBUF_INIT
;
2176 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2177 die_errno(_("unable to read signature file '%s'"), signature_file
);
2178 signature
= strbuf_detach(&buf
, NULL
);
2181 memset(&bases
, 0, sizeof(bases
));
2182 base
= get_base_commit(base_commit
, list
, nr
);
2184 reset_revision_walk();
2185 clear_object_flags(UNINTERESTING
);
2186 prepare_bases(&bases
, base
, list
, nr
);
2189 if (in_reply_to
|| thread
|| cover_letter
)
2190 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
2192 const char *msgid
= clean_message_id(in_reply_to
);
2193 string_list_append(rev
.ref_message_ids
, msgid
);
2195 rev
.numbered_files
= just_numbers
;
2196 rev
.patch_suffix
= fmt_patch_suffix
;
2199 gen_message_id(&rev
, "cover");
2200 make_cover_letter(&rev
, !!output_directory
,
2201 origin
, nr
, list
, branch_name
, quiet
);
2202 print_bases(&bases
, rev
.diffopt
.file
);
2203 print_signature(rev
.diffopt
.file
);
2206 /* interdiff/range-diff in cover-letter; omit from patches */
2207 rev
.idiff_oid1
= NULL
;
2210 rev
.add_signoff
= do_signoff
;
2213 progress
= start_delayed_progress(_("Generating patches"), total
);
2216 display_progress(progress
, total
- nr
);
2218 rev
.nr
= total
- nr
+ (start_number
- 1);
2219 /* Make the second and subsequent mails replies to the first */
2221 /* Have we already had a message ID? */
2222 if (rev
.message_id
) {
2224 * For deep threading: make every mail
2225 * a reply to the previous one, no
2226 * matter what other options are set.
2228 * For shallow threading:
2230 * Without --cover-letter and
2231 * --in-reply-to, make every mail a
2232 * reply to the one before.
2234 * With --in-reply-to but no
2235 * --cover-letter, make every mail a
2236 * reply to the <reply-to>.
2238 * With --cover-letter, make every
2239 * mail but the cover letter a reply
2240 * to the cover letter. The cover
2241 * letter is a reply to the
2242 * --in-reply-to, if specified.
2244 if (thread
== THREAD_SHALLOW
2245 && rev
.ref_message_ids
->nr
> 0
2246 && (!cover_letter
|| rev
.nr
> 1))
2247 free(rev
.message_id
);
2249 string_list_append(rev
.ref_message_ids
,
2252 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2255 if (output_directory
&&
2256 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2257 die(_("failed to create output files"));
2258 shown
= log_tree_commit(&rev
, commit
);
2259 free_commit_buffer(the_repository
->parsed_objects
,
2262 /* We put one extra blank line between formatted
2263 * patches and this flag is used by log-tree code
2264 * to see if it needs to emit a LF before showing
2265 * the log; when using one file per patch, we do
2266 * not want the extra blank line.
2268 if (output_directory
)
2271 print_bases(&bases
, rev
.diffopt
.file
);
2272 if (rev
.mime_boundary
)
2273 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2274 mime_boundary_leader
,
2277 print_signature(rev
.diffopt
.file
);
2279 if (output_directory
)
2280 fclose(rev
.diffopt
.file
);
2282 stop_progress(&progress
);
2285 string_list_clear(&extra_to
, 0);
2286 string_list_clear(&extra_cc
, 0);
2287 string_list_clear(&extra_hdr
, 0);
2288 if (ignore_if_in_upstream
)
2289 free_patch_ids(&ids
);
2292 oid_array_clear(&idiff_prev
);
2293 strbuf_release(&idiff_title
);
2294 strbuf_release(&rdiff1
);
2295 strbuf_release(&rdiff2
);
2296 strbuf_release(&rdiff_title
);
2301 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2303 struct object_id oid
;
2304 if (get_oid(arg
, &oid
) == 0) {
2305 struct commit
*commit
= lookup_commit_reference(the_repository
,
2308 commit
->object
.flags
|= flags
;
2309 add_pending_object(revs
, &commit
->object
, arg
);
2316 static const char * const cherry_usage
[] = {
2317 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2321 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2322 int abbrev
, FILE *file
)
2325 fprintf(file
, "%c %s\n", sign
,
2326 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2328 struct strbuf buf
= STRBUF_INIT
;
2329 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2330 fprintf(file
, "%c %s %s\n", sign
,
2331 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2333 strbuf_release(&buf
);
2337 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2339 struct rev_info revs
;
2340 struct patch_ids ids
;
2341 struct commit
*commit
;
2342 struct commit_list
*list
= NULL
;
2343 struct branch
*current_branch
;
2344 const char *upstream
;
2345 const char *head
= "HEAD";
2346 const char *limit
= NULL
;
2347 int verbose
= 0, abbrev
= 0;
2349 struct option options
[] = {
2350 OPT__ABBREV(&abbrev
),
2351 OPT__VERBOSE(&verbose
, N_("be verbose")),
2355 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2368 current_branch
= branch_get(NULL
);
2369 upstream
= branch_get_upstream(current_branch
, NULL
);
2371 fprintf(stderr
, _("Could not find a tracked"
2372 " remote branch, please"
2373 " specify <upstream> manually.\n"));
2374 usage_with_options(cherry_usage
, options
);
2378 repo_init_revisions(the_repository
, &revs
, prefix
);
2379 revs
.max_parents
= 1;
2381 if (add_pending_commit(head
, &revs
, 0))
2382 die(_("unknown commit %s"), head
);
2383 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2384 die(_("unknown commit %s"), upstream
);
2386 /* Don't say anything if head and upstream are the same. */
2387 if (revs
.pending
.nr
== 2) {
2388 struct object_array_entry
*o
= revs
.pending
.objects
;
2389 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2393 get_patch_ids(&revs
, &ids
);
2395 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2396 die(_("unknown commit %s"), limit
);
2398 /* reverse the list of commits */
2399 if (prepare_revision_walk(&revs
))
2400 die(_("revision walk setup failed"));
2401 while ((commit
= get_revision(&revs
)) != NULL
) {
2402 commit_list_insert(commit
, &list
);
2408 commit
= list
->item
;
2409 if (has_commit_patch_id(commit
, &ids
))
2411 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2415 free_patch_ids(&ids
);