Add the LAST_ARG_MUST_BE_NULL macro
[git/git.git] / builtin / revert.c
CommitLineData
9509af68
JS
1#include "cache.h"
2#include "builtin.h"
f8103794 3#include "parse-options.h"
0f2d4476
JK
4#include "diff.h"
5#include "revision.h"
aa1a0111 6#include "rerere.h"
04d3d3cf 7#include "dir.h"
26ae337b 8#include "sequencer.h"
9509af68
JS
9
10/*
11 * This implements the builtins revert and cherry-pick.
12 *
13 * Copyright (c) 2007 Johannes E. Schindelin
14 *
15 * Based on git-revert.sh, which is
16 *
17 * Copyright (c) 2005 Linus Torvalds
18 * Copyright (c) 2005 Junio C Hamano
19 */
20
f8103794 21static const char * const revert_usage[] = {
2130bf3c 22 N_("git revert [options] <commit-ish>..."),
a3b26dc7 23 N_("git revert <subcommand>"),
f8103794
PH
24 NULL
25};
9509af68 26
f8103794 27static const char * const cherry_pick_usage[] = {
2130bf3c 28 N_("git cherry-pick [options] <commit-ish>..."),
a3b26dc7 29 N_("git cherry-pick <subcommand>"),
f8103794
PH
30 NULL
31};
9509af68 32
80e1f791
RR
33static const char *action_name(const struct replay_opts *opts)
34{
644a3690 35 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
80e1f791
RR
36}
37
80e1f791 38static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
e0ef8495 39{
644a3690 40 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
e0ef8495
JN
41}
42
67ac1e1d
JN
43static int option_parse_x(const struct option *opt,
44 const char *arg, int unset)
45{
80e1f791
RR
46 struct replay_opts **opts_ptr = opt->value;
47 struct replay_opts *opts = *opts_ptr;
48
67ac1e1d
JN
49 if (unset)
50 return 0;
51
80e1f791
RR
52 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
53 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
67ac1e1d
JN
54 return 0;
55}
56
9fe3edc4 57LAST_ARG_MUST_BE_NULL
9044143f
RR
58static void verify_opt_compatible(const char *me, const char *base_opt, ...)
59{
60 const char *this_opt;
61 va_list ap;
62
63 va_start(ap, base_opt);
64 while ((this_opt = va_arg(ap, const char *))) {
65 if (va_arg(ap, int))
66 break;
67 }
68 va_end(ap);
69
70 if (this_opt)
71 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
72}
73
9fe3edc4 74LAST_ARG_MUST_BE_NULL
5a5d80f4
RR
75static void verify_opt_mutually_compatible(const char *me, ...)
76{
27c0f768 77 const char *opt1, *opt2 = NULL;
5a5d80f4
RR
78 va_list ap;
79
80 va_start(ap, me);
81 while ((opt1 = va_arg(ap, const char *))) {
82 if (va_arg(ap, int))
83 break;
84 }
85 if (opt1) {
86 while ((opt2 = va_arg(ap, const char *))) {
87 if (va_arg(ap, int))
88 break;
89 }
90 }
5ba9b5e7 91 va_end(ap);
5a5d80f4
RR
92
93 if (opt1 && opt2)
94 die(_("%s: %s cannot be used with %s"), me, opt1, opt2);
95}
96
80e1f791 97static void parse_args(int argc, const char **argv, struct replay_opts *opts)
9509af68 98{
80e1f791 99 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
9044143f 100 const char *me = action_name(opts);
f80a8726 101 int remove_state = 0;
5a5d80f4 102 int contin = 0;
539047c1 103 int rollback = 0;
f8103794 104 struct option options[] = {
a3b26dc7
NTND
105 OPT_BOOLEAN(0, "quit", &remove_state, N_("end revert or cherry-pick sequence")),
106 OPT_BOOLEAN(0, "continue", &contin, N_("resume revert or cherry-pick sequence")),
107 OPT_BOOLEAN(0, "abort", &rollback, N_("cancel revert or cherry-pick sequence")),
108 OPT_BOOLEAN('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
109 OPT_BOOLEAN('e', "edit", &opts->edit, N_("edit the commit message")),
7cfc6057 110 OPT_NOOP_NOARG('r', NULL),
a3b26dc7
NTND
111 OPT_BOOLEAN('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
112 OPT_INTEGER('m', "mainline", &opts->mainline, N_("parent number")),
80e1f791 113 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
a3b26dc7
NTND
114 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
115 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
116 N_("option for merge strategy"), option_parse_x),
f8103794 117 OPT_END(),
c62f6ec3
JH
118 OPT_END(),
119 OPT_END(),
df478b74 120 OPT_END(),
b27cfb0d 121 OPT_END(),
4bee9584 122 OPT_END(),
f8103794
PH
123 };
124
644a3690 125 if (opts->action == REPLAY_PICK) {
c62f6ec3 126 struct option cp_extra[] = {
a3b26dc7
NTND
127 OPT_BOOLEAN('x', NULL, &opts->record_origin, N_("append commit name")),
128 OPT_BOOLEAN(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
129 OPT_BOOLEAN(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
096bbd65 130 OPT_BOOLEAN(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
a3b26dc7 131 OPT_BOOLEAN(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
c62f6ec3
JH
132 OPT_END(),
133 };
134 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
f30f71ce 135 die(_("program error"));
c62f6ec3
JH
136 }
137
7f13334e
JN
138 argc = parse_options(argc, argv, NULL, options, usage_str,
139 PARSE_OPT_KEEP_ARGV0 |
140 PARSE_OPT_KEEP_UNKNOWN);
26ae337b 141
5a5d80f4
RR
142 /* Check for incompatible subcommands */
143 verify_opt_mutually_compatible(me,
f80a8726 144 "--quit", remove_state,
5a5d80f4 145 "--continue", contin,
539047c1 146 "--abort", rollback,
5a5d80f4
RR
147 NULL);
148
b27cfb0d
NH
149 /* implies allow_empty */
150 if (opts->keep_redundant_commits)
151 opts->allow_empty = 1;
152
26ae337b 153 /* Set the subcommand */
f80a8726
JN
154 if (remove_state)
155 opts->subcommand = REPLAY_REMOVE_STATE;
5a5d80f4
RR
156 else if (contin)
157 opts->subcommand = REPLAY_CONTINUE;
539047c1
JN
158 else if (rollback)
159 opts->subcommand = REPLAY_ROLLBACK;
26ae337b
RR
160 else
161 opts->subcommand = REPLAY_NONE;
162
163 /* Check for incompatible command line arguments */
5a5d80f4
RR
164 if (opts->subcommand != REPLAY_NONE) {
165 char *this_operation;
f80a8726
JN
166 if (opts->subcommand == REPLAY_REMOVE_STATE)
167 this_operation = "--quit";
539047c1 168 else if (opts->subcommand == REPLAY_CONTINUE)
5a5d80f4 169 this_operation = "--continue";
539047c1
JN
170 else {
171 assert(opts->subcommand == REPLAY_ROLLBACK);
172 this_operation = "--abort";
173 }
5a5d80f4
RR
174
175 verify_opt_compatible(me, this_operation,
26ae337b
RR
176 "--no-commit", opts->no_commit,
177 "--signoff", opts->signoff,
178 "--mainline", opts->mainline,
179 "--strategy", opts->strategy ? 1 : 0,
180 "--strategy-option", opts->xopts ? 1 : 0,
181 "-x", opts->record_origin,
182 "--ff", opts->allow_ff,
183 NULL);
184 }
185
9044143f
RR
186 if (opts->allow_ff)
187 verify_opt_compatible(me, "--ff",
188 "--signoff", opts->signoff,
189 "--no-commit", opts->no_commit,
190 "-x", opts->record_origin,
191 "--edit", opts->edit,
192 NULL);
7f13334e
JN
193
194 if (opts->subcommand != REPLAY_NONE) {
195 opts->revs = NULL;
196 } else {
6d5b93f2 197 struct setup_revision_opt s_r_opt;
7f13334e
JN
198 opts->revs = xmalloc(sizeof(*opts->revs));
199 init_revisions(opts->revs, NULL);
a73e22e9 200 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
7f13334e
JN
201 if (argc < 2)
202 usage_with_options(usage_str, options);
6d5b93f2
CB
203 memset(&s_r_opt, 0, sizeof(s_r_opt));
204 s_r_opt.assume_dashdash = 1;
205 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
7f13334e
JN
206 }
207
208 if (argc > 1)
209 usage_with_options(usage_str, options);
9509af68
JS
210}
211
9509af68
JS
212int cmd_revert(int argc, const char **argv, const char *prefix)
213{
80e1f791 214 struct replay_opts opts;
cf3e2486 215 int res;
80e1f791
RR
216
217 memset(&opts, 0, sizeof(opts));
9509af68 218 if (isatty(0))
80e1f791 219 opts.edit = 1;
644a3690 220 opts.action = REPLAY_REVERT;
89641472
RR
221 git_config(git_default_config, NULL);
222 parse_args(argc, argv, &opts);
043a4492 223 res = sequencer_pick_revisions(&opts);
cf3e2486
RR
224 if (res < 0)
225 die(_("revert failed"));
226 return res;
9509af68
JS
227}
228
229int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
230{
80e1f791 231 struct replay_opts opts;
cf3e2486 232 int res;
80e1f791
RR
233
234 memset(&opts, 0, sizeof(opts));
644a3690 235 opts.action = REPLAY_PICK;
89641472
RR
236 git_config(git_default_config, NULL);
237 parse_args(argc, argv, &opts);
043a4492 238 res = sequencer_pick_revisions(&opts);
cf3e2486
RR
239 if (res < 0)
240 die(_("cherry-pick failed"));
241 return res;
9509af68 242}