2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "object-store.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "run-command.h"
24 #include "string-list.h"
25 #include "parse-options.h"
28 #include "streaming.h"
31 #include "gpg-interface.h"
33 #include "commit-slab.h"
34 #include "repository.h"
35 #include "commit-reach.h"
36 #include "range-diff.h"
38 #define MAIL_DEFAULT_WRAP 72
39 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
40 #define FORMAT_PATCH_NAME_MAX_DEFAULT 64
42 /* Set a default date-time format for git log ("log.date" config variable) */
43 static const char *default_date_mode
= NULL
;
45 static int default_abbrev_commit
;
46 static int default_show_root
= 1;
47 static int default_follow
;
48 static int default_show_signature
;
49 static int default_encode_email_headers
= 1;
50 static int decoration_style
;
51 static int decoration_given
;
52 static int use_mailmap_config
= 1;
53 static const char *fmt_patch_subject_prefix
= "PATCH";
54 static int fmt_patch_name_max
= FORMAT_PATCH_NAME_MAX_DEFAULT
;
55 static const char *fmt_pretty
;
57 static const char * const builtin_log_usage
[] = {
58 N_("git log [<options>] [<revision-range>] [[--] <path>...]"),
59 N_("git show [<options>] <object>..."),
63 struct line_opt_callback_data
{
66 struct string_list args
;
69 static int session_is_interactive(void)
71 return isatty(1) || pager_in_use();
74 static int auto_decoration_style(void)
76 return session_is_interactive() ? DECORATE_SHORT_REFS
: 0;
79 static int parse_decoration_style(const char *value
)
81 switch (git_parse_maybe_bool(value
)) {
83 return DECORATE_SHORT_REFS
;
89 if (!strcmp(value
, "full"))
90 return DECORATE_FULL_REFS
;
91 else if (!strcmp(value
, "short"))
92 return DECORATE_SHORT_REFS
;
93 else if (!strcmp(value
, "auto"))
94 return auto_decoration_style();
96 * Please update _git_log() in git-completion.bash when you
97 * add new decoration styles.
102 static int decorate_callback(const struct option
*opt
, const char *arg
, int unset
)
105 decoration_style
= 0;
107 decoration_style
= parse_decoration_style(arg
);
109 decoration_style
= DECORATE_SHORT_REFS
;
111 if (decoration_style
< 0)
112 die(_("invalid --decorate option: %s"), arg
);
114 decoration_given
= 1;
119 static int log_line_range_callback(const struct option
*option
, const char *arg
, int unset
)
121 struct line_opt_callback_data
*data
= option
->value
;
123 BUG_ON_OPT_NEG(unset
);
128 data
->rev
->line_level_traverse
= 1;
129 string_list_append(&data
->args
, arg
);
134 static void init_log_defaults(void)
136 init_grep_defaults(the_repository
);
137 init_diff_ui_defaults();
139 decoration_style
= auto_decoration_style();
142 static void cmd_log_init_defaults(struct rev_info
*rev
)
145 get_commit_format(fmt_pretty
, rev
);
147 rev
->diffopt
.flags
.default_follow_renames
= 1;
148 rev
->verbose_header
= 1;
149 rev
->diffopt
.flags
.recursive
= 1;
150 rev
->diffopt
.stat_width
= -1; /* use full terminal width */
151 rev
->diffopt
.stat_graph_width
= -1; /* respect statGraphWidth config */
152 rev
->abbrev_commit
= default_abbrev_commit
;
153 rev
->show_root_diff
= default_show_root
;
154 rev
->subject_prefix
= fmt_patch_subject_prefix
;
155 rev
->patch_name_max
= fmt_patch_name_max
;
156 rev
->show_signature
= default_show_signature
;
157 rev
->encode_email_headers
= default_encode_email_headers
;
158 rev
->diffopt
.flags
.allow_textconv
= 1;
160 if (default_date_mode
)
161 parse_date_format(default_date_mode
, &rev
->date_mode
);
164 static void cmd_log_init_finish(int argc
, const char **argv
, const char *prefix
,
165 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
167 struct userformat_want w
;
168 int quiet
= 0, source
= 0, mailmap
;
169 static struct line_opt_callback_data line_cb
= {NULL
, NULL
, STRING_LIST_INIT_DUP
};
170 static struct string_list decorate_refs_exclude
= STRING_LIST_INIT_NODUP
;
171 static struct string_list decorate_refs_exclude_config
= STRING_LIST_INIT_NODUP
;
172 static struct string_list decorate_refs_include
= STRING_LIST_INIT_NODUP
;
173 struct decoration_filter decoration_filter
= {&decorate_refs_include
,
174 &decorate_refs_exclude
,
175 &decorate_refs_exclude_config
};
176 static struct revision_sources revision_sources
;
178 const struct option builtin_log_options
[] = {
179 OPT__QUIET(&quiet
, N_("suppress diff output")),
180 OPT_BOOL(0, "source", &source
, N_("show source")),
181 OPT_BOOL(0, "use-mailmap", &mailmap
, N_("Use mail map file")),
182 OPT_ALIAS(0, "mailmap", "use-mailmap"),
183 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include
,
184 N_("pattern"), N_("only decorate refs that match <pattern>")),
185 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude
,
186 N_("pattern"), N_("do not decorate refs that match <pattern>")),
187 OPT_CALLBACK_F(0, "decorate", NULL
, NULL
, N_("decorate options"),
188 PARSE_OPT_OPTARG
, decorate_callback
),
189 OPT_CALLBACK('L', NULL
, &line_cb
, "n,m:file",
190 N_("Process line range n,m in file, counting from 1"),
191 log_line_range_callback
),
196 line_cb
.prefix
= prefix
;
198 mailmap
= use_mailmap_config
;
199 argc
= parse_options(argc
, argv
, prefix
,
200 builtin_log_options
, builtin_log_usage
,
201 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
202 PARSE_OPT_KEEP_DASHDASH
);
205 rev
->diffopt
.output_format
|= DIFF_FORMAT_NO_OUTPUT
;
206 argc
= setup_revisions(argc
, argv
, rev
, opt
);
208 /* Any arguments at this point are not recognized */
210 die(_("unrecognized argument: %s"), argv
[1]);
212 memset(&w
, 0, sizeof(w
));
213 userformat_find_requirements(NULL
, &w
);
215 if (!rev
->show_notes_given
&& (!rev
->pretty_given
|| w
.notes
))
218 load_display_notes(&rev
->notes_opt
);
220 if ((rev
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
221 rev
->diffopt
.filter
|| rev
->diffopt
.flags
.follow_renames
)
222 rev
->always_show_header
= 0;
224 if (source
|| w
.source
) {
225 init_revision_sources(&revision_sources
);
226 rev
->sources
= &revision_sources
;
230 rev
->mailmap
= xcalloc(1, sizeof(struct string_list
));
231 read_mailmap(rev
->mailmap
, NULL
);
234 if (rev
->pretty_given
&& rev
->commit_format
== CMIT_FMT_RAW
) {
236 * "log --pretty=raw" is special; ignore UI oriented
237 * configuration variables such as decoration.
239 if (!decoration_given
)
240 decoration_style
= 0;
241 if (!rev
->abbrev_commit_given
)
242 rev
->abbrev_commit
= 0;
245 if (decoration_style
) {
246 const struct string_list
*config_exclude
=
247 repo_config_get_value_multi(the_repository
,
248 "log.excludeDecoration");
250 if (config_exclude
) {
251 struct string_list_item
*item
;
252 for_each_string_list_item(item
, config_exclude
)
253 string_list_append(&decorate_refs_exclude_config
,
257 rev
->show_decorations
= 1;
259 load_ref_decorations(&decoration_filter
, decoration_style
);
262 if (rev
->line_level_traverse
)
263 line_log_init(rev
, line_cb
.prefix
, &line_cb
.args
);
268 static void cmd_log_init(int argc
, const char **argv
, const char *prefix
,
269 struct rev_info
*rev
, struct setup_revision_opt
*opt
)
271 cmd_log_init_defaults(rev
);
272 cmd_log_init_finish(argc
, argv
, prefix
, rev
, opt
);
276 * This gives a rough estimate for how many commits we
277 * will print out in the list.
279 static int estimate_commit_count(struct commit_list
*list
)
284 struct commit
*commit
= list
->item
;
285 unsigned int flags
= commit
->object
.flags
;
287 if (!(flags
& (TREESAME
| UNINTERESTING
)))
293 static void show_early_header(struct rev_info
*rev
, const char *stage
, int nr
)
295 if (rev
->shown_one
) {
297 if (rev
->commit_format
!= CMIT_FMT_ONELINE
)
298 putchar(rev
->diffopt
.line_termination
);
300 fprintf(rev
->diffopt
.file
, _("Final output: %d %s\n"), nr
, stage
);
303 static struct itimerval early_output_timer
;
305 static void log_show_early(struct rev_info
*revs
, struct commit_list
*list
)
307 int i
= revs
->early_output
, close_file
= revs
->diffopt
.close_file
;
310 revs
->diffopt
.close_file
= 0;
311 sort_in_topological_order(&list
, revs
->sort_order
);
313 struct commit
*commit
= list
->item
;
314 switch (simplify_commit(revs
, commit
)) {
317 int n
= estimate_commit_count(list
);
318 show_early_header(revs
, "incomplete", n
);
321 log_tree_commit(revs
, commit
);
328 fclose(revs
->diffopt
.file
);
334 /* Did we already get enough commits for the early output? */
337 fclose(revs
->diffopt
.file
);
342 * ..if no, then repeat it twice a second until we
345 * NOTE! We don't use "it_interval", because if the
346 * reader isn't listening, we want our output to be
347 * throttled by the writing, and not have the timer
348 * trigger every second even if we're blocked on a
351 early_output_timer
.it_value
.tv_sec
= 0;
352 early_output_timer
.it_value
.tv_usec
= 500000;
353 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
356 static void early_output(int signal
)
358 show_early_output
= log_show_early
;
361 static void setup_early_output(void)
366 * Set up the signal handler, minimally intrusively:
367 * we only set a single volatile integer word (not
368 * using sigatomic_t - trying to avoid unnecessary
369 * system dependencies and headers), and using
372 memset(&sa
, 0, sizeof(sa
));
373 sa
.sa_handler
= early_output
;
374 sigemptyset(&sa
.sa_mask
);
375 sa
.sa_flags
= SA_RESTART
;
376 sigaction(SIGALRM
, &sa
, NULL
);
379 * If we can get the whole output in less than a
380 * tenth of a second, don't even bother doing the
381 * early-output thing..
383 * This is a one-time-only trigger.
385 early_output_timer
.it_value
.tv_sec
= 0;
386 early_output_timer
.it_value
.tv_usec
= 100000;
387 setitimer(ITIMER_REAL
, &early_output_timer
, NULL
);
390 static void finish_early_output(struct rev_info
*rev
)
392 int n
= estimate_commit_count(rev
->commits
);
393 signal(SIGALRM
, SIG_IGN
);
394 show_early_header(rev
, "done", n
);
397 static int cmd_log_walk(struct rev_info
*rev
)
399 struct commit
*commit
;
401 int saved_dcctc
= 0, close_file
= rev
->diffopt
.close_file
;
403 if (rev
->early_output
)
404 setup_early_output();
406 if (prepare_revision_walk(rev
))
407 die(_("revision walk setup failed"));
409 if (rev
->early_output
)
410 finish_early_output(rev
);
413 * For --check and --exit-code, the exit code is based on CHECK_FAILED
414 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
415 * retain that state information if replacing rev->diffopt in this loop
417 rev
->diffopt
.close_file
= 0;
418 while ((commit
= get_revision(rev
)) != NULL
) {
419 if (!log_tree_commit(rev
, commit
) && rev
->max_count
>= 0)
421 * We decremented max_count in get_revision,
422 * but we didn't actually show the commit.
425 if (!rev
->reflog_info
) {
427 * We may show a given commit multiple times when
428 * walking the reflogs.
430 free_commit_buffer(the_repository
->parsed_objects
,
432 free_commit_list(commit
->parents
);
433 commit
->parents
= NULL
;
435 if (saved_nrl
< rev
->diffopt
.needed_rename_limit
)
436 saved_nrl
= rev
->diffopt
.needed_rename_limit
;
437 if (rev
->diffopt
.degraded_cc_to_c
)
440 rev
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
441 rev
->diffopt
.needed_rename_limit
= saved_nrl
;
443 fclose(rev
->diffopt
.file
);
445 if (rev
->diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
&&
446 rev
->diffopt
.flags
.check_failed
) {
449 return diff_result_code(&rev
->diffopt
, 0);
452 static int git_log_config(const char *var
, const char *value
, void *cb
)
454 const char *slot_name
;
456 if (!strcmp(var
, "format.pretty"))
457 return git_config_string(&fmt_pretty
, var
, value
);
458 if (!strcmp(var
, "format.subjectprefix"))
459 return git_config_string(&fmt_patch_subject_prefix
, var
, value
);
460 if (!strcmp(var
, "format.filenamemaxlength")) {
461 fmt_patch_name_max
= git_config_int(var
, value
);
464 if (!strcmp(var
, "format.encodeemailheaders")) {
465 default_encode_email_headers
= git_config_bool(var
, value
);
468 if (!strcmp(var
, "log.abbrevcommit")) {
469 default_abbrev_commit
= git_config_bool(var
, value
);
472 if (!strcmp(var
, "log.date"))
473 return git_config_string(&default_date_mode
, var
, value
);
474 if (!strcmp(var
, "log.decorate")) {
475 decoration_style
= parse_decoration_style(value
);
476 if (decoration_style
< 0)
477 decoration_style
= 0; /* maybe warn? */
480 if (!strcmp(var
, "log.showroot")) {
481 default_show_root
= git_config_bool(var
, value
);
484 if (!strcmp(var
, "log.follow")) {
485 default_follow
= git_config_bool(var
, value
);
488 if (skip_prefix(var
, "color.decorate.", &slot_name
))
489 return parse_decorate_color_config(var
, slot_name
, value
);
490 if (!strcmp(var
, "log.mailmap")) {
491 use_mailmap_config
= git_config_bool(var
, value
);
494 if (!strcmp(var
, "log.showsignature")) {
495 default_show_signature
= git_config_bool(var
, value
);
499 if (grep_config(var
, value
, cb
) < 0)
501 if (git_gpg_config(var
, value
, cb
) < 0)
503 return git_diff_ui_config(var
, value
, cb
);
506 int cmd_whatchanged(int argc
, const char **argv
, const char *prefix
)
509 struct setup_revision_opt opt
;
512 git_config(git_log_config
, NULL
);
514 repo_init_revisions(the_repository
, &rev
, prefix
);
516 rev
.simplify_history
= 0;
517 memset(&opt
, 0, sizeof(opt
));
519 opt
.revarg_opt
= REVARG_COMMITTISH
;
520 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
521 if (!rev
.diffopt
.output_format
)
522 rev
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
523 return cmd_log_walk(&rev
);
526 static void show_tagger(const char *buf
, struct rev_info
*rev
)
528 struct strbuf out
= STRBUF_INIT
;
529 struct pretty_print_context pp
= {0};
531 pp
.fmt
= rev
->commit_format
;
532 pp
.date_mode
= rev
->date_mode
;
533 pp_user_info(&pp
, "Tagger", &out
, buf
, get_log_output_encoding());
534 fprintf(rev
->diffopt
.file
, "%s", out
.buf
);
535 strbuf_release(&out
);
538 static int show_blob_object(const struct object_id
*oid
, struct rev_info
*rev
, const char *obj_name
)
540 struct object_id oidc
;
541 struct object_context obj_context
;
545 fflush(rev
->diffopt
.file
);
546 if (!rev
->diffopt
.flags
.textconv_set_via_cmdline
||
547 !rev
->diffopt
.flags
.allow_textconv
)
548 return stream_blob_to_fd(1, oid
, NULL
, 0);
550 if (get_oid_with_context(the_repository
, obj_name
,
552 &oidc
, &obj_context
))
553 die(_("not a valid object name %s"), obj_name
);
554 if (!obj_context
.path
||
555 !textconv_object(the_repository
, obj_context
.path
,
556 obj_context
.mode
, &oidc
, 1, &buf
, &size
)) {
557 free(obj_context
.path
);
558 return stream_blob_to_fd(1, oid
, NULL
, 0);
562 die(_("git show %s: bad file"), obj_name
);
564 write_or_die(1, buf
, size
);
565 free(obj_context
.path
);
569 static int show_tag_object(const struct object_id
*oid
, struct rev_info
*rev
)
572 enum object_type type
;
573 char *buf
= read_object_file(oid
, &type
, &size
);
577 return error(_("could not read object %s"), oid_to_hex(oid
));
579 assert(type
== OBJ_TAG
);
580 while (offset
< size
&& buf
[offset
] != '\n') {
581 int new_offset
= offset
+ 1;
583 while (new_offset
< size
&& buf
[new_offset
++] != '\n')
585 if (skip_prefix(buf
+ offset
, "tagger ", &ident
))
586 show_tagger(ident
, rev
);
591 fwrite(buf
+ offset
, size
- offset
, 1, rev
->diffopt
.file
);
596 static int show_tree_object(const struct object_id
*oid
,
598 const char *pathname
, unsigned mode
, int stage
, void *context
)
600 FILE *file
= context
;
601 fprintf(file
, "%s%s\n", pathname
, S_ISDIR(mode
) ? "/" : "");
605 static void show_setup_revisions_tweak(struct rev_info
*rev
,
606 struct setup_revision_opt
*opt
)
608 if (rev
->ignore_merges
< 0) {
609 /* There was no "-m" variant on the command line */
610 rev
->ignore_merges
= 0;
611 if (!rev
->first_parent_only
&& !rev
->combine_merges
) {
612 /* No "--first-parent", "-c", or "--cc" */
613 rev
->combine_merges
= 1;
614 rev
->dense_combined_merges
= 1;
617 if (!rev
->diffopt
.output_format
)
618 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
621 int cmd_show(int argc
, const char **argv
, const char *prefix
)
624 struct object_array_entry
*objects
;
625 struct setup_revision_opt opt
;
626 struct pathspec match_all
;
627 int i
, count
, ret
= 0;
630 git_config(git_log_config
, NULL
);
632 memset(&match_all
, 0, sizeof(match_all
));
633 repo_init_revisions(the_repository
, &rev
, prefix
);
635 rev
.always_show_header
= 1;
636 rev
.no_walk
= REVISION_WALK_NO_WALK_SORTED
;
637 rev
.diffopt
.stat_width
= -1; /* Scale to real terminal size */
639 memset(&opt
, 0, sizeof(opt
));
641 opt
.tweak
= show_setup_revisions_tweak
;
642 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
645 return cmd_log_walk(&rev
);
647 count
= rev
.pending
.nr
;
648 objects
= rev
.pending
.objects
;
649 for (i
= 0; i
< count
&& !ret
; i
++) {
650 struct object
*o
= objects
[i
].item
;
651 const char *name
= objects
[i
].name
;
654 ret
= show_blob_object(&o
->oid
, &rev
, name
);
657 struct tag
*t
= (struct tag
*)o
;
658 struct object_id
*oid
= get_tagged_oid(t
);
662 fprintf(rev
.diffopt
.file
, "%stag %s%s\n",
663 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
665 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
666 ret
= show_tag_object(&o
->oid
, &rev
);
670 o
= parse_object(the_repository
, oid
);
672 ret
= error(_("could not read object %s"),
681 fprintf(rev
.diffopt
.file
, "%stree %s%s\n\n",
682 diff_get_color_opt(&rev
.diffopt
, DIFF_COMMIT
),
684 diff_get_color_opt(&rev
.diffopt
, DIFF_RESET
));
685 read_tree_recursive(the_repository
, (struct tree
*)o
, "",
686 0, 0, &match_all
, show_tree_object
,
691 rev
.pending
.nr
= rev
.pending
.alloc
= 0;
692 rev
.pending
.objects
= NULL
;
693 add_object_array(o
, name
, &rev
.pending
);
694 ret
= cmd_log_walk(&rev
);
697 ret
= error(_("unknown type: %d"), o
->type
);
705 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
707 int cmd_log_reflog(int argc
, const char **argv
, const char *prefix
)
710 struct setup_revision_opt opt
;
713 git_config(git_log_config
, NULL
);
715 repo_init_revisions(the_repository
, &rev
, prefix
);
716 init_reflog_walk(&rev
.reflog_info
);
717 rev
.verbose_header
= 1;
718 memset(&opt
, 0, sizeof(opt
));
720 cmd_log_init_defaults(&rev
);
721 rev
.abbrev_commit
= 1;
722 rev
.commit_format
= CMIT_FMT_ONELINE
;
723 rev
.use_terminator
= 1;
724 rev
.always_show_header
= 1;
725 cmd_log_init_finish(argc
, argv
, prefix
, &rev
, &opt
);
727 return cmd_log_walk(&rev
);
730 static void log_setup_revisions_tweak(struct rev_info
*rev
,
731 struct setup_revision_opt
*opt
)
733 if (rev
->diffopt
.flags
.default_follow_renames
&&
734 rev
->prune_data
.nr
== 1)
735 rev
->diffopt
.flags
.follow_renames
= 1;
737 /* Turn --cc/-c into -p --cc/-c when -p was not given */
738 if (!rev
->diffopt
.output_format
&& rev
->combine_merges
)
739 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
741 if (rev
->first_parent_only
&& rev
->ignore_merges
< 0)
742 rev
->ignore_merges
= 0;
745 int cmd_log(int argc
, const char **argv
, const char *prefix
)
748 struct setup_revision_opt opt
;
751 git_config(git_log_config
, NULL
);
753 repo_init_revisions(the_repository
, &rev
, prefix
);
754 rev
.always_show_header
= 1;
755 memset(&opt
, 0, sizeof(opt
));
757 opt
.revarg_opt
= REVARG_COMMITTISH
;
758 opt
.tweak
= log_setup_revisions_tweak
;
759 cmd_log_init(argc
, argv
, prefix
, &rev
, &opt
);
760 return cmd_log_walk(&rev
);
765 static const char *fmt_patch_suffix
= ".patch";
766 static int numbered
= 0;
767 static int auto_number
= 1;
769 static char *default_attach
= NULL
;
771 static struct string_list extra_hdr
= STRING_LIST_INIT_NODUP
;
772 static struct string_list extra_to
= STRING_LIST_INIT_NODUP
;
773 static struct string_list extra_cc
= STRING_LIST_INIT_NODUP
;
775 static void add_header(const char *value
)
777 struct string_list_item
*item
;
778 int len
= strlen(value
);
779 while (len
&& value
[len
- 1] == '\n')
782 if (!strncasecmp(value
, "to: ", 4)) {
783 item
= string_list_append(&extra_to
, value
+ 4);
785 } else if (!strncasecmp(value
, "cc: ", 4)) {
786 item
= string_list_append(&extra_cc
, value
+ 4);
789 item
= string_list_append(&extra_hdr
, value
);
792 item
->string
[len
] = '\0';
808 enum cover_from_description
{
815 enum auto_base_setting
{
821 static enum thread_level thread
;
822 static int do_signoff
;
823 static enum auto_base_setting auto_base
;
825 static const char *signature
= git_version_string
;
826 static const char *signature_file
;
827 static enum cover_setting config_cover_letter
;
828 static const char *config_output_directory
;
829 static enum cover_from_description cover_from_description_mode
= COVER_FROM_MESSAGE
;
830 static int show_notes
;
831 static struct display_notes_opt notes_opt
;
833 static enum cover_from_description
parse_cover_from_description(const char *arg
)
835 if (!arg
|| !strcmp(arg
, "default"))
836 return COVER_FROM_MESSAGE
;
837 else if (!strcmp(arg
, "none"))
838 return COVER_FROM_NONE
;
839 else if (!strcmp(arg
, "message"))
840 return COVER_FROM_MESSAGE
;
841 else if (!strcmp(arg
, "subject"))
842 return COVER_FROM_SUBJECT
;
843 else if (!strcmp(arg
, "auto"))
844 return COVER_FROM_AUTO
;
846 die(_("%s: invalid cover from description mode"), arg
);
849 static int git_format_config(const char *var
, const char *value
, void *cb
)
851 if (!strcmp(var
, "format.headers")) {
853 die(_("format.headers without value"));
857 if (!strcmp(var
, "format.suffix"))
858 return git_config_string(&fmt_patch_suffix
, var
, value
);
859 if (!strcmp(var
, "format.to")) {
861 return config_error_nonbool(var
);
862 string_list_append(&extra_to
, value
);
865 if (!strcmp(var
, "format.cc")) {
867 return config_error_nonbool(var
);
868 string_list_append(&extra_cc
, value
);
871 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff") ||
872 !strcmp(var
, "color.ui") || !strcmp(var
, "diff.submodule")) {
875 if (!strcmp(var
, "format.numbered")) {
876 if (value
&& !strcasecmp(value
, "auto")) {
880 numbered
= git_config_bool(var
, value
);
881 auto_number
= auto_number
&& numbered
;
884 if (!strcmp(var
, "format.attach")) {
886 default_attach
= xstrdup(value
);
888 default_attach
= xstrdup(git_version_string
);
891 if (!strcmp(var
, "format.thread")) {
892 if (value
&& !strcasecmp(value
, "deep")) {
893 thread
= THREAD_DEEP
;
896 if (value
&& !strcasecmp(value
, "shallow")) {
897 thread
= THREAD_SHALLOW
;
900 thread
= git_config_bool(var
, value
) ? THREAD_SHALLOW
: THREAD_UNSET
;
903 if (!strcmp(var
, "format.signoff")) {
904 do_signoff
= git_config_bool(var
, value
);
907 if (!strcmp(var
, "format.signature"))
908 return git_config_string(&signature
, var
, value
);
909 if (!strcmp(var
, "format.signaturefile"))
910 return git_config_pathname(&signature_file
, var
, value
);
911 if (!strcmp(var
, "format.coverletter")) {
912 if (value
&& !strcasecmp(value
, "auto")) {
913 config_cover_letter
= COVER_AUTO
;
916 config_cover_letter
= git_config_bool(var
, value
) ? COVER_ON
: COVER_OFF
;
919 if (!strcmp(var
, "format.outputdirectory"))
920 return git_config_string(&config_output_directory
, var
, value
);
921 if (!strcmp(var
, "format.useautobase")) {
922 if (value
&& !strcasecmp(value
, "whenAble")) {
923 auto_base
= AUTO_BASE_WHEN_ABLE
;
926 auto_base
= git_config_bool(var
, value
) ? AUTO_BASE_ALWAYS
: AUTO_BASE_NEVER
;
929 if (!strcmp(var
, "format.from")) {
930 int b
= git_parse_maybe_bool(value
);
933 from
= xstrdup(value
);
935 from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
940 if (!strcmp(var
, "format.notes")) {
941 int b
= git_parse_maybe_bool(value
);
943 enable_ref_display_notes(¬es_opt
, &show_notes
, value
);
945 enable_default_display_notes(¬es_opt
, &show_notes
);
947 disable_display_notes(¬es_opt
, &show_notes
);
950 if (!strcmp(var
, "format.coverfromdescription")) {
951 cover_from_description_mode
= parse_cover_from_description(value
);
955 return git_log_config(var
, value
, cb
);
958 static const char *output_directory
= NULL
;
959 static int outdir_offset
;
961 static int open_next_file(struct commit
*commit
, const char *subject
,
962 struct rev_info
*rev
, int quiet
)
964 struct strbuf filename
= STRBUF_INIT
;
966 if (output_directory
) {
967 strbuf_addstr(&filename
, output_directory
);
968 strbuf_complete(&filename
, '/');
971 if (rev
->numbered_files
)
972 strbuf_addf(&filename
, "%d", rev
->nr
);
974 fmt_output_commit(&filename
, commit
, rev
);
976 fmt_output_subject(&filename
, subject
, rev
);
979 printf("%s\n", filename
.buf
+ outdir_offset
);
981 if ((rev
->diffopt
.file
= fopen(filename
.buf
, "w")) == NULL
) {
982 error_errno(_("cannot open patch file %s"), filename
.buf
);
983 strbuf_release(&filename
);
987 strbuf_release(&filename
);
991 static void get_patch_ids(struct rev_info
*rev
, struct patch_ids
*ids
)
993 struct rev_info check_rev
;
994 struct commit
*commit
, *c1
, *c2
;
995 struct object
*o1
, *o2
;
996 unsigned flags1
, flags2
;
998 if (rev
->pending
.nr
!= 2)
999 die(_("need exactly one range"));
1001 o1
= rev
->pending
.objects
[0].item
;
1002 o2
= rev
->pending
.objects
[1].item
;
1005 c1
= lookup_commit_reference(the_repository
, &o1
->oid
);
1006 c2
= lookup_commit_reference(the_repository
, &o2
->oid
);
1008 if ((flags1
& UNINTERESTING
) == (flags2
& UNINTERESTING
))
1009 die(_("not a range"));
1011 init_patch_ids(the_repository
, ids
);
1013 /* given a range a..b get all patch ids for b..a */
1014 repo_init_revisions(the_repository
, &check_rev
, rev
->prefix
);
1015 check_rev
.max_parents
= 1;
1016 o1
->flags
^= UNINTERESTING
;
1017 o2
->flags
^= UNINTERESTING
;
1018 add_pending_object(&check_rev
, o1
, "o1");
1019 add_pending_object(&check_rev
, o2
, "o2");
1020 if (prepare_revision_walk(&check_rev
))
1021 die(_("revision walk setup failed"));
1023 while ((commit
= get_revision(&check_rev
)) != NULL
) {
1024 add_commit_patch_id(commit
, ids
);
1027 /* reset for next revision walk */
1028 clear_commit_marks(c1
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1029 clear_commit_marks(c2
, SEEN
| UNINTERESTING
| SHOWN
| ADDED
);
1034 static void gen_message_id(struct rev_info
*info
, char *base
)
1036 struct strbuf buf
= STRBUF_INIT
;
1037 strbuf_addf(&buf
, "%s.%"PRItime
".git.%s", base
,
1038 (timestamp_t
) time(NULL
),
1039 git_committer_info(IDENT_NO_NAME
|IDENT_NO_DATE
|IDENT_STRICT
));
1040 info
->message_id
= strbuf_detach(&buf
, NULL
);
1043 static void print_signature(FILE *file
)
1045 if (!signature
|| !*signature
)
1048 fprintf(file
, "-- \n%s", signature
);
1049 if (signature
[strlen(signature
)-1] != '\n')
1054 static char *find_branch_name(struct rev_info
*rev
)
1056 int i
, positive
= -1;
1057 struct object_id branch_oid
;
1058 const struct object_id
*tip_oid
;
1059 const char *ref
, *v
;
1060 char *full_ref
, *branch
= NULL
;
1062 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
1063 if (rev
->cmdline
.rev
[i
].flags
& UNINTERESTING
)
1072 ref
= rev
->cmdline
.rev
[positive
].name
;
1073 tip_oid
= &rev
->cmdline
.rev
[positive
].item
->oid
;
1074 if (dwim_ref(ref
, strlen(ref
), &branch_oid
, &full_ref
, 0) &&
1075 skip_prefix(full_ref
, "refs/heads/", &v
) &&
1076 oideq(tip_oid
, &branch_oid
))
1077 branch
= xstrdup(v
);
1082 static void show_diffstat(struct rev_info
*rev
,
1083 struct commit
*origin
, struct commit
*head
)
1085 struct diff_options opts
;
1087 memcpy(&opts
, &rev
->diffopt
, sizeof(opts
));
1088 opts
.output_format
= DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1089 diff_setup_done(&opts
);
1091 diff_tree_oid(get_commit_tree_oid(origin
),
1092 get_commit_tree_oid(head
),
1094 diffcore_std(&opts
);
1097 fprintf(rev
->diffopt
.file
, "\n");
1100 static void prepare_cover_text(struct pretty_print_context
*pp
,
1101 const char *branch_name
,
1103 const char *encoding
,
1106 const char *subject
= "*** SUBJECT HERE ***";
1107 const char *body
= "*** BLURB HERE ***";
1108 struct strbuf description_sb
= STRBUF_INIT
;
1109 struct strbuf subject_sb
= STRBUF_INIT
;
1111 if (cover_from_description_mode
== COVER_FROM_NONE
)
1114 if (branch_name
&& *branch_name
)
1115 read_branch_desc(&description_sb
, branch_name
);
1116 if (!description_sb
.len
)
1119 if (cover_from_description_mode
== COVER_FROM_SUBJECT
||
1120 cover_from_description_mode
== COVER_FROM_AUTO
)
1121 body
= format_subject(&subject_sb
, description_sb
.buf
, " ");
1123 if (cover_from_description_mode
== COVER_FROM_MESSAGE
||
1124 (cover_from_description_mode
== COVER_FROM_AUTO
&&
1125 subject_sb
.len
> COVER_FROM_AUTO_MAX_SUBJECT_LEN
))
1126 body
= description_sb
.buf
;
1128 subject
= subject_sb
.buf
;
1131 pp_title_line(pp
, &subject
, sb
, encoding
, need_8bit_cte
);
1132 pp_remainder(pp
, &body
, sb
, 0);
1134 strbuf_release(&description_sb
);
1135 strbuf_release(&subject_sb
);
1138 static int get_notes_refs(struct string_list_item
*item
, void *arg
)
1140 strvec_pushf(arg
, "--notes=%s", item
->string
);
1144 static void get_notes_args(struct strvec
*arg
, struct rev_info
*rev
)
1146 if (!rev
->show_notes
) {
1147 strvec_push(arg
, "--no-notes");
1148 } else if (rev
->notes_opt
.use_default_notes
> 0 ||
1149 (rev
->notes_opt
.use_default_notes
== -1 &&
1150 !rev
->notes_opt
.extra_notes_refs
.nr
)) {
1151 strvec_push(arg
, "--notes");
1153 for_each_string_list(&rev
->notes_opt
.extra_notes_refs
, get_notes_refs
, arg
);
1157 static void make_cover_letter(struct rev_info
*rev
, int use_stdout
,
1158 struct commit
*origin
,
1159 int nr
, struct commit
**list
,
1160 const char *branch_name
,
1163 const char *committer
;
1164 struct shortlog log
;
1165 struct strbuf sb
= STRBUF_INIT
;
1167 const char *encoding
= "UTF-8";
1168 int need_8bit_cte
= 0;
1169 struct pretty_print_context pp
= {0};
1170 struct commit
*head
= list
[0];
1172 if (!cmit_fmt_is_mail(rev
->commit_format
))
1173 die(_("cover letter needs email format"));
1175 committer
= git_committer_info(0);
1178 open_next_file(NULL
, rev
->numbered_files
? NULL
: "cover-letter", rev
, quiet
))
1179 die(_("failed to create cover-letter file"));
1181 log_write_email_headers(rev
, head
, &pp
.after_subject
, &need_8bit_cte
, 0);
1183 for (i
= 0; !need_8bit_cte
&& i
< nr
; i
++) {
1184 const char *buf
= get_commit_buffer(list
[i
], NULL
);
1185 if (has_non_ascii(buf
))
1187 unuse_commit_buffer(list
[i
], buf
);
1191 branch_name
= find_branch_name(rev
);
1193 pp
.fmt
= CMIT_FMT_EMAIL
;
1194 pp
.date_mode
.type
= DATE_RFC2822
;
1196 pp
.print_email_subject
= 1;
1197 pp_user_info(&pp
, NULL
, &sb
, committer
, encoding
);
1198 prepare_cover_text(&pp
, branch_name
, &sb
, encoding
, need_8bit_cte
);
1199 fprintf(rev
->diffopt
.file
, "%s\n", sb
.buf
);
1201 strbuf_release(&sb
);
1203 shortlog_init(&log
);
1205 log
.wrap
= MAIL_DEFAULT_WRAP
;
1208 log
.file
= rev
->diffopt
.file
;
1209 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
1210 for (i
= 0; i
< nr
; i
++)
1211 shortlog_add_commit(&log
, list
[i
]);
1213 shortlog_output(&log
);
1215 /* We can only do diffstat with a unique reference point */
1217 show_diffstat(rev
, origin
, head
);
1219 if (rev
->idiff_oid1
) {
1220 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->idiff_title
);
1221 show_interdiff(rev
->idiff_oid1
, rev
->idiff_oid2
, 0,
1227 * Pass minimum required diff-options to range-diff; others
1228 * can be added later if deemed desirable.
1230 struct diff_options opts
;
1231 struct strvec other_arg
= STRVEC_INIT
;
1233 opts
.file
= rev
->diffopt
.file
;
1234 opts
.use_color
= rev
->diffopt
.use_color
;
1235 diff_setup_done(&opts
);
1236 fprintf_ln(rev
->diffopt
.file
, "%s", rev
->rdiff_title
);
1237 get_notes_args(&other_arg
, rev
);
1238 show_range_diff(rev
->rdiff1
, rev
->rdiff2
,
1239 rev
->creation_factor
, 1, &opts
, &other_arg
);
1240 strvec_clear(&other_arg
);
1244 static const char *clean_message_id(const char *msg_id
)
1247 const char *a
, *z
, *m
;
1250 while ((ch
= *m
) && (isspace(ch
) || (ch
== '<')))
1255 if (!isspace(ch
) && (ch
!= '>'))
1260 die(_("insane in-reply-to: %s"), msg_id
);
1263 return xmemdupz(a
, z
- a
);
1266 static const char *set_outdir(const char *prefix
, const char *output_directory
)
1268 if (output_directory
&& is_absolute_path(output_directory
))
1269 return output_directory
;
1271 if (!prefix
|| !*prefix
) {
1272 if (output_directory
)
1273 return output_directory
;
1274 /* The user did not explicitly ask for "./" */
1279 outdir_offset
= strlen(prefix
);
1280 if (!output_directory
)
1283 return prefix_filename(prefix
, output_directory
);
1286 static const char * const builtin_format_patch_usage
[] = {
1287 N_("git format-patch [<options>] [<since> | <revision-range>]"),
1291 static int keep_subject
= 0;
1293 static int keep_callback(const struct option
*opt
, const char *arg
, int unset
)
1295 BUG_ON_OPT_NEG(unset
);
1296 BUG_ON_OPT_ARG(arg
);
1297 ((struct rev_info
*)opt
->value
)->total
= -1;
1302 static int subject_prefix
= 0;
1304 static int subject_prefix_callback(const struct option
*opt
, const char *arg
,
1307 BUG_ON_OPT_NEG(unset
);
1309 ((struct rev_info
*)opt
->value
)->subject_prefix
= arg
;
1313 static int rfc_callback(const struct option
*opt
, const char *arg
, int unset
)
1315 BUG_ON_OPT_NEG(unset
);
1316 BUG_ON_OPT_ARG(arg
);
1317 return subject_prefix_callback(opt
, "RFC PATCH", unset
);
1320 static int numbered_cmdline_opt
= 0;
1322 static int numbered_callback(const struct option
*opt
, const char *arg
,
1325 BUG_ON_OPT_ARG(arg
);
1326 *(int *)opt
->value
= numbered_cmdline_opt
= unset
? 0 : 1;
1332 static int no_numbered_callback(const struct option
*opt
, const char *arg
,
1335 BUG_ON_OPT_NEG(unset
);
1336 return numbered_callback(opt
, arg
, 1);
1339 static int output_directory_callback(const struct option
*opt
, const char *arg
,
1342 const char **dir
= (const char **)opt
->value
;
1343 BUG_ON_OPT_NEG(unset
);
1345 die(_("two output directories?"));
1350 static int thread_callback(const struct option
*opt
, const char *arg
, int unset
)
1352 enum thread_level
*thread
= (enum thread_level
*)opt
->value
;
1354 *thread
= THREAD_UNSET
;
1355 else if (!arg
|| !strcmp(arg
, "shallow"))
1356 *thread
= THREAD_SHALLOW
;
1357 else if (!strcmp(arg
, "deep"))
1358 *thread
= THREAD_DEEP
;
1360 * Please update _git_formatpatch() in git-completion.bash
1361 * when you add new options.
1368 static int attach_callback(const struct option
*opt
, const char *arg
, int unset
)
1370 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1372 rev
->mime_boundary
= NULL
;
1374 rev
->mime_boundary
= arg
;
1376 rev
->mime_boundary
= git_version_string
;
1377 rev
->no_inline
= unset
? 0 : 1;
1381 static int inline_callback(const struct option
*opt
, const char *arg
, int unset
)
1383 struct rev_info
*rev
= (struct rev_info
*)opt
->value
;
1385 rev
->mime_boundary
= NULL
;
1387 rev
->mime_boundary
= arg
;
1389 rev
->mime_boundary
= git_version_string
;
1394 static int header_callback(const struct option
*opt
, const char *arg
, int unset
)
1397 string_list_clear(&extra_hdr
, 0);
1398 string_list_clear(&extra_to
, 0);
1399 string_list_clear(&extra_cc
, 0);
1406 static int to_callback(const struct option
*opt
, const char *arg
, int unset
)
1409 string_list_clear(&extra_to
, 0);
1411 string_list_append(&extra_to
, arg
);
1415 static int cc_callback(const struct option
*opt
, const char *arg
, int unset
)
1418 string_list_clear(&extra_cc
, 0);
1420 string_list_append(&extra_cc
, arg
);
1424 static int from_callback(const struct option
*opt
, const char *arg
, int unset
)
1426 char **from
= opt
->value
;
1433 *from
= xstrdup(arg
);
1435 *from
= xstrdup(git_committer_info(IDENT_NO_DATE
));
1439 static int base_callback(const struct option
*opt
, const char *arg
, int unset
)
1441 const char **base_commit
= opt
->value
;
1444 auto_base
= AUTO_BASE_NEVER
;
1445 *base_commit
= NULL
;
1446 } else if (!strcmp(arg
, "auto")) {
1447 auto_base
= AUTO_BASE_ALWAYS
;
1448 *base_commit
= NULL
;
1450 auto_base
= AUTO_BASE_NEVER
;
1456 struct base_tree_info
{
1457 struct object_id base_commit
;
1458 int nr_patch_id
, alloc_patch_id
;
1459 struct object_id
*patch_id
;
1462 static struct commit
*get_base_commit(const char *base_commit
,
1463 struct commit
**list
,
1466 struct commit
*base
= NULL
;
1467 struct commit
**rev
;
1468 int i
= 0, rev_nr
= 0, auto_select
, die_on_failure
;
1470 switch (auto_base
) {
1471 case AUTO_BASE_NEVER
:
1476 /* no base information is requested */
1480 case AUTO_BASE_ALWAYS
:
1481 case AUTO_BASE_WHEN_ABLE
:
1483 BUG("requested automatic base selection but a commit was provided");
1486 die_on_failure
= auto_base
== AUTO_BASE_ALWAYS
;
1490 BUG("unexpected automatic base selection method");
1494 base
= lookup_commit_reference_by_name(base_commit
);
1496 die(_("unknown commit %s"), base_commit
);
1498 struct branch
*curr_branch
= branch_get(NULL
);
1499 const char *upstream
= branch_get_upstream(curr_branch
, NULL
);
1501 struct commit_list
*base_list
;
1502 struct commit
*commit
;
1503 struct object_id oid
;
1505 if (get_oid(upstream
, &oid
)) {
1507 die(_("failed to resolve '%s' as a valid ref"), upstream
);
1511 commit
= lookup_commit_or_die(&oid
, "upstream base");
1512 base_list
= get_merge_bases_many(commit
, total
, list
);
1513 /* There should be one and only one merge base. */
1514 if (!base_list
|| base_list
->next
) {
1515 if (die_on_failure
) {
1516 die(_("could not find exact merge base"));
1518 free_commit_list(base_list
);
1522 base
= base_list
->item
;
1523 free_commit_list(base_list
);
1526 die(_("failed to get upstream, if you want to record base commit automatically,\n"
1527 "please use git branch --set-upstream-to to track a remote branch.\n"
1528 "Or you could specify base commit by --base=<base-commit-id> manually"));
1534 ALLOC_ARRAY(rev
, total
);
1535 for (i
= 0; i
< total
; i
++)
1540 * Get merge base through pair-wise computations
1541 * and store it in rev[0].
1543 while (rev_nr
> 1) {
1544 for (i
= 0; i
< rev_nr
/ 2; i
++) {
1545 struct commit_list
*merge_base
;
1546 merge_base
= get_merge_bases(rev
[2 * i
], rev
[2 * i
+ 1]);
1547 if (!merge_base
|| merge_base
->next
) {
1548 if (die_on_failure
) {
1549 die(_("failed to find exact merge base"));
1556 rev
[i
] = merge_base
->item
;
1560 rev
[i
] = rev
[2 * i
];
1561 rev_nr
= DIV_ROUND_UP(rev_nr
, 2);
1564 if (!in_merge_bases(base
, rev
[0])) {
1565 if (die_on_failure
) {
1566 die(_("base commit should be the ancestor of revision list"));
1573 for (i
= 0; i
< total
; i
++) {
1574 if (base
== list
[i
]) {
1575 if (die_on_failure
) {
1576 die(_("base commit shouldn't be in revision list"));
1588 define_commit_slab(commit_base
, int);
1590 static void prepare_bases(struct base_tree_info
*bases
,
1591 struct commit
*base
,
1592 struct commit
**list
,
1595 struct commit
*commit
;
1596 struct rev_info revs
;
1597 struct diff_options diffopt
;
1598 struct commit_base commit_base
;
1604 init_commit_base(&commit_base
);
1605 repo_diff_setup(the_repository
, &diffopt
);
1606 diffopt
.flags
.recursive
= 1;
1607 diff_setup_done(&diffopt
);
1609 oidcpy(&bases
->base_commit
, &base
->object
.oid
);
1611 repo_init_revisions(the_repository
, &revs
, NULL
);
1612 revs
.max_parents
= 1;
1613 revs
.topo_order
= 1;
1614 for (i
= 0; i
< total
; i
++) {
1615 list
[i
]->object
.flags
&= ~UNINTERESTING
;
1616 add_pending_object(&revs
, &list
[i
]->object
, "rev_list");
1617 *commit_base_at(&commit_base
, list
[i
]) = 1;
1619 base
->object
.flags
|= UNINTERESTING
;
1620 add_pending_object(&revs
, &base
->object
, "base");
1622 if (prepare_revision_walk(&revs
))
1623 die(_("revision walk setup failed"));
1625 * Traverse the commits list, get prerequisite patch ids
1626 * and stuff them in bases structure.
1628 while ((commit
= get_revision(&revs
)) != NULL
) {
1629 struct object_id oid
;
1630 struct object_id
*patch_id
;
1631 if (*commit_base_at(&commit_base
, commit
))
1633 if (commit_patch_id(commit
, &diffopt
, &oid
, 0, 1))
1634 die(_("cannot get patch id"));
1635 ALLOC_GROW(bases
->patch_id
, bases
->nr_patch_id
+ 1, bases
->alloc_patch_id
);
1636 patch_id
= bases
->patch_id
+ bases
->nr_patch_id
;
1637 oidcpy(patch_id
, &oid
);
1638 bases
->nr_patch_id
++;
1640 clear_commit_base(&commit_base
);
1643 static void print_bases(struct base_tree_info
*bases
, FILE *file
)
1647 /* Only do this once, either for the cover or for the first one */
1648 if (is_null_oid(&bases
->base_commit
))
1651 /* Show the base commit */
1652 fprintf(file
, "\nbase-commit: %s\n", oid_to_hex(&bases
->base_commit
));
1654 /* Show the prerequisite patches */
1655 for (i
= bases
->nr_patch_id
- 1; i
>= 0; i
--)
1656 fprintf(file
, "prerequisite-patch-id: %s\n", oid_to_hex(&bases
->patch_id
[i
]));
1658 free(bases
->patch_id
);
1659 bases
->nr_patch_id
= 0;
1660 bases
->alloc_patch_id
= 0;
1661 oidclr(&bases
->base_commit
);
1664 static const char *diff_title(struct strbuf
*sb
, int reroll_count
,
1665 const char *generic
, const char *rerolled
)
1667 if (reroll_count
<= 0)
1668 strbuf_addstr(sb
, generic
);
1669 else /* RFC may be v0, so allow -v1 to diff against v0 */
1670 strbuf_addf(sb
, rerolled
, reroll_count
- 1);
1674 static void infer_range_diff_ranges(struct strbuf
*r1
,
1677 struct commit
*origin
,
1678 struct commit
*head
)
1680 const char *head_oid
= oid_to_hex(&head
->object
.oid
);
1681 int prev_is_range
= !!strstr(prev
, "..");
1684 strbuf_addstr(r1
, prev
);
1686 strbuf_addf(r1
, "%s..%s", head_oid
, prev
);
1689 strbuf_addf(r2
, "%s..%s", oid_to_hex(&origin
->object
.oid
), head_oid
);
1690 else if (prev_is_range
)
1691 die(_("failed to infer range-diff origin of current series"));
1693 warning(_("using '%s' as range-diff origin of current series"), prev
);
1694 strbuf_addf(r2
, "%s..%s", prev
, head_oid
);
1698 int cmd_format_patch(int argc
, const char **argv
, const char *prefix
)
1700 struct commit
*commit
;
1701 struct commit
**list
= NULL
;
1702 struct rev_info rev
;
1703 struct setup_revision_opt s_r_opt
;
1704 int nr
= 0, total
, i
;
1706 int start_number
= -1;
1707 int just_numbers
= 0;
1708 int ignore_if_in_upstream
= 0;
1709 int cover_letter
= -1;
1710 int boundary_count
= 0;
1711 int no_binary_diff
= 0;
1712 int zero_commit
= 0;
1713 struct commit
*origin
= NULL
;
1714 const char *in_reply_to
= NULL
;
1715 struct patch_ids ids
;
1716 struct strbuf buf
= STRBUF_INIT
;
1717 int use_patch_format
= 0;
1719 int reroll_count
= -1;
1720 char *cover_from_description_arg
= NULL
;
1721 char *branch_name
= NULL
;
1722 char *base_commit
= NULL
;
1723 struct base_tree_info bases
;
1724 struct commit
*base
;
1725 int show_progress
= 0;
1726 struct progress
*progress
= NULL
;
1727 struct oid_array idiff_prev
= OID_ARRAY_INIT
;
1728 struct strbuf idiff_title
= STRBUF_INIT
;
1729 const char *rdiff_prev
= NULL
;
1730 struct strbuf rdiff1
= STRBUF_INIT
;
1731 struct strbuf rdiff2
= STRBUF_INIT
;
1732 struct strbuf rdiff_title
= STRBUF_INIT
;
1733 int creation_factor
= -1;
1735 const struct option builtin_format_patch_options
[] = {
1736 OPT_CALLBACK_F('n', "numbered", &numbered
, NULL
,
1737 N_("use [PATCH n/m] even with a single patch"),
1738 PARSE_OPT_NOARG
, numbered_callback
),
1739 OPT_CALLBACK_F('N', "no-numbered", &numbered
, NULL
,
1740 N_("use [PATCH] even with multiple patches"),
1741 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, no_numbered_callback
),
1742 OPT_BOOL('s', "signoff", &do_signoff
, N_("add Signed-off-by:")),
1743 OPT_BOOL(0, "stdout", &use_stdout
,
1744 N_("print patches to standard out")),
1745 OPT_BOOL(0, "cover-letter", &cover_letter
,
1746 N_("generate a cover letter")),
1747 OPT_BOOL(0, "numbered-files", &just_numbers
,
1748 N_("use simple number sequence for output file names")),
1749 OPT_STRING(0, "suffix", &fmt_patch_suffix
, N_("sfx"),
1750 N_("use <sfx> instead of '.patch'")),
1751 OPT_INTEGER(0, "start-number", &start_number
,
1752 N_("start numbering patches at <n> instead of 1")),
1753 OPT_INTEGER('v', "reroll-count", &reroll_count
,
1754 N_("mark the series as Nth re-roll")),
1755 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max
,
1756 N_("max length of output filename")),
1757 OPT_CALLBACK_F(0, "rfc", &rev
, NULL
,
1758 N_("Use [RFC PATCH] instead of [PATCH]"),
1759 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, rfc_callback
),
1760 OPT_STRING(0, "cover-from-description", &cover_from_description_arg
,
1761 N_("cover-from-description-mode"),
1762 N_("generate parts of a cover letter based on a branch's description")),
1763 OPT_CALLBACK_F(0, "subject-prefix", &rev
, N_("prefix"),
1764 N_("Use [<prefix>] instead of [PATCH]"),
1765 PARSE_OPT_NONEG
, subject_prefix_callback
),
1766 OPT_CALLBACK_F('o', "output-directory", &output_directory
,
1767 N_("dir"), N_("store resulting files in <dir>"),
1768 PARSE_OPT_NONEG
, output_directory_callback
),
1769 OPT_CALLBACK_F('k', "keep-subject", &rev
, NULL
,
1770 N_("don't strip/add [PATCH]"),
1771 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, keep_callback
),
1772 OPT_BOOL(0, "no-binary", &no_binary_diff
,
1773 N_("don't output binary diffs")),
1774 OPT_BOOL(0, "zero-commit", &zero_commit
,
1775 N_("output all-zero hash in From header")),
1776 OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream
,
1777 N_("don't include a patch matching a commit upstream")),
1778 OPT_SET_INT_F('p', "no-stat", &use_patch_format
,
1779 N_("show patch format instead of default (patch + stat)"),
1780 1, PARSE_OPT_NONEG
),
1781 OPT_GROUP(N_("Messaging")),
1782 OPT_CALLBACK(0, "add-header", NULL
, N_("header"),
1783 N_("add email header"), header_callback
),
1784 OPT_CALLBACK(0, "to", NULL
, N_("email"), N_("add To: header"), to_callback
),
1785 OPT_CALLBACK(0, "cc", NULL
, N_("email"), N_("add Cc: header"), cc_callback
),
1786 OPT_CALLBACK_F(0, "from", &from
, N_("ident"),
1787 N_("set From address to <ident> (or committer ident if absent)"),
1788 PARSE_OPT_OPTARG
, from_callback
),
1789 OPT_STRING(0, "in-reply-to", &in_reply_to
, N_("message-id"),
1790 N_("make first mail a reply to <message-id>")),
1791 OPT_CALLBACK_F(0, "attach", &rev
, N_("boundary"),
1792 N_("attach the patch"), PARSE_OPT_OPTARG
,
1794 OPT_CALLBACK_F(0, "inline", &rev
, N_("boundary"),
1795 N_("inline the patch"),
1796 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
1798 OPT_CALLBACK_F(0, "thread", &thread
, N_("style"),
1799 N_("enable message threading, styles: shallow, deep"),
1800 PARSE_OPT_OPTARG
, thread_callback
),
1801 OPT_STRING(0, "signature", &signature
, N_("signature"),
1802 N_("add a signature")),
1803 OPT_CALLBACK_F(0, "base", &base_commit
, N_("base-commit"),
1804 N_("add prerequisite tree info to the patch series"),
1806 OPT_FILENAME(0, "signature-file", &signature_file
,
1807 N_("add a signature from a file")),
1808 OPT__QUIET(&quiet
, N_("don't print the patch filenames")),
1809 OPT_BOOL(0, "progress", &show_progress
,
1810 N_("show progress while generating patches")),
1811 OPT_CALLBACK(0, "interdiff", &idiff_prev
, N_("rev"),
1812 N_("show changes against <rev> in cover letter or single patch"),
1813 parse_opt_object_name
),
1814 OPT_STRING(0, "range-diff", &rdiff_prev
, N_("refspec"),
1815 N_("show changes against <refspec> in cover letter or single patch")),
1816 OPT_INTEGER(0, "creation-factor", &creation_factor
,
1817 N_("percentage by which creation is weighted")),
1821 extra_hdr
.strdup_strings
= 1;
1822 extra_to
.strdup_strings
= 1;
1823 extra_cc
.strdup_strings
= 1;
1824 init_log_defaults();
1825 init_display_notes(¬es_opt
);
1826 git_config(git_format_config
, NULL
);
1827 repo_init_revisions(the_repository
, &rev
, prefix
);
1828 rev
.show_notes
= show_notes
;
1829 memcpy(&rev
.notes_opt
, ¬es_opt
, sizeof(notes_opt
));
1830 rev
.commit_format
= CMIT_FMT_EMAIL
;
1831 rev
.encode_email_headers
= default_encode_email_headers
;
1832 rev
.expand_tabs_in_log_default
= 0;
1833 rev
.verbose_header
= 1;
1835 rev
.max_parents
= 1;
1836 rev
.diffopt
.flags
.recursive
= 1;
1837 rev
.subject_prefix
= fmt_patch_subject_prefix
;
1838 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
1839 s_r_opt
.def
= "HEAD";
1840 s_r_opt
.revarg_opt
= REVARG_COMMITTISH
;
1842 if (default_attach
) {
1843 rev
.mime_boundary
= default_attach
;
1848 * Parse the arguments before setup_revisions(), or something
1849 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
1850 * possibly a valid SHA1.
1852 argc
= parse_options(argc
, argv
, prefix
, builtin_format_patch_options
,
1853 builtin_format_patch_usage
,
1854 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
|
1855 PARSE_OPT_KEEP_DASHDASH
);
1857 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1858 if (fmt_patch_name_max
<= strlen("0000-") + strlen(fmt_patch_suffix
))
1859 fmt_patch_name_max
= strlen("0000-") + strlen(fmt_patch_suffix
);
1861 if (cover_from_description_arg
)
1862 cover_from_description_mode
= parse_cover_from_description(cover_from_description_arg
);
1864 if (0 < reroll_count
) {
1865 struct strbuf sprefix
= STRBUF_INIT
;
1866 strbuf_addf(&sprefix
, "%s v%d",
1867 rev
.subject_prefix
, reroll_count
);
1868 rev
.reroll_count
= reroll_count
;
1869 rev
.subject_prefix
= strbuf_detach(&sprefix
, NULL
);
1872 for (i
= 0; i
< extra_hdr
.nr
; i
++) {
1873 strbuf_addstr(&buf
, extra_hdr
.items
[i
].string
);
1874 strbuf_addch(&buf
, '\n');
1878 strbuf_addstr(&buf
, "To: ");
1879 for (i
= 0; i
< extra_to
.nr
; i
++) {
1881 strbuf_addstr(&buf
, " ");
1882 strbuf_addstr(&buf
, extra_to
.items
[i
].string
);
1883 if (i
+ 1 < extra_to
.nr
)
1884 strbuf_addch(&buf
, ',');
1885 strbuf_addch(&buf
, '\n');
1889 strbuf_addstr(&buf
, "Cc: ");
1890 for (i
= 0; i
< extra_cc
.nr
; i
++) {
1892 strbuf_addstr(&buf
, " ");
1893 strbuf_addstr(&buf
, extra_cc
.items
[i
].string
);
1894 if (i
+ 1 < extra_cc
.nr
)
1895 strbuf_addch(&buf
, ',');
1896 strbuf_addch(&buf
, '\n');
1899 rev
.extra_headers
= strbuf_detach(&buf
, NULL
);
1902 if (split_ident_line(&rev
.from_ident
, from
, strlen(from
)))
1903 die(_("invalid ident line: %s"), from
);
1906 if (start_number
< 0)
1910 * If numbered is set solely due to format.numbered in config,
1911 * and it would conflict with --keep-subject (-k) from the
1912 * command line, reset "numbered".
1914 if (numbered
&& keep_subject
&& !numbered_cmdline_opt
)
1917 if (numbered
&& keep_subject
)
1918 die(_("-n and -k are mutually exclusive"));
1919 if (keep_subject
&& subject_prefix
)
1920 die(_("--subject-prefix/--rfc and -k are mutually exclusive"));
1921 rev
.preserve_subject
= keep_subject
;
1923 argc
= setup_revisions(argc
, argv
, &rev
, &s_r_opt
);
1925 die(_("unrecognized argument: %s"), argv
[1]);
1927 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME
)
1928 die(_("--name-only does not make sense"));
1929 if (rev
.diffopt
.output_format
& DIFF_FORMAT_NAME_STATUS
)
1930 die(_("--name-status does not make sense"));
1931 if (rev
.diffopt
.output_format
& DIFF_FORMAT_CHECKDIFF
)
1932 die(_("--check does not make sense"));
1934 if (!use_patch_format
&&
1935 (!rev
.diffopt
.output_format
||
1936 rev
.diffopt
.output_format
== DIFF_FORMAT_PATCH
))
1937 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_SUMMARY
;
1938 if (!rev
.diffopt
.stat_width
)
1939 rev
.diffopt
.stat_width
= MAIL_DEFAULT_WRAP
;
1941 /* Always generate a patch */
1942 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1944 rev
.zero_commit
= zero_commit
;
1945 rev
.patch_name_max
= fmt_patch_name_max
;
1947 if (!rev
.diffopt
.flags
.text
&& !no_binary_diff
)
1948 rev
.diffopt
.flags
.binary
= 1;
1951 load_display_notes(&rev
.notes_opt
);
1953 if (!output_directory
&& !use_stdout
)
1954 output_directory
= config_output_directory
;
1957 output_directory
= set_outdir(prefix
, output_directory
);
1961 if (output_directory
) {
1963 if (rev
.diffopt
.use_color
!= GIT_COLOR_ALWAYS
)
1964 rev
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1966 die(_("standard output, or directory, which one?"));
1968 * We consider <outdir> as 'outside of gitdir', therefore avoid
1969 * applying adjust_shared_perm in s-c-l-d.
1971 saved
= get_shared_repository();
1972 set_shared_repository(0);
1973 switch (safe_create_leading_directories_const(output_directory
)) {
1978 die(_("could not create leading directories "
1979 "of '%s'"), output_directory
);
1981 set_shared_repository(saved
);
1982 if (mkdir(output_directory
, 0777) < 0 && errno
!= EEXIST
)
1983 die_errno(_("could not create directory '%s'"),
1987 if (rev
.pending
.nr
== 1) {
1990 if (rev
.max_count
< 0 && !rev
.show_root_diff
) {
1992 * This is traditional behaviour of "git format-patch
1993 * origin" that prepares what the origin side still
1996 rev
.pending
.objects
[0].item
->flags
|= UNINTERESTING
;
1997 add_head_to_pending(&rev
);
2001 * Otherwise, it is "format-patch -22 HEAD", and/or
2002 * "format-patch --root HEAD". The user wants
2003 * get_revision() to do the usual traversal.
2006 if (!strcmp(rev
.pending
.objects
[0].name
, "HEAD"))
2010 const char *ref
, *v
;
2011 ref
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
2013 if (ref
&& skip_prefix(ref
, "refs/heads/", &v
))
2014 branch_name
= xstrdup(v
);
2016 branch_name
= xstrdup(""); /* no branch */
2021 * We cannot move this anywhere earlier because we do want to
2022 * know if --root was given explicitly from the command line.
2024 rev
.show_root_diff
= 1;
2026 if (ignore_if_in_upstream
) {
2027 /* Don't say anything if head and upstream are the same. */
2028 if (rev
.pending
.nr
== 2) {
2029 struct object_array_entry
*o
= rev
.pending
.objects
;
2030 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2033 get_patch_ids(&rev
, &ids
);
2036 if (prepare_revision_walk(&rev
))
2037 die(_("revision walk setup failed"));
2039 while ((commit
= get_revision(&rev
)) != NULL
) {
2040 if (commit
->object
.flags
& BOUNDARY
) {
2042 origin
= (boundary_count
== 1) ? commit
: NULL
;
2046 if (ignore_if_in_upstream
&& has_commit_patch_id(commit
, &ids
))
2050 REALLOC_ARRAY(list
, nr
);
2051 list
[nr
- 1] = commit
;
2057 if (cover_letter
== -1) {
2058 if (config_cover_letter
== COVER_AUTO
)
2059 cover_letter
= (total
> 1);
2061 cover_letter
= (config_cover_letter
== COVER_ON
);
2063 if (!keep_subject
&& auto_number
&& (total
> 1 || cover_letter
))
2066 rev
.total
= total
+ start_number
- 1;
2068 if (idiff_prev
.nr
) {
2069 if (!cover_letter
&& total
!= 1)
2070 die(_("--interdiff requires --cover-letter or single patch"));
2071 rev
.idiff_oid1
= &idiff_prev
.oid
[idiff_prev
.nr
- 1];
2072 rev
.idiff_oid2
= get_commit_tree_oid(list
[0]);
2073 rev
.idiff_title
= diff_title(&idiff_title
, reroll_count
,
2075 _("Interdiff against v%d:"));
2078 if (creation_factor
< 0)
2079 creation_factor
= RANGE_DIFF_CREATION_FACTOR_DEFAULT
;
2080 else if (!rdiff_prev
)
2081 die(_("--creation-factor requires --range-diff"));
2084 if (!cover_letter
&& total
!= 1)
2085 die(_("--range-diff requires --cover-letter or single patch"));
2087 infer_range_diff_ranges(&rdiff1
, &rdiff2
, rdiff_prev
,
2089 rev
.rdiff1
= rdiff1
.buf
;
2090 rev
.rdiff2
= rdiff2
.buf
;
2091 rev
.creation_factor
= creation_factor
;
2092 rev
.rdiff_title
= diff_title(&rdiff_title
, reroll_count
,
2094 _("Range-diff against v%d:"));
2098 ; /* --no-signature inhibits all signatures */
2099 } else if (signature
&& signature
!= git_version_string
) {
2100 ; /* non-default signature already set */
2101 } else if (signature_file
) {
2102 struct strbuf buf
= STRBUF_INIT
;
2104 if (strbuf_read_file(&buf
, signature_file
, 128) < 0)
2105 die_errno(_("unable to read signature file '%s'"), signature_file
);
2106 signature
= strbuf_detach(&buf
, NULL
);
2109 memset(&bases
, 0, sizeof(bases
));
2110 base
= get_base_commit(base_commit
, list
, nr
);
2112 reset_revision_walk();
2113 clear_object_flags(UNINTERESTING
);
2114 prepare_bases(&bases
, base
, list
, nr
);
2117 if (in_reply_to
|| thread
|| cover_letter
)
2118 rev
.ref_message_ids
= xcalloc(1, sizeof(struct string_list
));
2120 const char *msgid
= clean_message_id(in_reply_to
);
2121 string_list_append(rev
.ref_message_ids
, msgid
);
2123 rev
.numbered_files
= just_numbers
;
2124 rev
.patch_suffix
= fmt_patch_suffix
;
2127 gen_message_id(&rev
, "cover");
2128 make_cover_letter(&rev
, use_stdout
,
2129 origin
, nr
, list
, branch_name
, quiet
);
2130 print_bases(&bases
, rev
.diffopt
.file
);
2131 print_signature(rev
.diffopt
.file
);
2134 /* interdiff/range-diff in cover-letter; omit from patches */
2135 rev
.idiff_oid1
= NULL
;
2138 rev
.add_signoff
= do_signoff
;
2141 progress
= start_delayed_progress(_("Generating patches"), total
);
2144 display_progress(progress
, total
- nr
);
2146 rev
.nr
= total
- nr
+ (start_number
- 1);
2147 /* Make the second and subsequent mails replies to the first */
2149 /* Have we already had a message ID? */
2150 if (rev
.message_id
) {
2152 * For deep threading: make every mail
2153 * a reply to the previous one, no
2154 * matter what other options are set.
2156 * For shallow threading:
2158 * Without --cover-letter and
2159 * --in-reply-to, make every mail a
2160 * reply to the one before.
2162 * With --in-reply-to but no
2163 * --cover-letter, make every mail a
2164 * reply to the <reply-to>.
2166 * With --cover-letter, make every
2167 * mail but the cover letter a reply
2168 * to the cover letter. The cover
2169 * letter is a reply to the
2170 * --in-reply-to, if specified.
2172 if (thread
== THREAD_SHALLOW
2173 && rev
.ref_message_ids
->nr
> 0
2174 && (!cover_letter
|| rev
.nr
> 1))
2175 free(rev
.message_id
);
2177 string_list_append(rev
.ref_message_ids
,
2180 gen_message_id(&rev
, oid_to_hex(&commit
->object
.oid
));
2184 open_next_file(rev
.numbered_files
? NULL
: commit
, NULL
, &rev
, quiet
))
2185 die(_("failed to create output files"));
2186 shown
= log_tree_commit(&rev
, commit
);
2187 free_commit_buffer(the_repository
->parsed_objects
,
2190 /* We put one extra blank line between formatted
2191 * patches and this flag is used by log-tree code
2192 * to see if it needs to emit a LF before showing
2193 * the log; when using one file per patch, we do
2194 * not want the extra blank line.
2199 print_bases(&bases
, rev
.diffopt
.file
);
2200 if (rev
.mime_boundary
)
2201 fprintf(rev
.diffopt
.file
, "\n--%s%s--\n\n\n",
2202 mime_boundary_leader
,
2205 print_signature(rev
.diffopt
.file
);
2208 fclose(rev
.diffopt
.file
);
2210 stop_progress(&progress
);
2213 string_list_clear(&extra_to
, 0);
2214 string_list_clear(&extra_cc
, 0);
2215 string_list_clear(&extra_hdr
, 0);
2216 if (ignore_if_in_upstream
)
2217 free_patch_ids(&ids
);
2220 oid_array_clear(&idiff_prev
);
2221 strbuf_release(&idiff_title
);
2222 strbuf_release(&rdiff1
);
2223 strbuf_release(&rdiff2
);
2224 strbuf_release(&rdiff_title
);
2228 static int add_pending_commit(const char *arg
, struct rev_info
*revs
, int flags
)
2230 struct object_id oid
;
2231 if (get_oid(arg
, &oid
) == 0) {
2232 struct commit
*commit
= lookup_commit_reference(the_repository
,
2235 commit
->object
.flags
|= flags
;
2236 add_pending_object(revs
, &commit
->object
, arg
);
2243 static const char * const cherry_usage
[] = {
2244 N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"),
2248 static void print_commit(char sign
, struct commit
*commit
, int verbose
,
2249 int abbrev
, FILE *file
)
2252 fprintf(file
, "%c %s\n", sign
,
2253 find_unique_abbrev(&commit
->object
.oid
, abbrev
));
2255 struct strbuf buf
= STRBUF_INIT
;
2256 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
2257 fprintf(file
, "%c %s %s\n", sign
,
2258 find_unique_abbrev(&commit
->object
.oid
, abbrev
),
2260 strbuf_release(&buf
);
2264 int cmd_cherry(int argc
, const char **argv
, const char *prefix
)
2266 struct rev_info revs
;
2267 struct patch_ids ids
;
2268 struct commit
*commit
;
2269 struct commit_list
*list
= NULL
;
2270 struct branch
*current_branch
;
2271 const char *upstream
;
2272 const char *head
= "HEAD";
2273 const char *limit
= NULL
;
2274 int verbose
= 0, abbrev
= 0;
2276 struct option options
[] = {
2277 OPT__ABBREV(&abbrev
),
2278 OPT__VERBOSE(&verbose
, N_("be verbose")),
2282 argc
= parse_options(argc
, argv
, prefix
, options
, cherry_usage
, 0);
2295 current_branch
= branch_get(NULL
);
2296 upstream
= branch_get_upstream(current_branch
, NULL
);
2298 fprintf(stderr
, _("Could not find a tracked"
2299 " remote branch, please"
2300 " specify <upstream> manually.\n"));
2301 usage_with_options(cherry_usage
, options
);
2305 repo_init_revisions(the_repository
, &revs
, prefix
);
2306 revs
.max_parents
= 1;
2308 if (add_pending_commit(head
, &revs
, 0))
2309 die(_("unknown commit %s"), head
);
2310 if (add_pending_commit(upstream
, &revs
, UNINTERESTING
))
2311 die(_("unknown commit %s"), upstream
);
2313 /* Don't say anything if head and upstream are the same. */
2314 if (revs
.pending
.nr
== 2) {
2315 struct object_array_entry
*o
= revs
.pending
.objects
;
2316 if (oideq(&o
[0].item
->oid
, &o
[1].item
->oid
))
2320 get_patch_ids(&revs
, &ids
);
2322 if (limit
&& add_pending_commit(limit
, &revs
, UNINTERESTING
))
2323 die(_("unknown commit %s"), limit
);
2325 /* reverse the list of commits */
2326 if (prepare_revision_walk(&revs
))
2327 die(_("revision walk setup failed"));
2328 while ((commit
= get_revision(&revs
)) != NULL
) {
2329 commit_list_insert(commit
, &list
);
2335 commit
= list
->item
;
2336 if (has_commit_patch_id(commit
, &ids
))
2338 print_commit(sign
, commit
, verbose
, abbrev
, revs
.diffopt
.file
);
2342 free_patch_ids(&ids
);