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 void fatal(const char *advice
, ...)
73 va_start(params
, advice
);
74 vreportf("fatal: ", advice
, params
);
78 static const char *action_name(const struct replay_opts
*opts
)
80 return opts
->action
== REVERT
? "revert" : "cherry-pick";
83 static char *get_encoding(const char *message
);
85 static const char * const *revert_or_cherry_pick_usage(struct replay_opts
*opts
)
87 return opts
->action
== REVERT
? revert_usage
: cherry_pick_usage
;
90 static int option_parse_x(const struct option
*opt
,
91 const char *arg
, int unset
)
93 struct replay_opts
**opts_ptr
= opt
->value
;
94 struct replay_opts
*opts
= *opts_ptr
;
99 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
100 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(arg
);
104 static void verify_opt_compatible(const char *me
, const char *base_opt
, ...)
106 const char *this_opt
;
109 va_start(ap
, base_opt
);
110 while ((this_opt
= va_arg(ap
, const char *))) {
117 die(_("%s: %s cannot be used with %s"), me
, this_opt
, base_opt
);
120 static void verify_opt_mutually_compatible(const char *me
, ...)
122 const char *opt1
, *opt2
;
126 while ((opt1
= va_arg(ap
, const char *))) {
131 while ((opt2
= va_arg(ap
, const char *))) {
138 die(_("%s: %s cannot be used with %s"), me
, opt1
, opt2
);
141 static void parse_args(int argc
, const char **argv
, struct replay_opts
*opts
)
143 const char * const * usage_str
= revert_or_cherry_pick_usage(opts
);
144 const char *me
= action_name(opts
);
148 struct option options
[] = {
149 OPT_BOOLEAN(0, "reset", &reset
, "forget the current operation"),
150 OPT_BOOLEAN(0, "continue", &contin
, "continue the current operation"),
151 OPT_BOOLEAN('n', "no-commit", &opts
->no_commit
, "don't automatically commit"),
152 OPT_BOOLEAN('e', "edit", &opts
->edit
, "edit the commit message"),
153 { OPTION_BOOLEAN
, 'r', NULL
, &noop
, NULL
, "no-op (backward compatibility)",
154 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 0 },
155 OPT_BOOLEAN('s', "signoff", &opts
->signoff
, "add Signed-off-by:"),
156 OPT_INTEGER('m', "mainline", &opts
->mainline
, "parent number"),
157 OPT_RERERE_AUTOUPDATE(&opts
->allow_rerere_auto
),
158 OPT_STRING(0, "strategy", &opts
->strategy
, "strategy", "merge strategy"),
159 OPT_CALLBACK('X', "strategy-option", &opts
, "option",
160 "option for merge strategy", option_parse_x
),
166 if (opts
->action
== CHERRY_PICK
) {
167 struct option cp_extra
[] = {
168 OPT_BOOLEAN('x', NULL
, &opts
->record_origin
, "append commit name"),
169 OPT_BOOLEAN(0, "ff", &opts
->allow_ff
, "allow fast-forward"),
172 if (parse_options_concat(options
, ARRAY_SIZE(options
), cp_extra
))
173 die(_("program error"));
176 opts
->commit_argc
= parse_options(argc
, argv
, NULL
, options
, usage_str
,
177 PARSE_OPT_KEEP_ARGV0
|
178 PARSE_OPT_KEEP_UNKNOWN
);
180 /* Check for incompatible subcommands */
181 verify_opt_mutually_compatible(me
,
183 "--continue", contin
,
186 /* Set the subcommand */
188 opts
->subcommand
= REPLAY_RESET
;
190 opts
->subcommand
= REPLAY_CONTINUE
;
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_RESET
)
198 this_operation
= "--reset";
200 this_operation
= "--continue";
202 verify_opt_compatible(me
, this_operation
,
203 "--no-commit", opts
->no_commit
,
204 "--signoff", opts
->signoff
,
205 "--mainline", opts
->mainline
,
206 "--strategy", opts
->strategy
? 1 : 0,
207 "--strategy-option", opts
->xopts
? 1 : 0,
208 "-x", opts
->record_origin
,
209 "--ff", opts
->allow_ff
,
213 else if (opts
->commit_argc
< 2)
214 usage_with_options(usage_str
, options
);
217 verify_opt_compatible(me
, "--ff",
218 "--signoff", opts
->signoff
,
219 "--no-commit", opts
->no_commit
,
220 "-x", opts
->record_origin
,
221 "--edit", opts
->edit
,
223 opts
->commit_argv
= argv
;
226 struct commit_message
{
230 char *reencoded_message
;
234 static int get_message(struct commit
*commit
, struct commit_message
*out
)
236 const char *encoding
;
237 const char *abbrev
, *subject
;
238 int abbrev_len
, subject_len
;
243 encoding
= get_encoding(commit
->buffer
);
246 if (!git_commit_encoding
)
247 git_commit_encoding
= "UTF-8";
249 out
->reencoded_message
= NULL
;
250 out
->message
= commit
->buffer
;
251 if (strcmp(encoding
, git_commit_encoding
))
252 out
->reencoded_message
= reencode_string(commit
->buffer
,
253 git_commit_encoding
, encoding
);
254 if (out
->reencoded_message
)
255 out
->message
= out
->reencoded_message
;
257 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
258 abbrev_len
= strlen(abbrev
);
260 subject_len
= find_commit_subject(out
->message
, &subject
);
262 out
->parent_label
= xmalloc(strlen("parent of ") + abbrev_len
+
263 strlen("... ") + subject_len
+ 1);
264 q
= out
->parent_label
;
265 q
= mempcpy(q
, "parent of ", strlen("parent of "));
267 q
= mempcpy(q
, abbrev
, abbrev_len
);
268 q
= mempcpy(q
, "... ", strlen("... "));
270 q
= mempcpy(q
, subject
, subject_len
);
275 static void free_message(struct commit_message
*msg
)
277 free(msg
->parent_label
);
278 free(msg
->reencoded_message
);
281 static char *get_encoding(const char *message
)
283 const char *p
= message
, *eol
;
285 while (*p
&& *p
!= '\n') {
286 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
288 if (!prefixcmp(p
, "encoding ")) {
289 char *result
= xmalloc(eol
- 8 - p
);
290 strlcpy(result
, p
+ 9, eol
- 8 - p
);
300 static void write_cherry_pick_head(struct commit
*commit
)
303 struct strbuf buf
= STRBUF_INIT
;
305 strbuf_addf(&buf
, "%s\n", sha1_to_hex(commit
->object
.sha1
));
307 fd
= open(git_path("CHERRY_PICK_HEAD"), O_WRONLY
| O_CREAT
, 0666);
309 die_errno(_("Could not open '%s' for writing"),
310 git_path("CHERRY_PICK_HEAD"));
311 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
|| close(fd
))
312 die_errno(_("Could not write to '%s'"), git_path("CHERRY_PICK_HEAD"));
313 strbuf_release(&buf
);
316 static void print_advice(void)
318 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
321 fprintf(stderr
, "%s\n", msg
);
323 * A conflict has occured but the porcelain
324 * (typically rebase --interactive) wants to take care
325 * of the commit itself so remove CHERRY_PICK_HEAD
327 unlink(git_path("CHERRY_PICK_HEAD"));
331 advise("after resolving the conflicts, mark the corrected paths");
332 advise("with 'git add <paths>' or 'git rm <paths>'");
333 advise("and commit the result with 'git commit'");
336 static void write_message(struct strbuf
*msgbuf
, const char *filename
)
338 static struct lock_file msg_file
;
340 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
,
342 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
343 die_errno(_("Could not write to %s."), filename
);
344 strbuf_release(msgbuf
);
345 if (commit_lock_file(&msg_file
) < 0)
346 die(_("Error wrapping up %s"), filename
);
349 static struct tree
*empty_tree(void)
351 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
353 tree
->object
.parsed
= 1;
354 tree
->object
.type
= OBJ_TREE
;
355 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
359 static NORETURN
void die_dirty_index(struct replay_opts
*opts
)
361 if (read_cache_unmerged()) {
362 die_resolve_conflict(action_name(opts
));
364 if (advice_commit_before_merge
) {
365 if (opts
->action
== REVERT
)
366 die(_("Your local changes would be overwritten by revert.\n"
367 "Please, commit your changes or stash them to proceed."));
369 die(_("Your local changes would be overwritten by cherry-pick.\n"
370 "Please, commit your changes or stash them to proceed."));
372 if (opts
->action
== REVERT
)
373 die(_("Your local changes would be overwritten by revert.\n"));
375 die(_("Your local changes would be overwritten by cherry-pick.\n"));
380 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
)
382 struct ref_lock
*ref_lock
;
385 if (checkout_fast_forward(from
, to
))
386 exit(1); /* the callee should have complained already */
387 ref_lock
= lock_any_ref_for_update("HEAD", from
, 0);
388 return write_ref_sha1(ref_lock
, to
, "cherry-pick");
391 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
392 const char *base_label
, const char *next_label
,
393 unsigned char *head
, struct strbuf
*msgbuf
,
394 struct replay_opts
*opts
)
396 struct merge_options o
;
397 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
400 static struct lock_file index_lock
;
402 index_fd
= hold_locked_index(&index_lock
, 1);
406 init_merge_options(&o
);
407 o
.ancestor
= base
? base_label
: "(empty tree)";
409 o
.branch2
= next
? next_label
: "(empty tree)";
411 head_tree
= parse_tree_indirect(head
);
412 next_tree
= next
? next
->tree
: empty_tree();
413 base_tree
= base
? base
->tree
: empty_tree();
415 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
416 parse_merge_opt(&o
, *xopt
);
418 clean
= merge_trees(&o
,
420 next_tree
, base_tree
, &result
);
422 if (active_cache_changed
&&
423 (write_cache(index_fd
, active_cache
, active_nr
) ||
424 commit_locked_index(&index_lock
)))
425 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
426 die(_("%s: Unable to write new index file"), action_name(opts
));
427 rollback_lock_file(&index_lock
);
431 strbuf_addstr(msgbuf
, "\nConflicts:\n\n");
432 for (i
= 0; i
< active_nr
;) {
433 struct cache_entry
*ce
= active_cache
[i
++];
435 strbuf_addch(msgbuf
, '\t');
436 strbuf_addstr(msgbuf
, ce
->name
);
437 strbuf_addch(msgbuf
, '\n');
438 while (i
< active_nr
&& !strcmp(ce
->name
,
439 active_cache
[i
]->name
))
449 * If we are cherry-pick, and if the merge did not result in
450 * hand-editing, we will hit this commit and inherit the original
451 * author date and name.
452 * If we are revert, or if our cherry-pick results in a hand merge,
453 * we had better say that the current user is responsible for that.
455 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
)
457 /* 6 is max possible length of our args array including NULL */
461 args
[i
++] = "commit";
471 return run_command_v_opt(args
, RUN_GIT_CMD
);
474 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
476 unsigned char head
[20];
477 struct commit
*base
, *next
, *parent
;
478 const char *base_label
, *next_label
;
479 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
481 struct strbuf msgbuf
= STRBUF_INIT
;
484 if (opts
->no_commit
) {
486 * We do not intend to commit immediately. We just want to
487 * merge the differences in, so let's compute the tree
488 * that represents the "current" state for merge-recursive
491 if (write_cache_as_tree(head
, 0, NULL
))
492 die (_("Your index file is unmerged."));
494 if (get_sha1("HEAD", head
))
495 die (_("You do not have a valid HEAD"));
496 if (index_differs_from("HEAD", 0))
497 die_dirty_index(opts
);
501 if (!commit
->parents
) {
504 else if (commit
->parents
->next
) {
505 /* Reverting or cherry-picking a merge commit */
507 struct commit_list
*p
;
510 die(_("Commit %s is a merge but no -m option was given."),
511 sha1_to_hex(commit
->object
.sha1
));
513 for (cnt
= 1, p
= commit
->parents
;
514 cnt
!= opts
->mainline
&& p
;
517 if (cnt
!= opts
->mainline
|| !p
)
518 die(_("Commit %s does not have parent %d"),
519 sha1_to_hex(commit
->object
.sha1
), opts
->mainline
);
521 } else if (0 < opts
->mainline
)
522 die(_("Mainline was specified but commit %s is not a merge."),
523 sha1_to_hex(commit
->object
.sha1
));
525 parent
= commit
->parents
->item
;
527 if (opts
->allow_ff
&& parent
&& !hashcmp(parent
->object
.sha1
, head
))
528 return fast_forward_to(commit
->object
.sha1
, head
);
530 if (parent
&& parse_commit(parent
) < 0)
531 /* TRANSLATORS: The first %s will be "revert" or
532 "cherry-pick", the second %s a SHA1 */
533 die(_("%s: cannot parse parent commit %s"),
534 action_name(opts
), sha1_to_hex(parent
->object
.sha1
));
536 if (get_message(commit
, &msg
) != 0)
537 die(_("Cannot get commit message for %s"),
538 sha1_to_hex(commit
->object
.sha1
));
541 * "commit" is an existing commit. We would want to apply
542 * the difference it introduces since its first parent "prev"
543 * on top of the current HEAD if we are cherry-pick. Or the
544 * reverse of it if we are revert.
547 defmsg
= git_pathdup("MERGE_MSG");
549 if (opts
->action
== REVERT
) {
551 base_label
= msg
.label
;
553 next_label
= msg
.parent_label
;
554 strbuf_addstr(&msgbuf
, "Revert \"");
555 strbuf_addstr(&msgbuf
, msg
.subject
);
556 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
557 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
559 if (commit
->parents
&& commit
->parents
->next
) {
560 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
561 strbuf_addstr(&msgbuf
, sha1_to_hex(parent
->object
.sha1
));
563 strbuf_addstr(&msgbuf
, ".\n");
568 base_label
= msg
.parent_label
;
570 next_label
= msg
.label
;
573 * Append the commit log message to msgbuf; it starts
574 * after the tree, parent, author, committer
575 * information followed by "\n\n".
577 p
= strstr(msg
.message
, "\n\n");
580 strbuf_addstr(&msgbuf
, p
);
583 if (opts
->record_origin
) {
584 strbuf_addstr(&msgbuf
, "(cherry picked from commit ");
585 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
586 strbuf_addstr(&msgbuf
, ")\n");
588 if (!opts
->no_commit
)
589 write_cherry_pick_head(commit
);
592 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REVERT
) {
593 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
594 head
, &msgbuf
, opts
);
595 write_message(&msgbuf
, defmsg
);
597 struct commit_list
*common
= NULL
;
598 struct commit_list
*remotes
= NULL
;
600 write_message(&msgbuf
, defmsg
);
602 commit_list_insert(base
, &common
);
603 commit_list_insert(next
, &remotes
);
604 res
= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
605 common
, sha1_to_hex(head
), remotes
);
606 free_commit_list(common
);
607 free_commit_list(remotes
);
611 error(opts
->action
== REVERT
612 ? _("could not revert %s... %s")
613 : _("could not apply %s... %s"),
614 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
),
617 rerere(opts
->allow_rerere_auto
);
619 if (!opts
->no_commit
)
620 res
= run_git_commit(defmsg
, opts
);
629 static void prepare_revs(struct rev_info
*revs
, struct replay_opts
*opts
)
633 init_revisions(revs
, NULL
);
635 if (opts
->action
!= REVERT
)
638 argc
= setup_revisions(opts
->commit_argc
, opts
->commit_argv
, revs
, NULL
);
640 usage(*revert_or_cherry_pick_usage(opts
));
642 if (prepare_revision_walk(revs
))
643 die(_("revision walk setup failed"));
646 die(_("empty commit set passed"));
649 static void read_and_refresh_cache(struct replay_opts
*opts
)
651 static struct lock_file index_lock
;
652 int index_fd
= hold_locked_index(&index_lock
, 0);
653 if (read_index_preload(&the_index
, NULL
) < 0)
654 die(_("git %s: failed to read the index"), action_name(opts
));
655 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
656 if (the_index
.cache_changed
) {
657 if (write_index(&the_index
, index_fd
) ||
658 commit_locked_index(&index_lock
))
659 die(_("git %s: failed to refresh the index"), action_name(opts
));
661 rollback_lock_file(&index_lock
);
665 * Append a commit to the end of the commit_list.
667 * next starts by pointing to the variable that holds the head of an
668 * empty commit_list, and is updated to point to the "next" field of
669 * the last item on the list as new commits are appended.
673 * struct commit_list *list;
674 * struct commit_list **next = &list;
676 * next = commit_list_append(c1, next);
677 * next = commit_list_append(c2, next);
678 * assert(commit_list_count(list) == 2);
681 struct commit_list
**commit_list_append(struct commit
*commit
,
682 struct commit_list
**next
)
684 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
691 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
692 struct replay_opts
*opts
)
694 struct commit_list
*cur
= NULL
;
695 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
696 const char *sha1_abbrev
= NULL
;
697 const char *action_str
= opts
->action
== REVERT
? "revert" : "pick";
699 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
700 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.sha1
, DEFAULT_ABBREV
);
701 if (get_message(cur
->item
, &msg
))
702 return error(_("Cannot get commit message for %s"), sha1_abbrev
);
703 strbuf_addf(buf
, "%s %s %s\n", action_str
, sha1_abbrev
, msg
.subject
);
708 static struct commit
*parse_insn_line(char *start
, struct replay_opts
*opts
)
710 unsigned char commit_sha1
[20];
711 char sha1_abbrev
[40];
712 enum replay_action action
;
716 if (!prefixcmp(start
, "pick ")) {
717 action
= CHERRY_PICK
;
718 insn_len
= strlen("pick");
719 p
= start
+ insn_len
+ 1;
720 } else if (!prefixcmp(start
, "revert ")) {
722 insn_len
= strlen("revert");
723 p
= start
+ insn_len
+ 1;
732 strlcpy(sha1_abbrev
, p
, q
- p
);
735 * Verify that the action matches up with the one in
736 * opts; we don't support arbitrary instructions
738 if (action
!= opts
->action
) {
739 const char *action_str
;
740 action_str
= action
== REVERT
? "revert" : "cherry-pick";
741 error(_("Cannot %s during a %s"), action_str
, action_name(opts
));
745 if (get_sha1(sha1_abbrev
, commit_sha1
) < 0)
748 return lookup_commit_reference(commit_sha1
);
751 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
752 struct replay_opts
*opts
)
754 struct commit_list
**next
= todo_list
;
755 struct commit
*commit
;
759 for (i
= 1; *p
; i
++) {
760 commit
= parse_insn_line(p
, opts
);
762 return error(_("Could not parse line %d."), i
);
763 next
= commit_list_append(commit
, next
);
764 p
= strchrnul(p
, '\n');
769 return error(_("No commits parsed."));
773 static void read_populate_todo(struct commit_list
**todo_list
,
774 struct replay_opts
*opts
)
776 const char *todo_file
= git_path(SEQ_TODO_FILE
);
777 struct strbuf buf
= STRBUF_INIT
;
780 fd
= open(todo_file
, O_RDONLY
);
782 die_errno(_("Could not open %s."), todo_file
);
783 if (strbuf_read(&buf
, fd
, 0) < 0) {
785 strbuf_release(&buf
);
786 die(_("Could not read %s."), todo_file
);
790 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
791 strbuf_release(&buf
);
793 die(_("Unusable instruction sheet: %s"), todo_file
);
796 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
798 struct replay_opts
*opts
= data
;
803 else if (!strcmp(key
, "options.no-commit"))
804 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
805 else if (!strcmp(key
, "options.edit"))
806 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
807 else if (!strcmp(key
, "options.signoff"))
808 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
809 else if (!strcmp(key
, "options.record-origin"))
810 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
811 else if (!strcmp(key
, "options.allow-ff"))
812 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
813 else if (!strcmp(key
, "options.mainline"))
814 opts
->mainline
= git_config_int(key
, value
);
815 else if (!strcmp(key
, "options.strategy"))
816 git_config_string(&opts
->strategy
, key
, value
);
817 else if (!strcmp(key
, "options.strategy-option")) {
818 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
819 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
821 return error(_("Invalid key: %s"), key
);
824 return error(_("Invalid value for %s: %s"), key
, value
);
829 static void read_populate_opts(struct replay_opts
**opts_ptr
)
831 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
833 if (!file_exists(opts_file
))
835 if (git_config_from_file(populate_opts_cb
, opts_file
, *opts_ptr
) < 0)
836 die(_("Malformed options sheet: %s"), opts_file
);
839 static void walk_revs_populate_todo(struct commit_list
**todo_list
,
840 struct replay_opts
*opts
)
842 struct rev_info revs
;
843 struct commit
*commit
;
844 struct commit_list
**next
;
846 prepare_revs(&revs
, opts
);
849 while ((commit
= get_revision(&revs
)))
850 next
= commit_list_append(commit
, next
);
853 static int create_seq_dir(void)
855 const char *seq_dir
= git_path(SEQ_DIR
);
857 if (file_exists(seq_dir
))
858 return error(_("%s already exists."), seq_dir
);
859 else if (mkdir(seq_dir
, 0777) < 0)
860 die_errno(_("Could not create sequencer directory '%s'."), seq_dir
);
864 static void save_head(const char *head
)
866 const char *head_file
= git_path(SEQ_HEAD_FILE
);
867 static struct lock_file head_lock
;
868 struct strbuf buf
= STRBUF_INIT
;
871 fd
= hold_lock_file_for_update(&head_lock
, head_file
, LOCK_DIE_ON_ERROR
);
872 strbuf_addf(&buf
, "%s\n", head
);
873 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0)
874 die_errno(_("Could not write to %s."), head_file
);
875 if (commit_lock_file(&head_lock
) < 0)
876 die(_("Error wrapping up %s."), head_file
);
879 static void save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
881 const char *todo_file
= git_path(SEQ_TODO_FILE
);
882 static struct lock_file todo_lock
;
883 struct strbuf buf
= STRBUF_INIT
;
886 fd
= hold_lock_file_for_update(&todo_lock
, todo_file
, LOCK_DIE_ON_ERROR
);
887 if (format_todo(&buf
, todo_list
, opts
) < 0)
888 die(_("Could not format %s."), todo_file
);
889 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
890 strbuf_release(&buf
);
891 die_errno(_("Could not write to %s."), todo_file
);
893 if (commit_lock_file(&todo_lock
) < 0) {
894 strbuf_release(&buf
);
895 die(_("Error wrapping up %s."), todo_file
);
897 strbuf_release(&buf
);
900 static void save_opts(struct replay_opts
*opts
)
902 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
905 git_config_set_in_file(opts_file
, "options.no-commit", "true");
907 git_config_set_in_file(opts_file
, "options.edit", "true");
909 git_config_set_in_file(opts_file
, "options.signoff", "true");
910 if (opts
->record_origin
)
911 git_config_set_in_file(opts_file
, "options.record-origin", "true");
913 git_config_set_in_file(opts_file
, "options.allow-ff", "true");
914 if (opts
->mainline
) {
915 struct strbuf buf
= STRBUF_INIT
;
916 strbuf_addf(&buf
, "%d", opts
->mainline
);
917 git_config_set_in_file(opts_file
, "options.mainline", buf
.buf
);
918 strbuf_release(&buf
);
921 git_config_set_in_file(opts_file
, "options.strategy", opts
->strategy
);
924 for (i
= 0; i
< opts
->xopts_nr
; i
++)
925 git_config_set_multivar_in_file(opts_file
,
926 "options.strategy-option",
927 opts
->xopts
[i
], "^$", 0);
931 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
933 struct commit_list
*cur
;
936 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
938 assert(!(opts
->signoff
|| opts
->no_commit
||
939 opts
->record_origin
|| opts
->edit
));
940 read_and_refresh_cache(opts
);
942 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
943 save_todo(cur
, opts
);
944 res
= do_pick_commit(cur
->item
, opts
);
948 * An error was encountered while
949 * picking the last commit; the
950 * sequencer state is useless now --
951 * the user simply needs to resolve
952 * the conflict and commit
954 remove_sequencer_state(0);
960 * Sequence of picks finished successfully; cleanup by
961 * removing the .git/sequencer directory
963 remove_sequencer_state(1);
967 static int pick_revisions(struct replay_opts
*opts
)
969 struct commit_list
*todo_list
= NULL
;
970 unsigned char sha1
[20];
972 read_and_refresh_cache(opts
);
975 * Decide what to do depending on the arguments; a fresh
976 * cherry-pick should be handled differently from an existing
977 * one that is being continued
979 if (opts
->subcommand
== REPLAY_RESET
) {
980 remove_sequencer_state(1);
982 } else if (opts
->subcommand
== REPLAY_CONTINUE
) {
983 if (!file_exists(git_path(SEQ_TODO_FILE
)))
985 read_populate_opts(&opts
);
986 read_populate_todo(&todo_list
, opts
);
988 /* Verify that the conflict has been resolved */
989 if (!index_differs_from("HEAD", 0))
990 todo_list
= todo_list
->next
;
993 * Start a new cherry-pick/ revert sequence; but
994 * first, make sure that an existing one isn't in
998 walk_revs_populate_todo(&todo_list
, opts
);
999 if (create_seq_dir() < 0) {
1000 fatal(_("A cherry-pick or revert is in progress."));
1001 advise(_("Use --continue to continue the operation"));
1002 advise(_("or --reset to forget about it"));
1005 if (get_sha1("HEAD", sha1
)) {
1006 if (opts
->action
== REVERT
)
1007 die(_("Can't revert as initial commit"));
1008 die(_("Can't cherry-pick into empty head"));
1010 save_head(sha1_to_hex(sha1
));
1013 return pick_commits(todo_list
, opts
);
1015 die(_("No %s in progress"), action_name(opts
));
1018 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
1020 struct replay_opts opts
;
1022 memset(&opts
, 0, sizeof(opts
));
1025 opts
.action
= REVERT
;
1026 git_config(git_default_config
, NULL
);
1027 parse_args(argc
, argv
, &opts
);
1028 return pick_revisions(&opts
);
1031 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
1033 struct replay_opts opts
;
1035 memset(&opts
, 0, sizeof(opts
));
1036 opts
.action
= CHERRY_PICK
;
1037 git_config(git_default_config
, NULL
);
1038 parse_args(argc
, argv
, &opts
);
1039 return pick_revisions(&opts
);