3 #include "parse-options.h"
11 * This implements the builtins revert and cherry-pick.
13 * Copyright (c) 2007 Johannes E. Schindelin
15 * Based on git-revert.sh, which is
17 * Copyright (c) 2005 Linus Torvalds
18 * Copyright (c) 2005 Junio C Hamano
21 static const char * const revert_usage
[] = {
22 "git revert [options] <commit-ish>",
23 "git revert <subcommand>",
27 static const char * const cherry_pick_usage
[] = {
28 "git cherry-pick [options] <commit-ish>",
29 "git cherry-pick <subcommand>",
33 static const char *action_name(const struct replay_opts
*opts
)
35 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
38 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
40 return opts
->action
== REPLAY_REVERT
? revert_usage
: cherry_pick_usage
;
43 static int option_parse_x(const struct option
*opt
,
44 const char *arg
, int unset
)
46 struct replay_opts
**opts_ptr
= opt
->value
;
47 struct replay_opts
*opts
= *opts_ptr
;
52 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
53 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(arg
);
57 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
62 va_start(ap
, base_opt
);
63 while ((this_opt
= va_arg(ap
, const char *))) {
70 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
73 static void verify_opt_mutually_compatible(const char *me
, ...)
75 const char *opt1
, *opt2
= NULL
;
79 while ((opt1
= va_arg(ap
, const char *))) {
84 while ((opt2
= va_arg(ap
, const char *))) {
91 die(_("%s: %s cannot be used with %s"), me
, opt1
, opt2
);
94 static void parse_args(int argc
, const char **argv
, struct replay_opts
*opts
)
96 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
97 const char *me
= action_name(opts
);
101 struct option options
[] = {
102 OPT_BOOLEAN(0, "quit", &remove_state
, "end revert or cherry-pick sequence"),
103 OPT_BOOLEAN(0, "continue", &contin
, "resume revert or cherry-pick sequence"),
104 OPT_BOOLEAN(0, "abort", &rollback
, "cancel revert or cherry-pick sequence"),
105 OPT_BOOLEAN('n', "no-commit", &opts
->no_commit
, "don't automatically commit"),
106 OPT_BOOLEAN('e', "edit", &opts
->edit
, "edit the commit message"),
107 OPT_NOOP_NOARG('r', NULL
),
108 OPT_BOOLEAN('s', "signoff", &opts
->signoff
, "add Signed-off-by:"),
109 OPT_INTEGER('m', "mainline", &opts
->mainline
, "parent number"),
110 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
111 OPT_STRING(0, "strategy", &opts
->strategy
, "strategy", "merge strategy"),
112 OPT_CALLBACK('X', "strategy-option", &opts
, "option",
113 "option for merge strategy", option_parse_x
),
119 if (opts
->action
== REPLAY_PICK
) {
120 struct option cp_extra
[] = {
121 OPT_BOOLEAN('x', NULL
, &opts
->record_origin
, "append commit name"),
122 OPT_BOOLEAN(0, "ff", &opts
->allow_ff
, "allow fast-forward"),
125 if (parse_options_concat(options
, ARRAY_SIZE(options
), cp_extra
))
126 die(_("program error"));
129 argc
= parse_options(argc
, argv
, NULL
, options
, usage_str
,
130 PARSE_OPT_KEEP_ARGV0
|
131 PARSE_OPT_KEEP_UNKNOWN
);
133 /* Check for incompatible subcommands */
134 verify_opt_mutually_compatible(me
,
135 "--quit", remove_state
,
136 "--continue", contin
,
140 /* Set the subcommand */
142 opts
->subcommand
= REPLAY_REMOVE_STATE
;
144 opts
->subcommand
= REPLAY_CONTINUE
;
146 opts
->subcommand
= REPLAY_ROLLBACK
;
148 opts
->subcommand
= REPLAY_NONE
;
150 /* Check for incompatible command line arguments */
151 if (opts
->subcommand
!= REPLAY_NONE
) {
152 char *this_operation
;
153 if (opts
->subcommand
== REPLAY_REMOVE_STATE
)
154 this_operation
= "--quit";
155 else if (opts
->subcommand
== REPLAY_CONTINUE
)
156 this_operation
= "--continue";
158 assert(opts
->subcommand
== REPLAY_ROLLBACK
);
159 this_operation
= "--abort";
162 verify_opt_compatible(me
, this_operation
,
163 "--no-commit", opts
->no_commit
,
164 "--signoff", opts
->signoff
,
165 "--mainline", opts
->mainline
,
166 "--strategy", opts
->strategy
? 1 : 0,
167 "--strategy-option", opts
->xopts
? 1 : 0,
168 "-x", opts
->record_origin
,
169 "--ff", opts
->allow_ff
,
174 verify_opt_compatible(me
, "--ff",
175 "--signoff", opts
->signoff
,
176 "--no-commit", opts
->no_commit
,
177 "-x", opts
->record_origin
,
178 "--edit", opts
->edit
,
181 if (opts
->subcommand
!= REPLAY_NONE
) {
184 opts
->revs
= xmalloc(sizeof(*opts
->revs
));
185 init_revisions(opts
->revs
, NULL
);
186 opts
->revs
->no_walk
= 1;
188 usage_with_options(usage_str
, options
);
189 argc
= setup_revisions(argc
, argv
, opts
->revs
, NULL
);
193 usage_with_options(usage_str
, options
);
196 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
198 struct replay_opts opts
;
201 memset(&opts
, 0, sizeof(opts
));
204 opts
.action
= REPLAY_REVERT
;
205 git_config(git_default_config
, NULL
);
206 parse_args(argc
, argv
, &opts
);
207 res
= sequencer_pick_revisions(&opts
);
209 die(_("revert failed"));
213 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
215 struct replay_opts opts
;
218 memset(&opts
, 0, sizeof(opts
));
219 opts
.action
= REPLAY_PICK
;
220 git_config(git_default_config
, NULL
);
221 parse_args(argc
, argv
, &opts
);
222 res
= sequencer_pick_revisions(&opts
);
224 die(_("cherry-pick failed"));