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(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 rev
->diffopt
.no_free
= 1;
448 while ((commit
= get_revision(rev
)) != NULL
) {
449 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
451 * We decremented max_count in get_revision,
452 * but we didn't actually show the commit.
455 if (!rev
->reflog_info
) {
457 * We may show a given commit multiple times when
458 * walking the reflogs.
460 free_commit_buffer(the_repository
->parsed_objects
,
462 free_commit_list(commit
->parents
);
463 commit
->parents
= NULL
;
465 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
466 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
467 if (rev
->diffopt
.degraded_cc_to_c
)
470 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
471 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
472 rev
->diffopt
.no_free
= 0;
473 diff_free(&rev
->diffopt
);
475 if (rev
->remerge_diff
) {
476 tmp_objdir_destroy(rev
->remerge_objdir
);
477 rev
->remerge_objdir
= NULL
;
480 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
481 rev
->diffopt
.flags
.check_failed
) {
484 return diff_result_code(&rev
->diffopt
, 0);
487 static int git_log_config(const char *var
, const char *value
, void *cb
)
489 const char *slot_name
;
491 if (!strcmp(var
, "format.pretty"))
492 return git_config_string(&fmt_pretty
, var
, value
);
493 if (!strcmp(var
, "format.subjectprefix"))
494 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
495 if (!strcmp(var
, "format.filenamemaxlength")) {
496 fmt_patch_name_max
= git_config_int(var
, value
);
499 if (!strcmp(var
, "format.encodeemailheaders")) {
500 default_encode_email_headers
= git_config_bool(var
, value
);
503 if (!strcmp(var
, "log.abbrevcommit")) {
504 default_abbrev_commit
= git_config_bool(var
, value
);
507 if (!strcmp(var
, "log.date"))
508 return git_config_string(&default_date_mode
, var
, value
);
509 if (!strcmp(var
, "log.decorate")) {
510 decoration_style
= parse_decoration_style(value
);
511 if (decoration_style
< 0)
512 decoration_style
= 0; /* maybe warn? */
515 if (!strcmp(var
, "log.diffmerges"))
516 return diff_merges_config(value
);
517 if (!strcmp(var
, "log.showroot")) {
518 default_show_root
= git_config_bool(var
, value
);
521 if (!strcmp(var
, "log.follow")) {
522 default_follow
= git_config_bool(var
, value
);
525 if (skip_prefix(var
, "color.decorate.", &slot_name
))
526 return parse_decorate_color_config(var
, slot_name
, value
);
527 if (!strcmp(var
, "log.mailmap")) {
528 use_mailmap_config
= git_config_bool(var
, value
);
531 if (!strcmp(var
, "log.showsignature")) {
532 default_show_signature
= git_config_bool(var
, value
);
536 if (grep_config(var
, value
, cb
) < 0)
538 if (git_gpg_config(var
, value
, cb
) < 0)
540 return git_diff_ui_config(var
, value
, cb
);
543 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
546 struct setup_revision_opt opt
;
549 git_config(git_log_config
, NULL
);
551 repo_init_revisions(the_repository
, &rev
, prefix
);
553 rev
.simplify_history
= 0;
554 memset(&opt
, 0, sizeof(opt
));
556 opt
.revarg_opt
= REVARG_COMMITTISH
;
557 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
558 if (!rev
.diffopt
.output_format
)
559 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
560 return cmd_log_walk(&rev
);
563 static void show_tagger(const char *buf
, struct rev_info
*rev
)
565 struct strbuf out
= STRBUF_INIT
;
566 struct pretty_print_context pp
= {0};
568 pp
.fmt
= rev
->commit_format
;
569 pp
.date_mode
= rev
->date_mode
;
570 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
571 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
572 strbuf_release(&out
);
575 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
577 struct object_id oidc
;
578 struct object_context obj_context
;
582 fflush(rev
->diffopt
.file
);
583 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
584 !rev
->diffopt
.flags
.allow_textconv
)
585 return stream_blob_to_fd(1, oid
, NULL
, 0);
587 if (get_oid_with_context(the_repository
, obj_name
,
589 &oidc
, &obj_context
))
590 die(_("not a valid object name %s"), obj_name
);
591 if (!obj_context
.path
||
592 !textconv_object(the_repository
, obj_context
.path
,
593 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
594 free(obj_context
.path
);
595 return stream_blob_to_fd(1, oid
, NULL
, 0);
599 die(_("git show %s: bad file"), obj_name
);
601 write_or_die(1, buf
, size
);
602 free(obj_context
.path
);
606 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
609 enum object_type type
;
610 char *buf
= read_object_file(oid
, &type
, &size
);
614 return error(_("could not read object %s"), oid_to_hex(oid
));
616 assert(type
== OBJ_TAG
);
617 while (offset
< size
&& buf
[offset
] != '\n') {
618 int new_offset
= offset
+ 1;
620 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
622 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
623 show_tagger(ident
, rev
);
628 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
633 static int show_tree_object(const struct object_id
*oid
,
635 const char *pathname
, unsigned mode
, void *context
)
637 FILE *file
= context
;
638 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
642 static void show_setup_revisions_tweak(struct rev_info
*rev
,
643 struct setup_revision_opt
*opt
)
645 if (rev
->first_parent_only
)
646 diff_merges_default_to_first_parent(rev
);
648 diff_merges_default_to_dense_combined(rev
);
649 if (!rev
->diffopt
.output_format
)
650 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
653 int cmd_show(int argc
, const char **argv
, const char *prefix
)
656 struct object_array_entry
*objects
;
657 struct setup_revision_opt opt
;
658 struct pathspec match_all
;
659 int i
, count
, ret
= 0;
662 git_config(git_log_config
, NULL
);
664 memset(&match_all
, 0, sizeof(match_all
));
665 repo_init_revisions(the_repository
, &rev
, prefix
);
667 rev
.always_show_header
= 1;
669 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
671 memset(&opt
, 0, sizeof(opt
));
673 opt
.tweak
= show_setup_revisions_tweak
;
674 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
677 return cmd_log_walk(&rev
);
679 count
= rev
.pending
.nr
;
680 objects
= rev
.pending
.objects
;
681 for (i
= 0; i
< count
&& !ret
; i
++) {
682 struct object
*o
= objects
[i
].item
;
683 const char *name
= objects
[i
].name
;
686 ret
= show_blob_object(&o
->oid
, &rev
, name
);
689 struct tag
*t
= (struct tag
*)o
;
690 struct object_id
*oid
= get_tagged_oid(t
);
694 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
695 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
697 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
698 ret
= show_tag_object(&o
->oid
, &rev
);
702 o
= parse_object(the_repository
, oid
);
704 ret
= error(_("could not read object %s"),
713 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
714 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
716 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
717 read_tree(the_repository
, (struct tree
*)o
,
718 &match_all
, show_tree_object
,
723 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
724 rev
.pending
.objects
= NULL
;
725 add_object_array(o
, name
, &rev
.pending
);
726 ret
= cmd_log_walk(&rev
);
729 ret
= error(_("unknown type: %d"), o
->type
);
737 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
739 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
742 struct setup_revision_opt opt
;
745 git_config(git_log_config
, NULL
);
747 repo_init_revisions(the_repository
, &rev
, prefix
);
748 init_reflog_walk(&rev
.reflog_info
);
749 rev
.verbose_header
= 1;
750 memset(&opt
, 0, sizeof(opt
));
752 cmd_log_init_defaults(&rev
);
753 rev
.abbrev_commit
= 1;
754 rev
.commit_format
= CMIT_FMT_ONELINE
;
755 rev
.use_terminator
= 1;
756 rev
.always_show_header
= 1;
757 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
759 return cmd_log_walk(&rev
);
762 static void log_setup_revisions_tweak(struct rev_info
*rev
,
763 struct setup_revision_opt
*opt
)
765 if (rev
->diffopt
.flags
.default_follow_renames
&&
766 rev
->prune_data
.nr
== 1)
767 rev
->diffopt
.flags
.follow_renames
= 1;
769 if (rev
->first_parent_only
)
770 diff_merges_default_to_first_parent(rev
);
773 int cmd_log(int argc
, const char **argv
, const char *prefix
)
776 struct setup_revision_opt opt
;
779 git_config(git_log_config
, NULL
);
781 repo_init_revisions(the_repository
, &rev
, prefix
);
782 rev
.always_show_header
= 1;
783 memset(&opt
, 0, sizeof(opt
));
785 opt
.revarg_opt
= REVARG_COMMITTISH
;
786 opt
.tweak
= log_setup_revisions_tweak
;
787 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
788 return cmd_log_walk(&rev
);
793 static const char *fmt_patch_suffix
= ".patch";
794 static int numbered
= 0;
795 static int auto_number
= 1;
797 static char *default_attach
= NULL
;
799 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
800 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
801 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
803 static void add_header(const char *value
)
805 struct string_list_item
*item
;
806 int len
= strlen(value
);
807 while (len
&& value
[len
- 1] == '\n')
810 if (!strncasecmp(value
, "to: ", 4)) {
811 item
= string_list_append(&extra_to
, value
+ 4);
813 } else if (!strncasecmp(value
, "cc: ", 4)) {
814 item
= string_list_append(&extra_cc
, value
+ 4);
817 item
= string_list_append(&extra_hdr
, value
);
820 item
->string
[len
] = '\0';
836 enum cover_from_description
{
843 enum auto_base_setting
{
849 static enum thread_level thread
;
850 static int do_signoff
;
851 static enum auto_base_setting auto_base
;
853 static const char *signature
= git_version_string
;
854 static const char *signature_file
;
855 static enum cover_setting config_cover_letter
;
856 static const char *config_output_directory
;
857 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
858 static int show_notes
;
859 static struct display_notes_opt notes_opt
;
861 static enum cover_from_description
parse_cover_from_description(const char *arg
)
863 if (!arg
|| !strcmp(arg
, "default"))
864 return COVER_FROM_MESSAGE
;
865 else if (!strcmp(arg
, "none"))
866 return COVER_FROM_NONE
;
867 else if (!strcmp(arg
, "message"))
868 return COVER_FROM_MESSAGE
;
869 else if (!strcmp(arg
, "subject"))
870 return COVER_FROM_SUBJECT
;
871 else if (!strcmp(arg
, "auto"))
872 return COVER_FROM_AUTO
;
874 die(_("%s: invalid cover from description mode"), arg
);
877 static int git_format_config(const char *var
, const char *value
, void *cb
)
879 if (!strcmp(var
, "format.headers")) {
881 die(_("format.headers without value"));
885 if (!strcmp(var
, "format.suffix"))
886 return git_config_string(&fmt_patch_suffix
, var
, value
);
887 if (!strcmp(var
, "format.to")) {
889 return config_error_nonbool(var
);
890 string_list_append(&extra_to
, value
);
893 if (!strcmp(var
, "format.cc")) {
895 return config_error_nonbool(var
);
896 string_list_append(&extra_cc
, value
);
899 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
900 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
903 if (!strcmp(var
, "format.numbered")) {
904 if (value
&& !strcasecmp(value
, "auto")) {
908 numbered
= git_config_bool(var
, value
);
909 auto_number
= auto_number
&& numbered
;
912 if (!strcmp(var
, "format.attach")) {
914 default_attach
= xstrdup(value
);
916 default_attach
= xstrdup(git_version_string
);
919 if (!strcmp(var
, "format.thread")) {
920 if (value
&& !strcasecmp(value
, "deep")) {
921 thread
= THREAD_DEEP
;
924 if (value
&& !strcasecmp(value
, "shallow")) {
925 thread
= THREAD_SHALLOW
;
928 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
931 if (!strcmp(var
, "format.signoff")) {
932 do_signoff
= git_config_bool(var
, value
);
935 if (!strcmp(var
, "format.signature"))
936 return git_config_string(&signature
, var
, value
);
937 if (!strcmp(var
, "format.signaturefile"))
938 return git_config_pathname(&signature_file
, var
, value
);
939 if (!strcmp(var
, "format.coverletter")) {
940 if (value
&& !strcasecmp(value
, "auto")) {
941 config_cover_letter
= COVER_AUTO
;
944 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
947 if (!strcmp(var
, "format.outputdirectory"))
948 return git_config_string(&config_output_directory
, var
, value
);
949 if (!strcmp(var
, "format.useautobase")) {
950 if (value
&& !strcasecmp(value
, "whenAble")) {
951 auto_base
= AUTO_BASE_WHEN_ABLE
;
954 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
957 if (!strcmp(var
, "format.from")) {
958 int b
= git_parse_maybe_bool(value
);
961 from
= xstrdup(value
);
963 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
968 if (!strcmp(var
, "format.notes")) {
969 int b
= git_parse_maybe_bool(value
);
971 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
973 enable_default_display_notes(¬es_opt
, &show_notes
);
975 disable_display_notes(¬es_opt
, &show_notes
);
978 if (!strcmp(var
, "format.coverfromdescription")) {
979 cover_from_description_mode
= parse_cover_from_description(value
);
983 return git_log_config(var
, value
, cb
);
986 static const char *output_directory
= NULL
;
987 static int outdir_offset
;
989 static int open_next_file(struct commit
*commit
, const char *subject
,
990 struct rev_info
*rev
, int quiet
)
992 struct strbuf filename
= STRBUF_INIT
;
994 if (output_directory
) {
995 strbuf_addstr(&filename
, output_directory
);
996 strbuf_complete(&filename
, '/');
999 if (rev
->numbered_files
)
1000 strbuf_addf(&filename
, "%d", rev
->nr
);
1002 fmt_output_commit(&filename
, commit
, rev
);
1004 fmt_output_subject(&filename
, subject
, rev
);
1007 printf("%s\n", filename
.buf
+ outdir_offset
);
1009 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
1010 error_errno(_("cannot open patch file %s"), filename
.buf
);
1011 strbuf_release(&filename
);
1015 strbuf_release(&filename
);
1019 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
1021 struct rev_info check_rev
;
1022 struct commit
*commit
, *c1
, *c2
;
1023 struct object
*o1
, *o2
;
1024 unsigned flags1
, flags2
;
1026 if (rev
->pending
.nr
!= 2)
1027 die(_("need exactly one range"));
1029 o1
= rev
->pending
.objects
[0].item
;
1030 o2
= rev
->pending
.objects
[1].item
;
1033 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1034 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1036 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1037 die(_("not a range"));
1039 init_patch_ids(the_repository
, ids
);
1041 /* given a range a..b get all patch ids for b..a */
1042 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1043 check_rev
.max_parents
= 1;
1044 o1
->flags
^= UNINTERESTING
;
1045 o2
->flags
^= UNINTERESTING
;
1046 add_pending_object(&check_rev
, o1
, "o1");
1047 add_pending_object(&check_rev
, o2
, "o2");
1048 if (prepare_revision_walk(&check_rev
))
1049 die(_("revision walk setup failed"));
1051 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1052 add_commit_patch_id(commit
, ids
);
1055 /* reset for next revision walk */
1056 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1057 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1062 static void gen_message_id(struct rev_info
*info
, char *base
)
1064 struct strbuf buf
= STRBUF_INIT
;
1065 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1066 (timestamp_t
) time(NULL
),
1067 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1068 info
->message_id
= strbuf_detach(&buf
, NULL
);
1071 static void print_signature(FILE *file
)
1073 if (!signature
|| !*signature
)
1076 fprintf(file
, "-- \n%s", signature
);
1077 if (signature
[strlen(signature
)-1] != '\n')
1082 static char *find_branch_name(struct rev_info
*rev
)
1084 int i
, positive
= -1;
1085 struct object_id branch_oid
;
1086 const struct object_id
*tip_oid
;
1087 const char *ref
, *v
;
1088 char *full_ref
, *branch
= NULL
;
1090 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1091 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1100 ref
= rev
->cmdline
.rev
[positive
].name
;
1101 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1102 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1103 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1104 oideq(tip_oid
, &branch_oid
))
1105 branch
= xstrdup(v
);
1110 static void show_diffstat(struct rev_info
*rev
,
1111 struct commit
*origin
, struct commit
*head
)
1113 struct diff_options opts
;
1115 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1116 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1117 diff_setup_done(&opts
);
1119 diff_tree_oid(get_commit_tree_oid(origin
),
1120 get_commit_tree_oid(head
),
1122 diffcore_std(&opts
);
1125 fprintf(rev
->diffopt
.file
, "\n");
1128 static void prepare_cover_text(struct pretty_print_context
*pp
,
1129 const char *branch_name
,
1131 const char *encoding
,
1134 const char *subject
= "*** SUBJECT HERE ***";
1135 const char *body
= "*** BLURB HERE ***";
1136 struct strbuf description_sb
= STRBUF_INIT
;
1137 struct strbuf subject_sb
= STRBUF_INIT
;
1139 if (cover_from_description_mode
== COVER_FROM_NONE
)
1142 if (branch_name
&& *branch_name
)
1143 read_branch_desc(&description_sb
, branch_name
);
1144 if (!description_sb
.len
)
1147 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1148 cover_from_description_mode
== COVER_FROM_AUTO
)
1149 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1151 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1152 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1153 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1154 body
= description_sb
.buf
;
1156 subject
= subject_sb
.buf
;
1159 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1160 pp_remainder(pp
, &body
, sb
, 0);
1162 strbuf_release(&description_sb
);
1163 strbuf_release(&subject_sb
);
1166 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1168 strvec_pushf(arg
, "--notes=%s", item
->string
);
1172 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1174 if (!rev
->show_notes
) {
1175 strvec_push(arg
, "--no-notes");
1176 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1177 (rev
->notes_opt
.use_default_notes
== -1 &&
1178 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1179 strvec_push(arg
, "--notes");
1181 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1185 static void make_cover_letter(struct rev_info
*rev
, int use_separate_file
,
1186 struct commit
*origin
,
1187 int nr
, struct commit
**list
,
1188 const char *branch_name
,
1191 const char *committer
;
1192 struct shortlog log
;
1193 struct strbuf sb
= STRBUF_INIT
;
1195 const char *encoding
= "UTF-8";
1196 int need_8bit_cte
= 0;
1197 struct pretty_print_context pp
= {0};
1198 struct commit
*head
= list
[0];
1200 if (!cmit_fmt_is_mail(rev
->commit_format
))
1201 die(_("cover letter needs email format"));
1203 committer
= git_committer_info(0);
1205 if (use_separate_file
&&
1206 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1207 die(_("failed to create cover-letter file"));
1209 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1211 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1212 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1213 if (has_non_ascii(buf
))
1215 unuse_commit_buffer(list
[i
], buf
);
1219 branch_name
= find_branch_name(rev
);
1221 pp
.fmt
= CMIT_FMT_EMAIL
;
1222 pp
.date_mode
.type
= DATE_RFC2822
;
1224 pp
.print_email_subject
= 1;
1225 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1226 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1227 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1229 strbuf_release(&sb
);
1231 shortlog_init(&log
);
1233 log
.wrap
= MAIL_DEFAULT_WRAP
;
1236 log
.file
= rev
->diffopt
.file
;
1237 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1238 for (i
= 0; i
< nr
; i
++)
1239 shortlog_add_commit(&log
, list
[i
]);
1241 shortlog_output(&log
);
1243 /* We can only do diffstat with a unique reference point */
1245 show_diffstat(rev
, origin
, head
);
1247 if (rev
->idiff_oid1
) {
1248 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1249 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1255 * Pass minimum required diff-options to range-diff; others
1256 * can be added later if deemed desirable.
1258 struct diff_options opts
;
1259 struct strvec other_arg
= STRVEC_INIT
;
1260 struct range_diff_options range_diff_opts
= {
1261 .creation_factor
= rev
->creation_factor
,
1264 .other_arg
= &other_arg
1268 opts
.file
= rev
->diffopt
.file
;
1269 opts
.use_color
= rev
->diffopt
.use_color
;
1270 diff_setup_done(&opts
);
1271 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1272 get_notes_args(&other_arg
, rev
);
1273 show_range_diff(rev
->rdiff1
, rev
->rdiff2
, &range_diff_opts
);
1274 strvec_clear(&other_arg
);
1278 static const char *clean_message_id(const char *msg_id
)
1281 const char *a
, *z
, *m
;
1284 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1289 if (!isspace(ch
) && (ch
!= '>'))
1294 die(_("insane in-reply-to: %s"), msg_id
);
1297 return xmemdupz(a
, z
- a
);
1300 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1302 if (output_directory
&& is_absolute_path(output_directory
))
1303 return output_directory
;
1305 if (!prefix
|| !*prefix
) {
1306 if (output_directory
)
1307 return output_directory
;
1308 /* The user did not explicitly ask for "./" */
1313 outdir_offset
= strlen(prefix
);
1314 if (!output_directory
)
1317 return prefix_filename(prefix
, output_directory
);
1320 static const char * const builtin_format_patch_usage
[] = {
1321 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1325 static int keep_subject
= 0;
1327 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1329 BUG_ON_OPT_NEG(unset
);
1330 BUG_ON_OPT_ARG(arg
);
1331 ((struct rev_info
*)opt
->value
)->total
= -1;
1336 static int subject_prefix
= 0;
1338 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1341 BUG_ON_OPT_NEG(unset
);
1343 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1347 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1349 BUG_ON_OPT_NEG(unset
);
1350 BUG_ON_OPT_ARG(arg
);
1351 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1354 static int numbered_cmdline_opt
= 0;
1356 static int numbered_callback(const struct option
*opt
, const char *arg
,
1359 BUG_ON_OPT_ARG(arg
);
1360 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1366 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1369 BUG_ON_OPT_NEG(unset
);
1370 return numbered_callback(opt
, arg
, 1);
1373 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1376 const char **dir
= (const char **)opt
->value
;
1377 BUG_ON_OPT_NEG(unset
);
1379 die(_("two output directories?"));
1384 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1386 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1388 *thread
= THREAD_UNSET
;
1389 else if (!arg
|| !strcmp(arg
, "shallow"))
1390 *thread
= THREAD_SHALLOW
;
1391 else if (!strcmp(arg
, "deep"))
1392 *thread
= THREAD_DEEP
;
1394 * Please update _git_formatpatch() in git-completion.bash
1395 * when you add new options.
1402 static int attach_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
;
1411 rev
->no_inline
= unset
? 0 : 1;
1415 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1417 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1419 rev
->mime_boundary
= NULL
;
1421 rev
->mime_boundary
= arg
;
1423 rev
->mime_boundary
= git_version_string
;
1428 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1431 string_list_clear(&extra_hdr
, 0);
1432 string_list_clear(&extra_to
, 0);
1433 string_list_clear(&extra_cc
, 0);
1440 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1443 string_list_clear(&extra_to
, 0);
1445 string_list_append(&extra_to
, arg
);
1449 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1452 string_list_clear(&extra_cc
, 0);
1454 string_list_append(&extra_cc
, arg
);
1458 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1460 char **from
= opt
->value
;
1467 *from
= xstrdup(arg
);
1469 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1473 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1475 const char **base_commit
= opt
->value
;
1478 auto_base
= AUTO_BASE_NEVER
;
1479 *base_commit
= NULL
;
1480 } else if (!strcmp(arg
, "auto")) {
1481 auto_base
= AUTO_BASE_ALWAYS
;
1482 *base_commit
= NULL
;
1484 auto_base
= AUTO_BASE_NEVER
;
1490 struct base_tree_info
{
1491 struct object_id base_commit
;
1492 int nr_patch_id
, alloc_patch_id
;
1493 struct object_id
*patch_id
;
1496 static struct commit
*get_base_commit(const char *base_commit
,
1497 struct commit
**list
,
1500 struct commit
*base
= NULL
;
1501 struct commit
**rev
;
1502 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1504 switch (auto_base
) {
1505 case AUTO_BASE_NEVER
:
1510 /* no base information is requested */
1514 case AUTO_BASE_ALWAYS
:
1515 case AUTO_BASE_WHEN_ABLE
:
1517 BUG("requested automatic base selection but a commit was provided");
1520 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1524 BUG("unexpected automatic base selection method");
1528 base
= lookup_commit_reference_by_name(base_commit
);
1530 die(_("unknown commit %s"), base_commit
);
1532 struct branch
*curr_branch
= branch_get(NULL
);
1533 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1535 struct commit_list
*base_list
;
1536 struct commit
*commit
;
1537 struct object_id oid
;
1539 if (get_oid(upstream
, &oid
)) {
1541 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1545 commit
= lookup_commit_or_die(&oid
, "upstream base");
1546 base_list
= get_merge_bases_many(commit
, total
, list
);
1547 /* There should be one and only one merge base. */
1548 if (!base_list
|| base_list
->next
) {
1549 if (die_on_failure
) {
1550 die(_("could not find exact merge base"));
1552 free_commit_list(base_list
);
1556 base
= base_list
->item
;
1557 free_commit_list(base_list
);
1560 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1561 "please use git branch --set-upstream-to to track a remote branch.\n"
1562 "Or you could specify base commit by --base=<base-commit-id> manually"));
1568 ALLOC_ARRAY(rev
, total
);
1569 for (i
= 0; i
< total
; i
++)
1574 * Get merge base through pair-wise computations
1575 * and store it in rev[0].
1577 while (rev_nr
> 1) {
1578 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1579 struct commit_list
*merge_base
;
1580 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1581 if (!merge_base
|| merge_base
->next
) {
1582 if (die_on_failure
) {
1583 die(_("failed to find exact merge base"));
1590 rev
[i
] = merge_base
->item
;
1594 rev
[i
] = rev
[2 * i
];
1595 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1598 if (!in_merge_bases(base
, rev
[0])) {
1599 if (die_on_failure
) {
1600 die(_("base commit should be the ancestor of revision list"));
1607 for (i
= 0; i
< total
; i
++) {
1608 if (base
== list
[i
]) {
1609 if (die_on_failure
) {
1610 die(_("base commit shouldn't be in revision list"));
1622 define_commit_slab(commit_base
, int);
1624 static void prepare_bases(struct base_tree_info
*bases
,
1625 struct commit
*base
,
1626 struct commit
**list
,
1629 struct commit
*commit
;
1630 struct rev_info revs
;
1631 struct diff_options diffopt
;
1632 struct commit_base commit_base
;
1638 init_commit_base(&commit_base
);
1639 repo_diff_setup(the_repository
, &diffopt
);
1640 diffopt
.flags
.recursive
= 1;
1641 diff_setup_done(&diffopt
);
1643 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1645 repo_init_revisions(the_repository
, &revs
, NULL
);
1646 revs
.max_parents
= 1;
1647 revs
.topo_order
= 1;
1648 for (i
= 0; i
< total
; i
++) {
1649 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1650 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1651 *commit_base_at(&commit_base
, list
[i
]) = 1;
1653 base
->object
.flags
|= UNINTERESTING
;
1654 add_pending_object(&revs
, &base
->object
, "base");
1656 if (prepare_revision_walk(&revs
))
1657 die(_("revision walk setup failed"));
1659 * Traverse the commits list, get prerequisite patch ids
1660 * and stuff them in bases structure.
1662 while ((commit
= get_revision(&revs
)) != NULL
) {
1663 struct object_id oid
;
1664 struct object_id
*patch_id
;
1665 if (*commit_base_at(&commit_base
, commit
))
1667 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1668 die(_("cannot get patch id"));
1669 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1670 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1671 oidcpy(patch_id
, &oid
);
1672 bases
->nr_patch_id
++;
1674 clear_commit_base(&commit_base
);
1677 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1681 /* Only do this once, either for the cover or for the first one */
1682 if (is_null_oid(&bases
->base_commit
))
1685 /* Show the base commit */
1686 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1688 /* Show the prerequisite patches */
1689 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1690 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1692 free(bases
->patch_id
);
1693 bases
->nr_patch_id
= 0;
1694 bases
->alloc_patch_id
= 0;
1695 oidclr(&bases
->base_commit
);
1698 static const char *diff_title(struct strbuf
*sb
,
1699 const char *reroll_count
,
1700 const char *generic
,
1701 const char *rerolled
)
1705 /* RFC may be v0, so allow -v1 to diff against v0 */
1706 if (reroll_count
&& !strtol_i(reroll_count
, 10, &v
) &&
1708 strbuf_addf(sb
, rerolled
, v
- 1);
1710 strbuf_addstr(sb
, generic
);
1714 static void infer_range_diff_ranges(struct strbuf
*r1
,
1717 struct commit
*origin
,
1718 struct commit
*head
)
1720 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1721 int prev_is_range
= is_range_diff_range(prev
);
1724 strbuf_addstr(r1
, prev
);
1726 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1729 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1730 else if (prev_is_range
)
1731 die(_("failed to infer range-diff origin of current series"));
1733 warning(_("using '%s' as range-diff origin of current series"), prev
);
1734 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1738 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1740 struct commit
*commit
;
1741 struct commit
**list
= NULL
;
1742 struct rev_info rev
;
1743 struct setup_revision_opt s_r_opt
;
1744 int nr
= 0, total
, i
;
1746 int start_number
= -1;
1747 int just_numbers
= 0;
1748 int ignore_if_in_upstream
= 0;
1749 int cover_letter
= -1;
1750 int boundary_count
= 0;
1751 int no_binary_diff
= 0;
1752 int zero_commit
= 0;
1753 struct commit
*origin
= NULL
;
1754 const char *in_reply_to
= NULL
;
1755 struct patch_ids ids
;
1756 struct strbuf buf
= STRBUF_INIT
;
1757 int use_patch_format
= 0;
1759 const char *reroll_count
= NULL
;
1760 char *cover_from_description_arg
= NULL
;
1761 char *branch_name
= NULL
;
1762 char *base_commit
= NULL
;
1763 struct base_tree_info bases
;
1764 struct commit
*base
;
1765 int show_progress
= 0;
1766 struct progress
*progress
= NULL
;
1767 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1768 struct strbuf idiff_title
= STRBUF_INIT
;
1769 const char *rdiff_prev
= NULL
;
1770 struct strbuf rdiff1
= STRBUF_INIT
;
1771 struct strbuf rdiff2
= STRBUF_INIT
;
1772 struct strbuf rdiff_title
= STRBUF_INIT
;
1773 int creation_factor
= -1;
1775 const struct option builtin_format_patch_options
[] = {
1776 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1777 N_("use [PATCH n/m] even with a single patch"),
1778 PARSE_OPT_NOARG
, numbered_callback
),
1779 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1780 N_("use [PATCH] even with multiple patches"),
1781 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1782 OPT_BOOL('s', "signoff", &do_signoff
, N_("add a Signed-off-by trailer")),
1783 OPT_BOOL(0, "stdout", &use_stdout
,
1784 N_("print patches to standard out")),
1785 OPT_BOOL(0, "cover-letter", &cover_letter
,
1786 N_("generate a cover letter")),
1787 OPT_BOOL(0, "numbered-files", &just_numbers
,
1788 N_("use simple number sequence for output file names")),
1789 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1790 N_("use <sfx> instead of '.patch'")),
1791 OPT_INTEGER(0, "start-number", &start_number
,
1792 N_("start numbering patches at <n> instead of 1")),
1793 OPT_STRING('v', "reroll-count", &reroll_count
, N_("reroll-count"),
1794 N_("mark the series as Nth re-roll")),
1795 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1796 N_("max length of output filename")),
1797 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1798 N_("use [RFC PATCH] instead of [PATCH]"),
1799 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1800 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1801 N_("cover-from-description-mode"),
1802 N_("generate parts of a cover letter based on a branch's description")),
1803 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1804 N_("use [<prefix>] instead of [PATCH]"),
1805 PARSE_OPT_NONEG
, subject_prefix_callback
),
1806 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1807 N_("dir"), N_("store resulting files in <dir>"),
1808 PARSE_OPT_NONEG
, output_directory_callback
),
1809 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1810 N_("don't strip/add [PATCH]"),
1811 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1812 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1813 N_("don't output binary diffs")),
1814 OPT_BOOL(0, "zero-commit", &zero_commit
,
1815 N_("output all-zero hash in From header")),
1816 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1817 N_("don't include a patch matching a commit upstream")),
1818 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1819 N_("show patch format instead of default (patch + stat)"),
1820 1, PARSE_OPT_NONEG
),
1821 OPT_GROUP(N_("Messaging")),
1822 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1823 N_("add email header"), header_callback
),
1824 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1825 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1826 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1827 N_("set From address to <ident> (or committer ident if absent)"),
1828 PARSE_OPT_OPTARG
, from_callback
),
1829 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1830 N_("make first mail a reply to <message-id>")),
1831 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1832 N_("attach the patch"), PARSE_OPT_OPTARG
,
1834 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1835 N_("inline the patch"),
1836 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1838 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1839 N_("enable message threading, styles: shallow, deep"),
1840 PARSE_OPT_OPTARG
, thread_callback
),
1841 OPT_STRING(0, "signature", &signature
, N_("signature"),
1842 N_("add a signature")),
1843 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1844 N_("add prerequisite tree info to the patch series"),
1846 OPT_FILENAME(0, "signature-file", &signature_file
,
1847 N_("add a signature from a file")),
1848 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1849 OPT_BOOL(0, "progress", &show_progress
,
1850 N_("show progress while generating patches")),
1851 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1852 N_("show changes against <rev> in cover letter or single patch"),
1853 parse_opt_object_name
),
1854 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1855 N_("show changes against <refspec> in cover letter or single patch")),
1856 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1857 N_("percentage by which creation is weighted")),
1861 extra_hdr
.strdup_strings
= 1;
1862 extra_to
.strdup_strings
= 1;
1863 extra_cc
.strdup_strings
= 1;
1864 init_log_defaults();
1865 init_display_notes(¬es_opt
);
1866 git_config(git_format_config
, NULL
);
1867 repo_init_revisions(the_repository
, &rev
, prefix
);
1868 rev
.show_notes
= show_notes
;
1869 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1870 rev
.commit_format
= CMIT_FMT_EMAIL
;
1871 rev
.encode_email_headers
= default_encode_email_headers
;
1872 rev
.expand_tabs_in_log_default
= 0;
1873 rev
.verbose_header
= 1;
1875 rev
.max_parents
= 1;
1876 rev
.diffopt
.flags
.recursive
= 1;
1877 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1878 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1879 s_r_opt
.def
= "HEAD";
1880 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1882 if (default_attach
) {
1883 rev
.mime_boundary
= default_attach
;
1888 * Parse the arguments before setup_revisions(), or something
1889 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1890 * possibly a valid SHA1.
1892 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1893 builtin_format_patch_usage
,
1894 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1895 PARSE_OPT_KEEP_DASHDASH
);
1897 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1898 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
1899 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
1901 if (cover_from_description_arg
)
1902 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
1905 struct strbuf sprefix
= STRBUF_INIT
;
1907 strbuf_addf(&sprefix
, "%s v%s",
1908 rev
.subject_prefix
, reroll_count
);
1909 rev
.reroll_count
= reroll_count
;
1910 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1913 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1914 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1915 strbuf_addch(&buf
, '\n');
1919 strbuf_addstr(&buf
, "To: ");
1920 for (i
= 0; i
< extra_to
.nr
; i
++) {
1922 strbuf_addstr(&buf
, " ");
1923 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1924 if (i
+ 1 < extra_to
.nr
)
1925 strbuf_addch(&buf
, ',');
1926 strbuf_addch(&buf
, '\n');
1930 strbuf_addstr(&buf
, "Cc: ");
1931 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1933 strbuf_addstr(&buf
, " ");
1934 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1935 if (i
+ 1 < extra_cc
.nr
)
1936 strbuf_addch(&buf
, ',');
1937 strbuf_addch(&buf
, '\n');
1940 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1943 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1944 die(_("invalid ident line: %s"), from
);
1947 if (start_number
< 0)
1951 * If numbered is set solely due to format.numbered in config,
1952 * and it would conflict with --keep-subject (-k) from the
1953 * command line, reset "numbered".
1955 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1958 if (numbered
&& keep_subject
)
1959 die(_("options '%s' and '%s' cannot be used together"), "-n", "-k");
1960 if (keep_subject
&& subject_prefix
)
1961 die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k");
1962 rev
.preserve_subject
= keep_subject
;
1964 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1966 die(_("unrecognized argument: %s"), argv
[1]);
1968 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1969 die(_("--name-only does not make sense"));
1970 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1971 die(_("--name-status does not make sense"));
1972 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1973 die(_("--check does not make sense"));
1974 if (rev
.remerge_diff
)
1975 die(_("--remerge-diff does not make sense"));
1977 if (!use_patch_format
&&
1978 (!rev
.diffopt
.output_format
||
1979 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1980 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1981 if (!rev
.diffopt
.stat_width
)
1982 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1984 /* Always generate a patch */
1985 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1987 rev
.zero_commit
= zero_commit
;
1988 rev
.patch_name_max
= fmt_patch_name_max
;
1990 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1991 rev
.diffopt
.flags
.binary
= 1;
1994 load_display_notes(&rev
.notes_opt
);
1996 if (use_stdout
+ rev
.diffopt
.close_file
+ !!output_directory
> 1)
1997 die(_("options '%s', '%s', and '%s' cannot be used together"), "--stdout", "--output", "--output-directory");
2001 } else if (rev
.diffopt
.close_file
) {
2003 * The diff code parsed --output; it has already opened the
2004 * file, but we must instruct it not to close after each diff.
2006 rev
.diffopt
.no_free
= 1;
2010 if (!output_directory
)
2011 output_directory
= config_output_directory
;
2012 output_directory
= set_outdir(prefix
, output_directory
);
2014 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
2015 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2017 * We consider <outdir> as 'outside of gitdir', therefore avoid
2018 * applying adjust_shared_perm in s-c-l-d.
2020 saved
= get_shared_repository();
2021 set_shared_repository(0);
2022 switch (safe_create_leading_directories_const(output_directory
)) {
2027 die(_("could not create leading directories "
2028 "of '%s'"), output_directory
);
2030 set_shared_repository(saved
);
2031 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
2032 die_errno(_("could not create directory '%s'"),
2036 if (rev
.pending
.nr
== 1) {
2039 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
2041 * This is traditional behaviour of "git format-patch
2042 * origin" that prepares what the origin side still
2045 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
2046 add_head_to_pending(&rev
);
2050 * Otherwise, it is "format-patch -22 HEAD", and/or
2051 * "format-patch --root HEAD". The user wants
2052 * get_revision() to do the usual traversal.
2055 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2059 const char *ref
, *v
;
2060 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2062 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2063 branch_name
= xstrdup(v
);
2065 branch_name
= xstrdup(""); /* no branch */
2070 * We cannot move this anywhere earlier because we do want to
2071 * know if --root was given explicitly from the command line.
2073 rev
.show_root_diff
= 1;
2075 if (ignore_if_in_upstream
) {
2076 /* Don't say anything if head and upstream are the same. */
2077 if (rev
.pending
.nr
== 2) {
2078 struct object_array_entry
*o
= rev
.pending
.objects
;
2079 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2082 get_patch_ids(&rev
, &ids
);
2085 if (prepare_revision_walk(&rev
))
2086 die(_("revision walk setup failed"));
2088 while ((commit
= get_revision(&rev
)) != NULL
) {
2089 if (commit
->object
.flags
& BOUNDARY
) {
2091 origin
= (boundary_count
== 1) ? commit
: NULL
;
2095 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2099 REALLOC_ARRAY(list
, nr
);
2100 list
[nr
- 1] = commit
;
2106 if (cover_letter
== -1) {
2107 if (config_cover_letter
== COVER_AUTO
)
2108 cover_letter
= (total
> 1);
2110 cover_letter
= (config_cover_letter
== COVER_ON
);
2112 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2115 rev
.total
= total
+ start_number
- 1;
2117 if (idiff_prev
.nr
) {
2118 if (!cover_letter
&& total
!= 1)
2119 die(_("--interdiff requires --cover-letter or single patch"));
2120 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2121 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2122 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2124 _("Interdiff against v%d:"));
2127 if (creation_factor
< 0)
2128 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2129 else if (!rdiff_prev
)
2130 die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff");
2133 if (!cover_letter
&& total
!= 1)
2134 die(_("--range-diff requires --cover-letter or single patch"));
2136 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2138 rev
.rdiff1
= rdiff1
.buf
;
2139 rev
.rdiff2
= rdiff2
.buf
;
2140 rev
.creation_factor
= creation_factor
;
2141 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2143 _("Range-diff against v%d:"));
2147 ; /* --no-signature inhibits all signatures */
2148 } else if (signature
&& signature
!= git_version_string
) {
2149 ; /* non-default signature already set */
2150 } else if (signature_file
) {
2151 struct strbuf buf
= STRBUF_INIT
;
2153 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2154 die_errno(_("unable to read signature file '%s'"), signature_file
);
2155 signature
= strbuf_detach(&buf
, NULL
);
2158 memset(&bases
, 0, sizeof(bases
));
2159 base
= get_base_commit(base_commit
, list
, nr
);
2161 reset_revision_walk();
2162 clear_object_flags(UNINTERESTING
);
2163 prepare_bases(&bases
, base
, list
, nr
);
2166 if (in_reply_to
|| thread
|| cover_letter
)
2167 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
2169 const char *msgid
= clean_message_id(in_reply_to
);
2170 string_list_append(rev
.ref_message_ids
, msgid
);
2172 rev
.numbered_files
= just_numbers
;
2173 rev
.patch_suffix
= fmt_patch_suffix
;
2176 gen_message_id(&rev
, "cover");
2177 make_cover_letter(&rev
, !!output_directory
,
2178 origin
, nr
, list
, branch_name
, quiet
);
2179 print_bases(&bases
, rev
.diffopt
.file
);
2180 print_signature(rev
.diffopt
.file
);
2183 /* interdiff/range-diff in cover-letter; omit from patches */
2184 rev
.idiff_oid1
= NULL
;
2187 rev
.add_signoff
= do_signoff
;
2190 progress
= start_delayed_progress(_("Generating patches"), total
);
2193 display_progress(progress
, total
- nr
);
2195 rev
.nr
= total
- nr
+ (start_number
- 1);
2196 /* Make the second and subsequent mails replies to the first */
2198 /* Have we already had a message ID? */
2199 if (rev
.message_id
) {
2201 * For deep threading: make every mail
2202 * a reply to the previous one, no
2203 * matter what other options are set.
2205 * For shallow threading:
2207 * Without --cover-letter and
2208 * --in-reply-to, make every mail a
2209 * reply to the one before.
2211 * With --in-reply-to but no
2212 * --cover-letter, make every mail a
2213 * reply to the <reply-to>.
2215 * With --cover-letter, make every
2216 * mail but the cover letter a reply
2217 * to the cover letter. The cover
2218 * letter is a reply to the
2219 * --in-reply-to, if specified.
2221 if (thread
== THREAD_SHALLOW
2222 && rev
.ref_message_ids
->nr
> 0
2223 && (!cover_letter
|| rev
.nr
> 1))
2224 free(rev
.message_id
);
2226 string_list_append(rev
.ref_message_ids
,
2229 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2232 if (output_directory
&&
2233 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2234 die(_("failed to create output files"));
2235 shown
= log_tree_commit(&rev
, commit
);
2236 free_commit_buffer(the_repository
->parsed_objects
,
2239 /* We put one extra blank line between formatted
2240 * patches and this flag is used by log-tree code
2241 * to see if it needs to emit a LF before showing
2242 * the log; when using one file per patch, we do
2243 * not want the extra blank line.
2245 if (output_directory
)
2248 print_bases(&bases
, rev
.diffopt
.file
);
2249 if (rev
.mime_boundary
)
2250 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2251 mime_boundary_leader
,
2254 print_signature(rev
.diffopt
.file
);
2256 if (output_directory
)
2257 fclose(rev
.diffopt
.file
);
2259 stop_progress(&progress
);
2262 string_list_clear(&extra_to
, 0);
2263 string_list_clear(&extra_cc
, 0);
2264 string_list_clear(&extra_hdr
, 0);
2265 if (ignore_if_in_upstream
)
2266 free_patch_ids(&ids
);
2269 oid_array_clear(&idiff_prev
);
2270 strbuf_release(&idiff_title
);
2271 strbuf_release(&rdiff1
);
2272 strbuf_release(&rdiff2
);
2273 strbuf_release(&rdiff_title
);
2278 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2280 struct object_id oid
;
2281 if (get_oid(arg
, &oid
) == 0) {
2282 struct commit
*commit
= lookup_commit_reference(the_repository
,
2285 commit
->object
.flags
|= flags
;
2286 add_pending_object(revs
, &commit
->object
, arg
);
2293 static const char * const cherry_usage
[] = {
2294 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2298 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2299 int abbrev
, FILE *file
)
2302 fprintf(file
, "%c %s\n", sign
,
2303 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2305 struct strbuf buf
= STRBUF_INIT
;
2306 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2307 fprintf(file
, "%c %s %s\n", sign
,
2308 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2310 strbuf_release(&buf
);
2314 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2316 struct rev_info revs
;
2317 struct patch_ids ids
;
2318 struct commit
*commit
;
2319 struct commit_list
*list
= NULL
;
2320 struct branch
*current_branch
;
2321 const char *upstream
;
2322 const char *head
= "HEAD";
2323 const char *limit
= NULL
;
2324 int verbose
= 0, abbrev
= 0;
2326 struct option options
[] = {
2327 OPT__ABBREV(&abbrev
),
2328 OPT__VERBOSE(&verbose
, N_("be verbose")),
2332 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2345 current_branch
= branch_get(NULL
);
2346 upstream
= branch_get_upstream(current_branch
, NULL
);
2348 fprintf(stderr
, _("Could not find a tracked"
2349 " remote branch, please"
2350 " specify <upstream> manually.\n"));
2351 usage_with_options(cherry_usage
, options
);
2355 repo_init_revisions(the_repository
, &revs
, prefix
);
2356 revs
.max_parents
= 1;
2358 if (add_pending_commit(head
, &revs
, 0))
2359 die(_("unknown commit %s"), head
);
2360 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2361 die(_("unknown commit %s"), upstream
);
2363 /* Don't say anything if head and upstream are the same. */
2364 if (revs
.pending
.nr
== 2) {
2365 struct object_array_entry
*o
= revs
.pending
.objects
;
2366 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2370 get_patch_ids(&revs
, &ids
);
2372 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2373 die(_("unknown commit %s"), limit
);
2375 /* reverse the list of commits */
2376 if (prepare_revision_walk(&revs
))
2377 die(_("revision walk setup failed"));
2378 while ((commit
= get_revision(&revs
)) != NULL
) {
2379 commit_list_insert(commit
, &list
);
2385 commit
= list
->item
;
2386 if (has_commit_patch_id(commit
, &ids
))
2388 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2392 free_patch_ids(&ids
);