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>",
47 enum replay_subcommand
{
55 enum replay_action action
;
56 enum replay_subcommand subcommand
;
64 int allow_rerere_auto
;
71 size_t xopts_nr
, xopts_alloc
;
73 /* Only used by REPLAY_NONE */
74 struct rev_info
*revs
;
77 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
79 static const char *action_name(const struct replay_opts
*opts
)
81 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
84 static char *get_encoding(const char *message
);
86 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
88 return opts
->action
== REPLAY_REVERT
? revert_usage
: cherry_pick_usage
;
91 static int option_parse_x(const struct option
*opt
,
92 const char *arg
, int unset
)
94 struct replay_opts
**opts_ptr
= opt
->value
;
95 struct replay_opts
*opts
= *opts_ptr
;
100 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
101 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(arg
);
105 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
107 const char *this_opt
;
110 va_start(ap
, base_opt
);
111 while ((this_opt
= va_arg(ap
, const char *))) {
118 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
121 static void verify_opt_mutually_compatible(const char *me
, ...)
123 const char *opt1
, *opt2
= NULL
;
127 while ((opt1
= va_arg(ap
, const char *))) {
132 while ((opt2
= va_arg(ap
, const char *))) {
139 die(_("%s: %s cannot be used with %s"), me
, opt1
, opt2
);
142 static void parse_args(int argc
, const char **argv
, struct replay_opts
*opts
)
144 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
145 const char *me
= action_name(opts
);
146 int remove_state
= 0;
149 struct option options
[] = {
150 OPT_BOOLEAN(0, "quit", &remove_state
, "end revert or cherry-pick sequence"),
151 OPT_BOOLEAN(0, "continue", &contin
, "resume revert or cherry-pick sequence"),
152 OPT_BOOLEAN(0, "abort", &rollback
, "cancel revert or cherry-pick sequence"),
153 OPT_BOOLEAN('n', "no-commit", &opts
->no_commit
, "don't automatically commit"),
154 OPT_BOOLEAN('e', "edit", &opts
->edit
, "edit the commit message"),
155 OPT_NOOP_NOARG('r', NULL
),
156 OPT_BOOLEAN('s', "signoff", &opts
->signoff
, "add Signed-off-by:"),
157 OPT_INTEGER('m', "mainline", &opts
->mainline
, "parent number"),
158 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
159 OPT_STRING(0, "strategy", &opts
->strategy
, "strategy", "merge strategy"),
160 OPT_CALLBACK('X', "strategy-option", &opts
, "option",
161 "option for merge strategy", option_parse_x
),
167 if (opts
->action
== REPLAY_PICK
) {
168 struct option cp_extra
[] = {
169 OPT_BOOLEAN('x', NULL
, &opts
->record_origin
, "append commit name"),
170 OPT_BOOLEAN(0, "ff", &opts
->allow_ff
, "allow fast-forward"),
173 if (parse_options_concat(options
, ARRAY_SIZE(options
), cp_extra
))
174 die(_("program error"));
177 argc
= parse_options(argc
, argv
, NULL
, options
, usage_str
,
178 PARSE_OPT_KEEP_ARGV0
|
179 PARSE_OPT_KEEP_UNKNOWN
);
181 /* Check for incompatible subcommands */
182 verify_opt_mutually_compatible(me
,
183 "--quit", remove_state
,
184 "--continue", contin
,
188 /* Set the subcommand */
190 opts
->subcommand
= REPLAY_REMOVE_STATE
;
192 opts
->subcommand
= REPLAY_CONTINUE
;
194 opts
->subcommand
= REPLAY_ROLLBACK
;
196 opts
->subcommand
= REPLAY_NONE
;
198 /* Check for incompatible command line arguments */
199 if (opts
->subcommand
!= REPLAY_NONE
) {
200 char *this_operation
;
201 if (opts
->subcommand
== REPLAY_REMOVE_STATE
)
202 this_operation
= "--quit";
203 else if (opts
->subcommand
== REPLAY_CONTINUE
)
204 this_operation
= "--continue";
206 assert(opts
->subcommand
== REPLAY_ROLLBACK
);
207 this_operation
= "--abort";
210 verify_opt_compatible(me
, this_operation
,
211 "--no-commit", opts
->no_commit
,
212 "--signoff", opts
->signoff
,
213 "--mainline", opts
->mainline
,
214 "--strategy", opts
->strategy
? 1 : 0,
215 "--strategy-option", opts
->xopts
? 1 : 0,
216 "-x", opts
->record_origin
,
217 "--ff", opts
->allow_ff
,
222 verify_opt_compatible(me
, "--ff",
223 "--signoff", opts
->signoff
,
224 "--no-commit", opts
->no_commit
,
225 "-x", opts
->record_origin
,
226 "--edit", opts
->edit
,
229 if (opts
->subcommand
!= REPLAY_NONE
) {
232 opts
->revs
= xmalloc(sizeof(*opts
->revs
));
233 init_revisions(opts
->revs
, NULL
);
234 opts
->revs
->no_walk
= 1;
236 usage_with_options(usage_str
, options
);
237 argc
= setup_revisions(argc
, argv
, opts
->revs
, NULL
);
241 usage_with_options(usage_str
, options
);
244 struct commit_message
{
248 char *reencoded_message
;
252 static int get_message(struct commit
*commit
, struct commit_message
*out
)
254 const char *encoding
;
255 const char *abbrev
, *subject
;
256 int abbrev_len
, subject_len
;
261 encoding
= get_encoding(commit
->buffer
);
264 if (!git_commit_encoding
)
265 git_commit_encoding
= "UTF-8";
267 out
->reencoded_message
= NULL
;
268 out
->message
= commit
->buffer
;
269 if (strcmp(encoding
, git_commit_encoding
))
270 out
->reencoded_message
= reencode_string(commit
->buffer
,
271 git_commit_encoding
, encoding
);
272 if (out
->reencoded_message
)
273 out
->message
= out
->reencoded_message
;
275 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
276 abbrev_len
= strlen(abbrev
);
278 subject_len
= find_commit_subject(out
->message
, &subject
);
280 out
->parent_label
= xmalloc(strlen("parent of ") + abbrev_len
+
281 strlen("... ") + subject_len
+ 1);
282 q
= out
->parent_label
;
283 q
= mempcpy(q
, "parent of ", strlen("parent of "));
285 q
= mempcpy(q
, abbrev
, abbrev_len
);
286 q
= mempcpy(q
, "... ", strlen("... "));
288 q
= mempcpy(q
, subject
, subject_len
);
293 static void free_message(struct commit_message
*msg
)
295 free(msg
->parent_label
);
296 free(msg
->reencoded_message
);
299 static char *get_encoding(const char *message
)
301 const char *p
= message
, *eol
;
303 while (*p
&& *p
!= '\n') {
304 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
306 if (!prefixcmp(p
, "encoding ")) {
307 char *result
= xmalloc(eol
- 8 - p
);
308 strlcpy(result
, p
+ 9, eol
- 8 - p
);
318 static void write_cherry_pick_head(struct commit
*commit
, const char *pseudoref
)
320 const char *filename
;
322 struct strbuf buf
= STRBUF_INIT
;
324 strbuf_addf(&buf
, "%s\n", sha1_to_hex(commit
->object
.sha1
));
326 filename
= git_path("%s", pseudoref
);
327 fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
329 die_errno(_("Could not open '%s' for writing"), filename
);
330 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
|| close(fd
))
331 die_errno(_("Could not write to '%s'"), filename
);
332 strbuf_release(&buf
);
335 static void print_advice(int show_hint
)
337 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
340 fprintf(stderr
, "%s\n", msg
);
342 * A conflict has occured but the porcelain
343 * (typically rebase --interactive) wants to take care
344 * of the commit itself so remove CHERRY_PICK_HEAD
346 unlink(git_path("CHERRY_PICK_HEAD"));
351 advise("after resolving the conflicts, mark the corrected paths");
352 advise("with 'git add <paths>' or 'git rm <paths>'");
353 advise("and commit the result with 'git commit'");
357 static void write_message(struct strbuf
*msgbuf
, const char *filename
)
359 static struct lock_file msg_file
;
361 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
,
363 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
364 die_errno(_("Could not write to %s"), filename
);
365 strbuf_release(msgbuf
);
366 if (commit_lock_file(&msg_file
) < 0)
367 die(_("Error wrapping up %s"), filename
);
370 static struct tree
*empty_tree(void)
372 return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN
);
375 static int error_dirty_index(struct replay_opts
*opts
)
377 if (read_cache_unmerged())
378 return error_resolve_conflict(action_name(opts
));
380 /* Different translation strings for cherry-pick and revert */
381 if (opts
->action
== REPLAY_PICK
)
382 error(_("Your local changes would be overwritten by cherry-pick."));
384 error(_("Your local changes would be overwritten by revert."));
386 if (advice_commit_before_merge
)
387 advise(_("Commit your changes or stash them to proceed."));
391 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
)
393 struct ref_lock
*ref_lock
;
396 if (checkout_fast_forward(from
, to
))
397 exit(1); /* the callee should have complained already */
398 ref_lock
= lock_any_ref_for_update("HEAD", from
, 0);
399 return write_ref_sha1(ref_lock
, to
, "cherry-pick");
402 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
403 const char *base_label
, const char *next_label
,
404 unsigned char *head
, struct strbuf
*msgbuf
,
405 struct replay_opts
*opts
)
407 struct merge_options o
;
408 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
411 static struct lock_file index_lock
;
413 index_fd
= hold_locked_index(&index_lock
, 1);
417 init_merge_options(&o
);
418 o
.ancestor
= base
? base_label
: "(empty tree)";
420 o
.branch2
= next
? next_label
: "(empty tree)";
422 head_tree
= parse_tree_indirect(head
);
423 next_tree
= next
? next
->tree
: empty_tree();
424 base_tree
= base
? base
->tree
: empty_tree();
426 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
427 parse_merge_opt(&o
, *xopt
);
429 clean
= merge_trees(&o
,
431 next_tree
, base_tree
, &result
);
433 if (active_cache_changed
&&
434 (write_cache(index_fd
, active_cache
, active_nr
) ||
435 commit_locked_index(&index_lock
)))
436 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
437 die(_("%s: Unable to write new index file"), action_name(opts
));
438 rollback_lock_file(&index_lock
);
442 strbuf_addstr(msgbuf
, "\nConflicts:\n\n");
443 for (i
= 0; i
< active_nr
;) {
444 struct cache_entry
*ce
= active_cache
[i
++];
446 strbuf_addch(msgbuf
, '\t');
447 strbuf_addstr(msgbuf
, ce
->name
);
448 strbuf_addch(msgbuf
, '\n');
449 while (i
< active_nr
&& !strcmp(ce
->name
,
450 active_cache
[i
]->name
))
460 * If we are cherry-pick, and if the merge did not result in
461 * hand-editing, we will hit this commit and inherit the original
462 * author date and name.
463 * If we are revert, or if our cherry-pick results in a hand merge,
464 * we had better say that the current user is responsible for that.
466 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
)
468 /* 6 is max possible length of our args array including NULL */
472 args
[i
++] = "commit";
482 return run_command_v_opt(args
, RUN_GIT_CMD
);
485 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
487 unsigned char head
[20];
488 struct commit
*base
, *next
, *parent
;
489 const char *base_label
, *next_label
;
490 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
492 struct strbuf msgbuf
= STRBUF_INIT
;
495 if (opts
->no_commit
) {
497 * We do not intend to commit immediately. We just want to
498 * merge the differences in, so let's compute the tree
499 * that represents the "current" state for merge-recursive
502 if (write_cache_as_tree(head
, 0, NULL
))
503 die (_("Your index file is unmerged."));
505 if (get_sha1("HEAD", head
))
506 return error(_("You do not have a valid HEAD"));
507 if (index_differs_from("HEAD", 0))
508 return error_dirty_index(opts
);
512 if (!commit
->parents
) {
515 else if (commit
->parents
->next
) {
516 /* Reverting or cherry-picking a merge commit */
518 struct commit_list
*p
;
521 return error(_("Commit %s is a merge but no -m option was given."),
522 sha1_to_hex(commit
->object
.sha1
));
524 for (cnt
= 1, p
= commit
->parents
;
525 cnt
!= opts
->mainline
&& p
;
528 if (cnt
!= opts
->mainline
|| !p
)
529 return error(_("Commit %s does not have parent %d"),
530 sha1_to_hex(commit
->object
.sha1
), opts
->mainline
);
532 } else if (0 < opts
->mainline
)
533 return error(_("Mainline was specified but commit %s is not a merge."),
534 sha1_to_hex(commit
->object
.sha1
));
536 parent
= commit
->parents
->item
;
538 if (opts
->allow_ff
&& parent
&& !hashcmp(parent
->object
.sha1
, head
))
539 return fast_forward_to(commit
->object
.sha1
, head
);
541 if (parent
&& parse_commit(parent
) < 0)
542 /* TRANSLATORS: The first %s will be "revert" or
543 "cherry-pick", the second %s a SHA1 */
544 return error(_("%s: cannot parse parent commit %s"),
545 action_name(opts
), sha1_to_hex(parent
->object
.sha1
));
547 if (get_message(commit
, &msg
) != 0)
548 return error(_("Cannot get commit message for %s"),
549 sha1_to_hex(commit
->object
.sha1
));
552 * "commit" is an existing commit. We would want to apply
553 * the difference it introduces since its first parent "prev"
554 * on top of the current HEAD if we are cherry-pick. Or the
555 * reverse of it if we are revert.
558 defmsg
= git_pathdup("MERGE_MSG");
560 if (opts
->action
== REPLAY_REVERT
) {
562 base_label
= msg
.label
;
564 next_label
= msg
.parent_label
;
565 strbuf_addstr(&msgbuf
, "Revert \"");
566 strbuf_addstr(&msgbuf
, msg
.subject
);
567 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
568 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
570 if (commit
->parents
&& commit
->parents
->next
) {
571 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
572 strbuf_addstr(&msgbuf
, sha1_to_hex(parent
->object
.sha1
));
574 strbuf_addstr(&msgbuf
, ".\n");
579 base_label
= msg
.parent_label
;
581 next_label
= msg
.label
;
584 * Append the commit log message to msgbuf; it starts
585 * after the tree, parent, author, committer
586 * information followed by "\n\n".
588 p
= strstr(msg
.message
, "\n\n");
591 strbuf_addstr(&msgbuf
, p
);
594 if (opts
->record_origin
) {
595 strbuf_addstr(&msgbuf
, "(cherry picked from commit ");
596 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
597 strbuf_addstr(&msgbuf
, ")\n");
601 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REPLAY_REVERT
) {
602 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
603 head
, &msgbuf
, opts
);
604 write_message(&msgbuf
, defmsg
);
606 struct commit_list
*common
= NULL
;
607 struct commit_list
*remotes
= NULL
;
609 write_message(&msgbuf
, defmsg
);
611 commit_list_insert(base
, &common
);
612 commit_list_insert(next
, &remotes
);
613 res
= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
614 common
, sha1_to_hex(head
), remotes
);
615 free_commit_list(common
);
616 free_commit_list(remotes
);
620 * If the merge was clean or if it failed due to conflict, we write
621 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
622 * However, if the merge did not even start, then we don't want to
625 if (opts
->action
== REPLAY_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1))
626 write_cherry_pick_head(commit
, "CHERRY_PICK_HEAD");
627 if (opts
->action
== REPLAY_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1))
628 write_cherry_pick_head(commit
, "REVERT_HEAD");
631 error(opts
->action
== REPLAY_REVERT
632 ? _("could not revert %s... %s")
633 : _("could not apply %s... %s"),
634 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
),
636 print_advice(res
== 1);
637 rerere(opts
->allow_rerere_auto
);
639 if (!opts
->no_commit
)
640 res
= run_git_commit(defmsg
, opts
);
649 static void prepare_revs(struct replay_opts
*opts
)
651 if (opts
->action
!= REPLAY_REVERT
)
652 opts
->revs
->reverse
^= 1;
654 if (prepare_revision_walk(opts
->revs
))
655 die(_("revision walk setup failed"));
657 if (!opts
->revs
->commits
)
658 die(_("empty commit set passed"));
661 static void read_and_refresh_cache(struct replay_opts
*opts
)
663 static struct lock_file index_lock
;
664 int index_fd
= hold_locked_index(&index_lock
, 0);
665 if (read_index_preload(&the_index
, NULL
) < 0)
666 die(_("git %s: failed to read the index"), action_name(opts
));
667 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
668 if (the_index
.cache_changed
) {
669 if (write_index(&the_index
, index_fd
) ||
670 commit_locked_index(&index_lock
))
671 die(_("git %s: failed to refresh the index"), action_name(opts
));
673 rollback_lock_file(&index_lock
);
677 * Append a commit to the end of the commit_list.
679 * next starts by pointing to the variable that holds the head of an
680 * empty commit_list, and is updated to point to the "next" field of
681 * the last item on the list as new commits are appended.
685 * struct commit_list *list;
686 * struct commit_list **next = &list;
688 * next = commit_list_append(c1, next);
689 * next = commit_list_append(c2, next);
690 * assert(commit_list_count(list) == 2);
693 static struct commit_list
**commit_list_append(struct commit
*commit
,
694 struct commit_list
**next
)
696 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
703 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
704 struct replay_opts
*opts
)
706 struct commit_list
*cur
= NULL
;
707 const char *sha1_abbrev
= NULL
;
708 const char *action_str
= opts
->action
== REPLAY_REVERT
? "revert" : "pick";
712 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
713 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.sha1
, DEFAULT_ABBREV
);
714 subject_len
= find_commit_subject(cur
->item
->buffer
, &subject
);
715 strbuf_addf(buf
, "%s %s %.*s\n", action_str
, sha1_abbrev
,
716 subject_len
, subject
);
721 static struct commit
*parse_insn_line(char *bol
, char *eol
, struct replay_opts
*opts
)
723 unsigned char commit_sha1
[20];
724 enum replay_action action
;
725 char *end_of_object_name
;
726 int saved
, status
, padding
;
728 if (!prefixcmp(bol
, "pick")) {
729 action
= REPLAY_PICK
;
730 bol
+= strlen("pick");
731 } else if (!prefixcmp(bol
, "revert")) {
732 action
= REPLAY_REVERT
;
733 bol
+= strlen("revert");
737 /* Eat up extra spaces/ tabs before object name */
738 padding
= strspn(bol
, " \t");
743 end_of_object_name
= bol
+ strcspn(bol
, " \t\n");
744 saved
= *end_of_object_name
;
745 *end_of_object_name
= '\0';
746 status
= get_sha1(bol
, commit_sha1
);
747 *end_of_object_name
= saved
;
750 * Verify that the action matches up with the one in
751 * opts; we don't support arbitrary instructions
753 if (action
!= opts
->action
) {
754 const char *action_str
;
755 action_str
= action
== REPLAY_REVERT
? "revert" : "cherry-pick";
756 error(_("Cannot %s during a %s"), action_str
, action_name(opts
));
763 return lookup_commit_reference(commit_sha1
);
766 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
767 struct replay_opts
*opts
)
769 struct commit_list
**next
= todo_list
;
770 struct commit
*commit
;
774 for (i
= 1; *p
; i
++) {
775 char *eol
= strchrnul(p
, '\n');
776 commit
= parse_insn_line(p
, eol
, opts
);
778 return error(_("Could not parse line %d."), i
);
779 next
= commit_list_append(commit
, next
);
780 p
= *eol
? eol
+ 1 : eol
;
783 return error(_("No commits parsed."));
787 static void read_populate_todo(struct commit_list
**todo_list
,
788 struct replay_opts
*opts
)
790 const char *todo_file
= git_path(SEQ_TODO_FILE
);
791 struct strbuf buf
= STRBUF_INIT
;
794 fd
= open(todo_file
, O_RDONLY
);
796 die_errno(_("Could not open %s"), todo_file
);
797 if (strbuf_read(&buf
, fd
, 0) < 0) {
799 strbuf_release(&buf
);
800 die(_("Could not read %s."), todo_file
);
804 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
805 strbuf_release(&buf
);
807 die(_("Unusable instruction sheet: %s"), todo_file
);
810 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
812 struct replay_opts
*opts
= data
;
817 else if (!strcmp(key
, "options.no-commit"))
818 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
819 else if (!strcmp(key
, "options.edit"))
820 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
821 else if (!strcmp(key
, "options.signoff"))
822 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
823 else if (!strcmp(key
, "options.record-origin"))
824 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
825 else if (!strcmp(key
, "options.allow-ff"))
826 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
827 else if (!strcmp(key
, "options.mainline"))
828 opts
->mainline
= git_config_int(key
, value
);
829 else if (!strcmp(key
, "options.strategy"))
830 git_config_string(&opts
->strategy
, key
, value
);
831 else if (!strcmp(key
, "options.strategy-option")) {
832 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
833 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
835 return error(_("Invalid key: %s"), key
);
838 return error(_("Invalid value for %s: %s"), key
, value
);
843 static void read_populate_opts(struct replay_opts
**opts_ptr
)
845 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
847 if (!file_exists(opts_file
))
849 if (git_config_from_file(populate_opts_cb
, opts_file
, *opts_ptr
) < 0)
850 die(_("Malformed options sheet: %s"), opts_file
);
853 static void walk_revs_populate_todo(struct commit_list
**todo_list
,
854 struct replay_opts
*opts
)
856 struct commit
*commit
;
857 struct commit_list
**next
;
862 while ((commit
= get_revision(opts
->revs
)))
863 next
= commit_list_append(commit
, next
);
866 static int create_seq_dir(void)
868 const char *seq_dir
= git_path(SEQ_DIR
);
870 if (file_exists(seq_dir
)) {
871 error(_("a cherry-pick or revert is already in progress"));
872 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
875 else if (mkdir(seq_dir
, 0777) < 0)
876 die_errno(_("Could not create sequencer directory %s"), seq_dir
);
880 static void save_head(const char *head
)
882 const char *head_file
= git_path(SEQ_HEAD_FILE
);
883 static struct lock_file head_lock
;
884 struct strbuf buf
= STRBUF_INIT
;
887 fd
= hold_lock_file_for_update(&head_lock
, head_file
, LOCK_DIE_ON_ERROR
);
888 strbuf_addf(&buf
, "%s\n", head
);
889 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0)
890 die_errno(_("Could not write to %s"), head_file
);
891 if (commit_lock_file(&head_lock
) < 0)
892 die(_("Error wrapping up %s."), head_file
);
895 static int reset_for_rollback(const unsigned char *sha1
)
897 const char *argv
[4]; /* reset --merge <arg> + NULL */
900 argv
[2] = sha1_to_hex(sha1
);
902 return run_command_v_opt(argv
, RUN_GIT_CMD
);
905 static int rollback_single_pick(void)
907 unsigned char head_sha1
[20];
909 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
910 !file_exists(git_path("REVERT_HEAD")))
911 return error(_("no cherry-pick or revert in progress"));
912 if (read_ref_full("HEAD", head_sha1
, 0, NULL
))
913 return error(_("cannot resolve HEAD"));
914 if (is_null_sha1(head_sha1
))
915 return error(_("cannot abort from a branch yet to be born"));
916 return reset_for_rollback(head_sha1
);
919 static int sequencer_rollback(struct replay_opts
*opts
)
921 const char *filename
;
923 unsigned char sha1
[20];
924 struct strbuf buf
= STRBUF_INIT
;
926 filename
= git_path(SEQ_HEAD_FILE
);
927 f
= fopen(filename
, "r");
928 if (!f
&& errno
== ENOENT
) {
930 * There is no multiple-cherry-pick in progress.
931 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
932 * a single-cherry-pick in progress, abort that.
934 return rollback_single_pick();
937 return error(_("cannot open %s: %s"), filename
,
939 if (strbuf_getline(&buf
, f
, '\n')) {
940 error(_("cannot read %s: %s"), filename
, ferror(f
) ?
941 strerror(errno
) : _("unexpected end of file"));
946 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
947 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
951 if (reset_for_rollback(sha1
))
953 remove_sequencer_state();
954 strbuf_release(&buf
);
957 strbuf_release(&buf
);
961 static void save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
963 const char *todo_file
= git_path(SEQ_TODO_FILE
);
964 static struct lock_file todo_lock
;
965 struct strbuf buf
= STRBUF_INIT
;
968 fd
= hold_lock_file_for_update(&todo_lock
, todo_file
, LOCK_DIE_ON_ERROR
);
969 if (format_todo(&buf
, todo_list
, opts
) < 0)
970 die(_("Could not format %s."), todo_file
);
971 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
972 strbuf_release(&buf
);
973 die_errno(_("Could not write to %s"), todo_file
);
975 if (commit_lock_file(&todo_lock
) < 0) {
976 strbuf_release(&buf
);
977 die(_("Error wrapping up %s."), todo_file
);
979 strbuf_release(&buf
);
982 static void save_opts(struct replay_opts
*opts
)
984 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
987 git_config_set_in_file(opts_file
, "options.no-commit", "true");
989 git_config_set_in_file(opts_file
, "options.edit", "true");
991 git_config_set_in_file(opts_file
, "options.signoff", "true");
992 if (opts
->record_origin
)
993 git_config_set_in_file(opts_file
, "options.record-origin", "true");
995 git_config_set_in_file(opts_file
, "options.allow-ff", "true");
996 if (opts
->mainline
) {
997 struct strbuf buf
= STRBUF_INIT
;
998 strbuf_addf(&buf
, "%d", opts
->mainline
);
999 git_config_set_in_file(opts_file
, "options.mainline", buf
.buf
);
1000 strbuf_release(&buf
);
1003 git_config_set_in_file(opts_file
, "options.strategy", opts
->strategy
);
1006 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1007 git_config_set_multivar_in_file(opts_file
,
1008 "options.strategy-option",
1009 opts
->xopts
[i
], "^$", 0);
1013 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
1015 struct commit_list
*cur
;
1018 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1020 assert(!(opts
->signoff
|| opts
->no_commit
||
1021 opts
->record_origin
|| opts
->edit
));
1022 read_and_refresh_cache(opts
);
1024 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
1025 save_todo(cur
, opts
);
1026 res
= do_pick_commit(cur
->item
, opts
);
1032 * Sequence of picks finished successfully; cleanup by
1033 * removing the .git/sequencer directory
1035 remove_sequencer_state();
1039 static int continue_single_pick(void)
1041 const char *argv
[] = { "commit", NULL
};
1043 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
1044 !file_exists(git_path("REVERT_HEAD")))
1045 return error(_("no cherry-pick or revert in progress"));
1046 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1049 static int sequencer_continue(struct replay_opts
*opts
)
1051 struct commit_list
*todo_list
= NULL
;
1053 if (!file_exists(git_path(SEQ_TODO_FILE
)))
1054 return continue_single_pick();
1055 read_populate_opts(&opts
);
1056 read_populate_todo(&todo_list
, opts
);
1058 /* Verify that the conflict has been resolved */
1059 if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
1060 file_exists(git_path("REVERT_HEAD"))) {
1061 int ret
= continue_single_pick();
1065 if (index_differs_from("HEAD", 0))
1066 return error_dirty_index(opts
);
1067 todo_list
= todo_list
->next
;
1068 return pick_commits(todo_list
, opts
);
1071 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1073 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1074 return do_pick_commit(cmit
, opts
);
1077 static int pick_revisions(struct replay_opts
*opts
)
1079 struct commit_list
*todo_list
= NULL
;
1080 unsigned char sha1
[20];
1082 if (opts
->subcommand
== REPLAY_NONE
)
1085 read_and_refresh_cache(opts
);
1088 * Decide what to do depending on the arguments; a fresh
1089 * cherry-pick should be handled differently from an existing
1090 * one that is being continued
1092 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1093 remove_sequencer_state();
1096 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1097 return sequencer_rollback(opts
);
1098 if (opts
->subcommand
== REPLAY_CONTINUE
)
1099 return sequencer_continue(opts
);
1102 * If we were called as "git cherry-pick <commit>", just
1103 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1104 * REVERT_HEAD, and don't touch the sequencer state.
1105 * This means it is possible to cherry-pick in the middle
1106 * of a cherry-pick sequence.
1108 if (opts
->revs
->cmdline
.nr
== 1 &&
1109 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1110 opts
->revs
->no_walk
&&
1111 !opts
->revs
->cmdline
.rev
->flags
) {
1112 struct commit
*cmit
;
1113 if (prepare_revision_walk(opts
->revs
))
1114 die(_("revision walk setup failed"));
1115 cmit
= get_revision(opts
->revs
);
1116 if (!cmit
|| get_revision(opts
->revs
))
1117 die("BUG: expected exactly one commit from walk");
1118 return single_pick(cmit
, opts
);
1122 * Start a new cherry-pick/ revert sequence; but
1123 * first, make sure that an existing one isn't in
1127 walk_revs_populate_todo(&todo_list
, opts
);
1128 if (create_seq_dir() < 0)
1130 if (get_sha1("HEAD", sha1
)) {
1131 if (opts
->action
== REPLAY_REVERT
)
1132 return error(_("Can't revert as initial commit"));
1133 return error(_("Can't cherry-pick into empty head"));
1135 save_head(sha1_to_hex(sha1
));
1137 return pick_commits(todo_list
, opts
);
1140 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
1142 struct replay_opts opts
;
1145 memset(&opts
, 0, sizeof(opts
));
1148 opts
.action
= REPLAY_REVERT
;
1149 git_config(git_default_config
, NULL
);
1150 parse_args(argc
, argv
, &opts
);
1151 res
= pick_revisions(&opts
);
1153 die(_("revert failed"));
1157 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
1159 struct replay_opts opts
;
1162 memset(&opts
, 0, sizeof(opts
));
1163 opts
.action
= REPLAY_PICK
;
1164 git_config(git_default_config
, NULL
);
1165 parse_args(argc
, argv
, &opts
);
1166 res
= pick_revisions(&opts
);
1168 die(_("cherry-pick failed"));