reftable/merged: advance subiter on subsequent iteration
[alt-git.git] / builtin / revert.c
blob89821bab95745a5fca294d00811804799ecd323d
1 #include "git-compat-util.h"
2 #include "builtin.h"
3 #include "parse-options.h"
4 #include "diff.h"
5 #include "gettext.h"
6 #include "repository.h"
7 #include "revision.h"
8 #include "rerere.h"
9 #include "sequencer.h"
10 #include "branch.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)"),
26 NULL
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)"),
33 NULL
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;
46 static int option_parse_m(const struct option *opt,
47 const char *arg, int unset)
49 struct replay_opts *replay = opt->value;
50 char *end;
52 if (unset) {
53 replay->mainline = 0;
54 return 0;
57 replay->mainline = strtol(arg, &end, 10);
58 if (*end || replay->mainline <= 0)
59 return error(_("option `%s' expects a number greater than zero"),
60 opt->long_name);
62 return 0;
65 LAST_ARG_MUST_BE_NULL
66 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
68 const char *this_opt;
69 va_list ap;
71 va_start(ap, base_opt);
72 while ((this_opt = va_arg(ap, const char *))) {
73 if (va_arg(ap, int))
74 break;
76 va_end(ap);
78 if (this_opt)
79 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
82 static int run_sequencer(int argc, const char **argv, const char *prefix,
83 struct replay_opts *opts)
85 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
86 const char *me = action_name(opts);
87 const char *cleanup_arg = NULL;
88 int cmd = 0;
89 struct option base_options[] = {
90 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
91 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
92 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
93 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
94 OPT_CLEANUP(&cleanup_arg),
95 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
96 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
97 OPT_NOOP_NOARG('r', NULL),
98 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
99 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
100 N_("select mainline parent"), option_parse_m),
101 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
102 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
103 OPT_STRVEC('X', "strategy-option", &opts->xopts, N_("option"),
104 N_("option for merge strategy")),
105 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
106 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
107 OPT_END()
109 struct option *options = base_options;
111 if (opts->action == REPLAY_PICK) {
112 struct option cp_extra[] = {
113 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
114 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
115 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
116 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
117 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
118 OPT_END(),
120 options = parse_options_concat(options, cp_extra);
121 } else if (opts->action == REPLAY_REVERT) {
122 struct option cp_extra[] = {
123 OPT_BOOL(0, "reference", &opts->commit_use_reference,
124 N_("use the 'reference' format to refer to commits")),
125 OPT_END(),
127 options = parse_options_concat(options, cp_extra);
130 argc = parse_options(argc, argv, prefix, options, usage_str,
131 PARSE_OPT_KEEP_ARGV0 |
132 PARSE_OPT_KEEP_UNKNOWN_OPT);
134 prepare_repo_settings(the_repository);
135 the_repository->settings.command_requires_full_index = 0;
137 /* implies allow_empty */
138 if (opts->keep_redundant_commits)
139 opts->allow_empty = 1;
141 if (cleanup_arg) {
142 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
143 opts->explicit_cleanup = 1;
146 /* Check for incompatible command line arguments */
147 if (cmd) {
148 char *this_operation;
149 if (cmd == 'q')
150 this_operation = "--quit";
151 else if (cmd == 'c')
152 this_operation = "--continue";
153 else if (cmd == 's')
154 this_operation = "--skip";
155 else {
156 assert(cmd == 'a');
157 this_operation = "--abort";
160 verify_opt_compatible(me, this_operation,
161 "--no-commit", opts->no_commit,
162 "--signoff", opts->signoff,
163 "--mainline", opts->mainline,
164 "--strategy", opts->strategy ? 1 : 0,
165 "--strategy-option", opts->xopts.nr ? 1 : 0,
166 "-x", opts->record_origin,
167 "--ff", opts->allow_ff,
168 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
169 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
170 NULL);
173 if (!opts->strategy && opts->default_strategy) {
174 opts->strategy = opts->default_strategy;
175 opts->default_strategy = NULL;
178 if (opts->allow_ff)
179 verify_opt_compatible(me, "--ff",
180 "--signoff", opts->signoff,
181 "--no-commit", opts->no_commit,
182 "-x", opts->record_origin,
183 "--edit", opts->edit > 0,
184 NULL);
186 if (cmd) {
187 opts->revs = NULL;
188 } else {
189 struct setup_revision_opt s_r_opt;
190 opts->revs = xmalloc(sizeof(*opts->revs));
191 repo_init_revisions(the_repository, opts->revs, NULL);
192 opts->revs->no_walk = 1;
193 opts->revs->unsorted_input = 1;
194 if (argc < 2)
195 usage_with_options(usage_str, options);
196 if (!strcmp(argv[1], "-"))
197 argv[1] = "@{-1}";
198 memset(&s_r_opt, 0, sizeof(s_r_opt));
199 s_r_opt.assume_dashdash = 1;
200 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
203 if (argc > 1)
204 usage_with_options(usage_str, options);
206 /* These option values will be free()d */
207 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
208 opts->strategy = xstrdup_or_null(opts->strategy);
209 if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
210 opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
211 free(options);
213 if (cmd == 'q') {
214 int ret = sequencer_remove_state(opts);
215 if (!ret)
216 remove_branch_state(the_repository, 0);
217 return ret;
219 if (cmd == 'c')
220 return sequencer_continue(the_repository, opts);
221 if (cmd == 'a')
222 return sequencer_rollback(the_repository, opts);
223 if (cmd == 's')
224 return sequencer_skip(the_repository, opts);
225 return sequencer_pick_revisions(the_repository, opts);
228 int cmd_revert(int argc, const char **argv, const char *prefix)
230 struct replay_opts opts = REPLAY_OPTS_INIT;
231 int res;
233 opts.action = REPLAY_REVERT;
234 sequencer_init_config(&opts);
235 res = run_sequencer(argc, argv, prefix, &opts);
236 if (res < 0)
237 die(_("revert failed"));
238 replay_opts_release(&opts);
239 return res;
242 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
244 struct replay_opts opts = REPLAY_OPTS_INIT;
245 int res;
247 opts.action = REPLAY_PICK;
248 sequencer_init_config(&opts);
249 res = run_sequencer(argc, argv, prefix, &opts);
250 if (res < 0)
251 die(_("cherry-pick failed"));
252 replay_opts_release(&opts);
253 return res;