8 #include "run-command.h"
11 #include "cache-tree.h"
15 #include "merge-recursive.h"
17 #include "argv-array.h"
21 #include "wt-status.h"
23 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
25 const char sign_off_header
[] = "Signed-off-by: ";
26 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
28 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
30 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
31 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
32 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
33 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
35 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
37 * The file containing rebase commands, comments, and empty lines.
38 * This file is created by "git rebase -i" then edited by the user. As
39 * the lines are processed, they are removed from the front of this
40 * file and written to the tail of 'done'.
42 static GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
44 * The rebase command lines that have already been processed. A line
45 * is moved here when it is first handled, before any associated user
48 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
50 * The commit message that is planned to be used for any changes that
51 * need to be committed following a user interaction.
53 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
55 * The file into which is accumulated the suggested commit message for
56 * squash/fixup commands. When the first of a series of squash/fixups
57 * is seen, the file is created and the commit message from the
58 * previous commit and from the first squash/fixup commit are written
59 * to it. The commit message for each subsequent squash/fixup commit
60 * is appended to the file as it is processed.
62 * The first line of the file is of the form
63 * # This is a combination of $count commits.
64 * where $count is the number of commits whose messages have been
65 * written to the file so far (including the initial "pick" commit).
66 * Each time that a commit message is processed, this line is read and
67 * updated. It is deleted just before the combined commit is made.
69 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
71 * If the current series of squash/fixups has not yet included a squash
72 * command, then this file exists and holds the commit message of the
73 * original "pick" commit. (If the series ends without a "squash"
74 * command, then this can be used as the commit message of the combined
75 * commit without opening the editor.)
77 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
79 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
80 * GIT_AUTHOR_DATE that will be used for the commit that is currently
83 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
85 * When an "edit" rebase command is being processed, the SHA1 of the
86 * commit to be edited is recorded in this file. When "git rebase
87 * --continue" is executed, if there are any staged changes then they
88 * will be amended to the HEAD commit, but only provided the HEAD
89 * commit is still the commit to be edited. When any other rebase
90 * command is processed, this file is deleted.
92 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
94 * When we stop at a given patch via the "edit" command, this file contains
95 * the abbreviated commit name of the corresponding patch.
97 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
99 * The following files are written by git-rebase just after parsing the
100 * command-line (and are only consumed, not modified, by the sequencer).
102 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
103 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
104 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
105 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
106 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
108 static inline int is_rebase_i(const struct replay_opts
*opts
)
110 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
113 static const char *get_dir(const struct replay_opts
*opts
)
115 if (is_rebase_i(opts
))
116 return rebase_path();
117 return git_path_seq_dir();
120 static const char *get_todo_path(const struct replay_opts
*opts
)
122 if (is_rebase_i(opts
))
123 return rebase_path_todo();
124 return git_path_todo_file();
128 * Returns 0 for non-conforming footer
129 * Returns 1 for conforming footer
130 * Returns 2 when sob exists within conforming footer
131 * Returns 3 when sob exists within conforming footer as last entry
133 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
136 struct trailer_info info
;
138 int found_sob
= 0, found_sob_last
= 0;
140 trailer_info_get(&info
, sb
->buf
);
142 if (info
.trailer_start
== info
.trailer_end
)
145 for (i
= 0; i
< info
.trailer_nr
; i
++)
146 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
148 if (i
== info
.trailer_nr
- 1)
152 trailer_info_release(&info
);
161 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
163 static struct strbuf buf
= STRBUF_INIT
;
167 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
171 int sequencer_remove_state(struct replay_opts
*opts
)
173 struct strbuf dir
= STRBUF_INIT
;
176 free(opts
->gpg_sign
);
177 free(opts
->strategy
);
178 for (i
= 0; i
< opts
->xopts_nr
; i
++)
179 free(opts
->xopts
[i
]);
182 strbuf_addf(&dir
, "%s", get_dir(opts
));
183 remove_dir_recursively(&dir
, 0);
184 strbuf_release(&dir
);
189 static const char *action_name(const struct replay_opts
*opts
)
191 switch (opts
->action
) {
195 return N_("cherry-pick");
196 case REPLAY_INTERACTIVE_REBASE
:
197 return N_("rebase -i");
199 die(_("Unknown action: %d"), opts
->action
);
202 struct commit_message
{
209 static const char *short_commit_name(struct commit
*commit
)
211 return find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
214 static int get_message(struct commit
*commit
, struct commit_message
*out
)
216 const char *abbrev
, *subject
;
219 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
220 abbrev
= short_commit_name(commit
);
222 subject_len
= find_commit_subject(out
->message
, &subject
);
224 out
->subject
= xmemdupz(subject
, subject_len
);
225 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
226 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
231 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
233 free(msg
->parent_label
);
236 unuse_commit_buffer(commit
, msg
->message
);
239 static void print_advice(int show_hint
, struct replay_opts
*opts
)
241 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
244 fprintf(stderr
, "%s\n", msg
);
246 * A conflict has occurred but the porcelain
247 * (typically rebase --interactive) wants to take care
248 * of the commit itself so remove CHERRY_PICK_HEAD
250 unlink(git_path_cherry_pick_head());
256 advise(_("after resolving the conflicts, mark the corrected paths\n"
257 "with 'git add <paths>' or 'git rm <paths>'"));
259 advise(_("after resolving the conflicts, mark the corrected paths\n"
260 "with 'git add <paths>' or 'git rm <paths>'\n"
261 "and commit the result with 'git commit'"));
265 static int write_message(const void *buf
, size_t len
, const char *filename
,
268 static struct lock_file msg_file
;
270 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
272 return error_errno(_("could not lock '%s'"), filename
);
273 if (write_in_full(msg_fd
, buf
, len
) < 0) {
274 rollback_lock_file(&msg_file
);
275 return error_errno(_("could not write to '%s'"), filename
);
277 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
278 rollback_lock_file(&msg_file
);
279 return error_errno(_("could not write eol to '%s'"), filename
);
281 if (commit_lock_file(&msg_file
) < 0) {
282 rollback_lock_file(&msg_file
);
283 return error(_("failed to finalize '%s'."), filename
);
290 * Reads a file that was presumably written by a shell script, i.e. with an
291 * end-of-line marker that needs to be stripped.
293 * Note that only the last end-of-line marker is stripped, consistent with the
294 * behavior of "$(cat path)" in a shell script.
296 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
298 static int read_oneliner(struct strbuf
*buf
,
299 const char *path
, int skip_if_empty
)
301 int orig_len
= buf
->len
;
303 if (!file_exists(path
))
306 if (strbuf_read_file(buf
, path
, 0) < 0) {
307 warning_errno(_("could not read '%s'"), path
);
311 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
312 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
314 buf
->buf
[buf
->len
] = '\0';
317 if (skip_if_empty
&& buf
->len
== orig_len
)
323 static struct tree
*empty_tree(void)
325 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
328 static int error_dirty_index(struct replay_opts
*opts
)
330 if (read_cache_unmerged())
331 return error_resolve_conflict(_(action_name(opts
)));
333 error(_("your local changes would be overwritten by %s."),
334 _(action_name(opts
)));
336 if (advice_commit_before_merge
)
337 advise(_("commit your changes or stash them to proceed."));
341 static void update_abort_safety_file(void)
343 struct object_id head
;
345 /* Do nothing on a single-pick */
346 if (!file_exists(git_path_seq_dir()))
349 if (!get_oid("HEAD", &head
))
350 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
352 write_file(git_path_abort_safety_file(), "%s", "");
355 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
356 int unborn
, struct replay_opts
*opts
)
358 struct ref_transaction
*transaction
;
359 struct strbuf sb
= STRBUF_INIT
;
360 struct strbuf err
= STRBUF_INIT
;
363 if (checkout_fast_forward(from
, to
, 1))
364 return -1; /* the callee should have complained already */
366 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
368 transaction
= ref_transaction_begin(&err
);
370 ref_transaction_update(transaction
, "HEAD",
371 to
, unborn
? null_sha1
: from
,
373 ref_transaction_commit(transaction
, &err
)) {
374 ref_transaction_free(transaction
);
375 error("%s", err
.buf
);
377 strbuf_release(&err
);
382 strbuf_release(&err
);
383 ref_transaction_free(transaction
);
384 update_abort_safety_file();
388 void append_conflicts_hint(struct strbuf
*msgbuf
)
392 strbuf_addch(msgbuf
, '\n');
393 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
394 for (i
= 0; i
< active_nr
;) {
395 const struct cache_entry
*ce
= active_cache
[i
++];
397 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
398 while (i
< active_nr
&& !strcmp(ce
->name
,
399 active_cache
[i
]->name
))
405 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
406 const char *base_label
, const char *next_label
,
407 unsigned char *head
, struct strbuf
*msgbuf
,
408 struct replay_opts
*opts
)
410 struct merge_options o
;
411 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
414 static struct lock_file index_lock
;
416 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
420 init_merge_options(&o
);
421 o
.ancestor
= base
? base_label
: "(empty tree)";
423 o
.branch2
= next
? next_label
: "(empty tree)";
425 head_tree
= parse_tree_indirect(head
);
426 next_tree
= next
? next
->tree
: empty_tree();
427 base_tree
= base
? base
->tree
: empty_tree();
429 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
430 parse_merge_opt(&o
, *xopt
);
432 clean
= merge_trees(&o
,
434 next_tree
, base_tree
, &result
);
435 strbuf_release(&o
.obuf
);
439 if (active_cache_changed
&&
440 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
441 /* TRANSLATORS: %s will be "revert", "cherry-pick" or
444 return error(_("%s: Unable to write new index file"),
445 _(action_name(opts
)));
446 rollback_lock_file(&index_lock
);
449 append_signoff(msgbuf
, 0, 0);
452 append_conflicts_hint(msgbuf
);
457 static int is_index_unchanged(void)
459 unsigned char head_sha1
[20];
460 struct commit
*head_commit
;
462 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
463 return error(_("could not resolve HEAD commit\n"));
465 head_commit
= lookup_commit(head_sha1
);
468 * If head_commit is NULL, check_commit, called from
469 * lookup_commit, would have indicated that head_commit is not
470 * a commit object already. parse_commit() will return failure
471 * without further complaints in such a case. Otherwise, if
472 * the commit is invalid, parse_commit() will complain. So
473 * there is nothing for us to say here. Just return failure.
475 if (parse_commit(head_commit
))
478 if (!active_cache_tree
)
479 active_cache_tree
= cache_tree();
481 if (!cache_tree_fully_valid(active_cache_tree
))
482 if (cache_tree_update(&the_index
, 0))
483 return error(_("unable to update cache tree\n"));
485 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
488 static int write_author_script(const char *message
)
490 struct strbuf buf
= STRBUF_INIT
;
495 if (!*message
|| starts_with(message
, "\n")) {
497 /* Missing 'author' line? */
498 unlink(rebase_path_author_script());
500 } else if (skip_prefix(message
, "author ", &message
))
502 else if ((eol
= strchr(message
, '\n')))
507 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
508 while (*message
&& *message
!= '\n' && *message
!= '\r')
509 if (skip_prefix(message
, " <", &message
))
511 else if (*message
!= '\'')
512 strbuf_addch(&buf
, *(message
++));
514 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
515 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
516 while (*message
&& *message
!= '\n' && *message
!= '\r')
517 if (skip_prefix(message
, "> ", &message
))
519 else if (*message
!= '\'')
520 strbuf_addch(&buf
, *(message
++));
522 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
523 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
524 while (*message
&& *message
!= '\n' && *message
!= '\r')
525 if (*message
!= '\'')
526 strbuf_addch(&buf
, *(message
++));
528 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
529 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
530 strbuf_release(&buf
);
535 * Read the author-script file into an environment block, ready for use in
536 * run_command(), that can be free()d afterwards.
538 static char **read_author_script(void)
540 struct strbuf script
= STRBUF_INIT
;
545 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
548 for (p
= script
.buf
; *p
; p
++)
549 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
550 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
552 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
553 else if (*p
== '\n') {
558 env_size
= (count
+ 1) * sizeof(*env
);
559 strbuf_grow(&script
, env_size
);
560 memmove(script
.buf
+ env_size
, script
.buf
, script
.len
);
561 p
= script
.buf
+ env_size
;
562 env
= (char **)strbuf_detach(&script
, NULL
);
564 for (i
= 0; i
< count
; i
++) {
573 static const char staged_changes_advice
[] =
574 N_("you have staged changes in your working tree\n"
575 "If these changes are meant to be squashed into the previous commit, run:\n"
577 " git commit --amend %s\n"
579 "If they are meant to go into a new commit, run:\n"
583 "In both cases, once you're done, continue with:\n"
585 " git rebase --continue\n");
588 * If we are cherry-pick, and if the merge did not result in
589 * hand-editing, we will hit this commit and inherit the original
590 * author date and name.
592 * If we are revert, or if our cherry-pick results in a hand merge,
593 * we had better say that the current user is responsible for that.
595 * An exception is when run_git_commit() is called during an
596 * interactive rebase: in that case, we will want to retain the
599 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
600 int allow_empty
, int edit
, int amend
,
601 int cleanup_commit_message
)
604 struct argv_array array
;
608 if (is_rebase_i(opts
)) {
609 env
= read_author_script();
611 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
613 return error(_(staged_changes_advice
),
618 argv_array_init(&array
);
619 argv_array_push(&array
, "commit");
620 argv_array_push(&array
, "-n");
623 argv_array_push(&array
, "--amend");
625 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
627 argv_array_push(&array
, "-s");
629 argv_array_pushl(&array
, "-F", defmsg
, NULL
);
630 if (cleanup_commit_message
)
631 argv_array_push(&array
, "--cleanup=strip");
633 argv_array_push(&array
, "-e");
634 else if (!cleanup_commit_message
&&
635 !opts
->signoff
&& !opts
->record_origin
&&
636 git_config_get_value("commit.cleanup", &value
))
637 argv_array_push(&array
, "--cleanup=verbatim");
640 argv_array_push(&array
, "--allow-empty");
642 if (opts
->allow_empty_message
)
643 argv_array_push(&array
, "--allow-empty-message");
645 rc
= run_command_v_opt_cd_env(array
.argv
, RUN_GIT_CMD
, NULL
,
646 (const char *const *)env
);
647 argv_array_clear(&array
);
653 static int is_original_commit_empty(struct commit
*commit
)
655 const unsigned char *ptree_sha1
;
657 if (parse_commit(commit
))
658 return error(_("could not parse commit %s\n"),
659 oid_to_hex(&commit
->object
.oid
));
660 if (commit
->parents
) {
661 struct commit
*parent
= commit
->parents
->item
;
662 if (parse_commit(parent
))
663 return error(_("could not parse parent commit %s\n"),
664 oid_to_hex(&parent
->object
.oid
));
665 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
667 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
670 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
674 * Do we run "git commit" with "--allow-empty"?
676 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
678 int index_unchanged
, empty_commit
;
683 * (1) we do not allow empty at all and error out.
685 * (2) we allow ones that were initially empty, but
686 * forbid the ones that become empty;
690 if (!opts
->allow_empty
)
691 return 0; /* let "git commit" barf as necessary */
693 index_unchanged
= is_index_unchanged();
694 if (index_unchanged
< 0)
695 return index_unchanged
;
696 if (!index_unchanged
)
697 return 0; /* we do not have to say --allow-empty */
699 if (opts
->keep_redundant_commits
)
702 empty_commit
= is_original_commit_empty(commit
);
703 if (empty_commit
< 0)
712 * Note that ordering matters in this enum. Not only must it match the mapping
713 * below, it is also divided into several sections that matter. When adding
714 * new commands, make sure you add it in the right section.
717 /* commands that handle commits */
723 /* commands that do something else than handling a single commit */
725 /* commands that do nothing but are counted for reporting progress */
732 } todo_command_info
[] = {
742 static const char *command_to_string(const enum todo_command command
)
744 if ((size_t)command
< ARRAY_SIZE(todo_command_info
))
745 return todo_command_info
[command
].str
;
746 die("Unknown command: %d", command
);
749 static int is_noop(const enum todo_command command
)
751 return TODO_NOOP
<= (size_t)command
;
754 static int is_fixup(enum todo_command command
)
756 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
759 static int update_squash_messages(enum todo_command command
,
760 struct commit
*commit
, struct replay_opts
*opts
)
762 struct strbuf buf
= STRBUF_INIT
;
764 const char *message
, *body
;
766 if (file_exists(rebase_path_squash_msg())) {
767 struct strbuf header
= STRBUF_INIT
;
770 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 2048) <= 0)
771 return error(_("could not read '%s'"),
772 rebase_path_squash_msg());
775 eol
= strchrnul(buf
.buf
, '\n');
776 if (buf
.buf
[0] != comment_line_char
||
777 (p
+= strcspn(p
, "0123456789\n")) == eol
)
778 return error(_("unexpected 1st line of squash message:"
780 (int)(eol
- buf
.buf
), buf
.buf
);
781 count
= strtol(p
, NULL
, 10);
784 return error(_("invalid 1st line of squash message:\n"
786 (int)(eol
- buf
.buf
), buf
.buf
);
788 strbuf_addf(&header
, "%c ", comment_line_char
);
790 _("This is a combination of %d commits."), ++count
);
791 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
792 strbuf_release(&header
);
794 unsigned char head
[20];
795 struct commit
*head_commit
;
796 const char *head_message
, *body
;
798 if (get_sha1("HEAD", head
))
799 return error(_("need a HEAD to fixup"));
800 if (!(head_commit
= lookup_commit_reference(head
)))
801 return error(_("could not read HEAD"));
802 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
803 return error(_("could not read HEAD's commit message"));
805 find_commit_subject(head_message
, &body
);
806 if (write_message(body
, strlen(body
),
807 rebase_path_fixup_msg(), 0)) {
808 unuse_commit_buffer(head_commit
, head_message
);
809 return error(_("cannot write '%s'"),
810 rebase_path_fixup_msg());
814 strbuf_addf(&buf
, "%c ", comment_line_char
);
815 strbuf_addf(&buf
, _("This is a combination of %d commits."),
817 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
818 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
819 strbuf_addstr(&buf
, "\n\n");
820 strbuf_addstr(&buf
, body
);
822 unuse_commit_buffer(head_commit
, head_message
);
825 if (!(message
= get_commit_buffer(commit
, NULL
)))
826 return error(_("could not read commit message of %s"),
827 oid_to_hex(&commit
->object
.oid
));
828 find_commit_subject(message
, &body
);
830 if (command
== TODO_SQUASH
) {
831 unlink(rebase_path_fixup_msg());
832 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
833 strbuf_addf(&buf
, _("This is the commit message #%d:"), count
);
834 strbuf_addstr(&buf
, "\n\n");
835 strbuf_addstr(&buf
, body
);
836 } else if (command
== TODO_FIXUP
) {
837 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
838 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
840 strbuf_addstr(&buf
, "\n\n");
841 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
843 return error(_("unknown command: %d"), command
);
844 unuse_commit_buffer(commit
, message
);
846 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
847 strbuf_release(&buf
);
851 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
852 struct replay_opts
*opts
, int final_fixup
)
854 int edit
= opts
->edit
, cleanup_commit_message
= 0;
855 const char *msg_file
= edit
? NULL
: git_path_merge_msg();
856 unsigned char head
[20];
857 struct commit
*base
, *next
, *parent
;
858 const char *base_label
, *next_label
;
859 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
860 struct strbuf msgbuf
= STRBUF_INIT
;
861 int res
, unborn
= 0, amend
= 0, allow
;
863 if (opts
->no_commit
) {
865 * We do not intend to commit immediately. We just want to
866 * merge the differences in, so let's compute the tree
867 * that represents the "current" state for merge-recursive
870 if (write_cache_as_tree(head
, 0, NULL
))
871 return error(_("your index file is unmerged."));
873 unborn
= get_sha1("HEAD", head
);
875 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
876 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0, 0))
877 return error_dirty_index(opts
);
881 if (!commit
->parents
)
883 else if (commit
->parents
->next
) {
884 /* Reverting or cherry-picking a merge commit */
886 struct commit_list
*p
;
889 return error(_("commit %s is a merge but no -m option was given."),
890 oid_to_hex(&commit
->object
.oid
));
892 for (cnt
= 1, p
= commit
->parents
;
893 cnt
!= opts
->mainline
&& p
;
896 if (cnt
!= opts
->mainline
|| !p
)
897 return error(_("commit %s does not have parent %d"),
898 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
900 } else if (0 < opts
->mainline
)
901 return error(_("mainline was specified but commit %s is not a merge."),
902 oid_to_hex(&commit
->object
.oid
));
904 parent
= commit
->parents
->item
;
906 if (opts
->allow_ff
&& !is_fixup(command
) &&
907 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
908 (!parent
&& unborn
)))
909 return fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
, opts
);
911 if (parent
&& parse_commit(parent
) < 0)
912 /* TRANSLATORS: The first %s will be a "todo" command like
913 "revert" or "pick", the second %s a SHA1. */
914 return error(_("%s: cannot parse parent commit %s"),
915 command_to_string(command
),
916 oid_to_hex(&parent
->object
.oid
));
918 if (get_message(commit
, &msg
) != 0)
919 return error(_("cannot get commit message for %s"),
920 oid_to_hex(&commit
->object
.oid
));
923 * "commit" is an existing commit. We would want to apply
924 * the difference it introduces since its first parent "prev"
925 * on top of the current HEAD if we are cherry-pick. Or the
926 * reverse of it if we are revert.
929 if (command
== TODO_REVERT
) {
931 base_label
= msg
.label
;
933 next_label
= msg
.parent_label
;
934 strbuf_addstr(&msgbuf
, "Revert \"");
935 strbuf_addstr(&msgbuf
, msg
.subject
);
936 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
937 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
939 if (commit
->parents
&& commit
->parents
->next
) {
940 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
941 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
943 strbuf_addstr(&msgbuf
, ".\n");
948 base_label
= msg
.parent_label
;
950 next_label
= msg
.label
;
952 /* Append the commit log message to msgbuf. */
953 if (find_commit_subject(msg
.message
, &p
))
954 strbuf_addstr(&msgbuf
, p
);
956 if (opts
->record_origin
) {
957 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
958 strbuf_addch(&msgbuf
, '\n');
959 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
960 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
961 strbuf_addstr(&msgbuf
, ")\n");
965 if (is_fixup(command
)) {
966 if (update_squash_messages(command
, commit
, opts
))
970 msg_file
= rebase_path_squash_msg();
971 else if (file_exists(rebase_path_fixup_msg())) {
972 cleanup_commit_message
= 1;
973 msg_file
= rebase_path_fixup_msg();
975 const char *dest
= git_path("SQUASH_MSG");
977 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
978 return error(_("could not rename '%s' to '%s'"),
979 rebase_path_squash_msg(), dest
);
980 unlink(git_path("MERGE_MSG"));
986 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
988 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
989 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
990 head
, &msgbuf
, opts
);
993 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
994 git_path_merge_msg(), 0);
996 struct commit_list
*common
= NULL
;
997 struct commit_list
*remotes
= NULL
;
999 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1000 git_path_merge_msg(), 0);
1002 commit_list_insert(base
, &common
);
1003 commit_list_insert(next
, &remotes
);
1004 res
|= try_merge_command(opts
->strategy
,
1005 opts
->xopts_nr
, (const char **)opts
->xopts
,
1006 common
, sha1_to_hex(head
), remotes
);
1007 free_commit_list(common
);
1008 free_commit_list(remotes
);
1010 strbuf_release(&msgbuf
);
1013 * If the merge was clean or if it failed due to conflict, we write
1014 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1015 * However, if the merge did not even start, then we don't want to
1018 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1019 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
1020 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
1022 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1023 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
1024 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
1028 error(command
== TODO_REVERT
1029 ? _("could not revert %s... %s")
1030 : _("could not apply %s... %s"),
1031 short_commit_name(commit
), msg
.subject
);
1032 print_advice(res
== 1, opts
);
1033 rerere(opts
->allow_rerere_auto
);
1037 allow
= allow_empty(opts
, commit
);
1042 if (!opts
->no_commit
)
1043 res
= run_git_commit(msg_file
, opts
, allow
, edit
, amend
,
1044 cleanup_commit_message
);
1046 if (!res
&& final_fixup
) {
1047 unlink(rebase_path_fixup_msg());
1048 unlink(rebase_path_squash_msg());
1052 free_message(commit
, &msg
);
1053 update_abort_safety_file();
1058 static int prepare_revs(struct replay_opts
*opts
)
1061 * picking (but not reverting) ranges (but not individual revisions)
1062 * should be done in reverse
1064 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1065 opts
->revs
->reverse
^= 1;
1067 if (prepare_revision_walk(opts
->revs
))
1068 return error(_("revision walk setup failed"));
1070 if (!opts
->revs
->commits
)
1071 return error(_("empty commit set passed"));
1075 static int read_and_refresh_cache(struct replay_opts
*opts
)
1077 static struct lock_file index_lock
;
1078 int index_fd
= hold_locked_index(&index_lock
, 0);
1079 if (read_index_preload(&the_index
, NULL
) < 0) {
1080 rollback_lock_file(&index_lock
);
1081 return error(_("git %s: failed to read the index"),
1082 _(action_name(opts
)));
1084 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1085 if (the_index
.cache_changed
&& index_fd
>= 0) {
1086 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
1087 rollback_lock_file(&index_lock
);
1088 return error(_("git %s: failed to refresh the index"),
1089 _(action_name(opts
)));
1092 rollback_lock_file(&index_lock
);
1097 enum todo_command command
;
1098 struct commit
*commit
;
1101 size_t offset_in_buf
;
1106 struct todo_item
*items
;
1107 int nr
, alloc
, current
;
1110 #define TODO_LIST_INIT { STRBUF_INIT }
1112 static void todo_list_release(struct todo_list
*todo_list
)
1114 strbuf_release(&todo_list
->buf
);
1115 free(todo_list
->items
);
1116 todo_list
->items
= NULL
;
1117 todo_list
->nr
= todo_list
->alloc
= 0;
1120 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1122 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1123 return todo_list
->items
+ todo_list
->nr
++;
1126 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1128 unsigned char commit_sha1
[20];
1129 char *end_of_object_name
;
1130 int i
, saved
, status
, padding
;
1133 bol
+= strspn(bol
, " \t");
1135 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1136 item
->command
= TODO_NOOP
;
1137 item
->commit
= NULL
;
1139 item
->arg_len
= eol
- bol
;
1143 for (i
= 0; i
< ARRAY_SIZE(todo_command_info
); i
++)
1144 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1147 } else if (bol
[1] == ' ' && *bol
== todo_command_info
[i
].c
) {
1152 if (i
>= ARRAY_SIZE(todo_command_info
))
1155 if (item
->command
== TODO_NOOP
) {
1156 item
->commit
= NULL
;
1158 item
->arg_len
= eol
- bol
;
1162 /* Eat up extra spaces/ tabs before object name */
1163 padding
= strspn(bol
, " \t");
1168 if (item
->command
== TODO_EXEC
) {
1170 item
->arg_len
= (int)(eol
- bol
);
1174 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
1175 saved
= *end_of_object_name
;
1176 *end_of_object_name
= '\0';
1177 status
= get_sha1(bol
, commit_sha1
);
1178 *end_of_object_name
= saved
;
1180 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
1181 item
->arg_len
= (int)(eol
- item
->arg
);
1186 item
->commit
= lookup_commit_reference(commit_sha1
);
1187 return !item
->commit
;
1190 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
1192 struct todo_item
*item
;
1193 char *p
= buf
, *next_p
;
1194 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
1196 for (i
= 1; *p
; i
++, p
= next_p
) {
1197 char *eol
= strchrnul(p
, '\n');
1199 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
1201 if (p
!= eol
&& eol
[-1] == '\r')
1202 eol
--; /* strip Carriage Return */
1204 item
= append_new_todo(todo_list
);
1205 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
1206 if (parse_insn_line(item
, p
, eol
)) {
1207 res
= error(_("invalid line %d: %.*s"),
1208 i
, (int)(eol
- p
), p
);
1209 item
->command
= TODO_NOOP
;
1214 else if (is_fixup(item
->command
))
1215 return error(_("cannot '%s' without a previous commit"),
1216 command_to_string(item
->command
));
1217 else if (!is_noop(item
->command
))
1224 static int read_populate_todo(struct todo_list
*todo_list
,
1225 struct replay_opts
*opts
)
1227 const char *todo_file
= get_todo_path(opts
);
1230 strbuf_reset(&todo_list
->buf
);
1231 fd
= open(todo_file
, O_RDONLY
);
1233 return error_errno(_("could not open '%s'"), todo_file
);
1234 if (strbuf_read(&todo_list
->buf
, fd
, 0) < 0) {
1236 return error(_("could not read '%s'."), todo_file
);
1240 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
1242 return error(_("unusable instruction sheet: '%s'"), todo_file
);
1244 if (!todo_list
->nr
&&
1245 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
1246 return error(_("no commits parsed."));
1248 if (!is_rebase_i(opts
)) {
1249 enum todo_command valid
=
1250 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
1253 for (i
= 0; i
< todo_list
->nr
; i
++)
1254 if (valid
== todo_list
->items
[i
].command
)
1256 else if (valid
== TODO_PICK
)
1257 return error(_("cannot cherry-pick during a revert."));
1259 return error(_("cannot revert during a cherry-pick."));
1265 static int git_config_string_dup(char **dest
,
1266 const char *var
, const char *value
)
1269 return config_error_nonbool(var
);
1271 *dest
= xstrdup(value
);
1275 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
1277 struct replay_opts
*opts
= data
;
1282 else if (!strcmp(key
, "options.no-commit"))
1283 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
1284 else if (!strcmp(key
, "options.edit"))
1285 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
1286 else if (!strcmp(key
, "options.signoff"))
1287 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
1288 else if (!strcmp(key
, "options.record-origin"))
1289 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
1290 else if (!strcmp(key
, "options.allow-ff"))
1291 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
1292 else if (!strcmp(key
, "options.mainline"))
1293 opts
->mainline
= git_config_int(key
, value
);
1294 else if (!strcmp(key
, "options.strategy"))
1295 git_config_string_dup(&opts
->strategy
, key
, value
);
1296 else if (!strcmp(key
, "options.gpg-sign"))
1297 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
1298 else if (!strcmp(key
, "options.strategy-option")) {
1299 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
1300 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
1302 return error(_("invalid key: %s"), key
);
1305 return error(_("invalid value for %s: %s"), key
, value
);
1310 static int read_populate_opts(struct replay_opts
*opts
)
1312 if (is_rebase_i(opts
)) {
1313 struct strbuf buf
= STRBUF_INIT
;
1315 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
1316 if (!starts_with(buf
.buf
, "-S"))
1319 free(opts
->gpg_sign
);
1320 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
1323 strbuf_release(&buf
);
1325 if (file_exists(rebase_path_verbose()))
1331 if (!file_exists(git_path_opts_file()))
1334 * The function git_parse_source(), called from git_config_from_file(),
1335 * may die() in case of a syntactically incorrect file. We do not care
1336 * about this case, though, because we wrote that file ourselves, so we
1337 * are pretty certain that it is syntactically correct.
1339 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
1340 return error(_("malformed options sheet: '%s'"),
1341 git_path_opts_file());
1345 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
1346 struct replay_opts
*opts
)
1348 enum todo_command command
= opts
->action
== REPLAY_PICK
?
1349 TODO_PICK
: TODO_REVERT
;
1350 const char *command_string
= todo_command_info
[command
].str
;
1351 struct commit
*commit
;
1353 if (prepare_revs(opts
))
1356 while ((commit
= get_revision(opts
->revs
))) {
1357 struct todo_item
*item
= append_new_todo(todo_list
);
1358 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1359 const char *subject
;
1362 item
->command
= command
;
1363 item
->commit
= commit
;
1366 item
->offset_in_buf
= todo_list
->buf
.len
;
1367 subject_len
= find_commit_subject(commit_buffer
, &subject
);
1368 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
1369 short_commit_name(commit
), subject_len
, subject
);
1370 unuse_commit_buffer(commit
, commit_buffer
);
1375 static int create_seq_dir(void)
1377 if (file_exists(git_path_seq_dir())) {
1378 error(_("a cherry-pick or revert is already in progress"));
1379 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
1381 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
1382 return error_errno(_("could not create sequencer directory '%s'"),
1383 git_path_seq_dir());
1387 static int save_head(const char *head
)
1389 static struct lock_file head_lock
;
1390 struct strbuf buf
= STRBUF_INIT
;
1393 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
1395 rollback_lock_file(&head_lock
);
1396 return error_errno(_("could not lock HEAD"));
1398 strbuf_addf(&buf
, "%s\n", head
);
1399 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
1400 rollback_lock_file(&head_lock
);
1401 return error_errno(_("could not write to '%s'"),
1402 git_path_head_file());
1404 if (commit_lock_file(&head_lock
) < 0) {
1405 rollback_lock_file(&head_lock
);
1406 return error(_("failed to finalize '%s'."), git_path_head_file());
1411 static int rollback_is_safe(void)
1413 struct strbuf sb
= STRBUF_INIT
;
1414 struct object_id expected_head
, actual_head
;
1416 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
1418 if (get_oid_hex(sb
.buf
, &expected_head
)) {
1419 strbuf_release(&sb
);
1420 die(_("could not parse %s"), git_path_abort_safety_file());
1422 strbuf_release(&sb
);
1424 else if (errno
== ENOENT
)
1425 oidclr(&expected_head
);
1427 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
1429 if (get_oid("HEAD", &actual_head
))
1430 oidclr(&actual_head
);
1432 return !oidcmp(&actual_head
, &expected_head
);
1435 static int reset_for_rollback(const unsigned char *sha1
)
1437 const char *argv
[4]; /* reset --merge <arg> + NULL */
1440 argv
[1] = "--merge";
1441 argv
[2] = sha1_to_hex(sha1
);
1443 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1446 static int rollback_single_pick(void)
1448 unsigned char head_sha1
[20];
1450 if (!file_exists(git_path_cherry_pick_head()) &&
1451 !file_exists(git_path_revert_head()))
1452 return error(_("no cherry-pick or revert in progress"));
1453 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
1454 return error(_("cannot resolve HEAD"));
1455 if (is_null_sha1(head_sha1
))
1456 return error(_("cannot abort from a branch yet to be born"));
1457 return reset_for_rollback(head_sha1
);
1460 int sequencer_rollback(struct replay_opts
*opts
)
1463 unsigned char sha1
[20];
1464 struct strbuf buf
= STRBUF_INIT
;
1466 f
= fopen(git_path_head_file(), "r");
1467 if (!f
&& errno
== ENOENT
) {
1469 * There is no multiple-cherry-pick in progress.
1470 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
1471 * a single-cherry-pick in progress, abort that.
1473 return rollback_single_pick();
1476 return error_errno(_("cannot open '%s'"), git_path_head_file());
1477 if (strbuf_getline_lf(&buf
, f
)) {
1478 error(_("cannot read '%s': %s"), git_path_head_file(),
1479 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
1484 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
1485 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
1486 git_path_head_file());
1489 if (is_null_sha1(sha1
)) {
1490 error(_("cannot abort from a branch yet to be born"));
1494 if (!rollback_is_safe()) {
1495 /* Do not error, just do not rollback */
1496 warning(_("You seem to have moved HEAD. "
1497 "Not rewinding, check your HEAD!"));
1499 if (reset_for_rollback(sha1
))
1501 strbuf_release(&buf
);
1502 return sequencer_remove_state(opts
);
1504 strbuf_release(&buf
);
1508 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1510 static struct lock_file todo_lock
;
1511 const char *todo_path
= get_todo_path(opts
);
1512 int next
= todo_list
->current
, offset
, fd
;
1515 * rebase -i writes "git-rebase-todo" without the currently executing
1516 * command, appending it to "done" instead.
1518 if (is_rebase_i(opts
))
1521 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
1523 return error_errno(_("could not lock '%s'"), todo_path
);
1524 offset
= next
< todo_list
->nr
?
1525 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
1526 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
1527 todo_list
->buf
.len
- offset
) < 0)
1528 return error_errno(_("could not write to '%s'"), todo_path
);
1529 if (commit_lock_file(&todo_lock
) < 0)
1530 return error(_("failed to finalize '%s'."), todo_path
);
1532 if (is_rebase_i(opts
)) {
1533 const char *done_path
= rebase_path_done();
1534 int fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
1535 int prev_offset
= !next
? 0 :
1536 todo_list
->items
[next
- 1].offset_in_buf
;
1538 if (fd
>= 0 && offset
> prev_offset
&&
1539 write_in_full(fd
, todo_list
->buf
.buf
+ prev_offset
,
1540 offset
- prev_offset
) < 0) {
1542 return error_errno(_("could not write to '%s'"),
1551 static int save_opts(struct replay_opts
*opts
)
1553 const char *opts_file
= git_path_opts_file();
1556 if (opts
->no_commit
)
1557 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
1559 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
1561 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
1562 if (opts
->record_origin
)
1563 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
1565 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
1566 if (opts
->mainline
) {
1567 struct strbuf buf
= STRBUF_INIT
;
1568 strbuf_addf(&buf
, "%d", opts
->mainline
);
1569 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
1570 strbuf_release(&buf
);
1573 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
1575 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
1578 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1579 res
|= git_config_set_multivar_in_file_gently(opts_file
,
1580 "options.strategy-option",
1581 opts
->xopts
[i
], "^$", 0);
1586 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
1588 struct strbuf buf
= STRBUF_INIT
;
1589 struct rev_info log_tree_opt
;
1590 const char *subject
, *p
;
1593 p
= short_commit_name(commit
);
1594 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
1597 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
1598 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
1599 init_revisions(&log_tree_opt
, NULL
);
1600 log_tree_opt
.abbrev
= 0;
1601 log_tree_opt
.diff
= 1;
1602 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1603 log_tree_opt
.disable_stdin
= 1;
1604 log_tree_opt
.no_commit_id
= 1;
1605 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
1606 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1607 if (!log_tree_opt
.diffopt
.file
)
1608 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
1610 res
|= log_tree_commit(&log_tree_opt
, commit
);
1611 fclose(log_tree_opt
.diffopt
.file
);
1615 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
1616 if (!file_exists(buf
.buf
)) {
1617 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1618 find_commit_subject(commit_buffer
, &subject
);
1619 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
1620 unuse_commit_buffer(commit
, commit_buffer
);
1622 strbuf_release(&buf
);
1627 static int intend_to_amend(void)
1629 unsigned char head
[20];
1632 if (get_sha1("HEAD", head
))
1633 return error(_("cannot read HEAD"));
1635 p
= sha1_to_hex(head
);
1636 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
1639 static int error_with_patch(struct commit
*commit
,
1640 const char *subject
, int subject_len
,
1641 struct replay_opts
*opts
, int exit_code
, int to_amend
)
1643 if (make_patch(commit
, opts
))
1647 if (intend_to_amend())
1650 fprintf(stderr
, "You can amend the commit now, with\n"
1652 " git commit --amend %s\n"
1654 "Once you are satisfied with your changes, run\n"
1656 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
1657 } else if (exit_code
)
1658 fprintf(stderr
, "Could not apply %s... %.*s\n",
1659 short_commit_name(commit
), subject_len
, subject
);
1664 static int error_failed_squash(struct commit
*commit
,
1665 struct replay_opts
*opts
, int subject_len
, const char *subject
)
1667 if (rename(rebase_path_squash_msg(), rebase_path_message()))
1668 return error(_("could not rename '%s' to '%s'"),
1669 rebase_path_squash_msg(), rebase_path_message());
1670 unlink(rebase_path_fixup_msg());
1671 unlink(git_path("MERGE_MSG"));
1672 if (copy_file(git_path("MERGE_MSG"), rebase_path_message(), 0666))
1673 return error(_("could not copy '%s' to '%s'"),
1674 rebase_path_message(), git_path("MERGE_MSG"));
1675 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
1678 static int do_exec(const char *command_line
)
1680 const char *child_argv
[] = { NULL
, NULL
};
1683 fprintf(stderr
, "Executing: %s\n", command_line
);
1684 child_argv
[0] = command_line
;
1685 status
= run_command_v_opt(child_argv
, RUN_USING_SHELL
);
1687 /* force re-reading of the cache */
1688 if (discard_cache() < 0 || read_cache() < 0)
1689 return error(_("could not read index"));
1691 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
1694 warning(_("execution failed: %s\n%s"
1695 "You can fix the problem, and then run\n"
1697 " git rebase --continue\n"
1700 dirty
? N_("and made changes to the index and/or the "
1701 "working tree\n") : "");
1703 /* command not found */
1706 warning(_("execution succeeded: %s\nbut "
1707 "left changes to the index and/or the working tree\n"
1708 "Commit or stash your changes, and then run\n"
1710 " git rebase --continue\n"
1711 "\n"), command_line
);
1718 static int is_final_fixup(struct todo_list
*todo_list
)
1720 int i
= todo_list
->current
;
1722 if (!is_fixup(todo_list
->items
[i
].command
))
1725 while (++i
< todo_list
->nr
)
1726 if (is_fixup(todo_list
->items
[i
].command
))
1728 else if (!is_noop(todo_list
->items
[i
].command
))
1733 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1737 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1739 assert(!(opts
->signoff
|| opts
->no_commit
||
1740 opts
->record_origin
|| opts
->edit
));
1741 if (read_and_refresh_cache(opts
))
1744 while (todo_list
->current
< todo_list
->nr
) {
1745 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
1746 if (save_todo(todo_list
, opts
))
1748 if (is_rebase_i(opts
)) {
1749 unlink(rebase_path_message());
1750 unlink(rebase_path_author_script());
1751 unlink(rebase_path_stopped_sha());
1752 unlink(rebase_path_amend());
1754 if (item
->command
<= TODO_SQUASH
) {
1755 res
= do_pick_commit(item
->command
, item
->commit
,
1756 opts
, is_final_fixup(todo_list
));
1757 if (item
->command
== TODO_EDIT
) {
1758 struct commit
*commit
= item
->commit
;
1760 warning(_("stopped at %s... %.*s"),
1761 short_commit_name(commit
),
1762 item
->arg_len
, item
->arg
);
1763 return error_with_patch(commit
,
1764 item
->arg
, item
->arg_len
, opts
, res
,
1767 if (res
&& is_fixup(item
->command
)) {
1770 return error_failed_squash(item
->commit
, opts
,
1771 item
->arg_len
, item
->arg
);
1772 } else if (res
&& is_rebase_i(opts
))
1773 return res
| error_with_patch(item
->commit
,
1774 item
->arg
, item
->arg_len
, opts
, res
, 0);
1775 } else if (item
->command
== TODO_EXEC
) {
1776 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
1777 int saved
= *end_of_arg
;
1780 res
= do_exec(item
->arg
);
1781 *end_of_arg
= saved
;
1782 } else if (!is_noop(item
->command
))
1783 return error(_("unknown command %d"), item
->command
);
1785 todo_list
->current
++;
1790 if (is_rebase_i(opts
)) {
1791 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
1793 /* Stopped in the middle, as planned? */
1794 if (todo_list
->current
< todo_list
->nr
)
1797 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
1798 starts_with(head_ref
.buf
, "refs/")) {
1799 unsigned char head
[20], orig
[20];
1802 if (get_sha1("HEAD", head
)) {
1803 res
= error(_("cannot read HEAD"));
1805 strbuf_release(&head_ref
);
1806 strbuf_release(&buf
);
1809 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
1810 get_sha1_hex(buf
.buf
, orig
)) {
1811 res
= error(_("could not read orig-head"));
1812 goto cleanup_head_ref
;
1814 strbuf_addf(&buf
, "rebase -i (finish): %s onto ",
1816 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
1817 res
= error(_("could not read 'onto'"));
1818 goto cleanup_head_ref
;
1820 if (update_ref(buf
.buf
, head_ref
.buf
, head
, orig
,
1821 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
1822 res
= error(_("could not update %s"),
1824 goto cleanup_head_ref
;
1828 "rebase -i (finish): returning to %s",
1830 if (create_symref("HEAD", head_ref
.buf
, buf
.buf
)) {
1831 res
= error(_("could not update HEAD to %s"),
1833 goto cleanup_head_ref
;
1838 if (opts
->verbose
) {
1839 struct rev_info log_tree_opt
;
1840 struct object_id orig
, head
;
1842 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
1843 init_revisions(&log_tree_opt
, NULL
);
1844 log_tree_opt
.diff
= 1;
1845 log_tree_opt
.diffopt
.output_format
=
1846 DIFF_FORMAT_DIFFSTAT
;
1847 log_tree_opt
.disable_stdin
= 1;
1849 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
1850 !get_sha1(buf
.buf
, orig
.hash
) &&
1851 !get_sha1("HEAD", head
.hash
)) {
1852 diff_tree_sha1(orig
.hash
, head
.hash
,
1853 "", &log_tree_opt
.diffopt
);
1854 log_tree_diff_flush(&log_tree_opt
);
1857 strbuf_release(&buf
);
1858 strbuf_release(&head_ref
);
1862 * Sequence of picks finished successfully; cleanup by
1863 * removing the .git/sequencer directory
1865 return sequencer_remove_state(opts
);
1868 static int continue_single_pick(void)
1870 const char *argv
[] = { "commit", NULL
};
1872 if (!file_exists(git_path_cherry_pick_head()) &&
1873 !file_exists(git_path_revert_head()))
1874 return error(_("no cherry-pick or revert in progress"));
1875 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1878 static int commit_staged_changes(struct replay_opts
*opts
)
1882 if (has_unstaged_changes(1))
1883 return error(_("cannot rebase: You have unstaged changes."));
1884 if (!has_uncommitted_changes(0)) {
1885 const char *cherry_pick_head
= git_path("CHERRY_PICK_HEAD");
1887 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
1888 return error(_("could not remove CHERRY_PICK_HEAD"));
1892 if (file_exists(rebase_path_amend())) {
1893 struct strbuf rev
= STRBUF_INIT
;
1894 unsigned char head
[20], to_amend
[20];
1896 if (get_sha1("HEAD", head
))
1897 return error(_("cannot amend non-existing commit"));
1898 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
1899 return error(_("invalid file: '%s'"), rebase_path_amend());
1900 if (get_sha1_hex(rev
.buf
, to_amend
))
1901 return error(_("invalid contents: '%s'"),
1902 rebase_path_amend());
1903 if (hashcmp(head
, to_amend
))
1904 return error(_("\nYou have uncommitted changes in your "
1905 "working tree. Please, commit them\n"
1906 "first and then run 'git rebase "
1907 "--continue' again."));
1909 strbuf_release(&rev
);
1913 if (run_git_commit(rebase_path_message(), opts
, 1, 1, amend
, 0))
1914 return error(_("could not commit staged changes."));
1915 unlink(rebase_path_amend());
1919 int sequencer_continue(struct replay_opts
*opts
)
1921 struct todo_list todo_list
= TODO_LIST_INIT
;
1924 if (read_and_refresh_cache(opts
))
1927 if (is_rebase_i(opts
)) {
1928 if (commit_staged_changes(opts
))
1930 } else if (!file_exists(get_todo_path(opts
)))
1931 return continue_single_pick();
1932 if (read_populate_opts(opts
))
1934 if ((res
= read_populate_todo(&todo_list
, opts
)))
1935 goto release_todo_list
;
1937 if (!is_rebase_i(opts
)) {
1938 /* Verify that the conflict has been resolved */
1939 if (file_exists(git_path_cherry_pick_head()) ||
1940 file_exists(git_path_revert_head())) {
1941 res
= continue_single_pick();
1943 goto release_todo_list
;
1945 if (index_differs_from("HEAD", 0, 0)) {
1946 res
= error_dirty_index(opts
);
1947 goto release_todo_list
;
1949 todo_list
.current
++;
1952 res
= pick_commits(&todo_list
, opts
);
1954 todo_list_release(&todo_list
);
1958 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1960 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1961 return do_pick_commit(opts
->action
== REPLAY_PICK
?
1962 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
1965 int sequencer_pick_revisions(struct replay_opts
*opts
)
1967 struct todo_list todo_list
= TODO_LIST_INIT
;
1968 unsigned char sha1
[20];
1972 if (read_and_refresh_cache(opts
))
1975 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1976 unsigned char sha1
[20];
1977 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1979 /* This happens when using --stdin. */
1983 if (!get_sha1(name
, sha1
)) {
1984 if (!lookup_commit_reference_gently(sha1
, 1)) {
1985 enum object_type type
= sha1_object_info(sha1
, NULL
);
1986 return error(_("%s: can't cherry-pick a %s"),
1987 name
, typename(type
));
1990 return error(_("%s: bad revision"), name
);
1994 * If we were called as "git cherry-pick <commit>", just
1995 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1996 * REVERT_HEAD, and don't touch the sequencer state.
1997 * This means it is possible to cherry-pick in the middle
1998 * of a cherry-pick sequence.
2000 if (opts
->revs
->cmdline
.nr
== 1 &&
2001 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
2002 opts
->revs
->no_walk
&&
2003 !opts
->revs
->cmdline
.rev
->flags
) {
2004 struct commit
*cmit
;
2005 if (prepare_revision_walk(opts
->revs
))
2006 return error(_("revision walk setup failed"));
2007 cmit
= get_revision(opts
->revs
);
2008 if (!cmit
|| get_revision(opts
->revs
))
2009 return error("BUG: expected exactly one commit from walk");
2010 return single_pick(cmit
, opts
);
2014 * Start a new cherry-pick/ revert sequence; but
2015 * first, make sure that an existing one isn't in
2019 if (walk_revs_populate_todo(&todo_list
, opts
) ||
2020 create_seq_dir() < 0)
2022 if (get_sha1("HEAD", sha1
) && (opts
->action
== REPLAY_REVERT
))
2023 return error(_("can't revert as initial commit"));
2024 if (save_head(sha1_to_hex(sha1
)))
2026 if (save_opts(opts
))
2028 update_abort_safety_file();
2029 res
= pick_commits(&todo_list
, opts
);
2030 todo_list_release(&todo_list
);
2034 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
2036 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
2037 struct strbuf sob
= STRBUF_INIT
;
2040 strbuf_addstr(&sob
, sign_off_header
);
2041 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
2042 getenv("GIT_COMMITTER_EMAIL")));
2043 strbuf_addch(&sob
, '\n');
2046 * If the whole message buffer is equal to the sob, pretend that we
2047 * found a conforming footer with a matching sob
2049 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
2050 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
2053 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
2056 const char *append_newlines
= NULL
;
2057 size_t len
= msgbuf
->len
- ignore_footer
;
2061 * The buffer is completely empty. Leave foom for
2062 * the title and body to be filled in by the user.
2064 append_newlines
= "\n\n";
2065 } else if (msgbuf
->buf
[len
- 1] != '\n') {
2067 * Incomplete line. Complete the line and add a
2068 * blank one so that there is an empty line between
2069 * the message body and the sob.
2071 append_newlines
= "\n\n";
2072 } else if (len
== 1) {
2074 * Buffer contains a single newline. Add another
2075 * so that we leave room for the title and body.
2077 append_newlines
= "\n";
2078 } else if (msgbuf
->buf
[len
- 2] != '\n') {
2080 * Buffer ends with a single newline. Add another
2081 * so that there is an empty line between the message
2084 append_newlines
= "\n";
2085 } /* else, the buffer already ends with two newlines. */
2087 if (append_newlines
)
2088 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2089 append_newlines
, strlen(append_newlines
));
2092 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
2093 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2096 strbuf_release(&sob
);