5 #include "object-store.h"
10 #include "run-command.h"
13 #include "cache-tree.h"
17 #include "merge-recursive.h"
19 #include "argv-array.h"
23 #include "wt-status.h"
25 #include "notes-utils.h"
27 #include "unpack-trees.h"
31 #include "commit-slab.h"
33 #include "rebase-interactive.h"
35 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
37 const char sign_off_header
[] = "Signed-off-by: ";
38 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
40 GIT_PATH_FUNC(git_path_commit_editmsg
, "COMMIT_EDITMSG")
42 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
44 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
45 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
46 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
47 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
49 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
51 * The file containing rebase commands, comments, and empty lines.
52 * This file is created by "git rebase -i" then edited by the user. As
53 * the lines are processed, they are removed from the front of this
54 * file and written to the tail of 'done'.
56 GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
57 static GIT_PATH_FUNC(rebase_path_todo_backup
,
58 "rebase-merge/git-rebase-todo.backup")
61 * The rebase command lines that have already been processed. A line
62 * is moved here when it is first handled, before any associated user
65 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
67 * The file to keep track of how many commands were already processed (e.g.
70 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum");
72 * The file to keep track of how many commands are to be processed in total
73 * (e.g. for the prompt).
75 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end");
77 * The commit message that is planned to be used for any changes that
78 * need to be committed following a user interaction.
80 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
82 * The file into which is accumulated the suggested commit message for
83 * squash/fixup commands. When the first of a series of squash/fixups
84 * is seen, the file is created and the commit message from the
85 * previous commit and from the first squash/fixup commit are written
86 * to it. The commit message for each subsequent squash/fixup commit
87 * is appended to the file as it is processed.
89 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
91 * If the current series of squash/fixups has not yet included a squash
92 * command, then this file exists and holds the commit message of the
93 * original "pick" commit. (If the series ends without a "squash"
94 * command, then this can be used as the commit message of the combined
95 * commit without opening the editor.)
97 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
99 * This file contains the list fixup/squash commands that have been
100 * accumulated into message-fixup or message-squash so far.
102 static GIT_PATH_FUNC(rebase_path_current_fixups
, "rebase-merge/current-fixups")
104 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
105 * GIT_AUTHOR_DATE that will be used for the commit that is currently
108 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
110 * When an "edit" rebase command is being processed, the SHA1 of the
111 * commit to be edited is recorded in this file. When "git rebase
112 * --continue" is executed, if there are any staged changes then they
113 * will be amended to the HEAD commit, but only provided the HEAD
114 * commit is still the commit to be edited. When any other rebase
115 * command is processed, this file is deleted.
117 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
119 * When we stop at a given patch via the "edit" command, this file contains
120 * the abbreviated commit name of the corresponding patch.
122 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
124 * For the post-rewrite hook, we make a list of rewritten commits and
125 * their new sha1s. The rewritten-pending list keeps the sha1s of
126 * commits that have been processed, but not committed yet,
127 * e.g. because they are waiting for a 'squash' command.
129 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
130 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
131 "rebase-merge/rewritten-pending")
134 * The path of the file containig the OID of the "squash onto" commit, i.e.
135 * the dummy commit used for `reset [new root]`.
137 static GIT_PATH_FUNC(rebase_path_squash_onto
, "rebase-merge/squash-onto")
140 * The path of the file listing refs that need to be deleted after the rebase
141 * finishes. This is used by the `label` command to record the need for cleanup.
143 static GIT_PATH_FUNC(rebase_path_refs_to_delete
, "rebase-merge/refs-to-delete")
146 * The following files are written by git-rebase just after parsing the
147 * command-line (and are only consumed, not modified, by the sequencer).
149 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
150 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
151 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
152 static GIT_PATH_FUNC(rebase_path_signoff
, "rebase-merge/signoff")
153 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
154 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
155 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
156 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
157 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
158 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
160 static int git_sequencer_config(const char *k
, const char *v
, void *cb
)
162 struct replay_opts
*opts
= cb
;
165 if (!strcmp(k
, "commit.cleanup")) {
168 status
= git_config_string(&s
, k
, v
);
172 if (!strcmp(s
, "verbatim"))
173 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
174 else if (!strcmp(s
, "whitespace"))
175 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
176 else if (!strcmp(s
, "strip"))
177 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
178 else if (!strcmp(s
, "scissors"))
179 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
181 warning(_("invalid commit message cleanup mode '%s'"),
188 if (!strcmp(k
, "commit.gpgsign")) {
189 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
193 status
= git_gpg_config(k
, v
, NULL
);
197 return git_diff_basic_config(k
, v
, NULL
);
200 void sequencer_init_config(struct replay_opts
*opts
)
202 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
203 git_config(git_sequencer_config
, opts
);
206 static inline int is_rebase_i(const struct replay_opts
*opts
)
208 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
211 static const char *get_dir(const struct replay_opts
*opts
)
213 if (is_rebase_i(opts
))
214 return rebase_path();
215 return git_path_seq_dir();
218 static const char *get_todo_path(const struct replay_opts
*opts
)
220 if (is_rebase_i(opts
))
221 return rebase_path_todo();
222 return git_path_todo_file();
226 * Returns 0 for non-conforming footer
227 * Returns 1 for conforming footer
228 * Returns 2 when sob exists within conforming footer
229 * Returns 3 when sob exists within conforming footer as last entry
231 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
234 struct trailer_info info
;
236 int found_sob
= 0, found_sob_last
= 0;
238 trailer_info_get(&info
, sb
->buf
);
240 if (info
.trailer_start
== info
.trailer_end
)
243 for (i
= 0; i
< info
.trailer_nr
; i
++)
244 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
246 if (i
== info
.trailer_nr
- 1)
250 trailer_info_release(&info
);
259 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
261 static struct strbuf buf
= STRBUF_INIT
;
265 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
269 int sequencer_remove_state(struct replay_opts
*opts
)
271 struct strbuf buf
= STRBUF_INIT
;
274 if (is_rebase_i(opts
) &&
275 strbuf_read_file(&buf
, rebase_path_refs_to_delete(), 0) > 0) {
278 char *eol
= strchr(p
, '\n');
281 if (delete_ref("(rebase -i) cleanup", p
, NULL
, 0) < 0)
282 warning(_("could not delete '%s'"), p
);
289 free(opts
->gpg_sign
);
290 free(opts
->strategy
);
291 for (i
= 0; i
< opts
->xopts_nr
; i
++)
292 free(opts
->xopts
[i
]);
294 strbuf_release(&opts
->current_fixups
);
297 strbuf_addstr(&buf
, get_dir(opts
));
298 remove_dir_recursively(&buf
, 0);
299 strbuf_release(&buf
);
304 static const char *action_name(const struct replay_opts
*opts
)
306 switch (opts
->action
) {
310 return N_("cherry-pick");
311 case REPLAY_INTERACTIVE_REBASE
:
312 return N_("rebase -i");
314 die(_("Unknown action: %d"), opts
->action
);
317 struct commit_message
{
324 static const char *short_commit_name(struct commit
*commit
)
326 return find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
);
329 static int get_message(struct commit
*commit
, struct commit_message
*out
)
331 const char *abbrev
, *subject
;
334 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
335 abbrev
= short_commit_name(commit
);
337 subject_len
= find_commit_subject(out
->message
, &subject
);
339 out
->subject
= xmemdupz(subject
, subject_len
);
340 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
341 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
346 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
348 free(msg
->parent_label
);
351 unuse_commit_buffer(commit
, msg
->message
);
354 static void print_advice(int show_hint
, struct replay_opts
*opts
)
356 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
359 fprintf(stderr
, "%s\n", msg
);
361 * A conflict has occurred but the porcelain
362 * (typically rebase --interactive) wants to take care
363 * of the commit itself so remove CHERRY_PICK_HEAD
365 unlink(git_path_cherry_pick_head(the_repository
));
371 advise(_("after resolving the conflicts, mark the corrected paths\n"
372 "with 'git add <paths>' or 'git rm <paths>'"));
374 advise(_("after resolving the conflicts, mark the corrected paths\n"
375 "with 'git add <paths>' or 'git rm <paths>'\n"
376 "and commit the result with 'git commit'"));
380 int write_message(const void *buf
, size_t len
, const char *filename
,
383 struct lock_file msg_file
= LOCK_INIT
;
385 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
387 return error_errno(_("could not lock '%s'"), filename
);
388 if (write_in_full(msg_fd
, buf
, len
) < 0) {
389 error_errno(_("could not write to '%s'"), filename
);
390 rollback_lock_file(&msg_file
);
393 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
394 error_errno(_("could not write eol to '%s'"), filename
);
395 rollback_lock_file(&msg_file
);
398 if (commit_lock_file(&msg_file
) < 0)
399 return error(_("failed to finalize '%s'"), filename
);
405 * Reads a file that was presumably written by a shell script, i.e. with an
406 * end-of-line marker that needs to be stripped.
408 * Note that only the last end-of-line marker is stripped, consistent with the
409 * behavior of "$(cat path)" in a shell script.
411 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
413 static int read_oneliner(struct strbuf
*buf
,
414 const char *path
, int skip_if_empty
)
416 int orig_len
= buf
->len
;
418 if (!file_exists(path
))
421 if (strbuf_read_file(buf
, path
, 0) < 0) {
422 warning_errno(_("could not read '%s'"), path
);
426 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
427 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
429 buf
->buf
[buf
->len
] = '\0';
432 if (skip_if_empty
&& buf
->len
== orig_len
)
438 static struct tree
*empty_tree(void)
440 return lookup_tree(the_hash_algo
->empty_tree
);
443 static int error_dirty_index(struct replay_opts
*opts
)
445 if (read_cache_unmerged())
446 return error_resolve_conflict(_(action_name(opts
)));
448 error(_("your local changes would be overwritten by %s."),
449 _(action_name(opts
)));
451 if (advice_commit_before_merge
)
452 advise(_("commit your changes or stash them to proceed."));
456 static void update_abort_safety_file(void)
458 struct object_id head
;
460 /* Do nothing on a single-pick */
461 if (!file_exists(git_path_seq_dir()))
464 if (!get_oid("HEAD", &head
))
465 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
467 write_file(git_path_abort_safety_file(), "%s", "");
470 static int fast_forward_to(const struct object_id
*to
, const struct object_id
*from
,
471 int unborn
, struct replay_opts
*opts
)
473 struct ref_transaction
*transaction
;
474 struct strbuf sb
= STRBUF_INIT
;
475 struct strbuf err
= STRBUF_INIT
;
478 if (checkout_fast_forward(from
, to
, 1))
479 return -1; /* the callee should have complained already */
481 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
483 transaction
= ref_transaction_begin(&err
);
485 ref_transaction_update(transaction
, "HEAD",
486 to
, unborn
&& !is_rebase_i(opts
) ?
489 ref_transaction_commit(transaction
, &err
)) {
490 ref_transaction_free(transaction
);
491 error("%s", err
.buf
);
493 strbuf_release(&err
);
498 strbuf_release(&err
);
499 ref_transaction_free(transaction
);
500 update_abort_safety_file();
504 void append_conflicts_hint(struct strbuf
*msgbuf
)
508 strbuf_addch(msgbuf
, '\n');
509 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
510 for (i
= 0; i
< active_nr
;) {
511 const struct cache_entry
*ce
= active_cache
[i
++];
513 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
514 while (i
< active_nr
&& !strcmp(ce
->name
,
515 active_cache
[i
]->name
))
521 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
522 const char *base_label
, const char *next_label
,
523 struct object_id
*head
, struct strbuf
*msgbuf
,
524 struct replay_opts
*opts
)
526 struct merge_options o
;
527 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
530 struct lock_file index_lock
= LOCK_INIT
;
532 if (hold_locked_index(&index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
537 init_merge_options(&o
);
538 o
.ancestor
= base
? base_label
: "(empty tree)";
540 o
.branch2
= next
? next_label
: "(empty tree)";
541 if (is_rebase_i(opts
))
543 o
.show_rename_progress
= 1;
545 head_tree
= parse_tree_indirect(head
);
546 next_tree
= next
? get_commit_tree(next
) : empty_tree();
547 base_tree
= base
? get_commit_tree(base
) : empty_tree();
549 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
550 parse_merge_opt(&o
, *xopt
);
552 clean
= merge_trees(&o
,
554 next_tree
, base_tree
, &result
);
555 if (is_rebase_i(opts
) && clean
<= 0)
556 fputs(o
.obuf
.buf
, stdout
);
557 strbuf_release(&o
.obuf
);
558 diff_warn_rename_limit("merge.renamelimit", o
.needed_rename_limit
, 0);
560 rollback_lock_file(&index_lock
);
564 if (write_locked_index(&the_index
, &index_lock
,
565 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
567 * TRANSLATORS: %s will be "revert", "cherry-pick" or
570 return error(_("%s: Unable to write new index file"),
571 _(action_name(opts
)));
574 append_conflicts_hint(msgbuf
);
579 static struct object_id
*get_cache_tree_oid(void)
581 if (!active_cache_tree
)
582 active_cache_tree
= cache_tree();
584 if (!cache_tree_fully_valid(active_cache_tree
))
585 if (cache_tree_update(&the_index
, 0)) {
586 error(_("unable to update cache tree"));
590 return &active_cache_tree
->oid
;
593 static int is_index_unchanged(void)
595 struct object_id head_oid
, *cache_tree_oid
;
596 struct commit
*head_commit
;
598 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
599 return error(_("could not resolve HEAD commit"));
601 head_commit
= lookup_commit(&head_oid
);
604 * If head_commit is NULL, check_commit, called from
605 * lookup_commit, would have indicated that head_commit is not
606 * a commit object already. parse_commit() will return failure
607 * without further complaints in such a case. Otherwise, if
608 * the commit is invalid, parse_commit() will complain. So
609 * there is nothing for us to say here. Just return failure.
611 if (parse_commit(head_commit
))
614 if (!(cache_tree_oid
= get_cache_tree_oid()))
617 return !oidcmp(cache_tree_oid
, get_commit_tree_oid(head_commit
));
620 static int write_author_script(const char *message
)
622 struct strbuf buf
= STRBUF_INIT
;
627 if (!*message
|| starts_with(message
, "\n")) {
629 /* Missing 'author' line? */
630 unlink(rebase_path_author_script());
632 } else if (skip_prefix(message
, "author ", &message
))
634 else if ((eol
= strchr(message
, '\n')))
639 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
640 while (*message
&& *message
!= '\n' && *message
!= '\r')
641 if (skip_prefix(message
, " <", &message
))
643 else if (*message
!= '\'')
644 strbuf_addch(&buf
, *(message
++));
646 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
647 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
648 while (*message
&& *message
!= '\n' && *message
!= '\r')
649 if (skip_prefix(message
, "> ", &message
))
651 else if (*message
!= '\'')
652 strbuf_addch(&buf
, *(message
++));
654 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
655 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
656 while (*message
&& *message
!= '\n' && *message
!= '\r')
657 if (*message
!= '\'')
658 strbuf_addch(&buf
, *(message
++));
660 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
661 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
662 strbuf_release(&buf
);
667 * Read a list of environment variable assignments (such as the author-script
668 * file) into an environment block. Returns -1 on error, 0 otherwise.
670 static int read_env_script(struct argv_array
*env
)
672 struct strbuf script
= STRBUF_INIT
;
676 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
679 for (p
= script
.buf
; *p
; p
++)
680 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
681 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
683 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
684 else if (*p
== '\n') {
689 for (i
= 0, p
= script
.buf
; i
< count
; i
++) {
690 argv_array_push(env
, p
);
697 static char *get_author(const char *message
)
702 a
= find_commit_header(message
, "author", &len
);
704 return xmemdupz(a
, len
);
709 /* Read author-script and return an ident line (author <email> timestamp) */
710 static const char *read_author_ident(struct strbuf
*buf
)
712 const char *keys
[] = {
713 "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE="
715 char *in
, *out
, *eol
;
718 if (strbuf_read_file(buf
, rebase_path_author_script(), 256) <= 0)
721 /* dequote values and construct ident line in-place */
722 for (in
= out
= buf
->buf
; i
< 3 && in
- buf
->buf
< buf
->len
; i
++) {
723 if (!skip_prefix(in
, keys
[i
], (const char **)&in
)) {
724 warning("could not parse '%s' (looking for '%s'",
725 rebase_path_author_script(), keys
[i
]);
729 eol
= strchrnul(in
, '\n');
734 if (i
> 0) /* separate values by spaces */
736 if (i
== 1) /* email needs to be surrounded by <...> */
738 memmove(out
, in
, len
);
740 if (i
== 1) /* email needs to be surrounded by <...> */
746 warning("could not parse '%s' (looking for '%s')",
747 rebase_path_author_script(), keys
[i
]);
751 buf
->len
= out
- buf
->buf
;
755 static const char staged_changes_advice
[] =
756 N_("you have staged changes in your working tree\n"
757 "If these changes are meant to be squashed into the previous commit, run:\n"
759 " git commit --amend %s\n"
761 "If they are meant to go into a new commit, run:\n"
765 "In both cases, once you're done, continue with:\n"
767 " git rebase --continue\n");
769 #define ALLOW_EMPTY (1<<0)
770 #define EDIT_MSG (1<<1)
771 #define AMEND_MSG (1<<2)
772 #define CLEANUP_MSG (1<<3)
773 #define VERIFY_MSG (1<<4)
774 #define CREATE_ROOT_COMMIT (1<<5)
776 static int run_command_silent_on_success(struct child_process
*cmd
)
778 struct strbuf buf
= STRBUF_INIT
;
781 cmd
->stdout_to_stderr
= 1;
782 rc
= pipe_command(cmd
,
788 fputs(buf
.buf
, stderr
);
789 strbuf_release(&buf
);
794 * If we are cherry-pick, and if the merge did not result in
795 * hand-editing, we will hit this commit and inherit the original
796 * author date and name.
798 * If we are revert, or if our cherry-pick results in a hand merge,
799 * we had better say that the current user is responsible for that.
801 * An exception is when run_git_commit() is called during an
802 * interactive rebase: in that case, we will want to retain the
805 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
808 struct child_process cmd
= CHILD_PROCESS_INIT
;
811 if ((flags
& CREATE_ROOT_COMMIT
) && !(flags
& AMEND_MSG
)) {
812 struct strbuf msg
= STRBUF_INIT
, script
= STRBUF_INIT
;
813 const char *author
= is_rebase_i(opts
) ?
814 read_author_ident(&script
) : NULL
;
815 struct object_id root_commit
, *cache_tree_oid
;
819 BUG("root commit without message");
821 if (!(cache_tree_oid
= get_cache_tree_oid()))
825 res
= strbuf_read_file(&msg
, defmsg
, 0);
828 res
= error_errno(_("could not read '%s'"), defmsg
);
830 res
= commit_tree(msg
.buf
, msg
.len
, cache_tree_oid
,
831 NULL
, &root_commit
, author
,
834 strbuf_release(&msg
);
835 strbuf_release(&script
);
837 update_ref(NULL
, "CHERRY_PICK_HEAD", &root_commit
, NULL
,
838 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
);
839 res
= update_ref(NULL
, "HEAD", &root_commit
, NULL
, 0,
840 UPDATE_REFS_MSG_ON_ERR
);
842 return res
< 0 ? error(_("writing root commit")) : 0;
847 if (is_rebase_i(opts
) && read_env_script(&cmd
.env_array
)) {
848 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
850 return error(_(staged_changes_advice
),
854 argv_array_push(&cmd
.args
, "commit");
856 if (!(flags
& VERIFY_MSG
))
857 argv_array_push(&cmd
.args
, "-n");
858 if ((flags
& AMEND_MSG
))
859 argv_array_push(&cmd
.args
, "--amend");
861 argv_array_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
863 argv_array_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
864 else if (!(flags
& EDIT_MSG
))
865 argv_array_pushl(&cmd
.args
, "-C", "HEAD", NULL
);
866 if ((flags
& CLEANUP_MSG
))
867 argv_array_push(&cmd
.args
, "--cleanup=strip");
868 if ((flags
& EDIT_MSG
))
869 argv_array_push(&cmd
.args
, "-e");
870 else if (!(flags
& CLEANUP_MSG
) &&
871 !opts
->signoff
&& !opts
->record_origin
&&
872 git_config_get_value("commit.cleanup", &value
))
873 argv_array_push(&cmd
.args
, "--cleanup=verbatim");
875 if ((flags
& ALLOW_EMPTY
))
876 argv_array_push(&cmd
.args
, "--allow-empty");
878 if (opts
->allow_empty_message
)
879 argv_array_push(&cmd
.args
, "--allow-empty-message");
881 if (is_rebase_i(opts
) && !(flags
& EDIT_MSG
))
882 return run_command_silent_on_success(&cmd
);
884 return run_command(&cmd
);
887 static int rest_is_empty(const struct strbuf
*sb
, int start
)
892 /* Check if the rest is just whitespace and Signed-off-by's. */
893 for (i
= start
; i
< sb
->len
; i
++) {
894 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
900 if (strlen(sign_off_header
) <= eol
- i
&&
901 starts_with(sb
->buf
+ i
, sign_off_header
)) {
906 if (!isspace(sb
->buf
[i
++]))
914 * Find out if the message in the strbuf contains only whitespace and
915 * Signed-off-by lines.
917 int message_is_empty(const struct strbuf
*sb
,
918 enum commit_msg_cleanup_mode cleanup_mode
)
920 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
922 return rest_is_empty(sb
, 0);
926 * See if the user edited the message in the editor or left what
927 * was in the template intact
929 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
930 enum commit_msg_cleanup_mode cleanup_mode
)
932 struct strbuf tmpl
= STRBUF_INIT
;
935 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
938 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
941 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
942 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
944 strbuf_release(&tmpl
);
945 return rest_is_empty(sb
, start
- sb
->buf
);
948 int update_head_with_reflog(const struct commit
*old_head
,
949 const struct object_id
*new_head
,
950 const char *action
, const struct strbuf
*msg
,
953 struct ref_transaction
*transaction
;
954 struct strbuf sb
= STRBUF_INIT
;
959 strbuf_addstr(&sb
, action
);
960 strbuf_addstr(&sb
, ": ");
963 nl
= strchr(msg
->buf
, '\n');
965 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
967 strbuf_addbuf(&sb
, msg
);
968 strbuf_addch(&sb
, '\n');
971 transaction
= ref_transaction_begin(err
);
973 ref_transaction_update(transaction
, "HEAD", new_head
,
974 old_head
? &old_head
->object
.oid
: &null_oid
,
976 ref_transaction_commit(transaction
, err
)) {
979 ref_transaction_free(transaction
);
985 static int run_rewrite_hook(const struct object_id
*oldoid
,
986 const struct object_id
*newoid
)
988 struct child_process proc
= CHILD_PROCESS_INIT
;
991 struct strbuf sb
= STRBUF_INIT
;
993 argv
[0] = find_hook("post-rewrite");
1002 proc
.stdout_to_stderr
= 1;
1004 code
= start_command(&proc
);
1007 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
1008 sigchain_push(SIGPIPE
, SIG_IGN
);
1009 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
1011 strbuf_release(&sb
);
1012 sigchain_pop(SIGPIPE
);
1013 return finish_command(&proc
);
1016 void commit_post_rewrite(const struct commit
*old_head
,
1017 const struct object_id
*new_head
)
1019 struct notes_rewrite_cfg
*cfg
;
1021 cfg
= init_copy_notes_for_rewrite("amend");
1023 /* we are amending, so old_head is not NULL */
1024 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
1025 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
1027 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
1030 static int run_prepare_commit_msg_hook(struct strbuf
*msg
, const char *commit
)
1032 struct argv_array hook_env
= ARGV_ARRAY_INIT
;
1036 name
= git_path_commit_editmsg();
1037 if (write_message(msg
->buf
, msg
->len
, name
, 0))
1040 argv_array_pushf(&hook_env
, "GIT_INDEX_FILE=%s", get_index_file());
1041 argv_array_push(&hook_env
, "GIT_EDITOR=:");
1043 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
1044 "commit", commit
, NULL
);
1046 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
1049 ret
= error(_("'prepare-commit-msg' hook failed"));
1050 argv_array_clear(&hook_env
);
1055 static const char implicit_ident_advice_noconfig
[] =
1056 N_("Your name and email address were configured automatically based\n"
1057 "on your username and hostname. Please check that they are accurate.\n"
1058 "You can suppress this message by setting them explicitly. Run the\n"
1059 "following command and follow the instructions in your editor to edit\n"
1060 "your configuration file:\n"
1062 " git config --global --edit\n"
1064 "After doing this, you may fix the identity used for this commit with:\n"
1066 " git commit --amend --reset-author\n");
1068 static const char implicit_ident_advice_config
[] =
1069 N_("Your name and email address were configured automatically based\n"
1070 "on your username and hostname. Please check that they are accurate.\n"
1071 "You can suppress this message by setting them explicitly:\n"
1073 " git config --global user.name \"Your Name\"\n"
1074 " git config --global user.email you@example.com\n"
1076 "After doing this, you may fix the identity used for this commit with:\n"
1078 " git commit --amend --reset-author\n");
1080 static const char *implicit_ident_advice(void)
1082 char *user_config
= expand_user_path("~/.gitconfig", 0);
1083 char *xdg_config
= xdg_config_home("config");
1084 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
1090 return _(implicit_ident_advice_config
);
1092 return _(implicit_ident_advice_noconfig
);
1096 void print_commit_summary(const char *prefix
, const struct object_id
*oid
,
1099 struct rev_info rev
;
1100 struct commit
*commit
;
1101 struct strbuf format
= STRBUF_INIT
;
1103 struct pretty_print_context pctx
= {0};
1104 struct strbuf author_ident
= STRBUF_INIT
;
1105 struct strbuf committer_ident
= STRBUF_INIT
;
1107 commit
= lookup_commit(oid
);
1109 die(_("couldn't look up newly created commit"));
1110 if (parse_commit(commit
))
1111 die(_("could not parse newly created commit"));
1113 strbuf_addstr(&format
, "format:%h] %s");
1115 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1116 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1117 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1118 strbuf_addstr(&format
, "\n Author: ");
1119 strbuf_addbuf_percentquote(&format
, &author_ident
);
1121 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
1122 struct strbuf date
= STRBUF_INIT
;
1124 format_commit_message(commit
, "%ad", &date
, &pctx
);
1125 strbuf_addstr(&format
, "\n Date: ");
1126 strbuf_addbuf_percentquote(&format
, &date
);
1127 strbuf_release(&date
);
1129 if (!committer_ident_sufficiently_given()) {
1130 strbuf_addstr(&format
, "\n Committer: ");
1131 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1132 if (advice_implicit_identity
) {
1133 strbuf_addch(&format
, '\n');
1134 strbuf_addstr(&format
, implicit_ident_advice());
1137 strbuf_release(&author_ident
);
1138 strbuf_release(&committer_ident
);
1140 init_revisions(&rev
, prefix
);
1141 setup_revisions(0, NULL
, &rev
, NULL
);
1144 rev
.diffopt
.output_format
=
1145 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1147 rev
.verbose_header
= 1;
1148 rev
.show_root_diff
= 1;
1149 get_commit_format(format
.buf
, &rev
);
1150 rev
.always_show_header
= 0;
1151 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1152 rev
.diffopt
.break_opt
= 0;
1153 diff_setup_done(&rev
.diffopt
);
1155 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1157 die_errno(_("unable to resolve HEAD after creating commit"));
1158 if (!strcmp(head
, "HEAD"))
1159 head
= _("detached HEAD");
1161 skip_prefix(head
, "refs/heads/", &head
);
1162 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1163 _(" (root-commit)") : "");
1165 if (!log_tree_commit(&rev
, commit
)) {
1166 rev
.always_show_header
= 1;
1167 rev
.use_terminator
= 1;
1168 log_tree_commit(&rev
, commit
);
1171 strbuf_release(&format
);
1174 static int parse_head(struct commit
**head
)
1176 struct commit
*current_head
;
1177 struct object_id oid
;
1179 if (get_oid("HEAD", &oid
)) {
1180 current_head
= NULL
;
1182 current_head
= lookup_commit_reference(&oid
);
1184 return error(_("could not parse HEAD"));
1185 if (oidcmp(&oid
, ¤t_head
->object
.oid
)) {
1186 warning(_("HEAD %s is not a commit!"),
1189 if (parse_commit(current_head
))
1190 return error(_("could not parse HEAD commit"));
1192 *head
= current_head
;
1198 * Try to commit without forking 'git commit'. In some cases we need
1199 * to run 'git commit' to display an error message
1202 * -1 - error unable to commit
1204 * 1 - run 'git commit'
1206 static int try_to_commit(struct strbuf
*msg
, const char *author
,
1207 struct replay_opts
*opts
, unsigned int flags
,
1208 struct object_id
*oid
)
1210 struct object_id tree
;
1211 struct commit
*current_head
;
1212 struct commit_list
*parents
= NULL
;
1213 struct commit_extra_header
*extra
= NULL
;
1214 struct strbuf err
= STRBUF_INIT
;
1215 struct strbuf commit_msg
= STRBUF_INIT
;
1216 char *amend_author
= NULL
;
1217 const char *hook_commit
= NULL
;
1218 enum commit_msg_cleanup_mode cleanup
;
1221 if (parse_head(¤t_head
))
1224 if (flags
& AMEND_MSG
) {
1225 const char *exclude_gpgsig
[] = { "gpgsig", NULL
};
1226 const char *out_enc
= get_commit_output_encoding();
1227 const char *message
= logmsg_reencode(current_head
, NULL
,
1231 const char *orig_message
= NULL
;
1233 find_commit_subject(message
, &orig_message
);
1235 strbuf_addstr(msg
, orig_message
);
1236 hook_commit
= "HEAD";
1238 author
= amend_author
= get_author(message
);
1239 unuse_commit_buffer(current_head
, message
);
1241 res
= error(_("unable to parse commit author"));
1244 parents
= copy_commit_list(current_head
->parents
);
1245 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1246 } else if (current_head
) {
1247 commit_list_insert(current_head
, &parents
);
1250 if (write_cache_as_tree(&tree
, 0, NULL
)) {
1251 res
= error(_("git write-tree failed to write a tree"));
1255 if (!(flags
& ALLOW_EMPTY
) && !oidcmp(current_head
?
1256 get_commit_tree_oid(current_head
) :
1257 the_hash_algo
->empty_tree
, &tree
)) {
1258 res
= 1; /* run 'git commit' to display error message */
1262 if (find_hook("prepare-commit-msg")) {
1263 res
= run_prepare_commit_msg_hook(msg
, hook_commit
);
1266 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1268 res
= error_errno(_("unable to read commit message "
1270 git_path_commit_editmsg());
1276 cleanup
= (flags
& CLEANUP_MSG
) ? COMMIT_MSG_CLEANUP_ALL
:
1277 opts
->default_msg_cleanup
;
1279 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1280 strbuf_stripspace(msg
, cleanup
== COMMIT_MSG_CLEANUP_ALL
);
1281 if (!opts
->allow_empty_message
&& message_is_empty(msg
, cleanup
)) {
1282 res
= 1; /* run 'git commit' to display error message */
1288 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
,
1289 oid
, author
, opts
->gpg_sign
, extra
)) {
1290 res
= error(_("failed to write commit object"));
1294 if (update_head_with_reflog(current_head
, oid
,
1295 getenv("GIT_REFLOG_ACTION"), msg
, &err
)) {
1296 res
= error("%s", err
.buf
);
1300 if (flags
& AMEND_MSG
)
1301 commit_post_rewrite(current_head
, oid
);
1304 free_commit_extra_headers(extra
);
1305 strbuf_release(&err
);
1306 strbuf_release(&commit_msg
);
1312 static int do_commit(const char *msg_file
, const char *author
,
1313 struct replay_opts
*opts
, unsigned int flags
)
1317 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
) &&
1318 !(flags
& CREATE_ROOT_COMMIT
)) {
1319 struct object_id oid
;
1320 struct strbuf sb
= STRBUF_INIT
;
1322 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1323 return error_errno(_("unable to read commit message "
1327 res
= try_to_commit(msg_file
? &sb
: NULL
, author
, opts
, flags
,
1329 strbuf_release(&sb
);
1331 unlink(git_path_cherry_pick_head(the_repository
));
1332 unlink(git_path_merge_msg(the_repository
));
1333 if (!is_rebase_i(opts
))
1334 print_commit_summary(NULL
, &oid
,
1335 SUMMARY_SHOW_AUTHOR_DATE
);
1340 return run_git_commit(msg_file
, opts
, flags
);
1345 static int is_original_commit_empty(struct commit
*commit
)
1347 const struct object_id
*ptree_oid
;
1349 if (parse_commit(commit
))
1350 return error(_("could not parse commit %s"),
1351 oid_to_hex(&commit
->object
.oid
));
1352 if (commit
->parents
) {
1353 struct commit
*parent
= commit
->parents
->item
;
1354 if (parse_commit(parent
))
1355 return error(_("could not parse parent commit %s"),
1356 oid_to_hex(&parent
->object
.oid
));
1357 ptree_oid
= get_commit_tree_oid(parent
);
1359 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1362 return !oidcmp(ptree_oid
, get_commit_tree_oid(commit
));
1366 * Do we run "git commit" with "--allow-empty"?
1368 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
1370 int index_unchanged
, empty_commit
;
1375 * (1) we do not allow empty at all and error out.
1377 * (2) we allow ones that were initially empty, but
1378 * forbid the ones that become empty;
1380 * (3) we allow both.
1382 if (!opts
->allow_empty
)
1383 return 0; /* let "git commit" barf as necessary */
1385 index_unchanged
= is_index_unchanged();
1386 if (index_unchanged
< 0)
1387 return index_unchanged
;
1388 if (!index_unchanged
)
1389 return 0; /* we do not have to say --allow-empty */
1391 if (opts
->keep_redundant_commits
)
1394 empty_commit
= is_original_commit_empty(commit
);
1395 if (empty_commit
< 0)
1396 return empty_commit
;
1404 * Note that ordering matters in this enum. Not only must it match the mapping
1405 * below, it is also divided into several sections that matter. When adding
1406 * new commands, make sure you add it in the right section.
1409 /* commands that handle commits */
1416 /* commands that do something else than handling a single commit */
1421 /* commands that do nothing but are counted for reporting progress */
1424 /* comments (not counted for reporting progress) */
1431 } todo_command_info
[] = {
1447 static const char *command_to_string(const enum todo_command command
)
1449 if (command
< TODO_COMMENT
)
1450 return todo_command_info
[command
].str
;
1451 die("Unknown command: %d", command
);
1454 static char command_to_char(const enum todo_command command
)
1456 if (command
< TODO_COMMENT
&& todo_command_info
[command
].c
)
1457 return todo_command_info
[command
].c
;
1458 return comment_line_char
;
1461 static int is_noop(const enum todo_command command
)
1463 return TODO_NOOP
<= command
;
1466 static int is_fixup(enum todo_command command
)
1468 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1471 /* Does this command create a (non-merge) commit? */
1472 static int is_pick_or_similar(enum todo_command command
)
1487 static int update_squash_messages(enum todo_command command
,
1488 struct commit
*commit
, struct replay_opts
*opts
)
1490 struct strbuf buf
= STRBUF_INIT
;
1492 const char *message
, *body
;
1494 if (opts
->current_fixup_count
> 0) {
1495 struct strbuf header
= STRBUF_INIT
;
1498 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 9) <= 0)
1499 return error(_("could not read '%s'"),
1500 rebase_path_squash_msg());
1502 eol
= buf
.buf
[0] != comment_line_char
?
1503 buf
.buf
: strchrnul(buf
.buf
, '\n');
1505 strbuf_addf(&header
, "%c ", comment_line_char
);
1506 strbuf_addf(&header
, _("This is a combination of %d commits."),
1507 opts
->current_fixup_count
+ 2);
1508 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1509 strbuf_release(&header
);
1511 struct object_id head
;
1512 struct commit
*head_commit
;
1513 const char *head_message
, *body
;
1515 if (get_oid("HEAD", &head
))
1516 return error(_("need a HEAD to fixup"));
1517 if (!(head_commit
= lookup_commit_reference(&head
)))
1518 return error(_("could not read HEAD"));
1519 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
1520 return error(_("could not read HEAD's commit message"));
1522 find_commit_subject(head_message
, &body
);
1523 if (write_message(body
, strlen(body
),
1524 rebase_path_fixup_msg(), 0)) {
1525 unuse_commit_buffer(head_commit
, head_message
);
1526 return error(_("cannot write '%s'"),
1527 rebase_path_fixup_msg());
1530 strbuf_addf(&buf
, "%c ", comment_line_char
);
1531 strbuf_addf(&buf
, _("This is a combination of %d commits."), 2);
1532 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1533 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
1534 strbuf_addstr(&buf
, "\n\n");
1535 strbuf_addstr(&buf
, body
);
1537 unuse_commit_buffer(head_commit
, head_message
);
1540 if (!(message
= get_commit_buffer(commit
, NULL
)))
1541 return error(_("could not read commit message of %s"),
1542 oid_to_hex(&commit
->object
.oid
));
1543 find_commit_subject(message
, &body
);
1545 if (command
== TODO_SQUASH
) {
1546 unlink(rebase_path_fixup_msg());
1547 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1548 strbuf_addf(&buf
, _("This is the commit message #%d:"),
1549 ++opts
->current_fixup_count
);
1550 strbuf_addstr(&buf
, "\n\n");
1551 strbuf_addstr(&buf
, body
);
1552 } else if (command
== TODO_FIXUP
) {
1553 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1554 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
1555 ++opts
->current_fixup_count
);
1556 strbuf_addstr(&buf
, "\n\n");
1557 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1559 return error(_("unknown command: %d"), command
);
1560 unuse_commit_buffer(commit
, message
);
1562 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
1563 strbuf_release(&buf
);
1566 strbuf_addf(&opts
->current_fixups
, "%s%s %s",
1567 opts
->current_fixups
.len
? "\n" : "",
1568 command_to_string(command
),
1569 oid_to_hex(&commit
->object
.oid
));
1570 res
= write_message(opts
->current_fixups
.buf
,
1571 opts
->current_fixups
.len
,
1572 rebase_path_current_fixups(), 0);
1578 static void flush_rewritten_pending(void) {
1579 struct strbuf buf
= STRBUF_INIT
;
1580 struct object_id newoid
;
1583 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
1584 !get_oid("HEAD", &newoid
) &&
1585 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
1586 char *bol
= buf
.buf
, *eol
;
1589 eol
= strchrnul(bol
, '\n');
1590 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
1591 bol
, oid_to_hex(&newoid
));
1597 unlink(rebase_path_rewritten_pending());
1599 strbuf_release(&buf
);
1602 static void record_in_rewritten(struct object_id
*oid
,
1603 enum todo_command next_command
) {
1604 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
1609 fprintf(out
, "%s\n", oid_to_hex(oid
));
1612 if (!is_fixup(next_command
))
1613 flush_rewritten_pending();
1616 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
1617 struct replay_opts
*opts
, int final_fixup
)
1619 unsigned int flags
= opts
->edit
? EDIT_MSG
: 0;
1620 const char *msg_file
= opts
->edit
? NULL
: git_path_merge_msg(the_repository
);
1621 struct object_id head
;
1622 struct commit
*base
, *next
, *parent
;
1623 const char *base_label
, *next_label
;
1624 char *author
= NULL
;
1625 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
1626 struct strbuf msgbuf
= STRBUF_INIT
;
1627 int res
, unborn
= 0, allow
;
1629 if (opts
->no_commit
) {
1631 * We do not intend to commit immediately. We just want to
1632 * merge the differences in, so let's compute the tree
1633 * that represents the "current" state for merge-recursive
1636 if (write_cache_as_tree(&head
, 0, NULL
))
1637 return error(_("your index file is unmerged."));
1639 unborn
= get_oid("HEAD", &head
);
1640 /* Do we want to generate a root commit? */
1641 if (is_pick_or_similar(command
) && opts
->have_squash_onto
&&
1642 !oidcmp(&head
, &opts
->squash_onto
)) {
1643 if (is_fixup(command
))
1644 return error(_("cannot fixup root commit"));
1645 flags
|= CREATE_ROOT_COMMIT
;
1648 oidcpy(&head
, the_hash_algo
->empty_tree
);
1649 if (index_differs_from(unborn
? empty_tree_oid_hex() : "HEAD",
1651 return error_dirty_index(opts
);
1655 if (!commit
->parents
)
1657 else if (commit
->parents
->next
) {
1658 /* Reverting or cherry-picking a merge commit */
1660 struct commit_list
*p
;
1662 if (!opts
->mainline
)
1663 return error(_("commit %s is a merge but no -m option was given."),
1664 oid_to_hex(&commit
->object
.oid
));
1666 for (cnt
= 1, p
= commit
->parents
;
1667 cnt
!= opts
->mainline
&& p
;
1670 if (cnt
!= opts
->mainline
|| !p
)
1671 return error(_("commit %s does not have parent %d"),
1672 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1674 } else if (0 < opts
->mainline
)
1675 return error(_("mainline was specified but commit %s is not a merge."),
1676 oid_to_hex(&commit
->object
.oid
));
1678 parent
= commit
->parents
->item
;
1680 if (get_message(commit
, &msg
) != 0)
1681 return error(_("cannot get commit message for %s"),
1682 oid_to_hex(&commit
->object
.oid
));
1684 if (opts
->allow_ff
&& !is_fixup(command
) &&
1685 ((parent
&& !oidcmp(&parent
->object
.oid
, &head
)) ||
1686 (!parent
&& unborn
))) {
1687 if (is_rebase_i(opts
))
1688 write_author_script(msg
.message
);
1689 res
= fast_forward_to(&commit
->object
.oid
, &head
, unborn
,
1691 if (res
|| command
!= TODO_REWORD
)
1693 flags
|= EDIT_MSG
| AMEND_MSG
| VERIFY_MSG
;
1695 goto fast_forward_edit
;
1697 if (parent
&& parse_commit(parent
) < 0)
1698 /* TRANSLATORS: The first %s will be a "todo" command like
1699 "revert" or "pick", the second %s a SHA1. */
1700 return error(_("%s: cannot parse parent commit %s"),
1701 command_to_string(command
),
1702 oid_to_hex(&parent
->object
.oid
));
1705 * "commit" is an existing commit. We would want to apply
1706 * the difference it introduces since its first parent "prev"
1707 * on top of the current HEAD if we are cherry-pick. Or the
1708 * reverse of it if we are revert.
1711 if (command
== TODO_REVERT
) {
1713 base_label
= msg
.label
;
1715 next_label
= msg
.parent_label
;
1716 strbuf_addstr(&msgbuf
, "Revert \"");
1717 strbuf_addstr(&msgbuf
, msg
.subject
);
1718 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
1719 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1721 if (commit
->parents
&& commit
->parents
->next
) {
1722 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1723 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1725 strbuf_addstr(&msgbuf
, ".\n");
1730 base_label
= msg
.parent_label
;
1732 next_label
= msg
.label
;
1734 /* Append the commit log message to msgbuf. */
1735 if (find_commit_subject(msg
.message
, &p
))
1736 strbuf_addstr(&msgbuf
, p
);
1738 if (opts
->record_origin
) {
1739 strbuf_complete_line(&msgbuf
);
1740 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1741 strbuf_addch(&msgbuf
, '\n');
1742 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1743 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1744 strbuf_addstr(&msgbuf
, ")\n");
1746 if (!is_fixup(command
))
1747 author
= get_author(msg
.message
);
1750 if (command
== TODO_REWORD
)
1751 flags
|= EDIT_MSG
| VERIFY_MSG
;
1752 else if (is_fixup(command
)) {
1753 if (update_squash_messages(command
, commit
, opts
))
1757 msg_file
= rebase_path_squash_msg();
1758 else if (file_exists(rebase_path_fixup_msg())) {
1759 flags
|= CLEANUP_MSG
;
1760 msg_file
= rebase_path_fixup_msg();
1762 const char *dest
= git_path_squash_msg(the_repository
);
1764 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1765 return error(_("could not rename '%s' to '%s'"),
1766 rebase_path_squash_msg(), dest
);
1767 unlink(git_path_merge_msg(the_repository
));
1773 if (opts
->signoff
&& !is_fixup(command
))
1774 append_signoff(&msgbuf
, 0, 0);
1776 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1778 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1779 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
1780 &head
, &msgbuf
, opts
);
1784 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
1785 git_path_merge_msg(the_repository
), 0);
1787 struct commit_list
*common
= NULL
;
1788 struct commit_list
*remotes
= NULL
;
1790 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1791 git_path_merge_msg(the_repository
), 0);
1793 commit_list_insert(base
, &common
);
1794 commit_list_insert(next
, &remotes
);
1795 res
|= try_merge_command(opts
->strategy
,
1796 opts
->xopts_nr
, (const char **)opts
->xopts
,
1797 common
, oid_to_hex(&head
), remotes
);
1798 free_commit_list(common
);
1799 free_commit_list(remotes
);
1801 strbuf_release(&msgbuf
);
1804 * If the merge was clean or if it failed due to conflict, we write
1805 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1806 * However, if the merge did not even start, then we don't want to
1809 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1810 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
1811 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1813 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1814 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
1815 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1819 error(command
== TODO_REVERT
1820 ? _("could not revert %s... %s")
1821 : _("could not apply %s... %s"),
1822 short_commit_name(commit
), msg
.subject
);
1823 print_advice(res
== 1, opts
);
1824 rerere(opts
->allow_rerere_auto
);
1828 allow
= allow_empty(opts
, commit
);
1833 flags
|= ALLOW_EMPTY
;
1834 if (!opts
->no_commit
) {
1836 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
1837 res
= do_commit(msg_file
, author
, opts
, flags
);
1839 res
= error(_("unable to parse commit author"));
1842 if (!res
&& final_fixup
) {
1843 unlink(rebase_path_fixup_msg());
1844 unlink(rebase_path_squash_msg());
1845 unlink(rebase_path_current_fixups());
1846 strbuf_reset(&opts
->current_fixups
);
1847 opts
->current_fixup_count
= 0;
1851 free_message(commit
, &msg
);
1853 update_abort_safety_file();
1858 static int prepare_revs(struct replay_opts
*opts
)
1861 * picking (but not reverting) ranges (but not individual revisions)
1862 * should be done in reverse
1864 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1865 opts
->revs
->reverse
^= 1;
1867 if (prepare_revision_walk(opts
->revs
))
1868 return error(_("revision walk setup failed"));
1870 if (!opts
->revs
->commits
)
1871 return error(_("empty commit set passed"));
1875 static int read_and_refresh_cache(struct replay_opts
*opts
)
1877 struct lock_file index_lock
= LOCK_INIT
;
1878 int index_fd
= hold_locked_index(&index_lock
, 0);
1879 if (read_index_preload(&the_index
, NULL
) < 0) {
1880 rollback_lock_file(&index_lock
);
1881 return error(_("git %s: failed to read the index"),
1882 _(action_name(opts
)));
1884 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1885 if (index_fd
>= 0) {
1886 if (write_locked_index(&the_index
, &index_lock
,
1887 COMMIT_LOCK
| SKIP_IF_UNCHANGED
)) {
1888 return error(_("git %s: failed to refresh the index"),
1889 _(action_name(opts
)));
1895 enum todo_item_flags
{
1896 TODO_EDIT_MERGE_MSG
= 1
1900 enum todo_command command
;
1901 struct commit
*commit
;
1905 size_t offset_in_buf
;
1910 struct todo_item
*items
;
1911 int nr
, alloc
, current
;
1912 int done_nr
, total_nr
;
1913 struct stat_data stat
;
1916 #define TODO_LIST_INIT { STRBUF_INIT }
1918 static void todo_list_release(struct todo_list
*todo_list
)
1920 strbuf_release(&todo_list
->buf
);
1921 FREE_AND_NULL(todo_list
->items
);
1922 todo_list
->nr
= todo_list
->alloc
= 0;
1925 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1927 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1928 return todo_list
->items
+ todo_list
->nr
++;
1931 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1933 struct object_id commit_oid
;
1934 char *end_of_object_name
;
1935 int i
, saved
, status
, padding
;
1940 bol
+= strspn(bol
, " \t");
1942 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1943 item
->command
= TODO_COMMENT
;
1944 item
->commit
= NULL
;
1946 item
->arg_len
= eol
- bol
;
1950 for (i
= 0; i
< TODO_COMMENT
; i
++)
1951 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1954 } else if (bol
[1] == ' ' && *bol
== todo_command_info
[i
].c
) {
1959 if (i
>= TODO_COMMENT
)
1962 /* Eat up extra spaces/ tabs before object name */
1963 padding
= strspn(bol
, " \t");
1966 if (item
->command
== TODO_NOOP
) {
1968 return error(_("%s does not accept arguments: '%s'"),
1969 command_to_string(item
->command
), bol
);
1970 item
->commit
= NULL
;
1972 item
->arg_len
= eol
- bol
;
1977 return error(_("missing arguments for %s"),
1978 command_to_string(item
->command
));
1980 if (item
->command
== TODO_EXEC
|| item
->command
== TODO_LABEL
||
1981 item
->command
== TODO_RESET
) {
1982 item
->commit
= NULL
;
1984 item
->arg_len
= (int)(eol
- bol
);
1988 if (item
->command
== TODO_MERGE
) {
1989 if (skip_prefix(bol
, "-C", &bol
))
1990 bol
+= strspn(bol
, " \t");
1991 else if (skip_prefix(bol
, "-c", &bol
)) {
1992 bol
+= strspn(bol
, " \t");
1993 item
->flags
|= TODO_EDIT_MERGE_MSG
;
1995 item
->flags
|= TODO_EDIT_MERGE_MSG
;
1996 item
->commit
= NULL
;
1998 item
->arg_len
= (int)(eol
- bol
);
2003 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
2004 saved
= *end_of_object_name
;
2005 *end_of_object_name
= '\0';
2006 status
= get_oid(bol
, &commit_oid
);
2007 *end_of_object_name
= saved
;
2009 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
2010 item
->arg_len
= (int)(eol
- item
->arg
);
2015 item
->commit
= lookup_commit_reference(&commit_oid
);
2016 return !item
->commit
;
2019 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
2021 struct todo_item
*item
;
2022 char *p
= buf
, *next_p
;
2023 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
2025 for (i
= 1; *p
; i
++, p
= next_p
) {
2026 char *eol
= strchrnul(p
, '\n');
2028 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
2030 if (p
!= eol
&& eol
[-1] == '\r')
2031 eol
--; /* strip Carriage Return */
2033 item
= append_new_todo(todo_list
);
2034 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
2035 if (parse_insn_line(item
, p
, eol
)) {
2036 res
= error(_("invalid line %d: %.*s"),
2037 i
, (int)(eol
- p
), p
);
2038 item
->command
= TODO_NOOP
;
2043 else if (is_fixup(item
->command
))
2044 return error(_("cannot '%s' without a previous commit"),
2045 command_to_string(item
->command
));
2046 else if (!is_noop(item
->command
))
2053 static int count_commands(struct todo_list
*todo_list
)
2057 for (i
= 0; i
< todo_list
->nr
; i
++)
2058 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
2064 static int get_item_line_offset(struct todo_list
*todo_list
, int index
)
2066 return index
< todo_list
->nr
?
2067 todo_list
->items
[index
].offset_in_buf
: todo_list
->buf
.len
;
2070 static const char *get_item_line(struct todo_list
*todo_list
, int index
)
2072 return todo_list
->buf
.buf
+ get_item_line_offset(todo_list
, index
);
2075 static int get_item_line_length(struct todo_list
*todo_list
, int index
)
2077 return get_item_line_offset(todo_list
, index
+ 1)
2078 - get_item_line_offset(todo_list
, index
);
2081 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
2086 fd
= open(path
, O_RDONLY
);
2088 return error_errno(_("could not open '%s'"), path
);
2089 len
= strbuf_read(sb
, fd
, 0);
2092 return error(_("could not read '%s'."), path
);
2096 static int read_populate_todo(struct todo_list
*todo_list
,
2097 struct replay_opts
*opts
)
2100 const char *todo_file
= get_todo_path(opts
);
2103 strbuf_reset(&todo_list
->buf
);
2104 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
2107 res
= stat(todo_file
, &st
);
2109 return error(_("could not stat '%s'"), todo_file
);
2110 fill_stat_data(&todo_list
->stat
, &st
);
2112 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
2114 if (is_rebase_i(opts
))
2115 return error(_("please fix this using "
2116 "'git rebase --edit-todo'."));
2117 return error(_("unusable instruction sheet: '%s'"), todo_file
);
2120 if (!todo_list
->nr
&&
2121 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
2122 return error(_("no commits parsed."));
2124 if (!is_rebase_i(opts
)) {
2125 enum todo_command valid
=
2126 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
2129 for (i
= 0; i
< todo_list
->nr
; i
++)
2130 if (valid
== todo_list
->items
[i
].command
)
2132 else if (valid
== TODO_PICK
)
2133 return error(_("cannot cherry-pick during a revert."));
2135 return error(_("cannot revert during a cherry-pick."));
2138 if (is_rebase_i(opts
)) {
2139 struct todo_list done
= TODO_LIST_INIT
;
2140 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
2142 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
2143 !parse_insn_buffer(done
.buf
.buf
, &done
))
2144 todo_list
->done_nr
= count_commands(&done
);
2146 todo_list
->done_nr
= 0;
2148 todo_list
->total_nr
= todo_list
->done_nr
2149 + count_commands(todo_list
);
2150 todo_list_release(&done
);
2153 fprintf(f
, "%d\n", todo_list
->total_nr
);
2161 static int git_config_string_dup(char **dest
,
2162 const char *var
, const char *value
)
2165 return config_error_nonbool(var
);
2167 *dest
= xstrdup(value
);
2171 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
2173 struct replay_opts
*opts
= data
;
2178 else if (!strcmp(key
, "options.no-commit"))
2179 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
2180 else if (!strcmp(key
, "options.edit"))
2181 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
2182 else if (!strcmp(key
, "options.signoff"))
2183 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
2184 else if (!strcmp(key
, "options.record-origin"))
2185 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
2186 else if (!strcmp(key
, "options.allow-ff"))
2187 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
2188 else if (!strcmp(key
, "options.mainline"))
2189 opts
->mainline
= git_config_int(key
, value
);
2190 else if (!strcmp(key
, "options.strategy"))
2191 git_config_string_dup(&opts
->strategy
, key
, value
);
2192 else if (!strcmp(key
, "options.gpg-sign"))
2193 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
2194 else if (!strcmp(key
, "options.strategy-option")) {
2195 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
2196 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
2197 } else if (!strcmp(key
, "options.allow-rerere-auto"))
2198 opts
->allow_rerere_auto
=
2199 git_config_bool_or_int(key
, value
, &error_flag
) ?
2200 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
2202 return error(_("invalid key: %s"), key
);
2205 return error(_("invalid value for %s: %s"), key
, value
);
2210 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
2213 char *strategy_opts_string
;
2216 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
2218 opts
->strategy
= strbuf_detach(buf
, NULL
);
2219 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
2222 strategy_opts_string
= buf
->buf
;
2223 if (*strategy_opts_string
== ' ')
2224 strategy_opts_string
++;
2225 opts
->xopts_nr
= split_cmdline(strategy_opts_string
,
2226 (const char ***)&opts
->xopts
);
2227 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
2228 const char *arg
= opts
->xopts
[i
];
2230 skip_prefix(arg
, "--", &arg
);
2231 opts
->xopts
[i
] = xstrdup(arg
);
2235 static int read_populate_opts(struct replay_opts
*opts
)
2237 if (is_rebase_i(opts
)) {
2238 struct strbuf buf
= STRBUF_INIT
;
2240 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
2241 if (!starts_with(buf
.buf
, "-S"))
2244 free(opts
->gpg_sign
);
2245 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
2250 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(), 1)) {
2251 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
2252 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
2253 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
2254 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
2258 if (file_exists(rebase_path_verbose()))
2261 if (file_exists(rebase_path_signoff())) {
2266 read_strategy_opts(opts
, &buf
);
2267 strbuf_release(&buf
);
2269 if (read_oneliner(&opts
->current_fixups
,
2270 rebase_path_current_fixups(), 1)) {
2271 const char *p
= opts
->current_fixups
.buf
;
2272 opts
->current_fixup_count
= 1;
2273 while ((p
= strchr(p
, '\n'))) {
2274 opts
->current_fixup_count
++;
2279 if (read_oneliner(&buf
, rebase_path_squash_onto(), 0)) {
2280 if (get_oid_hex(buf
.buf
, &opts
->squash_onto
) < 0)
2281 return error(_("unusable squash-onto"));
2282 opts
->have_squash_onto
= 1;
2288 if (!file_exists(git_path_opts_file()))
2291 * The function git_parse_source(), called from git_config_from_file(),
2292 * may die() in case of a syntactically incorrect file. We do not care
2293 * about this case, though, because we wrote that file ourselves, so we
2294 * are pretty certain that it is syntactically correct.
2296 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
2297 return error(_("malformed options sheet: '%s'"),
2298 git_path_opts_file());
2302 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
2303 struct replay_opts
*opts
)
2305 enum todo_command command
= opts
->action
== REPLAY_PICK
?
2306 TODO_PICK
: TODO_REVERT
;
2307 const char *command_string
= todo_command_info
[command
].str
;
2308 struct commit
*commit
;
2310 if (prepare_revs(opts
))
2313 while ((commit
= get_revision(opts
->revs
))) {
2314 struct todo_item
*item
= append_new_todo(todo_list
);
2315 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2316 const char *subject
;
2319 item
->command
= command
;
2320 item
->commit
= commit
;
2323 item
->offset_in_buf
= todo_list
->buf
.len
;
2324 subject_len
= find_commit_subject(commit_buffer
, &subject
);
2325 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
2326 short_commit_name(commit
), subject_len
, subject
);
2327 unuse_commit_buffer(commit
, commit_buffer
);
2332 static int create_seq_dir(void)
2334 if (file_exists(git_path_seq_dir())) {
2335 error(_("a cherry-pick or revert is already in progress"));
2336 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
2338 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
2339 return error_errno(_("could not create sequencer directory '%s'"),
2340 git_path_seq_dir());
2344 static int save_head(const char *head
)
2346 struct lock_file head_lock
= LOCK_INIT
;
2347 struct strbuf buf
= STRBUF_INIT
;
2351 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
2353 return error_errno(_("could not lock HEAD"));
2354 strbuf_addf(&buf
, "%s\n", head
);
2355 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
2356 strbuf_release(&buf
);
2358 error_errno(_("could not write to '%s'"), git_path_head_file());
2359 rollback_lock_file(&head_lock
);
2362 if (commit_lock_file(&head_lock
) < 0)
2363 return error(_("failed to finalize '%s'"), git_path_head_file());
2367 static int rollback_is_safe(void)
2369 struct strbuf sb
= STRBUF_INIT
;
2370 struct object_id expected_head
, actual_head
;
2372 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
2374 if (get_oid_hex(sb
.buf
, &expected_head
)) {
2375 strbuf_release(&sb
);
2376 die(_("could not parse %s"), git_path_abort_safety_file());
2378 strbuf_release(&sb
);
2380 else if (errno
== ENOENT
)
2381 oidclr(&expected_head
);
2383 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
2385 if (get_oid("HEAD", &actual_head
))
2386 oidclr(&actual_head
);
2388 return !oidcmp(&actual_head
, &expected_head
);
2391 static int reset_for_rollback(const struct object_id
*oid
)
2393 const char *argv
[4]; /* reset --merge <arg> + NULL */
2396 argv
[1] = "--merge";
2397 argv
[2] = oid_to_hex(oid
);
2399 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2402 static int rollback_single_pick(void)
2404 struct object_id head_oid
;
2406 if (!file_exists(git_path_cherry_pick_head(the_repository
)) &&
2407 !file_exists(git_path_revert_head(the_repository
)))
2408 return error(_("no cherry-pick or revert in progress"));
2409 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
2410 return error(_("cannot resolve HEAD"));
2411 if (is_null_oid(&head_oid
))
2412 return error(_("cannot abort from a branch yet to be born"));
2413 return reset_for_rollback(&head_oid
);
2416 int sequencer_rollback(struct replay_opts
*opts
)
2419 struct object_id oid
;
2420 struct strbuf buf
= STRBUF_INIT
;
2423 f
= fopen(git_path_head_file(), "r");
2424 if (!f
&& errno
== ENOENT
) {
2426 * There is no multiple-cherry-pick in progress.
2427 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
2428 * a single-cherry-pick in progress, abort that.
2430 return rollback_single_pick();
2433 return error_errno(_("cannot open '%s'"), git_path_head_file());
2434 if (strbuf_getline_lf(&buf
, f
)) {
2435 error(_("cannot read '%s': %s"), git_path_head_file(),
2436 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
2441 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
2442 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
2443 git_path_head_file());
2446 if (is_null_oid(&oid
)) {
2447 error(_("cannot abort from a branch yet to be born"));
2451 if (!rollback_is_safe()) {
2452 /* Do not error, just do not rollback */
2453 warning(_("You seem to have moved HEAD. "
2454 "Not rewinding, check your HEAD!"));
2456 if (reset_for_rollback(&oid
))
2458 strbuf_release(&buf
);
2459 return sequencer_remove_state(opts
);
2461 strbuf_release(&buf
);
2465 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2467 struct lock_file todo_lock
= LOCK_INIT
;
2468 const char *todo_path
= get_todo_path(opts
);
2469 int next
= todo_list
->current
, offset
, fd
;
2472 * rebase -i writes "git-rebase-todo" without the currently executing
2473 * command, appending it to "done" instead.
2475 if (is_rebase_i(opts
))
2478 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
2480 return error_errno(_("could not lock '%s'"), todo_path
);
2481 offset
= get_item_line_offset(todo_list
, next
);
2482 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
2483 todo_list
->buf
.len
- offset
) < 0)
2484 return error_errno(_("could not write to '%s'"), todo_path
);
2485 if (commit_lock_file(&todo_lock
) < 0)
2486 return error(_("failed to finalize '%s'"), todo_path
);
2488 if (is_rebase_i(opts
) && next
> 0) {
2489 const char *done
= rebase_path_done();
2490 int fd
= open(done
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
2495 if (write_in_full(fd
, get_item_line(todo_list
, next
- 1),
2496 get_item_line_length(todo_list
, next
- 1))
2498 ret
= error_errno(_("could not write to '%s'"), done
);
2500 ret
= error_errno(_("failed to finalize '%s'"), done
);
2506 static int save_opts(struct replay_opts
*opts
)
2508 const char *opts_file
= git_path_opts_file();
2511 if (opts
->no_commit
)
2512 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
2514 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
2516 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
2517 if (opts
->record_origin
)
2518 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
2520 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
2521 if (opts
->mainline
) {
2522 struct strbuf buf
= STRBUF_INIT
;
2523 strbuf_addf(&buf
, "%d", opts
->mainline
);
2524 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
2525 strbuf_release(&buf
);
2528 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
2530 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
2533 for (i
= 0; i
< opts
->xopts_nr
; i
++)
2534 res
|= git_config_set_multivar_in_file_gently(opts_file
,
2535 "options.strategy-option",
2536 opts
->xopts
[i
], "^$", 0);
2538 if (opts
->allow_rerere_auto
)
2539 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-rerere-auto",
2540 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
2545 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
2547 struct strbuf buf
= STRBUF_INIT
;
2548 struct rev_info log_tree_opt
;
2549 const char *subject
, *p
;
2552 p
= short_commit_name(commit
);
2553 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
2555 if (update_ref("rebase", "REBASE_HEAD", &commit
->object
.oid
,
2556 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2557 res
|= error(_("could not update %s"), "REBASE_HEAD");
2559 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
2560 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2561 init_revisions(&log_tree_opt
, NULL
);
2562 log_tree_opt
.abbrev
= 0;
2563 log_tree_opt
.diff
= 1;
2564 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
2565 log_tree_opt
.disable_stdin
= 1;
2566 log_tree_opt
.no_commit_id
= 1;
2567 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
2568 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2569 if (!log_tree_opt
.diffopt
.file
)
2570 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
2572 res
|= log_tree_commit(&log_tree_opt
, commit
);
2573 fclose(log_tree_opt
.diffopt
.file
);
2577 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
2578 if (!file_exists(buf
.buf
)) {
2579 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2580 find_commit_subject(commit_buffer
, &subject
);
2581 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
2582 unuse_commit_buffer(commit
, commit_buffer
);
2584 strbuf_release(&buf
);
2589 static int intend_to_amend(void)
2591 struct object_id head
;
2594 if (get_oid("HEAD", &head
))
2595 return error(_("cannot read HEAD"));
2597 p
= oid_to_hex(&head
);
2598 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
2601 static int error_with_patch(struct commit
*commit
,
2602 const char *subject
, int subject_len
,
2603 struct replay_opts
*opts
, int exit_code
, int to_amend
)
2605 if (make_patch(commit
, opts
))
2609 if (intend_to_amend())
2612 fprintf(stderr
, "You can amend the commit now, with\n"
2614 " git commit --amend %s\n"
2616 "Once you are satisfied with your changes, run\n"
2618 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
2619 } else if (exit_code
)
2620 fprintf(stderr
, "Could not apply %s... %.*s\n",
2621 short_commit_name(commit
), subject_len
, subject
);
2626 static int error_failed_squash(struct commit
*commit
,
2627 struct replay_opts
*opts
, int subject_len
, const char *subject
)
2629 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
2630 return error(_("could not copy '%s' to '%s'"),
2631 rebase_path_squash_msg(), rebase_path_message());
2632 unlink(git_path_merge_msg(the_repository
));
2633 if (copy_file(git_path_merge_msg(the_repository
), rebase_path_message(), 0666))
2634 return error(_("could not copy '%s' to '%s'"),
2635 rebase_path_message(),
2636 git_path_merge_msg(the_repository
));
2637 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
2640 static int do_exec(const char *command_line
)
2642 struct argv_array child_env
= ARGV_ARRAY_INIT
;
2643 const char *child_argv
[] = { NULL
, NULL
};
2646 fprintf(stderr
, "Executing: %s\n", command_line
);
2647 child_argv
[0] = command_line
;
2648 argv_array_pushf(&child_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
2649 status
= run_command_v_opt_cd_env(child_argv
, RUN_USING_SHELL
, NULL
,
2652 /* force re-reading of the cache */
2653 if (discard_cache() < 0 || read_cache() < 0)
2654 return error(_("could not read index"));
2656 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
2659 warning(_("execution failed: %s\n%s"
2660 "You can fix the problem, and then run\n"
2662 " git rebase --continue\n"
2665 dirty
? N_("and made changes to the index and/or the "
2666 "working tree\n") : "");
2668 /* command not found */
2671 warning(_("execution succeeded: %s\nbut "
2672 "left changes to the index and/or the working tree\n"
2673 "Commit or stash your changes, and then run\n"
2675 " git rebase --continue\n"
2676 "\n"), command_line
);
2680 argv_array_clear(&child_env
);
2685 static int safe_append(const char *filename
, const char *fmt
, ...)
2688 struct lock_file lock
= LOCK_INIT
;
2689 int fd
= hold_lock_file_for_update(&lock
, filename
,
2690 LOCK_REPORT_ON_ERROR
);
2691 struct strbuf buf
= STRBUF_INIT
;
2696 if (strbuf_read_file(&buf
, filename
, 0) < 0 && errno
!= ENOENT
) {
2697 error_errno(_("could not read '%s'"), filename
);
2698 rollback_lock_file(&lock
);
2701 strbuf_complete(&buf
, '\n');
2703 strbuf_vaddf(&buf
, fmt
, ap
);
2706 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
2707 error_errno(_("could not write to '%s'"), filename
);
2708 strbuf_release(&buf
);
2709 rollback_lock_file(&lock
);
2712 if (commit_lock_file(&lock
) < 0) {
2713 strbuf_release(&buf
);
2714 rollback_lock_file(&lock
);
2715 return error(_("failed to finalize '%s'"), filename
);
2718 strbuf_release(&buf
);
2722 static int do_label(const char *name
, int len
)
2724 struct ref_store
*refs
= get_main_ref_store(the_repository
);
2725 struct ref_transaction
*transaction
;
2726 struct strbuf ref_name
= STRBUF_INIT
, err
= STRBUF_INIT
;
2727 struct strbuf msg
= STRBUF_INIT
;
2729 struct object_id head_oid
;
2731 if (len
== 1 && *name
== '#')
2732 return error("Illegal label name: '%.*s'", len
, name
);
2734 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
2735 strbuf_addf(&msg
, "rebase -i (label) '%.*s'", len
, name
);
2737 transaction
= ref_store_transaction_begin(refs
, &err
);
2739 error("%s", err
.buf
);
2741 } else if (get_oid("HEAD", &head_oid
)) {
2742 error(_("could not read HEAD"));
2744 } else if (ref_transaction_update(transaction
, ref_name
.buf
, &head_oid
,
2745 NULL
, 0, msg
.buf
, &err
) < 0 ||
2746 ref_transaction_commit(transaction
, &err
)) {
2747 error("%s", err
.buf
);
2750 ref_transaction_free(transaction
);
2751 strbuf_release(&err
);
2752 strbuf_release(&msg
);
2755 ret
= safe_append(rebase_path_refs_to_delete(),
2756 "%s\n", ref_name
.buf
);
2757 strbuf_release(&ref_name
);
2762 static const char *reflog_message(struct replay_opts
*opts
,
2763 const char *sub_action
, const char *fmt
, ...);
2765 static int do_reset(const char *name
, int len
, struct replay_opts
*opts
)
2767 struct strbuf ref_name
= STRBUF_INIT
;
2768 struct object_id oid
;
2769 struct lock_file lock
= LOCK_INIT
;
2770 struct tree_desc desc
;
2772 struct unpack_trees_options unpack_tree_opts
;
2775 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0)
2778 if (len
== 10 && !strncmp("[new root]", name
, len
)) {
2779 if (!opts
->have_squash_onto
) {
2781 if (commit_tree("", 0, the_hash_algo
->empty_tree
,
2782 NULL
, &opts
->squash_onto
,
2784 return error(_("writing fake root commit"));
2785 opts
->have_squash_onto
= 1;
2786 hex
= oid_to_hex(&opts
->squash_onto
);
2787 if (write_message(hex
, strlen(hex
),
2788 rebase_path_squash_onto(), 0))
2789 return error(_("writing squash-onto"));
2791 oidcpy(&oid
, &opts
->squash_onto
);
2793 /* Determine the length of the label */
2794 for (i
= 0; i
< len
; i
++)
2795 if (isspace(name
[i
]))
2798 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
2799 if (get_oid(ref_name
.buf
, &oid
) &&
2800 get_oid(ref_name
.buf
+ strlen("refs/rewritten/"), &oid
)) {
2801 error(_("could not read '%s'"), ref_name
.buf
);
2802 rollback_lock_file(&lock
);
2803 strbuf_release(&ref_name
);
2808 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
2809 setup_unpack_trees_porcelain(&unpack_tree_opts
, "reset");
2810 unpack_tree_opts
.head_idx
= 1;
2811 unpack_tree_opts
.src_index
= &the_index
;
2812 unpack_tree_opts
.dst_index
= &the_index
;
2813 unpack_tree_opts
.fn
= oneway_merge
;
2814 unpack_tree_opts
.merge
= 1;
2815 unpack_tree_opts
.update
= 1;
2817 if (read_cache_unmerged()) {
2818 rollback_lock_file(&lock
);
2819 strbuf_release(&ref_name
);
2820 return error_resolve_conflict(_(action_name(opts
)));
2823 if (!fill_tree_descriptor(&desc
, &oid
)) {
2824 error(_("failed to find tree of %s"), oid_to_hex(&oid
));
2825 rollback_lock_file(&lock
);
2826 free((void *)desc
.buffer
);
2827 strbuf_release(&ref_name
);
2831 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
2832 rollback_lock_file(&lock
);
2833 free((void *)desc
.buffer
);
2834 strbuf_release(&ref_name
);
2838 tree
= parse_tree_indirect(&oid
);
2839 prime_cache_tree(&the_index
, tree
);
2841 if (write_locked_index(&the_index
, &lock
, COMMIT_LOCK
) < 0)
2842 ret
= error(_("could not write index"));
2843 free((void *)desc
.buffer
);
2846 ret
= update_ref(reflog_message(opts
, "reset", "'%.*s'",
2847 len
, name
), "HEAD", &oid
,
2848 NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
2850 strbuf_release(&ref_name
);
2854 static int do_merge(struct commit
*commit
, const char *arg
, int arg_len
,
2855 int flags
, struct replay_opts
*opts
)
2857 int run_commit_flags
= (flags
& TODO_EDIT_MERGE_MSG
) ?
2858 EDIT_MSG
| VERIFY_MSG
: 0;
2859 struct strbuf ref_name
= STRBUF_INIT
;
2860 struct commit
*head_commit
, *merge_commit
, *i
;
2861 struct commit_list
*bases
, *j
, *reversed
= NULL
;
2862 struct merge_options o
;
2863 int merge_arg_len
, oneline_offset
, can_fast_forward
, ret
;
2864 static struct lock_file lock
;
2867 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0) {
2872 head_commit
= lookup_commit_reference_by_name("HEAD");
2874 ret
= error(_("cannot merge without a current revision"));
2878 oneline_offset
= arg_len
;
2879 merge_arg_len
= strcspn(arg
, " \t\n");
2880 p
= arg
+ merge_arg_len
;
2881 p
+= strspn(p
, " \t\n");
2882 if (*p
== '#' && (!p
[1] || isspace(p
[1]))) {
2883 p
+= 1 + strspn(p
+ 1, " \t\n");
2884 oneline_offset
= p
- arg
;
2885 } else if (p
- arg
< arg_len
)
2886 BUG("octopus merges are not supported yet: '%s'", p
);
2888 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", merge_arg_len
, arg
);
2889 merge_commit
= lookup_commit_reference_by_name(ref_name
.buf
);
2890 if (!merge_commit
) {
2891 /* fall back to non-rewritten ref or commit */
2892 strbuf_splice(&ref_name
, 0, strlen("refs/rewritten/"), "", 0);
2893 merge_commit
= lookup_commit_reference_by_name(ref_name
.buf
);
2896 if (!merge_commit
) {
2897 ret
= error(_("could not resolve '%s'"), ref_name
.buf
);
2901 if (opts
->have_squash_onto
&&
2902 !oidcmp(&head_commit
->object
.oid
, &opts
->squash_onto
)) {
2904 * When the user tells us to "merge" something into a
2905 * "[new root]", let's simply fast-forward to the merge head.
2907 rollback_lock_file(&lock
);
2908 ret
= fast_forward_to(&merge_commit
->object
.oid
,
2909 &head_commit
->object
.oid
, 0, opts
);
2914 const char *message
= get_commit_buffer(commit
, NULL
);
2919 ret
= error(_("could not get commit message of '%s'"),
2920 oid_to_hex(&commit
->object
.oid
));
2923 write_author_script(message
);
2924 find_commit_subject(message
, &body
);
2926 ret
= write_message(body
, len
, git_path_merge_msg(the_repository
), 0);
2927 unuse_commit_buffer(commit
, message
);
2929 error_errno(_("could not write '%s'"),
2930 git_path_merge_msg(the_repository
));
2934 struct strbuf buf
= STRBUF_INIT
;
2937 strbuf_addf(&buf
, "author %s", git_author_info(0));
2938 write_author_script(buf
.buf
);
2941 if (oneline_offset
< arg_len
) {
2942 p
= arg
+ oneline_offset
;
2943 len
= arg_len
- oneline_offset
;
2945 strbuf_addf(&buf
, "Merge branch '%.*s'",
2946 merge_arg_len
, arg
);
2951 ret
= write_message(p
, len
, git_path_merge_msg(the_repository
), 0);
2952 strbuf_release(&buf
);
2954 error_errno(_("could not write '%s'"),
2955 git_path_merge_msg(the_repository
));
2961 * If HEAD is not identical to the first parent of the original merge
2962 * commit, we cannot fast-forward.
2964 can_fast_forward
= opts
->allow_ff
&& commit
&& commit
->parents
&&
2965 !oidcmp(&commit
->parents
->item
->object
.oid
,
2966 &head_commit
->object
.oid
);
2969 * If the merge head is different from the original one, we cannot
2972 if (can_fast_forward
) {
2973 struct commit_list
*second_parent
= commit
->parents
->next
;
2975 if (second_parent
&& !second_parent
->next
&&
2976 oidcmp(&merge_commit
->object
.oid
,
2977 &second_parent
->item
->object
.oid
))
2978 can_fast_forward
= 0;
2981 if (can_fast_forward
&& commit
->parents
->next
&&
2982 !commit
->parents
->next
->next
&&
2983 !oidcmp(&commit
->parents
->next
->item
->object
.oid
,
2984 &merge_commit
->object
.oid
)) {
2985 rollback_lock_file(&lock
);
2986 ret
= fast_forward_to(&commit
->object
.oid
,
2987 &head_commit
->object
.oid
, 0, opts
);
2991 write_message(oid_to_hex(&merge_commit
->object
.oid
), GIT_SHA1_HEXSZ
,
2992 git_path_merge_head(the_repository
), 0);
2993 write_message("no-ff", 5, git_path_merge_mode(the_repository
), 0);
2995 bases
= get_merge_bases(head_commit
, merge_commit
);
2996 if (bases
&& !oidcmp(&merge_commit
->object
.oid
,
2997 &bases
->item
->object
.oid
)) {
2999 /* skip merging an ancestor of HEAD */
3003 for (j
= bases
; j
; j
= j
->next
)
3004 commit_list_insert(j
->item
, &reversed
);
3005 free_commit_list(bases
);
3008 init_merge_options(&o
);
3010 o
.branch2
= ref_name
.buf
;
3011 o
.buffer_output
= 2;
3013 ret
= merge_recursive(&o
, head_commit
, merge_commit
, reversed
, &i
);
3015 fputs(o
.obuf
.buf
, stdout
);
3016 strbuf_release(&o
.obuf
);
3018 error(_("could not even attempt to merge '%.*s'"),
3019 merge_arg_len
, arg
);
3023 * The return value of merge_recursive() is 1 on clean, and 0 on
3026 * Let's reverse that, so that do_merge() returns 0 upon success and
3027 * 1 upon failed merge (keeping the return value -1 for the cases where
3028 * we will want to reschedule the `merge` command).
3032 if (active_cache_changed
&&
3033 write_locked_index(&the_index
, &lock
, COMMIT_LOCK
)) {
3034 ret
= error(_("merge: Unable to write new index file"));
3038 rollback_lock_file(&lock
);
3040 rerere(opts
->allow_rerere_auto
);
3043 * In case of problems, we now want to return a positive
3044 * value (a negative one would indicate that the `merge`
3045 * command needs to be rescheduled).
3047 ret
= !!run_git_commit(git_path_merge_msg(the_repository
), opts
,
3051 strbuf_release(&ref_name
);
3052 rollback_lock_file(&lock
);
3056 static int is_final_fixup(struct todo_list
*todo_list
)
3058 int i
= todo_list
->current
;
3060 if (!is_fixup(todo_list
->items
[i
].command
))
3063 while (++i
< todo_list
->nr
)
3064 if (is_fixup(todo_list
->items
[i
].command
))
3066 else if (!is_noop(todo_list
->items
[i
].command
))
3071 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
3075 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
3076 if (!is_noop(todo_list
->items
[i
].command
))
3077 return todo_list
->items
[i
].command
;
3082 static int apply_autostash(struct replay_opts
*opts
)
3084 struct strbuf stash_sha1
= STRBUF_INIT
;
3085 struct child_process child
= CHILD_PROCESS_INIT
;
3088 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
3089 strbuf_release(&stash_sha1
);
3092 strbuf_trim(&stash_sha1
);
3095 child
.no_stdout
= 1;
3096 child
.no_stderr
= 1;
3097 argv_array_push(&child
.args
, "stash");
3098 argv_array_push(&child
.args
, "apply");
3099 argv_array_push(&child
.args
, stash_sha1
.buf
);
3100 if (!run_command(&child
))
3101 fprintf(stderr
, _("Applied autostash.\n"));
3103 struct child_process store
= CHILD_PROCESS_INIT
;
3106 argv_array_push(&store
.args
, "stash");
3107 argv_array_push(&store
.args
, "store");
3108 argv_array_push(&store
.args
, "-m");
3109 argv_array_push(&store
.args
, "autostash");
3110 argv_array_push(&store
.args
, "-q");
3111 argv_array_push(&store
.args
, stash_sha1
.buf
);
3112 if (run_command(&store
))
3113 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
3116 _("Applying autostash resulted in conflicts.\n"
3117 "Your changes are safe in the stash.\n"
3118 "You can run \"git stash pop\" or"
3119 " \"git stash drop\" at any time.\n"));
3122 strbuf_release(&stash_sha1
);
3126 static const char *reflog_message(struct replay_opts
*opts
,
3127 const char *sub_action
, const char *fmt
, ...)
3130 static struct strbuf buf
= STRBUF_INIT
;
3134 strbuf_addstr(&buf
, action_name(opts
));
3136 strbuf_addf(&buf
, " (%s)", sub_action
);
3138 strbuf_addstr(&buf
, ": ");
3139 strbuf_vaddf(&buf
, fmt
, ap
);
3146 static int run_git_checkout(struct replay_opts
*opts
, const char *commit
,
3149 struct child_process cmd
= CHILD_PROCESS_INIT
;
3153 argv_array_push(&cmd
.args
, "checkout");
3154 argv_array_push(&cmd
.args
, commit
);
3155 argv_array_pushf(&cmd
.env_array
, GIT_REFLOG_ACTION
"=%s", action
);
3158 return run_command(&cmd
);
3160 return run_command_silent_on_success(&cmd
);
3163 int prepare_branch_to_be_rebased(struct replay_opts
*opts
, const char *commit
)
3167 if (commit
&& *commit
) {
3168 action
= reflog_message(opts
, "start", "checkout %s", commit
);
3169 if (run_git_checkout(opts
, commit
, action
))
3170 return error(_("could not checkout %s"), commit
);
3176 static int checkout_onto(struct replay_opts
*opts
,
3177 const char *onto_name
, const char *onto
,
3178 const char *orig_head
)
3180 struct object_id oid
;
3181 const char *action
= reflog_message(opts
, "start", "checkout %s", onto_name
);
3183 if (get_oid(orig_head
, &oid
))
3184 return error(_("%s: not a valid OID"), orig_head
);
3186 if (run_git_checkout(opts
, onto
, action
)) {
3187 apply_autostash(opts
);
3188 sequencer_remove_state(opts
);
3189 return error(_("could not detach HEAD"));
3192 return update_ref(NULL
, "ORIG_HEAD", &oid
, NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
3195 static const char rescheduled_advice
[] =
3196 N_("Could not execute the todo command\n"
3200 "It has been rescheduled; To edit the command before continuing, please\n"
3201 "edit the todo list first:\n"
3203 " git rebase --edit-todo\n"
3204 " git rebase --continue\n");
3206 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
3208 int res
= 0, reschedule
= 0;
3210 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3212 assert(!(opts
->signoff
|| opts
->no_commit
||
3213 opts
->record_origin
|| opts
->edit
));
3214 if (read_and_refresh_cache(opts
))
3217 while (todo_list
->current
< todo_list
->nr
) {
3218 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
3219 if (save_todo(todo_list
, opts
))
3221 if (is_rebase_i(opts
)) {
3222 if (item
->command
!= TODO_COMMENT
) {
3223 FILE *f
= fopen(rebase_path_msgnum(), "w");
3225 todo_list
->done_nr
++;
3228 fprintf(f
, "%d\n", todo_list
->done_nr
);
3231 fprintf(stderr
, "Rebasing (%d/%d)%s",
3233 todo_list
->total_nr
,
3234 opts
->verbose
? "\n" : "\r");
3236 unlink(rebase_path_message());
3237 unlink(rebase_path_author_script());
3238 unlink(rebase_path_stopped_sha());
3239 unlink(rebase_path_amend());
3240 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
3242 if (item
->command
<= TODO_SQUASH
) {
3243 if (is_rebase_i(opts
))
3244 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
3245 command_to_string(item
->command
), NULL
),
3247 res
= do_pick_commit(item
->command
, item
->commit
,
3248 opts
, is_final_fixup(todo_list
));
3249 if (is_rebase_i(opts
) && res
< 0) {
3251 advise(_(rescheduled_advice
),
3252 get_item_line_length(todo_list
,
3253 todo_list
->current
),
3254 get_item_line(todo_list
,
3255 todo_list
->current
));
3256 todo_list
->current
--;
3257 if (save_todo(todo_list
, opts
))
3260 if (item
->command
== TODO_EDIT
) {
3261 struct commit
*commit
= item
->commit
;
3264 _("Stopped at %s... %.*s\n"),
3265 short_commit_name(commit
),
3266 item
->arg_len
, item
->arg
);
3267 return error_with_patch(commit
,
3268 item
->arg
, item
->arg_len
, opts
, res
,
3271 if (is_rebase_i(opts
) && !res
)
3272 record_in_rewritten(&item
->commit
->object
.oid
,
3273 peek_command(todo_list
, 1));
3274 if (res
&& is_fixup(item
->command
)) {
3277 return error_failed_squash(item
->commit
, opts
,
3278 item
->arg_len
, item
->arg
);
3279 } else if (res
&& is_rebase_i(opts
) && item
->commit
) {
3281 struct object_id oid
;
3284 * If we are rewording and have either
3285 * fast-forwarded already, or are about to
3286 * create a new root commit, we want to amend,
3287 * otherwise we do not.
3289 if (item
->command
== TODO_REWORD
&&
3290 !get_oid("HEAD", &oid
) &&
3291 (!oidcmp(&item
->commit
->object
.oid
, &oid
) ||
3292 (opts
->have_squash_onto
&&
3293 !oidcmp(&opts
->squash_onto
, &oid
))))
3296 return res
| error_with_patch(item
->commit
,
3297 item
->arg
, item
->arg_len
, opts
,
3300 } else if (item
->command
== TODO_EXEC
) {
3301 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
3302 int saved
= *end_of_arg
;
3306 res
= do_exec(item
->arg
);
3307 *end_of_arg
= saved
;
3309 /* Reread the todo file if it has changed. */
3311 ; /* fall through */
3312 else if (stat(get_todo_path(opts
), &st
))
3313 res
= error_errno(_("could not stat '%s'"),
3314 get_todo_path(opts
));
3315 else if (match_stat_data(&todo_list
->stat
, &st
)) {
3316 todo_list_release(todo_list
);
3317 if (read_populate_todo(todo_list
, opts
))
3318 res
= -1; /* message was printed */
3319 /* `current` will be incremented below */
3320 todo_list
->current
= -1;
3322 } else if (item
->command
== TODO_LABEL
) {
3323 if ((res
= do_label(item
->arg
, item
->arg_len
)))
3325 } else if (item
->command
== TODO_RESET
) {
3326 if ((res
= do_reset(item
->arg
, item
->arg_len
, opts
)))
3328 } else if (item
->command
== TODO_MERGE
) {
3329 if ((res
= do_merge(item
->commit
,
3330 item
->arg
, item
->arg_len
,
3331 item
->flags
, opts
)) < 0)
3333 else if (item
->commit
)
3334 record_in_rewritten(&item
->commit
->object
.oid
,
3335 peek_command(todo_list
, 1));
3337 /* failed with merge conflicts */
3338 return error_with_patch(item
->commit
,
3340 item
->arg_len
, opts
,
3342 } else if (!is_noop(item
->command
))
3343 return error(_("unknown command %d"), item
->command
);
3346 advise(_(rescheduled_advice
),
3347 get_item_line_length(todo_list
,
3348 todo_list
->current
),
3349 get_item_line(todo_list
, todo_list
->current
));
3350 todo_list
->current
--;
3351 if (save_todo(todo_list
, opts
))
3354 return error_with_patch(item
->commit
,
3356 item
->arg_len
, opts
,
3360 todo_list
->current
++;
3365 if (is_rebase_i(opts
)) {
3366 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
3369 /* Stopped in the middle, as planned? */
3370 if (todo_list
->current
< todo_list
->nr
)
3373 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
3374 starts_with(head_ref
.buf
, "refs/")) {
3376 struct object_id head
, orig
;
3379 if (get_oid("HEAD", &head
)) {
3380 res
= error(_("cannot read HEAD"));
3382 strbuf_release(&head_ref
);
3383 strbuf_release(&buf
);
3386 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
3387 get_oid_hex(buf
.buf
, &orig
)) {
3388 res
= error(_("could not read orig-head"));
3389 goto cleanup_head_ref
;
3392 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
3393 res
= error(_("could not read 'onto'"));
3394 goto cleanup_head_ref
;
3396 msg
= reflog_message(opts
, "finish", "%s onto %s",
3397 head_ref
.buf
, buf
.buf
);
3398 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
3399 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
3400 res
= error(_("could not update %s"),
3402 goto cleanup_head_ref
;
3404 msg
= reflog_message(opts
, "finish", "returning to %s",
3406 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
3407 res
= error(_("could not update HEAD to %s"),
3409 goto cleanup_head_ref
;
3414 if (opts
->verbose
) {
3415 struct rev_info log_tree_opt
;
3416 struct object_id orig
, head
;
3418 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
3419 init_revisions(&log_tree_opt
, NULL
);
3420 log_tree_opt
.diff
= 1;
3421 log_tree_opt
.diffopt
.output_format
=
3422 DIFF_FORMAT_DIFFSTAT
;
3423 log_tree_opt
.disable_stdin
= 1;
3425 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
3426 !get_oid(buf
.buf
, &orig
) &&
3427 !get_oid("HEAD", &head
)) {
3428 diff_tree_oid(&orig
, &head
, "",
3429 &log_tree_opt
.diffopt
);
3430 log_tree_diff_flush(&log_tree_opt
);
3433 flush_rewritten_pending();
3434 if (!stat(rebase_path_rewritten_list(), &st
) &&
3436 struct child_process child
= CHILD_PROCESS_INIT
;
3437 const char *post_rewrite_hook
=
3438 find_hook("post-rewrite");
3440 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
3442 argv_array_push(&child
.args
, "notes");
3443 argv_array_push(&child
.args
, "copy");
3444 argv_array_push(&child
.args
, "--for-rewrite=rebase");
3445 /* we don't care if this copying failed */
3446 run_command(&child
);
3448 if (post_rewrite_hook
) {
3449 struct child_process hook
= CHILD_PROCESS_INIT
;
3451 hook
.in
= open(rebase_path_rewritten_list(),
3453 hook
.stdout_to_stderr
= 1;
3454 argv_array_push(&hook
.args
, post_rewrite_hook
);
3455 argv_array_push(&hook
.args
, "rebase");
3456 /* we don't care if this hook failed */
3460 apply_autostash(opts
);
3462 fprintf(stderr
, "Successfully rebased and updated %s.\n",
3465 strbuf_release(&buf
);
3466 strbuf_release(&head_ref
);
3470 * Sequence of picks finished successfully; cleanup by
3471 * removing the .git/sequencer directory
3473 return sequencer_remove_state(opts
);
3476 static int continue_single_pick(void)
3478 const char *argv
[] = { "commit", NULL
};
3480 if (!file_exists(git_path_cherry_pick_head(the_repository
)) &&
3481 !file_exists(git_path_revert_head(the_repository
)))
3482 return error(_("no cherry-pick or revert in progress"));
3483 return run_command_v_opt(argv
, RUN_GIT_CMD
);
3486 static int commit_staged_changes(struct replay_opts
*opts
,
3487 struct todo_list
*todo_list
)
3489 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
3490 unsigned int final_fixup
= 0, is_clean
;
3492 if (has_unstaged_changes(1))
3493 return error(_("cannot rebase: You have unstaged changes."));
3495 is_clean
= !has_uncommitted_changes(0);
3497 if (file_exists(rebase_path_amend())) {
3498 struct strbuf rev
= STRBUF_INIT
;
3499 struct object_id head
, to_amend
;
3501 if (get_oid("HEAD", &head
))
3502 return error(_("cannot amend non-existing commit"));
3503 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
3504 return error(_("invalid file: '%s'"), rebase_path_amend());
3505 if (get_oid_hex(rev
.buf
, &to_amend
))
3506 return error(_("invalid contents: '%s'"),
3507 rebase_path_amend());
3508 if (!is_clean
&& oidcmp(&head
, &to_amend
))
3509 return error(_("\nYou have uncommitted changes in your "
3510 "working tree. Please, commit them\n"
3511 "first and then run 'git rebase "
3512 "--continue' again."));
3514 * When skipping a failed fixup/squash, we need to edit the
3515 * commit message, the current fixup list and count, and if it
3516 * was the last fixup/squash in the chain, we need to clean up
3517 * the commit message and if there was a squash, let the user
3520 if (is_clean
&& !oidcmp(&head
, &to_amend
) &&
3521 opts
->current_fixup_count
> 0 &&
3522 file_exists(rebase_path_stopped_sha())) {
3523 const char *p
= opts
->current_fixups
.buf
;
3524 int len
= opts
->current_fixups
.len
;
3526 opts
->current_fixup_count
--;
3528 BUG("Incorrect current_fixups:\n%s", p
);
3529 while (len
&& p
[len
- 1] != '\n')
3531 strbuf_setlen(&opts
->current_fixups
, len
);
3532 if (write_message(p
, len
, rebase_path_current_fixups(),
3534 return error(_("could not write file: '%s'"),
3535 rebase_path_current_fixups());
3538 * If a fixup/squash in a fixup/squash chain failed, the
3539 * commit message is already correct, no need to commit
3542 * Only if it is the final command in the fixup/squash
3543 * chain, and only if the chain is longer than a single
3544 * fixup/squash command (which was just skipped), do we
3545 * actually need to re-commit with a cleaned up commit
3548 if (opts
->current_fixup_count
> 0 &&
3549 !is_fixup(peek_command(todo_list
, 0))) {
3552 * If there was not a single "squash" in the
3553 * chain, we only need to clean up the commit
3554 * message, no need to bother the user with
3555 * opening the commit message in the editor.
3557 if (!starts_with(p
, "squash ") &&
3558 !strstr(p
, "\nsquash "))
3559 flags
= (flags
& ~EDIT_MSG
) | CLEANUP_MSG
;
3560 } else if (is_fixup(peek_command(todo_list
, 0))) {
3562 * We need to update the squash message to skip
3563 * the latest commit message.
3565 struct commit
*commit
;
3566 const char *path
= rebase_path_squash_msg();
3568 if (parse_head(&commit
) ||
3569 !(p
= get_commit_buffer(commit
, NULL
)) ||
3570 write_message(p
, strlen(p
), path
, 0)) {
3571 unuse_commit_buffer(commit
, p
);
3572 return error(_("could not write file: "
3575 unuse_commit_buffer(commit
, p
);
3579 strbuf_release(&rev
);
3584 const char *cherry_pick_head
= git_path_cherry_pick_head(the_repository
);
3586 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
3587 return error(_("could not remove CHERRY_PICK_HEAD"));
3592 if (run_git_commit(final_fixup
? NULL
: rebase_path_message(),
3594 return error(_("could not commit staged changes."));
3595 unlink(rebase_path_amend());
3597 unlink(rebase_path_fixup_msg());
3598 unlink(rebase_path_squash_msg());
3600 if (opts
->current_fixup_count
> 0) {
3602 * Whether final fixup or not, we just cleaned up the commit
3605 unlink(rebase_path_current_fixups());
3606 strbuf_reset(&opts
->current_fixups
);
3607 opts
->current_fixup_count
= 0;
3612 int sequencer_continue(struct replay_opts
*opts
)
3614 struct todo_list todo_list
= TODO_LIST_INIT
;
3617 if (read_and_refresh_cache(opts
))
3620 if (read_populate_opts(opts
))
3622 if (is_rebase_i(opts
)) {
3623 if ((res
= read_populate_todo(&todo_list
, opts
)))
3624 goto release_todo_list
;
3625 if (commit_staged_changes(opts
, &todo_list
))
3627 } else if (!file_exists(get_todo_path(opts
)))
3628 return continue_single_pick();
3629 else if ((res
= read_populate_todo(&todo_list
, opts
)))
3630 goto release_todo_list
;
3632 if (!is_rebase_i(opts
)) {
3633 /* Verify that the conflict has been resolved */
3634 if (file_exists(git_path_cherry_pick_head(the_repository
)) ||
3635 file_exists(git_path_revert_head(the_repository
))) {
3636 res
= continue_single_pick();
3638 goto release_todo_list
;
3640 if (index_differs_from("HEAD", NULL
, 0)) {
3641 res
= error_dirty_index(opts
);
3642 goto release_todo_list
;
3644 todo_list
.current
++;
3645 } else if (file_exists(rebase_path_stopped_sha())) {
3646 struct strbuf buf
= STRBUF_INIT
;
3647 struct object_id oid
;
3649 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
3650 !get_oid_committish(buf
.buf
, &oid
))
3651 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
3652 strbuf_release(&buf
);
3655 res
= pick_commits(&todo_list
, opts
);
3657 todo_list_release(&todo_list
);
3661 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
3663 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3664 return do_pick_commit(opts
->action
== REPLAY_PICK
?
3665 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
3668 int sequencer_pick_revisions(struct replay_opts
*opts
)
3670 struct todo_list todo_list
= TODO_LIST_INIT
;
3671 struct object_id oid
;
3675 if (read_and_refresh_cache(opts
))
3678 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
3679 struct object_id oid
;
3680 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
3682 /* This happens when using --stdin. */
3686 if (!get_oid(name
, &oid
)) {
3687 if (!lookup_commit_reference_gently(&oid
, 1)) {
3688 enum object_type type
= oid_object_info(the_repository
,
3691 return error(_("%s: can't cherry-pick a %s"),
3692 name
, type_name(type
));
3695 return error(_("%s: bad revision"), name
);
3699 * If we were called as "git cherry-pick <commit>", just
3700 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
3701 * REVERT_HEAD, and don't touch the sequencer state.
3702 * This means it is possible to cherry-pick in the middle
3703 * of a cherry-pick sequence.
3705 if (opts
->revs
->cmdline
.nr
== 1 &&
3706 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
3707 opts
->revs
->no_walk
&&
3708 !opts
->revs
->cmdline
.rev
->flags
) {
3709 struct commit
*cmit
;
3710 if (prepare_revision_walk(opts
->revs
))
3711 return error(_("revision walk setup failed"));
3712 cmit
= get_revision(opts
->revs
);
3713 if (!cmit
|| get_revision(opts
->revs
))
3714 return error("BUG: expected exactly one commit from walk");
3715 return single_pick(cmit
, opts
);
3719 * Start a new cherry-pick/ revert sequence; but
3720 * first, make sure that an existing one isn't in
3724 if (walk_revs_populate_todo(&todo_list
, opts
) ||
3725 create_seq_dir() < 0)
3727 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
3728 return error(_("can't revert as initial commit"));
3729 if (save_head(oid_to_hex(&oid
)))
3731 if (save_opts(opts
))
3733 update_abort_safety_file();
3734 res
= pick_commits(&todo_list
, opts
);
3735 todo_list_release(&todo_list
);
3739 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
3741 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
3742 struct strbuf sob
= STRBUF_INIT
;
3745 strbuf_addstr(&sob
, sign_off_header
);
3746 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
3747 getenv("GIT_COMMITTER_EMAIL")));
3748 strbuf_addch(&sob
, '\n');
3751 strbuf_complete_line(msgbuf
);
3754 * If the whole message buffer is equal to the sob, pretend that we
3755 * found a conforming footer with a matching sob
3757 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
3758 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
3761 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
3764 const char *append_newlines
= NULL
;
3765 size_t len
= msgbuf
->len
- ignore_footer
;
3769 * The buffer is completely empty. Leave foom for
3770 * the title and body to be filled in by the user.
3772 append_newlines
= "\n\n";
3773 } else if (len
== 1) {
3775 * Buffer contains a single newline. Add another
3776 * so that we leave room for the title and body.
3778 append_newlines
= "\n";
3779 } else if (msgbuf
->buf
[len
- 2] != '\n') {
3781 * Buffer ends with a single newline. Add another
3782 * so that there is an empty line between the message
3785 append_newlines
= "\n";
3786 } /* else, the buffer already ends with two newlines. */
3788 if (append_newlines
)
3789 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
3790 append_newlines
, strlen(append_newlines
));
3793 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
3794 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
3797 strbuf_release(&sob
);
3800 struct labels_entry
{
3801 struct hashmap_entry entry
;
3802 char label
[FLEX_ARRAY
];
3805 static int labels_cmp(const void *fndata
, const struct labels_entry
*a
,
3806 const struct labels_entry
*b
, const void *key
)
3808 return key
? strcmp(a
->label
, key
) : strcmp(a
->label
, b
->label
);
3811 struct string_entry
{
3812 struct oidmap_entry entry
;
3813 char string
[FLEX_ARRAY
];
3816 struct label_state
{
3817 struct oidmap commit2label
;
3818 struct hashmap labels
;
3822 static const char *label_oid(struct object_id
*oid
, const char *label
,
3823 struct label_state
*state
)
3825 struct labels_entry
*labels_entry
;
3826 struct string_entry
*string_entry
;
3827 struct object_id dummy
;
3831 string_entry
= oidmap_get(&state
->commit2label
, oid
);
3833 return string_entry
->string
;
3836 * For "uninteresting" commits, i.e. commits that are not to be
3837 * rebased, and which can therefore not be labeled, we use a unique
3838 * abbreviation of the commit name. This is slightly more complicated
3839 * than calling find_unique_abbrev() because we also need to make
3840 * sure that the abbreviation does not conflict with any other
3843 * We disallow "interesting" commits to be labeled by a string that
3844 * is a valid full-length hash, to ensure that we always can find an
3845 * abbreviation for any uninteresting commit's names that does not
3846 * clash with any other label.
3851 strbuf_reset(&state
->buf
);
3852 strbuf_grow(&state
->buf
, GIT_SHA1_HEXSZ
);
3853 label
= p
= state
->buf
.buf
;
3855 find_unique_abbrev_r(p
, oid
, default_abbrev
);
3858 * We may need to extend the abbreviated hash so that there is
3859 * no conflicting label.
3861 if (hashmap_get_from_hash(&state
->labels
, strihash(p
), p
)) {
3862 size_t i
= strlen(p
) + 1;
3864 oid_to_hex_r(p
, oid
);
3865 for (; i
< GIT_SHA1_HEXSZ
; i
++) {
3868 if (!hashmap_get_from_hash(&state
->labels
,
3874 } else if (((len
= strlen(label
)) == the_hash_algo
->hexsz
&&
3875 !get_oid_hex(label
, &dummy
)) ||
3876 (len
== 1 && *label
== '#') ||
3877 hashmap_get_from_hash(&state
->labels
,
3878 strihash(label
), label
)) {
3880 * If the label already exists, or if the label is a valid full
3881 * OID, or the label is a '#' (which we use as a separator
3882 * between merge heads and oneline), we append a dash and a
3883 * number to make it unique.
3885 struct strbuf
*buf
= &state
->buf
;
3888 strbuf_add(buf
, label
, len
);
3890 for (i
= 2; ; i
++) {
3891 strbuf_setlen(buf
, len
);
3892 strbuf_addf(buf
, "-%d", i
);
3893 if (!hashmap_get_from_hash(&state
->labels
,
3902 FLEX_ALLOC_STR(labels_entry
, label
, label
);
3903 hashmap_entry_init(labels_entry
, strihash(label
));
3904 hashmap_add(&state
->labels
, labels_entry
);
3906 FLEX_ALLOC_STR(string_entry
, string
, label
);
3907 oidcpy(&string_entry
->entry
.oid
, oid
);
3908 oidmap_put(&state
->commit2label
, string_entry
);
3910 return string_entry
->string
;
3913 static int make_script_with_merges(struct pretty_print_context
*pp
,
3914 struct rev_info
*revs
, FILE *out
,
3917 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
3918 int rebase_cousins
= flags
& TODO_LIST_REBASE_COUSINS
;
3919 struct strbuf buf
= STRBUF_INIT
, oneline
= STRBUF_INIT
;
3920 struct strbuf label
= STRBUF_INIT
;
3921 struct commit_list
*commits
= NULL
, **tail
= &commits
, *iter
;
3922 struct commit_list
*tips
= NULL
, **tips_tail
= &tips
;
3923 struct commit
*commit
;
3924 struct oidmap commit2todo
= OIDMAP_INIT
;
3925 struct string_entry
*entry
;
3926 struct oidset interesting
= OIDSET_INIT
, child_seen
= OIDSET_INIT
,
3927 shown
= OIDSET_INIT
;
3928 struct label_state state
= { OIDMAP_INIT
, { NULL
}, STRBUF_INIT
};
3930 int abbr
= flags
& TODO_LIST_ABBREVIATE_CMDS
;
3931 const char *cmd_pick
= abbr
? "p" : "pick",
3932 *cmd_label
= abbr
? "l" : "label",
3933 *cmd_reset
= abbr
? "t" : "reset",
3934 *cmd_merge
= abbr
? "m" : "merge";
3936 oidmap_init(&commit2todo
, 0);
3937 oidmap_init(&state
.commit2label
, 0);
3938 hashmap_init(&state
.labels
, (hashmap_cmp_fn
) labels_cmp
, NULL
, 0);
3939 strbuf_init(&state
.buf
, 32);
3941 if (revs
->cmdline
.nr
&& (revs
->cmdline
.rev
[0].flags
& BOTTOM
)) {
3942 struct object_id
*oid
= &revs
->cmdline
.rev
[0].item
->oid
;
3943 FLEX_ALLOC_STR(entry
, string
, "onto");
3944 oidcpy(&entry
->entry
.oid
, oid
);
3945 oidmap_put(&state
.commit2label
, entry
);
3950 * - get onelines for all commits
3951 * - gather all branch tips (i.e. 2nd or later parents of merges)
3952 * - label all branch tips
3954 while ((commit
= get_revision(revs
))) {
3955 struct commit_list
*to_merge
;
3957 const char *p1
, *p2
;
3958 struct object_id
*oid
;
3961 tail
= &commit_list_insert(commit
, tail
)->next
;
3962 oidset_insert(&interesting
, &commit
->object
.oid
);
3964 is_empty
= is_original_commit_empty(commit
);
3965 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
3968 strbuf_reset(&oneline
);
3969 pretty_print_commit(pp
, commit
, &oneline
);
3971 to_merge
= commit
->parents
? commit
->parents
->next
: NULL
;
3973 /* non-merge commit: easy case */
3975 if (!keep_empty
&& is_empty
)
3976 strbuf_addf(&buf
, "%c ", comment_line_char
);
3977 strbuf_addf(&buf
, "%s %s %s", cmd_pick
,
3978 oid_to_hex(&commit
->object
.oid
),
3981 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
3982 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
3983 oidmap_put(&commit2todo
, entry
);
3988 is_octopus
= to_merge
&& to_merge
->next
;
3991 BUG("Octopus merges not yet supported");
3993 /* Create a label */
3994 strbuf_reset(&label
);
3995 if (skip_prefix(oneline
.buf
, "Merge ", &p1
) &&
3996 (p1
= strchr(p1
, '\'')) &&
3997 (p2
= strchr(++p1
, '\'')))
3998 strbuf_add(&label
, p1
, p2
- p1
);
3999 else if (skip_prefix(oneline
.buf
, "Merge pull request ",
4001 (p1
= strstr(p1
, " from ")))
4002 strbuf_addstr(&label
, p1
+ strlen(" from "));
4004 strbuf_addbuf(&label
, &oneline
);
4006 for (p1
= label
.buf
; *p1
; p1
++)
4011 strbuf_addf(&buf
, "%s -C %s",
4012 cmd_merge
, oid_to_hex(&commit
->object
.oid
));
4014 /* label the tip of merged branch */
4015 oid
= &to_merge
->item
->object
.oid
;
4016 strbuf_addch(&buf
, ' ');
4018 if (!oidset_contains(&interesting
, oid
))
4019 strbuf_addstr(&buf
, label_oid(oid
, NULL
, &state
));
4021 tips_tail
= &commit_list_insert(to_merge
->item
,
4024 strbuf_addstr(&buf
, label_oid(oid
, label
.buf
, &state
));
4026 strbuf_addf(&buf
, " # %s", oneline
.buf
);
4028 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
4029 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
4030 oidmap_put(&commit2todo
, entry
);
4035 * - label branch points
4036 * - add HEAD to the branch tips
4038 for (iter
= commits
; iter
; iter
= iter
->next
) {
4039 struct commit_list
*parent
= iter
->item
->parents
;
4040 for (; parent
; parent
= parent
->next
) {
4041 struct object_id
*oid
= &parent
->item
->object
.oid
;
4042 if (!oidset_contains(&interesting
, oid
))
4044 if (!oidset_contains(&child_seen
, oid
))
4045 oidset_insert(&child_seen
, oid
);
4047 label_oid(oid
, "branch-point", &state
);
4050 /* Add HEAD as implict "tip of branch" */
4052 tips_tail
= &commit_list_insert(iter
->item
,
4057 * Third phase: output the todo list. This is a bit tricky, as we
4058 * want to avoid jumping back and forth between revisions. To
4059 * accomplish that goal, we walk backwards from the branch tips,
4060 * gathering commits not yet shown, reversing the list on the fly,
4061 * then outputting that list (labeling revisions as needed).
4063 fprintf(out
, "%s onto\n", cmd_label
);
4064 for (iter
= tips
; iter
; iter
= iter
->next
) {
4065 struct commit_list
*list
= NULL
, *iter2
;
4067 commit
= iter
->item
;
4068 if (oidset_contains(&shown
, &commit
->object
.oid
))
4070 entry
= oidmap_get(&state
.commit2label
, &commit
->object
.oid
);
4073 fprintf(out
, "\n# Branch %s\n", entry
->string
);
4077 while (oidset_contains(&interesting
, &commit
->object
.oid
) &&
4078 !oidset_contains(&shown
, &commit
->object
.oid
)) {
4079 commit_list_insert(commit
, &list
);
4080 if (!commit
->parents
) {
4084 commit
= commit
->parents
->item
;
4088 fprintf(out
, "%s %s\n", cmd_reset
,
4089 rebase_cousins
? "onto" : "[new root]");
4091 const char *to
= NULL
;
4093 entry
= oidmap_get(&state
.commit2label
,
4094 &commit
->object
.oid
);
4097 else if (!rebase_cousins
)
4098 to
= label_oid(&commit
->object
.oid
, NULL
,
4101 if (!to
|| !strcmp(to
, "onto"))
4102 fprintf(out
, "%s onto\n", cmd_reset
);
4104 strbuf_reset(&oneline
);
4105 pretty_print_commit(pp
, commit
, &oneline
);
4106 fprintf(out
, "%s %s # %s\n",
4107 cmd_reset
, to
, oneline
.buf
);
4111 for (iter2
= list
; iter2
; iter2
= iter2
->next
) {
4112 struct object_id
*oid
= &iter2
->item
->object
.oid
;
4113 entry
= oidmap_get(&commit2todo
, oid
);
4114 /* only show if not already upstream */
4116 fprintf(out
, "%s\n", entry
->string
);
4117 entry
= oidmap_get(&state
.commit2label
, oid
);
4119 fprintf(out
, "%s %s\n",
4120 cmd_label
, entry
->string
);
4121 oidset_insert(&shown
, oid
);
4124 free_commit_list(list
);
4127 free_commit_list(commits
);
4128 free_commit_list(tips
);
4130 strbuf_release(&label
);
4131 strbuf_release(&oneline
);
4132 strbuf_release(&buf
);
4134 oidmap_free(&commit2todo
, 1);
4135 oidmap_free(&state
.commit2label
, 1);
4136 hashmap_free(&state
.labels
, 1);
4137 strbuf_release(&state
.buf
);
4142 int sequencer_make_script(FILE *out
, int argc
, const char **argv
,
4145 char *format
= NULL
;
4146 struct pretty_print_context pp
= {0};
4147 struct strbuf buf
= STRBUF_INIT
;
4148 struct rev_info revs
;
4149 struct commit
*commit
;
4150 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
4151 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
4152 int rebase_merges
= flags
& TODO_LIST_REBASE_MERGES
;
4154 init_revisions(&revs
, NULL
);
4155 revs
.verbose_header
= 1;
4157 revs
.max_parents
= 1;
4158 revs
.cherry_mark
= 1;
4161 revs
.right_only
= 1;
4162 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
4163 revs
.topo_order
= 1;
4165 revs
.pretty_given
= 1;
4166 git_config_get_string("rebase.instructionFormat", &format
);
4167 if (!format
|| !*format
) {
4169 format
= xstrdup("%s");
4171 get_commit_format(format
, &revs
);
4173 pp
.fmt
= revs
.commit_format
;
4174 pp
.output_encoding
= get_log_output_encoding();
4176 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
4177 return error(_("make_script: unhandled options"));
4179 if (prepare_revision_walk(&revs
) < 0)
4180 return error(_("make_script: error preparing revisions"));
4183 return make_script_with_merges(&pp
, &revs
, out
, flags
);
4185 while ((commit
= get_revision(&revs
))) {
4186 int is_empty
= is_original_commit_empty(commit
);
4188 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
4191 if (!keep_empty
&& is_empty
)
4192 strbuf_addf(&buf
, "%c ", comment_line_char
);
4193 strbuf_addf(&buf
, "%s %s ", insn
,
4194 oid_to_hex(&commit
->object
.oid
));
4195 pretty_print_commit(&pp
, commit
, &buf
);
4196 strbuf_addch(&buf
, '\n');
4197 fputs(buf
.buf
, out
);
4199 strbuf_release(&buf
);
4204 * Add commands after pick and (series of) squash/fixup commands
4207 int sequencer_add_exec_commands(const char *commands
)
4209 const char *todo_file
= rebase_path_todo();
4210 struct todo_list todo_list
= TODO_LIST_INIT
;
4211 struct todo_item
*item
;
4212 struct strbuf
*buf
= &todo_list
.buf
;
4213 size_t offset
= 0, commands_len
= strlen(commands
);
4216 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
4217 return error(_("could not read '%s'."), todo_file
);
4219 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
4220 todo_list_release(&todo_list
);
4221 return error(_("unusable todo list: '%s'"), todo_file
);
4225 /* insert <commands> before every pick except the first one */
4226 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
4227 if (item
->command
== TODO_PICK
&& !first
) {
4228 strbuf_insert(buf
, item
->offset_in_buf
+ offset
,
4229 commands
, commands_len
);
4230 offset
+= commands_len
;
4235 /* append final <commands> */
4236 strbuf_add(buf
, commands
, commands_len
);
4238 i
= write_message(buf
->buf
, buf
->len
, todo_file
, 0);
4239 todo_list_release(&todo_list
);
4243 int transform_todos(unsigned flags
)
4245 const char *todo_file
= rebase_path_todo();
4246 struct todo_list todo_list
= TODO_LIST_INIT
;
4247 struct strbuf buf
= STRBUF_INIT
;
4248 struct todo_item
*item
;
4251 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
4252 return error(_("could not read '%s'."), todo_file
);
4254 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
4255 todo_list_release(&todo_list
);
4256 return error(_("unusable todo list: '%s'"), todo_file
);
4259 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
4260 /* if the item is not a command write it and continue */
4261 if (item
->command
>= TODO_COMMENT
) {
4262 strbuf_addf(&buf
, "%.*s\n", item
->arg_len
, item
->arg
);
4266 /* add command to the buffer */
4267 if (flags
& TODO_LIST_ABBREVIATE_CMDS
)
4268 strbuf_addch(&buf
, command_to_char(item
->command
));
4270 strbuf_addstr(&buf
, command_to_string(item
->command
));
4274 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
4275 short_commit_name(item
->commit
) :
4276 oid_to_hex(&item
->commit
->object
.oid
);
4278 if (item
->command
== TODO_MERGE
) {
4279 if (item
->flags
& TODO_EDIT_MERGE_MSG
)
4280 strbuf_addstr(&buf
, " -c");
4282 strbuf_addstr(&buf
, " -C");
4285 strbuf_addf(&buf
, " %s", oid
);
4288 /* add all the rest */
4290 strbuf_addch(&buf
, '\n');
4292 strbuf_addf(&buf
, " %.*s\n", item
->arg_len
, item
->arg
);
4295 i
= write_message(buf
.buf
, buf
.len
, todo_file
, 0);
4296 todo_list_release(&todo_list
);
4300 enum missing_commit_check_level
get_missing_commit_check_level(void)
4304 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
4305 !strcasecmp("ignore", value
))
4306 return MISSING_COMMIT_CHECK_IGNORE
;
4307 if (!strcasecmp("warn", value
))
4308 return MISSING_COMMIT_CHECK_WARN
;
4309 if (!strcasecmp("error", value
))
4310 return MISSING_COMMIT_CHECK_ERROR
;
4311 warning(_("unrecognized setting %s for option "
4312 "rebase.missingCommitsCheck. Ignoring."), value
);
4313 return MISSING_COMMIT_CHECK_IGNORE
;
4316 define_commit_slab(commit_seen
, unsigned char);
4318 * Check if the user dropped some commits by mistake
4319 * Behaviour determined by rebase.missingCommitsCheck.
4320 * Check if there is an unrecognized command or a
4321 * bad SHA-1 in a command.
4323 int check_todo_list(void)
4325 enum missing_commit_check_level check_level
= get_missing_commit_check_level();
4326 struct strbuf todo_file
= STRBUF_INIT
;
4327 struct todo_list todo_list
= TODO_LIST_INIT
;
4328 struct strbuf missing
= STRBUF_INIT
;
4329 int advise_to_edit_todo
= 0, res
= 0, i
;
4330 struct commit_seen commit_seen
;
4332 init_commit_seen(&commit_seen
);
4334 strbuf_addstr(&todo_file
, rebase_path_todo());
4335 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
4339 advise_to_edit_todo
= res
=
4340 parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
4342 if (res
|| check_level
== MISSING_COMMIT_CHECK_IGNORE
)
4345 /* Mark the commits in git-rebase-todo as seen */
4346 for (i
= 0; i
< todo_list
.nr
; i
++) {
4347 struct commit
*commit
= todo_list
.items
[i
].commit
;
4349 *commit_seen_at(&commit_seen
, commit
) = 1;
4352 todo_list_release(&todo_list
);
4353 strbuf_addstr(&todo_file
, ".backup");
4354 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
4358 strbuf_release(&todo_file
);
4359 res
= !!parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
4361 /* Find commits in git-rebase-todo.backup yet unseen */
4362 for (i
= todo_list
.nr
- 1; i
>= 0; i
--) {
4363 struct todo_item
*item
= todo_list
.items
+ i
;
4364 struct commit
*commit
= item
->commit
;
4365 if (commit
&& !*commit_seen_at(&commit_seen
, commit
)) {
4366 strbuf_addf(&missing
, " - %s %.*s\n",
4367 short_commit_name(commit
),
4368 item
->arg_len
, item
->arg
);
4369 *commit_seen_at(&commit_seen
, commit
) = 1;
4373 /* Warn about missing commits */
4377 if (check_level
== MISSING_COMMIT_CHECK_ERROR
)
4378 advise_to_edit_todo
= res
= 1;
4381 _("Warning: some commits may have been dropped accidentally.\n"
4382 "Dropped commits (newer to older):\n"));
4384 /* Make the list user-friendly and display */
4385 fputs(missing
.buf
, stderr
);
4386 strbuf_release(&missing
);
4388 fprintf(stderr
, _("To avoid this message, use \"drop\" to "
4389 "explicitly remove a commit.\n\n"
4390 "Use 'git config rebase.missingCommitsCheck' to change "
4391 "the level of warnings.\n"
4392 "The possible behaviours are: ignore, warn, error.\n\n"));
4395 clear_commit_seen(&commit_seen
);
4396 strbuf_release(&todo_file
);
4397 todo_list_release(&todo_list
);
4399 if (advise_to_edit_todo
)
4401 _("You can fix this with 'git rebase --edit-todo' "
4402 "and then run 'git rebase --continue'.\n"
4403 "Or you can abort the rebase with 'git rebase"
4409 static int rewrite_file(const char *path
, const char *buf
, size_t len
)
4412 int fd
= open(path
, O_WRONLY
| O_TRUNC
);
4414 return error_errno(_("could not open '%s' for writing"), path
);
4415 if (write_in_full(fd
, buf
, len
) < 0)
4416 rc
= error_errno(_("could not write to '%s'"), path
);
4417 if (close(fd
) && !rc
)
4418 rc
= error_errno(_("could not close '%s'"), path
);
4422 /* skip picking commits whose parents are unchanged */
4423 static int skip_unnecessary_picks(struct object_id
*output_oid
)
4425 const char *todo_file
= rebase_path_todo();
4426 struct strbuf buf
= STRBUF_INIT
;
4427 struct todo_list todo_list
= TODO_LIST_INIT
;
4428 struct object_id
*parent_oid
;
4431 if (!read_oneliner(&buf
, rebase_path_onto(), 0))
4432 return error(_("could not read 'onto'"));
4433 if (get_oid(buf
.buf
, output_oid
)) {
4434 strbuf_release(&buf
);
4435 return error(_("need a HEAD to fixup"));
4437 strbuf_release(&buf
);
4439 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
4441 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
4442 todo_list_release(&todo_list
);
4446 for (i
= 0; i
< todo_list
.nr
; i
++) {
4447 struct todo_item
*item
= todo_list
.items
+ i
;
4449 if (item
->command
>= TODO_NOOP
)
4451 if (item
->command
!= TODO_PICK
)
4453 if (parse_commit(item
->commit
)) {
4454 todo_list_release(&todo_list
);
4455 return error(_("could not parse commit '%s'"),
4456 oid_to_hex(&item
->commit
->object
.oid
));
4458 if (!item
->commit
->parents
)
4459 break; /* root commit */
4460 if (item
->commit
->parents
->next
)
4461 break; /* merge commit */
4462 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
4463 if (hashcmp(parent_oid
->hash
, output_oid
->hash
))
4465 oidcpy(output_oid
, &item
->commit
->object
.oid
);
4468 int offset
= get_item_line_offset(&todo_list
, i
);
4469 const char *done_path
= rebase_path_done();
4471 fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
4473 error_errno(_("could not open '%s' for writing"),
4475 todo_list_release(&todo_list
);
4478 if (write_in_full(fd
, todo_list
.buf
.buf
, offset
) < 0) {
4479 error_errno(_("could not write to '%s'"), done_path
);
4480 todo_list_release(&todo_list
);
4486 if (rewrite_file(rebase_path_todo(), todo_list
.buf
.buf
+ offset
,
4487 todo_list
.buf
.len
- offset
) < 0) {
4488 todo_list_release(&todo_list
);
4492 todo_list
.current
= i
;
4493 if (is_fixup(peek_command(&todo_list
, 0)))
4494 record_in_rewritten(output_oid
, peek_command(&todo_list
, 0));
4497 todo_list_release(&todo_list
);
4502 int complete_action(struct replay_opts
*opts
, unsigned flags
,
4503 const char *shortrevisions
, const char *onto_name
,
4504 const char *onto
, const char *orig_head
, const char *cmd
,
4505 unsigned autosquash
)
4507 const char *shortonto
, *todo_file
= rebase_path_todo();
4508 struct todo_list todo_list
= TODO_LIST_INIT
;
4509 struct strbuf
*buf
= &(todo_list
.buf
);
4510 struct object_id oid
;
4513 get_oid(onto
, &oid
);
4514 shortonto
= find_unique_abbrev(&oid
, DEFAULT_ABBREV
);
4516 if (!lstat(todo_file
, &st
) && st
.st_size
== 0 &&
4517 write_message("noop\n", 5, todo_file
, 0))
4520 if (autosquash
&& rearrange_squash())
4524 sequencer_add_exec_commands(cmd
);
4526 if (strbuf_read_file(buf
, todo_file
, 0) < 0)
4527 return error_errno(_("could not read '%s'."), todo_file
);
4529 if (parse_insn_buffer(buf
->buf
, &todo_list
)) {
4530 todo_list_release(&todo_list
);
4531 return error(_("unusable todo list: '%s'"), todo_file
);
4534 if (count_commands(&todo_list
) == 0) {
4535 apply_autostash(opts
);
4536 sequencer_remove_state(opts
);
4537 todo_list_release(&todo_list
);
4539 return error(_("nothing to do"));
4542 strbuf_addch(buf
, '\n');
4543 strbuf_commented_addf(buf
, Q_("Rebase %s onto %s (%d command)",
4544 "Rebase %s onto %s (%d commands)",
4545 count_commands(&todo_list
)),
4546 shortrevisions
, shortonto
, count_commands(&todo_list
));
4547 append_todo_help(0, flags
& TODO_LIST_KEEP_EMPTY
, buf
);
4549 if (write_message(buf
->buf
, buf
->len
, todo_file
, 0)) {
4550 todo_list_release(&todo_list
);
4554 if (copy_file(rebase_path_todo_backup(), todo_file
, 0666))
4555 return error(_("could not copy '%s' to '%s'."), todo_file
,
4556 rebase_path_todo_backup());
4558 if (transform_todos(flags
| TODO_LIST_SHORTEN_IDS
))
4559 return error(_("could not transform the todo list"));
4563 if (launch_sequence_editor(todo_file
, buf
, NULL
)) {
4564 apply_autostash(opts
);
4565 sequencer_remove_state(opts
);
4566 todo_list_release(&todo_list
);
4571 strbuf_stripspace(buf
, 1);
4572 if (buf
->len
== 0) {
4573 apply_autostash(opts
);
4574 sequencer_remove_state(opts
);
4575 todo_list_release(&todo_list
);
4577 return error(_("nothing to do"));
4580 todo_list_release(&todo_list
);
4582 if (check_todo_list()) {
4583 checkout_onto(opts
, onto_name
, onto
, orig_head
);
4587 if (transform_todos(flags
& ~(TODO_LIST_SHORTEN_IDS
)))
4588 return error(_("could not transform the todo list"));
4590 if (opts
->allow_ff
&& skip_unnecessary_picks(&oid
))
4591 return error(_("could not skip unnecessary pick commands"));
4593 if (checkout_onto(opts
, onto_name
, oid_to_hex(&oid
), orig_head
))
4596 if (require_clean_work_tree("rebase", "", 1, 1))
4599 return sequencer_continue(opts
);
4602 struct subject2item_entry
{
4603 struct hashmap_entry entry
;
4605 char subject
[FLEX_ARRAY
];
4608 static int subject2item_cmp(const void *fndata
,
4609 const struct subject2item_entry
*a
,
4610 const struct subject2item_entry
*b
, const void *key
)
4612 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
4615 define_commit_slab(commit_todo_item
, struct todo_item
*);
4618 * Rearrange the todo list that has both "pick commit-id msg" and "pick
4619 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
4620 * after the former, and change "pick" to "fixup"/"squash".
4622 * Note that if the config has specified a custom instruction format, each log
4623 * message will have to be retrieved from the commit (as the oneline in the
4624 * script cannot be trusted) in order to normalize the autosquash arrangement.
4626 int rearrange_squash(void)
4628 const char *todo_file
= rebase_path_todo();
4629 struct todo_list todo_list
= TODO_LIST_INIT
;
4630 struct hashmap subject2item
;
4631 int res
= 0, rearranged
= 0, *next
, *tail
, i
;
4633 struct commit_todo_item commit_todo
;
4635 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
4637 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
4638 todo_list_release(&todo_list
);
4642 init_commit_todo_item(&commit_todo
);
4644 * The hashmap maps onelines to the respective todo list index.
4646 * If any items need to be rearranged, the next[i] value will indicate
4647 * which item was moved directly after the i'th.
4649 * In that case, last[i] will indicate the index of the latest item to
4650 * be moved to appear after the i'th.
4652 hashmap_init(&subject2item
, (hashmap_cmp_fn
) subject2item_cmp
,
4653 NULL
, todo_list
.nr
);
4654 ALLOC_ARRAY(next
, todo_list
.nr
);
4655 ALLOC_ARRAY(tail
, todo_list
.nr
);
4656 ALLOC_ARRAY(subjects
, todo_list
.nr
);
4657 for (i
= 0; i
< todo_list
.nr
; i
++) {
4658 struct strbuf buf
= STRBUF_INIT
;
4659 struct todo_item
*item
= todo_list
.items
+ i
;
4660 const char *commit_buffer
, *subject
, *p
;
4663 struct subject2item_entry
*entry
;
4665 next
[i
] = tail
[i
] = -1;
4666 if (!item
->commit
|| item
->command
== TODO_DROP
) {
4671 if (is_fixup(item
->command
)) {
4672 todo_list_release(&todo_list
);
4673 clear_commit_todo_item(&commit_todo
);
4674 return error(_("the script was already rearranged."));
4677 *commit_todo_item_at(&commit_todo
, item
->commit
) = item
;
4679 parse_commit(item
->commit
);
4680 commit_buffer
= get_commit_buffer(item
->commit
, NULL
);
4681 find_commit_subject(commit_buffer
, &subject
);
4682 format_subject(&buf
, subject
, " ");
4683 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
4684 unuse_commit_buffer(item
->commit
, commit_buffer
);
4685 if ((skip_prefix(subject
, "fixup! ", &p
) ||
4686 skip_prefix(subject
, "squash! ", &p
))) {
4687 struct commit
*commit2
;
4692 if (!skip_prefix(p
, "fixup! ", &p
) &&
4693 !skip_prefix(p
, "squash! ", &p
))
4697 if ((entry
= hashmap_get_from_hash(&subject2item
,
4699 /* found by title */
4701 else if (!strchr(p
, ' ') &&
4703 lookup_commit_reference_by_name(p
)) &&
4704 *commit_todo_item_at(&commit_todo
, commit2
))
4705 /* found by commit name */
4706 i2
= *commit_todo_item_at(&commit_todo
, commit2
)
4709 /* copy can be a prefix of the commit subject */
4710 for (i2
= 0; i2
< i
; i2
++)
4712 starts_with(subjects
[i2
], p
))
4720 todo_list
.items
[i
].command
=
4721 starts_with(subject
, "fixup!") ?
4722 TODO_FIXUP
: TODO_SQUASH
;
4728 } else if (!hashmap_get_from_hash(&subject2item
,
4729 strhash(subject
), subject
)) {
4730 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
4732 hashmap_entry_init(entry
, strhash(entry
->subject
));
4733 hashmap_put(&subject2item
, entry
);
4738 struct strbuf buf
= STRBUF_INIT
;
4740 for (i
= 0; i
< todo_list
.nr
; i
++) {
4741 enum todo_command command
= todo_list
.items
[i
].command
;
4745 * Initially, all commands are 'pick's. If it is a
4746 * fixup or a squash now, we have rearranged it.
4748 if (is_fixup(command
))
4753 get_item_line(&todo_list
, cur
);
4755 get_item_line(&todo_list
, cur
+ 1);
4757 /* replace 'pick', by 'fixup' or 'squash' */
4758 command
= todo_list
.items
[cur
].command
;
4759 if (is_fixup(command
)) {
4761 todo_command_info
[command
].str
);
4762 bol
+= strcspn(bol
, " \t");
4765 strbuf_add(&buf
, bol
, eol
- bol
);
4771 res
= rewrite_file(todo_file
, buf
.buf
, buf
.len
);
4772 strbuf_release(&buf
);
4777 for (i
= 0; i
< todo_list
.nr
; i
++)
4780 hashmap_free(&subject2item
, 1);
4781 todo_list_release(&todo_list
);
4783 clear_commit_todo_item(&commit_todo
);