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
{
51 enum replay_action action
;
52 enum replay_subcommand subcommand
;
60 int allow_rerere_auto
;
67 size_t xopts_nr
, xopts_alloc
;
69 /* Only used by REPLAY_NONE */
70 struct rev_info
*revs
;
73 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
75 static const char *action_name(const struct replay_opts
*opts
)
77 return opts
->action
== REVERT
? "revert" : "cherry-pick";
80 static char *get_encoding(const char *message
);
82 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
84 return opts
->action
== REVERT
? revert_usage
: cherry_pick_usage
;
87 static int option_parse_x(const struct option
*opt
,
88 const char *arg
, int unset
)
90 struct replay_opts
**opts_ptr
= opt
->value
;
91 struct replay_opts
*opts
= *opts_ptr
;
96 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
97 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(arg
);
101 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
103 const char *this_opt
;
106 va_start(ap
, base_opt
);
107 while ((this_opt
= va_arg(ap
, const char *))) {
114 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
117 static void verify_opt_mutually_compatible(const char *me
, ...)
119 const char *opt1
, *opt2
= NULL
;
123 while ((opt1
= va_arg(ap
, const char *))) {
128 while ((opt2
= va_arg(ap
, const char *))) {
135 die(_("%s: %s cannot be used with %s"), me
, opt1
, opt2
);
138 static void parse_args(int argc
, const char **argv
, struct replay_opts
*opts
)
140 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
141 const char *me
= action_name(opts
);
142 int remove_state
= 0;
145 struct option options
[] = {
146 OPT_BOOLEAN(0, "quit", &remove_state
, "end revert or cherry-pick sequence"),
147 OPT_BOOLEAN(0, "continue", &contin
, "resume revert or cherry-pick sequence"),
148 OPT_BOOLEAN(0, "abort", &rollback
, "cancel revert or cherry-pick sequence"),
149 OPT_BOOLEAN('n', "no-commit", &opts
->no_commit
, "don't automatically commit"),
150 OPT_BOOLEAN('e', "edit", &opts
->edit
, "edit the commit message"),
151 OPT_NOOP_NOARG('r', NULL
),
152 OPT_BOOLEAN('s', "signoff", &opts
->signoff
, "add Signed-off-by:"),
153 OPT_INTEGER('m', "mainline", &opts
->mainline
, "parent number"),
154 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
155 OPT_STRING(0, "strategy", &opts
->strategy
, "strategy", "merge strategy"),
156 OPT_CALLBACK('X', "strategy-option", &opts
, "option",
157 "option for merge strategy", option_parse_x
),
163 if (opts
->action
== CHERRY_PICK
) {
164 struct option cp_extra
[] = {
165 OPT_BOOLEAN('x', NULL
, &opts
->record_origin
, "append commit name"),
166 OPT_BOOLEAN(0, "ff", &opts
->allow_ff
, "allow fast-forward"),
169 if (parse_options_concat(options
, ARRAY_SIZE(options
), cp_extra
))
170 die(_("program error"));
173 argc
= parse_options(argc
, argv
, NULL
, options
, usage_str
,
174 PARSE_OPT_KEEP_ARGV0
|
175 PARSE_OPT_KEEP_UNKNOWN
);
177 /* Check for incompatible subcommands */
178 verify_opt_mutually_compatible(me
,
179 "--quit", remove_state
,
180 "--continue", contin
,
184 /* Set the subcommand */
186 opts
->subcommand
= REPLAY_REMOVE_STATE
;
188 opts
->subcommand
= REPLAY_CONTINUE
;
190 opts
->subcommand
= REPLAY_ROLLBACK
;
192 opts
->subcommand
= REPLAY_NONE
;
194 /* Check for incompatible command line arguments */
195 if (opts
->subcommand
!= REPLAY_NONE
) {
196 char *this_operation
;
197 if (opts
->subcommand
== REPLAY_REMOVE_STATE
)
198 this_operation
= "--quit";
199 else if (opts
->subcommand
== REPLAY_CONTINUE
)
200 this_operation
= "--continue";
202 assert(opts
->subcommand
== REPLAY_ROLLBACK
);
203 this_operation
= "--abort";
206 verify_opt_compatible(me
, this_operation
,
207 "--no-commit", opts
->no_commit
,
208 "--signoff", opts
->signoff
,
209 "--mainline", opts
->mainline
,
210 "--strategy", opts
->strategy
? 1 : 0,
211 "--strategy-option", opts
->xopts
? 1 : 0,
212 "-x", opts
->record_origin
,
213 "--ff", opts
->allow_ff
,
218 verify_opt_compatible(me
, "--ff",
219 "--signoff", opts
->signoff
,
220 "--no-commit", opts
->no_commit
,
221 "-x", opts
->record_origin
,
222 "--edit", opts
->edit
,
225 if (opts
->subcommand
!= REPLAY_NONE
) {
228 opts
->revs
= xmalloc(sizeof(*opts
->revs
));
229 init_revisions(opts
->revs
, NULL
);
230 opts
->revs
->no_walk
= 1;
232 usage_with_options(usage_str
, options
);
233 argc
= setup_revisions(argc
, argv
, opts
->revs
, NULL
);
237 usage_with_options(usage_str
, options
);
240 struct commit_message
{
244 char *reencoded_message
;
248 static int get_message(struct commit
*commit
, struct commit_message
*out
)
250 const char *encoding
;
251 const char *abbrev
, *subject
;
252 int abbrev_len
, subject_len
;
257 encoding
= get_encoding(commit
->buffer
);
260 if (!git_commit_encoding
)
261 git_commit_encoding
= "UTF-8";
263 out
->reencoded_message
= NULL
;
264 out
->message
= commit
->buffer
;
265 if (strcmp(encoding
, git_commit_encoding
))
266 out
->reencoded_message
= reencode_string(commit
->buffer
,
267 git_commit_encoding
, encoding
);
268 if (out
->reencoded_message
)
269 out
->message
= out
->reencoded_message
;
271 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
272 abbrev_len
= strlen(abbrev
);
274 subject_len
= find_commit_subject(out
->message
, &subject
);
276 out
->parent_label
= xmalloc(strlen("parent of ") + abbrev_len
+
277 strlen("... ") + subject_len
+ 1);
278 q
= out
->parent_label
;
279 q
= mempcpy(q
, "parent of ", strlen("parent of "));
281 q
= mempcpy(q
, abbrev
, abbrev_len
);
282 q
= mempcpy(q
, "... ", strlen("... "));
284 q
= mempcpy(q
, subject
, subject_len
);
289 static void free_message(struct commit_message
*msg
)
291 free(msg
->parent_label
);
292 free(msg
->reencoded_message
);
295 static char *get_encoding(const char *message
)
297 const char *p
= message
, *eol
;
299 while (*p
&& *p
!= '\n') {
300 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
302 if (!prefixcmp(p
, "encoding ")) {
303 char *result
= xmalloc(eol
- 8 - p
);
304 strlcpy(result
, p
+ 9, eol
- 8 - p
);
314 static void write_cherry_pick_head(struct commit
*commit
, const char *pseudoref
)
316 const char *filename
;
318 struct strbuf buf
= STRBUF_INIT
;
320 strbuf_addf(&buf
, "%s\n", sha1_to_hex(commit
->object
.sha1
));
322 filename
= git_path("%s", pseudoref
);
323 fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
325 die_errno(_("Could not open '%s' for writing"), filename
);
326 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
|| close(fd
))
327 die_errno(_("Could not write to '%s'"), filename
);
328 strbuf_release(&buf
);
331 static void print_advice(int show_hint
)
333 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
336 fprintf(stderr
, "%s\n", msg
);
338 * A conflict has occured but the porcelain
339 * (typically rebase --interactive) wants to take care
340 * of the commit itself so remove CHERRY_PICK_HEAD
342 unlink(git_path("CHERRY_PICK_HEAD"));
347 advise(_("after resolving the conflicts, mark the corrected paths\n"
348 "with 'git add <paths>' or 'git rm <paths>'\n"
349 "and commit the result with 'git commit'"));
352 static void write_message(struct strbuf
*msgbuf
, const char *filename
)
354 static struct lock_file msg_file
;
356 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
,
358 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
359 die_errno(_("Could not write to %s"), filename
);
360 strbuf_release(msgbuf
);
361 if (commit_lock_file(&msg_file
) < 0)
362 die(_("Error wrapping up %s"), filename
);
365 static struct tree
*empty_tree(void)
367 return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN
);
370 static int error_dirty_index(struct replay_opts
*opts
)
372 if (read_cache_unmerged())
373 return error_resolve_conflict(action_name(opts
));
375 /* Different translation strings for cherry-pick and revert */
376 if (opts
->action
== CHERRY_PICK
)
377 error(_("Your local changes would be overwritten by cherry-pick."));
379 error(_("Your local changes would be overwritten by revert."));
381 if (advice_commit_before_merge
)
382 advise(_("Commit your changes or stash them to proceed."));
386 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
)
388 struct ref_lock
*ref_lock
;
391 if (checkout_fast_forward(from
, to
))
392 exit(1); /* the callee should have complained already */
393 ref_lock
= lock_any_ref_for_update("HEAD", from
, 0);
394 return write_ref_sha1(ref_lock
, to
, "cherry-pick");
397 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
398 const char *base_label
, const char *next_label
,
399 unsigned char *head
, struct strbuf
*msgbuf
,
400 struct replay_opts
*opts
)
402 struct merge_options o
;
403 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
406 static struct lock_file index_lock
;
408 index_fd
= hold_locked_index(&index_lock
, 1);
412 init_merge_options(&o
);
413 o
.ancestor
= base
? base_label
: "(empty tree)";
415 o
.branch2
= next
? next_label
: "(empty tree)";
417 head_tree
= parse_tree_indirect(head
);
418 next_tree
= next
? next
->tree
: empty_tree();
419 base_tree
= base
? base
->tree
: empty_tree();
421 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
422 parse_merge_opt(&o
, *xopt
);
424 clean
= merge_trees(&o
,
426 next_tree
, base_tree
, &result
);
428 if (active_cache_changed
&&
429 (write_cache(index_fd
, active_cache
, active_nr
) ||
430 commit_locked_index(&index_lock
)))
431 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
432 die(_("%s: Unable to write new index file"), action_name(opts
));
433 rollback_lock_file(&index_lock
);
437 strbuf_addstr(msgbuf
, "\nConflicts:\n\n");
438 for (i
= 0; i
< active_nr
;) {
439 struct cache_entry
*ce
= active_cache
[i
++];
441 strbuf_addch(msgbuf
, '\t');
442 strbuf_addstr(msgbuf
, ce
->name
);
443 strbuf_addch(msgbuf
, '\n');
444 while (i
< active_nr
&& !strcmp(ce
->name
,
445 active_cache
[i
]->name
))
455 * If we are cherry-pick, and if the merge did not result in
456 * hand-editing, we will hit this commit and inherit the original
457 * author date and name.
458 * If we are revert, or if our cherry-pick results in a hand merge,
459 * we had better say that the current user is responsible for that.
461 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
)
463 /* 6 is max possible length of our args array including NULL */
467 args
[i
++] = "commit";
477 return run_command_v_opt(args
, RUN_GIT_CMD
);
480 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
482 unsigned char head
[20];
483 struct commit
*base
, *next
, *parent
;
484 const char *base_label
, *next_label
;
485 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
487 struct strbuf msgbuf
= STRBUF_INIT
;
490 if (opts
->no_commit
) {
492 * We do not intend to commit immediately. We just want to
493 * merge the differences in, so let's compute the tree
494 * that represents the "current" state for merge-recursive
497 if (write_cache_as_tree(head
, 0, NULL
))
498 die (_("Your index file is unmerged."));
500 if (get_sha1("HEAD", head
))
501 return error(_("You do not have a valid HEAD"));
502 if (index_differs_from("HEAD", 0))
503 return error_dirty_index(opts
);
507 if (!commit
->parents
) {
510 else if (commit
->parents
->next
) {
511 /* Reverting or cherry-picking a merge commit */
513 struct commit_list
*p
;
516 return error(_("Commit %s is a merge but no -m option was given."),
517 sha1_to_hex(commit
->object
.sha1
));
519 for (cnt
= 1, p
= commit
->parents
;
520 cnt
!= opts
->mainline
&& p
;
523 if (cnt
!= opts
->mainline
|| !p
)
524 return error(_("Commit %s does not have parent %d"),
525 sha1_to_hex(commit
->object
.sha1
), opts
->mainline
);
527 } else if (0 < opts
->mainline
)
528 return error(_("Mainline was specified but commit %s is not a merge."),
529 sha1_to_hex(commit
->object
.sha1
));
531 parent
= commit
->parents
->item
;
533 if (opts
->allow_ff
&& parent
&& !hashcmp(parent
->object
.sha1
, head
))
534 return fast_forward_to(commit
->object
.sha1
, head
);
536 if (parent
&& parse_commit(parent
) < 0)
537 /* TRANSLATORS: The first %s will be "revert" or
538 "cherry-pick", the second %s a SHA1 */
539 return error(_("%s: cannot parse parent commit %s"),
540 action_name(opts
), sha1_to_hex(parent
->object
.sha1
));
542 if (get_message(commit
, &msg
) != 0)
543 return error(_("Cannot get commit message for %s"),
544 sha1_to_hex(commit
->object
.sha1
));
547 * "commit" is an existing commit. We would want to apply
548 * the difference it introduces since its first parent "prev"
549 * on top of the current HEAD if we are cherry-pick. Or the
550 * reverse of it if we are revert.
553 defmsg
= git_pathdup("MERGE_MSG");
555 if (opts
->action
== REVERT
) {
557 base_label
= msg
.label
;
559 next_label
= msg
.parent_label
;
560 strbuf_addstr(&msgbuf
, "Revert \"");
561 strbuf_addstr(&msgbuf
, msg
.subject
);
562 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
563 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
565 if (commit
->parents
&& commit
->parents
->next
) {
566 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
567 strbuf_addstr(&msgbuf
, sha1_to_hex(parent
->object
.sha1
));
569 strbuf_addstr(&msgbuf
, ".\n");
574 base_label
= msg
.parent_label
;
576 next_label
= msg
.label
;
579 * Append the commit log message to msgbuf; it starts
580 * after the tree, parent, author, committer
581 * information followed by "\n\n".
583 p
= strstr(msg
.message
, "\n\n");
586 strbuf_addstr(&msgbuf
, p
);
589 if (opts
->record_origin
) {
590 strbuf_addstr(&msgbuf
, "(cherry picked from commit ");
591 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
592 strbuf_addstr(&msgbuf
, ")\n");
596 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REVERT
) {
597 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
598 head
, &msgbuf
, opts
);
599 write_message(&msgbuf
, defmsg
);
601 struct commit_list
*common
= NULL
;
602 struct commit_list
*remotes
= NULL
;
604 write_message(&msgbuf
, defmsg
);
606 commit_list_insert(base
, &common
);
607 commit_list_insert(next
, &remotes
);
608 res
= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
609 common
, sha1_to_hex(head
), remotes
);
610 free_commit_list(common
);
611 free_commit_list(remotes
);
615 * If the merge was clean or if it failed due to conflict, we write
616 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
617 * However, if the merge did not even start, then we don't want to
620 if (opts
->action
== CHERRY_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1))
621 write_cherry_pick_head(commit
, "CHERRY_PICK_HEAD");
622 if (opts
->action
== REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1))
623 write_cherry_pick_head(commit
, "REVERT_HEAD");
626 error(opts
->action
== REVERT
627 ? _("could not revert %s... %s")
628 : _("could not apply %s... %s"),
629 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
),
631 print_advice(res
== 1);
632 rerere(opts
->allow_rerere_auto
);
634 if (!opts
->no_commit
)
635 res
= run_git_commit(defmsg
, opts
);
644 static void prepare_revs(struct replay_opts
*opts
)
646 if (opts
->action
!= REVERT
)
647 opts
->revs
->reverse
^= 1;
649 if (prepare_revision_walk(opts
->revs
))
650 die(_("revision walk setup failed"));
652 if (!opts
->revs
->commits
)
653 die(_("empty commit set passed"));
656 static void read_and_refresh_cache(struct replay_opts
*opts
)
658 static struct lock_file index_lock
;
659 int index_fd
= hold_locked_index(&index_lock
, 0);
660 if (read_index_preload(&the_index
, NULL
) < 0)
661 die(_("git %s: failed to read the index"), action_name(opts
));
662 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
663 if (the_index
.cache_changed
) {
664 if (write_index(&the_index
, index_fd
) ||
665 commit_locked_index(&index_lock
))
666 die(_("git %s: failed to refresh the index"), action_name(opts
));
668 rollback_lock_file(&index_lock
);
672 * Append a commit to the end of the commit_list.
674 * next starts by pointing to the variable that holds the head of an
675 * empty commit_list, and is updated to point to the "next" field of
676 * the last item on the list as new commits are appended.
680 * struct commit_list *list;
681 * struct commit_list **next = &list;
683 * next = commit_list_append(c1, next);
684 * next = commit_list_append(c2, next);
685 * assert(commit_list_count(list) == 2);
688 static struct commit_list
**commit_list_append(struct commit
*commit
,
689 struct commit_list
**next
)
691 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
698 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
699 struct replay_opts
*opts
)
701 struct commit_list
*cur
= NULL
;
702 const char *sha1_abbrev
= NULL
;
703 const char *action_str
= opts
->action
== REVERT
? "revert" : "pick";
707 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
708 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.sha1
, DEFAULT_ABBREV
);
709 subject_len
= find_commit_subject(cur
->item
->buffer
, &subject
);
710 strbuf_addf(buf
, "%s %s %.*s\n", action_str
, sha1_abbrev
,
711 subject_len
, subject
);
716 static struct commit
*parse_insn_line(char *bol
, char *eol
, struct replay_opts
*opts
)
718 unsigned char commit_sha1
[20];
719 enum replay_action action
;
720 char *end_of_object_name
;
721 int saved
, status
, padding
;
723 if (!prefixcmp(bol
, "pick")) {
724 action
= CHERRY_PICK
;
725 bol
+= strlen("pick");
726 } else if (!prefixcmp(bol
, "revert")) {
728 bol
+= strlen("revert");
732 /* Eat up extra spaces/ tabs before object name */
733 padding
= strspn(bol
, " \t");
738 end_of_object_name
= bol
+ strcspn(bol
, " \t\n");
739 saved
= *end_of_object_name
;
740 *end_of_object_name
= '\0';
741 status
= get_sha1(bol
, commit_sha1
);
742 *end_of_object_name
= saved
;
745 * Verify that the action matches up with the one in
746 * opts; we don't support arbitrary instructions
748 if (action
!= opts
->action
) {
749 const char *action_str
;
750 action_str
= action
== REVERT
? "revert" : "cherry-pick";
751 error(_("Cannot %s during a %s"), action_str
, action_name(opts
));
758 return lookup_commit_reference(commit_sha1
);
761 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
762 struct replay_opts
*opts
)
764 struct commit_list
**next
= todo_list
;
765 struct commit
*commit
;
769 for (i
= 1; *p
; i
++) {
770 char *eol
= strchrnul(p
, '\n');
771 commit
= parse_insn_line(p
, eol
, opts
);
773 return error(_("Could not parse line %d."), i
);
774 next
= commit_list_append(commit
, next
);
775 p
= *eol
? eol
+ 1 : eol
;
778 return error(_("No commits parsed."));
782 static void read_populate_todo(struct commit_list
**todo_list
,
783 struct replay_opts
*opts
)
785 const char *todo_file
= git_path(SEQ_TODO_FILE
);
786 struct strbuf buf
= STRBUF_INIT
;
789 fd
= open(todo_file
, O_RDONLY
);
791 die_errno(_("Could not open %s"), todo_file
);
792 if (strbuf_read(&buf
, fd
, 0) < 0) {
794 strbuf_release(&buf
);
795 die(_("Could not read %s."), todo_file
);
799 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
800 strbuf_release(&buf
);
802 die(_("Unusable instruction sheet: %s"), todo_file
);
805 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
807 struct replay_opts
*opts
= data
;
812 else if (!strcmp(key
, "options.no-commit"))
813 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
814 else if (!strcmp(key
, "options.edit"))
815 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
816 else if (!strcmp(key
, "options.signoff"))
817 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
818 else if (!strcmp(key
, "options.record-origin"))
819 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
820 else if (!strcmp(key
, "options.allow-ff"))
821 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
822 else if (!strcmp(key
, "options.mainline"))
823 opts
->mainline
= git_config_int(key
, value
);
824 else if (!strcmp(key
, "options.strategy"))
825 git_config_string(&opts
->strategy
, key
, value
);
826 else if (!strcmp(key
, "options.strategy-option")) {
827 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
828 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
830 return error(_("Invalid key: %s"), key
);
833 return error(_("Invalid value for %s: %s"), key
, value
);
838 static void read_populate_opts(struct replay_opts
**opts_ptr
)
840 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
842 if (!file_exists(opts_file
))
844 if (git_config_from_file(populate_opts_cb
, opts_file
, *opts_ptr
) < 0)
845 die(_("Malformed options sheet: %s"), opts_file
);
848 static void walk_revs_populate_todo(struct commit_list
**todo_list
,
849 struct replay_opts
*opts
)
851 struct commit
*commit
;
852 struct commit_list
**next
;
857 while ((commit
= get_revision(opts
->revs
)))
858 next
= commit_list_append(commit
, next
);
861 static int create_seq_dir(void)
863 const char *seq_dir
= git_path(SEQ_DIR
);
865 if (file_exists(seq_dir
)) {
866 error(_("a cherry-pick or revert is already in progress"));
867 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
870 else if (mkdir(seq_dir
, 0777) < 0)
871 die_errno(_("Could not create sequencer directory %s"), seq_dir
);
875 static void save_head(const char *head
)
877 const char *head_file
= git_path(SEQ_HEAD_FILE
);
878 static struct lock_file head_lock
;
879 struct strbuf buf
= STRBUF_INIT
;
882 fd
= hold_lock_file_for_update(&head_lock
, head_file
, LOCK_DIE_ON_ERROR
);
883 strbuf_addf(&buf
, "%s\n", head
);
884 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0)
885 die_errno(_("Could not write to %s"), head_file
);
886 if (commit_lock_file(&head_lock
) < 0)
887 die(_("Error wrapping up %s."), head_file
);
890 static int reset_for_rollback(const unsigned char *sha1
)
892 const char *argv
[4]; /* reset --merge <arg> + NULL */
895 argv
[2] = sha1_to_hex(sha1
);
897 return run_command_v_opt(argv
, RUN_GIT_CMD
);
900 static int rollback_single_pick(void)
902 unsigned char head_sha1
[20];
904 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
905 !file_exists(git_path("REVERT_HEAD")))
906 return error(_("no cherry-pick or revert in progress"));
907 if (read_ref_full("HEAD", head_sha1
, 0, NULL
))
908 return error(_("cannot resolve HEAD"));
909 if (is_null_sha1(head_sha1
))
910 return error(_("cannot abort from a branch yet to be born"));
911 return reset_for_rollback(head_sha1
);
914 static int sequencer_rollback(struct replay_opts
*opts
)
916 const char *filename
;
918 unsigned char sha1
[20];
919 struct strbuf buf
= STRBUF_INIT
;
921 filename
= git_path(SEQ_HEAD_FILE
);
922 f
= fopen(filename
, "r");
923 if (!f
&& errno
== ENOENT
) {
925 * There is no multiple-cherry-pick in progress.
926 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
927 * a single-cherry-pick in progress, abort that.
929 return rollback_single_pick();
932 return error(_("cannot open %s: %s"), filename
,
934 if (strbuf_getline(&buf
, f
, '\n')) {
935 error(_("cannot read %s: %s"), filename
, ferror(f
) ?
936 strerror(errno
) : _("unexpected end of file"));
941 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
942 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
946 if (reset_for_rollback(sha1
))
948 remove_sequencer_state();
949 strbuf_release(&buf
);
952 strbuf_release(&buf
);
956 static void save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
958 const char *todo_file
= git_path(SEQ_TODO_FILE
);
959 static struct lock_file todo_lock
;
960 struct strbuf buf
= STRBUF_INIT
;
963 fd
= hold_lock_file_for_update(&todo_lock
, todo_file
, LOCK_DIE_ON_ERROR
);
964 if (format_todo(&buf
, todo_list
, opts
) < 0)
965 die(_("Could not format %s."), todo_file
);
966 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
967 strbuf_release(&buf
);
968 die_errno(_("Could not write to %s"), todo_file
);
970 if (commit_lock_file(&todo_lock
) < 0) {
971 strbuf_release(&buf
);
972 die(_("Error wrapping up %s."), todo_file
);
974 strbuf_release(&buf
);
977 static void save_opts(struct replay_opts
*opts
)
979 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
982 git_config_set_in_file(opts_file
, "options.no-commit", "true");
984 git_config_set_in_file(opts_file
, "options.edit", "true");
986 git_config_set_in_file(opts_file
, "options.signoff", "true");
987 if (opts
->record_origin
)
988 git_config_set_in_file(opts_file
, "options.record-origin", "true");
990 git_config_set_in_file(opts_file
, "options.allow-ff", "true");
991 if (opts
->mainline
) {
992 struct strbuf buf
= STRBUF_INIT
;
993 strbuf_addf(&buf
, "%d", opts
->mainline
);
994 git_config_set_in_file(opts_file
, "options.mainline", buf
.buf
);
995 strbuf_release(&buf
);
998 git_config_set_in_file(opts_file
, "options.strategy", opts
->strategy
);
1001 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1002 git_config_set_multivar_in_file(opts_file
,
1003 "options.strategy-option",
1004 opts
->xopts
[i
], "^$", 0);
1008 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
1010 struct commit_list
*cur
;
1013 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1015 assert(!(opts
->signoff
|| opts
->no_commit
||
1016 opts
->record_origin
|| opts
->edit
));
1017 read_and_refresh_cache(opts
);
1019 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
1020 save_todo(cur
, opts
);
1021 res
= do_pick_commit(cur
->item
, opts
);
1027 * Sequence of picks finished successfully; cleanup by
1028 * removing the .git/sequencer directory
1030 remove_sequencer_state();
1034 static int continue_single_pick(void)
1036 const char *argv
[] = { "commit", NULL
};
1038 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
1039 !file_exists(git_path("REVERT_HEAD")))
1040 return error(_("no cherry-pick or revert in progress"));
1041 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1044 static int sequencer_continue(struct replay_opts
*opts
)
1046 struct commit_list
*todo_list
= NULL
;
1048 if (!file_exists(git_path(SEQ_TODO_FILE
)))
1049 return continue_single_pick();
1050 read_populate_opts(&opts
);
1051 read_populate_todo(&todo_list
, opts
);
1053 /* Verify that the conflict has been resolved */
1054 if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
1055 file_exists(git_path("REVERT_HEAD"))) {
1056 int ret
= continue_single_pick();
1060 if (index_differs_from("HEAD", 0))
1061 return error_dirty_index(opts
);
1062 todo_list
= todo_list
->next
;
1063 return pick_commits(todo_list
, opts
);
1066 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1068 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1069 return do_pick_commit(cmit
, opts
);
1072 static int pick_revisions(struct replay_opts
*opts
)
1074 struct commit_list
*todo_list
= NULL
;
1075 unsigned char sha1
[20];
1077 if (opts
->subcommand
== REPLAY_NONE
)
1080 read_and_refresh_cache(opts
);
1083 * Decide what to do depending on the arguments; a fresh
1084 * cherry-pick should be handled differently from an existing
1085 * one that is being continued
1087 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1088 remove_sequencer_state();
1091 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1092 return sequencer_rollback(opts
);
1093 if (opts
->subcommand
== REPLAY_CONTINUE
)
1094 return sequencer_continue(opts
);
1097 * If we were called as "git cherry-pick <commit>", just
1098 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1099 * REVERT_HEAD, and don't touch the sequencer state.
1100 * This means it is possible to cherry-pick in the middle
1101 * of a cherry-pick sequence.
1103 if (opts
->revs
->cmdline
.nr
== 1 &&
1104 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1105 opts
->revs
->no_walk
&&
1106 !opts
->revs
->cmdline
.rev
->flags
) {
1107 struct commit
*cmit
;
1108 if (prepare_revision_walk(opts
->revs
))
1109 die(_("revision walk setup failed"));
1110 cmit
= get_revision(opts
->revs
);
1111 if (!cmit
|| get_revision(opts
->revs
))
1112 die("BUG: expected exactly one commit from walk");
1113 return single_pick(cmit
, opts
);
1117 * Start a new cherry-pick/ revert sequence; but
1118 * first, make sure that an existing one isn't in
1122 walk_revs_populate_todo(&todo_list
, opts
);
1123 if (create_seq_dir() < 0)
1125 if (get_sha1("HEAD", sha1
)) {
1126 if (opts
->action
== REVERT
)
1127 return error(_("Can't revert as initial commit"));
1128 return error(_("Can't cherry-pick into empty head"));
1130 save_head(sha1_to_hex(sha1
));
1132 return pick_commits(todo_list
, opts
);
1135 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
1137 struct replay_opts opts
;
1140 memset(&opts
, 0, sizeof(opts
));
1143 opts
.action
= REVERT
;
1144 git_config(git_default_config
, NULL
);
1145 parse_args(argc
, argv
, &opts
);
1146 res
= pick_revisions(&opts
);
1148 die(_("revert failed"));
1152 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
1154 struct replay_opts opts
;
1157 memset(&opts
, 0, sizeof(opts
));
1158 opts
.action
= CHERRY_PICK
;
1159 git_config(git_default_config
, NULL
);
1160 parse_args(argc
, argv
, &opts
);
1161 res
= pick_revisions(&opts
);
1163 die(_("cherry-pick failed"));