1 #include "git-compat-util.h"
3 #include "parse-options.h"
6 #include "repository.h"
13 * This implements the builtins revert and cherry-pick.
15 * Copyright (c) 2007 Johannes E. Schindelin
17 * Based on git-revert.sh, which is
19 * Copyright (c) 2005 Linus Torvalds
20 * Copyright (c) 2005 Junio C Hamano
23 static const char * const revert_usage
[] = {
24 N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
25 N_("git revert (--continue | --skip | --abort | --quit)"),
29 static const char * const cherry_pick_usage
[] = {
30 N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
31 " [-S[<keyid>]] <commit>..."),
32 N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
36 static const char *action_name(const struct replay_opts
*opts
)
38 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
41 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
43 return opts
->action
== REPLAY_REVERT
? revert_usage
: cherry_pick_usage
;
47 EMPTY_COMMIT_UNSPECIFIED
= -1,
48 STOP_ON_EMPTY_COMMIT
, /* output errors and stop in the middle of a cherry-pick */
49 DROP_EMPTY_COMMIT
, /* skip with a notice message */
50 KEEP_EMPTY_COMMIT
, /* keep recording as empty commits */
53 static int parse_opt_empty(const struct option
*opt
, const char *arg
, int unset
)
55 int *opt_value
= opt
->value
;
57 BUG_ON_OPT_NEG(unset
);
59 if (!strcmp(arg
, "stop"))
60 *opt_value
= STOP_ON_EMPTY_COMMIT
;
61 else if (!strcmp(arg
, "drop"))
62 *opt_value
= DROP_EMPTY_COMMIT
;
63 else if (!strcmp(arg
, "keep"))
64 *opt_value
= KEEP_EMPTY_COMMIT
;
66 return error(_("invalid value for '%s': '%s'"), "--empty", arg
);
71 static int option_parse_m(const struct option
*opt
,
72 const char *arg
, int unset
)
74 struct replay_opts
*replay
= opt
->value
;
82 replay
->mainline
= strtol(arg
, &end
, 10);
83 if (*end
|| replay
->mainline
<= 0)
84 return error(_("option `%s' expects a number greater than zero"),
91 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
96 va_start(ap
, base_opt
);
97 while ((this_opt
= va_arg(ap
, const char *))) {
104 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
107 static int run_sequencer(int argc
, const char **argv
, const char *prefix
,
108 struct replay_opts
*opts
)
110 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
111 const char *me
= action_name(opts
);
112 const char *cleanup_arg
= NULL
;
113 enum empty_action empty_opt
= EMPTY_COMMIT_UNSPECIFIED
;
115 struct option base_options
[] = {
116 OPT_CMDMODE(0, "quit", &cmd
, N_("end revert or cherry-pick sequence"), 'q'),
117 OPT_CMDMODE(0, "continue", &cmd
, N_("resume revert or cherry-pick sequence"), 'c'),
118 OPT_CMDMODE(0, "abort", &cmd
, N_("cancel revert or cherry-pick sequence"), 'a'),
119 OPT_CMDMODE(0, "skip", &cmd
, N_("skip current commit and continue"), 's'),
120 OPT_CLEANUP(&cleanup_arg
),
121 OPT_BOOL('n', "no-commit", &opts
->no_commit
, N_("don't automatically commit")),
122 OPT_BOOL('e', "edit", &opts
->edit
, N_("edit the commit message")),
123 OPT_NOOP_NOARG('r', NULL
),
124 OPT_BOOL('s', "signoff", &opts
->signoff
, N_("add a Signed-off-by trailer")),
125 OPT_CALLBACK('m', "mainline", opts
, N_("parent-number"),
126 N_("select mainline parent"), option_parse_m
),
127 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
128 OPT_STRING(0, "strategy", &opts
->strategy
, N_("strategy"), N_("merge strategy")),
129 OPT_STRVEC('X', "strategy-option", &opts
->xopts
, N_("option"),
130 N_("option for merge strategy")),
131 { OPTION_STRING
, 'S', "gpg-sign", &opts
->gpg_sign
, N_("key-id"),
132 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
135 struct option
*options
= base_options
;
137 if (opts
->action
== REPLAY_PICK
) {
138 struct option cp_extra
[] = {
139 OPT_BOOL('x', NULL
, &opts
->record_origin
, N_("append commit name")),
140 OPT_BOOL(0, "ff", &opts
->allow_ff
, N_("allow fast-forward")),
141 OPT_BOOL(0, "allow-empty", &opts
->allow_empty
, N_("preserve initially empty commits")),
142 OPT_BOOL(0, "allow-empty-message", &opts
->allow_empty_message
, N_("allow commits with empty messages")),
143 OPT_BOOL(0, "keep-redundant-commits", &opts
->keep_redundant_commits
, N_("deprecated: use --empty=keep instead")),
144 OPT_CALLBACK_F(0, "empty", &empty_opt
, "(stop|drop|keep)",
145 N_("how to handle commits that become empty"),
146 PARSE_OPT_NONEG
, parse_opt_empty
),
149 options
= parse_options_concat(options
, cp_extra
);
150 } else if (opts
->action
== REPLAY_REVERT
) {
151 struct option cp_extra
[] = {
152 OPT_BOOL(0, "reference", &opts
->commit_use_reference
,
153 N_("use the 'reference' format to refer to commits")),
156 options
= parse_options_concat(options
, cp_extra
);
159 argc
= parse_options(argc
, argv
, prefix
, options
, usage_str
,
160 PARSE_OPT_KEEP_ARGV0
|
161 PARSE_OPT_KEEP_UNKNOWN_OPT
);
163 prepare_repo_settings(the_repository
);
164 the_repository
->settings
.command_requires_full_index
= 0;
166 if (opts
->action
== REPLAY_PICK
) {
167 opts
->drop_redundant_commits
= (empty_opt
== DROP_EMPTY_COMMIT
);
168 opts
->keep_redundant_commits
= opts
->keep_redundant_commits
|| (empty_opt
== KEEP_EMPTY_COMMIT
);
171 /* implies allow_empty */
172 if (opts
->keep_redundant_commits
)
173 opts
->allow_empty
= 1;
176 opts
->default_msg_cleanup
= get_cleanup_mode(cleanup_arg
, 1);
177 opts
->explicit_cleanup
= 1;
180 /* Check for incompatible command line arguments */
182 char *this_operation
;
184 this_operation
= "--quit";
186 this_operation
= "--continue";
188 this_operation
= "--skip";
191 this_operation
= "--abort";
194 verify_opt_compatible(me
, this_operation
,
195 "--no-commit", opts
->no_commit
,
196 "--signoff", opts
->signoff
,
197 "--mainline", opts
->mainline
,
198 "--strategy", opts
->strategy
? 1 : 0,
199 "--strategy-option", opts
->xopts
.nr
? 1 : 0,
200 "-x", opts
->record_origin
,
201 "--ff", opts
->allow_ff
,
202 "--rerere-autoupdate", opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
,
203 "--no-rerere-autoupdate", opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
,
204 "--keep-redundant-commits", opts
->keep_redundant_commits
,
205 "--empty", empty_opt
!= EMPTY_COMMIT_UNSPECIFIED
,
209 if (!opts
->strategy
&& opts
->default_strategy
) {
210 opts
->strategy
= opts
->default_strategy
;
211 opts
->default_strategy
= NULL
;
215 verify_opt_compatible(me
, "--ff",
216 "--signoff", opts
->signoff
,
217 "--no-commit", opts
->no_commit
,
218 "-x", opts
->record_origin
,
219 "--edit", opts
->edit
> 0,
225 struct setup_revision_opt s_r_opt
;
226 opts
->revs
= xmalloc(sizeof(*opts
->revs
));
227 repo_init_revisions(the_repository
, opts
->revs
, NULL
);
228 opts
->revs
->no_walk
= 1;
229 opts
->revs
->unsorted_input
= 1;
231 usage_with_options(usage_str
, options
);
232 if (!strcmp(argv
[1], "-"))
234 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
235 s_r_opt
.assume_dashdash
= 1;
236 argc
= setup_revisions(argc
, argv
, opts
->revs
, &s_r_opt
);
240 usage_with_options(usage_str
, options
);
242 /* These option values will be free()d */
243 opts
->gpg_sign
= xstrdup_or_null(opts
->gpg_sign
);
244 opts
->strategy
= xstrdup_or_null(opts
->strategy
);
245 if (!opts
->strategy
&& getenv("GIT_TEST_MERGE_ALGORITHM"))
246 opts
->strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
250 int ret
= sequencer_remove_state(opts
);
252 remove_branch_state(the_repository
, 0);
256 return sequencer_continue(the_repository
, opts
);
258 return sequencer_rollback(the_repository
, opts
);
260 return sequencer_skip(the_repository
, opts
);
261 return sequencer_pick_revisions(the_repository
, opts
);
264 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
266 struct replay_opts opts
= REPLAY_OPTS_INIT
;
269 opts
.action
= REPLAY_REVERT
;
270 sequencer_init_config(&opts
);
271 res
= run_sequencer(argc
, argv
, prefix
, &opts
);
273 die(_("revert failed"));
274 replay_opts_release(&opts
);
278 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
280 struct replay_opts opts
= REPLAY_OPTS_INIT
;
283 opts
.action
= REPLAY_PICK
;
284 sequencer_init_config(&opts
);
285 res
= run_sequencer(argc
, argv
, prefix
, &opts
);
287 die(_("cherry-pick failed"));
288 replay_opts_release(&opts
);