6 #include "run-command.h"
9 #include "parse-options.h"
10 #include "cache-tree.h"
14 #include "merge-recursive.h"
17 #include "sequencer.h"
20 * This implements the builtins revert and cherry-pick.
22 * Copyright (c) 2007 Johannes E. Schindelin
24 * Based on git-revert.sh, which is
26 * Copyright (c) 2005 Linus Torvalds
27 * Copyright (c) 2005 Junio C Hamano
30 static const char * const revert_usage[] = {
31 "git revert [options] <commit-ish>",
32 "git revert <subcommand>",
36 static const char * const cherry_pick_usage[] = {
37 "git cherry-pick [options] <commit-ish>",
38 "git cherry-pick <subcommand>",
42 enum replay_action { REVERT, CHERRY_PICK };
43 enum replay_subcommand { REPLAY_NONE, REPLAY_RESET, REPLAY_CONTINUE };
46 enum replay_action action;
47 enum replay_subcommand subcommand;
55 int allow_rerere_auto;
59 const char **commit_argv;
64 size_t xopts_nr, xopts_alloc;
67 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
69 static const char *action_name(const struct replay_opts *opts)
71 return opts->action == REVERT ? "revert" : "cherry-pick";
74 static char *get_encoding(const char *message);
76 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
78 return opts->action == REVERT ? revert_usage : cherry_pick_usage;
81 static int option_parse_x(const struct option *opt,
82 const char *arg, int unset)
84 struct replay_opts **opts_ptr = opt->value;
85 struct replay_opts *opts = *opts_ptr;
90 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
91 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
95 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
100 va_start(ap, base_opt);
101 while ((this_opt = va_arg(ap, const char *))) {
108 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
111 static void verify_opt_mutually_compatible(const char *me, ...)
113 const char *opt1, *opt2 = NULL;
117 while ((opt1 = va_arg(ap, const char *))) {
122 while ((opt2 = va_arg(ap, const char *))) {
129 die(_("%s: %s cannot be used with %s"), me, opt1, opt2);
132 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
134 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
135 const char *me = action_name(opts);
138 struct option options[] = {
139 OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"),
140 OPT_BOOLEAN(0, "continue", &contin, "continue the current operation"),
141 OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
142 OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
143 OPT_NOOP_NOARG('r', NULL),
144 OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
145 OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
146 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
147 OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"),
148 OPT_CALLBACK('X', "strategy-option", &opts, "option",
149 "option for merge strategy", option_parse_x),
155 if (opts->action == CHERRY_PICK) {
156 struct option cp_extra[] = {
157 OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"),
158 OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"),
161 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
162 die(_("program error"));
165 opts->commit_argc = parse_options(argc, argv, NULL, options, usage_str,
166 PARSE_OPT_KEEP_ARGV0 |
167 PARSE_OPT_KEEP_UNKNOWN);
169 /* Check for incompatible subcommands */
170 verify_opt_mutually_compatible(me,
172 "--continue", contin,
175 /* Set the subcommand */
177 opts->subcommand = REPLAY_RESET;
179 opts->subcommand = REPLAY_CONTINUE;
181 opts->subcommand = REPLAY_NONE;
183 /* Check for incompatible command line arguments */
184 if (opts->subcommand != REPLAY_NONE) {
185 char *this_operation;
186 if (opts->subcommand == REPLAY_RESET)
187 this_operation = "--reset";
189 this_operation = "--continue";
191 verify_opt_compatible(me, this_operation,
192 "--no-commit", opts->no_commit,
193 "--signoff", opts->signoff,
194 "--mainline", opts->mainline,
195 "--strategy", opts->strategy ? 1 : 0,
196 "--strategy-option", opts->xopts ? 1 : 0,
197 "-x", opts->record_origin,
198 "--ff", opts->allow_ff,
202 else if (opts->commit_argc < 2)
203 usage_with_options(usage_str, options);
206 verify_opt_compatible(me, "--ff",
207 "--signoff", opts->signoff,
208 "--no-commit", opts->no_commit,
209 "-x", opts->record_origin,
210 "--edit", opts->edit,
212 opts->commit_argv = argv;
215 struct commit_message {
219 char *reencoded_message;
223 static int get_message(struct commit *commit, struct commit_message *out)
225 const char *encoding;
226 const char *abbrev, *subject;
227 int abbrev_len, subject_len;
232 encoding = get_encoding(commit->buffer);
235 if (!git_commit_encoding)
236 git_commit_encoding = "UTF-8";
238 out->reencoded_message = NULL;
239 out->message = commit->buffer;
240 if (strcmp(encoding, git_commit_encoding))
241 out->reencoded_message = reencode_string(commit->buffer,
242 git_commit_encoding, encoding);
243 if (out->reencoded_message)
244 out->message = out->reencoded_message;
246 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
247 abbrev_len = strlen(abbrev);
249 subject_len = find_commit_subject(out->message, &subject);
251 out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
252 strlen("... ") + subject_len + 1);
253 q = out->parent_label;
254 q = mempcpy(q, "parent of ", strlen("parent of "));
256 q = mempcpy(q, abbrev, abbrev_len);
257 q = mempcpy(q, "... ", strlen("... "));
259 q = mempcpy(q, subject, subject_len);
264 static void free_message(struct commit_message *msg)
266 free(msg->parent_label);
267 free(msg->reencoded_message);
270 static char *get_encoding(const char *message)
272 const char *p = message, *eol;
274 while (*p && *p != '\n') {
275 for (eol = p + 1; *eol && *eol != '\n'; eol++)
277 if (!prefixcmp(p, "encoding ")) {
278 char *result = xmalloc(eol - 8 - p);
279 strlcpy(result, p + 9, eol - 8 - p);
289 static void write_cherry_pick_head(struct commit *commit)
292 struct strbuf buf = STRBUF_INIT;
294 strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1));
296 fd = open(git_path("CHERRY_PICK_HEAD"), O_WRONLY | O_CREAT, 0666);
298 die_errno(_("Could not open '%s' for writing"),
299 git_path("CHERRY_PICK_HEAD"));
300 if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd))
301 die_errno(_("Could not write to '%s'"), git_path("CHERRY_PICK_HEAD"));
302 strbuf_release(&buf);
305 static void print_advice(int show_hint)
307 char *msg = getenv("GIT_CHERRY_PICK_HELP");
310 fprintf(stderr, "%s\n", msg);
312 * A conflict has occured but the porcelain
313 * (typically rebase --interactive) wants to take care
314 * of the commit itself so remove CHERRY_PICK_HEAD
316 unlink(git_path("CHERRY_PICK_HEAD"));
321 advise("after resolving the conflicts, mark the corrected paths");
322 advise("with 'git add <paths>' or 'git rm <paths>'");
323 advise("and commit the result with 'git commit'");
327 static void write_message(struct strbuf *msgbuf, const char *filename)
329 static struct lock_file msg_file;
331 int msg_fd = hold_lock_file_for_update(&msg_file, filename,
333 if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
334 die_errno(_("Could not write to %s."), filename);
335 strbuf_release(msgbuf);
336 if (commit_lock_file(&msg_file) < 0)
337 die(_("Error wrapping up %s"), filename);
340 static struct tree *empty_tree(void)
342 return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN);
345 static int error_dirty_index(struct replay_opts *opts)
347 if (read_cache_unmerged())
348 return error_resolve_conflict(action_name(opts));
350 /* Different translation strings for cherry-pick and revert */
351 if (opts->action == CHERRY_PICK)
352 error(_("Your local changes would be overwritten by cherry-pick."));
354 error(_("Your local changes would be overwritten by revert."));
356 if (advice_commit_before_merge)
357 advise(_("Commit your changes or stash them to proceed."));
361 static int fast_forward_to(const unsigned char *to, const unsigned char *from)
363 struct ref_lock *ref_lock;
366 if (checkout_fast_forward(from, to))
367 exit(1); /* the callee should have complained already */
368 ref_lock = lock_any_ref_for_update("HEAD", from, 0);
369 return write_ref_sha1(ref_lock, to, "cherry-pick");
372 static int do_recursive_merge(struct commit *base, struct commit *next,
373 const char *base_label, const char *next_label,
374 unsigned char *head, struct strbuf *msgbuf,
375 struct replay_opts *opts)
377 struct merge_options o;
378 struct tree *result, *next_tree, *base_tree, *head_tree;
381 static struct lock_file index_lock;
383 index_fd = hold_locked_index(&index_lock, 1);
387 init_merge_options(&o);
388 o.ancestor = base ? base_label : "(empty tree)";
390 o.branch2 = next ? next_label : "(empty tree)";
392 head_tree = parse_tree_indirect(head);
393 next_tree = next ? next->tree : empty_tree();
394 base_tree = base ? base->tree : empty_tree();
396 for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++)
397 parse_merge_opt(&o, *xopt);
399 clean = merge_trees(&o,
401 next_tree, base_tree, &result);
403 if (active_cache_changed &&
404 (write_cache(index_fd, active_cache, active_nr) ||
405 commit_locked_index(&index_lock)))
406 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
407 die(_("%s: Unable to write new index file"), action_name(opts));
408 rollback_lock_file(&index_lock);
412 strbuf_addstr(msgbuf, "\nConflicts:\n\n");
413 for (i = 0; i < active_nr;) {
414 struct cache_entry *ce = active_cache[i++];
416 strbuf_addch(msgbuf, '\t');
417 strbuf_addstr(msgbuf, ce->name);
418 strbuf_addch(msgbuf, '\n');
419 while (i < active_nr && !strcmp(ce->name,
420 active_cache[i]->name))
430 * If we are cherry-pick, and if the merge did not result in
431 * hand-editing, we will hit this commit and inherit the original
432 * author date and name.
433 * If we are revert, or if our cherry-pick results in a hand merge,
434 * we had better say that the current user is responsible for that.
436 static int run_git_commit(const char *defmsg, struct replay_opts *opts)
438 /* 6 is max possible length of our args array including NULL */
442 args[i++] = "commit";
452 return run_command_v_opt(args, RUN_GIT_CMD);
455 static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
457 unsigned char head[20];
458 struct commit *base, *next, *parent;
459 const char *base_label, *next_label;
460 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
462 struct strbuf msgbuf = STRBUF_INIT;
465 if (opts->no_commit) {
467 * We do not intend to commit immediately. We just want to
468 * merge the differences in, so let's compute the tree
469 * that represents the "current" state for merge-recursive
472 if (write_cache_as_tree(head, 0, NULL))
473 die (_("Your index file is unmerged."));
475 if (get_sha1("HEAD", head))
476 return error(_("You do not have a valid HEAD"));
477 if (index_differs_from("HEAD", 0))
478 return error_dirty_index(opts);
482 if (!commit->parents) {
485 else if (commit->parents->next) {
486 /* Reverting or cherry-picking a merge commit */
488 struct commit_list *p;
491 return error(_("Commit %s is a merge but no -m option was given."),
492 sha1_to_hex(commit->object.sha1));
494 for (cnt = 1, p = commit->parents;
495 cnt != opts->mainline && p;
498 if (cnt != opts->mainline || !p)
499 return error(_("Commit %s does not have parent %d"),
500 sha1_to_hex(commit->object.sha1), opts->mainline);
502 } else if (0 < opts->mainline)
503 return error(_("Mainline was specified but commit %s is not a merge."),
504 sha1_to_hex(commit->object.sha1));
506 parent = commit->parents->item;
508 if (opts->allow_ff && parent && !hashcmp(parent->object.sha1, head))
509 return fast_forward_to(commit->object.sha1, head);
511 if (parent && parse_commit(parent) < 0)
512 /* TRANSLATORS: The first %s will be "revert" or
513 "cherry-pick", the second %s a SHA1 */
514 return error(_("%s: cannot parse parent commit %s"),
515 action_name(opts), sha1_to_hex(parent->object.sha1));
517 if (get_message(commit, &msg) != 0)
518 return error(_("Cannot get commit message for %s"),
519 sha1_to_hex(commit->object.sha1));
522 * "commit" is an existing commit. We would want to apply
523 * the difference it introduces since its first parent "prev"
524 * on top of the current HEAD if we are cherry-pick. Or the
525 * reverse of it if we are revert.
528 defmsg = git_pathdup("MERGE_MSG");
530 if (opts->action == REVERT) {
532 base_label = msg.label;
534 next_label = msg.parent_label;
535 strbuf_addstr(&msgbuf, "Revert \"");
536 strbuf_addstr(&msgbuf, msg.subject);
537 strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
538 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
540 if (commit->parents && commit->parents->next) {
541 strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
542 strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1));
544 strbuf_addstr(&msgbuf, ".\n");
549 base_label = msg.parent_label;
551 next_label = msg.label;
554 * Append the commit log message to msgbuf; it starts
555 * after the tree, parent, author, committer
556 * information followed by "\n\n".
558 p = strstr(msg.message, "\n\n");
561 strbuf_addstr(&msgbuf, p);
564 if (opts->record_origin) {
565 strbuf_addstr(&msgbuf, "(cherry picked from commit ");
566 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
567 strbuf_addstr(&msgbuf, ")\n");
571 if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REVERT) {
572 res = do_recursive_merge(base, next, base_label, next_label,
573 head, &msgbuf, opts);
574 write_message(&msgbuf, defmsg);
576 struct commit_list *common = NULL;
577 struct commit_list *remotes = NULL;
579 write_message(&msgbuf, defmsg);
581 commit_list_insert(base, &common);
582 commit_list_insert(next, &remotes);
583 res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts,
584 common, sha1_to_hex(head), remotes);
585 free_commit_list(common);
586 free_commit_list(remotes);
590 * If the merge was clean or if it failed due to conflict, we write
591 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
592 * However, if the merge did not even start, then we don't want to
595 if (opts->action == CHERRY_PICK && !opts->no_commit && (res == 0 || res == 1))
596 write_cherry_pick_head(commit);
599 error(opts->action == REVERT
600 ? _("could not revert %s... %s")
601 : _("could not apply %s... %s"),
602 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
604 print_advice(res == 1);
605 rerere(opts->allow_rerere_auto);
607 if (!opts->no_commit)
608 res = run_git_commit(defmsg, opts);
617 static void prepare_revs(struct rev_info *revs, struct replay_opts *opts)
621 init_revisions(revs, NULL);
623 if (opts->action != REVERT)
626 argc = setup_revisions(opts->commit_argc, opts->commit_argv, revs, NULL);
628 usage(*revert_or_cherry_pick_usage(opts));
630 if (prepare_revision_walk(revs))
631 die(_("revision walk setup failed"));
634 die(_("empty commit set passed"));
637 static void read_and_refresh_cache(struct replay_opts *opts)
639 static struct lock_file index_lock;
640 int index_fd = hold_locked_index(&index_lock, 0);
641 if (read_index_preload(&the_index, NULL) < 0)
642 die(_("git %s: failed to read the index"), action_name(opts));
643 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
644 if (the_index.cache_changed) {
645 if (write_index(&the_index, index_fd) ||
646 commit_locked_index(&index_lock))
647 die(_("git %s: failed to refresh the index"), action_name(opts));
649 rollback_lock_file(&index_lock);
653 * Append a commit to the end of the commit_list.
655 * next starts by pointing to the variable that holds the head of an
656 * empty commit_list, and is updated to point to the "next" field of
657 * the last item on the list as new commits are appended.
661 * struct commit_list *list;
662 * struct commit_list **next = &list;
664 * next = commit_list_append(c1, next);
665 * next = commit_list_append(c2, next);
666 * assert(commit_list_count(list) == 2);
669 static struct commit_list **commit_list_append(struct commit *commit,
670 struct commit_list **next)
672 struct commit_list *new = xmalloc(sizeof(struct commit_list));
679 static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
680 struct replay_opts *opts)
682 struct commit_list *cur = NULL;
683 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
684 const char *sha1_abbrev = NULL;
685 const char *action_str = opts->action == REVERT ? "revert" : "pick";
687 for (cur = todo_list; cur; cur = cur->next) {
688 sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV);
689 if (get_message(cur->item, &msg))
690 return error(_("Cannot get commit message for %s"), sha1_abbrev);
691 strbuf_addf(buf, "%s %s %s\n", action_str, sha1_abbrev, msg.subject);
696 static struct commit *parse_insn_line(char *start, struct replay_opts *opts)
698 unsigned char commit_sha1[20];
699 char sha1_abbrev[40];
700 enum replay_action action;
704 if (!prefixcmp(start, "pick ")) {
705 action = CHERRY_PICK;
706 insn_len = strlen("pick");
707 p = start + insn_len + 1;
708 } else if (!prefixcmp(start, "revert ")) {
710 insn_len = strlen("revert");
711 p = start + insn_len + 1;
720 strlcpy(sha1_abbrev, p, q - p);
723 * Verify that the action matches up with the one in
724 * opts; we don't support arbitrary instructions
726 if (action != opts->action) {
727 const char *action_str;
728 action_str = action == REVERT ? "revert" : "cherry-pick";
729 error(_("Cannot %s during a %s"), action_str, action_name(opts));
733 if (get_sha1(sha1_abbrev, commit_sha1) < 0)
736 return lookup_commit_reference(commit_sha1);
739 static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
740 struct replay_opts *opts)
742 struct commit_list **next = todo_list;
743 struct commit *commit;
747 for (i = 1; *p; i++) {
748 commit = parse_insn_line(p, opts);
750 return error(_("Could not parse line %d."), i);
751 next = commit_list_append(commit, next);
752 p = strchrnul(p, '\n');
757 return error(_("No commits parsed."));
761 static void read_populate_todo(struct commit_list **todo_list,
762 struct replay_opts *opts)
764 const char *todo_file = git_path(SEQ_TODO_FILE);
765 struct strbuf buf = STRBUF_INIT;
768 fd = open(todo_file, O_RDONLY);
770 die_errno(_("Could not open %s."), todo_file);
771 if (strbuf_read(&buf, fd, 0) < 0) {
773 strbuf_release(&buf);
774 die(_("Could not read %s."), todo_file);
778 res = parse_insn_buffer(buf.buf, todo_list, opts);
779 strbuf_release(&buf);
781 die(_("Unusable instruction sheet: %s"), todo_file);
784 static int populate_opts_cb(const char *key, const char *value, void *data)
786 struct replay_opts *opts = data;
791 else if (!strcmp(key, "options.no-commit"))
792 opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
793 else if (!strcmp(key, "options.edit"))
794 opts->edit = git_config_bool_or_int(key, value, &error_flag);
795 else if (!strcmp(key, "options.signoff"))
796 opts->signoff = git_config_bool_or_int(key, value, &error_flag);
797 else if (!strcmp(key, "options.record-origin"))
798 opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
799 else if (!strcmp(key, "options.allow-ff"))
800 opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
801 else if (!strcmp(key, "options.mainline"))
802 opts->mainline = git_config_int(key, value);
803 else if (!strcmp(key, "options.strategy"))
804 git_config_string(&opts->strategy, key, value);
805 else if (!strcmp(key, "options.strategy-option")) {
806 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
807 opts->xopts[opts->xopts_nr++] = xstrdup(value);
809 return error(_("Invalid key: %s"), key);
812 return error(_("Invalid value for %s: %s"), key, value);
817 static void read_populate_opts(struct replay_opts **opts_ptr)
819 const char *opts_file = git_path(SEQ_OPTS_FILE);
821 if (!file_exists(opts_file))
823 if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0)
824 die(_("Malformed options sheet: %s"), opts_file);
827 static void walk_revs_populate_todo(struct commit_list **todo_list,
828 struct replay_opts *opts)
830 struct rev_info revs;
831 struct commit *commit;
832 struct commit_list **next;
834 prepare_revs(&revs, opts);
837 while ((commit = get_revision(&revs)))
838 next = commit_list_append(commit, next);
841 static int create_seq_dir(void)
843 const char *seq_dir = git_path(SEQ_DIR);
845 if (file_exists(seq_dir))
846 return error(_("%s already exists."), seq_dir);
847 else if (mkdir(seq_dir, 0777) < 0)
848 die_errno(_("Could not create sequencer directory '%s'."), seq_dir);
852 static void save_head(const char *head)
854 const char *head_file = git_path(SEQ_HEAD_FILE);
855 static struct lock_file head_lock;
856 struct strbuf buf = STRBUF_INIT;
859 fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR);
860 strbuf_addf(&buf, "%s\n", head);
861 if (write_in_full(fd, buf.buf, buf.len) < 0)
862 die_errno(_("Could not write to %s."), head_file);
863 if (commit_lock_file(&head_lock) < 0)
864 die(_("Error wrapping up %s."), head_file);
867 static void save_todo(struct commit_list *todo_list, struct replay_opts *opts)
869 const char *todo_file = git_path(SEQ_TODO_FILE);
870 static struct lock_file todo_lock;
871 struct strbuf buf = STRBUF_INIT;
874 fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR);
875 if (format_todo(&buf, todo_list, opts) < 0)
876 die(_("Could not format %s."), todo_file);
877 if (write_in_full(fd, buf.buf, buf.len) < 0) {
878 strbuf_release(&buf);
879 die_errno(_("Could not write to %s."), todo_file);
881 if (commit_lock_file(&todo_lock) < 0) {
882 strbuf_release(&buf);
883 die(_("Error wrapping up %s."), todo_file);
885 strbuf_release(&buf);
888 static void save_opts(struct replay_opts *opts)
890 const char *opts_file = git_path(SEQ_OPTS_FILE);
893 git_config_set_in_file(opts_file, "options.no-commit", "true");
895 git_config_set_in_file(opts_file, "options.edit", "true");
897 git_config_set_in_file(opts_file, "options.signoff", "true");
898 if (opts->record_origin)
899 git_config_set_in_file(opts_file, "options.record-origin", "true");
901 git_config_set_in_file(opts_file, "options.allow-ff", "true");
902 if (opts->mainline) {
903 struct strbuf buf = STRBUF_INIT;
904 strbuf_addf(&buf, "%d", opts->mainline);
905 git_config_set_in_file(opts_file, "options.mainline", buf.buf);
906 strbuf_release(&buf);
909 git_config_set_in_file(opts_file, "options.strategy", opts->strategy);
912 for (i = 0; i < opts->xopts_nr; i++)
913 git_config_set_multivar_in_file(opts_file,
914 "options.strategy-option",
915 opts->xopts[i], "^$", 0);
919 static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
921 struct commit_list *cur;
924 setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
926 assert(!(opts->signoff || opts->no_commit ||
927 opts->record_origin || opts->edit));
928 read_and_refresh_cache(opts);
930 for (cur = todo_list; cur; cur = cur->next) {
931 save_todo(cur, opts);
932 res = do_pick_commit(cur->item, opts);
936 * An error was encountered while
937 * picking the last commit; the
938 * sequencer state is useless now --
939 * the user simply needs to resolve
940 * the conflict and commit
942 remove_sequencer_state(0);
948 * Sequence of picks finished successfully; cleanup by
949 * removing the .git/sequencer directory
951 remove_sequencer_state(1);
955 static int pick_revisions(struct replay_opts *opts)
957 struct commit_list *todo_list = NULL;
958 unsigned char sha1[20];
960 read_and_refresh_cache(opts);
963 * Decide what to do depending on the arguments; a fresh
964 * cherry-pick should be handled differently from an existing
965 * one that is being continued
967 if (opts->subcommand == REPLAY_RESET) {
968 remove_sequencer_state(1);
970 } else if (opts->subcommand == REPLAY_CONTINUE) {
971 if (!file_exists(git_path(SEQ_TODO_FILE)))
973 read_populate_opts(&opts);
974 read_populate_todo(&todo_list, opts);
976 /* Verify that the conflict has been resolved */
977 if (!index_differs_from("HEAD", 0))
978 todo_list = todo_list->next;
981 * Start a new cherry-pick/ revert sequence; but
982 * first, make sure that an existing one isn't in
986 walk_revs_populate_todo(&todo_list, opts);
987 if (create_seq_dir() < 0) {
988 error(_("A cherry-pick or revert is in progress."));
989 advise(_("Use --continue to continue the operation"));
990 advise(_("or --reset to forget about it"));
993 if (get_sha1("HEAD", sha1)) {
994 if (opts->action == REVERT)
995 return error(_("Can't revert as initial commit"));
996 return error(_("Can't cherry-pick into empty head"));
998 save_head(sha1_to_hex(sha1));
1001 return pick_commits(todo_list, opts);
1003 return error(_("No %s in progress"), action_name(opts));
1006 int cmd_revert(int argc, const char **argv, const char *prefix)
1008 struct replay_opts opts;
1011 memset(&opts, 0, sizeof(opts));
1014 opts.action = REVERT;
1015 git_config(git_default_config, NULL);
1016 parse_args(argc, argv, &opts);
1017 res = pick_revisions(&opts);
1019 die(_("revert failed"));
1023 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
1025 struct replay_opts opts;
1028 memset(&opts, 0, sizeof(opts));
1029 opts.action = CHERRY_PICK;
1030 git_config(git_default_config, NULL);
1031 parse_args(argc, argv, &opts);
1032 res = pick_revisions(&opts);
1034 die(_("cherry-pick failed"));