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