8 #include "run-command.h"
11 #include "cache-tree.h"
15 #include "merge-recursive.h"
17 #include "argv-array.h"
21 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
23 const char sign_off_header
[] = "Signed-off-by: ";
24 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
26 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
28 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
29 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
30 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
33 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
34 * GIT_AUTHOR_DATE that will be used for the commit that is currently
37 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
39 * The following files are written by git-rebase just after parsing the
40 * command-line (and are only consumed, not modified, by the sequencer).
42 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
44 /* We will introduce the 'interactive rebase' mode later */
45 static inline int is_rebase_i(const struct replay_opts
*opts
)
50 static const char *get_dir(const struct replay_opts
*opts
)
52 return git_path_seq_dir();
55 static const char *get_todo_path(const struct replay_opts
*opts
)
57 return git_path_todo_file();
61 * Returns 0 for non-conforming footer
62 * Returns 1 for conforming footer
63 * Returns 2 when sob exists within conforming footer
64 * Returns 3 when sob exists within conforming footer as last entry
66 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
69 struct trailer_info info
;
71 int found_sob
= 0, found_sob_last
= 0;
73 trailer_info_get(&info
, sb
->buf
);
75 if (info
.trailer_start
== info
.trailer_end
)
78 for (i
= 0; i
< info
.trailer_nr
; i
++)
79 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
81 if (i
== info
.trailer_nr
- 1)
85 trailer_info_release(&info
);
94 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
96 static struct strbuf buf
= STRBUF_INIT
;
100 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
104 int sequencer_remove_state(struct replay_opts
*opts
)
106 struct strbuf dir
= STRBUF_INIT
;
109 free(opts
->gpg_sign
);
110 free(opts
->strategy
);
111 for (i
= 0; i
< opts
->xopts_nr
; i
++)
112 free(opts
->xopts
[i
]);
115 strbuf_addf(&dir
, "%s", get_dir(opts
));
116 remove_dir_recursively(&dir
, 0);
117 strbuf_release(&dir
);
122 static const char *action_name(const struct replay_opts
*opts
)
124 return opts
->action
== REPLAY_REVERT
? N_("revert") : N_("cherry-pick");
127 struct commit_message
{
134 static const char *short_commit_name(struct commit
*commit
)
136 return find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
139 static int get_message(struct commit
*commit
, struct commit_message
*out
)
141 const char *abbrev
, *subject
;
144 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
145 abbrev
= short_commit_name(commit
);
147 subject_len
= find_commit_subject(out
->message
, &subject
);
149 out
->subject
= xmemdupz(subject
, subject_len
);
150 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
151 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
156 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
158 free(msg
->parent_label
);
161 unuse_commit_buffer(commit
, msg
->message
);
164 static void print_advice(int show_hint
, struct replay_opts
*opts
)
166 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
169 fprintf(stderr
, "%s\n", msg
);
171 * A conflict has occurred but the porcelain
172 * (typically rebase --interactive) wants to take care
173 * of the commit itself so remove CHERRY_PICK_HEAD
175 unlink(git_path_cherry_pick_head());
181 advise(_("after resolving the conflicts, mark the corrected paths\n"
182 "with 'git add <paths>' or 'git rm <paths>'"));
184 advise(_("after resolving the conflicts, mark the corrected paths\n"
185 "with 'git add <paths>' or 'git rm <paths>'\n"
186 "and commit the result with 'git commit'"));
190 static int write_message(const void *buf
, size_t len
, const char *filename
,
193 static struct lock_file msg_file
;
195 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
197 return error_errno(_("could not lock '%s'"), filename
);
198 if (write_in_full(msg_fd
, buf
, len
) < 0) {
199 rollback_lock_file(&msg_file
);
200 return error_errno(_("could not write to '%s'"), filename
);
202 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
203 rollback_lock_file(&msg_file
);
204 return error_errno(_("could not write eol to '%s'"), filename
);
206 if (commit_lock_file(&msg_file
) < 0) {
207 rollback_lock_file(&msg_file
);
208 return error(_("failed to finalize '%s'."), filename
);
215 * Reads a file that was presumably written by a shell script, i.e. with an
216 * end-of-line marker that needs to be stripped.
218 * Note that only the last end-of-line marker is stripped, consistent with the
219 * behavior of "$(cat path)" in a shell script.
221 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
223 static int read_oneliner(struct strbuf
*buf
,
224 const char *path
, int skip_if_empty
)
226 int orig_len
= buf
->len
;
228 if (!file_exists(path
))
231 if (strbuf_read_file(buf
, path
, 0) < 0) {
232 warning_errno(_("could not read '%s'"), path
);
236 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
237 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
239 buf
->buf
[buf
->len
] = '\0';
242 if (skip_if_empty
&& buf
->len
== orig_len
)
248 static struct tree
*empty_tree(void)
250 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
253 static int error_dirty_index(struct replay_opts
*opts
)
255 if (read_cache_unmerged())
256 return error_resolve_conflict(_(action_name(opts
)));
258 error(_("your local changes would be overwritten by %s."),
259 _(action_name(opts
)));
261 if (advice_commit_before_merge
)
262 advise(_("commit your changes or stash them to proceed."));
266 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
267 int unborn
, struct replay_opts
*opts
)
269 struct ref_transaction
*transaction
;
270 struct strbuf sb
= STRBUF_INIT
;
271 struct strbuf err
= STRBUF_INIT
;
274 if (checkout_fast_forward(from
, to
, 1))
275 return -1; /* the callee should have complained already */
277 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
279 transaction
= ref_transaction_begin(&err
);
281 ref_transaction_update(transaction
, "HEAD",
282 to
, unborn
? null_sha1
: from
,
284 ref_transaction_commit(transaction
, &err
)) {
285 ref_transaction_free(transaction
);
286 error("%s", err
.buf
);
288 strbuf_release(&err
);
293 strbuf_release(&err
);
294 ref_transaction_free(transaction
);
298 void append_conflicts_hint(struct strbuf
*msgbuf
)
302 strbuf_addch(msgbuf
, '\n');
303 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
304 for (i
= 0; i
< active_nr
;) {
305 const struct cache_entry
*ce
= active_cache
[i
++];
307 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
308 while (i
< active_nr
&& !strcmp(ce
->name
,
309 active_cache
[i
]->name
))
315 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
316 const char *base_label
, const char *next_label
,
317 unsigned char *head
, struct strbuf
*msgbuf
,
318 struct replay_opts
*opts
)
320 struct merge_options o
;
321 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
324 static struct lock_file index_lock
;
326 hold_locked_index(&index_lock
, 1);
330 init_merge_options(&o
);
331 o
.ancestor
= base
? base_label
: "(empty tree)";
333 o
.branch2
= next
? next_label
: "(empty tree)";
335 head_tree
= parse_tree_indirect(head
);
336 next_tree
= next
? next
->tree
: empty_tree();
337 base_tree
= base
? base
->tree
: empty_tree();
339 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
340 parse_merge_opt(&o
, *xopt
);
342 clean
= merge_trees(&o
,
344 next_tree
, base_tree
, &result
);
345 strbuf_release(&o
.obuf
);
349 if (active_cache_changed
&&
350 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
351 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
352 return error(_("%s: Unable to write new index file"),
353 _(action_name(opts
)));
354 rollback_lock_file(&index_lock
);
357 append_signoff(msgbuf
, 0, 0);
360 append_conflicts_hint(msgbuf
);
365 static int is_index_unchanged(void)
367 unsigned char head_sha1
[20];
368 struct commit
*head_commit
;
370 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
371 return error(_("could not resolve HEAD commit\n"));
373 head_commit
= lookup_commit(head_sha1
);
376 * If head_commit is NULL, check_commit, called from
377 * lookup_commit, would have indicated that head_commit is not
378 * a commit object already. parse_commit() will return failure
379 * without further complaints in such a case. Otherwise, if
380 * the commit is invalid, parse_commit() will complain. So
381 * there is nothing for us to say here. Just return failure.
383 if (parse_commit(head_commit
))
386 if (!active_cache_tree
)
387 active_cache_tree
= cache_tree();
389 if (!cache_tree_fully_valid(active_cache_tree
))
390 if (cache_tree_update(&the_index
, 0))
391 return error(_("unable to update cache tree\n"));
393 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
397 * Read the author-script file into an environment block, ready for use in
398 * run_command(), that can be free()d afterwards.
400 static char **read_author_script(void)
402 struct strbuf script
= STRBUF_INIT
;
407 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
410 for (p
= script
.buf
; *p
; p
++)
411 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
412 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
414 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
415 else if (*p
== '\n') {
420 env_size
= (count
+ 1) * sizeof(*env
);
421 strbuf_grow(&script
, env_size
);
422 memmove(script
.buf
+ env_size
, script
.buf
, script
.len
);
423 p
= script
.buf
+ env_size
;
424 env
= (char **)strbuf_detach(&script
, NULL
);
426 for (i
= 0; i
< count
; i
++) {
435 static const char staged_changes_advice
[] =
436 N_("you have staged changes in your working tree\n"
437 "If these changes are meant to be squashed into the previous commit, run:\n"
439 " git commit --amend %s\n"
441 "If they are meant to go into a new commit, run:\n"
445 "In both cases, once you're done, continue with:\n"
447 " git rebase --continue\n");
450 * If we are cherry-pick, and if the merge did not result in
451 * hand-editing, we will hit this commit and inherit the original
452 * author date and name.
454 * If we are revert, or if our cherry-pick results in a hand merge,
455 * we had better say that the current user is responsible for that.
457 * An exception is when run_git_commit() is called during an
458 * interactive rebase: in that case, we will want to retain the
461 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
462 int allow_empty
, int edit
, int amend
,
463 int cleanup_commit_message
)
466 struct argv_array array
;
470 if (is_rebase_i(opts
)) {
471 env
= read_author_script();
473 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
475 return error(_(staged_changes_advice
),
480 argv_array_init(&array
);
481 argv_array_push(&array
, "commit");
482 argv_array_push(&array
, "-n");
485 argv_array_push(&array
, "--amend");
487 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
489 argv_array_push(&array
, "-s");
491 argv_array_pushl(&array
, "-F", defmsg
, NULL
);
492 if (cleanup_commit_message
)
493 argv_array_push(&array
, "--cleanup=strip");
495 argv_array_push(&array
, "-e");
496 else if (!cleanup_commit_message
&&
497 !opts
->signoff
&& !opts
->record_origin
&&
498 git_config_get_value("commit.cleanup", &value
))
499 argv_array_push(&array
, "--cleanup=verbatim");
502 argv_array_push(&array
, "--allow-empty");
504 if (opts
->allow_empty_message
)
505 argv_array_push(&array
, "--allow-empty-message");
507 rc
= run_command_v_opt_cd_env(array
.argv
, RUN_GIT_CMD
, NULL
,
508 (const char *const *)env
);
509 argv_array_clear(&array
);
515 static int is_original_commit_empty(struct commit
*commit
)
517 const unsigned char *ptree_sha1
;
519 if (parse_commit(commit
))
520 return error(_("could not parse commit %s\n"),
521 oid_to_hex(&commit
->object
.oid
));
522 if (commit
->parents
) {
523 struct commit
*parent
= commit
->parents
->item
;
524 if (parse_commit(parent
))
525 return error(_("could not parse parent commit %s\n"),
526 oid_to_hex(&parent
->object
.oid
));
527 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
529 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
532 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
536 * Do we run "git commit" with "--allow-empty"?
538 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
540 int index_unchanged
, empty_commit
;
545 * (1) we do not allow empty at all and error out.
547 * (2) we allow ones that were initially empty, but
548 * forbid the ones that become empty;
552 if (!opts
->allow_empty
)
553 return 0; /* let "git commit" barf as necessary */
555 index_unchanged
= is_index_unchanged();
556 if (index_unchanged
< 0)
557 return index_unchanged
;
558 if (!index_unchanged
)
559 return 0; /* we do not have to say --allow-empty */
561 if (opts
->keep_redundant_commits
)
564 empty_commit
= is_original_commit_empty(commit
);
565 if (empty_commit
< 0)
578 static const char *todo_command_strings
[] = {
583 static const char *command_to_string(const enum todo_command command
)
585 if ((size_t)command
< ARRAY_SIZE(todo_command_strings
))
586 return todo_command_strings
[command
];
587 die("Unknown command: %d", command
);
591 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
592 struct replay_opts
*opts
)
594 unsigned char head
[20];
595 struct commit
*base
, *next
, *parent
;
596 const char *base_label
, *next_label
;
597 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
598 struct strbuf msgbuf
= STRBUF_INIT
;
599 int res
, unborn
= 0, allow
;
601 if (opts
->no_commit
) {
603 * We do not intend to commit immediately. We just want to
604 * merge the differences in, so let's compute the tree
605 * that represents the "current" state for merge-recursive
608 if (write_cache_as_tree(head
, 0, NULL
))
609 return error(_("your index file is unmerged."));
611 unborn
= get_sha1("HEAD", head
);
613 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
614 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0, 0))
615 return error_dirty_index(opts
);
619 if (!commit
->parents
) {
622 else if (commit
->parents
->next
) {
623 /* Reverting or cherry-picking a merge commit */
625 struct commit_list
*p
;
628 return error(_("commit %s is a merge but no -m option was given."),
629 oid_to_hex(&commit
->object
.oid
));
631 for (cnt
= 1, p
= commit
->parents
;
632 cnt
!= opts
->mainline
&& p
;
635 if (cnt
!= opts
->mainline
|| !p
)
636 return error(_("commit %s does not have parent %d"),
637 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
639 } else if (0 < opts
->mainline
)
640 return error(_("mainline was specified but commit %s is not a merge."),
641 oid_to_hex(&commit
->object
.oid
));
643 parent
= commit
->parents
->item
;
645 if (opts
->allow_ff
&&
646 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
647 (!parent
&& unborn
)))
648 return fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
, opts
);
650 if (parent
&& parse_commit(parent
) < 0)
651 /* TRANSLATORS: The first %s will be a "todo" command like
652 "revert" or "pick", the second %s a SHA1. */
653 return error(_("%s: cannot parse parent commit %s"),
654 command_to_string(command
),
655 oid_to_hex(&parent
->object
.oid
));
657 if (get_message(commit
, &msg
) != 0)
658 return error(_("cannot get commit message for %s"),
659 oid_to_hex(&commit
->object
.oid
));
662 * "commit" is an existing commit. We would want to apply
663 * the difference it introduces since its first parent "prev"
664 * on top of the current HEAD if we are cherry-pick. Or the
665 * reverse of it if we are revert.
668 if (command
== TODO_REVERT
) {
670 base_label
= msg
.label
;
672 next_label
= msg
.parent_label
;
673 strbuf_addstr(&msgbuf
, "Revert \"");
674 strbuf_addstr(&msgbuf
, msg
.subject
);
675 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
676 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
678 if (commit
->parents
&& commit
->parents
->next
) {
679 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
680 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
682 strbuf_addstr(&msgbuf
, ".\n");
687 base_label
= msg
.parent_label
;
689 next_label
= msg
.label
;
692 * Append the commit log message to msgbuf; it starts
693 * after the tree, parent, author, committer
694 * information followed by "\n\n".
696 p
= strstr(msg
.message
, "\n\n");
698 strbuf_addstr(&msgbuf
, skip_blank_lines(p
+ 2));
700 if (opts
->record_origin
) {
701 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
702 strbuf_addch(&msgbuf
, '\n');
703 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
704 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
705 strbuf_addstr(&msgbuf
, ")\n");
709 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
710 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
711 head
, &msgbuf
, opts
);
714 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
715 git_path_merge_msg(), 0);
717 struct commit_list
*common
= NULL
;
718 struct commit_list
*remotes
= NULL
;
720 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
721 git_path_merge_msg(), 0);
723 commit_list_insert(base
, &common
);
724 commit_list_insert(next
, &remotes
);
725 res
|= try_merge_command(opts
->strategy
,
726 opts
->xopts_nr
, (const char **)opts
->xopts
,
727 common
, sha1_to_hex(head
), remotes
);
728 free_commit_list(common
);
729 free_commit_list(remotes
);
731 strbuf_release(&msgbuf
);
734 * If the merge was clean or if it failed due to conflict, we write
735 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
736 * However, if the merge did not even start, then we don't want to
739 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
740 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
741 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
743 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
744 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
745 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
749 error(command
== TODO_REVERT
750 ? _("could not revert %s... %s")
751 : _("could not apply %s... %s"),
752 short_commit_name(commit
), msg
.subject
);
753 print_advice(res
== 1, opts
);
754 rerere(opts
->allow_rerere_auto
);
758 allow
= allow_empty(opts
, commit
);
763 if (!opts
->no_commit
)
764 res
= run_git_commit(opts
->edit
? NULL
: git_path_merge_msg(),
765 opts
, allow
, opts
->edit
, 0, 0);
768 free_message(commit
, &msg
);
773 static int prepare_revs(struct replay_opts
*opts
)
776 * picking (but not reverting) ranges (but not individual revisions)
777 * should be done in reverse
779 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
780 opts
->revs
->reverse
^= 1;
782 if (prepare_revision_walk(opts
->revs
))
783 return error(_("revision walk setup failed"));
785 if (!opts
->revs
->commits
)
786 return error(_("empty commit set passed"));
790 static int read_and_refresh_cache(struct replay_opts
*opts
)
792 static struct lock_file index_lock
;
793 int index_fd
= hold_locked_index(&index_lock
, 0);
794 if (read_index_preload(&the_index
, NULL
) < 0) {
795 rollback_lock_file(&index_lock
);
796 return error(_("git %s: failed to read the index"),
797 _(action_name(opts
)));
799 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
800 if (the_index
.cache_changed
&& index_fd
>= 0) {
801 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
802 rollback_lock_file(&index_lock
);
803 return error(_("git %s: failed to refresh the index"),
804 _(action_name(opts
)));
807 rollback_lock_file(&index_lock
);
812 enum todo_command command
;
813 struct commit
*commit
;
816 size_t offset_in_buf
;
821 struct todo_item
*items
;
822 int nr
, alloc
, current
;
825 #define TODO_LIST_INIT { STRBUF_INIT }
827 static void todo_list_release(struct todo_list
*todo_list
)
829 strbuf_release(&todo_list
->buf
);
830 free(todo_list
->items
);
831 todo_list
->items
= NULL
;
832 todo_list
->nr
= todo_list
->alloc
= 0;
835 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
837 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
838 return todo_list
->items
+ todo_list
->nr
++;
841 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
843 unsigned char commit_sha1
[20];
844 char *end_of_object_name
;
845 int i
, saved
, status
, padding
;
848 bol
+= strspn(bol
, " \t");
850 for (i
= 0; i
< ARRAY_SIZE(todo_command_strings
); i
++)
851 if (skip_prefix(bol
, todo_command_strings
[i
], &bol
)) {
855 if (i
>= ARRAY_SIZE(todo_command_strings
))
858 /* Eat up extra spaces/ tabs before object name */
859 padding
= strspn(bol
, " \t");
864 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
865 saved
= *end_of_object_name
;
866 *end_of_object_name
= '\0';
867 status
= get_sha1(bol
, commit_sha1
);
868 *end_of_object_name
= saved
;
870 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
871 item
->arg_len
= (int)(eol
- item
->arg
);
876 item
->commit
= lookup_commit_reference(commit_sha1
);
877 return !item
->commit
;
880 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
882 struct todo_item
*item
;
883 char *p
= buf
, *next_p
;
886 for (i
= 1; *p
; i
++, p
= next_p
) {
887 char *eol
= strchrnul(p
, '\n');
889 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
891 if (p
!= eol
&& eol
[-1] == '\r')
892 eol
--; /* strip Carriage Return */
894 item
= append_new_todo(todo_list
);
895 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
896 if (parse_insn_line(item
, p
, eol
)) {
897 res
= error(_("invalid line %d: %.*s"),
898 i
, (int)(eol
- p
), p
);
903 return error(_("no commits parsed."));
907 static int read_populate_todo(struct todo_list
*todo_list
,
908 struct replay_opts
*opts
)
910 const char *todo_file
= get_todo_path(opts
);
913 strbuf_reset(&todo_list
->buf
);
914 fd
= open(todo_file
, O_RDONLY
);
916 return error_errno(_("could not open '%s'"), todo_file
);
917 if (strbuf_read(&todo_list
->buf
, fd
, 0) < 0) {
919 return error(_("could not read '%s'."), todo_file
);
923 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
925 return error(_("unusable instruction sheet: '%s'"), todo_file
);
927 if (!is_rebase_i(opts
)) {
928 enum todo_command valid
=
929 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
932 for (i
= 0; i
< todo_list
->nr
; i
++)
933 if (valid
== todo_list
->items
[i
].command
)
935 else if (valid
== TODO_PICK
)
936 return error(_("cannot cherry-pick during a revert."));
938 return error(_("cannot revert during a cherry-pick."));
944 static int git_config_string_dup(char **dest
,
945 const char *var
, const char *value
)
948 return config_error_nonbool(var
);
950 *dest
= xstrdup(value
);
954 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
956 struct replay_opts
*opts
= data
;
961 else if (!strcmp(key
, "options.no-commit"))
962 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
963 else if (!strcmp(key
, "options.edit"))
964 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
965 else if (!strcmp(key
, "options.signoff"))
966 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
967 else if (!strcmp(key
, "options.record-origin"))
968 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
969 else if (!strcmp(key
, "options.allow-ff"))
970 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
971 else if (!strcmp(key
, "options.mainline"))
972 opts
->mainline
= git_config_int(key
, value
);
973 else if (!strcmp(key
, "options.strategy"))
974 git_config_string_dup(&opts
->strategy
, key
, value
);
975 else if (!strcmp(key
, "options.gpg-sign"))
976 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
977 else if (!strcmp(key
, "options.strategy-option")) {
978 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
979 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
981 return error(_("invalid key: %s"), key
);
984 return error(_("invalid value for %s: %s"), key
, value
);
989 static int read_populate_opts(struct replay_opts
*opts
)
991 if (is_rebase_i(opts
)) {
992 struct strbuf buf
= STRBUF_INIT
;
994 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
995 if (!starts_with(buf
.buf
, "-S"))
998 free(opts
->gpg_sign
);
999 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
1002 strbuf_release(&buf
);
1007 if (!file_exists(git_path_opts_file()))
1010 * The function git_parse_source(), called from git_config_from_file(),
1011 * may die() in case of a syntactically incorrect file. We do not care
1012 * about this case, though, because we wrote that file ourselves, so we
1013 * are pretty certain that it is syntactically correct.
1015 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
1016 return error(_("malformed options sheet: '%s'"),
1017 git_path_opts_file());
1021 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
1022 struct replay_opts
*opts
)
1024 enum todo_command command
= opts
->action
== REPLAY_PICK
?
1025 TODO_PICK
: TODO_REVERT
;
1026 const char *command_string
= todo_command_strings
[command
];
1027 struct commit
*commit
;
1029 if (prepare_revs(opts
))
1032 while ((commit
= get_revision(opts
->revs
))) {
1033 struct todo_item
*item
= append_new_todo(todo_list
);
1034 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1035 const char *subject
;
1038 item
->command
= command
;
1039 item
->commit
= commit
;
1042 item
->offset_in_buf
= todo_list
->buf
.len
;
1043 subject_len
= find_commit_subject(commit_buffer
, &subject
);
1044 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
1045 short_commit_name(commit
), subject_len
, subject
);
1046 unuse_commit_buffer(commit
, commit_buffer
);
1051 static int create_seq_dir(void)
1053 if (file_exists(git_path_seq_dir())) {
1054 error(_("a cherry-pick or revert is already in progress"));
1055 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
1058 else if (mkdir(git_path_seq_dir(), 0777) < 0)
1059 return error_errno(_("could not create sequencer directory '%s'"),
1060 git_path_seq_dir());
1064 static int save_head(const char *head
)
1066 static struct lock_file head_lock
;
1067 struct strbuf buf
= STRBUF_INIT
;
1070 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
1072 rollback_lock_file(&head_lock
);
1073 return error_errno(_("could not lock HEAD"));
1075 strbuf_addf(&buf
, "%s\n", head
);
1076 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
1077 rollback_lock_file(&head_lock
);
1078 return error_errno(_("could not write to '%s'"),
1079 git_path_head_file());
1081 if (commit_lock_file(&head_lock
) < 0) {
1082 rollback_lock_file(&head_lock
);
1083 return error(_("failed to finalize '%s'."), git_path_head_file());
1088 static int reset_for_rollback(const unsigned char *sha1
)
1090 const char *argv
[4]; /* reset --merge <arg> + NULL */
1092 argv
[1] = "--merge";
1093 argv
[2] = sha1_to_hex(sha1
);
1095 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1098 static int rollback_single_pick(void)
1100 unsigned char head_sha1
[20];
1102 if (!file_exists(git_path_cherry_pick_head()) &&
1103 !file_exists(git_path_revert_head()))
1104 return error(_("no cherry-pick or revert in progress"));
1105 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
1106 return error(_("cannot resolve HEAD"));
1107 if (is_null_sha1(head_sha1
))
1108 return error(_("cannot abort from a branch yet to be born"));
1109 return reset_for_rollback(head_sha1
);
1112 int sequencer_rollback(struct replay_opts
*opts
)
1115 unsigned char sha1
[20];
1116 struct strbuf buf
= STRBUF_INIT
;
1118 f
= fopen(git_path_head_file(), "r");
1119 if (!f
&& errno
== ENOENT
) {
1121 * There is no multiple-cherry-pick in progress.
1122 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
1123 * a single-cherry-pick in progress, abort that.
1125 return rollback_single_pick();
1128 return error_errno(_("cannot open '%s'"), git_path_head_file());
1129 if (strbuf_getline_lf(&buf
, f
)) {
1130 error(_("cannot read '%s': %s"), git_path_head_file(),
1131 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
1136 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
1137 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
1138 git_path_head_file());
1141 if (is_null_sha1(sha1
)) {
1142 error(_("cannot abort from a branch yet to be born"));
1145 if (reset_for_rollback(sha1
))
1147 strbuf_release(&buf
);
1148 return sequencer_remove_state(opts
);
1150 strbuf_release(&buf
);
1154 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1156 static struct lock_file todo_lock
;
1157 const char *todo_path
= get_todo_path(opts
);
1158 int next
= todo_list
->current
, offset
, fd
;
1160 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
1162 return error_errno(_("could not lock '%s'"), todo_path
);
1163 offset
= next
< todo_list
->nr
?
1164 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
1165 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
1166 todo_list
->buf
.len
- offset
) < 0)
1167 return error_errno(_("could not write to '%s'"), todo_path
);
1168 if (commit_lock_file(&todo_lock
) < 0)
1169 return error(_("failed to finalize '%s'."), todo_path
);
1173 static int save_opts(struct replay_opts
*opts
)
1175 const char *opts_file
= git_path_opts_file();
1178 if (opts
->no_commit
)
1179 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
1181 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
1183 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
1184 if (opts
->record_origin
)
1185 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
1187 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
1188 if (opts
->mainline
) {
1189 struct strbuf buf
= STRBUF_INIT
;
1190 strbuf_addf(&buf
, "%d", opts
->mainline
);
1191 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
1192 strbuf_release(&buf
);
1195 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
1197 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
1200 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1201 res
|= git_config_set_multivar_in_file_gently(opts_file
,
1202 "options.strategy-option",
1203 opts
->xopts
[i
], "^$", 0);
1208 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1212 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1214 assert(!(opts
->signoff
|| opts
->no_commit
||
1215 opts
->record_origin
|| opts
->edit
));
1216 if (read_and_refresh_cache(opts
))
1219 while (todo_list
->current
< todo_list
->nr
) {
1220 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
1221 if (save_todo(todo_list
, opts
))
1223 res
= do_pick_commit(item
->command
, item
->commit
, opts
);
1224 todo_list
->current
++;
1230 * Sequence of picks finished successfully; cleanup by
1231 * removing the .git/sequencer directory
1233 return sequencer_remove_state(opts
);
1236 static int continue_single_pick(void)
1238 const char *argv
[] = { "commit", NULL
};
1240 if (!file_exists(git_path_cherry_pick_head()) &&
1241 !file_exists(git_path_revert_head()))
1242 return error(_("no cherry-pick or revert in progress"));
1243 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1246 int sequencer_continue(struct replay_opts
*opts
)
1248 struct todo_list todo_list
= TODO_LIST_INIT
;
1251 if (read_and_refresh_cache(opts
))
1254 if (!file_exists(get_todo_path(opts
)))
1255 return continue_single_pick();
1256 if (read_populate_opts(opts
))
1258 if ((res
= read_populate_todo(&todo_list
, opts
)))
1259 goto release_todo_list
;
1261 /* Verify that the conflict has been resolved */
1262 if (file_exists(git_path_cherry_pick_head()) ||
1263 file_exists(git_path_revert_head())) {
1264 res
= continue_single_pick();
1266 goto release_todo_list
;
1268 if (index_differs_from("HEAD", 0, 0)) {
1269 res
= error_dirty_index(opts
);
1270 goto release_todo_list
;
1272 todo_list
.current
++;
1273 res
= pick_commits(&todo_list
, opts
);
1275 todo_list_release(&todo_list
);
1279 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1281 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1282 return do_pick_commit(opts
->action
== REPLAY_PICK
?
1283 TODO_PICK
: TODO_REVERT
, cmit
, opts
);
1286 int sequencer_pick_revisions(struct replay_opts
*opts
)
1288 struct todo_list todo_list
= TODO_LIST_INIT
;
1289 unsigned char sha1
[20];
1293 if (read_and_refresh_cache(opts
))
1296 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1297 unsigned char sha1
[20];
1298 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1300 /* This happens when using --stdin. */
1304 if (!get_sha1(name
, sha1
)) {
1305 if (!lookup_commit_reference_gently(sha1
, 1)) {
1306 enum object_type type
= sha1_object_info(sha1
, NULL
);
1307 return error(_("%s: can't cherry-pick a %s"),
1308 name
, typename(type
));
1311 return error(_("%s: bad revision"), name
);
1315 * If we were called as "git cherry-pick <commit>", just
1316 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1317 * REVERT_HEAD, and don't touch the sequencer state.
1318 * This means it is possible to cherry-pick in the middle
1319 * of a cherry-pick sequence.
1321 if (opts
->revs
->cmdline
.nr
== 1 &&
1322 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1323 opts
->revs
->no_walk
&&
1324 !opts
->revs
->cmdline
.rev
->flags
) {
1325 struct commit
*cmit
;
1326 if (prepare_revision_walk(opts
->revs
))
1327 return error(_("revision walk setup failed"));
1328 cmit
= get_revision(opts
->revs
);
1329 if (!cmit
|| get_revision(opts
->revs
))
1330 return error("BUG: expected exactly one commit from walk");
1331 return single_pick(cmit
, opts
);
1335 * Start a new cherry-pick/ revert sequence; but
1336 * first, make sure that an existing one isn't in
1340 if (walk_revs_populate_todo(&todo_list
, opts
) ||
1341 create_seq_dir() < 0)
1343 if (get_sha1("HEAD", sha1
) && (opts
->action
== REPLAY_REVERT
))
1344 return error(_("can't revert as initial commit"));
1345 if (save_head(sha1_to_hex(sha1
)))
1347 if (save_opts(opts
))
1349 res
= pick_commits(&todo_list
, opts
);
1350 todo_list_release(&todo_list
);
1354 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1356 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1357 struct strbuf sob
= STRBUF_INIT
;
1360 strbuf_addstr(&sob
, sign_off_header
);
1361 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1362 getenv("GIT_COMMITTER_EMAIL")));
1363 strbuf_addch(&sob
, '\n');
1366 * If the whole message buffer is equal to the sob, pretend that we
1367 * found a conforming footer with a matching sob
1369 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1370 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1373 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1376 const char *append_newlines
= NULL
;
1377 size_t len
= msgbuf
->len
- ignore_footer
;
1381 * The buffer is completely empty. Leave foom for
1382 * the title and body to be filled in by the user.
1384 append_newlines
= "\n\n";
1385 } else if (msgbuf
->buf
[len
- 1] != '\n') {
1387 * Incomplete line. Complete the line and add a
1388 * blank one so that there is an empty line between
1389 * the message body and the sob.
1391 append_newlines
= "\n\n";
1392 } else if (len
== 1) {
1394 * Buffer contains a single newline. Add another
1395 * so that we leave room for the title and body.
1397 append_newlines
= "\n";
1398 } else if (msgbuf
->buf
[len
- 2] != '\n') {
1400 * Buffer ends with a single newline. Add another
1401 * so that there is an empty line between the message
1404 append_newlines
= "\n";
1405 } /* else, the buffer already ends with two newlines. */
1407 if (append_newlines
)
1408 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1409 append_newlines
, strlen(append_newlines
));
1412 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1413 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1416 strbuf_release(&sob
);