1 #include "git-compat-util.h"
5 #include "parse-options.h"
10 #include "sequencer.h"
14 * This implements the builtins revert and cherry-pick.
16 * Copyright (c) 2007 Johannes E. Schindelin
18 * Based on git-revert.sh, which is
20 * Copyright (c) 2005 Linus Torvalds
21 * Copyright (c) 2005 Junio C Hamano
24 static const char * const revert_usage
[] = {
25 N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
26 N_("git revert (--continue | --skip | --abort | --quit)"),
30 static const char * const cherry_pick_usage
[] = {
31 N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
32 " [-S[<keyid>]] <commit>..."),
33 N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
37 static const char *action_name(const struct replay_opts
*opts
)
39 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
42 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
44 return opts
->action
== REPLAY_REVERT
? revert_usage
: cherry_pick_usage
;
47 static int option_parse_x(const struct option
*opt
,
48 const char *arg
, int unset
)
50 struct replay_opts
**opts_ptr
= opt
->value
;
51 struct replay_opts
*opts
= *opts_ptr
;
56 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
57 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(arg
);
61 static int option_parse_m(const struct option
*opt
,
62 const char *arg
, int unset
)
64 struct replay_opts
*replay
= opt
->value
;
72 replay
->mainline
= strtol(arg
, &end
, 10);
73 if (*end
|| replay
->mainline
<= 0)
74 return error(_("option `%s' expects a number greater than zero"),
81 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
86 va_start(ap
, base_opt
);
87 while ((this_opt
= va_arg(ap
, const char *))) {
94 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
97 static int run_sequencer(int argc
, const char **argv
, struct replay_opts
*opts
)
99 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
100 const char *me
= action_name(opts
);
101 const char *cleanup_arg
= NULL
;
103 struct option base_options
[] = {
104 OPT_CMDMODE(0, "quit", &cmd
, N_("end revert or cherry-pick sequence"), 'q'),
105 OPT_CMDMODE(0, "continue", &cmd
, N_("resume revert or cherry-pick sequence"), 'c'),
106 OPT_CMDMODE(0, "abort", &cmd
, N_("cancel revert or cherry-pick sequence"), 'a'),
107 OPT_CMDMODE(0, "skip", &cmd
, N_("skip current commit and continue"), 's'),
108 OPT_CLEANUP(&cleanup_arg
),
109 OPT_BOOL('n', "no-commit", &opts
->no_commit
, N_("don't automatically commit")),
110 OPT_BOOL('e', "edit", &opts
->edit
, N_("edit the commit message")),
111 OPT_NOOP_NOARG('r', NULL
),
112 OPT_BOOL('s', "signoff", &opts
->signoff
, N_("add a Signed-off-by trailer")),
113 OPT_CALLBACK('m', "mainline", opts
, N_("parent-number"),
114 N_("select mainline parent"), option_parse_m
),
115 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
116 OPT_STRING(0, "strategy", &opts
->strategy
, N_("strategy"), N_("merge strategy")),
117 OPT_CALLBACK('X', "strategy-option", &opts
, N_("option"),
118 N_("option for merge strategy"), option_parse_x
),
119 { OPTION_STRING
, 'S', "gpg-sign", &opts
->gpg_sign
, N_("key-id"),
120 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
123 struct option
*options
= base_options
;
125 if (opts
->action
== REPLAY_PICK
) {
126 struct option cp_extra
[] = {
127 OPT_BOOL('x', NULL
, &opts
->record_origin
, N_("append commit name")),
128 OPT_BOOL(0, "ff", &opts
->allow_ff
, N_("allow fast-forward")),
129 OPT_BOOL(0, "allow-empty", &opts
->allow_empty
, N_("preserve initially empty commits")),
130 OPT_BOOL(0, "allow-empty-message", &opts
->allow_empty_message
, N_("allow commits with empty messages")),
131 OPT_BOOL(0, "keep-redundant-commits", &opts
->keep_redundant_commits
, N_("keep redundant, empty commits")),
134 options
= parse_options_concat(options
, cp_extra
);
135 } else if (opts
->action
== REPLAY_REVERT
) {
136 struct option cp_extra
[] = {
137 OPT_BOOL(0, "reference", &opts
->commit_use_reference
,
138 N_("use the 'reference' format to refer to commits")),
141 options
= parse_options_concat(options
, cp_extra
);
144 argc
= parse_options(argc
, argv
, NULL
, options
, usage_str
,
145 PARSE_OPT_KEEP_ARGV0
|
146 PARSE_OPT_KEEP_UNKNOWN_OPT
);
148 prepare_repo_settings(the_repository
);
149 the_repository
->settings
.command_requires_full_index
= 0;
151 /* implies allow_empty */
152 if (opts
->keep_redundant_commits
)
153 opts
->allow_empty
= 1;
156 opts
->default_msg_cleanup
= get_cleanup_mode(cleanup_arg
, 1);
157 opts
->explicit_cleanup
= 1;
160 /* Check for incompatible command line arguments */
162 char *this_operation
;
164 this_operation
= "--quit";
166 this_operation
= "--continue";
168 this_operation
= "--skip";
171 this_operation
= "--abort";
174 verify_opt_compatible(me
, this_operation
,
175 "--no-commit", opts
->no_commit
,
176 "--signoff", opts
->signoff
,
177 "--mainline", opts
->mainline
,
178 "--strategy", opts
->strategy
? 1 : 0,
179 "--strategy-option", opts
->xopts
? 1 : 0,
180 "-x", opts
->record_origin
,
181 "--ff", opts
->allow_ff
,
182 "--rerere-autoupdate", opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
,
183 "--no-rerere-autoupdate", opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
,
187 if (!opts
->strategy
&& opts
->default_strategy
) {
188 opts
->strategy
= opts
->default_strategy
;
189 opts
->default_strategy
= NULL
;
193 verify_opt_compatible(me
, "--ff",
194 "--signoff", opts
->signoff
,
195 "--no-commit", opts
->no_commit
,
196 "-x", opts
->record_origin
,
197 "--edit", opts
->edit
> 0,
203 struct setup_revision_opt s_r_opt
;
204 opts
->revs
= xmalloc(sizeof(*opts
->revs
));
205 repo_init_revisions(the_repository
, opts
->revs
, NULL
);
206 opts
->revs
->no_walk
= 1;
207 opts
->revs
->unsorted_input
= 1;
209 usage_with_options(usage_str
, options
);
210 if (!strcmp(argv
[1], "-"))
212 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
213 s_r_opt
.assume_dashdash
= 1;
214 argc
= setup_revisions(argc
, argv
, opts
->revs
, &s_r_opt
);
218 usage_with_options(usage_str
, options
);
220 /* These option values will be free()d */
221 opts
->gpg_sign
= xstrdup_or_null(opts
->gpg_sign
);
222 opts
->strategy
= xstrdup_or_null(opts
->strategy
);
223 if (!opts
->strategy
&& getenv("GIT_TEST_MERGE_ALGORITHM"))
224 opts
->strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
228 int ret
= sequencer_remove_state(opts
);
230 remove_branch_state(the_repository
, 0);
234 return sequencer_continue(the_repository
, opts
);
236 return sequencer_rollback(the_repository
, opts
);
238 return sequencer_skip(the_repository
, opts
);
239 return sequencer_pick_revisions(the_repository
, opts
);
242 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
244 struct replay_opts opts
= REPLAY_OPTS_INIT
;
247 opts
.action
= REPLAY_REVERT
;
248 sequencer_init_config(&opts
);
249 res
= run_sequencer(argc
, argv
, &opts
);
251 die(_("revert failed"));
252 replay_opts_release(&opts
);
256 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
258 struct replay_opts opts
= REPLAY_OPTS_INIT
;
261 opts
.action
= REPLAY_PICK
;
262 sequencer_init_config(&opts
);
263 res
= run_sequencer(argc
, argv
, &opts
);
265 die(_("cherry-pick failed"));
266 replay_opts_release(&opts
);