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"
34 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
36 const char sign_off_header
[] = "Signed-off-by: ";
37 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
39 GIT_PATH_FUNC(git_path_commit_editmsg
, "COMMIT_EDITMSG")
41 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
43 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
44 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
45 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
46 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
48 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
50 * The file containing rebase commands, comments, and empty lines.
51 * This file is created by "git rebase -i" then edited by the user. As
52 * the lines are processed, they are removed from the front of this
53 * file and written to the tail of 'done'.
55 static GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
57 * The rebase command lines that have already been processed. A line
58 * is moved here when it is first handled, before any associated user
61 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
63 * The file to keep track of how many commands were already processed (e.g.
66 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum");
68 * The file to keep track of how many commands are to be processed in total
69 * (e.g. for the prompt).
71 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end");
73 * The commit message that is planned to be used for any changes that
74 * need to be committed following a user interaction.
76 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
78 * The file into which is accumulated the suggested commit message for
79 * squash/fixup commands. When the first of a series of squash/fixups
80 * is seen, the file is created and the commit message from the
81 * previous commit and from the first squash/fixup commit are written
82 * to it. The commit message for each subsequent squash/fixup commit
83 * is appended to the file as it is processed.
85 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
87 * If the current series of squash/fixups has not yet included a squash
88 * command, then this file exists and holds the commit message of the
89 * original "pick" commit. (If the series ends without a "squash"
90 * command, then this can be used as the commit message of the combined
91 * commit without opening the editor.)
93 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
95 * This file contains the list fixup/squash commands that have been
96 * accumulated into message-fixup or message-squash so far.
98 static GIT_PATH_FUNC(rebase_path_current_fixups
, "rebase-merge/current-fixups")
100 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
101 * GIT_AUTHOR_DATE that will be used for the commit that is currently
104 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
106 * When an "edit" rebase command is being processed, the SHA1 of the
107 * commit to be edited is recorded in this file. When "git rebase
108 * --continue" is executed, if there are any staged changes then they
109 * will be amended to the HEAD commit, but only provided the HEAD
110 * commit is still the commit to be edited. When any other rebase
111 * command is processed, this file is deleted.
113 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
115 * When we stop at a given patch via the "edit" command, this file contains
116 * the abbreviated commit name of the corresponding patch.
118 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
120 * For the post-rewrite hook, we make a list of rewritten commits and
121 * their new sha1s. The rewritten-pending list keeps the sha1s of
122 * commits that have been processed, but not committed yet,
123 * e.g. because they are waiting for a 'squash' command.
125 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
126 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
127 "rebase-merge/rewritten-pending")
130 * The path of the file containig the OID of the "squash onto" commit, i.e.
131 * the dummy commit used for `reset [new root]`.
133 static GIT_PATH_FUNC(rebase_path_squash_onto
, "rebase-merge/squash-onto")
136 * The path of the file listing refs that need to be deleted after the rebase
137 * finishes. This is used by the `label` command to record the need for cleanup.
139 static GIT_PATH_FUNC(rebase_path_refs_to_delete
, "rebase-merge/refs-to-delete")
142 * The following files are written by git-rebase just after parsing the
143 * command-line (and are only consumed, not modified, by the sequencer).
145 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
146 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
147 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
148 static GIT_PATH_FUNC(rebase_path_signoff
, "rebase-merge/signoff")
149 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
150 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
151 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
152 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
153 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
154 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
156 static int git_sequencer_config(const char *k
, const char *v
, void *cb
)
158 struct replay_opts
*opts
= cb
;
161 if (!strcmp(k
, "commit.cleanup")) {
164 status
= git_config_string(&s
, k
, v
);
168 if (!strcmp(s
, "verbatim"))
169 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
170 else if (!strcmp(s
, "whitespace"))
171 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
172 else if (!strcmp(s
, "strip"))
173 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
174 else if (!strcmp(s
, "scissors"))
175 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
177 warning(_("invalid commit message cleanup mode '%s'"),
184 if (!strcmp(k
, "commit.gpgsign")) {
185 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
189 status
= git_gpg_config(k
, v
, NULL
);
193 return git_diff_basic_config(k
, v
, NULL
);
196 void sequencer_init_config(struct replay_opts
*opts
)
198 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
199 git_config(git_sequencer_config
, opts
);
202 static inline int is_rebase_i(const struct replay_opts
*opts
)
204 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
207 static const char *get_dir(const struct replay_opts
*opts
)
209 if (is_rebase_i(opts
))
210 return rebase_path();
211 return git_path_seq_dir();
214 static const char *get_todo_path(const struct replay_opts
*opts
)
216 if (is_rebase_i(opts
))
217 return rebase_path_todo();
218 return git_path_todo_file();
222 * Returns 0 for non-conforming footer
223 * Returns 1 for conforming footer
224 * Returns 2 when sob exists within conforming footer
225 * Returns 3 when sob exists within conforming footer as last entry
227 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
230 struct trailer_info info
;
232 int found_sob
= 0, found_sob_last
= 0;
234 trailer_info_get(&info
, sb
->buf
);
236 if (info
.trailer_start
== info
.trailer_end
)
239 for (i
= 0; i
< info
.trailer_nr
; i
++)
240 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
242 if (i
== info
.trailer_nr
- 1)
246 trailer_info_release(&info
);
255 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
257 static struct strbuf buf
= STRBUF_INIT
;
261 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
265 int sequencer_remove_state(struct replay_opts
*opts
)
267 struct strbuf buf
= STRBUF_INIT
;
270 if (is_rebase_i(opts
) &&
271 strbuf_read_file(&buf
, rebase_path_refs_to_delete(), 0) > 0) {
274 char *eol
= strchr(p
, '\n');
277 if (delete_ref("(rebase -i) cleanup", p
, NULL
, 0) < 0)
278 warning(_("could not delete '%s'"), p
);
285 free(opts
->gpg_sign
);
286 free(opts
->strategy
);
287 for (i
= 0; i
< opts
->xopts_nr
; i
++)
288 free(opts
->xopts
[i
]);
290 strbuf_release(&opts
->current_fixups
);
293 strbuf_addstr(&buf
, get_dir(opts
));
294 remove_dir_recursively(&buf
, 0);
295 strbuf_release(&buf
);
300 static const char *action_name(const struct replay_opts
*opts
)
302 switch (opts
->action
) {
306 return N_("cherry-pick");
307 case REPLAY_INTERACTIVE_REBASE
:
308 return N_("rebase -i");
310 die(_("Unknown action: %d"), opts
->action
);
313 struct commit_message
{
320 static const char *short_commit_name(struct commit
*commit
)
322 return find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
);
325 static int get_message(struct commit
*commit
, struct commit_message
*out
)
327 const char *abbrev
, *subject
;
330 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
331 abbrev
= short_commit_name(commit
);
333 subject_len
= find_commit_subject(out
->message
, &subject
);
335 out
->subject
= xmemdupz(subject
, subject_len
);
336 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
337 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
342 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
344 free(msg
->parent_label
);
347 unuse_commit_buffer(commit
, msg
->message
);
350 static void print_advice(int show_hint
, struct replay_opts
*opts
)
352 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
355 fprintf(stderr
, "%s\n", msg
);
357 * A conflict has occurred but the porcelain
358 * (typically rebase --interactive) wants to take care
359 * of the commit itself so remove CHERRY_PICK_HEAD
361 unlink(git_path_cherry_pick_head(the_repository
));
367 advise(_("after resolving the conflicts, mark the corrected paths\n"
368 "with 'git add <paths>' or 'git rm <paths>'"));
370 advise(_("after resolving the conflicts, mark the corrected paths\n"
371 "with 'git add <paths>' or 'git rm <paths>'\n"
372 "and commit the result with 'git commit'"));
376 static int write_message(const void *buf
, size_t len
, const char *filename
,
379 struct lock_file msg_file
= LOCK_INIT
;
381 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
383 return error_errno(_("could not lock '%s'"), filename
);
384 if (write_in_full(msg_fd
, buf
, len
) < 0) {
385 error_errno(_("could not write to '%s'"), filename
);
386 rollback_lock_file(&msg_file
);
389 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
390 error_errno(_("could not write eol to '%s'"), filename
);
391 rollback_lock_file(&msg_file
);
394 if (commit_lock_file(&msg_file
) < 0)
395 return error(_("failed to finalize '%s'"), filename
);
401 * Reads a file that was presumably written by a shell script, i.e. with an
402 * end-of-line marker that needs to be stripped.
404 * Note that only the last end-of-line marker is stripped, consistent with the
405 * behavior of "$(cat path)" in a shell script.
407 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
409 static int read_oneliner(struct strbuf
*buf
,
410 const char *path
, int skip_if_empty
)
412 int orig_len
= buf
->len
;
414 if (!file_exists(path
))
417 if (strbuf_read_file(buf
, path
, 0) < 0) {
418 warning_errno(_("could not read '%s'"), path
);
422 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
423 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
425 buf
->buf
[buf
->len
] = '\0';
428 if (skip_if_empty
&& buf
->len
== orig_len
)
434 static struct tree
*empty_tree(void)
436 return lookup_tree(the_hash_algo
->empty_tree
);
439 static int error_dirty_index(struct replay_opts
*opts
)
441 if (read_cache_unmerged())
442 return error_resolve_conflict(_(action_name(opts
)));
444 error(_("your local changes would be overwritten by %s."),
445 _(action_name(opts
)));
447 if (advice_commit_before_merge
)
448 advise(_("commit your changes or stash them to proceed."));
452 static void update_abort_safety_file(void)
454 struct object_id head
;
456 /* Do nothing on a single-pick */
457 if (!file_exists(git_path_seq_dir()))
460 if (!get_oid("HEAD", &head
))
461 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
463 write_file(git_path_abort_safety_file(), "%s", "");
466 static int fast_forward_to(const struct object_id
*to
, const struct object_id
*from
,
467 int unborn
, struct replay_opts
*opts
)
469 struct ref_transaction
*transaction
;
470 struct strbuf sb
= STRBUF_INIT
;
471 struct strbuf err
= STRBUF_INIT
;
474 if (checkout_fast_forward(from
, to
, 1))
475 return -1; /* the callee should have complained already */
477 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
479 transaction
= ref_transaction_begin(&err
);
481 ref_transaction_update(transaction
, "HEAD",
482 to
, unborn
&& !is_rebase_i(opts
) ?
485 ref_transaction_commit(transaction
, &err
)) {
486 ref_transaction_free(transaction
);
487 error("%s", err
.buf
);
489 strbuf_release(&err
);
494 strbuf_release(&err
);
495 ref_transaction_free(transaction
);
496 update_abort_safety_file();
500 void append_conflicts_hint(struct strbuf
*msgbuf
)
504 strbuf_addch(msgbuf
, '\n');
505 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
506 for (i
= 0; i
< active_nr
;) {
507 const struct cache_entry
*ce
= active_cache
[i
++];
509 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
510 while (i
< active_nr
&& !strcmp(ce
->name
,
511 active_cache
[i
]->name
))
517 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
518 const char *base_label
, const char *next_label
,
519 struct object_id
*head
, struct strbuf
*msgbuf
,
520 struct replay_opts
*opts
)
522 struct merge_options o
;
523 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
526 struct lock_file index_lock
= LOCK_INIT
;
528 if (hold_locked_index(&index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
533 init_merge_options(&o
);
534 o
.ancestor
= base
? base_label
: "(empty tree)";
536 o
.branch2
= next
? next_label
: "(empty tree)";
537 if (is_rebase_i(opts
))
539 o
.show_rename_progress
= 1;
541 head_tree
= parse_tree_indirect(head
);
542 next_tree
= next
? get_commit_tree(next
) : empty_tree();
543 base_tree
= base
? get_commit_tree(base
) : empty_tree();
545 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
546 parse_merge_opt(&o
, *xopt
);
548 clean
= merge_trees(&o
,
550 next_tree
, base_tree
, &result
);
551 if (is_rebase_i(opts
) && clean
<= 0)
552 fputs(o
.obuf
.buf
, stdout
);
553 strbuf_release(&o
.obuf
);
554 diff_warn_rename_limit("merge.renamelimit", o
.needed_rename_limit
, 0);
556 rollback_lock_file(&index_lock
);
560 if (write_locked_index(&the_index
, &index_lock
,
561 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
563 * TRANSLATORS: %s will be "revert", "cherry-pick" or
566 return error(_("%s: Unable to write new index file"),
567 _(action_name(opts
)));
570 append_conflicts_hint(msgbuf
);
575 static struct object_id
*get_cache_tree_oid(void)
577 if (!active_cache_tree
)
578 active_cache_tree
= cache_tree();
580 if (!cache_tree_fully_valid(active_cache_tree
))
581 if (cache_tree_update(&the_index
, 0)) {
582 error(_("unable to update cache tree"));
586 return &active_cache_tree
->oid
;
589 static int is_index_unchanged(void)
591 struct object_id head_oid
, *cache_tree_oid
;
592 struct commit
*head_commit
;
594 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
595 return error(_("could not resolve HEAD commit"));
597 head_commit
= lookup_commit(&head_oid
);
600 * If head_commit is NULL, check_commit, called from
601 * lookup_commit, would have indicated that head_commit is not
602 * a commit object already. parse_commit() will return failure
603 * without further complaints in such a case. Otherwise, if
604 * the commit is invalid, parse_commit() will complain. So
605 * there is nothing for us to say here. Just return failure.
607 if (parse_commit(head_commit
))
610 if (!(cache_tree_oid
= get_cache_tree_oid()))
613 return !oidcmp(cache_tree_oid
, get_commit_tree_oid(head_commit
));
616 static int write_author_script(const char *message
)
618 struct strbuf buf
= STRBUF_INIT
;
623 if (!*message
|| starts_with(message
, "\n")) {
625 /* Missing 'author' line? */
626 unlink(rebase_path_author_script());
628 } else if (skip_prefix(message
, "author ", &message
))
630 else if ((eol
= strchr(message
, '\n')))
635 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
636 while (*message
&& *message
!= '\n' && *message
!= '\r')
637 if (skip_prefix(message
, " <", &message
))
639 else if (*message
!= '\'')
640 strbuf_addch(&buf
, *(message
++));
642 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
643 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
644 while (*message
&& *message
!= '\n' && *message
!= '\r')
645 if (skip_prefix(message
, "> ", &message
))
647 else if (*message
!= '\'')
648 strbuf_addch(&buf
, *(message
++));
650 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
651 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
652 while (*message
&& *message
!= '\n' && *message
!= '\r')
653 if (*message
!= '\'')
654 strbuf_addch(&buf
, *(message
++));
656 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
657 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
658 strbuf_release(&buf
);
663 * Read a list of environment variable assignments (such as the author-script
664 * file) into an environment block. Returns -1 on error, 0 otherwise.
666 static int read_env_script(struct argv_array
*env
)
668 struct strbuf script
= STRBUF_INIT
;
672 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
675 for (p
= script
.buf
; *p
; p
++)
676 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
677 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
679 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
680 else if (*p
== '\n') {
685 for (i
= 0, p
= script
.buf
; i
< count
; i
++) {
686 argv_array_push(env
, p
);
693 static char *get_author(const char *message
)
698 a
= find_commit_header(message
, "author", &len
);
700 return xmemdupz(a
, len
);
705 /* Read author-script and return an ident line (author <email> timestamp) */
706 static const char *read_author_ident(struct strbuf
*buf
)
708 const char *keys
[] = {
709 "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE="
711 char *in
, *out
, *eol
;
714 if (strbuf_read_file(buf
, rebase_path_author_script(), 256) <= 0)
717 /* dequote values and construct ident line in-place */
718 for (in
= out
= buf
->buf
; i
< 3 && in
- buf
->buf
< buf
->len
; i
++) {
719 if (!skip_prefix(in
, keys
[i
], (const char **)&in
)) {
720 warning("could not parse '%s' (looking for '%s'",
721 rebase_path_author_script(), keys
[i
]);
725 eol
= strchrnul(in
, '\n');
730 if (i
> 0) /* separate values by spaces */
732 if (i
== 1) /* email needs to be surrounded by <...> */
734 memmove(out
, in
, len
);
736 if (i
== 1) /* email needs to be surrounded by <...> */
742 warning("could not parse '%s' (looking for '%s')",
743 rebase_path_author_script(), keys
[i
]);
747 buf
->len
= out
- buf
->buf
;
751 static const char staged_changes_advice
[] =
752 N_("you have staged changes in your working tree\n"
753 "If these changes are meant to be squashed into the previous commit, run:\n"
755 " git commit --amend %s\n"
757 "If they are meant to go into a new commit, run:\n"
761 "In both cases, once you're done, continue with:\n"
763 " git rebase --continue\n");
765 #define ALLOW_EMPTY (1<<0)
766 #define EDIT_MSG (1<<1)
767 #define AMEND_MSG (1<<2)
768 #define CLEANUP_MSG (1<<3)
769 #define VERIFY_MSG (1<<4)
770 #define CREATE_ROOT_COMMIT (1<<5)
773 * If we are cherry-pick, and if the merge did not result in
774 * hand-editing, we will hit this commit and inherit the original
775 * author date and name.
777 * If we are revert, or if our cherry-pick results in a hand merge,
778 * we had better say that the current user is responsible for that.
780 * An exception is when run_git_commit() is called during an
781 * interactive rebase: in that case, we will want to retain the
784 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
787 struct child_process cmd
= CHILD_PROCESS_INIT
;
790 if ((flags
& CREATE_ROOT_COMMIT
) && !(flags
& AMEND_MSG
)) {
791 struct strbuf msg
= STRBUF_INIT
, script
= STRBUF_INIT
;
792 const char *author
= is_rebase_i(opts
) ?
793 read_author_ident(&script
) : NULL
;
794 struct object_id root_commit
, *cache_tree_oid
;
798 BUG("root commit without message");
800 if (!(cache_tree_oid
= get_cache_tree_oid()))
804 res
= strbuf_read_file(&msg
, defmsg
, 0);
807 res
= error_errno(_("could not read '%s'"), defmsg
);
809 res
= commit_tree(msg
.buf
, msg
.len
, cache_tree_oid
,
810 NULL
, &root_commit
, author
,
813 strbuf_release(&msg
);
814 strbuf_release(&script
);
816 update_ref(NULL
, "CHERRY_PICK_HEAD", &root_commit
, NULL
,
817 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
);
818 res
= update_ref(NULL
, "HEAD", &root_commit
, NULL
, 0,
819 UPDATE_REFS_MSG_ON_ERR
);
821 return res
< 0 ? error(_("writing root commit")) : 0;
826 if (is_rebase_i(opts
)) {
827 if (!(flags
& EDIT_MSG
)) {
828 cmd
.stdout_to_stderr
= 1;
832 if (read_env_script(&cmd
.env_array
)) {
833 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
835 return error(_(staged_changes_advice
),
840 argv_array_push(&cmd
.args
, "commit");
842 if (!(flags
& VERIFY_MSG
))
843 argv_array_push(&cmd
.args
, "-n");
844 if ((flags
& AMEND_MSG
))
845 argv_array_push(&cmd
.args
, "--amend");
847 argv_array_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
849 argv_array_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
850 else if (!(flags
& EDIT_MSG
))
851 argv_array_pushl(&cmd
.args
, "-C", "HEAD", NULL
);
852 if ((flags
& CLEANUP_MSG
))
853 argv_array_push(&cmd
.args
, "--cleanup=strip");
854 if ((flags
& EDIT_MSG
))
855 argv_array_push(&cmd
.args
, "-e");
856 else if (!(flags
& CLEANUP_MSG
) &&
857 !opts
->signoff
&& !opts
->record_origin
&&
858 git_config_get_value("commit.cleanup", &value
))
859 argv_array_push(&cmd
.args
, "--cleanup=verbatim");
861 if ((flags
& ALLOW_EMPTY
))
862 argv_array_push(&cmd
.args
, "--allow-empty");
864 if (opts
->allow_empty_message
)
865 argv_array_push(&cmd
.args
, "--allow-empty-message");
868 /* hide stderr on success */
869 struct strbuf buf
= STRBUF_INIT
;
870 int rc
= pipe_command(&cmd
,
872 /* stdout is already redirected */
876 fputs(buf
.buf
, stderr
);
877 strbuf_release(&buf
);
881 return run_command(&cmd
);
884 static int rest_is_empty(const struct strbuf
*sb
, int start
)
889 /* Check if the rest is just whitespace and Signed-off-by's. */
890 for (i
= start
; i
< sb
->len
; i
++) {
891 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
897 if (strlen(sign_off_header
) <= eol
- i
&&
898 starts_with(sb
->buf
+ i
, sign_off_header
)) {
903 if (!isspace(sb
->buf
[i
++]))
911 * Find out if the message in the strbuf contains only whitespace and
912 * Signed-off-by lines.
914 int message_is_empty(const struct strbuf
*sb
,
915 enum commit_msg_cleanup_mode cleanup_mode
)
917 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
919 return rest_is_empty(sb
, 0);
923 * See if the user edited the message in the editor or left what
924 * was in the template intact
926 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
927 enum commit_msg_cleanup_mode cleanup_mode
)
929 struct strbuf tmpl
= STRBUF_INIT
;
932 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
935 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
938 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
939 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
941 strbuf_release(&tmpl
);
942 return rest_is_empty(sb
, start
- sb
->buf
);
945 int update_head_with_reflog(const struct commit
*old_head
,
946 const struct object_id
*new_head
,
947 const char *action
, const struct strbuf
*msg
,
950 struct ref_transaction
*transaction
;
951 struct strbuf sb
= STRBUF_INIT
;
956 strbuf_addstr(&sb
, action
);
957 strbuf_addstr(&sb
, ": ");
960 nl
= strchr(msg
->buf
, '\n');
962 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
964 strbuf_addbuf(&sb
, msg
);
965 strbuf_addch(&sb
, '\n');
968 transaction
= ref_transaction_begin(err
);
970 ref_transaction_update(transaction
, "HEAD", new_head
,
971 old_head
? &old_head
->object
.oid
: &null_oid
,
973 ref_transaction_commit(transaction
, err
)) {
976 ref_transaction_free(transaction
);
982 static int run_rewrite_hook(const struct object_id
*oldoid
,
983 const struct object_id
*newoid
)
985 struct child_process proc
= CHILD_PROCESS_INIT
;
988 struct strbuf sb
= STRBUF_INIT
;
990 argv
[0] = find_hook("post-rewrite");
999 proc
.stdout_to_stderr
= 1;
1001 code
= start_command(&proc
);
1004 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
1005 sigchain_push(SIGPIPE
, SIG_IGN
);
1006 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
1008 strbuf_release(&sb
);
1009 sigchain_pop(SIGPIPE
);
1010 return finish_command(&proc
);
1013 void commit_post_rewrite(const struct commit
*old_head
,
1014 const struct object_id
*new_head
)
1016 struct notes_rewrite_cfg
*cfg
;
1018 cfg
= init_copy_notes_for_rewrite("amend");
1020 /* we are amending, so old_head is not NULL */
1021 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
1022 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
1024 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
1027 static int run_prepare_commit_msg_hook(struct strbuf
*msg
, const char *commit
)
1029 struct argv_array hook_env
= ARGV_ARRAY_INIT
;
1033 name
= git_path_commit_editmsg();
1034 if (write_message(msg
->buf
, msg
->len
, name
, 0))
1037 argv_array_pushf(&hook_env
, "GIT_INDEX_FILE=%s", get_index_file());
1038 argv_array_push(&hook_env
, "GIT_EDITOR=:");
1040 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
1041 "commit", commit
, NULL
);
1043 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
1046 ret
= error(_("'prepare-commit-msg' hook failed"));
1047 argv_array_clear(&hook_env
);
1052 static const char implicit_ident_advice_noconfig
[] =
1053 N_("Your name and email address were configured automatically based\n"
1054 "on your username and hostname. Please check that they are accurate.\n"
1055 "You can suppress this message by setting them explicitly. Run the\n"
1056 "following command and follow the instructions in your editor to edit\n"
1057 "your configuration file:\n"
1059 " git config --global --edit\n"
1061 "After doing this, you may fix the identity used for this commit with:\n"
1063 " git commit --amend --reset-author\n");
1065 static const char implicit_ident_advice_config
[] =
1066 N_("Your name and email address were configured automatically based\n"
1067 "on your username and hostname. Please check that they are accurate.\n"
1068 "You can suppress this message by setting them explicitly:\n"
1070 " git config --global user.name \"Your Name\"\n"
1071 " git config --global user.email you@example.com\n"
1073 "After doing this, you may fix the identity used for this commit with:\n"
1075 " git commit --amend --reset-author\n");
1077 static const char *implicit_ident_advice(void)
1079 char *user_config
= expand_user_path("~/.gitconfig", 0);
1080 char *xdg_config
= xdg_config_home("config");
1081 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
1087 return _(implicit_ident_advice_config
);
1089 return _(implicit_ident_advice_noconfig
);
1093 void print_commit_summary(const char *prefix
, const struct object_id
*oid
,
1096 struct rev_info rev
;
1097 struct commit
*commit
;
1098 struct strbuf format
= STRBUF_INIT
;
1100 struct pretty_print_context pctx
= {0};
1101 struct strbuf author_ident
= STRBUF_INIT
;
1102 struct strbuf committer_ident
= STRBUF_INIT
;
1104 commit
= lookup_commit(oid
);
1106 die(_("couldn't look up newly created commit"));
1107 if (parse_commit(commit
))
1108 die(_("could not parse newly created commit"));
1110 strbuf_addstr(&format
, "format:%h] %s");
1112 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1113 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1114 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1115 strbuf_addstr(&format
, "\n Author: ");
1116 strbuf_addbuf_percentquote(&format
, &author_ident
);
1118 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
1119 struct strbuf date
= STRBUF_INIT
;
1121 format_commit_message(commit
, "%ad", &date
, &pctx
);
1122 strbuf_addstr(&format
, "\n Date: ");
1123 strbuf_addbuf_percentquote(&format
, &date
);
1124 strbuf_release(&date
);
1126 if (!committer_ident_sufficiently_given()) {
1127 strbuf_addstr(&format
, "\n Committer: ");
1128 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1129 if (advice_implicit_identity
) {
1130 strbuf_addch(&format
, '\n');
1131 strbuf_addstr(&format
, implicit_ident_advice());
1134 strbuf_release(&author_ident
);
1135 strbuf_release(&committer_ident
);
1137 init_revisions(&rev
, prefix
);
1138 setup_revisions(0, NULL
, &rev
, NULL
);
1141 rev
.diffopt
.output_format
=
1142 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1144 rev
.verbose_header
= 1;
1145 rev
.show_root_diff
= 1;
1146 get_commit_format(format
.buf
, &rev
);
1147 rev
.always_show_header
= 0;
1148 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1149 rev
.diffopt
.break_opt
= 0;
1150 diff_setup_done(&rev
.diffopt
);
1152 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1154 die_errno(_("unable to resolve HEAD after creating commit"));
1155 if (!strcmp(head
, "HEAD"))
1156 head
= _("detached HEAD");
1158 skip_prefix(head
, "refs/heads/", &head
);
1159 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1160 _(" (root-commit)") : "");
1162 if (!log_tree_commit(&rev
, commit
)) {
1163 rev
.always_show_header
= 1;
1164 rev
.use_terminator
= 1;
1165 log_tree_commit(&rev
, commit
);
1168 strbuf_release(&format
);
1171 static int parse_head(struct commit
**head
)
1173 struct commit
*current_head
;
1174 struct object_id oid
;
1176 if (get_oid("HEAD", &oid
)) {
1177 current_head
= NULL
;
1179 current_head
= lookup_commit_reference(&oid
);
1181 return error(_("could not parse HEAD"));
1182 if (oidcmp(&oid
, ¤t_head
->object
.oid
)) {
1183 warning(_("HEAD %s is not a commit!"),
1186 if (parse_commit(current_head
))
1187 return error(_("could not parse HEAD commit"));
1189 *head
= current_head
;
1195 * Try to commit without forking 'git commit'. In some cases we need
1196 * to run 'git commit' to display an error message
1199 * -1 - error unable to commit
1201 * 1 - run 'git commit'
1203 static int try_to_commit(struct strbuf
*msg
, const char *author
,
1204 struct replay_opts
*opts
, unsigned int flags
,
1205 struct object_id
*oid
)
1207 struct object_id tree
;
1208 struct commit
*current_head
;
1209 struct commit_list
*parents
= NULL
;
1210 struct commit_extra_header
*extra
= NULL
;
1211 struct strbuf err
= STRBUF_INIT
;
1212 struct strbuf commit_msg
= STRBUF_INIT
;
1213 char *amend_author
= NULL
;
1214 const char *hook_commit
= NULL
;
1215 enum commit_msg_cleanup_mode cleanup
;
1218 if (parse_head(¤t_head
))
1221 if (flags
& AMEND_MSG
) {
1222 const char *exclude_gpgsig
[] = { "gpgsig", NULL
};
1223 const char *out_enc
= get_commit_output_encoding();
1224 const char *message
= logmsg_reencode(current_head
, NULL
,
1228 const char *orig_message
= NULL
;
1230 find_commit_subject(message
, &orig_message
);
1232 strbuf_addstr(msg
, orig_message
);
1233 hook_commit
= "HEAD";
1235 author
= amend_author
= get_author(message
);
1236 unuse_commit_buffer(current_head
, message
);
1238 res
= error(_("unable to parse commit author"));
1241 parents
= copy_commit_list(current_head
->parents
);
1242 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1243 } else if (current_head
) {
1244 commit_list_insert(current_head
, &parents
);
1247 if (write_cache_as_tree(&tree
, 0, NULL
)) {
1248 res
= error(_("git write-tree failed to write a tree"));
1252 if (!(flags
& ALLOW_EMPTY
) && !oidcmp(current_head
?
1253 get_commit_tree_oid(current_head
) :
1254 the_hash_algo
->empty_tree
, &tree
)) {
1255 res
= 1; /* run 'git commit' to display error message */
1259 if (find_hook("prepare-commit-msg")) {
1260 res
= run_prepare_commit_msg_hook(msg
, hook_commit
);
1263 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1265 res
= error_errno(_("unable to read commit message "
1267 git_path_commit_editmsg());
1273 cleanup
= (flags
& CLEANUP_MSG
) ? COMMIT_MSG_CLEANUP_ALL
:
1274 opts
->default_msg_cleanup
;
1276 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1277 strbuf_stripspace(msg
, cleanup
== COMMIT_MSG_CLEANUP_ALL
);
1278 if (!opts
->allow_empty_message
&& message_is_empty(msg
, cleanup
)) {
1279 res
= 1; /* run 'git commit' to display error message */
1285 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
,
1286 oid
, author
, opts
->gpg_sign
, extra
)) {
1287 res
= error(_("failed to write commit object"));
1291 if (update_head_with_reflog(current_head
, oid
,
1292 getenv("GIT_REFLOG_ACTION"), msg
, &err
)) {
1293 res
= error("%s", err
.buf
);
1297 if (flags
& AMEND_MSG
)
1298 commit_post_rewrite(current_head
, oid
);
1301 free_commit_extra_headers(extra
);
1302 strbuf_release(&err
);
1303 strbuf_release(&commit_msg
);
1309 static int do_commit(const char *msg_file
, const char *author
,
1310 struct replay_opts
*opts
, unsigned int flags
)
1314 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
) &&
1315 !(flags
& CREATE_ROOT_COMMIT
)) {
1316 struct object_id oid
;
1317 struct strbuf sb
= STRBUF_INIT
;
1319 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1320 return error_errno(_("unable to read commit message "
1324 res
= try_to_commit(msg_file
? &sb
: NULL
, author
, opts
, flags
,
1326 strbuf_release(&sb
);
1328 unlink(git_path_cherry_pick_head(the_repository
));
1329 unlink(git_path_merge_msg(the_repository
));
1330 if (!is_rebase_i(opts
))
1331 print_commit_summary(NULL
, &oid
,
1332 SUMMARY_SHOW_AUTHOR_DATE
);
1337 return run_git_commit(msg_file
, opts
, flags
);
1342 static int is_original_commit_empty(struct commit
*commit
)
1344 const struct object_id
*ptree_oid
;
1346 if (parse_commit(commit
))
1347 return error(_("could not parse commit %s"),
1348 oid_to_hex(&commit
->object
.oid
));
1349 if (commit
->parents
) {
1350 struct commit
*parent
= commit
->parents
->item
;
1351 if (parse_commit(parent
))
1352 return error(_("could not parse parent commit %s"),
1353 oid_to_hex(&parent
->object
.oid
));
1354 ptree_oid
= get_commit_tree_oid(parent
);
1356 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1359 return !oidcmp(ptree_oid
, get_commit_tree_oid(commit
));
1363 * Do we run "git commit" with "--allow-empty"?
1365 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
1367 int index_unchanged
, empty_commit
;
1372 * (1) we do not allow empty at all and error out.
1374 * (2) we allow ones that were initially empty, but
1375 * forbid the ones that become empty;
1377 * (3) we allow both.
1379 if (!opts
->allow_empty
)
1380 return 0; /* let "git commit" barf as necessary */
1382 index_unchanged
= is_index_unchanged();
1383 if (index_unchanged
< 0)
1384 return index_unchanged
;
1385 if (!index_unchanged
)
1386 return 0; /* we do not have to say --allow-empty */
1388 if (opts
->keep_redundant_commits
)
1391 empty_commit
= is_original_commit_empty(commit
);
1392 if (empty_commit
< 0)
1393 return empty_commit
;
1401 * Note that ordering matters in this enum. Not only must it match the mapping
1402 * below, it is also divided into several sections that matter. When adding
1403 * new commands, make sure you add it in the right section.
1406 /* commands that handle commits */
1413 /* commands that do something else than handling a single commit */
1418 /* commands that do nothing but are counted for reporting progress */
1421 /* comments (not counted for reporting progress) */
1428 } todo_command_info
[] = {
1444 static const char *command_to_string(const enum todo_command command
)
1446 if (command
< TODO_COMMENT
)
1447 return todo_command_info
[command
].str
;
1448 die("Unknown command: %d", command
);
1451 static char command_to_char(const enum todo_command command
)
1453 if (command
< TODO_COMMENT
&& todo_command_info
[command
].c
)
1454 return todo_command_info
[command
].c
;
1455 return comment_line_char
;
1458 static int is_noop(const enum todo_command command
)
1460 return TODO_NOOP
<= command
;
1463 static int is_fixup(enum todo_command command
)
1465 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1468 /* Does this command create a (non-merge) commit? */
1469 static int is_pick_or_similar(enum todo_command command
)
1484 static int update_squash_messages(enum todo_command command
,
1485 struct commit
*commit
, struct replay_opts
*opts
)
1487 struct strbuf buf
= STRBUF_INIT
;
1489 const char *message
, *body
;
1491 if (opts
->current_fixup_count
> 0) {
1492 struct strbuf header
= STRBUF_INIT
;
1495 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 9) <= 0)
1496 return error(_("could not read '%s'"),
1497 rebase_path_squash_msg());
1499 eol
= buf
.buf
[0] != comment_line_char
?
1500 buf
.buf
: strchrnul(buf
.buf
, '\n');
1502 strbuf_addf(&header
, "%c ", comment_line_char
);
1503 strbuf_addf(&header
, _("This is a combination of %d commits."),
1504 opts
->current_fixup_count
+ 2);
1505 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1506 strbuf_release(&header
);
1508 struct object_id head
;
1509 struct commit
*head_commit
;
1510 const char *head_message
, *body
;
1512 if (get_oid("HEAD", &head
))
1513 return error(_("need a HEAD to fixup"));
1514 if (!(head_commit
= lookup_commit_reference(&head
)))
1515 return error(_("could not read HEAD"));
1516 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
1517 return error(_("could not read HEAD's commit message"));
1519 find_commit_subject(head_message
, &body
);
1520 if (write_message(body
, strlen(body
),
1521 rebase_path_fixup_msg(), 0)) {
1522 unuse_commit_buffer(head_commit
, head_message
);
1523 return error(_("cannot write '%s'"),
1524 rebase_path_fixup_msg());
1527 strbuf_addf(&buf
, "%c ", comment_line_char
);
1528 strbuf_addf(&buf
, _("This is a combination of %d commits."), 2);
1529 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1530 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
1531 strbuf_addstr(&buf
, "\n\n");
1532 strbuf_addstr(&buf
, body
);
1534 unuse_commit_buffer(head_commit
, head_message
);
1537 if (!(message
= get_commit_buffer(commit
, NULL
)))
1538 return error(_("could not read commit message of %s"),
1539 oid_to_hex(&commit
->object
.oid
));
1540 find_commit_subject(message
, &body
);
1542 if (command
== TODO_SQUASH
) {
1543 unlink(rebase_path_fixup_msg());
1544 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1545 strbuf_addf(&buf
, _("This is the commit message #%d:"),
1546 ++opts
->current_fixup_count
);
1547 strbuf_addstr(&buf
, "\n\n");
1548 strbuf_addstr(&buf
, body
);
1549 } else if (command
== TODO_FIXUP
) {
1550 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1551 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
1552 ++opts
->current_fixup_count
);
1553 strbuf_addstr(&buf
, "\n\n");
1554 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1556 return error(_("unknown command: %d"), command
);
1557 unuse_commit_buffer(commit
, message
);
1559 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
1560 strbuf_release(&buf
);
1563 strbuf_addf(&opts
->current_fixups
, "%s%s %s",
1564 opts
->current_fixups
.len
? "\n" : "",
1565 command_to_string(command
),
1566 oid_to_hex(&commit
->object
.oid
));
1567 res
= write_message(opts
->current_fixups
.buf
,
1568 opts
->current_fixups
.len
,
1569 rebase_path_current_fixups(), 0);
1575 static void flush_rewritten_pending(void) {
1576 struct strbuf buf
= STRBUF_INIT
;
1577 struct object_id newoid
;
1580 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
1581 !get_oid("HEAD", &newoid
) &&
1582 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
1583 char *bol
= buf
.buf
, *eol
;
1586 eol
= strchrnul(bol
, '\n');
1587 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
1588 bol
, oid_to_hex(&newoid
));
1594 unlink(rebase_path_rewritten_pending());
1596 strbuf_release(&buf
);
1599 static void record_in_rewritten(struct object_id
*oid
,
1600 enum todo_command next_command
) {
1601 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
1606 fprintf(out
, "%s\n", oid_to_hex(oid
));
1609 if (!is_fixup(next_command
))
1610 flush_rewritten_pending();
1613 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
1614 struct replay_opts
*opts
, int final_fixup
)
1616 unsigned int flags
= opts
->edit
? EDIT_MSG
: 0;
1617 const char *msg_file
= opts
->edit
? NULL
: git_path_merge_msg(the_repository
);
1618 struct object_id head
;
1619 struct commit
*base
, *next
, *parent
;
1620 const char *base_label
, *next_label
;
1621 char *author
= NULL
;
1622 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
1623 struct strbuf msgbuf
= STRBUF_INIT
;
1624 int res
, unborn
= 0, allow
;
1626 if (opts
->no_commit
) {
1628 * We do not intend to commit immediately. We just want to
1629 * merge the differences in, so let's compute the tree
1630 * that represents the "current" state for merge-recursive
1633 if (write_cache_as_tree(&head
, 0, NULL
))
1634 return error(_("your index file is unmerged."));
1636 unborn
= get_oid("HEAD", &head
);
1637 /* Do we want to generate a root commit? */
1638 if (is_pick_or_similar(command
) && opts
->have_squash_onto
&&
1639 !oidcmp(&head
, &opts
->squash_onto
)) {
1640 if (is_fixup(command
))
1641 return error(_("cannot fixup root commit"));
1642 flags
|= CREATE_ROOT_COMMIT
;
1645 oidcpy(&head
, the_hash_algo
->empty_tree
);
1646 if (index_differs_from(unborn
? empty_tree_oid_hex() : "HEAD",
1648 return error_dirty_index(opts
);
1652 if (!commit
->parents
)
1654 else if (commit
->parents
->next
) {
1655 /* Reverting or cherry-picking a merge commit */
1657 struct commit_list
*p
;
1659 if (!opts
->mainline
)
1660 return error(_("commit %s is a merge but no -m option was given."),
1661 oid_to_hex(&commit
->object
.oid
));
1663 for (cnt
= 1, p
= commit
->parents
;
1664 cnt
!= opts
->mainline
&& p
;
1667 if (cnt
!= opts
->mainline
|| !p
)
1668 return error(_("commit %s does not have parent %d"),
1669 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1671 } else if (0 < opts
->mainline
)
1672 return error(_("mainline was specified but commit %s is not a merge."),
1673 oid_to_hex(&commit
->object
.oid
));
1675 parent
= commit
->parents
->item
;
1677 if (get_message(commit
, &msg
) != 0)
1678 return error(_("cannot get commit message for %s"),
1679 oid_to_hex(&commit
->object
.oid
));
1681 if (opts
->allow_ff
&& !is_fixup(command
) &&
1682 ((parent
&& !oidcmp(&parent
->object
.oid
, &head
)) ||
1683 (!parent
&& unborn
))) {
1684 if (is_rebase_i(opts
))
1685 write_author_script(msg
.message
);
1686 res
= fast_forward_to(&commit
->object
.oid
, &head
, unborn
,
1688 if (res
|| command
!= TODO_REWORD
)
1690 flags
|= EDIT_MSG
| AMEND_MSG
| VERIFY_MSG
;
1692 goto fast_forward_edit
;
1694 if (parent
&& parse_commit(parent
) < 0)
1695 /* TRANSLATORS: The first %s will be a "todo" command like
1696 "revert" or "pick", the second %s a SHA1. */
1697 return error(_("%s: cannot parse parent commit %s"),
1698 command_to_string(command
),
1699 oid_to_hex(&parent
->object
.oid
));
1702 * "commit" is an existing commit. We would want to apply
1703 * the difference it introduces since its first parent "prev"
1704 * on top of the current HEAD if we are cherry-pick. Or the
1705 * reverse of it if we are revert.
1708 if (command
== TODO_REVERT
) {
1710 base_label
= msg
.label
;
1712 next_label
= msg
.parent_label
;
1713 strbuf_addstr(&msgbuf
, "Revert \"");
1714 strbuf_addstr(&msgbuf
, msg
.subject
);
1715 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
1716 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1718 if (commit
->parents
&& commit
->parents
->next
) {
1719 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1720 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1722 strbuf_addstr(&msgbuf
, ".\n");
1727 base_label
= msg
.parent_label
;
1729 next_label
= msg
.label
;
1731 /* Append the commit log message to msgbuf. */
1732 if (find_commit_subject(msg
.message
, &p
))
1733 strbuf_addstr(&msgbuf
, p
);
1735 if (opts
->record_origin
) {
1736 strbuf_complete_line(&msgbuf
);
1737 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1738 strbuf_addch(&msgbuf
, '\n');
1739 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1740 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1741 strbuf_addstr(&msgbuf
, ")\n");
1743 if (!is_fixup(command
))
1744 author
= get_author(msg
.message
);
1747 if (command
== TODO_REWORD
)
1748 flags
|= EDIT_MSG
| VERIFY_MSG
;
1749 else if (is_fixup(command
)) {
1750 if (update_squash_messages(command
, commit
, opts
))
1754 msg_file
= rebase_path_squash_msg();
1755 else if (file_exists(rebase_path_fixup_msg())) {
1756 flags
|= CLEANUP_MSG
;
1757 msg_file
= rebase_path_fixup_msg();
1759 const char *dest
= git_path_squash_msg(the_repository
);
1761 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1762 return error(_("could not rename '%s' to '%s'"),
1763 rebase_path_squash_msg(), dest
);
1764 unlink(git_path_merge_msg(the_repository
));
1770 if (opts
->signoff
&& !is_fixup(command
))
1771 append_signoff(&msgbuf
, 0, 0);
1773 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1775 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1776 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
1777 &head
, &msgbuf
, opts
);
1781 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
1782 git_path_merge_msg(the_repository
), 0);
1784 struct commit_list
*common
= NULL
;
1785 struct commit_list
*remotes
= NULL
;
1787 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1788 git_path_merge_msg(the_repository
), 0);
1790 commit_list_insert(base
, &common
);
1791 commit_list_insert(next
, &remotes
);
1792 res
|= try_merge_command(opts
->strategy
,
1793 opts
->xopts_nr
, (const char **)opts
->xopts
,
1794 common
, oid_to_hex(&head
), remotes
);
1795 free_commit_list(common
);
1796 free_commit_list(remotes
);
1798 strbuf_release(&msgbuf
);
1801 * If the merge was clean or if it failed due to conflict, we write
1802 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1803 * However, if the merge did not even start, then we don't want to
1806 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1807 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
1808 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1810 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1811 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
1812 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1816 error(command
== TODO_REVERT
1817 ? _("could not revert %s... %s")
1818 : _("could not apply %s... %s"),
1819 short_commit_name(commit
), msg
.subject
);
1820 print_advice(res
== 1, opts
);
1821 rerere(opts
->allow_rerere_auto
);
1825 allow
= allow_empty(opts
, commit
);
1830 flags
|= ALLOW_EMPTY
;
1831 if (!opts
->no_commit
) {
1833 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
1834 res
= do_commit(msg_file
, author
, opts
, flags
);
1836 res
= error(_("unable to parse commit author"));
1839 if (!res
&& final_fixup
) {
1840 unlink(rebase_path_fixup_msg());
1841 unlink(rebase_path_squash_msg());
1842 unlink(rebase_path_current_fixups());
1843 strbuf_reset(&opts
->current_fixups
);
1844 opts
->current_fixup_count
= 0;
1848 free_message(commit
, &msg
);
1850 update_abort_safety_file();
1855 static int prepare_revs(struct replay_opts
*opts
)
1858 * picking (but not reverting) ranges (but not individual revisions)
1859 * should be done in reverse
1861 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1862 opts
->revs
->reverse
^= 1;
1864 if (prepare_revision_walk(opts
->revs
))
1865 return error(_("revision walk setup failed"));
1867 if (!opts
->revs
->commits
)
1868 return error(_("empty commit set passed"));
1872 static int read_and_refresh_cache(struct replay_opts
*opts
)
1874 struct lock_file index_lock
= LOCK_INIT
;
1875 int index_fd
= hold_locked_index(&index_lock
, 0);
1876 if (read_index_preload(&the_index
, NULL
) < 0) {
1877 rollback_lock_file(&index_lock
);
1878 return error(_("git %s: failed to read the index"),
1879 _(action_name(opts
)));
1881 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1882 if (index_fd
>= 0) {
1883 if (write_locked_index(&the_index
, &index_lock
,
1884 COMMIT_LOCK
| SKIP_IF_UNCHANGED
)) {
1885 return error(_("git %s: failed to refresh the index"),
1886 _(action_name(opts
)));
1892 enum todo_item_flags
{
1893 TODO_EDIT_MERGE_MSG
= 1
1897 enum todo_command command
;
1898 struct commit
*commit
;
1902 size_t offset_in_buf
;
1907 struct todo_item
*items
;
1908 int nr
, alloc
, current
;
1909 int done_nr
, total_nr
;
1910 struct stat_data stat
;
1913 #define TODO_LIST_INIT { STRBUF_INIT }
1915 static void todo_list_release(struct todo_list
*todo_list
)
1917 strbuf_release(&todo_list
->buf
);
1918 FREE_AND_NULL(todo_list
->items
);
1919 todo_list
->nr
= todo_list
->alloc
= 0;
1922 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1924 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1925 return todo_list
->items
+ todo_list
->nr
++;
1928 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1930 struct object_id commit_oid
;
1931 char *end_of_object_name
;
1932 int i
, saved
, status
, padding
;
1937 bol
+= strspn(bol
, " \t");
1939 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1940 item
->command
= TODO_COMMENT
;
1941 item
->commit
= NULL
;
1943 item
->arg_len
= eol
- bol
;
1947 for (i
= 0; i
< TODO_COMMENT
; i
++)
1948 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1951 } else if (bol
[1] == ' ' && *bol
== todo_command_info
[i
].c
) {
1956 if (i
>= TODO_COMMENT
)
1959 /* Eat up extra spaces/ tabs before object name */
1960 padding
= strspn(bol
, " \t");
1963 if (item
->command
== TODO_NOOP
) {
1965 return error(_("%s does not accept arguments: '%s'"),
1966 command_to_string(item
->command
), bol
);
1967 item
->commit
= NULL
;
1969 item
->arg_len
= eol
- bol
;
1974 return error(_("missing arguments for %s"),
1975 command_to_string(item
->command
));
1977 if (item
->command
== TODO_EXEC
|| item
->command
== TODO_LABEL
||
1978 item
->command
== TODO_RESET
) {
1979 item
->commit
= NULL
;
1981 item
->arg_len
= (int)(eol
- bol
);
1985 if (item
->command
== TODO_MERGE
) {
1986 if (skip_prefix(bol
, "-C", &bol
))
1987 bol
+= strspn(bol
, " \t");
1988 else if (skip_prefix(bol
, "-c", &bol
)) {
1989 bol
+= strspn(bol
, " \t");
1990 item
->flags
|= TODO_EDIT_MERGE_MSG
;
1992 item
->flags
|= TODO_EDIT_MERGE_MSG
;
1993 item
->commit
= NULL
;
1995 item
->arg_len
= (int)(eol
- bol
);
2000 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
2001 saved
= *end_of_object_name
;
2002 *end_of_object_name
= '\0';
2003 status
= get_oid(bol
, &commit_oid
);
2004 *end_of_object_name
= saved
;
2006 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
2007 item
->arg_len
= (int)(eol
- item
->arg
);
2012 item
->commit
= lookup_commit_reference(&commit_oid
);
2013 return !item
->commit
;
2016 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
2018 struct todo_item
*item
;
2019 char *p
= buf
, *next_p
;
2020 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
2022 for (i
= 1; *p
; i
++, p
= next_p
) {
2023 char *eol
= strchrnul(p
, '\n');
2025 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
2027 if (p
!= eol
&& eol
[-1] == '\r')
2028 eol
--; /* strip Carriage Return */
2030 item
= append_new_todo(todo_list
);
2031 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
2032 if (parse_insn_line(item
, p
, eol
)) {
2033 res
= error(_("invalid line %d: %.*s"),
2034 i
, (int)(eol
- p
), p
);
2035 item
->command
= TODO_NOOP
;
2040 else if (is_fixup(item
->command
))
2041 return error(_("cannot '%s' without a previous commit"),
2042 command_to_string(item
->command
));
2043 else if (!is_noop(item
->command
))
2050 static int count_commands(struct todo_list
*todo_list
)
2054 for (i
= 0; i
< todo_list
->nr
; i
++)
2055 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
2061 static int get_item_line_offset(struct todo_list
*todo_list
, int index
)
2063 return index
< todo_list
->nr
?
2064 todo_list
->items
[index
].offset_in_buf
: todo_list
->buf
.len
;
2067 static const char *get_item_line(struct todo_list
*todo_list
, int index
)
2069 return todo_list
->buf
.buf
+ get_item_line_offset(todo_list
, index
);
2072 static int get_item_line_length(struct todo_list
*todo_list
, int index
)
2074 return get_item_line_offset(todo_list
, index
+ 1)
2075 - get_item_line_offset(todo_list
, index
);
2078 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
2083 fd
= open(path
, O_RDONLY
);
2085 return error_errno(_("could not open '%s'"), path
);
2086 len
= strbuf_read(sb
, fd
, 0);
2089 return error(_("could not read '%s'."), path
);
2093 static int read_populate_todo(struct todo_list
*todo_list
,
2094 struct replay_opts
*opts
)
2097 const char *todo_file
= get_todo_path(opts
);
2100 strbuf_reset(&todo_list
->buf
);
2101 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
2104 res
= stat(todo_file
, &st
);
2106 return error(_("could not stat '%s'"), todo_file
);
2107 fill_stat_data(&todo_list
->stat
, &st
);
2109 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
2111 if (is_rebase_i(opts
))
2112 return error(_("please fix this using "
2113 "'git rebase --edit-todo'."));
2114 return error(_("unusable instruction sheet: '%s'"), todo_file
);
2117 if (!todo_list
->nr
&&
2118 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
2119 return error(_("no commits parsed."));
2121 if (!is_rebase_i(opts
)) {
2122 enum todo_command valid
=
2123 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
2126 for (i
= 0; i
< todo_list
->nr
; i
++)
2127 if (valid
== todo_list
->items
[i
].command
)
2129 else if (valid
== TODO_PICK
)
2130 return error(_("cannot cherry-pick during a revert."));
2132 return error(_("cannot revert during a cherry-pick."));
2135 if (is_rebase_i(opts
)) {
2136 struct todo_list done
= TODO_LIST_INIT
;
2137 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
2139 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
2140 !parse_insn_buffer(done
.buf
.buf
, &done
))
2141 todo_list
->done_nr
= count_commands(&done
);
2143 todo_list
->done_nr
= 0;
2145 todo_list
->total_nr
= todo_list
->done_nr
2146 + count_commands(todo_list
);
2147 todo_list_release(&done
);
2150 fprintf(f
, "%d\n", todo_list
->total_nr
);
2158 static int git_config_string_dup(char **dest
,
2159 const char *var
, const char *value
)
2162 return config_error_nonbool(var
);
2164 *dest
= xstrdup(value
);
2168 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
2170 struct replay_opts
*opts
= data
;
2175 else if (!strcmp(key
, "options.no-commit"))
2176 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
2177 else if (!strcmp(key
, "options.edit"))
2178 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
2179 else if (!strcmp(key
, "options.signoff"))
2180 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
2181 else if (!strcmp(key
, "options.record-origin"))
2182 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
2183 else if (!strcmp(key
, "options.allow-ff"))
2184 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
2185 else if (!strcmp(key
, "options.mainline"))
2186 opts
->mainline
= git_config_int(key
, value
);
2187 else if (!strcmp(key
, "options.strategy"))
2188 git_config_string_dup(&opts
->strategy
, key
, value
);
2189 else if (!strcmp(key
, "options.gpg-sign"))
2190 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
2191 else if (!strcmp(key
, "options.strategy-option")) {
2192 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
2193 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
2194 } else if (!strcmp(key
, "options.allow-rerere-auto"))
2195 opts
->allow_rerere_auto
=
2196 git_config_bool_or_int(key
, value
, &error_flag
) ?
2197 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
2199 return error(_("invalid key: %s"), key
);
2202 return error(_("invalid value for %s: %s"), key
, value
);
2207 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
2210 char *strategy_opts_string
;
2213 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
2215 opts
->strategy
= strbuf_detach(buf
, NULL
);
2216 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
2219 strategy_opts_string
= buf
->buf
;
2220 if (*strategy_opts_string
== ' ')
2221 strategy_opts_string
++;
2222 opts
->xopts_nr
= split_cmdline(strategy_opts_string
,
2223 (const char ***)&opts
->xopts
);
2224 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
2225 const char *arg
= opts
->xopts
[i
];
2227 skip_prefix(arg
, "--", &arg
);
2228 opts
->xopts
[i
] = xstrdup(arg
);
2232 static int read_populate_opts(struct replay_opts
*opts
)
2234 if (is_rebase_i(opts
)) {
2235 struct strbuf buf
= STRBUF_INIT
;
2237 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
2238 if (!starts_with(buf
.buf
, "-S"))
2241 free(opts
->gpg_sign
);
2242 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
2247 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(), 1)) {
2248 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
2249 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
2250 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
2251 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
2255 if (file_exists(rebase_path_verbose()))
2258 if (file_exists(rebase_path_signoff())) {
2263 read_strategy_opts(opts
, &buf
);
2264 strbuf_release(&buf
);
2266 if (read_oneliner(&opts
->current_fixups
,
2267 rebase_path_current_fixups(), 1)) {
2268 const char *p
= opts
->current_fixups
.buf
;
2269 opts
->current_fixup_count
= 1;
2270 while ((p
= strchr(p
, '\n'))) {
2271 opts
->current_fixup_count
++;
2276 if (read_oneliner(&buf
, rebase_path_squash_onto(), 0)) {
2277 if (get_oid_hex(buf
.buf
, &opts
->squash_onto
) < 0)
2278 return error(_("unusable squash-onto"));
2279 opts
->have_squash_onto
= 1;
2285 if (!file_exists(git_path_opts_file()))
2288 * The function git_parse_source(), called from git_config_from_file(),
2289 * may die() in case of a syntactically incorrect file. We do not care
2290 * about this case, though, because we wrote that file ourselves, so we
2291 * are pretty certain that it is syntactically correct.
2293 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
2294 return error(_("malformed options sheet: '%s'"),
2295 git_path_opts_file());
2299 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
2300 struct replay_opts
*opts
)
2302 enum todo_command command
= opts
->action
== REPLAY_PICK
?
2303 TODO_PICK
: TODO_REVERT
;
2304 const char *command_string
= todo_command_info
[command
].str
;
2305 struct commit
*commit
;
2307 if (prepare_revs(opts
))
2310 while ((commit
= get_revision(opts
->revs
))) {
2311 struct todo_item
*item
= append_new_todo(todo_list
);
2312 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2313 const char *subject
;
2316 item
->command
= command
;
2317 item
->commit
= commit
;
2320 item
->offset_in_buf
= todo_list
->buf
.len
;
2321 subject_len
= find_commit_subject(commit_buffer
, &subject
);
2322 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
2323 short_commit_name(commit
), subject_len
, subject
);
2324 unuse_commit_buffer(commit
, commit_buffer
);
2329 static int create_seq_dir(void)
2331 if (file_exists(git_path_seq_dir())) {
2332 error(_("a cherry-pick or revert is already in progress"));
2333 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
2335 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
2336 return error_errno(_("could not create sequencer directory '%s'"),
2337 git_path_seq_dir());
2341 static int save_head(const char *head
)
2343 struct lock_file head_lock
= LOCK_INIT
;
2344 struct strbuf buf
= STRBUF_INIT
;
2348 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
2350 return error_errno(_("could not lock HEAD"));
2351 strbuf_addf(&buf
, "%s\n", head
);
2352 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
2353 strbuf_release(&buf
);
2355 error_errno(_("could not write to '%s'"), git_path_head_file());
2356 rollback_lock_file(&head_lock
);
2359 if (commit_lock_file(&head_lock
) < 0)
2360 return error(_("failed to finalize '%s'"), git_path_head_file());
2364 static int rollback_is_safe(void)
2366 struct strbuf sb
= STRBUF_INIT
;
2367 struct object_id expected_head
, actual_head
;
2369 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
2371 if (get_oid_hex(sb
.buf
, &expected_head
)) {
2372 strbuf_release(&sb
);
2373 die(_("could not parse %s"), git_path_abort_safety_file());
2375 strbuf_release(&sb
);
2377 else if (errno
== ENOENT
)
2378 oidclr(&expected_head
);
2380 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
2382 if (get_oid("HEAD", &actual_head
))
2383 oidclr(&actual_head
);
2385 return !oidcmp(&actual_head
, &expected_head
);
2388 static int reset_for_rollback(const struct object_id
*oid
)
2390 const char *argv
[4]; /* reset --merge <arg> + NULL */
2393 argv
[1] = "--merge";
2394 argv
[2] = oid_to_hex(oid
);
2396 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2399 static int rollback_single_pick(void)
2401 struct object_id head_oid
;
2403 if (!file_exists(git_path_cherry_pick_head(the_repository
)) &&
2404 !file_exists(git_path_revert_head(the_repository
)))
2405 return error(_("no cherry-pick or revert in progress"));
2406 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
2407 return error(_("cannot resolve HEAD"));
2408 if (is_null_oid(&head_oid
))
2409 return error(_("cannot abort from a branch yet to be born"));
2410 return reset_for_rollback(&head_oid
);
2413 int sequencer_rollback(struct replay_opts
*opts
)
2416 struct object_id oid
;
2417 struct strbuf buf
= STRBUF_INIT
;
2420 f
= fopen(git_path_head_file(), "r");
2421 if (!f
&& errno
== ENOENT
) {
2423 * There is no multiple-cherry-pick in progress.
2424 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
2425 * a single-cherry-pick in progress, abort that.
2427 return rollback_single_pick();
2430 return error_errno(_("cannot open '%s'"), git_path_head_file());
2431 if (strbuf_getline_lf(&buf
, f
)) {
2432 error(_("cannot read '%s': %s"), git_path_head_file(),
2433 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
2438 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
2439 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
2440 git_path_head_file());
2443 if (is_null_oid(&oid
)) {
2444 error(_("cannot abort from a branch yet to be born"));
2448 if (!rollback_is_safe()) {
2449 /* Do not error, just do not rollback */
2450 warning(_("You seem to have moved HEAD. "
2451 "Not rewinding, check your HEAD!"));
2453 if (reset_for_rollback(&oid
))
2455 strbuf_release(&buf
);
2456 return sequencer_remove_state(opts
);
2458 strbuf_release(&buf
);
2462 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2464 struct lock_file todo_lock
= LOCK_INIT
;
2465 const char *todo_path
= get_todo_path(opts
);
2466 int next
= todo_list
->current
, offset
, fd
;
2469 * rebase -i writes "git-rebase-todo" without the currently executing
2470 * command, appending it to "done" instead.
2472 if (is_rebase_i(opts
))
2475 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
2477 return error_errno(_("could not lock '%s'"), todo_path
);
2478 offset
= get_item_line_offset(todo_list
, next
);
2479 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
2480 todo_list
->buf
.len
- offset
) < 0)
2481 return error_errno(_("could not write to '%s'"), todo_path
);
2482 if (commit_lock_file(&todo_lock
) < 0)
2483 return error(_("failed to finalize '%s'"), todo_path
);
2485 if (is_rebase_i(opts
) && next
> 0) {
2486 const char *done
= rebase_path_done();
2487 int fd
= open(done
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
2492 if (write_in_full(fd
, get_item_line(todo_list
, next
- 1),
2493 get_item_line_length(todo_list
, next
- 1))
2495 ret
= error_errno(_("could not write to '%s'"), done
);
2497 ret
= error_errno(_("failed to finalize '%s'"), done
);
2503 static int save_opts(struct replay_opts
*opts
)
2505 const char *opts_file
= git_path_opts_file();
2508 if (opts
->no_commit
)
2509 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
2511 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
2513 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
2514 if (opts
->record_origin
)
2515 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
2517 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
2518 if (opts
->mainline
) {
2519 struct strbuf buf
= STRBUF_INIT
;
2520 strbuf_addf(&buf
, "%d", opts
->mainline
);
2521 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
2522 strbuf_release(&buf
);
2525 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
2527 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
2530 for (i
= 0; i
< opts
->xopts_nr
; i
++)
2531 res
|= git_config_set_multivar_in_file_gently(opts_file
,
2532 "options.strategy-option",
2533 opts
->xopts
[i
], "^$", 0);
2535 if (opts
->allow_rerere_auto
)
2536 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-rerere-auto",
2537 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
2542 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
2544 struct strbuf buf
= STRBUF_INIT
;
2545 struct rev_info log_tree_opt
;
2546 const char *subject
, *p
;
2549 p
= short_commit_name(commit
);
2550 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
2552 if (update_ref("rebase", "REBASE_HEAD", &commit
->object
.oid
,
2553 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2554 res
|= error(_("could not update %s"), "REBASE_HEAD");
2556 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
2557 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2558 init_revisions(&log_tree_opt
, NULL
);
2559 log_tree_opt
.abbrev
= 0;
2560 log_tree_opt
.diff
= 1;
2561 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
2562 log_tree_opt
.disable_stdin
= 1;
2563 log_tree_opt
.no_commit_id
= 1;
2564 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
2565 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2566 if (!log_tree_opt
.diffopt
.file
)
2567 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
2569 res
|= log_tree_commit(&log_tree_opt
, commit
);
2570 fclose(log_tree_opt
.diffopt
.file
);
2574 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
2575 if (!file_exists(buf
.buf
)) {
2576 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2577 find_commit_subject(commit_buffer
, &subject
);
2578 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
2579 unuse_commit_buffer(commit
, commit_buffer
);
2581 strbuf_release(&buf
);
2586 static int intend_to_amend(void)
2588 struct object_id head
;
2591 if (get_oid("HEAD", &head
))
2592 return error(_("cannot read HEAD"));
2594 p
= oid_to_hex(&head
);
2595 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
2598 static int error_with_patch(struct commit
*commit
,
2599 const char *subject
, int subject_len
,
2600 struct replay_opts
*opts
, int exit_code
, int to_amend
)
2602 if (make_patch(commit
, opts
))
2606 if (intend_to_amend())
2609 fprintf(stderr
, "You can amend the commit now, with\n"
2611 " git commit --amend %s\n"
2613 "Once you are satisfied with your changes, run\n"
2615 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
2616 } else if (exit_code
)
2617 fprintf(stderr
, "Could not apply %s... %.*s\n",
2618 short_commit_name(commit
), subject_len
, subject
);
2623 static int error_failed_squash(struct commit
*commit
,
2624 struct replay_opts
*opts
, int subject_len
, const char *subject
)
2626 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
2627 return error(_("could not copy '%s' to '%s'"),
2628 rebase_path_squash_msg(), rebase_path_message());
2629 unlink(git_path_merge_msg(the_repository
));
2630 if (copy_file(git_path_merge_msg(the_repository
), rebase_path_message(), 0666))
2631 return error(_("could not copy '%s' to '%s'"),
2632 rebase_path_message(),
2633 git_path_merge_msg(the_repository
));
2634 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
2637 static int do_exec(const char *command_line
)
2639 struct argv_array child_env
= ARGV_ARRAY_INIT
;
2640 const char *child_argv
[] = { NULL
, NULL
};
2643 fprintf(stderr
, "Executing: %s\n", command_line
);
2644 child_argv
[0] = command_line
;
2645 argv_array_pushf(&child_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
2646 status
= run_command_v_opt_cd_env(child_argv
, RUN_USING_SHELL
, NULL
,
2649 /* force re-reading of the cache */
2650 if (discard_cache() < 0 || read_cache() < 0)
2651 return error(_("could not read index"));
2653 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
2656 warning(_("execution failed: %s\n%s"
2657 "You can fix the problem, and then run\n"
2659 " git rebase --continue\n"
2662 dirty
? N_("and made changes to the index and/or the "
2663 "working tree\n") : "");
2665 /* command not found */
2668 warning(_("execution succeeded: %s\nbut "
2669 "left changes to the index and/or the working tree\n"
2670 "Commit or stash your changes, and then run\n"
2672 " git rebase --continue\n"
2673 "\n"), command_line
);
2677 argv_array_clear(&child_env
);
2682 static int safe_append(const char *filename
, const char *fmt
, ...)
2685 struct lock_file lock
= LOCK_INIT
;
2686 int fd
= hold_lock_file_for_update(&lock
, filename
,
2687 LOCK_REPORT_ON_ERROR
);
2688 struct strbuf buf
= STRBUF_INIT
;
2693 if (strbuf_read_file(&buf
, filename
, 0) < 0 && errno
!= ENOENT
) {
2694 error_errno(_("could not read '%s'"), filename
);
2695 rollback_lock_file(&lock
);
2698 strbuf_complete(&buf
, '\n');
2700 strbuf_vaddf(&buf
, fmt
, ap
);
2703 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
2704 error_errno(_("could not write to '%s'"), filename
);
2705 strbuf_release(&buf
);
2706 rollback_lock_file(&lock
);
2709 if (commit_lock_file(&lock
) < 0) {
2710 strbuf_release(&buf
);
2711 rollback_lock_file(&lock
);
2712 return error(_("failed to finalize '%s'"), filename
);
2715 strbuf_release(&buf
);
2719 static int do_label(const char *name
, int len
)
2721 struct ref_store
*refs
= get_main_ref_store(the_repository
);
2722 struct ref_transaction
*transaction
;
2723 struct strbuf ref_name
= STRBUF_INIT
, err
= STRBUF_INIT
;
2724 struct strbuf msg
= STRBUF_INIT
;
2726 struct object_id head_oid
;
2728 if (len
== 1 && *name
== '#')
2729 return error("Illegal label name: '%.*s'", len
, name
);
2731 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
2732 strbuf_addf(&msg
, "rebase -i (label) '%.*s'", len
, name
);
2734 transaction
= ref_store_transaction_begin(refs
, &err
);
2736 error("%s", err
.buf
);
2738 } else if (get_oid("HEAD", &head_oid
)) {
2739 error(_("could not read HEAD"));
2741 } else if (ref_transaction_update(transaction
, ref_name
.buf
, &head_oid
,
2742 NULL
, 0, msg
.buf
, &err
) < 0 ||
2743 ref_transaction_commit(transaction
, &err
)) {
2744 error("%s", err
.buf
);
2747 ref_transaction_free(transaction
);
2748 strbuf_release(&err
);
2749 strbuf_release(&msg
);
2752 ret
= safe_append(rebase_path_refs_to_delete(),
2753 "%s\n", ref_name
.buf
);
2754 strbuf_release(&ref_name
);
2759 static const char *reflog_message(struct replay_opts
*opts
,
2760 const char *sub_action
, const char *fmt
, ...);
2762 static int do_reset(const char *name
, int len
, struct replay_opts
*opts
)
2764 struct strbuf ref_name
= STRBUF_INIT
;
2765 struct object_id oid
;
2766 struct lock_file lock
= LOCK_INIT
;
2767 struct tree_desc desc
;
2769 struct unpack_trees_options unpack_tree_opts
;
2772 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0)
2775 if (len
== 10 && !strncmp("[new root]", name
, len
)) {
2776 if (!opts
->have_squash_onto
) {
2778 if (commit_tree("", 0, the_hash_algo
->empty_tree
,
2779 NULL
, &opts
->squash_onto
,
2781 return error(_("writing fake root commit"));
2782 opts
->have_squash_onto
= 1;
2783 hex
= oid_to_hex(&opts
->squash_onto
);
2784 if (write_message(hex
, strlen(hex
),
2785 rebase_path_squash_onto(), 0))
2786 return error(_("writing squash-onto"));
2788 oidcpy(&oid
, &opts
->squash_onto
);
2790 /* Determine the length of the label */
2791 for (i
= 0; i
< len
; i
++)
2792 if (isspace(name
[i
]))
2795 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
2796 if (get_oid(ref_name
.buf
, &oid
) &&
2797 get_oid(ref_name
.buf
+ strlen("refs/rewritten/"), &oid
)) {
2798 error(_("could not read '%s'"), ref_name
.buf
);
2799 rollback_lock_file(&lock
);
2800 strbuf_release(&ref_name
);
2805 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
2806 setup_unpack_trees_porcelain(&unpack_tree_opts
, "reset");
2807 unpack_tree_opts
.head_idx
= 1;
2808 unpack_tree_opts
.src_index
= &the_index
;
2809 unpack_tree_opts
.dst_index
= &the_index
;
2810 unpack_tree_opts
.fn
= oneway_merge
;
2811 unpack_tree_opts
.merge
= 1;
2812 unpack_tree_opts
.update
= 1;
2814 if (read_cache_unmerged()) {
2815 rollback_lock_file(&lock
);
2816 strbuf_release(&ref_name
);
2817 return error_resolve_conflict(_(action_name(opts
)));
2820 if (!fill_tree_descriptor(&desc
, &oid
)) {
2821 error(_("failed to find tree of %s"), oid_to_hex(&oid
));
2822 rollback_lock_file(&lock
);
2823 free((void *)desc
.buffer
);
2824 strbuf_release(&ref_name
);
2828 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
2829 rollback_lock_file(&lock
);
2830 free((void *)desc
.buffer
);
2831 strbuf_release(&ref_name
);
2835 tree
= parse_tree_indirect(&oid
);
2836 prime_cache_tree(&the_index
, tree
);
2838 if (write_locked_index(&the_index
, &lock
, COMMIT_LOCK
) < 0)
2839 ret
= error(_("could not write index"));
2840 free((void *)desc
.buffer
);
2843 ret
= update_ref(reflog_message(opts
, "reset", "'%.*s'",
2844 len
, name
), "HEAD", &oid
,
2845 NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
2847 strbuf_release(&ref_name
);
2851 static int do_merge(struct commit
*commit
, const char *arg
, int arg_len
,
2852 int flags
, struct replay_opts
*opts
)
2854 int run_commit_flags
= (flags
& TODO_EDIT_MERGE_MSG
) ?
2855 EDIT_MSG
| VERIFY_MSG
: 0;
2856 struct strbuf ref_name
= STRBUF_INIT
;
2857 struct commit
*head_commit
, *merge_commit
, *i
;
2858 struct commit_list
*bases
, *j
, *reversed
= NULL
;
2859 struct merge_options o
;
2860 int merge_arg_len
, oneline_offset
, can_fast_forward
, ret
;
2861 static struct lock_file lock
;
2864 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0) {
2869 head_commit
= lookup_commit_reference_by_name("HEAD");
2871 ret
= error(_("cannot merge without a current revision"));
2875 oneline_offset
= arg_len
;
2876 merge_arg_len
= strcspn(arg
, " \t\n");
2877 p
= arg
+ merge_arg_len
;
2878 p
+= strspn(p
, " \t\n");
2879 if (*p
== '#' && (!p
[1] || isspace(p
[1]))) {
2880 p
+= 1 + strspn(p
+ 1, " \t\n");
2881 oneline_offset
= p
- arg
;
2882 } else if (p
- arg
< arg_len
)
2883 BUG("octopus merges are not supported yet: '%s'", p
);
2885 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", merge_arg_len
, arg
);
2886 merge_commit
= lookup_commit_reference_by_name(ref_name
.buf
);
2887 if (!merge_commit
) {
2888 /* fall back to non-rewritten ref or commit */
2889 strbuf_splice(&ref_name
, 0, strlen("refs/rewritten/"), "", 0);
2890 merge_commit
= lookup_commit_reference_by_name(ref_name
.buf
);
2893 if (!merge_commit
) {
2894 ret
= error(_("could not resolve '%s'"), ref_name
.buf
);
2898 if (opts
->have_squash_onto
&&
2899 !oidcmp(&head_commit
->object
.oid
, &opts
->squash_onto
)) {
2901 * When the user tells us to "merge" something into a
2902 * "[new root]", let's simply fast-forward to the merge head.
2904 rollback_lock_file(&lock
);
2905 ret
= fast_forward_to(&merge_commit
->object
.oid
,
2906 &head_commit
->object
.oid
, 0, opts
);
2911 const char *message
= get_commit_buffer(commit
, NULL
);
2916 ret
= error(_("could not get commit message of '%s'"),
2917 oid_to_hex(&commit
->object
.oid
));
2920 write_author_script(message
);
2921 find_commit_subject(message
, &body
);
2923 ret
= write_message(body
, len
, git_path_merge_msg(the_repository
), 0);
2924 unuse_commit_buffer(commit
, message
);
2926 error_errno(_("could not write '%s'"),
2927 git_path_merge_msg(the_repository
));
2931 struct strbuf buf
= STRBUF_INIT
;
2934 strbuf_addf(&buf
, "author %s", git_author_info(0));
2935 write_author_script(buf
.buf
);
2938 if (oneline_offset
< arg_len
) {
2939 p
= arg
+ oneline_offset
;
2940 len
= arg_len
- oneline_offset
;
2942 strbuf_addf(&buf
, "Merge branch '%.*s'",
2943 merge_arg_len
, arg
);
2948 ret
= write_message(p
, len
, git_path_merge_msg(the_repository
), 0);
2949 strbuf_release(&buf
);
2951 error_errno(_("could not write '%s'"),
2952 git_path_merge_msg(the_repository
));
2958 * If HEAD is not identical to the first parent of the original merge
2959 * commit, we cannot fast-forward.
2961 can_fast_forward
= opts
->allow_ff
&& commit
&& commit
->parents
&&
2962 !oidcmp(&commit
->parents
->item
->object
.oid
,
2963 &head_commit
->object
.oid
);
2966 * If the merge head is different from the original one, we cannot
2969 if (can_fast_forward
) {
2970 struct commit_list
*second_parent
= commit
->parents
->next
;
2972 if (second_parent
&& !second_parent
->next
&&
2973 oidcmp(&merge_commit
->object
.oid
,
2974 &second_parent
->item
->object
.oid
))
2975 can_fast_forward
= 0;
2978 if (can_fast_forward
&& commit
->parents
->next
&&
2979 !commit
->parents
->next
->next
&&
2980 !oidcmp(&commit
->parents
->next
->item
->object
.oid
,
2981 &merge_commit
->object
.oid
)) {
2982 rollback_lock_file(&lock
);
2983 ret
= fast_forward_to(&commit
->object
.oid
,
2984 &head_commit
->object
.oid
, 0, opts
);
2988 write_message(oid_to_hex(&merge_commit
->object
.oid
), GIT_SHA1_HEXSZ
,
2989 git_path_merge_head(the_repository
), 0);
2990 write_message("no-ff", 5, git_path_merge_mode(the_repository
), 0);
2992 bases
= get_merge_bases(head_commit
, merge_commit
);
2993 if (bases
&& !oidcmp(&merge_commit
->object
.oid
,
2994 &bases
->item
->object
.oid
)) {
2996 /* skip merging an ancestor of HEAD */
3000 for (j
= bases
; j
; j
= j
->next
)
3001 commit_list_insert(j
->item
, &reversed
);
3002 free_commit_list(bases
);
3005 init_merge_options(&o
);
3007 o
.branch2
= ref_name
.buf
;
3008 o
.buffer_output
= 2;
3010 ret
= merge_recursive(&o
, head_commit
, merge_commit
, reversed
, &i
);
3012 fputs(o
.obuf
.buf
, stdout
);
3013 strbuf_release(&o
.obuf
);
3015 error(_("could not even attempt to merge '%.*s'"),
3016 merge_arg_len
, arg
);
3020 * The return value of merge_recursive() is 1 on clean, and 0 on
3023 * Let's reverse that, so that do_merge() returns 0 upon success and
3024 * 1 upon failed merge (keeping the return value -1 for the cases where
3025 * we will want to reschedule the `merge` command).
3029 if (active_cache_changed
&&
3030 write_locked_index(&the_index
, &lock
, COMMIT_LOCK
)) {
3031 ret
= error(_("merge: Unable to write new index file"));
3035 rollback_lock_file(&lock
);
3037 rerere(opts
->allow_rerere_auto
);
3040 * In case of problems, we now want to return a positive
3041 * value (a negative one would indicate that the `merge`
3042 * command needs to be rescheduled).
3044 ret
= !!run_git_commit(git_path_merge_msg(the_repository
), opts
,
3048 strbuf_release(&ref_name
);
3049 rollback_lock_file(&lock
);
3053 static int is_final_fixup(struct todo_list
*todo_list
)
3055 int i
= todo_list
->current
;
3057 if (!is_fixup(todo_list
->items
[i
].command
))
3060 while (++i
< todo_list
->nr
)
3061 if (is_fixup(todo_list
->items
[i
].command
))
3063 else if (!is_noop(todo_list
->items
[i
].command
))
3068 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
3072 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
3073 if (!is_noop(todo_list
->items
[i
].command
))
3074 return todo_list
->items
[i
].command
;
3079 static int apply_autostash(struct replay_opts
*opts
)
3081 struct strbuf stash_sha1
= STRBUF_INIT
;
3082 struct child_process child
= CHILD_PROCESS_INIT
;
3085 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
3086 strbuf_release(&stash_sha1
);
3089 strbuf_trim(&stash_sha1
);
3092 child
.no_stdout
= 1;
3093 child
.no_stderr
= 1;
3094 argv_array_push(&child
.args
, "stash");
3095 argv_array_push(&child
.args
, "apply");
3096 argv_array_push(&child
.args
, stash_sha1
.buf
);
3097 if (!run_command(&child
))
3098 fprintf(stderr
, _("Applied autostash.\n"));
3100 struct child_process store
= CHILD_PROCESS_INIT
;
3103 argv_array_push(&store
.args
, "stash");
3104 argv_array_push(&store
.args
, "store");
3105 argv_array_push(&store
.args
, "-m");
3106 argv_array_push(&store
.args
, "autostash");
3107 argv_array_push(&store
.args
, "-q");
3108 argv_array_push(&store
.args
, stash_sha1
.buf
);
3109 if (run_command(&store
))
3110 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
3113 _("Applying autostash resulted in conflicts.\n"
3114 "Your changes are safe in the stash.\n"
3115 "You can run \"git stash pop\" or"
3116 " \"git stash drop\" at any time.\n"));
3119 strbuf_release(&stash_sha1
);
3123 static const char *reflog_message(struct replay_opts
*opts
,
3124 const char *sub_action
, const char *fmt
, ...)
3127 static struct strbuf buf
= STRBUF_INIT
;
3131 strbuf_addstr(&buf
, action_name(opts
));
3133 strbuf_addf(&buf
, " (%s)", sub_action
);
3135 strbuf_addstr(&buf
, ": ");
3136 strbuf_vaddf(&buf
, fmt
, ap
);
3143 static const char rescheduled_advice
[] =
3144 N_("Could not execute the todo command\n"
3148 "It has been rescheduled; To edit the command before continuing, please\n"
3149 "edit the todo list first:\n"
3151 " git rebase --edit-todo\n"
3152 " git rebase --continue\n");
3154 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
3156 int res
= 0, reschedule
= 0;
3158 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3160 assert(!(opts
->signoff
|| opts
->no_commit
||
3161 opts
->record_origin
|| opts
->edit
));
3162 if (read_and_refresh_cache(opts
))
3165 while (todo_list
->current
< todo_list
->nr
) {
3166 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
3167 if (save_todo(todo_list
, opts
))
3169 if (is_rebase_i(opts
)) {
3170 if (item
->command
!= TODO_COMMENT
) {
3171 FILE *f
= fopen(rebase_path_msgnum(), "w");
3173 todo_list
->done_nr
++;
3176 fprintf(f
, "%d\n", todo_list
->done_nr
);
3179 fprintf(stderr
, "Rebasing (%d/%d)%s",
3181 todo_list
->total_nr
,
3182 opts
->verbose
? "\n" : "\r");
3184 unlink(rebase_path_message());
3185 unlink(rebase_path_author_script());
3186 unlink(rebase_path_stopped_sha());
3187 unlink(rebase_path_amend());
3188 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
3190 if (item
->command
<= TODO_SQUASH
) {
3191 if (is_rebase_i(opts
))
3192 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
3193 command_to_string(item
->command
), NULL
),
3195 res
= do_pick_commit(item
->command
, item
->commit
,
3196 opts
, is_final_fixup(todo_list
));
3197 if (is_rebase_i(opts
) && res
< 0) {
3199 advise(_(rescheduled_advice
),
3200 get_item_line_length(todo_list
,
3201 todo_list
->current
),
3202 get_item_line(todo_list
,
3203 todo_list
->current
));
3204 todo_list
->current
--;
3205 if (save_todo(todo_list
, opts
))
3208 if (item
->command
== TODO_EDIT
) {
3209 struct commit
*commit
= item
->commit
;
3212 _("Stopped at %s... %.*s\n"),
3213 short_commit_name(commit
),
3214 item
->arg_len
, item
->arg
);
3215 return error_with_patch(commit
,
3216 item
->arg
, item
->arg_len
, opts
, res
,
3219 if (is_rebase_i(opts
) && !res
)
3220 record_in_rewritten(&item
->commit
->object
.oid
,
3221 peek_command(todo_list
, 1));
3222 if (res
&& is_fixup(item
->command
)) {
3225 return error_failed_squash(item
->commit
, opts
,
3226 item
->arg_len
, item
->arg
);
3227 } else if (res
&& is_rebase_i(opts
) && item
->commit
) {
3229 struct object_id oid
;
3232 * If we are rewording and have either
3233 * fast-forwarded already, or are about to
3234 * create a new root commit, we want to amend,
3235 * otherwise we do not.
3237 if (item
->command
== TODO_REWORD
&&
3238 !get_oid("HEAD", &oid
) &&
3239 (!oidcmp(&item
->commit
->object
.oid
, &oid
) ||
3240 (opts
->have_squash_onto
&&
3241 !oidcmp(&opts
->squash_onto
, &oid
))))
3244 return res
| error_with_patch(item
->commit
,
3245 item
->arg
, item
->arg_len
, opts
,
3248 } else if (item
->command
== TODO_EXEC
) {
3249 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
3250 int saved
= *end_of_arg
;
3254 res
= do_exec(item
->arg
);
3255 *end_of_arg
= saved
;
3257 /* Reread the todo file if it has changed. */
3259 ; /* fall through */
3260 else if (stat(get_todo_path(opts
), &st
))
3261 res
= error_errno(_("could not stat '%s'"),
3262 get_todo_path(opts
));
3263 else if (match_stat_data(&todo_list
->stat
, &st
)) {
3264 todo_list_release(todo_list
);
3265 if (read_populate_todo(todo_list
, opts
))
3266 res
= -1; /* message was printed */
3267 /* `current` will be incremented below */
3268 todo_list
->current
= -1;
3270 } else if (item
->command
== TODO_LABEL
) {
3271 if ((res
= do_label(item
->arg
, item
->arg_len
)))
3273 } else if (item
->command
== TODO_RESET
) {
3274 if ((res
= do_reset(item
->arg
, item
->arg_len
, opts
)))
3276 } else if (item
->command
== TODO_MERGE
) {
3277 if ((res
= do_merge(item
->commit
,
3278 item
->arg
, item
->arg_len
,
3279 item
->flags
, opts
)) < 0)
3281 else if (item
->commit
)
3282 record_in_rewritten(&item
->commit
->object
.oid
,
3283 peek_command(todo_list
, 1));
3285 /* failed with merge conflicts */
3286 return error_with_patch(item
->commit
,
3288 item
->arg_len
, opts
,
3290 } else if (!is_noop(item
->command
))
3291 return error(_("unknown command %d"), item
->command
);
3294 advise(_(rescheduled_advice
),
3295 get_item_line_length(todo_list
,
3296 todo_list
->current
),
3297 get_item_line(todo_list
, todo_list
->current
));
3298 todo_list
->current
--;
3299 if (save_todo(todo_list
, opts
))
3302 return error_with_patch(item
->commit
,
3304 item
->arg_len
, opts
,
3308 todo_list
->current
++;
3313 if (is_rebase_i(opts
)) {
3314 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
3317 /* Stopped in the middle, as planned? */
3318 if (todo_list
->current
< todo_list
->nr
)
3321 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
3322 starts_with(head_ref
.buf
, "refs/")) {
3324 struct object_id head
, orig
;
3327 if (get_oid("HEAD", &head
)) {
3328 res
= error(_("cannot read HEAD"));
3330 strbuf_release(&head_ref
);
3331 strbuf_release(&buf
);
3334 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
3335 get_oid_hex(buf
.buf
, &orig
)) {
3336 res
= error(_("could not read orig-head"));
3337 goto cleanup_head_ref
;
3340 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
3341 res
= error(_("could not read 'onto'"));
3342 goto cleanup_head_ref
;
3344 msg
= reflog_message(opts
, "finish", "%s onto %s",
3345 head_ref
.buf
, buf
.buf
);
3346 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
3347 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
3348 res
= error(_("could not update %s"),
3350 goto cleanup_head_ref
;
3352 msg
= reflog_message(opts
, "finish", "returning to %s",
3354 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
3355 res
= error(_("could not update HEAD to %s"),
3357 goto cleanup_head_ref
;
3362 if (opts
->verbose
) {
3363 struct rev_info log_tree_opt
;
3364 struct object_id orig
, head
;
3366 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
3367 init_revisions(&log_tree_opt
, NULL
);
3368 log_tree_opt
.diff
= 1;
3369 log_tree_opt
.diffopt
.output_format
=
3370 DIFF_FORMAT_DIFFSTAT
;
3371 log_tree_opt
.disable_stdin
= 1;
3373 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
3374 !get_oid(buf
.buf
, &orig
) &&
3375 !get_oid("HEAD", &head
)) {
3376 diff_tree_oid(&orig
, &head
, "",
3377 &log_tree_opt
.diffopt
);
3378 log_tree_diff_flush(&log_tree_opt
);
3381 flush_rewritten_pending();
3382 if (!stat(rebase_path_rewritten_list(), &st
) &&
3384 struct child_process child
= CHILD_PROCESS_INIT
;
3385 const char *post_rewrite_hook
=
3386 find_hook("post-rewrite");
3388 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
3390 argv_array_push(&child
.args
, "notes");
3391 argv_array_push(&child
.args
, "copy");
3392 argv_array_push(&child
.args
, "--for-rewrite=rebase");
3393 /* we don't care if this copying failed */
3394 run_command(&child
);
3396 if (post_rewrite_hook
) {
3397 struct child_process hook
= CHILD_PROCESS_INIT
;
3399 hook
.in
= open(rebase_path_rewritten_list(),
3401 hook
.stdout_to_stderr
= 1;
3402 argv_array_push(&hook
.args
, post_rewrite_hook
);
3403 argv_array_push(&hook
.args
, "rebase");
3404 /* we don't care if this hook failed */
3408 apply_autostash(opts
);
3410 fprintf(stderr
, "Successfully rebased and updated %s.\n",
3413 strbuf_release(&buf
);
3414 strbuf_release(&head_ref
);
3418 * Sequence of picks finished successfully; cleanup by
3419 * removing the .git/sequencer directory
3421 return sequencer_remove_state(opts
);
3424 static int continue_single_pick(void)
3426 const char *argv
[] = { "commit", NULL
};
3428 if (!file_exists(git_path_cherry_pick_head(the_repository
)) &&
3429 !file_exists(git_path_revert_head(the_repository
)))
3430 return error(_("no cherry-pick or revert in progress"));
3431 return run_command_v_opt(argv
, RUN_GIT_CMD
);
3434 static int commit_staged_changes(struct replay_opts
*opts
,
3435 struct todo_list
*todo_list
)
3437 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
3438 unsigned int final_fixup
= 0, is_clean
;
3440 if (has_unstaged_changes(1))
3441 return error(_("cannot rebase: You have unstaged changes."));
3443 is_clean
= !has_uncommitted_changes(0);
3445 if (file_exists(rebase_path_amend())) {
3446 struct strbuf rev
= STRBUF_INIT
;
3447 struct object_id head
, to_amend
;
3449 if (get_oid("HEAD", &head
))
3450 return error(_("cannot amend non-existing commit"));
3451 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
3452 return error(_("invalid file: '%s'"), rebase_path_amend());
3453 if (get_oid_hex(rev
.buf
, &to_amend
))
3454 return error(_("invalid contents: '%s'"),
3455 rebase_path_amend());
3456 if (!is_clean
&& oidcmp(&head
, &to_amend
))
3457 return error(_("\nYou have uncommitted changes in your "
3458 "working tree. Please, commit them\n"
3459 "first and then run 'git rebase "
3460 "--continue' again."));
3462 * When skipping a failed fixup/squash, we need to edit the
3463 * commit message, the current fixup list and count, and if it
3464 * was the last fixup/squash in the chain, we need to clean up
3465 * the commit message and if there was a squash, let the user
3468 if (is_clean
&& !oidcmp(&head
, &to_amend
) &&
3469 opts
->current_fixup_count
> 0 &&
3470 file_exists(rebase_path_stopped_sha())) {
3471 const char *p
= opts
->current_fixups
.buf
;
3472 int len
= opts
->current_fixups
.len
;
3474 opts
->current_fixup_count
--;
3476 BUG("Incorrect current_fixups:\n%s", p
);
3477 while (len
&& p
[len
- 1] != '\n')
3479 strbuf_setlen(&opts
->current_fixups
, len
);
3480 if (write_message(p
, len
, rebase_path_current_fixups(),
3482 return error(_("could not write file: '%s'"),
3483 rebase_path_current_fixups());
3486 * If a fixup/squash in a fixup/squash chain failed, the
3487 * commit message is already correct, no need to commit
3490 * Only if it is the final command in the fixup/squash
3491 * chain, and only if the chain is longer than a single
3492 * fixup/squash command (which was just skipped), do we
3493 * actually need to re-commit with a cleaned up commit
3496 if (opts
->current_fixup_count
> 0 &&
3497 !is_fixup(peek_command(todo_list
, 0))) {
3500 * If there was not a single "squash" in the
3501 * chain, we only need to clean up the commit
3502 * message, no need to bother the user with
3503 * opening the commit message in the editor.
3505 if (!starts_with(p
, "squash ") &&
3506 !strstr(p
, "\nsquash "))
3507 flags
= (flags
& ~EDIT_MSG
) | CLEANUP_MSG
;
3508 } else if (is_fixup(peek_command(todo_list
, 0))) {
3510 * We need to update the squash message to skip
3511 * the latest commit message.
3513 struct commit
*commit
;
3514 const char *path
= rebase_path_squash_msg();
3516 if (parse_head(&commit
) ||
3517 !(p
= get_commit_buffer(commit
, NULL
)) ||
3518 write_message(p
, strlen(p
), path
, 0)) {
3519 unuse_commit_buffer(commit
, p
);
3520 return error(_("could not write file: "
3523 unuse_commit_buffer(commit
, p
);
3527 strbuf_release(&rev
);
3532 const char *cherry_pick_head
= git_path_cherry_pick_head(the_repository
);
3534 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
3535 return error(_("could not remove CHERRY_PICK_HEAD"));
3540 if (run_git_commit(final_fixup
? NULL
: rebase_path_message(),
3542 return error(_("could not commit staged changes."));
3543 unlink(rebase_path_amend());
3545 unlink(rebase_path_fixup_msg());
3546 unlink(rebase_path_squash_msg());
3548 if (opts
->current_fixup_count
> 0) {
3550 * Whether final fixup or not, we just cleaned up the commit
3553 unlink(rebase_path_current_fixups());
3554 strbuf_reset(&opts
->current_fixups
);
3555 opts
->current_fixup_count
= 0;
3560 int sequencer_continue(struct replay_opts
*opts
)
3562 struct todo_list todo_list
= TODO_LIST_INIT
;
3565 if (read_and_refresh_cache(opts
))
3568 if (read_populate_opts(opts
))
3570 if (is_rebase_i(opts
)) {
3571 if ((res
= read_populate_todo(&todo_list
, opts
)))
3572 goto release_todo_list
;
3573 if (commit_staged_changes(opts
, &todo_list
))
3575 } else if (!file_exists(get_todo_path(opts
)))
3576 return continue_single_pick();
3577 else if ((res
= read_populate_todo(&todo_list
, opts
)))
3578 goto release_todo_list
;
3580 if (!is_rebase_i(opts
)) {
3581 /* Verify that the conflict has been resolved */
3582 if (file_exists(git_path_cherry_pick_head(the_repository
)) ||
3583 file_exists(git_path_revert_head(the_repository
))) {
3584 res
= continue_single_pick();
3586 goto release_todo_list
;
3588 if (index_differs_from("HEAD", NULL
, 0)) {
3589 res
= error_dirty_index(opts
);
3590 goto release_todo_list
;
3592 todo_list
.current
++;
3593 } else if (file_exists(rebase_path_stopped_sha())) {
3594 struct strbuf buf
= STRBUF_INIT
;
3595 struct object_id oid
;
3597 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
3598 !get_oid_committish(buf
.buf
, &oid
))
3599 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
3600 strbuf_release(&buf
);
3603 res
= pick_commits(&todo_list
, opts
);
3605 todo_list_release(&todo_list
);
3609 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
3611 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3612 return do_pick_commit(opts
->action
== REPLAY_PICK
?
3613 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
3616 int sequencer_pick_revisions(struct replay_opts
*opts
)
3618 struct todo_list todo_list
= TODO_LIST_INIT
;
3619 struct object_id oid
;
3623 if (read_and_refresh_cache(opts
))
3626 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
3627 struct object_id oid
;
3628 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
3630 /* This happens when using --stdin. */
3634 if (!get_oid(name
, &oid
)) {
3635 if (!lookup_commit_reference_gently(&oid
, 1)) {
3636 enum object_type type
= oid_object_info(the_repository
,
3639 return error(_("%s: can't cherry-pick a %s"),
3640 name
, type_name(type
));
3643 return error(_("%s: bad revision"), name
);
3647 * If we were called as "git cherry-pick <commit>", just
3648 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
3649 * REVERT_HEAD, and don't touch the sequencer state.
3650 * This means it is possible to cherry-pick in the middle
3651 * of a cherry-pick sequence.
3653 if (opts
->revs
->cmdline
.nr
== 1 &&
3654 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
3655 opts
->revs
->no_walk
&&
3656 !opts
->revs
->cmdline
.rev
->flags
) {
3657 struct commit
*cmit
;
3658 if (prepare_revision_walk(opts
->revs
))
3659 return error(_("revision walk setup failed"));
3660 cmit
= get_revision(opts
->revs
);
3661 if (!cmit
|| get_revision(opts
->revs
))
3662 return error("BUG: expected exactly one commit from walk");
3663 return single_pick(cmit
, opts
);
3667 * Start a new cherry-pick/ revert sequence; but
3668 * first, make sure that an existing one isn't in
3672 if (walk_revs_populate_todo(&todo_list
, opts
) ||
3673 create_seq_dir() < 0)
3675 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
3676 return error(_("can't revert as initial commit"));
3677 if (save_head(oid_to_hex(&oid
)))
3679 if (save_opts(opts
))
3681 update_abort_safety_file();
3682 res
= pick_commits(&todo_list
, opts
);
3683 todo_list_release(&todo_list
);
3687 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
3689 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
3690 struct strbuf sob
= STRBUF_INIT
;
3693 strbuf_addstr(&sob
, sign_off_header
);
3694 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
3695 getenv("GIT_COMMITTER_EMAIL")));
3696 strbuf_addch(&sob
, '\n');
3699 strbuf_complete_line(msgbuf
);
3702 * If the whole message buffer is equal to the sob, pretend that we
3703 * found a conforming footer with a matching sob
3705 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
3706 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
3709 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
3712 const char *append_newlines
= NULL
;
3713 size_t len
= msgbuf
->len
- ignore_footer
;
3717 * The buffer is completely empty. Leave foom for
3718 * the title and body to be filled in by the user.
3720 append_newlines
= "\n\n";
3721 } else if (len
== 1) {
3723 * Buffer contains a single newline. Add another
3724 * so that we leave room for the title and body.
3726 append_newlines
= "\n";
3727 } else if (msgbuf
->buf
[len
- 2] != '\n') {
3729 * Buffer ends with a single newline. Add another
3730 * so that there is an empty line between the message
3733 append_newlines
= "\n";
3734 } /* else, the buffer already ends with two newlines. */
3736 if (append_newlines
)
3737 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
3738 append_newlines
, strlen(append_newlines
));
3741 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
3742 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
3745 strbuf_release(&sob
);
3748 struct labels_entry
{
3749 struct hashmap_entry entry
;
3750 char label
[FLEX_ARRAY
];
3753 static int labels_cmp(const void *fndata
, const struct labels_entry
*a
,
3754 const struct labels_entry
*b
, const void *key
)
3756 return key
? strcmp(a
->label
, key
) : strcmp(a
->label
, b
->label
);
3759 struct string_entry
{
3760 struct oidmap_entry entry
;
3761 char string
[FLEX_ARRAY
];
3764 struct label_state
{
3765 struct oidmap commit2label
;
3766 struct hashmap labels
;
3770 static const char *label_oid(struct object_id
*oid
, const char *label
,
3771 struct label_state
*state
)
3773 struct labels_entry
*labels_entry
;
3774 struct string_entry
*string_entry
;
3775 struct object_id dummy
;
3779 string_entry
= oidmap_get(&state
->commit2label
, oid
);
3781 return string_entry
->string
;
3784 * For "uninteresting" commits, i.e. commits that are not to be
3785 * rebased, and which can therefore not be labeled, we use a unique
3786 * abbreviation of the commit name. This is slightly more complicated
3787 * than calling find_unique_abbrev() because we also need to make
3788 * sure that the abbreviation does not conflict with any other
3791 * We disallow "interesting" commits to be labeled by a string that
3792 * is a valid full-length hash, to ensure that we always can find an
3793 * abbreviation for any uninteresting commit's names that does not
3794 * clash with any other label.
3799 strbuf_reset(&state
->buf
);
3800 strbuf_grow(&state
->buf
, GIT_SHA1_HEXSZ
);
3801 label
= p
= state
->buf
.buf
;
3803 find_unique_abbrev_r(p
, oid
, default_abbrev
);
3806 * We may need to extend the abbreviated hash so that there is
3807 * no conflicting label.
3809 if (hashmap_get_from_hash(&state
->labels
, strihash(p
), p
)) {
3810 size_t i
= strlen(p
) + 1;
3812 oid_to_hex_r(p
, oid
);
3813 for (; i
< GIT_SHA1_HEXSZ
; i
++) {
3816 if (!hashmap_get_from_hash(&state
->labels
,
3822 } else if (((len
= strlen(label
)) == the_hash_algo
->hexsz
&&
3823 !get_oid_hex(label
, &dummy
)) ||
3824 (len
== 1 && *label
== '#') ||
3825 hashmap_get_from_hash(&state
->labels
,
3826 strihash(label
), label
)) {
3828 * If the label already exists, or if the label is a valid full
3829 * OID, or the label is a '#' (which we use as a separator
3830 * between merge heads and oneline), we append a dash and a
3831 * number to make it unique.
3833 struct strbuf
*buf
= &state
->buf
;
3836 strbuf_add(buf
, label
, len
);
3838 for (i
= 2; ; i
++) {
3839 strbuf_setlen(buf
, len
);
3840 strbuf_addf(buf
, "-%d", i
);
3841 if (!hashmap_get_from_hash(&state
->labels
,
3850 FLEX_ALLOC_STR(labels_entry
, label
, label
);
3851 hashmap_entry_init(labels_entry
, strihash(label
));
3852 hashmap_add(&state
->labels
, labels_entry
);
3854 FLEX_ALLOC_STR(string_entry
, string
, label
);
3855 oidcpy(&string_entry
->entry
.oid
, oid
);
3856 oidmap_put(&state
->commit2label
, string_entry
);
3858 return string_entry
->string
;
3861 static int make_script_with_merges(struct pretty_print_context
*pp
,
3862 struct rev_info
*revs
, FILE *out
,
3865 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
3866 int rebase_cousins
= flags
& TODO_LIST_REBASE_COUSINS
;
3867 struct strbuf buf
= STRBUF_INIT
, oneline
= STRBUF_INIT
;
3868 struct strbuf label
= STRBUF_INIT
;
3869 struct commit_list
*commits
= NULL
, **tail
= &commits
, *iter
;
3870 struct commit_list
*tips
= NULL
, **tips_tail
= &tips
;
3871 struct commit
*commit
;
3872 struct oidmap commit2todo
= OIDMAP_INIT
;
3873 struct string_entry
*entry
;
3874 struct oidset interesting
= OIDSET_INIT
, child_seen
= OIDSET_INIT
,
3875 shown
= OIDSET_INIT
;
3876 struct label_state state
= { OIDMAP_INIT
, { NULL
}, STRBUF_INIT
};
3878 int abbr
= flags
& TODO_LIST_ABBREVIATE_CMDS
;
3879 const char *cmd_pick
= abbr
? "p" : "pick",
3880 *cmd_label
= abbr
? "l" : "label",
3881 *cmd_reset
= abbr
? "t" : "reset",
3882 *cmd_merge
= abbr
? "m" : "merge";
3884 oidmap_init(&commit2todo
, 0);
3885 oidmap_init(&state
.commit2label
, 0);
3886 hashmap_init(&state
.labels
, (hashmap_cmp_fn
) labels_cmp
, NULL
, 0);
3887 strbuf_init(&state
.buf
, 32);
3889 if (revs
->cmdline
.nr
&& (revs
->cmdline
.rev
[0].flags
& BOTTOM
)) {
3890 struct object_id
*oid
= &revs
->cmdline
.rev
[0].item
->oid
;
3891 FLEX_ALLOC_STR(entry
, string
, "onto");
3892 oidcpy(&entry
->entry
.oid
, oid
);
3893 oidmap_put(&state
.commit2label
, entry
);
3898 * - get onelines for all commits
3899 * - gather all branch tips (i.e. 2nd or later parents of merges)
3900 * - label all branch tips
3902 while ((commit
= get_revision(revs
))) {
3903 struct commit_list
*to_merge
;
3905 const char *p1
, *p2
;
3906 struct object_id
*oid
;
3909 tail
= &commit_list_insert(commit
, tail
)->next
;
3910 oidset_insert(&interesting
, &commit
->object
.oid
);
3912 is_empty
= is_original_commit_empty(commit
);
3913 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
3916 strbuf_reset(&oneline
);
3917 pretty_print_commit(pp
, commit
, &oneline
);
3919 to_merge
= commit
->parents
? commit
->parents
->next
: NULL
;
3921 /* non-merge commit: easy case */
3923 if (!keep_empty
&& is_empty
)
3924 strbuf_addf(&buf
, "%c ", comment_line_char
);
3925 strbuf_addf(&buf
, "%s %s %s", cmd_pick
,
3926 oid_to_hex(&commit
->object
.oid
),
3929 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
3930 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
3931 oidmap_put(&commit2todo
, entry
);
3936 is_octopus
= to_merge
&& to_merge
->next
;
3939 BUG("Octopus merges not yet supported");
3941 /* Create a label */
3942 strbuf_reset(&label
);
3943 if (skip_prefix(oneline
.buf
, "Merge ", &p1
) &&
3944 (p1
= strchr(p1
, '\'')) &&
3945 (p2
= strchr(++p1
, '\'')))
3946 strbuf_add(&label
, p1
, p2
- p1
);
3947 else if (skip_prefix(oneline
.buf
, "Merge pull request ",
3949 (p1
= strstr(p1
, " from ")))
3950 strbuf_addstr(&label
, p1
+ strlen(" from "));
3952 strbuf_addbuf(&label
, &oneline
);
3954 for (p1
= label
.buf
; *p1
; p1
++)
3959 strbuf_addf(&buf
, "%s -C %s",
3960 cmd_merge
, oid_to_hex(&commit
->object
.oid
));
3962 /* label the tip of merged branch */
3963 oid
= &to_merge
->item
->object
.oid
;
3964 strbuf_addch(&buf
, ' ');
3966 if (!oidset_contains(&interesting
, oid
))
3967 strbuf_addstr(&buf
, label_oid(oid
, NULL
, &state
));
3969 tips_tail
= &commit_list_insert(to_merge
->item
,
3972 strbuf_addstr(&buf
, label_oid(oid
, label
.buf
, &state
));
3974 strbuf_addf(&buf
, " # %s", oneline
.buf
);
3976 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
3977 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
3978 oidmap_put(&commit2todo
, entry
);
3983 * - label branch points
3984 * - add HEAD to the branch tips
3986 for (iter
= commits
; iter
; iter
= iter
->next
) {
3987 struct commit_list
*parent
= iter
->item
->parents
;
3988 for (; parent
; parent
= parent
->next
) {
3989 struct object_id
*oid
= &parent
->item
->object
.oid
;
3990 if (!oidset_contains(&interesting
, oid
))
3992 if (!oidset_contains(&child_seen
, oid
))
3993 oidset_insert(&child_seen
, oid
);
3995 label_oid(oid
, "branch-point", &state
);
3998 /* Add HEAD as implict "tip of branch" */
4000 tips_tail
= &commit_list_insert(iter
->item
,
4005 * Third phase: output the todo list. This is a bit tricky, as we
4006 * want to avoid jumping back and forth between revisions. To
4007 * accomplish that goal, we walk backwards from the branch tips,
4008 * gathering commits not yet shown, reversing the list on the fly,
4009 * then outputting that list (labeling revisions as needed).
4011 fprintf(out
, "%s onto\n", cmd_label
);
4012 for (iter
= tips
; iter
; iter
= iter
->next
) {
4013 struct commit_list
*list
= NULL
, *iter2
;
4015 commit
= iter
->item
;
4016 if (oidset_contains(&shown
, &commit
->object
.oid
))
4018 entry
= oidmap_get(&state
.commit2label
, &commit
->object
.oid
);
4021 fprintf(out
, "\n# Branch %s\n", entry
->string
);
4025 while (oidset_contains(&interesting
, &commit
->object
.oid
) &&
4026 !oidset_contains(&shown
, &commit
->object
.oid
)) {
4027 commit_list_insert(commit
, &list
);
4028 if (!commit
->parents
) {
4032 commit
= commit
->parents
->item
;
4036 fprintf(out
, "%s %s\n", cmd_reset
,
4037 rebase_cousins
? "onto" : "[new root]");
4039 const char *to
= NULL
;
4041 entry
= oidmap_get(&state
.commit2label
,
4042 &commit
->object
.oid
);
4045 else if (!rebase_cousins
)
4046 to
= label_oid(&commit
->object
.oid
, NULL
,
4049 if (!to
|| !strcmp(to
, "onto"))
4050 fprintf(out
, "%s onto\n", cmd_reset
);
4052 strbuf_reset(&oneline
);
4053 pretty_print_commit(pp
, commit
, &oneline
);
4054 fprintf(out
, "%s %s # %s\n",
4055 cmd_reset
, to
, oneline
.buf
);
4059 for (iter2
= list
; iter2
; iter2
= iter2
->next
) {
4060 struct object_id
*oid
= &iter2
->item
->object
.oid
;
4061 entry
= oidmap_get(&commit2todo
, oid
);
4062 /* only show if not already upstream */
4064 fprintf(out
, "%s\n", entry
->string
);
4065 entry
= oidmap_get(&state
.commit2label
, oid
);
4067 fprintf(out
, "%s %s\n",
4068 cmd_label
, entry
->string
);
4069 oidset_insert(&shown
, oid
);
4072 free_commit_list(list
);
4075 free_commit_list(commits
);
4076 free_commit_list(tips
);
4078 strbuf_release(&label
);
4079 strbuf_release(&oneline
);
4080 strbuf_release(&buf
);
4082 oidmap_free(&commit2todo
, 1);
4083 oidmap_free(&state
.commit2label
, 1);
4084 hashmap_free(&state
.labels
, 1);
4085 strbuf_release(&state
.buf
);
4090 int sequencer_make_script(FILE *out
, int argc
, const char **argv
,
4093 char *format
= NULL
;
4094 struct pretty_print_context pp
= {0};
4095 struct strbuf buf
= STRBUF_INIT
;
4096 struct rev_info revs
;
4097 struct commit
*commit
;
4098 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
4099 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
4100 int rebase_merges
= flags
& TODO_LIST_REBASE_MERGES
;
4102 init_revisions(&revs
, NULL
);
4103 revs
.verbose_header
= 1;
4105 revs
.max_parents
= 1;
4106 revs
.cherry_mark
= 1;
4109 revs
.right_only
= 1;
4110 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
4111 revs
.topo_order
= 1;
4113 revs
.pretty_given
= 1;
4114 git_config_get_string("rebase.instructionFormat", &format
);
4115 if (!format
|| !*format
) {
4117 format
= xstrdup("%s");
4119 get_commit_format(format
, &revs
);
4121 pp
.fmt
= revs
.commit_format
;
4122 pp
.output_encoding
= get_log_output_encoding();
4124 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
4125 return error(_("make_script: unhandled options"));
4127 if (prepare_revision_walk(&revs
) < 0)
4128 return error(_("make_script: error preparing revisions"));
4131 return make_script_with_merges(&pp
, &revs
, out
, flags
);
4133 while ((commit
= get_revision(&revs
))) {
4134 int is_empty
= is_original_commit_empty(commit
);
4136 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
4139 if (!keep_empty
&& is_empty
)
4140 strbuf_addf(&buf
, "%c ", comment_line_char
);
4141 strbuf_addf(&buf
, "%s %s ", insn
,
4142 oid_to_hex(&commit
->object
.oid
));
4143 pretty_print_commit(&pp
, commit
, &buf
);
4144 strbuf_addch(&buf
, '\n');
4145 fputs(buf
.buf
, out
);
4147 strbuf_release(&buf
);
4152 * Add commands after pick and (series of) squash/fixup commands
4155 int sequencer_add_exec_commands(const char *commands
)
4157 const char *todo_file
= rebase_path_todo();
4158 struct todo_list todo_list
= TODO_LIST_INIT
;
4159 struct todo_item
*item
;
4160 struct strbuf
*buf
= &todo_list
.buf
;
4161 size_t offset
= 0, commands_len
= strlen(commands
);
4164 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
4165 return error(_("could not read '%s'."), todo_file
);
4167 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
4168 todo_list_release(&todo_list
);
4169 return error(_("unusable todo list: '%s'"), todo_file
);
4173 /* insert <commands> before every pick except the first one */
4174 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
4175 if (item
->command
== TODO_PICK
&& !first
) {
4176 strbuf_insert(buf
, item
->offset_in_buf
+ offset
,
4177 commands
, commands_len
);
4178 offset
+= commands_len
;
4183 /* append final <commands> */
4184 strbuf_add(buf
, commands
, commands_len
);
4186 i
= write_message(buf
->buf
, buf
->len
, todo_file
, 0);
4187 todo_list_release(&todo_list
);
4191 int transform_todos(unsigned flags
)
4193 const char *todo_file
= rebase_path_todo();
4194 struct todo_list todo_list
= TODO_LIST_INIT
;
4195 struct strbuf buf
= STRBUF_INIT
;
4196 struct todo_item
*item
;
4199 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
4200 return error(_("could not read '%s'."), todo_file
);
4202 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
4203 todo_list_release(&todo_list
);
4204 return error(_("unusable todo list: '%s'"), todo_file
);
4207 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
4208 /* if the item is not a command write it and continue */
4209 if (item
->command
>= TODO_COMMENT
) {
4210 strbuf_addf(&buf
, "%.*s\n", item
->arg_len
, item
->arg
);
4214 /* add command to the buffer */
4215 if (flags
& TODO_LIST_ABBREVIATE_CMDS
)
4216 strbuf_addch(&buf
, command_to_char(item
->command
));
4218 strbuf_addstr(&buf
, command_to_string(item
->command
));
4222 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
4223 short_commit_name(item
->commit
) :
4224 oid_to_hex(&item
->commit
->object
.oid
);
4226 if (item
->command
== TODO_MERGE
) {
4227 if (item
->flags
& TODO_EDIT_MERGE_MSG
)
4228 strbuf_addstr(&buf
, " -c");
4230 strbuf_addstr(&buf
, " -C");
4233 strbuf_addf(&buf
, " %s", oid
);
4236 /* add all the rest */
4238 strbuf_addch(&buf
, '\n');
4240 strbuf_addf(&buf
, " %.*s\n", item
->arg_len
, item
->arg
);
4243 i
= write_message(buf
.buf
, buf
.len
, todo_file
, 0);
4244 todo_list_release(&todo_list
);
4249 CHECK_IGNORE
= 0, CHECK_WARN
, CHECK_ERROR
4252 static enum check_level
get_missing_commit_check_level(void)
4256 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
4257 !strcasecmp("ignore", value
))
4258 return CHECK_IGNORE
;
4259 if (!strcasecmp("warn", value
))
4261 if (!strcasecmp("error", value
))
4263 warning(_("unrecognized setting %s for option "
4264 "rebase.missingCommitsCheck. Ignoring."), value
);
4265 return CHECK_IGNORE
;
4268 define_commit_slab(commit_seen
, unsigned char);
4270 * Check if the user dropped some commits by mistake
4271 * Behaviour determined by rebase.missingCommitsCheck.
4272 * Check if there is an unrecognized command or a
4273 * bad SHA-1 in a command.
4275 int check_todo_list(void)
4277 enum check_level check_level
= get_missing_commit_check_level();
4278 struct strbuf todo_file
= STRBUF_INIT
;
4279 struct todo_list todo_list
= TODO_LIST_INIT
;
4280 struct strbuf missing
= STRBUF_INIT
;
4281 int advise_to_edit_todo
= 0, res
= 0, i
;
4282 struct commit_seen commit_seen
;
4284 init_commit_seen(&commit_seen
);
4286 strbuf_addstr(&todo_file
, rebase_path_todo());
4287 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
4291 advise_to_edit_todo
= res
=
4292 parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
4294 if (res
|| check_level
== CHECK_IGNORE
)
4297 /* Mark the commits in git-rebase-todo as seen */
4298 for (i
= 0; i
< todo_list
.nr
; i
++) {
4299 struct commit
*commit
= todo_list
.items
[i
].commit
;
4301 *commit_seen_at(&commit_seen
, commit
) = 1;
4304 todo_list_release(&todo_list
);
4305 strbuf_addstr(&todo_file
, ".backup");
4306 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
4310 strbuf_release(&todo_file
);
4311 res
= !!parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
4313 /* Find commits in git-rebase-todo.backup yet unseen */
4314 for (i
= todo_list
.nr
- 1; i
>= 0; i
--) {
4315 struct todo_item
*item
= todo_list
.items
+ i
;
4316 struct commit
*commit
= item
->commit
;
4317 if (commit
&& !*commit_seen_at(&commit_seen
, commit
)) {
4318 strbuf_addf(&missing
, " - %s %.*s\n",
4319 short_commit_name(commit
),
4320 item
->arg_len
, item
->arg
);
4321 *commit_seen_at(&commit_seen
, commit
) = 1;
4325 /* Warn about missing commits */
4329 if (check_level
== CHECK_ERROR
)
4330 advise_to_edit_todo
= res
= 1;
4333 _("Warning: some commits may have been dropped accidentally.\n"
4334 "Dropped commits (newer to older):\n"));
4336 /* Make the list user-friendly and display */
4337 fputs(missing
.buf
, stderr
);
4338 strbuf_release(&missing
);
4340 fprintf(stderr
, _("To avoid this message, use \"drop\" to "
4341 "explicitly remove a commit.\n\n"
4342 "Use 'git config rebase.missingCommitsCheck' to change "
4343 "the level of warnings.\n"
4344 "The possible behaviours are: ignore, warn, error.\n\n"));
4347 clear_commit_seen(&commit_seen
);
4348 strbuf_release(&todo_file
);
4349 todo_list_release(&todo_list
);
4351 if (advise_to_edit_todo
)
4353 _("You can fix this with 'git rebase --edit-todo' "
4354 "and then run 'git rebase --continue'.\n"
4355 "Or you can abort the rebase with 'git rebase"
4361 static int rewrite_file(const char *path
, const char *buf
, size_t len
)
4364 int fd
= open(path
, O_WRONLY
| O_TRUNC
);
4366 return error_errno(_("could not open '%s' for writing"), path
);
4367 if (write_in_full(fd
, buf
, len
) < 0)
4368 rc
= error_errno(_("could not write to '%s'"), path
);
4369 if (close(fd
) && !rc
)
4370 rc
= error_errno(_("could not close '%s'"), path
);
4374 /* skip picking commits whose parents are unchanged */
4375 int skip_unnecessary_picks(void)
4377 const char *todo_file
= rebase_path_todo();
4378 struct strbuf buf
= STRBUF_INIT
;
4379 struct todo_list todo_list
= TODO_LIST_INIT
;
4380 struct object_id onto_oid
, *oid
= &onto_oid
, *parent_oid
;
4383 if (!read_oneliner(&buf
, rebase_path_onto(), 0))
4384 return error(_("could not read 'onto'"));
4385 if (get_oid(buf
.buf
, &onto_oid
)) {
4386 strbuf_release(&buf
);
4387 return error(_("need a HEAD to fixup"));
4389 strbuf_release(&buf
);
4391 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
4393 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
4394 todo_list_release(&todo_list
);
4398 for (i
= 0; i
< todo_list
.nr
; i
++) {
4399 struct todo_item
*item
= todo_list
.items
+ i
;
4401 if (item
->command
>= TODO_NOOP
)
4403 if (item
->command
!= TODO_PICK
)
4405 if (parse_commit(item
->commit
)) {
4406 todo_list_release(&todo_list
);
4407 return error(_("could not parse commit '%s'"),
4408 oid_to_hex(&item
->commit
->object
.oid
));
4410 if (!item
->commit
->parents
)
4411 break; /* root commit */
4412 if (item
->commit
->parents
->next
)
4413 break; /* merge commit */
4414 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
4415 if (hashcmp(parent_oid
->hash
, oid
->hash
))
4417 oid
= &item
->commit
->object
.oid
;
4420 int offset
= get_item_line_offset(&todo_list
, i
);
4421 const char *done_path
= rebase_path_done();
4423 fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
4425 error_errno(_("could not open '%s' for writing"),
4427 todo_list_release(&todo_list
);
4430 if (write_in_full(fd
, todo_list
.buf
.buf
, offset
) < 0) {
4431 error_errno(_("could not write to '%s'"), done_path
);
4432 todo_list_release(&todo_list
);
4438 if (rewrite_file(rebase_path_todo(), todo_list
.buf
.buf
+ offset
,
4439 todo_list
.buf
.len
- offset
) < 0) {
4440 todo_list_release(&todo_list
);
4444 todo_list
.current
= i
;
4445 if (is_fixup(peek_command(&todo_list
, 0)))
4446 record_in_rewritten(oid
, peek_command(&todo_list
, 0));
4449 todo_list_release(&todo_list
);
4450 printf("%s\n", oid_to_hex(oid
));
4455 struct subject2item_entry
{
4456 struct hashmap_entry entry
;
4458 char subject
[FLEX_ARRAY
];
4461 static int subject2item_cmp(const void *fndata
,
4462 const struct subject2item_entry
*a
,
4463 const struct subject2item_entry
*b
, const void *key
)
4465 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
4468 define_commit_slab(commit_todo_item
, struct todo_item
*);
4471 * Rearrange the todo list that has both "pick commit-id msg" and "pick
4472 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
4473 * after the former, and change "pick" to "fixup"/"squash".
4475 * Note that if the config has specified a custom instruction format, each log
4476 * message will have to be retrieved from the commit (as the oneline in the
4477 * script cannot be trusted) in order to normalize the autosquash arrangement.
4479 int rearrange_squash(void)
4481 const char *todo_file
= rebase_path_todo();
4482 struct todo_list todo_list
= TODO_LIST_INIT
;
4483 struct hashmap subject2item
;
4484 int res
= 0, rearranged
= 0, *next
, *tail
, i
;
4486 struct commit_todo_item commit_todo
;
4488 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
4490 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
4491 todo_list_release(&todo_list
);
4495 init_commit_todo_item(&commit_todo
);
4497 * The hashmap maps onelines to the respective todo list index.
4499 * If any items need to be rearranged, the next[i] value will indicate
4500 * which item was moved directly after the i'th.
4502 * In that case, last[i] will indicate the index of the latest item to
4503 * be moved to appear after the i'th.
4505 hashmap_init(&subject2item
, (hashmap_cmp_fn
) subject2item_cmp
,
4506 NULL
, todo_list
.nr
);
4507 ALLOC_ARRAY(next
, todo_list
.nr
);
4508 ALLOC_ARRAY(tail
, todo_list
.nr
);
4509 ALLOC_ARRAY(subjects
, todo_list
.nr
);
4510 for (i
= 0; i
< todo_list
.nr
; i
++) {
4511 struct strbuf buf
= STRBUF_INIT
;
4512 struct todo_item
*item
= todo_list
.items
+ i
;
4513 const char *commit_buffer
, *subject
, *p
;
4516 struct subject2item_entry
*entry
;
4518 next
[i
] = tail
[i
] = -1;
4519 if (!item
->commit
|| item
->command
== TODO_DROP
) {
4524 if (is_fixup(item
->command
)) {
4525 todo_list_release(&todo_list
);
4526 clear_commit_todo_item(&commit_todo
);
4527 return error(_("the script was already rearranged."));
4530 *commit_todo_item_at(&commit_todo
, item
->commit
) = item
;
4532 parse_commit(item
->commit
);
4533 commit_buffer
= get_commit_buffer(item
->commit
, NULL
);
4534 find_commit_subject(commit_buffer
, &subject
);
4535 format_subject(&buf
, subject
, " ");
4536 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
4537 unuse_commit_buffer(item
->commit
, commit_buffer
);
4538 if ((skip_prefix(subject
, "fixup! ", &p
) ||
4539 skip_prefix(subject
, "squash! ", &p
))) {
4540 struct commit
*commit2
;
4545 if (!skip_prefix(p
, "fixup! ", &p
) &&
4546 !skip_prefix(p
, "squash! ", &p
))
4550 if ((entry
= hashmap_get_from_hash(&subject2item
,
4552 /* found by title */
4554 else if (!strchr(p
, ' ') &&
4556 lookup_commit_reference_by_name(p
)) &&
4557 *commit_todo_item_at(&commit_todo
, commit2
))
4558 /* found by commit name */
4559 i2
= *commit_todo_item_at(&commit_todo
, commit2
)
4562 /* copy can be a prefix of the commit subject */
4563 for (i2
= 0; i2
< i
; i2
++)
4565 starts_with(subjects
[i2
], p
))
4573 todo_list
.items
[i
].command
=
4574 starts_with(subject
, "fixup!") ?
4575 TODO_FIXUP
: TODO_SQUASH
;
4581 } else if (!hashmap_get_from_hash(&subject2item
,
4582 strhash(subject
), subject
)) {
4583 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
4585 hashmap_entry_init(entry
, strhash(entry
->subject
));
4586 hashmap_put(&subject2item
, entry
);
4591 struct strbuf buf
= STRBUF_INIT
;
4593 for (i
= 0; i
< todo_list
.nr
; i
++) {
4594 enum todo_command command
= todo_list
.items
[i
].command
;
4598 * Initially, all commands are 'pick's. If it is a
4599 * fixup or a squash now, we have rearranged it.
4601 if (is_fixup(command
))
4606 get_item_line(&todo_list
, cur
);
4608 get_item_line(&todo_list
, cur
+ 1);
4610 /* replace 'pick', by 'fixup' or 'squash' */
4611 command
= todo_list
.items
[cur
].command
;
4612 if (is_fixup(command
)) {
4614 todo_command_info
[command
].str
);
4615 bol
+= strcspn(bol
, " \t");
4618 strbuf_add(&buf
, bol
, eol
- bol
);
4624 res
= rewrite_file(todo_file
, buf
.buf
, buf
.len
);
4625 strbuf_release(&buf
);
4630 for (i
= 0; i
< todo_list
.nr
; i
++)
4633 hashmap_free(&subject2item
, 1);
4634 todo_list_release(&todo_list
);
4636 clear_commit_todo_item(&commit_todo
);