7 #include "run-command.h"
10 #include "cache-tree.h"
14 #include "merge-recursive.h"
17 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
19 void remove_sequencer_state(void)
21 struct strbuf seq_dir = STRBUF_INIT;
23 strbuf_addf(&seq_dir, "%s", git_path(SEQ_DIR));
24 remove_dir_recursively(&seq_dir, 0);
25 strbuf_release(&seq_dir);
28 static const char *action_name(const struct replay_opts *opts)
30 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
33 static char *get_encoding(const char *message);
35 struct commit_message {
39 char *reencoded_message;
43 static int get_message(struct commit *commit, struct commit_message *out)
46 const char *abbrev, *subject;
47 int abbrev_len, subject_len;
52 encoding = get_encoding(commit->buffer);
55 if (!git_commit_encoding)
56 git_commit_encoding = "UTF-8";
58 out->reencoded_message = NULL;
59 out->message = commit->buffer;
60 if (strcmp(encoding, git_commit_encoding))
61 out->reencoded_message = reencode_string(commit->buffer,
62 git_commit_encoding, encoding);
63 if (out->reencoded_message)
64 out->message = out->reencoded_message;
66 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
67 abbrev_len = strlen(abbrev);
69 subject_len = find_commit_subject(out->message, &subject);
71 out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
72 strlen("... ") + subject_len + 1);
73 q = out->parent_label;
74 q = mempcpy(q, "parent of ", strlen("parent of "));
76 q = mempcpy(q, abbrev, abbrev_len);
77 q = mempcpy(q, "... ", strlen("... "));
79 q = mempcpy(q, subject, subject_len);
84 static void free_message(struct commit_message *msg)
86 free(msg->parent_label);
87 free(msg->reencoded_message);
90 static char *get_encoding(const char *message)
92 const char *p = message, *eol;
94 while (*p && *p != '\n') {
95 for (eol = p + 1; *eol && *eol != '\n'; eol++)
97 if (!prefixcmp(p, "encoding ")) {
98 char *result = xmalloc(eol - 8 - p);
99 strlcpy(result, p + 9, eol - 8 - p);
109 static void write_cherry_pick_head(struct commit *commit, const char *pseudoref)
111 const char *filename;
113 struct strbuf buf = STRBUF_INIT;
115 strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1));
117 filename = git_path("%s", pseudoref);
118 fd = open(filename, O_WRONLY | O_CREAT, 0666);
120 die_errno(_("Could not open '%s' for writing"), filename);
121 if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd))
122 die_errno(_("Could not write to '%s'"), filename);
123 strbuf_release(&buf);
126 static void print_advice(int show_hint)
128 char *msg = getenv("GIT_CHERRY_PICK_HELP");
131 fprintf(stderr, "%s\n", msg);
133 * A conflict has occured but the porcelain
134 * (typically rebase --interactive) wants to take care
135 * of the commit itself so remove CHERRY_PICK_HEAD
137 unlink(git_path("CHERRY_PICK_HEAD"));
142 advise(_("after resolving the conflicts, mark the corrected paths\n"
143 "with 'git add <paths>' or 'git rm <paths>'\n"
144 "and commit the result with 'git commit'"));
147 static void write_message(struct strbuf *msgbuf, const char *filename)
149 static struct lock_file msg_file;
151 int msg_fd = hold_lock_file_for_update(&msg_file, filename,
153 if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
154 die_errno(_("Could not write to %s"), filename);
155 strbuf_release(msgbuf);
156 if (commit_lock_file(&msg_file) < 0)
157 die(_("Error wrapping up %s"), filename);
160 static struct tree *empty_tree(void)
162 return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN);
165 static int error_dirty_index(struct replay_opts *opts)
167 if (read_cache_unmerged())
168 return error_resolve_conflict(action_name(opts));
170 /* Different translation strings for cherry-pick and revert */
171 if (opts->action == REPLAY_PICK)
172 error(_("Your local changes would be overwritten by cherry-pick."));
174 error(_("Your local changes would be overwritten by revert."));
176 if (advice_commit_before_merge)
177 advise(_("Commit your changes or stash them to proceed."));
181 static int fast_forward_to(const unsigned char *to, const unsigned char *from)
183 struct ref_lock *ref_lock;
186 if (checkout_fast_forward(from, to))
187 exit(1); /* the callee should have complained already */
188 ref_lock = lock_any_ref_for_update("HEAD", from, 0);
189 return write_ref_sha1(ref_lock, to, "cherry-pick");
192 static int do_recursive_merge(struct commit *base, struct commit *next,
193 const char *base_label, const char *next_label,
194 unsigned char *head, struct strbuf *msgbuf,
195 struct replay_opts *opts)
197 struct merge_options o;
198 struct tree *result, *next_tree, *base_tree, *head_tree;
201 static struct lock_file index_lock;
203 index_fd = hold_locked_index(&index_lock, 1);
207 init_merge_options(&o);
208 o.ancestor = base ? base_label : "(empty tree)";
210 o.branch2 = next ? next_label : "(empty tree)";
212 head_tree = parse_tree_indirect(head);
213 next_tree = next ? next->tree : empty_tree();
214 base_tree = base ? base->tree : empty_tree();
216 for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++)
217 parse_merge_opt(&o, *xopt);
219 clean = merge_trees(&o,
221 next_tree, base_tree, &result);
223 if (active_cache_changed &&
224 (write_cache(index_fd, active_cache, active_nr) ||
225 commit_locked_index(&index_lock)))
226 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
227 die(_("%s: Unable to write new index file"), action_name(opts));
228 rollback_lock_file(&index_lock);
232 strbuf_addstr(msgbuf, "\nConflicts:\n\n");
233 for (i = 0; i < active_nr;) {
234 struct cache_entry *ce = active_cache[i++];
236 strbuf_addch(msgbuf, '\t');
237 strbuf_addstr(msgbuf, ce->name);
238 strbuf_addch(msgbuf, '\n');
239 while (i < active_nr && !strcmp(ce->name,
240 active_cache[i]->name))
250 * If we are cherry-pick, and if the merge did not result in
251 * hand-editing, we will hit this commit and inherit the original
252 * author date and name.
253 * If we are revert, or if our cherry-pick results in a hand merge,
254 * we had better say that the current user is responsible for that.
256 static int run_git_commit(const char *defmsg, struct replay_opts *opts)
258 /* 6 is max possible length of our args array including NULL */
262 args[i++] = "commit";
272 return run_command_v_opt(args, RUN_GIT_CMD);
275 static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
277 unsigned char head[20];
278 struct commit *base, *next, *parent;
279 const char *base_label, *next_label;
280 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
282 struct strbuf msgbuf = STRBUF_INIT;
285 if (opts->no_commit) {
287 * We do not intend to commit immediately. We just want to
288 * merge the differences in, so let's compute the tree
289 * that represents the "current" state for merge-recursive
292 if (write_cache_as_tree(head, 0, NULL))
293 die (_("Your index file is unmerged."));
295 if (get_sha1("HEAD", head))
296 return error(_("You do not have a valid HEAD"));
297 if (index_differs_from("HEAD", 0))
298 return error_dirty_index(opts);
302 if (!commit->parents) {
305 else if (commit->parents->next) {
306 /* Reverting or cherry-picking a merge commit */
308 struct commit_list *p;
311 return error(_("Commit %s is a merge but no -m option was given."),
312 sha1_to_hex(commit->object.sha1));
314 for (cnt = 1, p = commit->parents;
315 cnt != opts->mainline && p;
318 if (cnt != opts->mainline || !p)
319 return error(_("Commit %s does not have parent %d"),
320 sha1_to_hex(commit->object.sha1), opts->mainline);
322 } else if (0 < opts->mainline)
323 return error(_("Mainline was specified but commit %s is not a merge."),
324 sha1_to_hex(commit->object.sha1));
326 parent = commit->parents->item;
328 if (opts->allow_ff && parent && !hashcmp(parent->object.sha1, head))
329 return fast_forward_to(commit->object.sha1, head);
331 if (parent && parse_commit(parent) < 0)
332 /* TRANSLATORS: The first %s will be "revert" or
333 "cherry-pick", the second %s a SHA1 */
334 return error(_("%s: cannot parse parent commit %s"),
335 action_name(opts), sha1_to_hex(parent->object.sha1));
337 if (get_message(commit, &msg) != 0)
338 return error(_("Cannot get commit message for %s"),
339 sha1_to_hex(commit->object.sha1));
342 * "commit" is an existing commit. We would want to apply
343 * the difference it introduces since its first parent "prev"
344 * on top of the current HEAD if we are cherry-pick. Or the
345 * reverse of it if we are revert.
348 defmsg = git_pathdup("MERGE_MSG");
350 if (opts->action == REPLAY_REVERT) {
352 base_label = msg.label;
354 next_label = msg.parent_label;
355 strbuf_addstr(&msgbuf, "Revert \"");
356 strbuf_addstr(&msgbuf, msg.subject);
357 strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
358 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
360 if (commit->parents && commit->parents->next) {
361 strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
362 strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1));
364 strbuf_addstr(&msgbuf, ".\n");
369 base_label = msg.parent_label;
371 next_label = msg.label;
374 * Append the commit log message to msgbuf; it starts
375 * after the tree, parent, author, committer
376 * information followed by "\n\n".
378 p = strstr(msg.message, "\n\n");
381 strbuf_addstr(&msgbuf, p);
384 if (opts->record_origin) {
385 strbuf_addstr(&msgbuf, "(cherry picked from commit ");
386 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
387 strbuf_addstr(&msgbuf, ")\n");
391 if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) {
392 res = do_recursive_merge(base, next, base_label, next_label,
393 head, &msgbuf, opts);
394 write_message(&msgbuf, defmsg);
396 struct commit_list *common = NULL;
397 struct commit_list *remotes = NULL;
399 write_message(&msgbuf, defmsg);
401 commit_list_insert(base, &common);
402 commit_list_insert(next, &remotes);
403 res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts,
404 common, sha1_to_hex(head), remotes);
405 free_commit_list(common);
406 free_commit_list(remotes);
410 * If the merge was clean or if it failed due to conflict, we write
411 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
412 * However, if the merge did not even start, then we don't want to
415 if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1))
416 write_cherry_pick_head(commit, "CHERRY_PICK_HEAD");
417 if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1))
418 write_cherry_pick_head(commit, "REVERT_HEAD");
421 error(opts->action == REPLAY_REVERT
422 ? _("could not revert %s... %s")
423 : _("could not apply %s... %s"),
424 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
426 print_advice(res == 1);
427 rerere(opts->allow_rerere_auto);
429 if (!opts->no_commit)
430 res = run_git_commit(defmsg, opts);
439 static void prepare_revs(struct replay_opts *opts)
441 if (opts->action != REPLAY_REVERT)
442 opts->revs->reverse ^= 1;
444 if (prepare_revision_walk(opts->revs))
445 die(_("revision walk setup failed"));
447 if (!opts->revs->commits)
448 die(_("empty commit set passed"));
451 static void read_and_refresh_cache(struct replay_opts *opts)
453 static struct lock_file index_lock;
454 int index_fd = hold_locked_index(&index_lock, 0);
455 if (read_index_preload(&the_index, NULL) < 0)
456 die(_("git %s: failed to read the index"), action_name(opts));
457 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
458 if (the_index.cache_changed) {
459 if (write_index(&the_index, index_fd) ||
460 commit_locked_index(&index_lock))
461 die(_("git %s: failed to refresh the index"), action_name(opts));
463 rollback_lock_file(&index_lock);
467 * Append a commit to the end of the commit_list.
469 * next starts by pointing to the variable that holds the head of an
470 * empty commit_list, and is updated to point to the "next" field of
471 * the last item on the list as new commits are appended.
475 * struct commit_list *list;
476 * struct commit_list **next = &list;
478 * next = commit_list_append(c1, next);
479 * next = commit_list_append(c2, next);
480 * assert(commit_list_count(list) == 2);
483 static struct commit_list **commit_list_append(struct commit *commit,
484 struct commit_list **next)
486 struct commit_list *new = xmalloc(sizeof(struct commit_list));
493 static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
494 struct replay_opts *opts)
496 struct commit_list *cur = NULL;
497 const char *sha1_abbrev = NULL;
498 const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick";
502 for (cur = todo_list; cur; cur = cur->next) {
503 sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV);
504 subject_len = find_commit_subject(cur->item->buffer, &subject);
505 strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
506 subject_len, subject);
511 static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
513 unsigned char commit_sha1[20];
514 enum replay_action action;
515 char *end_of_object_name;
516 int saved, status, padding;
518 if (!prefixcmp(bol, "pick")) {
519 action = REPLAY_PICK;
520 bol += strlen("pick");
521 } else if (!prefixcmp(bol, "revert")) {
522 action = REPLAY_REVERT;
523 bol += strlen("revert");
527 /* Eat up extra spaces/ tabs before object name */
528 padding = strspn(bol, " \t");
533 end_of_object_name = bol + strcspn(bol, " \t\n");
534 saved = *end_of_object_name;
535 *end_of_object_name = '\0';
536 status = get_sha1(bol, commit_sha1);
537 *end_of_object_name = saved;
540 * Verify that the action matches up with the one in
541 * opts; we don't support arbitrary instructions
543 if (action != opts->action) {
544 const char *action_str;
545 action_str = action == REPLAY_REVERT ? "revert" : "cherry-pick";
546 error(_("Cannot %s during a %s"), action_str, action_name(opts));
553 return lookup_commit_reference(commit_sha1);
556 static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
557 struct replay_opts *opts)
559 struct commit_list **next = todo_list;
560 struct commit *commit;
564 for (i = 1; *p; i++) {
565 char *eol = strchrnul(p, '\n');
566 commit = parse_insn_line(p, eol, opts);
568 return error(_("Could not parse line %d."), i);
569 next = commit_list_append(commit, next);
570 p = *eol ? eol + 1 : eol;
573 return error(_("No commits parsed."));
577 static void read_populate_todo(struct commit_list **todo_list,
578 struct replay_opts *opts)
580 const char *todo_file = git_path(SEQ_TODO_FILE);
581 struct strbuf buf = STRBUF_INIT;
584 fd = open(todo_file, O_RDONLY);
586 die_errno(_("Could not open %s"), todo_file);
587 if (strbuf_read(&buf, fd, 0) < 0) {
589 strbuf_release(&buf);
590 die(_("Could not read %s."), todo_file);
594 res = parse_insn_buffer(buf.buf, todo_list, opts);
595 strbuf_release(&buf);
597 die(_("Unusable instruction sheet: %s"), todo_file);
600 static int populate_opts_cb(const char *key, const char *value, void *data)
602 struct replay_opts *opts = data;
607 else if (!strcmp(key, "options.no-commit"))
608 opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
609 else if (!strcmp(key, "options.edit"))
610 opts->edit = git_config_bool_or_int(key, value, &error_flag);
611 else if (!strcmp(key, "options.signoff"))
612 opts->signoff = git_config_bool_or_int(key, value, &error_flag);
613 else if (!strcmp(key, "options.record-origin"))
614 opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
615 else if (!strcmp(key, "options.allow-ff"))
616 opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
617 else if (!strcmp(key, "options.mainline"))
618 opts->mainline = git_config_int(key, value);
619 else if (!strcmp(key, "options.strategy"))
620 git_config_string(&opts->strategy, key, value);
621 else if (!strcmp(key, "options.strategy-option")) {
622 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
623 opts->xopts[opts->xopts_nr++] = xstrdup(value);
625 return error(_("Invalid key: %s"), key);
628 return error(_("Invalid value for %s: %s"), key, value);
633 static void read_populate_opts(struct replay_opts **opts_ptr)
635 const char *opts_file = git_path(SEQ_OPTS_FILE);
637 if (!file_exists(opts_file))
639 if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0)
640 die(_("Malformed options sheet: %s"), opts_file);
643 static void walk_revs_populate_todo(struct commit_list **todo_list,
644 struct replay_opts *opts)
646 struct commit *commit;
647 struct commit_list **next;
652 while ((commit = get_revision(opts->revs)))
653 next = commit_list_append(commit, next);
656 static int create_seq_dir(void)
658 const char *seq_dir = git_path(SEQ_DIR);
660 if (file_exists(seq_dir)) {
661 error(_("a cherry-pick or revert is already in progress"));
662 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
665 else if (mkdir(seq_dir, 0777) < 0)
666 die_errno(_("Could not create sequencer directory %s"), seq_dir);
670 static void save_head(const char *head)
672 const char *head_file = git_path(SEQ_HEAD_FILE);
673 static struct lock_file head_lock;
674 struct strbuf buf = STRBUF_INIT;
677 fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR);
678 strbuf_addf(&buf, "%s\n", head);
679 if (write_in_full(fd, buf.buf, buf.len) < 0)
680 die_errno(_("Could not write to %s"), head_file);
681 if (commit_lock_file(&head_lock) < 0)
682 die(_("Error wrapping up %s."), head_file);
685 static int reset_for_rollback(const unsigned char *sha1)
687 const char *argv[4]; /* reset --merge <arg> + NULL */
690 argv[2] = sha1_to_hex(sha1);
692 return run_command_v_opt(argv, RUN_GIT_CMD);
695 static int rollback_single_pick(void)
697 unsigned char head_sha1[20];
699 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
700 !file_exists(git_path("REVERT_HEAD")))
701 return error(_("no cherry-pick or revert in progress"));
702 if (read_ref_full("HEAD", head_sha1, 0, NULL))
703 return error(_("cannot resolve HEAD"));
704 if (is_null_sha1(head_sha1))
705 return error(_("cannot abort from a branch yet to be born"));
706 return reset_for_rollback(head_sha1);
709 static int sequencer_rollback(struct replay_opts *opts)
711 const char *filename;
713 unsigned char sha1[20];
714 struct strbuf buf = STRBUF_INIT;
716 filename = git_path(SEQ_HEAD_FILE);
717 f = fopen(filename, "r");
718 if (!f && errno == ENOENT) {
720 * There is no multiple-cherry-pick in progress.
721 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
722 * a single-cherry-pick in progress, abort that.
724 return rollback_single_pick();
727 return error(_("cannot open %s: %s"), filename,
729 if (strbuf_getline(&buf, f, '\n')) {
730 error(_("cannot read %s: %s"), filename, ferror(f) ?
731 strerror(errno) : _("unexpected end of file"));
736 if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') {
737 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
741 if (reset_for_rollback(sha1))
743 remove_sequencer_state();
744 strbuf_release(&buf);
747 strbuf_release(&buf);
751 static void save_todo(struct commit_list *todo_list, struct replay_opts *opts)
753 const char *todo_file = git_path(SEQ_TODO_FILE);
754 static struct lock_file todo_lock;
755 struct strbuf buf = STRBUF_INIT;
758 fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR);
759 if (format_todo(&buf, todo_list, opts) < 0)
760 die(_("Could not format %s."), todo_file);
761 if (write_in_full(fd, buf.buf, buf.len) < 0) {
762 strbuf_release(&buf);
763 die_errno(_("Could not write to %s"), todo_file);
765 if (commit_lock_file(&todo_lock) < 0) {
766 strbuf_release(&buf);
767 die(_("Error wrapping up %s."), todo_file);
769 strbuf_release(&buf);
772 static void save_opts(struct replay_opts *opts)
774 const char *opts_file = git_path(SEQ_OPTS_FILE);
777 git_config_set_in_file(opts_file, "options.no-commit", "true");
779 git_config_set_in_file(opts_file, "options.edit", "true");
781 git_config_set_in_file(opts_file, "options.signoff", "true");
782 if (opts->record_origin)
783 git_config_set_in_file(opts_file, "options.record-origin", "true");
785 git_config_set_in_file(opts_file, "options.allow-ff", "true");
786 if (opts->mainline) {
787 struct strbuf buf = STRBUF_INIT;
788 strbuf_addf(&buf, "%d", opts->mainline);
789 git_config_set_in_file(opts_file, "options.mainline", buf.buf);
790 strbuf_release(&buf);
793 git_config_set_in_file(opts_file, "options.strategy", opts->strategy);
796 for (i = 0; i < opts->xopts_nr; i++)
797 git_config_set_multivar_in_file(opts_file,
798 "options.strategy-option",
799 opts->xopts[i], "^$", 0);
803 static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
805 struct commit_list *cur;
808 setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
810 assert(!(opts->signoff || opts->no_commit ||
811 opts->record_origin || opts->edit));
812 read_and_refresh_cache(opts);
814 for (cur = todo_list; cur; cur = cur->next) {
815 save_todo(cur, opts);
816 res = do_pick_commit(cur->item, opts);
822 * Sequence of picks finished successfully; cleanup by
823 * removing the .git/sequencer directory
825 remove_sequencer_state();
829 static int continue_single_pick(void)
831 const char *argv[] = { "commit", NULL };
833 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
834 !file_exists(git_path("REVERT_HEAD")))
835 return error(_("no cherry-pick or revert in progress"));
836 return run_command_v_opt(argv, RUN_GIT_CMD);
839 static int sequencer_continue(struct replay_opts *opts)
841 struct commit_list *todo_list = NULL;
843 if (!file_exists(git_path(SEQ_TODO_FILE)))
844 return continue_single_pick();
845 read_populate_opts(&opts);
846 read_populate_todo(&todo_list, opts);
848 /* Verify that the conflict has been resolved */
849 if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
850 file_exists(git_path("REVERT_HEAD"))) {
851 int ret = continue_single_pick();
855 if (index_differs_from("HEAD", 0))
856 return error_dirty_index(opts);
857 todo_list = todo_list->next;
858 return pick_commits(todo_list, opts);
861 static int single_pick(struct commit *cmit, struct replay_opts *opts)
863 setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
864 return do_pick_commit(cmit, opts);
867 int sequencer_pick_revisions(struct replay_opts *opts)
869 struct commit_list *todo_list = NULL;
870 unsigned char sha1[20];
872 if (opts->subcommand == REPLAY_NONE)
875 read_and_refresh_cache(opts);
878 * Decide what to do depending on the arguments; a fresh
879 * cherry-pick should be handled differently from an existing
880 * one that is being continued
882 if (opts->subcommand == REPLAY_REMOVE_STATE) {
883 remove_sequencer_state();
886 if (opts->subcommand == REPLAY_ROLLBACK)
887 return sequencer_rollback(opts);
888 if (opts->subcommand == REPLAY_CONTINUE)
889 return sequencer_continue(opts);
892 * If we were called as "git cherry-pick <commit>", just
893 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
894 * REVERT_HEAD, and don't touch the sequencer state.
895 * This means it is possible to cherry-pick in the middle
896 * of a cherry-pick sequence.
898 if (opts->revs->cmdline.nr == 1 &&
899 opts->revs->cmdline.rev->whence == REV_CMD_REV &&
900 opts->revs->no_walk &&
901 !opts->revs->cmdline.rev->flags) {
903 if (prepare_revision_walk(opts->revs))
904 die(_("revision walk setup failed"));
905 cmit = get_revision(opts->revs);
906 if (!cmit || get_revision(opts->revs))
907 die("BUG: expected exactly one commit from walk");
908 return single_pick(cmit, opts);
912 * Start a new cherry-pick/ revert sequence; but
913 * first, make sure that an existing one isn't in
917 walk_revs_populate_todo(&todo_list, opts);
918 if (create_seq_dir() < 0)
920 if (get_sha1("HEAD", sha1)) {
921 if (opts->action == REPLAY_REVERT)
922 return error(_("Can't revert as initial commit"));
923 return error(_("Can't cherry-pick into empty head"));
925 save_head(sha1_to_hex(sha1));
927 return pick_commits(todo_list, opts);