revert: Separate cmdline parsing from functional code
[git/git.git] / builtin / revert.c
CommitLineData
9509af68
JS
1#include "cache.h"
2#include "builtin.h"
3#include "object.h"
4#include "commit.h"
5#include "tag.h"
9509af68
JS
6#include "run-command.h"
7#include "exec_cmd.h"
8#include "utf8.h"
f8103794 9#include "parse-options.h"
45525bd0 10#include "cache-tree.h"
0f2d4476
JK
11#include "diff.h"
12#include "revision.h"
aa1a0111 13#include "rerere.h"
6eb1b437 14#include "merge-recursive.h"
c62f6ec3 15#include "refs.h"
9509af68
JS
16
17/*
18 * This implements the builtins revert and cherry-pick.
19 *
20 * Copyright (c) 2007 Johannes E. Schindelin
21 *
22 * Based on git-revert.sh, which is
23 *
24 * Copyright (c) 2005 Linus Torvalds
25 * Copyright (c) 2005 Junio C Hamano
26 */
27
f8103794 28static const char * const revert_usage[] = {
1b1dd23f 29 "git revert [options] <commit-ish>",
f8103794
PH
30 NULL
31};
9509af68 32
f8103794 33static const char * const cherry_pick_usage[] = {
1b1dd23f 34 "git cherry-pick [options] <commit-ish>",
f8103794
PH
35 NULL
36};
9509af68 37
80e1f791
RR
38enum replay_action { REVERT, CHERRY_PICK };
39
40struct replay_opts {
41 enum replay_action action;
42
43 /* Boolean options */
44 int edit;
45 int record_origin;
46 int no_commit;
47 int signoff;
48 int allow_ff;
49 int allow_rerere_auto;
50
51 int mainline;
52 int commit_argc;
53 const char **commit_argv;
54
55 /* Merge strategy */
56 const char *strategy;
57 const char **xopts;
58 size_t xopts_nr, xopts_alloc;
59};
9509af68
JS
60
61#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
62
80e1f791
RR
63static const char *action_name(const struct replay_opts *opts)
64{
65 return opts->action == REVERT ? "revert" : "cherry-pick";
66}
67
d6856540
JN
68static char *get_encoding(const char *message);
69
80e1f791 70static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
e0ef8495 71{
80e1f791 72 return opts->action == REVERT ? revert_usage : cherry_pick_usage;
e0ef8495
JN
73}
74
67ac1e1d
JN
75static int option_parse_x(const struct option *opt,
76 const char *arg, int unset)
77{
80e1f791
RR
78 struct replay_opts **opts_ptr = opt->value;
79 struct replay_opts *opts = *opts_ptr;
80
67ac1e1d
JN
81 if (unset)
82 return 0;
83
80e1f791
RR
84 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
85 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
67ac1e1d
JN
86 return 0;
87}
88
80e1f791 89static void parse_args(int argc, const char **argv, struct replay_opts *opts)
9509af68 90{
80e1f791 91 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
f8103794
PH
92 int noop;
93 struct option options[] = {
80e1f791
RR
94 OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
95 OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
5e31075a
RR
96 { OPTION_BOOLEAN, 'r', NULL, &noop, NULL, "no-op (backward compatibility)",
97 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 0 },
80e1f791
RR
98 OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
99 OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
100 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
101 OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"),
102 OPT_CALLBACK('X', "strategy-option", &opts, "option",
67ac1e1d 103 "option for merge strategy", option_parse_x),
f8103794 104 OPT_END(),
c62f6ec3
JH
105 OPT_END(),
106 OPT_END(),
f8103794
PH
107 };
108
80e1f791 109 if (opts->action == CHERRY_PICK) {
c62f6ec3 110 struct option cp_extra[] = {
80e1f791
RR
111 OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"),
112 OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"),
c62f6ec3
JH
113 OPT_END(),
114 };
115 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
f30f71ce 116 die(_("program error"));
c62f6ec3
JH
117 }
118
80e1f791
RR
119 opts->commit_argc = parse_options(argc, argv, NULL, options, usage_str,
120 PARSE_OPT_KEEP_ARGV0 |
121 PARSE_OPT_KEEP_UNKNOWN);
122 if (opts->commit_argc < 2)
f8103794 123 usage_with_options(usage_str, options);
9509af68 124
80e1f791 125 opts->commit_argv = argv;
9509af68
JS
126}
127
d6856540
JN
128struct commit_message {
129 char *parent_label;
130 const char *label;
131 const char *subject;
132 char *reencoded_message;
133 const char *message;
134};
135
708f9d96 136static int get_message(struct commit *commit, struct commit_message *out)
9509af68 137{
d6856540 138 const char *encoding;
dfe7effe
CC
139 const char *abbrev, *subject;
140 int abbrev_len, subject_len;
d6856540 141 char *q;
9509af68 142
708f9d96 143 if (!commit->buffer)
d6856540 144 return -1;
708f9d96 145 encoding = get_encoding(commit->buffer);
d6856540
JN
146 if (!encoding)
147 encoding = "UTF-8";
148 if (!git_commit_encoding)
149 git_commit_encoding = "UTF-8";
43acff34
JN
150
151 out->reencoded_message = NULL;
708f9d96 152 out->message = commit->buffer;
43acff34 153 if (strcmp(encoding, git_commit_encoding))
708f9d96 154 out->reencoded_message = reencode_string(commit->buffer,
43acff34
JN
155 git_commit_encoding, encoding);
156 if (out->reencoded_message)
d6856540
JN
157 out->message = out->reencoded_message;
158
159 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
160 abbrev_len = strlen(abbrev);
161
dfe7effe 162 subject_len = find_commit_subject(out->message, &subject);
d6856540
JN
163
164 out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
dfe7effe 165 strlen("... ") + subject_len + 1);
d6856540
JN
166 q = out->parent_label;
167 q = mempcpy(q, "parent of ", strlen("parent of "));
168 out->label = q;
169 q = mempcpy(q, abbrev, abbrev_len);
170 q = mempcpy(q, "... ", strlen("... "));
171 out->subject = q;
dfe7effe 172 q = mempcpy(q, subject, subject_len);
d6856540
JN
173 *q = '\0';
174 return 0;
175}
176
177static void free_message(struct commit_message *msg)
178{
179 free(msg->parent_label);
180 free(msg->reencoded_message);
9509af68
JS
181}
182
52fae7de 183static char *get_encoding(const char *message)
9509af68
JS
184{
185 const char *p = message, *eol;
186
9509af68
JS
187 while (*p && *p != '\n') {
188 for (eol = p + 1; *eol && *eol != '\n'; eol++)
189 ; /* do nothing */
190 if (!prefixcmp(p, "encoding ")) {
191 char *result = xmalloc(eol - 8 - p);
192 strlcpy(result, p + 9, eol - 8 - p);
193 return result;
194 }
195 p = eol;
196 if (*p == '\n')
197 p++;
198 }
199 return NULL;
200}
201
708f9d96 202static void write_cherry_pick_head(struct commit *commit)
9509af68 203{
d7e5c0cb
JS
204 int fd;
205 struct strbuf buf = STRBUF_INIT;
206
207 strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1));
208
209 fd = open(git_path("CHERRY_PICK_HEAD"), O_WRONLY | O_CREAT, 0666);
210 if (fd < 0)
6c80cd29 211 die_errno(_("Could not open '%s' for writing"),
d7e5c0cb
JS
212 git_path("CHERRY_PICK_HEAD"));
213 if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd))
6c80cd29 214 die_errno(_("Could not write to '%s'"), git_path("CHERRY_PICK_HEAD"));
d7e5c0cb 215 strbuf_release(&buf);
9509af68
JS
216}
217
314eeb6e 218static void print_advice(void)
804c7174 219{
804c7174 220 char *msg = getenv("GIT_CHERRY_PICK_HELP");
804c7174 221
314eeb6e
JN
222 if (msg) {
223 fprintf(stderr, "%s\n", msg);
d7e5c0cb
JS
224 /*
225 * A conflict has occured but the porcelain
226 * (typically rebase --interactive) wants to take care
227 * of the commit itself so remove CHERRY_PICK_HEAD
228 */
229 unlink(git_path("CHERRY_PICK_HEAD"));
314eeb6e 230 return;
804c7174 231 }
804c7174 232
314eeb6e
JN
233 advise("after resolving the conflicts, mark the corrected paths");
234 advise("with 'git add <paths>' or 'git rm <paths>'");
37f7a857 235 advise("and commit the result with 'git commit'");
804c7174
WC
236}
237
bc84a7fb
CC
238static void write_message(struct strbuf *msgbuf, const char *filename)
239{
240 static struct lock_file msg_file;
241
242 int msg_fd = hold_lock_file_for_update(&msg_file, filename,
243 LOCK_DIE_ON_ERROR);
244 if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
f30f71ce 245 die_errno(_("Could not write to %s."), filename);
bc84a7fb
CC
246 strbuf_release(msgbuf);
247 if (commit_lock_file(&msg_file) < 0)
f30f71ce 248 die(_("Error wrapping up %s"), filename);
bc84a7fb
CC
249}
250
6eb1b437
MV
251static struct tree *empty_tree(void)
252{
253 struct tree *tree = xcalloc(1, sizeof(struct tree));
254
255 tree->object.parsed = 1;
256 tree->object.type = OBJ_TREE;
257 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
258 return tree;
259}
260
80e1f791 261static NORETURN void die_dirty_index(struct replay_opts *opts)
d38a30df
MM
262{
263 if (read_cache_unmerged()) {
80e1f791 264 die_resolve_conflict(action_name(opts));
d38a30df 265 } else {
d3ee177e 266 if (advice_commit_before_merge) {
80e1f791 267 if (opts->action == REVERT)
d3ee177e
ÆAB
268 die(_("Your local changes would be overwritten by revert.\n"
269 "Please, commit your changes or stash them to proceed."));
270 else
271 die(_("Your local changes would be overwritten by cherry-pick.\n"
272 "Please, commit your changes or stash them to proceed."));
273 } else {
80e1f791 274 if (opts->action == REVERT)
d3ee177e
ÆAB
275 die(_("Your local changes would be overwritten by revert.\n"));
276 else
277 die(_("Your local changes would be overwritten by cherry-pick.\n"));
278 }
d38a30df
MM
279 }
280}
281
c62f6ec3
JH
282static int fast_forward_to(const unsigned char *to, const unsigned char *from)
283{
284 struct ref_lock *ref_lock;
285
286 read_cache();
287 if (checkout_fast_forward(from, to))
288 exit(1); /* the callee should have complained already */
289 ref_lock = lock_any_ref_for_update("HEAD", from, 0);
290 return write_ref_sha1(ref_lock, to, "cherry-pick");
291}
292
7b53b92f
CC
293static int do_recursive_merge(struct commit *base, struct commit *next,
294 const char *base_label, const char *next_label,
80e1f791
RR
295 unsigned char *head, struct strbuf *msgbuf,
296 struct replay_opts *opts)
ae8c79fd
CC
297{
298 struct merge_options o;
299 struct tree *result, *next_tree, *base_tree, *head_tree;
300 int clean, index_fd;
67ac1e1d 301 const char **xopt;
ae8c79fd
CC
302 static struct lock_file index_lock;
303
304 index_fd = hold_locked_index(&index_lock, 1);
305
306 read_cache();
7610fa57 307
ae8c79fd
CC
308 init_merge_options(&o);
309 o.ancestor = base ? base_label : "(empty tree)";
310 o.branch1 = "HEAD";
311 o.branch2 = next ? next_label : "(empty tree)";
312
313 head_tree = parse_tree_indirect(head);
314 next_tree = next ? next->tree : empty_tree();
315 base_tree = base ? base->tree : empty_tree();
316
80e1f791 317 for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++)
67ac1e1d
JN
318 parse_merge_opt(&o, *xopt);
319
ae8c79fd
CC
320 clean = merge_trees(&o,
321 head_tree,
322 next_tree, base_tree, &result);
323
324 if (active_cache_changed &&
325 (write_cache(index_fd, active_cache, active_nr) ||
326 commit_locked_index(&index_lock)))
b9c993e0 327 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
80e1f791 328 die(_("%s: Unable to write new index file"), action_name(opts));
ae8c79fd
CC
329 rollback_lock_file(&index_lock);
330
331 if (!clean) {
332 int i;
333 strbuf_addstr(msgbuf, "\nConflicts:\n\n");
334 for (i = 0; i < active_nr;) {
335 struct cache_entry *ce = active_cache[i++];
336 if (ce_stage(ce)) {
337 strbuf_addch(msgbuf, '\t');
338 strbuf_addstr(msgbuf, ce->name);
339 strbuf_addch(msgbuf, '\n');
340 while (i < active_nr && !strcmp(ce->name,
341 active_cache[i]->name))
342 i++;
343 }
344 }
ae8c79fd 345 }
7b53b92f
CC
346
347 return !clean;
ae8c79fd
CC
348}
349
5df16453
CC
350/*
351 * If we are cherry-pick, and if the merge did not result in
352 * hand-editing, we will hit this commit and inherit the original
353 * author date and name.
354 * If we are revert, or if our cherry-pick results in a hand merge,
355 * we had better say that the current user is responsible for that.
356 */
80e1f791 357static int run_git_commit(const char *defmsg, struct replay_opts *opts)
5df16453
CC
358{
359 /* 6 is max possible length of our args array including NULL */
360 const char *args[6];
361 int i = 0;
362
363 args[i++] = "commit";
364 args[i++] = "-n";
80e1f791 365 if (opts->signoff)
5df16453 366 args[i++] = "-s";
80e1f791 367 if (!opts->edit) {
5df16453
CC
368 args[i++] = "-F";
369 args[i++] = defmsg;
370 }
371 args[i] = NULL;
372
373 return run_command_v_opt(args, RUN_GIT_CMD);
ae8c79fd
CC
374}
375
80e1f791 376static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
9509af68
JS
377{
378 unsigned char head[20];
7791ecbc 379 struct commit *base, *next, *parent;
bf975d37 380 const char *base_label, *next_label;
d6856540 381 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
28db756f 382 char *defmsg = NULL;
bc84a7fb 383 struct strbuf msgbuf = STRBUF_INIT;
7b53b92f 384 int res;
9509af68 385
80e1f791 386 if (opts->no_commit) {
9509af68
JS
387 /*
388 * We do not intend to commit immediately. We just want to
71aa2b8f
JH
389 * merge the differences in, so let's compute the tree
390 * that represents the "current" state for merge-recursive
391 * to work on.
9509af68 392 */
45525bd0 393 if (write_cache_as_tree(head, 0, NULL))
f30f71ce 394 die (_("Your index file is unmerged."));
9509af68 395 } else {
9509af68 396 if (get_sha1("HEAD", head))
f30f71ce 397 die (_("You do not have a valid HEAD"));
75f3ff2e 398 if (index_differs_from("HEAD", 0))
80e1f791 399 die_dirty_index(opts);
9509af68 400 }
6eb1b437
MV
401 discard_cache();
402
f95ebf74 403 if (!commit->parents) {
f95ebf74
JS
404 parent = NULL;
405 }
406 else if (commit->parents->next) {
7791ecbc
JH
407 /* Reverting or cherry-picking a merge commit */
408 int cnt;
409 struct commit_list *p;
410
80e1f791 411 if (!opts->mainline)
f30f71ce 412 die(_("Commit %s is a merge but no -m option was given."),
7791ecbc
JH
413 sha1_to_hex(commit->object.sha1));
414
415 for (cnt = 1, p = commit->parents;
80e1f791 416 cnt != opts->mainline && p;
7791ecbc
JH
417 cnt++)
418 p = p->next;
80e1f791 419 if (cnt != opts->mainline || !p)
f30f71ce 420 die(_("Commit %s does not have parent %d"),
80e1f791 421 sha1_to_hex(commit->object.sha1), opts->mainline);
7791ecbc 422 parent = p->item;
80e1f791 423 } else if (0 < opts->mainline)
f30f71ce 424 die(_("Mainline was specified but commit %s is not a merge."),
7791ecbc
JH
425 sha1_to_hex(commit->object.sha1));
426 else
427 parent = commit->parents->item;
428
80e1f791 429 if (opts->allow_ff && parent && !hashcmp(parent->object.sha1, head))
c62f6ec3
JH
430 return fast_forward_to(commit->object.sha1, head);
431
6eb1b437 432 if (parent && parse_commit(parent) < 0)
b9c993e0
ÆAB
433 /* TRANSLATORS: The first %s will be "revert" or
434 "cherry-pick", the second %s a SHA1 */
435 die(_("%s: cannot parse parent commit %s"),
80e1f791 436 action_name(opts), sha1_to_hex(parent->object.sha1));
6eb1b437 437
708f9d96 438 if (get_message(commit, &msg) != 0)
f30f71ce 439 die(_("Cannot get commit message for %s"),
d6856540
JN
440 sha1_to_hex(commit->object.sha1));
441
9509af68
JS
442 /*
443 * "commit" is an existing commit. We would want to apply
444 * the difference it introduces since its first parent "prev"
445 * on top of the current HEAD if we are cherry-pick. Or the
446 * reverse of it if we are revert.
447 */
448
28db756f 449 defmsg = git_pathdup("MERGE_MSG");
c62f6ec3 450
80e1f791 451 if (opts->action == REVERT) {
9509af68 452 base = commit;
bf975d37 453 base_label = msg.label;
7791ecbc 454 next = parent;
d6856540 455 next_label = msg.parent_label;
bc84a7fb
CC
456 strbuf_addstr(&msgbuf, "Revert \"");
457 strbuf_addstr(&msgbuf, msg.subject);
458 strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
459 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
d5be89d8 460
fad26526 461 if (commit->parents && commit->parents->next) {
bc84a7fb
CC
462 strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
463 strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1));
d5be89d8 464 }
bc84a7fb 465 strbuf_addstr(&msgbuf, ".\n");
9509af68 466 } else {
be33c46c
RR
467 const char *p;
468
7791ecbc 469 base = parent;
bf975d37 470 base_label = msg.parent_label;
9509af68 471 next = commit;
d6856540 472 next_label = msg.label;
be33c46c
RR
473
474 /*
475 * Append the commit log message to msgbuf; it starts
476 * after the tree, parent, author, committer
477 * information followed by "\n\n".
478 */
479 p = strstr(msg.message, "\n\n");
480 if (p) {
481 p += 2;
482 strbuf_addstr(&msgbuf, p);
483 }
484
80e1f791 485 if (opts->record_origin) {
bc84a7fb
CC
486 strbuf_addstr(&msgbuf, "(cherry picked from commit ");
487 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
488 strbuf_addstr(&msgbuf, ")\n");
9509af68 489 }
80e1f791 490 if (!opts->no_commit)
708f9d96 491 write_cherry_pick_head(commit);
9509af68 492 }
9509af68 493
80e1f791 494 if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REVERT) {
7b53b92f 495 res = do_recursive_merge(base, next, base_label, next_label,
80e1f791 496 head, &msgbuf, opts);
7b53b92f
CC
497 write_message(&msgbuf, defmsg);
498 } else {
91e52598
CC
499 struct commit_list *common = NULL;
500 struct commit_list *remotes = NULL;
7b53b92f 501
91e52598 502 write_message(&msgbuf, defmsg);
7b53b92f 503
91e52598
CC
504 commit_list_insert(base, &common);
505 commit_list_insert(next, &remotes);
80e1f791
RR
506 res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts,
507 common, sha1_to_hex(head), remotes);
91e52598
CC
508 free_commit_list(common);
509 free_commit_list(remotes);
9509af68 510 }
9509af68 511
7b53b92f 512 if (res) {
80e1f791 513 error(opts->action == REVERT
cb6aeb22
ÆAB
514 ? _("could not revert %s... %s")
515 : _("could not apply %s... %s"),
981ff5c3
JN
516 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
517 msg.subject);
314eeb6e 518 print_advice();
80e1f791 519 rerere(opts->allow_rerere_auto);
7b53b92f 520 } else {
80e1f791
RR
521 if (!opts->no_commit)
522 res = run_git_commit(defmsg, opts);
9509af68 523 }
2fb0e14f 524
2fb0e14f 525 free_message(&msg);
d258b258 526 free(defmsg);
9509af68 527
3b2c5b6d 528 return res;
9509af68
JS
529}
530
80e1f791 531static void prepare_revs(struct rev_info *revs, struct replay_opts *opts)
7e2bfd3f 532{
e0ef8495 533 int argc;
7e2bfd3f 534
e0ef8495
JN
535 init_revisions(revs, NULL);
536 revs->no_walk = 1;
80e1f791 537 if (opts->action != REVERT)
e0ef8495
JN
538 revs->reverse = 1;
539
80e1f791 540 argc = setup_revisions(opts->commit_argc, opts->commit_argv, revs, NULL);
e0ef8495 541 if (argc > 1)
80e1f791 542 usage(*revert_or_cherry_pick_usage(opts));
7e2bfd3f 543
7e2bfd3f 544 if (prepare_revision_walk(revs))
f30f71ce 545 die(_("revision walk setup failed"));
7e2bfd3f
CC
546
547 if (!revs->commits)
f30f71ce 548 die(_("empty commit set passed"));
7e2bfd3f
CC
549}
550
80e1f791 551static void read_and_refresh_cache(struct replay_opts *opts)
f6ce1f25
JN
552{
553 static struct lock_file index_lock;
554 int index_fd = hold_locked_index(&index_lock, 0);
555 if (read_index_preload(&the_index, NULL) < 0)
80e1f791 556 die(_("git %s: failed to read the index"), action_name(opts));
f6ce1f25
JN
557 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
558 if (the_index.cache_changed) {
559 if (write_index(&the_index, index_fd) ||
560 commit_locked_index(&index_lock))
80e1f791 561 die(_("git %s: failed to refresh the index"), action_name(opts));
f6ce1f25
JN
562 }
563 rollback_lock_file(&index_lock);
564}
565
89641472 566static int pick_commits(struct replay_opts *opts)
7af46595 567{
7e2bfd3f 568 struct rev_info revs;
708f9d96 569 struct commit *commit;
7e2bfd3f 570
80e1f791 571 setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
80e1f791
RR
572 if (opts->allow_ff) {
573 if (opts->signoff)
f30f71ce 574 die(_("cherry-pick --ff cannot be used with --signoff"));
80e1f791 575 if (opts->no_commit)
f30f71ce 576 die(_("cherry-pick --ff cannot be used with --no-commit"));
80e1f791 577 if (opts->record_origin)
f30f71ce 578 die(_("cherry-pick --ff cannot be used with -x"));
80e1f791 579 if (opts->edit)
f30f71ce 580 die(_("cherry-pick --ff cannot be used with --edit"));
7af46595
CC
581 }
582
80e1f791 583 read_and_refresh_cache(opts);
7af46595 584
80e1f791 585 prepare_revs(&revs, opts);
7e2bfd3f
CC
586
587 while ((commit = get_revision(&revs))) {
80e1f791 588 int res = do_pick_commit(commit, opts);
7e2bfd3f
CC
589 if (res)
590 return res;
591 }
592
593 return 0;
7af46595
CC
594}
595
9509af68
JS
596int cmd_revert(int argc, const char **argv, const char *prefix)
597{
80e1f791
RR
598 struct replay_opts opts;
599
600 memset(&opts, 0, sizeof(opts));
9509af68 601 if (isatty(0))
80e1f791
RR
602 opts.edit = 1;
603 opts.action = REVERT;
89641472
RR
604 git_config(git_default_config, NULL);
605 parse_args(argc, argv, &opts);
606 return pick_commits(&opts);
9509af68
JS
607}
608
609int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
610{
80e1f791
RR
611 struct replay_opts opts;
612
613 memset(&opts, 0, sizeof(opts));
614 opts.action = CHERRY_PICK;
89641472
RR
615 git_config(git_default_config, NULL);
616 parse_args(argc, argv, &opts);
617 return pick_commits(&opts);
9509af68 618}