ci: remove the pipe after "p4 -V" to catch errors
[alt-git.git] / builtin / revert.c
blob314a86c5621bd26b7ddcd35cab1a21702453a2ca
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "parse-options.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "rerere.h"
8 #include "dir.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 [<options>] <commit-ish>..."),
25 N_("git revert <subcommand>"),
26 NULL
29 static const char * const cherry_pick_usage[] = {
30 N_("git cherry-pick [<options>] <commit-ish>..."),
31 N_("git cherry-pick <subcommand>"),
32 NULL
35 static const char *action_name(const struct replay_opts *opts)
37 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
40 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
42 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
45 static int option_parse_x(const struct option *opt,
46 const char *arg, int unset)
48 struct replay_opts **opts_ptr = opt->value;
49 struct replay_opts *opts = *opts_ptr;
51 if (unset)
52 return 0;
54 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
55 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
56 return 0;
59 static int option_parse_m(const struct option *opt,
60 const char *arg, int unset)
62 struct replay_opts *replay = opt->value;
63 char *end;
65 if (unset) {
66 replay->mainline = 0;
67 return 0;
70 replay->mainline = strtol(arg, &end, 10);
71 if (*end || replay->mainline <= 0)
72 return error(_("option `%s' expects a number greater than zero"),
73 opt->long_name);
75 return 0;
78 LAST_ARG_MUST_BE_NULL
79 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
81 const char *this_opt;
82 va_list ap;
84 va_start(ap, base_opt);
85 while ((this_opt = va_arg(ap, const char *))) {
86 if (va_arg(ap, int))
87 break;
89 va_end(ap);
91 if (this_opt)
92 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
95 static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
97 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
98 const char *me = action_name(opts);
99 const char *cleanup_arg = NULL;
100 int cmd = 0;
101 struct option base_options[] = {
102 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
103 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
104 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
105 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
106 OPT_CLEANUP(&cleanup_arg),
107 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
108 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
109 OPT_NOOP_NOARG('r', NULL),
110 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
111 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
112 N_("select mainline parent"), option_parse_m),
113 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
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),
117 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
118 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
119 OPT_END()
121 struct option *options = base_options;
123 if (opts->action == REPLAY_PICK) {
124 struct option cp_extra[] = {
125 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
126 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
127 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
128 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
129 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
130 OPT_END(),
132 options = parse_options_concat(options, cp_extra);
135 argc = parse_options(argc, argv, NULL, options, usage_str,
136 PARSE_OPT_KEEP_ARGV0 |
137 PARSE_OPT_KEEP_UNKNOWN);
139 /* implies allow_empty */
140 if (opts->keep_redundant_commits)
141 opts->allow_empty = 1;
143 if (cleanup_arg) {
144 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
145 opts->explicit_cleanup = 1;
148 /* Check for incompatible command line arguments */
149 if (cmd) {
150 char *this_operation;
151 if (cmd == 'q')
152 this_operation = "--quit";
153 else if (cmd == 'c')
154 this_operation = "--continue";
155 else if (cmd == 's')
156 this_operation = "--skip";
157 else {
158 assert(cmd == 'a');
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,
170 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
171 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
172 NULL);
175 if (!opts->strategy && opts->default_strategy) {
176 opts->strategy = opts->default_strategy;
177 opts->default_strategy = NULL;
180 if (opts->allow_ff)
181 verify_opt_compatible(me, "--ff",
182 "--signoff", opts->signoff,
183 "--no-commit", opts->no_commit,
184 "-x", opts->record_origin,
185 "--edit", opts->edit,
186 NULL);
188 if (cmd) {
189 opts->revs = NULL;
190 } else {
191 struct setup_revision_opt s_r_opt;
192 opts->revs = xmalloc(sizeof(*opts->revs));
193 repo_init_revisions(the_repository, opts->revs, NULL);
194 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
195 if (argc < 2)
196 usage_with_options(usage_str, options);
197 if (!strcmp(argv[1], "-"))
198 argv[1] = "@{-1}";
199 memset(&s_r_opt, 0, sizeof(s_r_opt));
200 s_r_opt.assume_dashdash = 1;
201 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
204 if (argc > 1)
205 usage_with_options(usage_str, options);
207 /* These option values will be free()d */
208 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
209 opts->strategy = xstrdup_or_null(opts->strategy);
210 if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
211 opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
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 if (isatty(0))
234 opts.edit = 1;
235 opts.action = REPLAY_REVERT;
236 sequencer_init_config(&opts);
237 res = run_sequencer(argc, argv, &opts);
238 if (res < 0)
239 die(_("revert failed"));
240 return res;
243 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
245 struct replay_opts opts = REPLAY_OPTS_INIT;
246 int res;
248 opts.action = REPLAY_PICK;
249 sequencer_init_config(&opts);
250 res = run_sequencer(argc, argv, &opts);
251 if (res < 0)
252 die(_("cherry-pick failed"));
253 return res;