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")
106 static inline int is_rebase_i(const struct replay_opts
*opts
)
108 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
111 static const char *get_dir(const struct replay_opts
*opts
)
113 if (is_rebase_i(opts
))
114 return rebase_path();
115 return git_path_seq_dir();
118 static const char *get_todo_path(const struct replay_opts
*opts
)
120 if (is_rebase_i(opts
))
121 return rebase_path_todo();
122 return git_path_todo_file();
126 * Returns 0 for non-conforming footer
127 * Returns 1 for conforming footer
128 * Returns 2 when sob exists within conforming footer
129 * Returns 3 when sob exists within conforming footer as last entry
131 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
134 struct trailer_info info
;
136 int found_sob
= 0, found_sob_last
= 0;
138 trailer_info_get(&info
, sb
->buf
);
140 if (info
.trailer_start
== info
.trailer_end
)
143 for (i
= 0; i
< info
.trailer_nr
; i
++)
144 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
146 if (i
== info
.trailer_nr
- 1)
150 trailer_info_release(&info
);
159 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
161 static struct strbuf buf
= STRBUF_INIT
;
165 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
169 int sequencer_remove_state(struct replay_opts
*opts
)
171 struct strbuf dir
= STRBUF_INIT
;
174 free(opts
->gpg_sign
);
175 free(opts
->strategy
);
176 for (i
= 0; i
< opts
->xopts_nr
; i
++)
177 free(opts
->xopts
[i
]);
180 strbuf_addf(&dir
, "%s", get_dir(opts
));
181 remove_dir_recursively(&dir
, 0);
182 strbuf_release(&dir
);
187 static const char *action_name(const struct replay_opts
*opts
)
189 switch (opts
->action
) {
193 return N_("cherry-pick");
194 case REPLAY_INTERACTIVE_REBASE
:
195 return N_("rebase -i");
197 die(_("Unknown action: %d"), opts
->action
);
200 struct commit_message
{
207 static const char *short_commit_name(struct commit
*commit
)
209 return find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
212 static int get_message(struct commit
*commit
, struct commit_message
*out
)
214 const char *abbrev
, *subject
;
217 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
218 abbrev
= short_commit_name(commit
);
220 subject_len
= find_commit_subject(out
->message
, &subject
);
222 out
->subject
= xmemdupz(subject
, subject_len
);
223 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
224 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
229 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
231 free(msg
->parent_label
);
234 unuse_commit_buffer(commit
, msg
->message
);
237 static void print_advice(int show_hint
, struct replay_opts
*opts
)
239 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
242 fprintf(stderr
, "%s\n", msg
);
244 * A conflict has occurred but the porcelain
245 * (typically rebase --interactive) wants to take care
246 * of the commit itself so remove CHERRY_PICK_HEAD
248 unlink(git_path_cherry_pick_head());
254 advise(_("after resolving the conflicts, mark the corrected paths\n"
255 "with 'git add <paths>' or 'git rm <paths>'"));
257 advise(_("after resolving the conflicts, mark the corrected paths\n"
258 "with 'git add <paths>' or 'git rm <paths>'\n"
259 "and commit the result with 'git commit'"));
263 static int write_message(const void *buf
, size_t len
, const char *filename
,
266 static struct lock_file msg_file
;
268 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
270 return error_errno(_("could not lock '%s'"), filename
);
271 if (write_in_full(msg_fd
, buf
, len
) < 0) {
272 rollback_lock_file(&msg_file
);
273 return error_errno(_("could not write to '%s'"), filename
);
275 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
276 rollback_lock_file(&msg_file
);
277 return error_errno(_("could not write eol to '%s'"), filename
);
279 if (commit_lock_file(&msg_file
) < 0) {
280 rollback_lock_file(&msg_file
);
281 return error(_("failed to finalize '%s'."), filename
);
288 * Reads a file that was presumably written by a shell script, i.e. with an
289 * end-of-line marker that needs to be stripped.
291 * Note that only the last end-of-line marker is stripped, consistent with the
292 * behavior of "$(cat path)" in a shell script.
294 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
296 static int read_oneliner(struct strbuf
*buf
,
297 const char *path
, int skip_if_empty
)
299 int orig_len
= buf
->len
;
301 if (!file_exists(path
))
304 if (strbuf_read_file(buf
, path
, 0) < 0) {
305 warning_errno(_("could not read '%s'"), path
);
309 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
310 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
312 buf
->buf
[buf
->len
] = '\0';
315 if (skip_if_empty
&& buf
->len
== orig_len
)
321 static struct tree
*empty_tree(void)
323 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
326 static int error_dirty_index(struct replay_opts
*opts
)
328 if (read_cache_unmerged())
329 return error_resolve_conflict(_(action_name(opts
)));
331 error(_("your local changes would be overwritten by %s."),
332 _(action_name(opts
)));
334 if (advice_commit_before_merge
)
335 advise(_("commit your changes or stash them to proceed."));
339 static void update_abort_safety_file(void)
341 struct object_id head
;
343 /* Do nothing on a single-pick */
344 if (!file_exists(git_path_seq_dir()))
347 if (!get_oid("HEAD", &head
))
348 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
350 write_file(git_path_abort_safety_file(), "%s", "");
353 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
354 int unborn
, struct replay_opts
*opts
)
356 struct ref_transaction
*transaction
;
357 struct strbuf sb
= STRBUF_INIT
;
358 struct strbuf err
= STRBUF_INIT
;
361 if (checkout_fast_forward(from
, to
, 1))
362 return -1; /* the callee should have complained already */
364 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
366 transaction
= ref_transaction_begin(&err
);
368 ref_transaction_update(transaction
, "HEAD",
369 to
, unborn
? null_sha1
: from
,
371 ref_transaction_commit(transaction
, &err
)) {
372 ref_transaction_free(transaction
);
373 error("%s", err
.buf
);
375 strbuf_release(&err
);
380 strbuf_release(&err
);
381 ref_transaction_free(transaction
);
382 update_abort_safety_file();
386 void append_conflicts_hint(struct strbuf
*msgbuf
)
390 strbuf_addch(msgbuf
, '\n');
391 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
392 for (i
= 0; i
< active_nr
;) {
393 const struct cache_entry
*ce
= active_cache
[i
++];
395 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
396 while (i
< active_nr
&& !strcmp(ce
->name
,
397 active_cache
[i
]->name
))
403 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
404 const char *base_label
, const char *next_label
,
405 unsigned char *head
, struct strbuf
*msgbuf
,
406 struct replay_opts
*opts
)
408 struct merge_options o
;
409 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
412 static struct lock_file index_lock
;
414 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
418 init_merge_options(&o
);
419 o
.ancestor
= base
? base_label
: "(empty tree)";
421 o
.branch2
= next
? next_label
: "(empty tree)";
423 head_tree
= parse_tree_indirect(head
);
424 next_tree
= next
? next
->tree
: empty_tree();
425 base_tree
= base
? base
->tree
: empty_tree();
427 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
428 parse_merge_opt(&o
, *xopt
);
430 clean
= merge_trees(&o
,
432 next_tree
, base_tree
, &result
);
433 strbuf_release(&o
.obuf
);
437 if (active_cache_changed
&&
438 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
439 /* TRANSLATORS: %s will be "revert", "cherry-pick" or
442 return error(_("%s: Unable to write new index file"),
443 _(action_name(opts
)));
444 rollback_lock_file(&index_lock
);
447 append_signoff(msgbuf
, 0, 0);
450 append_conflicts_hint(msgbuf
);
455 static int is_index_unchanged(void)
457 unsigned char head_sha1
[20];
458 struct commit
*head_commit
;
460 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
461 return error(_("could not resolve HEAD commit\n"));
463 head_commit
= lookup_commit(head_sha1
);
466 * If head_commit is NULL, check_commit, called from
467 * lookup_commit, would have indicated that head_commit is not
468 * a commit object already. parse_commit() will return failure
469 * without further complaints in such a case. Otherwise, if
470 * the commit is invalid, parse_commit() will complain. So
471 * there is nothing for us to say here. Just return failure.
473 if (parse_commit(head_commit
))
476 if (!active_cache_tree
)
477 active_cache_tree
= cache_tree();
479 if (!cache_tree_fully_valid(active_cache_tree
))
480 if (cache_tree_update(&the_index
, 0))
481 return error(_("unable to update cache tree\n"));
483 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
487 * Read the author-script file into an environment block, ready for use in
488 * run_command(), that can be free()d afterwards.
490 static char **read_author_script(void)
492 struct strbuf script
= STRBUF_INIT
;
497 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
500 for (p
= script
.buf
; *p
; p
++)
501 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
502 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
504 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
505 else if (*p
== '\n') {
510 env_size
= (count
+ 1) * sizeof(*env
);
511 strbuf_grow(&script
, env_size
);
512 memmove(script
.buf
+ env_size
, script
.buf
, script
.len
);
513 p
= script
.buf
+ env_size
;
514 env
= (char **)strbuf_detach(&script
, NULL
);
516 for (i
= 0; i
< count
; i
++) {
525 static const char staged_changes_advice
[] =
526 N_("you have staged changes in your working tree\n"
527 "If these changes are meant to be squashed into the previous commit, run:\n"
529 " git commit --amend %s\n"
531 "If they are meant to go into a new commit, run:\n"
535 "In both cases, once you're done, continue with:\n"
537 " git rebase --continue\n");
540 * If we are cherry-pick, and if the merge did not result in
541 * hand-editing, we will hit this commit and inherit the original
542 * author date and name.
544 * If we are revert, or if our cherry-pick results in a hand merge,
545 * we had better say that the current user is responsible for that.
547 * An exception is when run_git_commit() is called during an
548 * interactive rebase: in that case, we will want to retain the
551 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
552 int allow_empty
, int edit
, int amend
,
553 int cleanup_commit_message
)
556 struct argv_array array
;
560 if (is_rebase_i(opts
)) {
561 env
= read_author_script();
563 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
565 return error(_(staged_changes_advice
),
570 argv_array_init(&array
);
571 argv_array_push(&array
, "commit");
572 argv_array_push(&array
, "-n");
575 argv_array_push(&array
, "--amend");
577 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
579 argv_array_push(&array
, "-s");
581 argv_array_pushl(&array
, "-F", defmsg
, NULL
);
582 if (cleanup_commit_message
)
583 argv_array_push(&array
, "--cleanup=strip");
585 argv_array_push(&array
, "-e");
586 else if (!cleanup_commit_message
&&
587 !opts
->signoff
&& !opts
->record_origin
&&
588 git_config_get_value("commit.cleanup", &value
))
589 argv_array_push(&array
, "--cleanup=verbatim");
592 argv_array_push(&array
, "--allow-empty");
594 if (opts
->allow_empty_message
)
595 argv_array_push(&array
, "--allow-empty-message");
597 rc
= run_command_v_opt_cd_env(array
.argv
, RUN_GIT_CMD
, NULL
,
598 (const char *const *)env
);
599 argv_array_clear(&array
);
605 static int is_original_commit_empty(struct commit
*commit
)
607 const unsigned char *ptree_sha1
;
609 if (parse_commit(commit
))
610 return error(_("could not parse commit %s\n"),
611 oid_to_hex(&commit
->object
.oid
));
612 if (commit
->parents
) {
613 struct commit
*parent
= commit
->parents
->item
;
614 if (parse_commit(parent
))
615 return error(_("could not parse parent commit %s\n"),
616 oid_to_hex(&parent
->object
.oid
));
617 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
619 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
622 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
626 * Do we run "git commit" with "--allow-empty"?
628 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
630 int index_unchanged
, empty_commit
;
635 * (1) we do not allow empty at all and error out.
637 * (2) we allow ones that were initially empty, but
638 * forbid the ones that become empty;
642 if (!opts
->allow_empty
)
643 return 0; /* let "git commit" barf as necessary */
645 index_unchanged
= is_index_unchanged();
646 if (index_unchanged
< 0)
647 return index_unchanged
;
648 if (!index_unchanged
)
649 return 0; /* we do not have to say --allow-empty */
651 if (opts
->keep_redundant_commits
)
654 empty_commit
= is_original_commit_empty(commit
);
655 if (empty_commit
< 0)
664 * Note that ordering matters in this enum. Not only must it match the mapping
665 * below, it is also divided into several sections that matter. When adding
666 * new commands, make sure you add it in the right section.
669 /* commands that handle commits */
675 /* commands that do something else than handling a single commit */
677 /* commands that do nothing but are counted for reporting progress */
681 static const char *todo_command_strings
[] = {
691 static const char *command_to_string(const enum todo_command command
)
693 if ((size_t)command
< ARRAY_SIZE(todo_command_strings
))
694 return todo_command_strings
[command
];
695 die("Unknown command: %d", command
);
698 static int is_noop(const enum todo_command command
)
700 return TODO_NOOP
<= (size_t)command
;
703 static int is_fixup(enum todo_command command
)
705 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
708 static int update_squash_messages(enum todo_command command
,
709 struct commit
*commit
, struct replay_opts
*opts
)
711 struct strbuf buf
= STRBUF_INIT
;
713 const char *message
, *body
;
715 if (file_exists(rebase_path_squash_msg())) {
716 struct strbuf header
= STRBUF_INIT
;
719 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 2048) <= 0)
720 return error(_("could not read '%s'"),
721 rebase_path_squash_msg());
724 eol
= strchrnul(buf
.buf
, '\n');
725 if (buf
.buf
[0] != comment_line_char
||
726 (p
+= strcspn(p
, "0123456789\n")) == eol
)
727 return error(_("unexpected 1st line of squash message:"
729 (int)(eol
- buf
.buf
), buf
.buf
);
730 count
= strtol(p
, NULL
, 10);
733 return error(_("invalid 1st line of squash message:\n"
735 (int)(eol
- buf
.buf
), buf
.buf
);
737 strbuf_addf(&header
, "%c ", comment_line_char
);
739 _("This is a combination of %d commits."), ++count
);
740 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
741 strbuf_release(&header
);
743 unsigned char head
[20];
744 struct commit
*head_commit
;
745 const char *head_message
, *body
;
747 if (get_sha1("HEAD", head
))
748 return error(_("need a HEAD to fixup"));
749 if (!(head_commit
= lookup_commit_reference(head
)))
750 return error(_("could not read HEAD"));
751 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
752 return error(_("could not read HEAD's commit message"));
754 find_commit_subject(head_message
, &body
);
755 if (write_message(body
, strlen(body
),
756 rebase_path_fixup_msg(), 0)) {
757 unuse_commit_buffer(head_commit
, head_message
);
758 return error(_("cannot write '%s'"),
759 rebase_path_fixup_msg());
763 strbuf_addf(&buf
, "%c ", comment_line_char
);
764 strbuf_addf(&buf
, _("This is a combination of %d commits."),
766 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
767 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
768 strbuf_addstr(&buf
, "\n\n");
769 strbuf_addstr(&buf
, body
);
771 unuse_commit_buffer(head_commit
, head_message
);
774 if (!(message
= get_commit_buffer(commit
, NULL
)))
775 return error(_("could not read commit message of %s"),
776 oid_to_hex(&commit
->object
.oid
));
777 find_commit_subject(message
, &body
);
779 if (command
== TODO_SQUASH
) {
780 unlink(rebase_path_fixup_msg());
781 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
782 strbuf_addf(&buf
, _("This is the commit message #%d:"), count
);
783 strbuf_addstr(&buf
, "\n\n");
784 strbuf_addstr(&buf
, body
);
785 } else if (command
== TODO_FIXUP
) {
786 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
787 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
789 strbuf_addstr(&buf
, "\n\n");
790 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
792 return error(_("unknown command: %d"), command
);
793 unuse_commit_buffer(commit
, message
);
795 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
796 strbuf_release(&buf
);
800 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
801 struct replay_opts
*opts
, int final_fixup
)
803 int edit
= opts
->edit
, cleanup_commit_message
= 0;
804 const char *msg_file
= edit
? NULL
: git_path_merge_msg();
805 unsigned char head
[20];
806 struct commit
*base
, *next
, *parent
;
807 const char *base_label
, *next_label
;
808 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
809 struct strbuf msgbuf
= STRBUF_INIT
;
810 int res
, unborn
= 0, amend
= 0, allow
;
812 if (opts
->no_commit
) {
814 * We do not intend to commit immediately. We just want to
815 * merge the differences in, so let's compute the tree
816 * that represents the "current" state for merge-recursive
819 if (write_cache_as_tree(head
, 0, NULL
))
820 return error(_("your index file is unmerged."));
822 unborn
= get_sha1("HEAD", head
);
824 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
825 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0, 0))
826 return error_dirty_index(opts
);
830 if (!commit
->parents
)
832 else if (commit
->parents
->next
) {
833 /* Reverting or cherry-picking a merge commit */
835 struct commit_list
*p
;
838 return error(_("commit %s is a merge but no -m option was given."),
839 oid_to_hex(&commit
->object
.oid
));
841 for (cnt
= 1, p
= commit
->parents
;
842 cnt
!= opts
->mainline
&& p
;
845 if (cnt
!= opts
->mainline
|| !p
)
846 return error(_("commit %s does not have parent %d"),
847 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
849 } else if (0 < opts
->mainline
)
850 return error(_("mainline was specified but commit %s is not a merge."),
851 oid_to_hex(&commit
->object
.oid
));
853 parent
= commit
->parents
->item
;
855 if (opts
->allow_ff
&& !is_fixup(command
) &&
856 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
857 (!parent
&& unborn
)))
858 return fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
, opts
);
860 if (parent
&& parse_commit(parent
) < 0)
861 /* TRANSLATORS: The first %s will be a "todo" command like
862 "revert" or "pick", the second %s a SHA1. */
863 return error(_("%s: cannot parse parent commit %s"),
864 command_to_string(command
),
865 oid_to_hex(&parent
->object
.oid
));
867 if (get_message(commit
, &msg
) != 0)
868 return error(_("cannot get commit message for %s"),
869 oid_to_hex(&commit
->object
.oid
));
872 * "commit" is an existing commit. We would want to apply
873 * the difference it introduces since its first parent "prev"
874 * on top of the current HEAD if we are cherry-pick. Or the
875 * reverse of it if we are revert.
878 if (command
== TODO_REVERT
) {
880 base_label
= msg
.label
;
882 next_label
= msg
.parent_label
;
883 strbuf_addstr(&msgbuf
, "Revert \"");
884 strbuf_addstr(&msgbuf
, msg
.subject
);
885 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
886 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
888 if (commit
->parents
&& commit
->parents
->next
) {
889 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
890 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
892 strbuf_addstr(&msgbuf
, ".\n");
897 base_label
= msg
.parent_label
;
899 next_label
= msg
.label
;
901 /* Append the commit log message to msgbuf. */
902 if (find_commit_subject(msg
.message
, &p
))
903 strbuf_addstr(&msgbuf
, p
);
905 if (opts
->record_origin
) {
906 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
907 strbuf_addch(&msgbuf
, '\n');
908 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
909 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
910 strbuf_addstr(&msgbuf
, ")\n");
914 if (is_fixup(command
)) {
915 if (update_squash_messages(command
, commit
, opts
))
919 msg_file
= rebase_path_squash_msg();
920 else if (file_exists(rebase_path_fixup_msg())) {
921 cleanup_commit_message
= 1;
922 msg_file
= rebase_path_fixup_msg();
924 const char *dest
= git_path("SQUASH_MSG");
926 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
927 return error(_("could not rename '%s' to '%s'"),
928 rebase_path_squash_msg(), dest
);
929 unlink(git_path("MERGE_MSG"));
935 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
936 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
937 head
, &msgbuf
, opts
);
940 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
941 git_path_merge_msg(), 0);
943 struct commit_list
*common
= NULL
;
944 struct commit_list
*remotes
= NULL
;
946 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
947 git_path_merge_msg(), 0);
949 commit_list_insert(base
, &common
);
950 commit_list_insert(next
, &remotes
);
951 res
|= try_merge_command(opts
->strategy
,
952 opts
->xopts_nr
, (const char **)opts
->xopts
,
953 common
, sha1_to_hex(head
), remotes
);
954 free_commit_list(common
);
955 free_commit_list(remotes
);
957 strbuf_release(&msgbuf
);
960 * If the merge was clean or if it failed due to conflict, we write
961 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
962 * However, if the merge did not even start, then we don't want to
965 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
966 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
967 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
969 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
970 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
971 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
975 error(command
== TODO_REVERT
976 ? _("could not revert %s... %s")
977 : _("could not apply %s... %s"),
978 short_commit_name(commit
), msg
.subject
);
979 print_advice(res
== 1, opts
);
980 rerere(opts
->allow_rerere_auto
);
984 allow
= allow_empty(opts
, commit
);
989 if (!opts
->no_commit
)
990 res
= run_git_commit(msg_file
, opts
, allow
, edit
, amend
,
991 cleanup_commit_message
);
993 if (!res
&& final_fixup
) {
994 unlink(rebase_path_fixup_msg());
995 unlink(rebase_path_squash_msg());
999 free_message(commit
, &msg
);
1000 update_abort_safety_file();
1005 static int prepare_revs(struct replay_opts
*opts
)
1008 * picking (but not reverting) ranges (but not individual revisions)
1009 * should be done in reverse
1011 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1012 opts
->revs
->reverse
^= 1;
1014 if (prepare_revision_walk(opts
->revs
))
1015 return error(_("revision walk setup failed"));
1017 if (!opts
->revs
->commits
)
1018 return error(_("empty commit set passed"));
1022 static int read_and_refresh_cache(struct replay_opts
*opts
)
1024 static struct lock_file index_lock
;
1025 int index_fd
= hold_locked_index(&index_lock
, 0);
1026 if (read_index_preload(&the_index
, NULL
) < 0) {
1027 rollback_lock_file(&index_lock
);
1028 return error(_("git %s: failed to read the index"),
1029 _(action_name(opts
)));
1031 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1032 if (the_index
.cache_changed
&& index_fd
>= 0) {
1033 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
1034 rollback_lock_file(&index_lock
);
1035 return error(_("git %s: failed to refresh the index"),
1036 _(action_name(opts
)));
1039 rollback_lock_file(&index_lock
);
1044 enum todo_command command
;
1045 struct commit
*commit
;
1048 size_t offset_in_buf
;
1053 struct todo_item
*items
;
1054 int nr
, alloc
, current
;
1057 #define TODO_LIST_INIT { STRBUF_INIT }
1059 static void todo_list_release(struct todo_list
*todo_list
)
1061 strbuf_release(&todo_list
->buf
);
1062 free(todo_list
->items
);
1063 todo_list
->items
= NULL
;
1064 todo_list
->nr
= todo_list
->alloc
= 0;
1067 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1069 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1070 return todo_list
->items
+ todo_list
->nr
++;
1073 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1075 unsigned char commit_sha1
[20];
1076 char *end_of_object_name
;
1077 int i
, saved
, status
, padding
;
1080 bol
+= strspn(bol
, " \t");
1082 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1083 item
->command
= TODO_NOOP
;
1084 item
->commit
= NULL
;
1086 item
->arg_len
= eol
- bol
;
1090 for (i
= 0; i
< ARRAY_SIZE(todo_command_strings
); i
++)
1091 if (skip_prefix(bol
, todo_command_strings
[i
], &bol
)) {
1095 if (i
>= ARRAY_SIZE(todo_command_strings
))
1098 if (item
->command
== TODO_NOOP
) {
1099 item
->commit
= NULL
;
1101 item
->arg_len
= eol
- bol
;
1105 /* Eat up extra spaces/ tabs before object name */
1106 padding
= strspn(bol
, " \t");
1111 if (item
->command
== TODO_EXEC
) {
1113 item
->arg_len
= (int)(eol
- bol
);
1117 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
1118 saved
= *end_of_object_name
;
1119 *end_of_object_name
= '\0';
1120 status
= get_sha1(bol
, commit_sha1
);
1121 *end_of_object_name
= saved
;
1123 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
1124 item
->arg_len
= (int)(eol
- item
->arg
);
1129 item
->commit
= lookup_commit_reference(commit_sha1
);
1130 return !item
->commit
;
1133 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
1135 struct todo_item
*item
;
1136 char *p
= buf
, *next_p
;
1137 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
1139 for (i
= 1; *p
; i
++, p
= next_p
) {
1140 char *eol
= strchrnul(p
, '\n');
1142 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
1144 if (p
!= eol
&& eol
[-1] == '\r')
1145 eol
--; /* strip Carriage Return */
1147 item
= append_new_todo(todo_list
);
1148 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
1149 if (parse_insn_line(item
, p
, eol
)) {
1150 res
= error(_("invalid line %d: %.*s"),
1151 i
, (int)(eol
- p
), p
);
1152 item
->command
= TODO_NOOP
;
1157 else if (is_fixup(item
->command
))
1158 return error(_("cannot '%s' without a previous commit"),
1159 command_to_string(item
->command
));
1160 else if (!is_noop(item
->command
))
1164 return error(_("no commits parsed."));
1168 static int read_populate_todo(struct todo_list
*todo_list
,
1169 struct replay_opts
*opts
)
1171 const char *todo_file
= get_todo_path(opts
);
1174 strbuf_reset(&todo_list
->buf
);
1175 fd
= open(todo_file
, O_RDONLY
);
1177 return error_errno(_("could not open '%s'"), todo_file
);
1178 if (strbuf_read(&todo_list
->buf
, fd
, 0) < 0) {
1180 return error(_("could not read '%s'."), todo_file
);
1184 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
1186 return error(_("unusable instruction sheet: '%s'"), todo_file
);
1188 if (!is_rebase_i(opts
)) {
1189 enum todo_command valid
=
1190 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
1193 for (i
= 0; i
< todo_list
->nr
; i
++)
1194 if (valid
== todo_list
->items
[i
].command
)
1196 else if (valid
== TODO_PICK
)
1197 return error(_("cannot cherry-pick during a revert."));
1199 return error(_("cannot revert during a cherry-pick."));
1205 static int git_config_string_dup(char **dest
,
1206 const char *var
, const char *value
)
1209 return config_error_nonbool(var
);
1211 *dest
= xstrdup(value
);
1215 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
1217 struct replay_opts
*opts
= data
;
1222 else if (!strcmp(key
, "options.no-commit"))
1223 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
1224 else if (!strcmp(key
, "options.edit"))
1225 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
1226 else if (!strcmp(key
, "options.signoff"))
1227 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
1228 else if (!strcmp(key
, "options.record-origin"))
1229 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
1230 else if (!strcmp(key
, "options.allow-ff"))
1231 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
1232 else if (!strcmp(key
, "options.mainline"))
1233 opts
->mainline
= git_config_int(key
, value
);
1234 else if (!strcmp(key
, "options.strategy"))
1235 git_config_string_dup(&opts
->strategy
, key
, value
);
1236 else if (!strcmp(key
, "options.gpg-sign"))
1237 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
1238 else if (!strcmp(key
, "options.strategy-option")) {
1239 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
1240 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
1242 return error(_("invalid key: %s"), key
);
1245 return error(_("invalid value for %s: %s"), key
, value
);
1250 static int read_populate_opts(struct replay_opts
*opts
)
1252 if (is_rebase_i(opts
)) {
1253 struct strbuf buf
= STRBUF_INIT
;
1255 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
1256 if (!starts_with(buf
.buf
, "-S"))
1259 free(opts
->gpg_sign
);
1260 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
1263 strbuf_release(&buf
);
1265 if (file_exists(rebase_path_verbose()))
1271 if (!file_exists(git_path_opts_file()))
1274 * The function git_parse_source(), called from git_config_from_file(),
1275 * may die() in case of a syntactically incorrect file. We do not care
1276 * about this case, though, because we wrote that file ourselves, so we
1277 * are pretty certain that it is syntactically correct.
1279 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
1280 return error(_("malformed options sheet: '%s'"),
1281 git_path_opts_file());
1285 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
1286 struct replay_opts
*opts
)
1288 enum todo_command command
= opts
->action
== REPLAY_PICK
?
1289 TODO_PICK
: TODO_REVERT
;
1290 const char *command_string
= todo_command_strings
[command
];
1291 struct commit
*commit
;
1293 if (prepare_revs(opts
))
1296 while ((commit
= get_revision(opts
->revs
))) {
1297 struct todo_item
*item
= append_new_todo(todo_list
);
1298 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1299 const char *subject
;
1302 item
->command
= command
;
1303 item
->commit
= commit
;
1306 item
->offset_in_buf
= todo_list
->buf
.len
;
1307 subject_len
= find_commit_subject(commit_buffer
, &subject
);
1308 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
1309 short_commit_name(commit
), subject_len
, subject
);
1310 unuse_commit_buffer(commit
, commit_buffer
);
1315 static int create_seq_dir(void)
1317 if (file_exists(git_path_seq_dir())) {
1318 error(_("a cherry-pick or revert is already in progress"));
1319 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
1321 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
1322 return error_errno(_("could not create sequencer directory '%s'"),
1323 git_path_seq_dir());
1327 static int save_head(const char *head
)
1329 static struct lock_file head_lock
;
1330 struct strbuf buf
= STRBUF_INIT
;
1333 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
1335 rollback_lock_file(&head_lock
);
1336 return error_errno(_("could not lock HEAD"));
1338 strbuf_addf(&buf
, "%s\n", head
);
1339 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
1340 rollback_lock_file(&head_lock
);
1341 return error_errno(_("could not write to '%s'"),
1342 git_path_head_file());
1344 if (commit_lock_file(&head_lock
) < 0) {
1345 rollback_lock_file(&head_lock
);
1346 return error(_("failed to finalize '%s'."), git_path_head_file());
1351 static int rollback_is_safe(void)
1353 struct strbuf sb
= STRBUF_INIT
;
1354 struct object_id expected_head
, actual_head
;
1356 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
1358 if (get_oid_hex(sb
.buf
, &expected_head
)) {
1359 strbuf_release(&sb
);
1360 die(_("could not parse %s"), git_path_abort_safety_file());
1362 strbuf_release(&sb
);
1364 else if (errno
== ENOENT
)
1365 oidclr(&expected_head
);
1367 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
1369 if (get_oid("HEAD", &actual_head
))
1370 oidclr(&actual_head
);
1372 return !oidcmp(&actual_head
, &expected_head
);
1375 static int reset_for_rollback(const unsigned char *sha1
)
1377 const char *argv
[4]; /* reset --merge <arg> + NULL */
1380 argv
[1] = "--merge";
1381 argv
[2] = sha1_to_hex(sha1
);
1383 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1386 static int rollback_single_pick(void)
1388 unsigned char head_sha1
[20];
1390 if (!file_exists(git_path_cherry_pick_head()) &&
1391 !file_exists(git_path_revert_head()))
1392 return error(_("no cherry-pick or revert in progress"));
1393 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
1394 return error(_("cannot resolve HEAD"));
1395 if (is_null_sha1(head_sha1
))
1396 return error(_("cannot abort from a branch yet to be born"));
1397 return reset_for_rollback(head_sha1
);
1400 int sequencer_rollback(struct replay_opts
*opts
)
1403 unsigned char sha1
[20];
1404 struct strbuf buf
= STRBUF_INIT
;
1406 f
= fopen(git_path_head_file(), "r");
1407 if (!f
&& errno
== ENOENT
) {
1409 * There is no multiple-cherry-pick in progress.
1410 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
1411 * a single-cherry-pick in progress, abort that.
1413 return rollback_single_pick();
1416 return error_errno(_("cannot open '%s'"), git_path_head_file());
1417 if (strbuf_getline_lf(&buf
, f
)) {
1418 error(_("cannot read '%s': %s"), git_path_head_file(),
1419 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
1424 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
1425 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
1426 git_path_head_file());
1429 if (is_null_sha1(sha1
)) {
1430 error(_("cannot abort from a branch yet to be born"));
1434 if (!rollback_is_safe()) {
1435 /* Do not error, just do not rollback */
1436 warning(_("You seem to have moved HEAD. "
1437 "Not rewinding, check your HEAD!"));
1439 if (reset_for_rollback(sha1
))
1441 strbuf_release(&buf
);
1442 return sequencer_remove_state(opts
);
1444 strbuf_release(&buf
);
1448 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1450 static struct lock_file todo_lock
;
1451 const char *todo_path
= get_todo_path(opts
);
1452 int next
= todo_list
->current
, offset
, fd
;
1455 * rebase -i writes "git-rebase-todo" without the currently executing
1456 * command, appending it to "done" instead.
1458 if (is_rebase_i(opts
))
1461 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
1463 return error_errno(_("could not lock '%s'"), todo_path
);
1464 offset
= next
< todo_list
->nr
?
1465 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
1466 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
1467 todo_list
->buf
.len
- offset
) < 0)
1468 return error_errno(_("could not write to '%s'"), todo_path
);
1469 if (commit_lock_file(&todo_lock
) < 0)
1470 return error(_("failed to finalize '%s'."), todo_path
);
1472 if (is_rebase_i(opts
)) {
1473 const char *done_path
= rebase_path_done();
1474 int fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
1475 int prev_offset
= !next
? 0 :
1476 todo_list
->items
[next
- 1].offset_in_buf
;
1478 if (fd
>= 0 && offset
> prev_offset
&&
1479 write_in_full(fd
, todo_list
->buf
.buf
+ prev_offset
,
1480 offset
- prev_offset
) < 0) {
1482 return error_errno(_("could not write to '%s'"),
1491 static int save_opts(struct replay_opts
*opts
)
1493 const char *opts_file
= git_path_opts_file();
1496 if (opts
->no_commit
)
1497 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
1499 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
1501 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
1502 if (opts
->record_origin
)
1503 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
1505 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
1506 if (opts
->mainline
) {
1507 struct strbuf buf
= STRBUF_INIT
;
1508 strbuf_addf(&buf
, "%d", opts
->mainline
);
1509 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
1510 strbuf_release(&buf
);
1513 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
1515 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
1518 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1519 res
|= git_config_set_multivar_in_file_gently(opts_file
,
1520 "options.strategy-option",
1521 opts
->xopts
[i
], "^$", 0);
1526 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
1528 struct strbuf buf
= STRBUF_INIT
;
1529 struct rev_info log_tree_opt
;
1530 const char *subject
, *p
;
1533 p
= short_commit_name(commit
);
1534 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
1537 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
1538 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
1539 init_revisions(&log_tree_opt
, NULL
);
1540 log_tree_opt
.abbrev
= 0;
1541 log_tree_opt
.diff
= 1;
1542 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1543 log_tree_opt
.disable_stdin
= 1;
1544 log_tree_opt
.no_commit_id
= 1;
1545 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
1546 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1547 if (!log_tree_opt
.diffopt
.file
)
1548 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
1550 res
|= log_tree_commit(&log_tree_opt
, commit
);
1551 fclose(log_tree_opt
.diffopt
.file
);
1555 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
1556 if (!file_exists(buf
.buf
)) {
1557 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1558 find_commit_subject(commit_buffer
, &subject
);
1559 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
1560 unuse_commit_buffer(commit
, commit_buffer
);
1562 strbuf_release(&buf
);
1567 static int intend_to_amend(void)
1569 unsigned char head
[20];
1572 if (get_sha1("HEAD", head
))
1573 return error(_("cannot read HEAD"));
1575 p
= sha1_to_hex(head
);
1576 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
1579 static int error_with_patch(struct commit
*commit
,
1580 const char *subject
, int subject_len
,
1581 struct replay_opts
*opts
, int exit_code
, int to_amend
)
1583 if (make_patch(commit
, opts
))
1587 if (intend_to_amend())
1590 fprintf(stderr
, "You can amend the commit now, with\n"
1592 " git commit --amend %s\n"
1594 "Once you are satisfied with your changes, run\n"
1596 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
1597 } else if (exit_code
)
1598 fprintf(stderr
, "Could not apply %s... %.*s\n",
1599 short_commit_name(commit
), subject_len
, subject
);
1604 static int error_failed_squash(struct commit
*commit
,
1605 struct replay_opts
*opts
, int subject_len
, const char *subject
)
1607 if (rename(rebase_path_squash_msg(), rebase_path_message()))
1608 return error(_("could not rename '%s' to '%s'"),
1609 rebase_path_squash_msg(), rebase_path_message());
1610 unlink(rebase_path_fixup_msg());
1611 unlink(git_path("MERGE_MSG"));
1612 if (copy_file(git_path("MERGE_MSG"), rebase_path_message(), 0666))
1613 return error(_("could not copy '%s' to '%s'"),
1614 rebase_path_message(), git_path("MERGE_MSG"));
1615 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
1618 static int do_exec(const char *command_line
)
1620 const char *child_argv
[] = { NULL
, NULL
};
1623 fprintf(stderr
, "Executing: %s\n", command_line
);
1624 child_argv
[0] = command_line
;
1625 status
= run_command_v_opt(child_argv
, RUN_USING_SHELL
);
1627 /* force re-reading of the cache */
1628 if (discard_cache() < 0 || read_cache() < 0)
1629 return error(_("could not read index"));
1631 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
1634 warning(_("execution failed: %s\n%s"
1635 "You can fix the problem, and then run\n"
1637 " git rebase --continue\n"
1640 dirty
? N_("and made changes to the index and/or the "
1641 "working tree\n") : "");
1643 /* command not found */
1646 warning(_("execution succeeded: %s\nbut "
1647 "left changes to the index and/or the working tree\n"
1648 "Commit or stash your changes, and then run\n"
1650 " git rebase --continue\n"
1651 "\n"), command_line
);
1658 static int is_final_fixup(struct todo_list
*todo_list
)
1660 int i
= todo_list
->current
;
1662 if (!is_fixup(todo_list
->items
[i
].command
))
1665 while (++i
< todo_list
->nr
)
1666 if (is_fixup(todo_list
->items
[i
].command
))
1668 else if (!is_noop(todo_list
->items
[i
].command
))
1673 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1677 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1679 assert(!(opts
->signoff
|| opts
->no_commit
||
1680 opts
->record_origin
|| opts
->edit
));
1681 if (read_and_refresh_cache(opts
))
1684 while (todo_list
->current
< todo_list
->nr
) {
1685 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
1686 if (save_todo(todo_list
, opts
))
1688 if (is_rebase_i(opts
)) {
1689 unlink(rebase_path_message());
1690 unlink(rebase_path_author_script());
1691 unlink(rebase_path_stopped_sha());
1692 unlink(rebase_path_amend());
1694 if (item
->command
<= TODO_SQUASH
) {
1695 res
= do_pick_commit(item
->command
, item
->commit
,
1696 opts
, is_final_fixup(todo_list
));
1697 if (item
->command
== TODO_EDIT
) {
1698 struct commit
*commit
= item
->commit
;
1700 warning(_("stopped at %s... %.*s"),
1701 short_commit_name(commit
),
1702 item
->arg_len
, item
->arg
);
1703 return error_with_patch(commit
,
1704 item
->arg
, item
->arg_len
, opts
, res
,
1707 if (res
&& is_fixup(item
->command
)) {
1710 return error_failed_squash(item
->commit
, opts
,
1711 item
->arg_len
, item
->arg
);
1713 } else if (item
->command
== TODO_EXEC
) {
1714 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
1715 int saved
= *end_of_arg
;
1718 res
= do_exec(item
->arg
);
1719 *end_of_arg
= saved
;
1720 } else if (!is_noop(item
->command
))
1721 return error(_("unknown command %d"), item
->command
);
1723 todo_list
->current
++;
1728 if (is_rebase_i(opts
)) {
1729 struct strbuf buf
= STRBUF_INIT
;
1731 /* Stopped in the middle, as planned? */
1732 if (todo_list
->current
< todo_list
->nr
)
1735 if (opts
->verbose
) {
1736 struct rev_info log_tree_opt
;
1737 struct object_id orig
, head
;
1739 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
1740 init_revisions(&log_tree_opt
, NULL
);
1741 log_tree_opt
.diff
= 1;
1742 log_tree_opt
.diffopt
.output_format
=
1743 DIFF_FORMAT_DIFFSTAT
;
1744 log_tree_opt
.disable_stdin
= 1;
1746 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
1747 !get_sha1(buf
.buf
, orig
.hash
) &&
1748 !get_sha1("HEAD", head
.hash
)) {
1749 diff_tree_sha1(orig
.hash
, head
.hash
,
1750 "", &log_tree_opt
.diffopt
);
1751 log_tree_diff_flush(&log_tree_opt
);
1754 strbuf_release(&buf
);
1758 * Sequence of picks finished successfully; cleanup by
1759 * removing the .git/sequencer directory
1761 return sequencer_remove_state(opts
);
1764 static int continue_single_pick(void)
1766 const char *argv
[] = { "commit", NULL
};
1768 if (!file_exists(git_path_cherry_pick_head()) &&
1769 !file_exists(git_path_revert_head()))
1770 return error(_("no cherry-pick or revert in progress"));
1771 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1774 int sequencer_continue(struct replay_opts
*opts
)
1776 struct todo_list todo_list
= TODO_LIST_INIT
;
1779 if (read_and_refresh_cache(opts
))
1782 if (!file_exists(get_todo_path(opts
)))
1783 return continue_single_pick();
1784 if (read_populate_opts(opts
))
1786 if ((res
= read_populate_todo(&todo_list
, opts
)))
1787 goto release_todo_list
;
1789 /* Verify that the conflict has been resolved */
1790 if (file_exists(git_path_cherry_pick_head()) ||
1791 file_exists(git_path_revert_head())) {
1792 res
= continue_single_pick();
1794 goto release_todo_list
;
1796 if (index_differs_from("HEAD", 0, 0)) {
1797 res
= error_dirty_index(opts
);
1798 goto release_todo_list
;
1800 todo_list
.current
++;
1801 res
= pick_commits(&todo_list
, opts
);
1803 todo_list_release(&todo_list
);
1807 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1809 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1810 return do_pick_commit(opts
->action
== REPLAY_PICK
?
1811 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
1814 int sequencer_pick_revisions(struct replay_opts
*opts
)
1816 struct todo_list todo_list
= TODO_LIST_INIT
;
1817 unsigned char sha1
[20];
1821 if (read_and_refresh_cache(opts
))
1824 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1825 unsigned char sha1
[20];
1826 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1828 /* This happens when using --stdin. */
1832 if (!get_sha1(name
, sha1
)) {
1833 if (!lookup_commit_reference_gently(sha1
, 1)) {
1834 enum object_type type
= sha1_object_info(sha1
, NULL
);
1835 return error(_("%s: can't cherry-pick a %s"),
1836 name
, typename(type
));
1839 return error(_("%s: bad revision"), name
);
1843 * If we were called as "git cherry-pick <commit>", just
1844 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1845 * REVERT_HEAD, and don't touch the sequencer state.
1846 * This means it is possible to cherry-pick in the middle
1847 * of a cherry-pick sequence.
1849 if (opts
->revs
->cmdline
.nr
== 1 &&
1850 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1851 opts
->revs
->no_walk
&&
1852 !opts
->revs
->cmdline
.rev
->flags
) {
1853 struct commit
*cmit
;
1854 if (prepare_revision_walk(opts
->revs
))
1855 return error(_("revision walk setup failed"));
1856 cmit
= get_revision(opts
->revs
);
1857 if (!cmit
|| get_revision(opts
->revs
))
1858 return error("BUG: expected exactly one commit from walk");
1859 return single_pick(cmit
, opts
);
1863 * Start a new cherry-pick/ revert sequence; but
1864 * first, make sure that an existing one isn't in
1868 if (walk_revs_populate_todo(&todo_list
, opts
) ||
1869 create_seq_dir() < 0)
1871 if (get_sha1("HEAD", sha1
) && (opts
->action
== REPLAY_REVERT
))
1872 return error(_("can't revert as initial commit"));
1873 if (save_head(sha1_to_hex(sha1
)))
1875 if (save_opts(opts
))
1877 update_abort_safety_file();
1878 res
= pick_commits(&todo_list
, opts
);
1879 todo_list_release(&todo_list
);
1883 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1885 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1886 struct strbuf sob
= STRBUF_INIT
;
1889 strbuf_addstr(&sob
, sign_off_header
);
1890 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1891 getenv("GIT_COMMITTER_EMAIL")));
1892 strbuf_addch(&sob
, '\n');
1895 * If the whole message buffer is equal to the sob, pretend that we
1896 * found a conforming footer with a matching sob
1898 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1899 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1902 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1905 const char *append_newlines
= NULL
;
1906 size_t len
= msgbuf
->len
- ignore_footer
;
1910 * The buffer is completely empty. Leave foom for
1911 * the title and body to be filled in by the user.
1913 append_newlines
= "\n\n";
1914 } else if (msgbuf
->buf
[len
- 1] != '\n') {
1916 * Incomplete line. Complete the line and add a
1917 * blank one so that there is an empty line between
1918 * the message body and the sob.
1920 append_newlines
= "\n\n";
1921 } else if (len
== 1) {
1923 * Buffer contains a single newline. Add another
1924 * so that we leave room for the title and body.
1926 append_newlines
= "\n";
1927 } else if (msgbuf
->buf
[len
- 2] != '\n') {
1929 * Buffer ends with a single newline. Add another
1930 * so that there is an empty line between the message
1933 append_newlines
= "\n";
1934 } /* else, the buffer already ends with two newlines. */
1936 if (append_newlines
)
1937 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1938 append_newlines
, strlen(append_newlines
));
1941 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1942 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1945 strbuf_release(&sob
);