cherry-pick: detect bogus arguments to --mainline
[git/raj.git] / builtin / revert.c
blob37df613ac7eae3e37122adcaf3db931f0a325134
1 #include "cache.h"
2 #include "builtin.h"
3 #include "parse-options.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "rerere.h"
7 #include "dir.h"
8 #include "sequencer.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 N_("git revert [<options>] <commit-ish>..."),
23 N_("git revert <subcommand>"),
24 NULL
27 static const char * const cherry_pick_usage[] = {
28 N_("git cherry-pick [<options>] <commit-ish>..."),
29 N_("git cherry-pick <subcommand>"),
30 NULL
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;
49 if (unset)
50 return 0;
52 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
53 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
54 return 0;
57 static int option_parse_m(const struct option *opt,
58 const char *arg, int unset)
60 struct replay_opts *replay = opt->value;
61 char *end;
63 if (unset) {
64 replay->mainline = 0;
65 return 0;
68 replay->mainline = strtol(arg, &end, 10);
69 if (*end || replay->mainline <= 0)
70 return opterror(opt, "expects a number greater than zero", 0);
72 return 0;
75 LAST_ARG_MUST_BE_NULL
76 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
78 const char *this_opt;
79 va_list ap;
81 va_start(ap, base_opt);
82 while ((this_opt = va_arg(ap, const char *))) {
83 if (va_arg(ap, int))
84 break;
86 va_end(ap);
88 if (this_opt)
89 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
92 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
94 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
95 const char *me = action_name(opts);
96 int cmd = 0;
97 struct option base_options[] = {
98 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
99 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
100 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
101 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
102 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
103 OPT_NOOP_NOARG('r', NULL),
104 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
105 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
106 N_("select mainline parent"), option_parse_m),
107 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
108 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
109 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
110 N_("option for merge strategy"), option_parse_x),
111 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
112 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
113 OPT_END()
115 struct option *options = base_options;
117 if (opts->action == REPLAY_PICK) {
118 struct option cp_extra[] = {
119 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
120 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
121 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
122 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
123 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
124 OPT_END(),
126 options = parse_options_concat(options, cp_extra);
129 argc = parse_options(argc, argv, NULL, options, usage_str,
130 PARSE_OPT_KEEP_ARGV0 |
131 PARSE_OPT_KEEP_UNKNOWN);
133 /* implies allow_empty */
134 if (opts->keep_redundant_commits)
135 opts->allow_empty = 1;
137 /* Set the subcommand */
138 if (cmd == 'q')
139 opts->subcommand = REPLAY_REMOVE_STATE;
140 else if (cmd == 'c')
141 opts->subcommand = REPLAY_CONTINUE;
142 else if (cmd == 'a')
143 opts->subcommand = REPLAY_ROLLBACK;
144 else
145 opts->subcommand = REPLAY_NONE;
147 /* Check for incompatible command line arguments */
148 if (opts->subcommand != REPLAY_NONE) {
149 char *this_operation;
150 if (opts->subcommand == REPLAY_REMOVE_STATE)
151 this_operation = "--quit";
152 else if (opts->subcommand == REPLAY_CONTINUE)
153 this_operation = "--continue";
154 else {
155 assert(opts->subcommand == REPLAY_ROLLBACK);
156 this_operation = "--abort";
159 verify_opt_compatible(me, this_operation,
160 "--no-commit", opts->no_commit,
161 "--signoff", opts->signoff,
162 "--mainline", opts->mainline,
163 "--strategy", opts->strategy ? 1 : 0,
164 "--strategy-option", opts->xopts ? 1 : 0,
165 "-x", opts->record_origin,
166 "--ff", opts->allow_ff,
167 NULL);
170 if (opts->allow_ff)
171 verify_opt_compatible(me, "--ff",
172 "--signoff", opts->signoff,
173 "--no-commit", opts->no_commit,
174 "-x", opts->record_origin,
175 "--edit", opts->edit,
176 NULL);
178 if (opts->subcommand != REPLAY_NONE) {
179 opts->revs = NULL;
180 } else {
181 struct setup_revision_opt s_r_opt;
182 opts->revs = xmalloc(sizeof(*opts->revs));
183 init_revisions(opts->revs, NULL);
184 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
185 if (argc < 2)
186 usage_with_options(usage_str, options);
187 if (!strcmp(argv[1], "-"))
188 argv[1] = "@{-1}";
189 memset(&s_r_opt, 0, sizeof(s_r_opt));
190 s_r_opt.assume_dashdash = 1;
191 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
194 if (argc > 1)
195 usage_with_options(usage_str, options);
198 int cmd_revert(int argc, const char **argv, const char *prefix)
200 struct replay_opts opts;
201 int res;
203 memset(&opts, 0, sizeof(opts));
204 if (isatty(0))
205 opts.edit = 1;
206 opts.action = REPLAY_REVERT;
207 git_config(git_default_config, NULL);
208 parse_args(argc, argv, &opts);
209 res = sequencer_pick_revisions(&opts);
210 if (res < 0)
211 die(_("revert failed"));
212 return res;
215 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
217 struct replay_opts opts;
218 int res;
220 memset(&opts, 0, sizeof(opts));
221 opts.action = REPLAY_PICK;
222 git_config(git_default_config, NULL);
223 parse_args(argc, argv, &opts);
224 res = sequencer_pick_revisions(&opts);
225 if (res < 0)
226 die(_("cherry-pick failed"));
227 return res;