9 #include "run-command.h"
12 #include "cache-tree.h"
16 #include "merge-recursive.h"
18 #include "argv-array.h"
22 #include "wt-status.h"
24 #include "notes-utils.h"
27 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
29 const char sign_off_header
[] = "Signed-off-by: ";
30 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
32 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
34 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
35 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
36 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
37 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
39 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
41 * The file containing rebase commands, comments, and empty lines.
42 * This file is created by "git rebase -i" then edited by the user. As
43 * the lines are processed, they are removed from the front of this
44 * file and written to the tail of 'done'.
46 static GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
48 * The rebase command lines that have already been processed. A line
49 * is moved here when it is first handled, before any associated user
52 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
54 * The file to keep track of how many commands were already processed (e.g.
57 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum");
59 * The file to keep track of how many commands are to be processed in total
60 * (e.g. for the prompt).
62 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end");
64 * The commit message that is planned to be used for any changes that
65 * need to be committed following a user interaction.
67 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
69 * The file into which is accumulated the suggested commit message for
70 * squash/fixup commands. When the first of a series of squash/fixups
71 * is seen, the file is created and the commit message from the
72 * previous commit and from the first squash/fixup commit are written
73 * to it. The commit message for each subsequent squash/fixup commit
74 * is appended to the file as it is processed.
76 * The first line of the file is of the form
77 * # This is a combination of $count commits.
78 * where $count is the number of commits whose messages have been
79 * written to the file so far (including the initial "pick" commit).
80 * Each time that a commit message is processed, this line is read and
81 * updated. It is deleted just before the combined commit is made.
83 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
85 * If the current series of squash/fixups has not yet included a squash
86 * command, then this file exists and holds the commit message of the
87 * original "pick" commit. (If the series ends without a "squash"
88 * command, then this can be used as the commit message of the combined
89 * commit without opening the editor.)
91 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
93 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
94 * GIT_AUTHOR_DATE that will be used for the commit that is currently
97 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
99 * When an "edit" rebase command is being processed, the SHA1 of the
100 * commit to be edited is recorded in this file. When "git rebase
101 * --continue" is executed, if there are any staged changes then they
102 * will be amended to the HEAD commit, but only provided the HEAD
103 * commit is still the commit to be edited. When any other rebase
104 * command is processed, this file is deleted.
106 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
108 * When we stop at a given patch via the "edit" command, this file contains
109 * the abbreviated commit name of the corresponding patch.
111 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
113 * For the post-rewrite hook, we make a list of rewritten commits and
114 * their new sha1s. The rewritten-pending list keeps the sha1s of
115 * commits that have been processed, but not committed yet,
116 * e.g. because they are waiting for a 'squash' command.
118 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
119 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
120 "rebase-merge/rewritten-pending")
122 * The following files are written by git-rebase just after parsing the
123 * command-line (and are only consumed, not modified, by the sequencer).
125 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
126 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
127 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
128 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
129 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
130 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
131 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
132 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
133 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
135 static inline int is_rebase_i(const struct replay_opts
*opts
)
137 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
140 static const char *get_dir(const struct replay_opts
*opts
)
142 if (is_rebase_i(opts
))
143 return rebase_path();
144 return git_path_seq_dir();
147 static const char *get_todo_path(const struct replay_opts
*opts
)
149 if (is_rebase_i(opts
))
150 return rebase_path_todo();
151 return git_path_todo_file();
155 * Returns 0 for non-conforming footer
156 * Returns 1 for conforming footer
157 * Returns 2 when sob exists within conforming footer
158 * Returns 3 when sob exists within conforming footer as last entry
160 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
163 struct trailer_info info
;
165 int found_sob
= 0, found_sob_last
= 0;
167 trailer_info_get(&info
, sb
->buf
);
169 if (info
.trailer_start
== info
.trailer_end
)
172 for (i
= 0; i
< info
.trailer_nr
; i
++)
173 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
175 if (i
== info
.trailer_nr
- 1)
179 trailer_info_release(&info
);
188 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
190 static struct strbuf buf
= STRBUF_INIT
;
194 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
198 int sequencer_remove_state(struct replay_opts
*opts
)
200 struct strbuf dir
= STRBUF_INIT
;
203 free(opts
->gpg_sign
);
204 free(opts
->strategy
);
205 for (i
= 0; i
< opts
->xopts_nr
; i
++)
206 free(opts
->xopts
[i
]);
209 strbuf_addstr(&dir
, get_dir(opts
));
210 remove_dir_recursively(&dir
, 0);
211 strbuf_release(&dir
);
216 static const char *action_name(const struct replay_opts
*opts
)
218 switch (opts
->action
) {
222 return N_("cherry-pick");
223 case REPLAY_INTERACTIVE_REBASE
:
224 return N_("rebase -i");
226 die(_("Unknown action: %d"), opts
->action
);
229 struct commit_message
{
236 static const char *short_commit_name(struct commit
*commit
)
238 return find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
241 static int get_message(struct commit
*commit
, struct commit_message
*out
)
243 const char *abbrev
, *subject
;
246 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
247 abbrev
= short_commit_name(commit
);
249 subject_len
= find_commit_subject(out
->message
, &subject
);
251 out
->subject
= xmemdupz(subject
, subject_len
);
252 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
253 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
258 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
260 free(msg
->parent_label
);
263 unuse_commit_buffer(commit
, msg
->message
);
266 static void print_advice(int show_hint
, struct replay_opts
*opts
)
268 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
271 fprintf(stderr
, "%s\n", msg
);
273 * A conflict has occurred but the porcelain
274 * (typically rebase --interactive) wants to take care
275 * of the commit itself so remove CHERRY_PICK_HEAD
277 unlink(git_path_cherry_pick_head());
283 advise(_("after resolving the conflicts, mark the corrected paths\n"
284 "with 'git add <paths>' or 'git rm <paths>'"));
286 advise(_("after resolving the conflicts, mark the corrected paths\n"
287 "with 'git add <paths>' or 'git rm <paths>'\n"
288 "and commit the result with 'git commit'"));
292 static int write_message(const void *buf
, size_t len
, const char *filename
,
295 static struct lock_file msg_file
;
297 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
299 return error_errno(_("could not lock '%s'"), filename
);
300 if (write_in_full(msg_fd
, buf
, len
) < 0) {
301 rollback_lock_file(&msg_file
);
302 return error_errno(_("could not write to '%s'"), filename
);
304 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
305 rollback_lock_file(&msg_file
);
306 return error_errno(_("could not write eol to '%s'"), filename
);
308 if (commit_lock_file(&msg_file
) < 0) {
309 rollback_lock_file(&msg_file
);
310 return error(_("failed to finalize '%s'."), filename
);
317 * Reads a file that was presumably written by a shell script, i.e. with an
318 * end-of-line marker that needs to be stripped.
320 * Note that only the last end-of-line marker is stripped, consistent with the
321 * behavior of "$(cat path)" in a shell script.
323 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
325 static int read_oneliner(struct strbuf
*buf
,
326 const char *path
, int skip_if_empty
)
328 int orig_len
= buf
->len
;
330 if (!file_exists(path
))
333 if (strbuf_read_file(buf
, path
, 0) < 0) {
334 warning_errno(_("could not read '%s'"), path
);
338 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
339 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
341 buf
->buf
[buf
->len
] = '\0';
344 if (skip_if_empty
&& buf
->len
== orig_len
)
350 static struct tree
*empty_tree(void)
352 return lookup_tree(&empty_tree_oid
);
355 static int error_dirty_index(struct replay_opts
*opts
)
357 if (read_cache_unmerged())
358 return error_resolve_conflict(_(action_name(opts
)));
360 error(_("your local changes would be overwritten by %s."),
361 _(action_name(opts
)));
363 if (advice_commit_before_merge
)
364 advise(_("commit your changes or stash them to proceed."));
368 static void update_abort_safety_file(void)
370 struct object_id head
;
372 /* Do nothing on a single-pick */
373 if (!file_exists(git_path_seq_dir()))
376 if (!get_oid("HEAD", &head
))
377 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
379 write_file(git_path_abort_safety_file(), "%s", "");
382 static int fast_forward_to(const struct object_id
*to
, const struct object_id
*from
,
383 int unborn
, struct replay_opts
*opts
)
385 struct ref_transaction
*transaction
;
386 struct strbuf sb
= STRBUF_INIT
;
387 struct strbuf err
= STRBUF_INIT
;
390 if (checkout_fast_forward(from
, to
, 1))
391 return -1; /* the callee should have complained already */
393 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
395 transaction
= ref_transaction_begin(&err
);
397 ref_transaction_update(transaction
, "HEAD",
398 to
, unborn
? &null_oid
: from
,
400 ref_transaction_commit(transaction
, &err
)) {
401 ref_transaction_free(transaction
);
402 error("%s", err
.buf
);
404 strbuf_release(&err
);
409 strbuf_release(&err
);
410 ref_transaction_free(transaction
);
411 update_abort_safety_file();
415 void append_conflicts_hint(struct strbuf
*msgbuf
)
419 strbuf_addch(msgbuf
, '\n');
420 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
421 for (i
= 0; i
< active_nr
;) {
422 const struct cache_entry
*ce
= active_cache
[i
++];
424 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
425 while (i
< active_nr
&& !strcmp(ce
->name
,
426 active_cache
[i
]->name
))
432 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
433 const char *base_label
, const char *next_label
,
434 struct object_id
*head
, struct strbuf
*msgbuf
,
435 struct replay_opts
*opts
)
437 struct merge_options o
;
438 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
441 static struct lock_file index_lock
;
443 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
447 init_merge_options(&o
);
448 o
.ancestor
= base
? base_label
: "(empty tree)";
450 o
.branch2
= next
? next_label
: "(empty tree)";
451 if (is_rebase_i(opts
))
454 head_tree
= parse_tree_indirect(head
);
455 next_tree
= next
? next
->tree
: empty_tree();
456 base_tree
= base
? base
->tree
: empty_tree();
458 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
459 parse_merge_opt(&o
, *xopt
);
461 clean
= merge_trees(&o
,
463 next_tree
, base_tree
, &result
);
464 if (is_rebase_i(opts
) && clean
<= 0)
465 fputs(o
.obuf
.buf
, stdout
);
466 strbuf_release(&o
.obuf
);
470 if (active_cache_changed
&&
471 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
473 * TRANSLATORS: %s will be "revert", "cherry-pick" or
476 return error(_("%s: Unable to write new index file"),
477 _(action_name(opts
)));
478 rollback_lock_file(&index_lock
);
481 append_conflicts_hint(msgbuf
);
486 static int is_index_unchanged(void)
488 struct object_id head_oid
;
489 struct commit
*head_commit
;
491 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
492 return error(_("could not resolve HEAD commit\n"));
494 head_commit
= lookup_commit(&head_oid
);
497 * If head_commit is NULL, check_commit, called from
498 * lookup_commit, would have indicated that head_commit is not
499 * a commit object already. parse_commit() will return failure
500 * without further complaints in such a case. Otherwise, if
501 * the commit is invalid, parse_commit() will complain. So
502 * there is nothing for us to say here. Just return failure.
504 if (parse_commit(head_commit
))
507 if (!active_cache_tree
)
508 active_cache_tree
= cache_tree();
510 if (!cache_tree_fully_valid(active_cache_tree
))
511 if (cache_tree_update(&the_index
, 0))
512 return error(_("unable to update cache tree\n"));
514 return !oidcmp(&active_cache_tree
->oid
,
515 &head_commit
->tree
->object
.oid
);
518 static int write_author_script(const char *message
)
520 struct strbuf buf
= STRBUF_INIT
;
525 if (!*message
|| starts_with(message
, "\n")) {
527 /* Missing 'author' line? */
528 unlink(rebase_path_author_script());
530 } else if (skip_prefix(message
, "author ", &message
))
532 else if ((eol
= strchr(message
, '\n')))
537 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
538 while (*message
&& *message
!= '\n' && *message
!= '\r')
539 if (skip_prefix(message
, " <", &message
))
541 else if (*message
!= '\'')
542 strbuf_addch(&buf
, *(message
++));
544 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
545 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
546 while (*message
&& *message
!= '\n' && *message
!= '\r')
547 if (skip_prefix(message
, "> ", &message
))
549 else if (*message
!= '\'')
550 strbuf_addch(&buf
, *(message
++));
552 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
553 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
554 while (*message
&& *message
!= '\n' && *message
!= '\r')
555 if (*message
!= '\'')
556 strbuf_addch(&buf
, *(message
++));
558 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
559 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
560 strbuf_release(&buf
);
565 * Read a list of environment variable assignments (such as the author-script
566 * file) into an environment block. Returns -1 on error, 0 otherwise.
568 static int read_env_script(struct argv_array
*env
)
570 struct strbuf script
= STRBUF_INIT
;
574 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
577 for (p
= script
.buf
; *p
; p
++)
578 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
579 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
581 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
582 else if (*p
== '\n') {
587 for (i
= 0, p
= script
.buf
; i
< count
; i
++) {
588 argv_array_push(env
, p
);
595 static const char staged_changes_advice
[] =
596 N_("you have staged changes in your working tree\n"
597 "If these changes are meant to be squashed into the previous commit, run:\n"
599 " git commit --amend %s\n"
601 "If they are meant to go into a new commit, run:\n"
605 "In both cases, once you're done, continue with:\n"
607 " git rebase --continue\n");
609 #define ALLOW_EMPTY (1<<0)
610 #define EDIT_MSG (1<<1)
611 #define AMEND_MSG (1<<2)
612 #define CLEANUP_MSG (1<<3)
613 #define VERIFY_MSG (1<<4)
616 * If we are cherry-pick, and if the merge did not result in
617 * hand-editing, we will hit this commit and inherit the original
618 * author date and name.
620 * If we are revert, or if our cherry-pick results in a hand merge,
621 * we had better say that the current user is responsible for that.
623 * An exception is when run_git_commit() is called during an
624 * interactive rebase: in that case, we will want to retain the
627 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
630 struct child_process cmd
= CHILD_PROCESS_INIT
;
635 if (is_rebase_i(opts
)) {
636 if (!(flags
& EDIT_MSG
)) {
637 cmd
.stdout_to_stderr
= 1;
641 if (read_env_script(&cmd
.env_array
)) {
642 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
644 return error(_(staged_changes_advice
),
649 argv_array_push(&cmd
.args
, "commit");
651 if (!(flags
& VERIFY_MSG
))
652 argv_array_push(&cmd
.args
, "-n");
653 if ((flags
& AMEND_MSG
))
654 argv_array_push(&cmd
.args
, "--amend");
656 argv_array_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
658 argv_array_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
659 if ((flags
& CLEANUP_MSG
))
660 argv_array_push(&cmd
.args
, "--cleanup=strip");
661 if ((flags
& EDIT_MSG
))
662 argv_array_push(&cmd
.args
, "-e");
663 else if (!(flags
& CLEANUP_MSG
) &&
664 !opts
->signoff
&& !opts
->record_origin
&&
665 git_config_get_value("commit.cleanup", &value
))
666 argv_array_push(&cmd
.args
, "--cleanup=verbatim");
668 if ((flags
& ALLOW_EMPTY
))
669 argv_array_push(&cmd
.args
, "--allow-empty");
671 if (opts
->allow_empty_message
)
672 argv_array_push(&cmd
.args
, "--allow-empty-message");
675 /* hide stderr on success */
676 struct strbuf buf
= STRBUF_INIT
;
677 int rc
= pipe_command(&cmd
,
679 /* stdout is already redirected */
683 fputs(buf
.buf
, stderr
);
684 strbuf_release(&buf
);
688 return run_command(&cmd
);
691 static int rest_is_empty(const struct strbuf
*sb
, int start
)
696 /* Check if the rest is just whitespace and Signed-off-by's. */
697 for (i
= start
; i
< sb
->len
; i
++) {
698 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
704 if (strlen(sign_off_header
) <= eol
- i
&&
705 starts_with(sb
->buf
+ i
, sign_off_header
)) {
710 if (!isspace(sb
->buf
[i
++]))
718 * Find out if the message in the strbuf contains only whitespace and
719 * Signed-off-by lines.
721 int message_is_empty(const struct strbuf
*sb
,
722 enum commit_msg_cleanup_mode cleanup_mode
)
724 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
726 return rest_is_empty(sb
, 0);
730 * See if the user edited the message in the editor or left what
731 * was in the template intact
733 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
734 enum commit_msg_cleanup_mode cleanup_mode
)
736 struct strbuf tmpl
= STRBUF_INIT
;
739 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
742 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
745 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
746 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
748 strbuf_release(&tmpl
);
749 return rest_is_empty(sb
, start
- sb
->buf
);
752 int update_head_with_reflog(const struct commit
*old_head
,
753 const struct object_id
*new_head
,
754 const char *action
, const struct strbuf
*msg
,
757 struct ref_transaction
*transaction
;
758 struct strbuf sb
= STRBUF_INIT
;
763 strbuf_addstr(&sb
, action
);
764 strbuf_addstr(&sb
, ": ");
767 nl
= strchr(msg
->buf
, '\n');
769 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
771 strbuf_addbuf(&sb
, msg
);
772 strbuf_addch(&sb
, '\n');
775 transaction
= ref_transaction_begin(err
);
777 ref_transaction_update(transaction
, "HEAD", new_head
,
778 old_head
? &old_head
->object
.oid
: &null_oid
,
780 ref_transaction_commit(transaction
, err
)) {
783 ref_transaction_free(transaction
);
789 static int run_rewrite_hook(const struct object_id
*oldoid
,
790 const struct object_id
*newoid
)
792 struct child_process proc
= CHILD_PROCESS_INIT
;
795 struct strbuf sb
= STRBUF_INIT
;
797 argv
[0] = find_hook("post-rewrite");
806 proc
.stdout_to_stderr
= 1;
808 code
= start_command(&proc
);
811 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
812 sigchain_push(SIGPIPE
, SIG_IGN
);
813 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
816 sigchain_pop(SIGPIPE
);
817 return finish_command(&proc
);
820 void commit_post_rewrite(const struct commit
*old_head
,
821 const struct object_id
*new_head
)
823 struct notes_rewrite_cfg
*cfg
;
825 cfg
= init_copy_notes_for_rewrite("amend");
827 /* we are amending, so old_head is not NULL */
828 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
829 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
831 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
834 static const char implicit_ident_advice_noconfig
[] =
835 N_("Your name and email address were configured automatically based\n"
836 "on your username and hostname. Please check that they are accurate.\n"
837 "You can suppress this message by setting them explicitly. Run the\n"
838 "following command and follow the instructions in your editor to edit\n"
839 "your configuration file:\n"
841 " git config --global --edit\n"
843 "After doing this, you may fix the identity used for this commit with:\n"
845 " git commit --amend --reset-author\n");
847 static const char implicit_ident_advice_config
[] =
848 N_("Your name and email address were configured automatically based\n"
849 "on your username and hostname. Please check that they are accurate.\n"
850 "You can suppress this message by setting them explicitly:\n"
852 " git config --global user.name \"Your Name\"\n"
853 " git config --global user.email you@example.com\n"
855 "After doing this, you may fix the identity used for this commit with:\n"
857 " git commit --amend --reset-author\n");
859 static const char *implicit_ident_advice(void)
861 char *user_config
= expand_user_path("~/.gitconfig", 0);
862 char *xdg_config
= xdg_config_home("config");
863 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
869 return _(implicit_ident_advice_config
);
871 return _(implicit_ident_advice_noconfig
);
875 void print_commit_summary(const char *prefix
, const struct object_id
*oid
,
879 struct commit
*commit
;
880 struct strbuf format
= STRBUF_INIT
;
882 struct pretty_print_context pctx
= {0};
883 struct strbuf author_ident
= STRBUF_INIT
;
884 struct strbuf committer_ident
= STRBUF_INIT
;
886 commit
= lookup_commit(oid
);
888 die(_("couldn't look up newly created commit"));
889 if (parse_commit(commit
))
890 die(_("could not parse newly created commit"));
892 strbuf_addstr(&format
, "format:%h] %s");
894 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
895 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
896 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
897 strbuf_addstr(&format
, "\n Author: ");
898 strbuf_addbuf_percentquote(&format
, &author_ident
);
900 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
901 struct strbuf date
= STRBUF_INIT
;
903 format_commit_message(commit
, "%ad", &date
, &pctx
);
904 strbuf_addstr(&format
, "\n Date: ");
905 strbuf_addbuf_percentquote(&format
, &date
);
906 strbuf_release(&date
);
908 if (!committer_ident_sufficiently_given()) {
909 strbuf_addstr(&format
, "\n Committer: ");
910 strbuf_addbuf_percentquote(&format
, &committer_ident
);
911 if (advice_implicit_identity
) {
912 strbuf_addch(&format
, '\n');
913 strbuf_addstr(&format
, implicit_ident_advice());
916 strbuf_release(&author_ident
);
917 strbuf_release(&committer_ident
);
919 init_revisions(&rev
, prefix
);
920 setup_revisions(0, NULL
, &rev
, NULL
);
923 rev
.diffopt
.output_format
=
924 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
926 rev
.verbose_header
= 1;
927 rev
.show_root_diff
= 1;
928 get_commit_format(format
.buf
, &rev
);
929 rev
.always_show_header
= 0;
930 rev
.diffopt
.detect_rename
= 1;
931 rev
.diffopt
.break_opt
= 0;
932 diff_setup_done(&rev
.diffopt
);
934 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
936 die_errno(_("unable to resolve HEAD after creating commit"));
937 if (!strcmp(head
, "HEAD"))
938 head
= _("detached HEAD");
940 skip_prefix(head
, "refs/heads/", &head
);
941 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
942 _(" (root-commit)") : "");
944 if (!log_tree_commit(&rev
, commit
)) {
945 rev
.always_show_header
= 1;
946 rev
.use_terminator
= 1;
947 log_tree_commit(&rev
, commit
);
950 strbuf_release(&format
);
953 static int is_original_commit_empty(struct commit
*commit
)
955 const struct object_id
*ptree_oid
;
957 if (parse_commit(commit
))
958 return error(_("could not parse commit %s\n"),
959 oid_to_hex(&commit
->object
.oid
));
960 if (commit
->parents
) {
961 struct commit
*parent
= commit
->parents
->item
;
962 if (parse_commit(parent
))
963 return error(_("could not parse parent commit %s\n"),
964 oid_to_hex(&parent
->object
.oid
));
965 ptree_oid
= &parent
->tree
->object
.oid
;
967 ptree_oid
= &empty_tree_oid
; /* commit is root */
970 return !oidcmp(ptree_oid
, &commit
->tree
->object
.oid
);
974 * Do we run "git commit" with "--allow-empty"?
976 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
978 int index_unchanged
, empty_commit
;
983 * (1) we do not allow empty at all and error out.
985 * (2) we allow ones that were initially empty, but
986 * forbid the ones that become empty;
990 if (!opts
->allow_empty
)
991 return 0; /* let "git commit" barf as necessary */
993 index_unchanged
= is_index_unchanged();
994 if (index_unchanged
< 0)
995 return index_unchanged
;
996 if (!index_unchanged
)
997 return 0; /* we do not have to say --allow-empty */
999 if (opts
->keep_redundant_commits
)
1002 empty_commit
= is_original_commit_empty(commit
);
1003 if (empty_commit
< 0)
1004 return empty_commit
;
1012 * Note that ordering matters in this enum. Not only must it match the mapping
1013 * below, it is also divided into several sections that matter. When adding
1014 * new commands, make sure you add it in the right section.
1017 /* commands that handle commits */
1024 /* commands that do something else than handling a single commit */
1026 /* commands that do nothing but are counted for reporting progress */
1029 /* comments (not counted for reporting progress) */
1036 } todo_command_info
[] = {
1049 static const char *command_to_string(const enum todo_command command
)
1051 if (command
< TODO_COMMENT
)
1052 return todo_command_info
[command
].str
;
1053 die("Unknown command: %d", command
);
1056 static int is_noop(const enum todo_command command
)
1058 return TODO_NOOP
<= command
;
1061 static int is_fixup(enum todo_command command
)
1063 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1066 static int update_squash_messages(enum todo_command command
,
1067 struct commit
*commit
, struct replay_opts
*opts
)
1069 struct strbuf buf
= STRBUF_INIT
;
1071 const char *message
, *body
;
1073 if (file_exists(rebase_path_squash_msg())) {
1074 struct strbuf header
= STRBUF_INIT
;
1077 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 2048) <= 0)
1078 return error(_("could not read '%s'"),
1079 rebase_path_squash_msg());
1082 eol
= strchrnul(buf
.buf
, '\n');
1083 if (buf
.buf
[0] != comment_line_char
||
1084 (p
+= strcspn(p
, "0123456789\n")) == eol
)
1085 return error(_("unexpected 1st line of squash message:"
1087 (int)(eol
- buf
.buf
), buf
.buf
);
1088 count
= strtol(p
, NULL
, 10);
1091 return error(_("invalid 1st line of squash message:\n"
1093 (int)(eol
- buf
.buf
), buf
.buf
);
1095 strbuf_addf(&header
, "%c ", comment_line_char
);
1096 strbuf_addf(&header
,
1097 _("This is a combination of %d commits."), ++count
);
1098 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1099 strbuf_release(&header
);
1101 struct object_id head
;
1102 struct commit
*head_commit
;
1103 const char *head_message
, *body
;
1105 if (get_oid("HEAD", &head
))
1106 return error(_("need a HEAD to fixup"));
1107 if (!(head_commit
= lookup_commit_reference(&head
)))
1108 return error(_("could not read HEAD"));
1109 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
1110 return error(_("could not read HEAD's commit message"));
1112 find_commit_subject(head_message
, &body
);
1113 if (write_message(body
, strlen(body
),
1114 rebase_path_fixup_msg(), 0)) {
1115 unuse_commit_buffer(head_commit
, head_message
);
1116 return error(_("cannot write '%s'"),
1117 rebase_path_fixup_msg());
1121 strbuf_addf(&buf
, "%c ", comment_line_char
);
1122 strbuf_addf(&buf
, _("This is a combination of %d commits."),
1124 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1125 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
1126 strbuf_addstr(&buf
, "\n\n");
1127 strbuf_addstr(&buf
, body
);
1129 unuse_commit_buffer(head_commit
, head_message
);
1132 if (!(message
= get_commit_buffer(commit
, NULL
)))
1133 return error(_("could not read commit message of %s"),
1134 oid_to_hex(&commit
->object
.oid
));
1135 find_commit_subject(message
, &body
);
1137 if (command
== TODO_SQUASH
) {
1138 unlink(rebase_path_fixup_msg());
1139 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1140 strbuf_addf(&buf
, _("This is the commit message #%d:"), count
);
1141 strbuf_addstr(&buf
, "\n\n");
1142 strbuf_addstr(&buf
, body
);
1143 } else if (command
== TODO_FIXUP
) {
1144 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1145 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
1147 strbuf_addstr(&buf
, "\n\n");
1148 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1150 return error(_("unknown command: %d"), command
);
1151 unuse_commit_buffer(commit
, message
);
1153 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
1154 strbuf_release(&buf
);
1158 static void flush_rewritten_pending(void) {
1159 struct strbuf buf
= STRBUF_INIT
;
1160 struct object_id newoid
;
1163 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
1164 !get_oid("HEAD", &newoid
) &&
1165 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
1166 char *bol
= buf
.buf
, *eol
;
1169 eol
= strchrnul(bol
, '\n');
1170 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
1171 bol
, oid_to_hex(&newoid
));
1177 unlink(rebase_path_rewritten_pending());
1179 strbuf_release(&buf
);
1182 static void record_in_rewritten(struct object_id
*oid
,
1183 enum todo_command next_command
) {
1184 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
1189 fprintf(out
, "%s\n", oid_to_hex(oid
));
1192 if (!is_fixup(next_command
))
1193 flush_rewritten_pending();
1196 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
1197 struct replay_opts
*opts
, int final_fixup
)
1199 unsigned int flags
= opts
->edit
? EDIT_MSG
: 0;
1200 const char *msg_file
= opts
->edit
? NULL
: git_path_merge_msg();
1201 struct object_id head
;
1202 struct commit
*base
, *next
, *parent
;
1203 const char *base_label
, *next_label
;
1204 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
1205 struct strbuf msgbuf
= STRBUF_INIT
;
1206 int res
, unborn
= 0, allow
;
1208 if (opts
->no_commit
) {
1210 * We do not intend to commit immediately. We just want to
1211 * merge the differences in, so let's compute the tree
1212 * that represents the "current" state for merge-recursive
1215 if (write_cache_as_tree(head
.hash
, 0, NULL
))
1216 return error(_("your index file is unmerged."));
1218 unborn
= get_oid("HEAD", &head
);
1220 oidcpy(&head
, &empty_tree_oid
);
1221 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD",
1223 return error_dirty_index(opts
);
1227 if (!commit
->parents
)
1229 else if (commit
->parents
->next
) {
1230 /* Reverting or cherry-picking a merge commit */
1232 struct commit_list
*p
;
1234 if (!opts
->mainline
)
1235 return error(_("commit %s is a merge but no -m option was given."),
1236 oid_to_hex(&commit
->object
.oid
));
1238 for (cnt
= 1, p
= commit
->parents
;
1239 cnt
!= opts
->mainline
&& p
;
1242 if (cnt
!= opts
->mainline
|| !p
)
1243 return error(_("commit %s does not have parent %d"),
1244 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1246 } else if (0 < opts
->mainline
)
1247 return error(_("mainline was specified but commit %s is not a merge."),
1248 oid_to_hex(&commit
->object
.oid
));
1250 parent
= commit
->parents
->item
;
1252 if (get_message(commit
, &msg
) != 0)
1253 return error(_("cannot get commit message for %s"),
1254 oid_to_hex(&commit
->object
.oid
));
1256 if (opts
->allow_ff
&& !is_fixup(command
) &&
1257 ((parent
&& !oidcmp(&parent
->object
.oid
, &head
)) ||
1258 (!parent
&& unborn
))) {
1259 if (is_rebase_i(opts
))
1260 write_author_script(msg
.message
);
1261 res
= fast_forward_to(&commit
->object
.oid
, &head
, unborn
,
1263 if (res
|| command
!= TODO_REWORD
)
1265 flags
|= EDIT_MSG
| AMEND_MSG
;
1266 if (command
== TODO_REWORD
)
1267 flags
|= VERIFY_MSG
;
1269 goto fast_forward_edit
;
1271 if (parent
&& parse_commit(parent
) < 0)
1272 /* TRANSLATORS: The first %s will be a "todo" command like
1273 "revert" or "pick", the second %s a SHA1. */
1274 return error(_("%s: cannot parse parent commit %s"),
1275 command_to_string(command
),
1276 oid_to_hex(&parent
->object
.oid
));
1279 * "commit" is an existing commit. We would want to apply
1280 * the difference it introduces since its first parent "prev"
1281 * on top of the current HEAD if we are cherry-pick. Or the
1282 * reverse of it if we are revert.
1285 if (command
== TODO_REVERT
) {
1287 base_label
= msg
.label
;
1289 next_label
= msg
.parent_label
;
1290 strbuf_addstr(&msgbuf
, "Revert \"");
1291 strbuf_addstr(&msgbuf
, msg
.subject
);
1292 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
1293 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1295 if (commit
->parents
&& commit
->parents
->next
) {
1296 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1297 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1299 strbuf_addstr(&msgbuf
, ".\n");
1304 base_label
= msg
.parent_label
;
1306 next_label
= msg
.label
;
1308 /* Append the commit log message to msgbuf. */
1309 if (find_commit_subject(msg
.message
, &p
))
1310 strbuf_addstr(&msgbuf
, p
);
1312 if (opts
->record_origin
) {
1313 strbuf_complete_line(&msgbuf
);
1314 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1315 strbuf_addch(&msgbuf
, '\n');
1316 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1317 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1318 strbuf_addstr(&msgbuf
, ")\n");
1322 if (command
== TODO_REWORD
)
1323 flags
|= EDIT_MSG
| VERIFY_MSG
;
1324 else if (is_fixup(command
)) {
1325 if (update_squash_messages(command
, commit
, opts
))
1329 msg_file
= rebase_path_squash_msg();
1330 else if (file_exists(rebase_path_fixup_msg())) {
1331 flags
|= CLEANUP_MSG
;
1332 msg_file
= rebase_path_fixup_msg();
1334 const char *dest
= git_path_squash_msg();
1336 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1337 return error(_("could not rename '%s' to '%s'"),
1338 rebase_path_squash_msg(), dest
);
1339 unlink(git_path_merge_msg());
1346 append_signoff(&msgbuf
, 0, 0);
1348 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1350 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1351 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
1352 &head
, &msgbuf
, opts
);
1355 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
1356 git_path_merge_msg(), 0);
1358 struct commit_list
*common
= NULL
;
1359 struct commit_list
*remotes
= NULL
;
1361 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1362 git_path_merge_msg(), 0);
1364 commit_list_insert(base
, &common
);
1365 commit_list_insert(next
, &remotes
);
1366 res
|= try_merge_command(opts
->strategy
,
1367 opts
->xopts_nr
, (const char **)opts
->xopts
,
1368 common
, oid_to_hex(&head
), remotes
);
1369 free_commit_list(common
);
1370 free_commit_list(remotes
);
1372 strbuf_release(&msgbuf
);
1375 * If the merge was clean or if it failed due to conflict, we write
1376 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1377 * However, if the merge did not even start, then we don't want to
1380 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1381 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
1382 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
1384 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1385 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
1386 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
1390 error(command
== TODO_REVERT
1391 ? _("could not revert %s... %s")
1392 : _("could not apply %s... %s"),
1393 short_commit_name(commit
), msg
.subject
);
1394 print_advice(res
== 1, opts
);
1395 rerere(opts
->allow_rerere_auto
);
1399 allow
= allow_empty(opts
, commit
);
1404 flags
|= ALLOW_EMPTY
;
1405 if (!opts
->no_commit
)
1407 res
= run_git_commit(msg_file
, opts
, flags
);
1409 if (!res
&& final_fixup
) {
1410 unlink(rebase_path_fixup_msg());
1411 unlink(rebase_path_squash_msg());
1415 free_message(commit
, &msg
);
1416 update_abort_safety_file();
1421 static int prepare_revs(struct replay_opts
*opts
)
1424 * picking (but not reverting) ranges (but not individual revisions)
1425 * should be done in reverse
1427 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1428 opts
->revs
->reverse
^= 1;
1430 if (prepare_revision_walk(opts
->revs
))
1431 return error(_("revision walk setup failed"));
1433 if (!opts
->revs
->commits
)
1434 return error(_("empty commit set passed"));
1438 static int read_and_refresh_cache(struct replay_opts
*opts
)
1440 static struct lock_file index_lock
;
1441 int index_fd
= hold_locked_index(&index_lock
, 0);
1442 if (read_index_preload(&the_index
, NULL
) < 0) {
1443 rollback_lock_file(&index_lock
);
1444 return error(_("git %s: failed to read the index"),
1445 _(action_name(opts
)));
1447 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1448 if (the_index
.cache_changed
&& index_fd
>= 0) {
1449 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
1450 return error(_("git %s: failed to refresh the index"),
1451 _(action_name(opts
)));
1454 rollback_lock_file(&index_lock
);
1459 enum todo_command command
;
1460 struct commit
*commit
;
1463 size_t offset_in_buf
;
1468 struct todo_item
*items
;
1469 int nr
, alloc
, current
;
1470 int done_nr
, total_nr
;
1471 struct stat_data stat
;
1474 #define TODO_LIST_INIT { STRBUF_INIT }
1476 static void todo_list_release(struct todo_list
*todo_list
)
1478 strbuf_release(&todo_list
->buf
);
1479 FREE_AND_NULL(todo_list
->items
);
1480 todo_list
->nr
= todo_list
->alloc
= 0;
1483 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1485 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1486 return todo_list
->items
+ todo_list
->nr
++;
1489 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1491 struct object_id commit_oid
;
1492 char *end_of_object_name
;
1493 int i
, saved
, status
, padding
;
1496 bol
+= strspn(bol
, " \t");
1498 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1499 item
->command
= TODO_COMMENT
;
1500 item
->commit
= NULL
;
1502 item
->arg_len
= eol
- bol
;
1506 for (i
= 0; i
< TODO_COMMENT
; i
++)
1507 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1510 } else if (bol
[1] == ' ' && *bol
== todo_command_info
[i
].c
) {
1515 if (i
>= TODO_COMMENT
)
1518 if (item
->command
== TODO_NOOP
) {
1519 item
->commit
= NULL
;
1521 item
->arg_len
= eol
- bol
;
1525 /* Eat up extra spaces/ tabs before object name */
1526 padding
= strspn(bol
, " \t");
1531 if (item
->command
== TODO_EXEC
) {
1533 item
->arg_len
= (int)(eol
- bol
);
1537 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
1538 saved
= *end_of_object_name
;
1539 *end_of_object_name
= '\0';
1540 status
= get_oid(bol
, &commit_oid
);
1541 *end_of_object_name
= saved
;
1543 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
1544 item
->arg_len
= (int)(eol
- item
->arg
);
1549 item
->commit
= lookup_commit_reference(&commit_oid
);
1550 return !item
->commit
;
1553 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
1555 struct todo_item
*item
;
1556 char *p
= buf
, *next_p
;
1557 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
1559 for (i
= 1; *p
; i
++, p
= next_p
) {
1560 char *eol
= strchrnul(p
, '\n');
1562 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
1564 if (p
!= eol
&& eol
[-1] == '\r')
1565 eol
--; /* strip Carriage Return */
1567 item
= append_new_todo(todo_list
);
1568 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
1569 if (parse_insn_line(item
, p
, eol
)) {
1570 res
= error(_("invalid line %d: %.*s"),
1571 i
, (int)(eol
- p
), p
);
1572 item
->command
= TODO_NOOP
;
1577 else if (is_fixup(item
->command
))
1578 return error(_("cannot '%s' without a previous commit"),
1579 command_to_string(item
->command
));
1580 else if (!is_noop(item
->command
))
1587 static int count_commands(struct todo_list
*todo_list
)
1591 for (i
= 0; i
< todo_list
->nr
; i
++)
1592 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
1598 static int read_populate_todo(struct todo_list
*todo_list
,
1599 struct replay_opts
*opts
)
1602 const char *todo_file
= get_todo_path(opts
);
1605 strbuf_reset(&todo_list
->buf
);
1606 fd
= open(todo_file
, O_RDONLY
);
1608 return error_errno(_("could not open '%s'"), todo_file
);
1609 if (strbuf_read(&todo_list
->buf
, fd
, 0) < 0) {
1611 return error(_("could not read '%s'."), todo_file
);
1615 res
= stat(todo_file
, &st
);
1617 return error(_("could not stat '%s'"), todo_file
);
1618 fill_stat_data(&todo_list
->stat
, &st
);
1620 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
1622 if (is_rebase_i(opts
))
1623 return error(_("please fix this using "
1624 "'git rebase --edit-todo'."));
1625 return error(_("unusable instruction sheet: '%s'"), todo_file
);
1628 if (!todo_list
->nr
&&
1629 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
1630 return error(_("no commits parsed."));
1632 if (!is_rebase_i(opts
)) {
1633 enum todo_command valid
=
1634 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
1637 for (i
= 0; i
< todo_list
->nr
; i
++)
1638 if (valid
== todo_list
->items
[i
].command
)
1640 else if (valid
== TODO_PICK
)
1641 return error(_("cannot cherry-pick during a revert."));
1643 return error(_("cannot revert during a cherry-pick."));
1646 if (is_rebase_i(opts
)) {
1647 struct todo_list done
= TODO_LIST_INIT
;
1648 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
1650 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
1651 !parse_insn_buffer(done
.buf
.buf
, &done
))
1652 todo_list
->done_nr
= count_commands(&done
);
1654 todo_list
->done_nr
= 0;
1656 todo_list
->total_nr
= todo_list
->done_nr
1657 + count_commands(todo_list
);
1658 todo_list_release(&done
);
1661 fprintf(f
, "%d\n", todo_list
->total_nr
);
1669 static int git_config_string_dup(char **dest
,
1670 const char *var
, const char *value
)
1673 return config_error_nonbool(var
);
1675 *dest
= xstrdup(value
);
1679 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
1681 struct replay_opts
*opts
= data
;
1686 else if (!strcmp(key
, "options.no-commit"))
1687 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
1688 else if (!strcmp(key
, "options.edit"))
1689 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
1690 else if (!strcmp(key
, "options.signoff"))
1691 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
1692 else if (!strcmp(key
, "options.record-origin"))
1693 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
1694 else if (!strcmp(key
, "options.allow-ff"))
1695 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
1696 else if (!strcmp(key
, "options.mainline"))
1697 opts
->mainline
= git_config_int(key
, value
);
1698 else if (!strcmp(key
, "options.strategy"))
1699 git_config_string_dup(&opts
->strategy
, key
, value
);
1700 else if (!strcmp(key
, "options.gpg-sign"))
1701 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
1702 else if (!strcmp(key
, "options.strategy-option")) {
1703 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
1704 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
1705 } else if (!strcmp(key
, "options.allow-rerere-auto"))
1706 opts
->allow_rerere_auto
=
1707 git_config_bool_or_int(key
, value
, &error_flag
) ?
1708 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
1710 return error(_("invalid key: %s"), key
);
1713 return error(_("invalid value for %s: %s"), key
, value
);
1718 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
1723 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
1725 opts
->strategy
= strbuf_detach(buf
, NULL
);
1726 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
1729 opts
->xopts_nr
= split_cmdline(buf
->buf
, (const char ***)&opts
->xopts
);
1730 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
1731 const char *arg
= opts
->xopts
[i
];
1733 skip_prefix(arg
, "--", &arg
);
1734 opts
->xopts
[i
] = xstrdup(arg
);
1738 static int read_populate_opts(struct replay_opts
*opts
)
1740 if (is_rebase_i(opts
)) {
1741 struct strbuf buf
= STRBUF_INIT
;
1743 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
1744 if (!starts_with(buf
.buf
, "-S"))
1747 free(opts
->gpg_sign
);
1748 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
1753 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(), 1)) {
1754 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
1755 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
1756 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
1757 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
1761 if (file_exists(rebase_path_verbose()))
1764 read_strategy_opts(opts
, &buf
);
1765 strbuf_release(&buf
);
1770 if (!file_exists(git_path_opts_file()))
1773 * The function git_parse_source(), called from git_config_from_file(),
1774 * may die() in case of a syntactically incorrect file. We do not care
1775 * about this case, though, because we wrote that file ourselves, so we
1776 * are pretty certain that it is syntactically correct.
1778 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
1779 return error(_("malformed options sheet: '%s'"),
1780 git_path_opts_file());
1784 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
1785 struct replay_opts
*opts
)
1787 enum todo_command command
= opts
->action
== REPLAY_PICK
?
1788 TODO_PICK
: TODO_REVERT
;
1789 const char *command_string
= todo_command_info
[command
].str
;
1790 struct commit
*commit
;
1792 if (prepare_revs(opts
))
1795 while ((commit
= get_revision(opts
->revs
))) {
1796 struct todo_item
*item
= append_new_todo(todo_list
);
1797 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1798 const char *subject
;
1801 item
->command
= command
;
1802 item
->commit
= commit
;
1805 item
->offset_in_buf
= todo_list
->buf
.len
;
1806 subject_len
= find_commit_subject(commit_buffer
, &subject
);
1807 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
1808 short_commit_name(commit
), subject_len
, subject
);
1809 unuse_commit_buffer(commit
, commit_buffer
);
1814 static int create_seq_dir(void)
1816 if (file_exists(git_path_seq_dir())) {
1817 error(_("a cherry-pick or revert is already in progress"));
1818 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
1820 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
1821 return error_errno(_("could not create sequencer directory '%s'"),
1822 git_path_seq_dir());
1826 static int save_head(const char *head
)
1828 static struct lock_file head_lock
;
1829 struct strbuf buf
= STRBUF_INIT
;
1833 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
1835 rollback_lock_file(&head_lock
);
1836 return error_errno(_("could not lock HEAD"));
1838 strbuf_addf(&buf
, "%s\n", head
);
1839 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
1840 strbuf_release(&buf
);
1842 rollback_lock_file(&head_lock
);
1843 return error_errno(_("could not write to '%s'"),
1844 git_path_head_file());
1846 if (commit_lock_file(&head_lock
) < 0) {
1847 rollback_lock_file(&head_lock
);
1848 return error(_("failed to finalize '%s'."), git_path_head_file());
1853 static int rollback_is_safe(void)
1855 struct strbuf sb
= STRBUF_INIT
;
1856 struct object_id expected_head
, actual_head
;
1858 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
1860 if (get_oid_hex(sb
.buf
, &expected_head
)) {
1861 strbuf_release(&sb
);
1862 die(_("could not parse %s"), git_path_abort_safety_file());
1864 strbuf_release(&sb
);
1866 else if (errno
== ENOENT
)
1867 oidclr(&expected_head
);
1869 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
1871 if (get_oid("HEAD", &actual_head
))
1872 oidclr(&actual_head
);
1874 return !oidcmp(&actual_head
, &expected_head
);
1877 static int reset_for_rollback(const struct object_id
*oid
)
1879 const char *argv
[4]; /* reset --merge <arg> + NULL */
1882 argv
[1] = "--merge";
1883 argv
[2] = oid_to_hex(oid
);
1885 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1888 static int rollback_single_pick(void)
1890 struct object_id head_oid
;
1892 if (!file_exists(git_path_cherry_pick_head()) &&
1893 !file_exists(git_path_revert_head()))
1894 return error(_("no cherry-pick or revert in progress"));
1895 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
1896 return error(_("cannot resolve HEAD"));
1897 if (is_null_oid(&head_oid
))
1898 return error(_("cannot abort from a branch yet to be born"));
1899 return reset_for_rollback(&head_oid
);
1902 int sequencer_rollback(struct replay_opts
*opts
)
1905 struct object_id oid
;
1906 struct strbuf buf
= STRBUF_INIT
;
1909 f
= fopen(git_path_head_file(), "r");
1910 if (!f
&& errno
== ENOENT
) {
1912 * There is no multiple-cherry-pick in progress.
1913 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
1914 * a single-cherry-pick in progress, abort that.
1916 return rollback_single_pick();
1919 return error_errno(_("cannot open '%s'"), git_path_head_file());
1920 if (strbuf_getline_lf(&buf
, f
)) {
1921 error(_("cannot read '%s': %s"), git_path_head_file(),
1922 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
1927 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
1928 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
1929 git_path_head_file());
1932 if (is_null_oid(&oid
)) {
1933 error(_("cannot abort from a branch yet to be born"));
1937 if (!rollback_is_safe()) {
1938 /* Do not error, just do not rollback */
1939 warning(_("You seem to have moved HEAD. "
1940 "Not rewinding, check your HEAD!"));
1942 if (reset_for_rollback(&oid
))
1944 strbuf_release(&buf
);
1945 return sequencer_remove_state(opts
);
1947 strbuf_release(&buf
);
1951 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1953 static struct lock_file todo_lock
;
1954 const char *todo_path
= get_todo_path(opts
);
1955 int next
= todo_list
->current
, offset
, fd
;
1958 * rebase -i writes "git-rebase-todo" without the currently executing
1959 * command, appending it to "done" instead.
1961 if (is_rebase_i(opts
))
1964 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
1966 return error_errno(_("could not lock '%s'"), todo_path
);
1967 offset
= next
< todo_list
->nr
?
1968 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
1969 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
1970 todo_list
->buf
.len
- offset
) < 0)
1971 return error_errno(_("could not write to '%s'"), todo_path
);
1972 if (commit_lock_file(&todo_lock
) < 0)
1973 return error(_("failed to finalize '%s'."), todo_path
);
1975 if (is_rebase_i(opts
)) {
1976 const char *done_path
= rebase_path_done();
1977 int fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
1978 int prev_offset
= !next
? 0 :
1979 todo_list
->items
[next
- 1].offset_in_buf
;
1981 if (fd
>= 0 && offset
> prev_offset
&&
1982 write_in_full(fd
, todo_list
->buf
.buf
+ prev_offset
,
1983 offset
- prev_offset
) < 0) {
1985 return error_errno(_("could not write to '%s'"),
1994 static int save_opts(struct replay_opts
*opts
)
1996 const char *opts_file
= git_path_opts_file();
1999 if (opts
->no_commit
)
2000 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
2002 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
2004 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
2005 if (opts
->record_origin
)
2006 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
2008 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
2009 if (opts
->mainline
) {
2010 struct strbuf buf
= STRBUF_INIT
;
2011 strbuf_addf(&buf
, "%d", opts
->mainline
);
2012 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
2013 strbuf_release(&buf
);
2016 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
2018 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
2021 for (i
= 0; i
< opts
->xopts_nr
; i
++)
2022 res
|= git_config_set_multivar_in_file_gently(opts_file
,
2023 "options.strategy-option",
2024 opts
->xopts
[i
], "^$", 0);
2026 if (opts
->allow_rerere_auto
)
2027 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-rerere-auto",
2028 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
2033 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
2035 struct strbuf buf
= STRBUF_INIT
;
2036 struct rev_info log_tree_opt
;
2037 const char *subject
, *p
;
2040 p
= short_commit_name(commit
);
2041 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
2044 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
2045 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2046 init_revisions(&log_tree_opt
, NULL
);
2047 log_tree_opt
.abbrev
= 0;
2048 log_tree_opt
.diff
= 1;
2049 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
2050 log_tree_opt
.disable_stdin
= 1;
2051 log_tree_opt
.no_commit_id
= 1;
2052 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
2053 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2054 if (!log_tree_opt
.diffopt
.file
)
2055 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
2057 res
|= log_tree_commit(&log_tree_opt
, commit
);
2058 fclose(log_tree_opt
.diffopt
.file
);
2062 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
2063 if (!file_exists(buf
.buf
)) {
2064 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2065 find_commit_subject(commit_buffer
, &subject
);
2066 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
2067 unuse_commit_buffer(commit
, commit_buffer
);
2069 strbuf_release(&buf
);
2074 static int intend_to_amend(void)
2076 struct object_id head
;
2079 if (get_oid("HEAD", &head
))
2080 return error(_("cannot read HEAD"));
2082 p
= oid_to_hex(&head
);
2083 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
2086 static int error_with_patch(struct commit
*commit
,
2087 const char *subject
, int subject_len
,
2088 struct replay_opts
*opts
, int exit_code
, int to_amend
)
2090 if (make_patch(commit
, opts
))
2094 if (intend_to_amend())
2097 fprintf(stderr
, "You can amend the commit now, with\n"
2099 " git commit --amend %s\n"
2101 "Once you are satisfied with your changes, run\n"
2103 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
2104 } else if (exit_code
)
2105 fprintf(stderr
, "Could not apply %s... %.*s\n",
2106 short_commit_name(commit
), subject_len
, subject
);
2111 static int error_failed_squash(struct commit
*commit
,
2112 struct replay_opts
*opts
, int subject_len
, const char *subject
)
2114 if (rename(rebase_path_squash_msg(), rebase_path_message()))
2115 return error(_("could not rename '%s' to '%s'"),
2116 rebase_path_squash_msg(), rebase_path_message());
2117 unlink(rebase_path_fixup_msg());
2118 unlink(git_path_merge_msg());
2119 if (copy_file(git_path_merge_msg(), rebase_path_message(), 0666))
2120 return error(_("could not copy '%s' to '%s'"),
2121 rebase_path_message(), git_path_merge_msg());
2122 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
2125 static int do_exec(const char *command_line
)
2127 struct argv_array child_env
= ARGV_ARRAY_INIT
;
2128 const char *child_argv
[] = { NULL
, NULL
};
2131 fprintf(stderr
, "Executing: %s\n", command_line
);
2132 child_argv
[0] = command_line
;
2133 argv_array_pushf(&child_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
2134 status
= run_command_v_opt_cd_env(child_argv
, RUN_USING_SHELL
, NULL
,
2137 /* force re-reading of the cache */
2138 if (discard_cache() < 0 || read_cache() < 0)
2139 return error(_("could not read index"));
2141 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
2144 warning(_("execution failed: %s\n%s"
2145 "You can fix the problem, and then run\n"
2147 " git rebase --continue\n"
2150 dirty
? N_("and made changes to the index and/or the "
2151 "working tree\n") : "");
2153 /* command not found */
2156 warning(_("execution succeeded: %s\nbut "
2157 "left changes to the index and/or the working tree\n"
2158 "Commit or stash your changes, and then run\n"
2160 " git rebase --continue\n"
2161 "\n"), command_line
);
2165 argv_array_clear(&child_env
);
2170 static int is_final_fixup(struct todo_list
*todo_list
)
2172 int i
= todo_list
->current
;
2174 if (!is_fixup(todo_list
->items
[i
].command
))
2177 while (++i
< todo_list
->nr
)
2178 if (is_fixup(todo_list
->items
[i
].command
))
2180 else if (!is_noop(todo_list
->items
[i
].command
))
2185 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
2189 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
2190 if (!is_noop(todo_list
->items
[i
].command
))
2191 return todo_list
->items
[i
].command
;
2196 static int apply_autostash(struct replay_opts
*opts
)
2198 struct strbuf stash_sha1
= STRBUF_INIT
;
2199 struct child_process child
= CHILD_PROCESS_INIT
;
2202 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
2203 strbuf_release(&stash_sha1
);
2206 strbuf_trim(&stash_sha1
);
2209 child
.no_stdout
= 1;
2210 child
.no_stderr
= 1;
2211 argv_array_push(&child
.args
, "stash");
2212 argv_array_push(&child
.args
, "apply");
2213 argv_array_push(&child
.args
, stash_sha1
.buf
);
2214 if (!run_command(&child
))
2215 fprintf(stderr
, _("Applied autostash.\n"));
2217 struct child_process store
= CHILD_PROCESS_INIT
;
2220 argv_array_push(&store
.args
, "stash");
2221 argv_array_push(&store
.args
, "store");
2222 argv_array_push(&store
.args
, "-m");
2223 argv_array_push(&store
.args
, "autostash");
2224 argv_array_push(&store
.args
, "-q");
2225 argv_array_push(&store
.args
, stash_sha1
.buf
);
2226 if (run_command(&store
))
2227 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
2230 _("Applying autostash resulted in conflicts.\n"
2231 "Your changes are safe in the stash.\n"
2232 "You can run \"git stash pop\" or"
2233 " \"git stash drop\" at any time.\n"));
2236 strbuf_release(&stash_sha1
);
2240 static const char *reflog_message(struct replay_opts
*opts
,
2241 const char *sub_action
, const char *fmt
, ...)
2244 static struct strbuf buf
= STRBUF_INIT
;
2248 strbuf_addstr(&buf
, action_name(opts
));
2250 strbuf_addf(&buf
, " (%s)", sub_action
);
2252 strbuf_addstr(&buf
, ": ");
2253 strbuf_vaddf(&buf
, fmt
, ap
);
2260 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2264 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
2266 assert(!(opts
->signoff
|| opts
->no_commit
||
2267 opts
->record_origin
|| opts
->edit
));
2268 if (read_and_refresh_cache(opts
))
2271 while (todo_list
->current
< todo_list
->nr
) {
2272 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
2273 if (save_todo(todo_list
, opts
))
2275 if (is_rebase_i(opts
)) {
2276 if (item
->command
!= TODO_COMMENT
) {
2277 FILE *f
= fopen(rebase_path_msgnum(), "w");
2279 todo_list
->done_nr
++;
2282 fprintf(f
, "%d\n", todo_list
->done_nr
);
2285 fprintf(stderr
, "Rebasing (%d/%d)%s",
2287 todo_list
->total_nr
,
2288 opts
->verbose
? "\n" : "\r");
2290 unlink(rebase_path_message());
2291 unlink(rebase_path_author_script());
2292 unlink(rebase_path_stopped_sha());
2293 unlink(rebase_path_amend());
2295 if (item
->command
<= TODO_SQUASH
) {
2296 if (is_rebase_i(opts
))
2297 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
2298 command_to_string(item
->command
), NULL
),
2300 res
= do_pick_commit(item
->command
, item
->commit
,
2301 opts
, is_final_fixup(todo_list
));
2302 if (is_rebase_i(opts
) && res
< 0) {
2304 todo_list
->current
--;
2305 if (save_todo(todo_list
, opts
))
2308 if (item
->command
== TODO_EDIT
) {
2309 struct commit
*commit
= item
->commit
;
2312 _("Stopped at %s... %.*s\n"),
2313 short_commit_name(commit
),
2314 item
->arg_len
, item
->arg
);
2315 return error_with_patch(commit
,
2316 item
->arg
, item
->arg_len
, opts
, res
,
2319 if (is_rebase_i(opts
) && !res
)
2320 record_in_rewritten(&item
->commit
->object
.oid
,
2321 peek_command(todo_list
, 1));
2322 if (res
&& is_fixup(item
->command
)) {
2325 return error_failed_squash(item
->commit
, opts
,
2326 item
->arg_len
, item
->arg
);
2327 } else if (res
&& is_rebase_i(opts
))
2328 return res
| error_with_patch(item
->commit
,
2329 item
->arg
, item
->arg_len
, opts
, res
,
2330 item
->command
== TODO_REWORD
);
2331 } else if (item
->command
== TODO_EXEC
) {
2332 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
2333 int saved
= *end_of_arg
;
2337 res
= do_exec(item
->arg
);
2338 *end_of_arg
= saved
;
2340 /* Reread the todo file if it has changed. */
2342 ; /* fall through */
2343 else if (stat(get_todo_path(opts
), &st
))
2344 res
= error_errno(_("could not stat '%s'"),
2345 get_todo_path(opts
));
2346 else if (match_stat_data(&todo_list
->stat
, &st
)) {
2347 todo_list_release(todo_list
);
2348 if (read_populate_todo(todo_list
, opts
))
2349 res
= -1; /* message was printed */
2350 /* `current` will be incremented below */
2351 todo_list
->current
= -1;
2353 } else if (!is_noop(item
->command
))
2354 return error(_("unknown command %d"), item
->command
);
2356 todo_list
->current
++;
2361 if (is_rebase_i(opts
)) {
2362 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
2365 /* Stopped in the middle, as planned? */
2366 if (todo_list
->current
< todo_list
->nr
)
2369 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
2370 starts_with(head_ref
.buf
, "refs/")) {
2372 struct object_id head
, orig
;
2375 if (get_oid("HEAD", &head
)) {
2376 res
= error(_("cannot read HEAD"));
2378 strbuf_release(&head_ref
);
2379 strbuf_release(&buf
);
2382 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
2383 get_oid_hex(buf
.buf
, &orig
)) {
2384 res
= error(_("could not read orig-head"));
2385 goto cleanup_head_ref
;
2388 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
2389 res
= error(_("could not read 'onto'"));
2390 goto cleanup_head_ref
;
2392 msg
= reflog_message(opts
, "finish", "%s onto %s",
2393 head_ref
.buf
, buf
.buf
);
2394 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
2395 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
2396 res
= error(_("could not update %s"),
2398 goto cleanup_head_ref
;
2400 msg
= reflog_message(opts
, "finish", "returning to %s",
2402 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
2403 res
= error(_("could not update HEAD to %s"),
2405 goto cleanup_head_ref
;
2410 if (opts
->verbose
) {
2411 struct rev_info log_tree_opt
;
2412 struct object_id orig
, head
;
2414 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2415 init_revisions(&log_tree_opt
, NULL
);
2416 log_tree_opt
.diff
= 1;
2417 log_tree_opt
.diffopt
.output_format
=
2418 DIFF_FORMAT_DIFFSTAT
;
2419 log_tree_opt
.disable_stdin
= 1;
2421 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
2422 !get_oid(buf
.buf
, &orig
) &&
2423 !get_oid("HEAD", &head
)) {
2424 diff_tree_oid(&orig
, &head
, "",
2425 &log_tree_opt
.diffopt
);
2426 log_tree_diff_flush(&log_tree_opt
);
2429 flush_rewritten_pending();
2430 if (!stat(rebase_path_rewritten_list(), &st
) &&
2432 struct child_process child
= CHILD_PROCESS_INIT
;
2433 const char *post_rewrite_hook
=
2434 find_hook("post-rewrite");
2436 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
2438 argv_array_push(&child
.args
, "notes");
2439 argv_array_push(&child
.args
, "copy");
2440 argv_array_push(&child
.args
, "--for-rewrite=rebase");
2441 /* we don't care if this copying failed */
2442 run_command(&child
);
2444 if (post_rewrite_hook
) {
2445 struct child_process hook
= CHILD_PROCESS_INIT
;
2447 hook
.in
= open(rebase_path_rewritten_list(),
2449 hook
.stdout_to_stderr
= 1;
2450 argv_array_push(&hook
.args
, post_rewrite_hook
);
2451 argv_array_push(&hook
.args
, "rebase");
2452 /* we don't care if this hook failed */
2456 apply_autostash(opts
);
2458 fprintf(stderr
, "Successfully rebased and updated %s.\n",
2461 strbuf_release(&buf
);
2462 strbuf_release(&head_ref
);
2466 * Sequence of picks finished successfully; cleanup by
2467 * removing the .git/sequencer directory
2469 return sequencer_remove_state(opts
);
2472 static int continue_single_pick(void)
2474 const char *argv
[] = { "commit", NULL
};
2476 if (!file_exists(git_path_cherry_pick_head()) &&
2477 !file_exists(git_path_revert_head()))
2478 return error(_("no cherry-pick or revert in progress"));
2479 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2482 static int commit_staged_changes(struct replay_opts
*opts
)
2484 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
2486 if (has_unstaged_changes(1))
2487 return error(_("cannot rebase: You have unstaged changes."));
2488 if (!has_uncommitted_changes(0)) {
2489 const char *cherry_pick_head
= git_path_cherry_pick_head();
2491 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
2492 return error(_("could not remove CHERRY_PICK_HEAD"));
2496 if (file_exists(rebase_path_amend())) {
2497 struct strbuf rev
= STRBUF_INIT
;
2498 struct object_id head
, to_amend
;
2500 if (get_oid("HEAD", &head
))
2501 return error(_("cannot amend non-existing commit"));
2502 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
2503 return error(_("invalid file: '%s'"), rebase_path_amend());
2504 if (get_oid_hex(rev
.buf
, &to_amend
))
2505 return error(_("invalid contents: '%s'"),
2506 rebase_path_amend());
2507 if (oidcmp(&head
, &to_amend
))
2508 return error(_("\nYou have uncommitted changes in your "
2509 "working tree. Please, commit them\n"
2510 "first and then run 'git rebase "
2511 "--continue' again."));
2513 strbuf_release(&rev
);
2517 if (run_git_commit(rebase_path_message(), opts
, flags
))
2518 return error(_("could not commit staged changes."));
2519 unlink(rebase_path_amend());
2523 int sequencer_continue(struct replay_opts
*opts
)
2525 struct todo_list todo_list
= TODO_LIST_INIT
;
2528 if (read_and_refresh_cache(opts
))
2531 if (is_rebase_i(opts
)) {
2532 if (commit_staged_changes(opts
))
2534 } else if (!file_exists(get_todo_path(opts
)))
2535 return continue_single_pick();
2536 if (read_populate_opts(opts
))
2538 if ((res
= read_populate_todo(&todo_list
, opts
)))
2539 goto release_todo_list
;
2541 if (!is_rebase_i(opts
)) {
2542 /* Verify that the conflict has been resolved */
2543 if (file_exists(git_path_cherry_pick_head()) ||
2544 file_exists(git_path_revert_head())) {
2545 res
= continue_single_pick();
2547 goto release_todo_list
;
2549 if (index_differs_from("HEAD", NULL
, 0)) {
2550 res
= error_dirty_index(opts
);
2551 goto release_todo_list
;
2553 todo_list
.current
++;
2554 } else if (file_exists(rebase_path_stopped_sha())) {
2555 struct strbuf buf
= STRBUF_INIT
;
2556 struct object_id oid
;
2558 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
2559 !get_oid_committish(buf
.buf
, &oid
))
2560 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
2561 strbuf_release(&buf
);
2564 res
= pick_commits(&todo_list
, opts
);
2566 todo_list_release(&todo_list
);
2570 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
2572 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
2573 return do_pick_commit(opts
->action
== REPLAY_PICK
?
2574 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
2577 int sequencer_pick_revisions(struct replay_opts
*opts
)
2579 struct todo_list todo_list
= TODO_LIST_INIT
;
2580 struct object_id oid
;
2584 if (read_and_refresh_cache(opts
))
2587 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
2588 struct object_id oid
;
2589 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
2591 /* This happens when using --stdin. */
2595 if (!get_oid(name
, &oid
)) {
2596 if (!lookup_commit_reference_gently(&oid
, 1)) {
2597 enum object_type type
= sha1_object_info(oid
.hash
, NULL
);
2598 return error(_("%s: can't cherry-pick a %s"),
2599 name
, typename(type
));
2602 return error(_("%s: bad revision"), name
);
2606 * If we were called as "git cherry-pick <commit>", just
2607 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
2608 * REVERT_HEAD, and don't touch the sequencer state.
2609 * This means it is possible to cherry-pick in the middle
2610 * of a cherry-pick sequence.
2612 if (opts
->revs
->cmdline
.nr
== 1 &&
2613 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
2614 opts
->revs
->no_walk
&&
2615 !opts
->revs
->cmdline
.rev
->flags
) {
2616 struct commit
*cmit
;
2617 if (prepare_revision_walk(opts
->revs
))
2618 return error(_("revision walk setup failed"));
2619 cmit
= get_revision(opts
->revs
);
2620 if (!cmit
|| get_revision(opts
->revs
))
2621 return error("BUG: expected exactly one commit from walk");
2622 return single_pick(cmit
, opts
);
2626 * Start a new cherry-pick/ revert sequence; but
2627 * first, make sure that an existing one isn't in
2631 if (walk_revs_populate_todo(&todo_list
, opts
) ||
2632 create_seq_dir() < 0)
2634 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
2635 return error(_("can't revert as initial commit"));
2636 if (save_head(oid_to_hex(&oid
)))
2638 if (save_opts(opts
))
2640 update_abort_safety_file();
2641 res
= pick_commits(&todo_list
, opts
);
2642 todo_list_release(&todo_list
);
2646 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
2648 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
2649 struct strbuf sob
= STRBUF_INIT
;
2652 strbuf_addstr(&sob
, sign_off_header
);
2653 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
2654 getenv("GIT_COMMITTER_EMAIL")));
2655 strbuf_addch(&sob
, '\n');
2658 strbuf_complete_line(msgbuf
);
2661 * If the whole message buffer is equal to the sob, pretend that we
2662 * found a conforming footer with a matching sob
2664 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
2665 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
2668 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
2671 const char *append_newlines
= NULL
;
2672 size_t len
= msgbuf
->len
- ignore_footer
;
2676 * The buffer is completely empty. Leave foom for
2677 * the title and body to be filled in by the user.
2679 append_newlines
= "\n\n";
2680 } else if (len
== 1) {
2682 * Buffer contains a single newline. Add another
2683 * so that we leave room for the title and body.
2685 append_newlines
= "\n";
2686 } else if (msgbuf
->buf
[len
- 2] != '\n') {
2688 * Buffer ends with a single newline. Add another
2689 * so that there is an empty line between the message
2692 append_newlines
= "\n";
2693 } /* else, the buffer already ends with two newlines. */
2695 if (append_newlines
)
2696 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2697 append_newlines
, strlen(append_newlines
));
2700 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
2701 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2704 strbuf_release(&sob
);
2707 int sequencer_make_script(int keep_empty
, FILE *out
,
2708 int argc
, const char **argv
)
2710 char *format
= NULL
;
2711 struct pretty_print_context pp
= {0};
2712 struct strbuf buf
= STRBUF_INIT
;
2713 struct rev_info revs
;
2714 struct commit
*commit
;
2716 init_revisions(&revs
, NULL
);
2717 revs
.verbose_header
= 1;
2718 revs
.max_parents
= 1;
2719 revs
.cherry_pick
= 1;
2722 revs
.right_only
= 1;
2723 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2724 revs
.topo_order
= 1;
2726 revs
.pretty_given
= 1;
2727 git_config_get_string("rebase.instructionFormat", &format
);
2728 if (!format
|| !*format
) {
2730 format
= xstrdup("%s");
2732 get_commit_format(format
, &revs
);
2734 pp
.fmt
= revs
.commit_format
;
2735 pp
.output_encoding
= get_log_output_encoding();
2737 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
2738 return error(_("make_script: unhandled options"));
2740 if (prepare_revision_walk(&revs
) < 0)
2741 return error(_("make_script: error preparing revisions"));
2743 while ((commit
= get_revision(&revs
))) {
2745 if (!keep_empty
&& is_original_commit_empty(commit
))
2746 strbuf_addf(&buf
, "%c ", comment_line_char
);
2747 strbuf_addf(&buf
, "pick %s ", oid_to_hex(&commit
->object
.oid
));
2748 pretty_print_commit(&pp
, commit
, &buf
);
2749 strbuf_addch(&buf
, '\n');
2750 fputs(buf
.buf
, out
);
2752 strbuf_release(&buf
);
2757 int transform_todo_ids(int shorten_ids
)
2759 const char *todo_file
= rebase_path_todo();
2760 struct todo_list todo_list
= TODO_LIST_INIT
;
2764 strbuf_reset(&todo_list
.buf
);
2765 fd
= open(todo_file
, O_RDONLY
);
2767 return error_errno(_("could not open '%s'"), todo_file
);
2768 if (strbuf_read(&todo_list
.buf
, fd
, 0) < 0) {
2770 return error(_("could not read '%s'."), todo_file
);
2774 res
= parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
2776 todo_list_release(&todo_list
);
2777 return error(_("unusable todo list: '%s'"), todo_file
);
2780 out
= fopen(todo_file
, "w");
2782 todo_list_release(&todo_list
);
2783 return error(_("unable to open '%s' for writing"), todo_file
);
2785 for (i
= 0; i
< todo_list
.nr
; i
++) {
2786 struct todo_item
*item
= todo_list
.items
+ i
;
2787 int bol
= item
->offset_in_buf
;
2788 const char *p
= todo_list
.buf
.buf
+ bol
;
2789 int eol
= i
+ 1 < todo_list
.nr
?
2790 todo_list
.items
[i
+ 1].offset_in_buf
:
2793 if (item
->command
>= TODO_EXEC
&& item
->command
!= TODO_DROP
)
2794 fwrite(p
, eol
- bol
, 1, out
);
2796 const char *id
= shorten_ids
?
2797 short_commit_name(item
->commit
) :
2798 oid_to_hex(&item
->commit
->object
.oid
);
2801 p
+= strspn(p
, " \t"); /* left-trim command */
2802 len
= strcspn(p
, " \t"); /* length of command */
2804 fprintf(out
, "%.*s %s %.*s\n",
2805 len
, p
, id
, item
->arg_len
, item
->arg
);
2809 todo_list_release(&todo_list
);
2814 CHECK_IGNORE
= 0, CHECK_WARN
, CHECK_ERROR
2817 static enum check_level
get_missing_commit_check_level(void)
2821 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
2822 !strcasecmp("ignore", value
))
2823 return CHECK_IGNORE
;
2824 if (!strcasecmp("warn", value
))
2826 if (!strcasecmp("error", value
))
2828 warning(_("unrecognized setting %s for option "
2829 "rebase.missingCommitsCheck. Ignoring."), value
);
2830 return CHECK_IGNORE
;
2834 * Check if the user dropped some commits by mistake
2835 * Behaviour determined by rebase.missingCommitsCheck.
2836 * Check if there is an unrecognized command or a
2837 * bad SHA-1 in a command.
2839 int check_todo_list(void)
2841 enum check_level check_level
= get_missing_commit_check_level();
2842 struct strbuf todo_file
= STRBUF_INIT
;
2843 struct todo_list todo_list
= TODO_LIST_INIT
;
2844 struct strbuf missing
= STRBUF_INIT
;
2845 int advise_to_edit_todo
= 0, res
= 0, fd
, i
;
2847 strbuf_addstr(&todo_file
, rebase_path_todo());
2848 fd
= open(todo_file
.buf
, O_RDONLY
);
2850 res
= error_errno(_("could not open '%s'"), todo_file
.buf
);
2853 if (strbuf_read(&todo_list
.buf
, fd
, 0) < 0) {
2855 res
= error(_("could not read '%s'."), todo_file
.buf
);
2859 advise_to_edit_todo
= res
=
2860 parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
2862 if (res
|| check_level
== CHECK_IGNORE
)
2865 /* Mark the commits in git-rebase-todo as seen */
2866 for (i
= 0; i
< todo_list
.nr
; i
++) {
2867 struct commit
*commit
= todo_list
.items
[i
].commit
;
2869 commit
->util
= (void *)1;
2872 todo_list_release(&todo_list
);
2873 strbuf_addstr(&todo_file
, ".backup");
2874 fd
= open(todo_file
.buf
, O_RDONLY
);
2876 res
= error_errno(_("could not open '%s'"), todo_file
.buf
);
2879 if (strbuf_read(&todo_list
.buf
, fd
, 0) < 0) {
2881 res
= error(_("could not read '%s'."), todo_file
.buf
);
2885 strbuf_release(&todo_file
);
2886 res
= !!parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
2888 /* Find commits in git-rebase-todo.backup yet unseen */
2889 for (i
= todo_list
.nr
- 1; i
>= 0; i
--) {
2890 struct todo_item
*item
= todo_list
.items
+ i
;
2891 struct commit
*commit
= item
->commit
;
2892 if (commit
&& !commit
->util
) {
2893 strbuf_addf(&missing
, " - %s %.*s\n",
2894 short_commit_name(commit
),
2895 item
->arg_len
, item
->arg
);
2896 commit
->util
= (void *)1;
2900 /* Warn about missing commits */
2904 if (check_level
== CHECK_ERROR
)
2905 advise_to_edit_todo
= res
= 1;
2908 _("Warning: some commits may have been dropped accidentally.\n"
2909 "Dropped commits (newer to older):\n"));
2911 /* Make the list user-friendly and display */
2912 fputs(missing
.buf
, stderr
);
2913 strbuf_release(&missing
);
2915 fprintf(stderr
, _("To avoid this message, use \"drop\" to "
2916 "explicitly remove a commit.\n\n"
2917 "Use 'git config rebase.missingCommitsCheck' to change "
2918 "the level of warnings.\n"
2919 "The possible behaviours are: ignore, warn, error.\n\n"));
2922 strbuf_release(&todo_file
);
2923 todo_list_release(&todo_list
);
2925 if (advise_to_edit_todo
)
2927 _("You can fix this with 'git rebase --edit-todo' "
2928 "and then run 'git rebase --continue'.\n"
2929 "Or you can abort the rebase with 'git rebase"
2935 /* skip picking commits whose parents are unchanged */
2936 int skip_unnecessary_picks(void)
2938 const char *todo_file
= rebase_path_todo();
2939 struct strbuf buf
= STRBUF_INIT
;
2940 struct todo_list todo_list
= TODO_LIST_INIT
;
2941 struct object_id onto_oid
, *oid
= &onto_oid
, *parent_oid
;
2944 if (!read_oneliner(&buf
, rebase_path_onto(), 0))
2945 return error(_("could not read 'onto'"));
2946 if (get_oid(buf
.buf
, &onto_oid
)) {
2947 strbuf_release(&buf
);
2948 return error(_("need a HEAD to fixup"));
2950 strbuf_release(&buf
);
2952 fd
= open(todo_file
, O_RDONLY
);
2954 return error_errno(_("could not open '%s'"), todo_file
);
2956 if (strbuf_read(&todo_list
.buf
, fd
, 0) < 0) {
2958 return error(_("could not read '%s'."), todo_file
);
2961 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
2962 todo_list_release(&todo_list
);
2966 for (i
= 0; i
< todo_list
.nr
; i
++) {
2967 struct todo_item
*item
= todo_list
.items
+ i
;
2969 if (item
->command
>= TODO_NOOP
)
2971 if (item
->command
!= TODO_PICK
)
2973 if (parse_commit(item
->commit
)) {
2974 todo_list_release(&todo_list
);
2975 return error(_("could not parse commit '%s'"),
2976 oid_to_hex(&item
->commit
->object
.oid
));
2978 if (!item
->commit
->parents
)
2979 break; /* root commit */
2980 if (item
->commit
->parents
->next
)
2981 break; /* merge commit */
2982 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
2983 if (hashcmp(parent_oid
->hash
, oid
->hash
))
2985 oid
= &item
->commit
->object
.oid
;
2988 int offset
= i
< todo_list
.nr
?
2989 todo_list
.items
[i
].offset_in_buf
: todo_list
.buf
.len
;
2990 const char *done_path
= rebase_path_done();
2992 fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
2994 error_errno(_("could not open '%s' for writing"),
2996 todo_list_release(&todo_list
);
2999 if (write_in_full(fd
, todo_list
.buf
.buf
, offset
) < 0) {
3000 error_errno(_("could not write to '%s'"), done_path
);
3001 todo_list_release(&todo_list
);
3007 fd
= open(rebase_path_todo(), O_WRONLY
, 0666);
3009 error_errno(_("could not open '%s' for writing"),
3010 rebase_path_todo());
3011 todo_list_release(&todo_list
);
3014 if (write_in_full(fd
, todo_list
.buf
.buf
+ offset
,
3015 todo_list
.buf
.len
- offset
) < 0) {
3016 error_errno(_("could not write to '%s'"),
3017 rebase_path_todo());
3019 todo_list_release(&todo_list
);
3022 if (ftruncate(fd
, todo_list
.buf
.len
- offset
) < 0) {
3023 error_errno(_("could not truncate '%s'"),
3024 rebase_path_todo());
3025 todo_list_release(&todo_list
);
3031 todo_list
.current
= i
;
3032 if (is_fixup(peek_command(&todo_list
, 0)))
3033 record_in_rewritten(oid
, peek_command(&todo_list
, 0));
3036 todo_list_release(&todo_list
);
3037 printf("%s\n", oid_to_hex(oid
));
3042 struct subject2item_entry
{
3043 struct hashmap_entry entry
;
3045 char subject
[FLEX_ARRAY
];
3048 static int subject2item_cmp(const void *fndata
,
3049 const struct subject2item_entry
*a
,
3050 const struct subject2item_entry
*b
, const void *key
)
3052 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
3056 * Rearrange the todo list that has both "pick commit-id msg" and "pick
3057 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
3058 * after the former, and change "pick" to "fixup"/"squash".
3060 * Note that if the config has specified a custom instruction format, each log
3061 * message will have to be retrieved from the commit (as the oneline in the
3062 * script cannot be trusted) in order to normalize the autosquash arrangement.
3064 int rearrange_squash(void)
3066 const char *todo_file
= rebase_path_todo();
3067 struct todo_list todo_list
= TODO_LIST_INIT
;
3068 struct hashmap subject2item
;
3069 int res
= 0, rearranged
= 0, *next
, *tail
, fd
, i
;
3072 fd
= open(todo_file
, O_RDONLY
);
3074 return error_errno(_("could not open '%s'"), todo_file
);
3075 if (strbuf_read(&todo_list
.buf
, fd
, 0) < 0) {
3077 return error(_("could not read '%s'."), todo_file
);
3080 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
3081 todo_list_release(&todo_list
);
3086 * The hashmap maps onelines to the respective todo list index.
3088 * If any items need to be rearranged, the next[i] value will indicate
3089 * which item was moved directly after the i'th.
3091 * In that case, last[i] will indicate the index of the latest item to
3092 * be moved to appear after the i'th.
3094 hashmap_init(&subject2item
, (hashmap_cmp_fn
) subject2item_cmp
,
3095 NULL
, todo_list
.nr
);
3096 ALLOC_ARRAY(next
, todo_list
.nr
);
3097 ALLOC_ARRAY(tail
, todo_list
.nr
);
3098 ALLOC_ARRAY(subjects
, todo_list
.nr
);
3099 for (i
= 0; i
< todo_list
.nr
; i
++) {
3100 struct strbuf buf
= STRBUF_INIT
;
3101 struct todo_item
*item
= todo_list
.items
+ i
;
3102 const char *commit_buffer
, *subject
, *p
;
3105 struct subject2item_entry
*entry
;
3107 next
[i
] = tail
[i
] = -1;
3108 if (item
->command
>= TODO_EXEC
) {
3113 if (is_fixup(item
->command
)) {
3114 todo_list_release(&todo_list
);
3115 return error(_("the script was already rearranged."));
3118 item
->commit
->util
= item
;
3120 parse_commit(item
->commit
);
3121 commit_buffer
= get_commit_buffer(item
->commit
, NULL
);
3122 find_commit_subject(commit_buffer
, &subject
);
3123 format_subject(&buf
, subject
, " ");
3124 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
3125 unuse_commit_buffer(item
->commit
, commit_buffer
);
3126 if ((skip_prefix(subject
, "fixup! ", &p
) ||
3127 skip_prefix(subject
, "squash! ", &p
))) {
3128 struct commit
*commit2
;
3133 if (!skip_prefix(p
, "fixup! ", &p
) &&
3134 !skip_prefix(p
, "squash! ", &p
))
3138 if ((entry
= hashmap_get_from_hash(&subject2item
,
3140 /* found by title */
3142 else if (!strchr(p
, ' ') &&
3144 lookup_commit_reference_by_name(p
)) &&
3146 /* found by commit name */
3147 i2
= (struct todo_item
*)commit2
->util
3150 /* copy can be a prefix of the commit subject */
3151 for (i2
= 0; i2
< i
; i2
++)
3153 starts_with(subjects
[i2
], p
))
3161 todo_list
.items
[i
].command
=
3162 starts_with(subject
, "fixup!") ?
3163 TODO_FIXUP
: TODO_SQUASH
;
3169 } else if (!hashmap_get_from_hash(&subject2item
,
3170 strhash(subject
), subject
)) {
3171 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
3173 hashmap_entry_init(entry
, strhash(entry
->subject
));
3174 hashmap_put(&subject2item
, entry
);
3179 struct strbuf buf
= STRBUF_INIT
;
3181 for (i
= 0; i
< todo_list
.nr
; i
++) {
3182 enum todo_command command
= todo_list
.items
[i
].command
;
3186 * Initially, all commands are 'pick's. If it is a
3187 * fixup or a squash now, we have rearranged it.
3189 if (is_fixup(command
))
3193 int offset
= todo_list
.items
[cur
].offset_in_buf
;
3194 int end_offset
= cur
+ 1 < todo_list
.nr
?
3195 todo_list
.items
[cur
+ 1].offset_in_buf
:
3197 char *bol
= todo_list
.buf
.buf
+ offset
;
3198 char *eol
= todo_list
.buf
.buf
+ end_offset
;
3200 /* replace 'pick', by 'fixup' or 'squash' */
3201 command
= todo_list
.items
[cur
].command
;
3202 if (is_fixup(command
)) {
3204 todo_command_info
[command
].str
);
3205 bol
+= strcspn(bol
, " \t");
3208 strbuf_add(&buf
, bol
, eol
- bol
);
3214 fd
= open(todo_file
, O_WRONLY
);
3216 res
= error_errno(_("could not open '%s'"), todo_file
);
3217 else if (write(fd
, buf
.buf
, buf
.len
) < 0)
3218 res
= error_errno(_("could not write to '%s'"), todo_file
);
3219 else if (ftruncate(fd
, buf
.len
) < 0)
3220 res
= error_errno(_("could not truncate '%s'"),
3223 strbuf_release(&buf
);
3228 for (i
= 0; i
< todo_list
.nr
; i
++)
3231 hashmap_free(&subject2item
, 1);
3232 todo_list_release(&todo_list
);