1 #include "git-compat-util.h"
5 #include "parse-options.h"
8 #include "repository.h"
12 #include "sequencer.h"
16 * This implements the builtins revert and cherry-pick.
18 * Copyright (c) 2007 Johannes E. Schindelin
20 * Based on git-revert.sh, which is
22 * Copyright (c) 2005 Linus Torvalds
23 * Copyright (c) 2005 Junio C Hamano
26 static const char * const revert_usage
[] = {
27 N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
28 N_("git revert (--continue | --skip | --abort | --quit)"),
32 static const char * const cherry_pick_usage
[] = {
33 N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
34 " [-S[<keyid>]] <commit>..."),
35 N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
39 static const char *action_name(const struct replay_opts
*opts
)
41 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
44 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
46 return opts
->action
== REPLAY_REVERT
? revert_usage
: cherry_pick_usage
;
49 static int option_parse_m(const struct option
*opt
,
50 const char *arg
, int unset
)
52 struct replay_opts
*replay
= opt
->value
;
60 replay
->mainline
= strtol(arg
, &end
, 10);
61 if (*end
|| replay
->mainline
<= 0)
62 return error(_("option `%s' expects a number greater than zero"),
69 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
74 va_start(ap
, base_opt
);
75 while ((this_opt
= va_arg(ap
, const char *))) {
82 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
85 static int run_sequencer(int argc
, const char **argv
, const char *prefix
,
86 struct replay_opts
*opts
)
88 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
89 const char *me
= action_name(opts
);
90 const char *cleanup_arg
= NULL
;
92 struct option base_options
[] = {
93 OPT_CMDMODE(0, "quit", &cmd
, N_("end revert or cherry-pick sequence"), 'q'),
94 OPT_CMDMODE(0, "continue", &cmd
, N_("resume revert or cherry-pick sequence"), 'c'),
95 OPT_CMDMODE(0, "abort", &cmd
, N_("cancel revert or cherry-pick sequence"), 'a'),
96 OPT_CMDMODE(0, "skip", &cmd
, N_("skip current commit and continue"), 's'),
97 OPT_CLEANUP(&cleanup_arg
),
98 OPT_BOOL('n', "no-commit", &opts
->no_commit
, N_("don't automatically commit")),
99 OPT_BOOL('e', "edit", &opts
->edit
, N_("edit the commit message")),
100 OPT_NOOP_NOARG('r', NULL
),
101 OPT_BOOL('s', "signoff", &opts
->signoff
, N_("add a Signed-off-by trailer")),
102 OPT_CALLBACK('m', "mainline", opts
, N_("parent-number"),
103 N_("select mainline parent"), option_parse_m
),
104 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
105 OPT_STRING(0, "strategy", &opts
->strategy
, N_("strategy"), N_("merge strategy")),
106 OPT_STRVEC('X', "strategy-option", &opts
->xopts
, N_("option"),
107 N_("option for merge strategy")),
108 { OPTION_STRING
, 'S', "gpg-sign", &opts
->gpg_sign
, N_("key-id"),
109 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
112 struct option
*options
= base_options
;
114 if (opts
->action
== REPLAY_PICK
) {
115 struct option cp_extra
[] = {
116 OPT_BOOL('x', NULL
, &opts
->record_origin
, N_("append commit name")),
117 OPT_BOOL(0, "ff", &opts
->allow_ff
, N_("allow fast-forward")),
118 OPT_BOOL(0, "allow-empty", &opts
->allow_empty
, N_("preserve initially empty commits")),
119 OPT_BOOL(0, "allow-empty-message", &opts
->allow_empty_message
, N_("allow commits with empty messages")),
120 OPT_BOOL(0, "keep-redundant-commits", &opts
->keep_redundant_commits
, N_("keep redundant, empty commits")),
123 options
= parse_options_concat(options
, cp_extra
);
124 } else if (opts
->action
== REPLAY_REVERT
) {
125 struct option cp_extra
[] = {
126 OPT_BOOL(0, "reference", &opts
->commit_use_reference
,
127 N_("use the 'reference' format to refer to commits")),
130 options
= parse_options_concat(options
, cp_extra
);
133 argc
= parse_options(argc
, argv
, prefix
, options
, usage_str
,
134 PARSE_OPT_KEEP_ARGV0
|
135 PARSE_OPT_KEEP_UNKNOWN_OPT
);
137 prepare_repo_settings(the_repository
);
138 the_repository
->settings
.command_requires_full_index
= 0;
140 /* implies allow_empty */
141 if (opts
->keep_redundant_commits
)
142 opts
->allow_empty
= 1;
145 opts
->default_msg_cleanup
= get_cleanup_mode(cleanup_arg
, 1);
146 opts
->explicit_cleanup
= 1;
149 /* Check for incompatible command line arguments */
151 char *this_operation
;
153 this_operation
= "--quit";
155 this_operation
= "--continue";
157 this_operation
= "--skip";
160 this_operation
= "--abort";
163 verify_opt_compatible(me
, this_operation
,
164 "--no-commit", opts
->no_commit
,
165 "--signoff", opts
->signoff
,
166 "--mainline", opts
->mainline
,
167 "--strategy", opts
->strategy
? 1 : 0,
168 "--strategy-option", opts
->xopts
.nr
? 1 : 0,
169 "-x", opts
->record_origin
,
170 "--ff", opts
->allow_ff
,
171 "--rerere-autoupdate", opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
,
172 "--no-rerere-autoupdate", opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
,
176 if (!opts
->strategy
&& opts
->default_strategy
) {
177 opts
->strategy
= opts
->default_strategy
;
178 opts
->default_strategy
= NULL
;
182 verify_opt_compatible(me
, "--ff",
183 "--signoff", opts
->signoff
,
184 "--no-commit", opts
->no_commit
,
185 "-x", opts
->record_origin
,
186 "--edit", opts
->edit
> 0,
192 struct setup_revision_opt s_r_opt
;
193 opts
->revs
= xmalloc(sizeof(*opts
->revs
));
194 repo_init_revisions(the_repository
, opts
->revs
, NULL
);
195 opts
->revs
->no_walk
= 1;
196 opts
->revs
->unsorted_input
= 1;
198 usage_with_options(usage_str
, options
);
199 if (!strcmp(argv
[1], "-"))
201 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
202 s_r_opt
.assume_dashdash
= 1;
203 argc
= setup_revisions(argc
, argv
, opts
->revs
, &s_r_opt
);
207 usage_with_options(usage_str
, options
);
209 /* These option values will be free()d */
210 opts
->gpg_sign
= xstrdup_or_null(opts
->gpg_sign
);
211 opts
->strategy
= xstrdup_or_null(opts
->strategy
);
212 if (!opts
->strategy
&& getenv("GIT_TEST_MERGE_ALGORITHM"))
213 opts
->strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
217 int ret
= sequencer_remove_state(opts
);
219 remove_branch_state(the_repository
, 0);
223 return sequencer_continue(the_repository
, opts
);
225 return sequencer_rollback(the_repository
, opts
);
227 return sequencer_skip(the_repository
, opts
);
228 return sequencer_pick_revisions(the_repository
, opts
);
231 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
233 struct replay_opts opts
= REPLAY_OPTS_INIT
;
236 opts
.action
= REPLAY_REVERT
;
237 sequencer_init_config(&opts
);
238 res
= run_sequencer(argc
, argv
, prefix
, &opts
);
240 die(_("revert failed"));
241 replay_opts_release(&opts
);
245 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
247 struct replay_opts opts
= REPLAY_OPTS_INIT
;
250 opts
.action
= REPLAY_PICK
;
251 sequencer_init_config(&opts
);
252 res
= run_sequencer(argc
, argv
, prefix
, &opts
);
254 die(_("cherry-pick failed"));
255 replay_opts_release(&opts
);