9 #include "run-command.h"
12 #include "cache-tree.h"
16 #include "merge-recursive.h"
18 #include "argv-array.h"
22 #include "wt-status.h"
24 #include "notes-utils.h"
27 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
29 const char sign_off_header
[] = "Signed-off-by: ";
30 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
32 GIT_PATH_FUNC(git_path_commit_editmsg
, "COMMIT_EDITMSG")
34 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
36 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
37 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
38 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
39 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
41 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
43 * The file containing rebase commands, comments, and empty lines.
44 * This file is created by "git rebase -i" then edited by the user. As
45 * the lines are processed, they are removed from the front of this
46 * file and written to the tail of 'done'.
48 static GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
50 * The rebase command lines that have already been processed. A line
51 * is moved here when it is first handled, before any associated user
54 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
56 * The file to keep track of how many commands were already processed (e.g.
59 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum");
61 * The file to keep track of how many commands are to be processed in total
62 * (e.g. for the prompt).
64 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end");
66 * The commit message that is planned to be used for any changes that
67 * need to be committed following a user interaction.
69 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
71 * The file into which is accumulated the suggested commit message for
72 * squash/fixup commands. When the first of a series of squash/fixups
73 * is seen, the file is created and the commit message from the
74 * previous commit and from the first squash/fixup commit are written
75 * to it. The commit message for each subsequent squash/fixup commit
76 * is appended to the file as it is processed.
78 * The first line of the file is of the form
79 * # This is a combination of $count commits.
80 * where $count is the number of commits whose messages have been
81 * written to the file so far (including the initial "pick" commit).
82 * Each time that a commit message is processed, this line is read and
83 * updated. It is deleted just before the combined commit is made.
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 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
96 * GIT_AUTHOR_DATE that will be used for the commit that is currently
99 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
101 * When an "edit" rebase command is being processed, the SHA1 of the
102 * commit to be edited is recorded in this file. When "git rebase
103 * --continue" is executed, if there are any staged changes then they
104 * will be amended to the HEAD commit, but only provided the HEAD
105 * commit is still the commit to be edited. When any other rebase
106 * command is processed, this file is deleted.
108 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
110 * When we stop at a given patch via the "edit" command, this file contains
111 * the abbreviated commit name of the corresponding patch.
113 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
115 * For the post-rewrite hook, we make a list of rewritten commits and
116 * their new sha1s. The rewritten-pending list keeps the sha1s of
117 * commits that have been processed, but not committed yet,
118 * e.g. because they are waiting for a 'squash' command.
120 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
121 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
122 "rebase-merge/rewritten-pending")
124 * The following files are written by git-rebase just after parsing the
125 * command-line (and are only consumed, not modified, by the sequencer).
127 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
128 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
129 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
130 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
131 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
132 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
133 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
134 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
135 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
137 static int git_sequencer_config(const char *k
, const char *v
, void *cb
)
139 struct replay_opts
*opts
= cb
;
142 if (!strcmp(k
, "commit.cleanup")) {
145 status
= git_config_string(&s
, k
, v
);
149 if (!strcmp(s
, "verbatim"))
150 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
151 else if (!strcmp(s
, "whitespace"))
152 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
153 else if (!strcmp(s
, "strip"))
154 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
155 else if (!strcmp(s
, "scissors"))
156 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
158 warning(_("invalid commit message cleanup mode '%s'"),
164 if (!strcmp(k
, "commit.gpgsign")) {
165 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
169 status
= git_gpg_config(k
, v
, NULL
);
173 return git_diff_basic_config(k
, v
, NULL
);
176 void sequencer_init_config(struct replay_opts
*opts
)
178 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
179 git_config(git_sequencer_config
, opts
);
182 static inline int is_rebase_i(const struct replay_opts
*opts
)
184 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
187 static const char *get_dir(const struct replay_opts
*opts
)
189 if (is_rebase_i(opts
))
190 return rebase_path();
191 return git_path_seq_dir();
194 static const char *get_todo_path(const struct replay_opts
*opts
)
196 if (is_rebase_i(opts
))
197 return rebase_path_todo();
198 return git_path_todo_file();
202 * Returns 0 for non-conforming footer
203 * Returns 1 for conforming footer
204 * Returns 2 when sob exists within conforming footer
205 * Returns 3 when sob exists within conforming footer as last entry
207 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
210 struct trailer_info info
;
212 int found_sob
= 0, found_sob_last
= 0;
214 trailer_info_get(&info
, sb
->buf
);
216 if (info
.trailer_start
== info
.trailer_end
)
219 for (i
= 0; i
< info
.trailer_nr
; i
++)
220 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
222 if (i
== info
.trailer_nr
- 1)
226 trailer_info_release(&info
);
235 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
237 static struct strbuf buf
= STRBUF_INIT
;
241 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
245 int sequencer_remove_state(struct replay_opts
*opts
)
247 struct strbuf dir
= STRBUF_INIT
;
250 free(opts
->gpg_sign
);
251 free(opts
->strategy
);
252 for (i
= 0; i
< opts
->xopts_nr
; i
++)
253 free(opts
->xopts
[i
]);
256 strbuf_addstr(&dir
, get_dir(opts
));
257 remove_dir_recursively(&dir
, 0);
258 strbuf_release(&dir
);
263 static const char *action_name(const struct replay_opts
*opts
)
265 switch (opts
->action
) {
269 return N_("cherry-pick");
270 case REPLAY_INTERACTIVE_REBASE
:
271 return N_("rebase -i");
273 die(_("Unknown action: %d"), opts
->action
);
276 struct commit_message
{
283 static const char *short_commit_name(struct commit
*commit
)
285 return find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
);
288 static int get_message(struct commit
*commit
, struct commit_message
*out
)
290 const char *abbrev
, *subject
;
293 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
294 abbrev
= short_commit_name(commit
);
296 subject_len
= find_commit_subject(out
->message
, &subject
);
298 out
->subject
= xmemdupz(subject
, subject_len
);
299 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
300 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
305 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
307 free(msg
->parent_label
);
310 unuse_commit_buffer(commit
, msg
->message
);
313 static void print_advice(int show_hint
, struct replay_opts
*opts
)
315 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
318 fprintf(stderr
, "%s\n", msg
);
320 * A conflict has occurred but the porcelain
321 * (typically rebase --interactive) wants to take care
322 * of the commit itself so remove CHERRY_PICK_HEAD
324 unlink(git_path_cherry_pick_head());
330 advise(_("after resolving the conflicts, mark the corrected paths\n"
331 "with 'git add <paths>' or 'git rm <paths>'"));
333 advise(_("after resolving the conflicts, mark the corrected paths\n"
334 "with 'git add <paths>' or 'git rm <paths>'\n"
335 "and commit the result with 'git commit'"));
339 static int write_message(const void *buf
, size_t len
, const char *filename
,
342 static struct lock_file msg_file
;
344 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
346 return error_errno(_("could not lock '%s'"), filename
);
347 if (write_in_full(msg_fd
, buf
, len
) < 0) {
348 rollback_lock_file(&msg_file
);
349 return error_errno(_("could not write to '%s'"), filename
);
351 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
352 rollback_lock_file(&msg_file
);
353 return error_errno(_("could not write eol to '%s'"), filename
);
355 if (commit_lock_file(&msg_file
) < 0) {
356 rollback_lock_file(&msg_file
);
357 return error(_("failed to finalize '%s'."), filename
);
364 * Reads a file that was presumably written by a shell script, i.e. with an
365 * end-of-line marker that needs to be stripped.
367 * Note that only the last end-of-line marker is stripped, consistent with the
368 * behavior of "$(cat path)" in a shell script.
370 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
372 static int read_oneliner(struct strbuf
*buf
,
373 const char *path
, int skip_if_empty
)
375 int orig_len
= buf
->len
;
377 if (!file_exists(path
))
380 if (strbuf_read_file(buf
, path
, 0) < 0) {
381 warning_errno(_("could not read '%s'"), path
);
385 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
386 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
388 buf
->buf
[buf
->len
] = '\0';
391 if (skip_if_empty
&& buf
->len
== orig_len
)
397 static struct tree
*empty_tree(void)
399 return lookup_tree(the_hash_algo
->empty_tree
);
402 static int error_dirty_index(struct replay_opts
*opts
)
404 if (read_cache_unmerged())
405 return error_resolve_conflict(_(action_name(opts
)));
407 error(_("your local changes would be overwritten by %s."),
408 _(action_name(opts
)));
410 if (advice_commit_before_merge
)
411 advise(_("commit your changes or stash them to proceed."));
415 static void update_abort_safety_file(void)
417 struct object_id head
;
419 /* Do nothing on a single-pick */
420 if (!file_exists(git_path_seq_dir()))
423 if (!get_oid("HEAD", &head
))
424 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
426 write_file(git_path_abort_safety_file(), "%s", "");
429 static int fast_forward_to(const struct object_id
*to
, const struct object_id
*from
,
430 int unborn
, struct replay_opts
*opts
)
432 struct ref_transaction
*transaction
;
433 struct strbuf sb
= STRBUF_INIT
;
434 struct strbuf err
= STRBUF_INIT
;
437 if (checkout_fast_forward(from
, to
, 1))
438 return -1; /* the callee should have complained already */
440 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
442 transaction
= ref_transaction_begin(&err
);
444 ref_transaction_update(transaction
, "HEAD",
445 to
, unborn
? &null_oid
: from
,
447 ref_transaction_commit(transaction
, &err
)) {
448 ref_transaction_free(transaction
);
449 error("%s", err
.buf
);
451 strbuf_release(&err
);
456 strbuf_release(&err
);
457 ref_transaction_free(transaction
);
458 update_abort_safety_file();
462 void append_conflicts_hint(struct strbuf
*msgbuf
)
466 strbuf_addch(msgbuf
, '\n');
467 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
468 for (i
= 0; i
< active_nr
;) {
469 const struct cache_entry
*ce
= active_cache
[i
++];
471 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
472 while (i
< active_nr
&& !strcmp(ce
->name
,
473 active_cache
[i
]->name
))
479 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
480 const char *base_label
, const char *next_label
,
481 struct object_id
*head
, struct strbuf
*msgbuf
,
482 struct replay_opts
*opts
)
484 struct merge_options o
;
485 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
488 static struct lock_file index_lock
;
490 if (hold_locked_index(&index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
495 init_merge_options(&o
);
496 o
.ancestor
= base
? base_label
: "(empty tree)";
498 o
.branch2
= next
? next_label
: "(empty tree)";
499 if (is_rebase_i(opts
))
501 o
.show_rename_progress
= 1;
503 head_tree
= parse_tree_indirect(head
);
504 next_tree
= next
? next
->tree
: empty_tree();
505 base_tree
= base
? base
->tree
: empty_tree();
507 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
508 parse_merge_opt(&o
, *xopt
);
510 clean
= merge_trees(&o
,
512 next_tree
, base_tree
, &result
);
513 if (is_rebase_i(opts
) && clean
<= 0)
514 fputs(o
.obuf
.buf
, stdout
);
515 strbuf_release(&o
.obuf
);
516 diff_warn_rename_limit("merge.renamelimit", o
.needed_rename_limit
, 0);
520 if (active_cache_changed
&&
521 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
523 * TRANSLATORS: %s will be "revert", "cherry-pick" or
526 return error(_("%s: Unable to write new index file"),
527 _(action_name(opts
)));
528 rollback_lock_file(&index_lock
);
531 append_conflicts_hint(msgbuf
);
536 static int is_index_unchanged(void)
538 struct object_id head_oid
;
539 struct commit
*head_commit
;
541 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
542 return error(_("could not resolve HEAD commit"));
544 head_commit
= lookup_commit(&head_oid
);
547 * If head_commit is NULL, check_commit, called from
548 * lookup_commit, would have indicated that head_commit is not
549 * a commit object already. parse_commit() will return failure
550 * without further complaints in such a case. Otherwise, if
551 * the commit is invalid, parse_commit() will complain. So
552 * there is nothing for us to say here. Just return failure.
554 if (parse_commit(head_commit
))
557 if (!active_cache_tree
)
558 active_cache_tree
= cache_tree();
560 if (!cache_tree_fully_valid(active_cache_tree
))
561 if (cache_tree_update(&the_index
, 0))
562 return error(_("unable to update cache tree"));
564 return !oidcmp(&active_cache_tree
->oid
,
565 &head_commit
->tree
->object
.oid
);
568 static int write_author_script(const char *message
)
570 struct strbuf buf
= STRBUF_INIT
;
575 if (!*message
|| starts_with(message
, "\n")) {
577 /* Missing 'author' line? */
578 unlink(rebase_path_author_script());
580 } else if (skip_prefix(message
, "author ", &message
))
582 else if ((eol
= strchr(message
, '\n')))
587 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
588 while (*message
&& *message
!= '\n' && *message
!= '\r')
589 if (skip_prefix(message
, " <", &message
))
591 else if (*message
!= '\'')
592 strbuf_addch(&buf
, *(message
++));
594 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
595 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
596 while (*message
&& *message
!= '\n' && *message
!= '\r')
597 if (skip_prefix(message
, "> ", &message
))
599 else if (*message
!= '\'')
600 strbuf_addch(&buf
, *(message
++));
602 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
603 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
604 while (*message
&& *message
!= '\n' && *message
!= '\r')
605 if (*message
!= '\'')
606 strbuf_addch(&buf
, *(message
++));
608 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
609 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
610 strbuf_release(&buf
);
615 * Read a list of environment variable assignments (such as the author-script
616 * file) into an environment block. Returns -1 on error, 0 otherwise.
618 static int read_env_script(struct argv_array
*env
)
620 struct strbuf script
= STRBUF_INIT
;
624 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
627 for (p
= script
.buf
; *p
; p
++)
628 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
629 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
631 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
632 else if (*p
== '\n') {
637 for (i
= 0, p
= script
.buf
; i
< count
; i
++) {
638 argv_array_push(env
, p
);
645 static char *get_author(const char *message
)
650 a
= find_commit_header(message
, "author", &len
);
652 return xmemdupz(a
, len
);
657 static const char staged_changes_advice
[] =
658 N_("you have staged changes in your working tree\n"
659 "If these changes are meant to be squashed into the previous commit, run:\n"
661 " git commit --amend %s\n"
663 "If they are meant to go into a new commit, run:\n"
667 "In both cases, once you're done, continue with:\n"
669 " git rebase --continue\n");
671 #define ALLOW_EMPTY (1<<0)
672 #define EDIT_MSG (1<<1)
673 #define AMEND_MSG (1<<2)
674 #define CLEANUP_MSG (1<<3)
675 #define VERIFY_MSG (1<<4)
678 * If we are cherry-pick, and if the merge did not result in
679 * hand-editing, we will hit this commit and inherit the original
680 * author date and name.
682 * If we are revert, or if our cherry-pick results in a hand merge,
683 * we had better say that the current user is responsible for that.
685 * An exception is when run_git_commit() is called during an
686 * interactive rebase: in that case, we will want to retain the
689 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
692 struct child_process cmd
= CHILD_PROCESS_INIT
;
697 if (is_rebase_i(opts
)) {
698 if (!(flags
& EDIT_MSG
)) {
699 cmd
.stdout_to_stderr
= 1;
703 if (read_env_script(&cmd
.env_array
)) {
704 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
706 return error(_(staged_changes_advice
),
711 argv_array_push(&cmd
.args
, "commit");
713 if (!(flags
& VERIFY_MSG
))
714 argv_array_push(&cmd
.args
, "-n");
715 if ((flags
& AMEND_MSG
))
716 argv_array_push(&cmd
.args
, "--amend");
718 argv_array_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
720 argv_array_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
721 if ((flags
& CLEANUP_MSG
))
722 argv_array_push(&cmd
.args
, "--cleanup=strip");
723 if ((flags
& EDIT_MSG
))
724 argv_array_push(&cmd
.args
, "-e");
725 else if (!(flags
& CLEANUP_MSG
) &&
726 !opts
->signoff
&& !opts
->record_origin
&&
727 git_config_get_value("commit.cleanup", &value
))
728 argv_array_push(&cmd
.args
, "--cleanup=verbatim");
730 if ((flags
& ALLOW_EMPTY
))
731 argv_array_push(&cmd
.args
, "--allow-empty");
733 if (opts
->allow_empty_message
)
734 argv_array_push(&cmd
.args
, "--allow-empty-message");
737 /* hide stderr on success */
738 struct strbuf buf
= STRBUF_INIT
;
739 int rc
= pipe_command(&cmd
,
741 /* stdout is already redirected */
745 fputs(buf
.buf
, stderr
);
746 strbuf_release(&buf
);
750 return run_command(&cmd
);
753 static int rest_is_empty(const struct strbuf
*sb
, int start
)
758 /* Check if the rest is just whitespace and Signed-off-by's. */
759 for (i
= start
; i
< sb
->len
; i
++) {
760 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
766 if (strlen(sign_off_header
) <= eol
- i
&&
767 starts_with(sb
->buf
+ i
, sign_off_header
)) {
772 if (!isspace(sb
->buf
[i
++]))
780 * Find out if the message in the strbuf contains only whitespace and
781 * Signed-off-by lines.
783 int message_is_empty(const struct strbuf
*sb
,
784 enum commit_msg_cleanup_mode cleanup_mode
)
786 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
788 return rest_is_empty(sb
, 0);
792 * See if the user edited the message in the editor or left what
793 * was in the template intact
795 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
796 enum commit_msg_cleanup_mode cleanup_mode
)
798 struct strbuf tmpl
= STRBUF_INIT
;
801 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
804 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
807 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
808 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
810 strbuf_release(&tmpl
);
811 return rest_is_empty(sb
, start
- sb
->buf
);
814 int update_head_with_reflog(const struct commit
*old_head
,
815 const struct object_id
*new_head
,
816 const char *action
, const struct strbuf
*msg
,
819 struct ref_transaction
*transaction
;
820 struct strbuf sb
= STRBUF_INIT
;
825 strbuf_addstr(&sb
, action
);
826 strbuf_addstr(&sb
, ": ");
829 nl
= strchr(msg
->buf
, '\n');
831 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
833 strbuf_addbuf(&sb
, msg
);
834 strbuf_addch(&sb
, '\n');
837 transaction
= ref_transaction_begin(err
);
839 ref_transaction_update(transaction
, "HEAD", new_head
,
840 old_head
? &old_head
->object
.oid
: &null_oid
,
842 ref_transaction_commit(transaction
, err
)) {
845 ref_transaction_free(transaction
);
851 static int run_rewrite_hook(const struct object_id
*oldoid
,
852 const struct object_id
*newoid
)
854 struct child_process proc
= CHILD_PROCESS_INIT
;
857 struct strbuf sb
= STRBUF_INIT
;
859 argv
[0] = find_hook("post-rewrite");
868 proc
.stdout_to_stderr
= 1;
870 code
= start_command(&proc
);
873 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
874 sigchain_push(SIGPIPE
, SIG_IGN
);
875 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
878 sigchain_pop(SIGPIPE
);
879 return finish_command(&proc
);
882 void commit_post_rewrite(const struct commit
*old_head
,
883 const struct object_id
*new_head
)
885 struct notes_rewrite_cfg
*cfg
;
887 cfg
= init_copy_notes_for_rewrite("amend");
889 /* we are amending, so old_head is not NULL */
890 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
891 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
893 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
896 static int run_prepare_commit_msg_hook(struct strbuf
*msg
, const char *commit
)
898 struct argv_array hook_env
= ARGV_ARRAY_INIT
;
902 name
= git_path_commit_editmsg();
903 if (write_message(msg
->buf
, msg
->len
, name
, 0))
906 argv_array_pushf(&hook_env
, "GIT_INDEX_FILE=%s", get_index_file());
907 argv_array_push(&hook_env
, "GIT_EDITOR=:");
909 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
910 "commit", commit
, NULL
);
912 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
915 ret
= error(_("'prepare-commit-msg' hook failed"));
916 argv_array_clear(&hook_env
);
921 static const char implicit_ident_advice_noconfig
[] =
922 N_("Your name and email address were configured automatically based\n"
923 "on your username and hostname. Please check that they are accurate.\n"
924 "You can suppress this message by setting them explicitly. Run the\n"
925 "following command and follow the instructions in your editor to edit\n"
926 "your configuration file:\n"
928 " git config --global --edit\n"
930 "After doing this, you may fix the identity used for this commit with:\n"
932 " git commit --amend --reset-author\n");
934 static const char implicit_ident_advice_config
[] =
935 N_("Your name and email address were configured automatically based\n"
936 "on your username and hostname. Please check that they are accurate.\n"
937 "You can suppress this message by setting them explicitly:\n"
939 " git config --global user.name \"Your Name\"\n"
940 " git config --global user.email you@example.com\n"
942 "After doing this, you may fix the identity used for this commit with:\n"
944 " git commit --amend --reset-author\n");
946 static const char *implicit_ident_advice(void)
948 char *user_config
= expand_user_path("~/.gitconfig", 0);
949 char *xdg_config
= xdg_config_home("config");
950 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
956 return _(implicit_ident_advice_config
);
958 return _(implicit_ident_advice_noconfig
);
962 void print_commit_summary(const char *prefix
, const struct object_id
*oid
,
966 struct commit
*commit
;
967 struct strbuf format
= STRBUF_INIT
;
969 struct pretty_print_context pctx
= {0};
970 struct strbuf author_ident
= STRBUF_INIT
;
971 struct strbuf committer_ident
= STRBUF_INIT
;
973 commit
= lookup_commit(oid
);
975 die(_("couldn't look up newly created commit"));
976 if (parse_commit(commit
))
977 die(_("could not parse newly created commit"));
979 strbuf_addstr(&format
, "format:%h] %s");
981 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
982 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
983 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
984 strbuf_addstr(&format
, "\n Author: ");
985 strbuf_addbuf_percentquote(&format
, &author_ident
);
987 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
988 struct strbuf date
= STRBUF_INIT
;
990 format_commit_message(commit
, "%ad", &date
, &pctx
);
991 strbuf_addstr(&format
, "\n Date: ");
992 strbuf_addbuf_percentquote(&format
, &date
);
993 strbuf_release(&date
);
995 if (!committer_ident_sufficiently_given()) {
996 strbuf_addstr(&format
, "\n Committer: ");
997 strbuf_addbuf_percentquote(&format
, &committer_ident
);
998 if (advice_implicit_identity
) {
999 strbuf_addch(&format
, '\n');
1000 strbuf_addstr(&format
, implicit_ident_advice());
1003 strbuf_release(&author_ident
);
1004 strbuf_release(&committer_ident
);
1006 init_revisions(&rev
, prefix
);
1007 setup_revisions(0, NULL
, &rev
, NULL
);
1010 rev
.diffopt
.output_format
=
1011 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1013 rev
.verbose_header
= 1;
1014 rev
.show_root_diff
= 1;
1015 get_commit_format(format
.buf
, &rev
);
1016 rev
.always_show_header
= 0;
1017 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1018 rev
.diffopt
.break_opt
= 0;
1019 diff_setup_done(&rev
.diffopt
);
1021 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1023 die_errno(_("unable to resolve HEAD after creating commit"));
1024 if (!strcmp(head
, "HEAD"))
1025 head
= _("detached HEAD");
1027 skip_prefix(head
, "refs/heads/", &head
);
1028 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1029 _(" (root-commit)") : "");
1031 if (!log_tree_commit(&rev
, commit
)) {
1032 rev
.always_show_header
= 1;
1033 rev
.use_terminator
= 1;
1034 log_tree_commit(&rev
, commit
);
1037 strbuf_release(&format
);
1040 static int parse_head(struct commit
**head
)
1042 struct commit
*current_head
;
1043 struct object_id oid
;
1045 if (get_oid("HEAD", &oid
)) {
1046 current_head
= NULL
;
1048 current_head
= lookup_commit_reference(&oid
);
1050 return error(_("could not parse HEAD"));
1051 if (oidcmp(&oid
, ¤t_head
->object
.oid
)) {
1052 warning(_("HEAD %s is not a commit!"),
1055 if (parse_commit(current_head
))
1056 return error(_("could not parse HEAD commit"));
1058 *head
= current_head
;
1064 * Try to commit without forking 'git commit'. In some cases we need
1065 * to run 'git commit' to display an error message
1068 * -1 - error unable to commit
1070 * 1 - run 'git commit'
1072 static int try_to_commit(struct strbuf
*msg
, const char *author
,
1073 struct replay_opts
*opts
, unsigned int flags
,
1074 struct object_id
*oid
)
1076 struct object_id tree
;
1077 struct commit
*current_head
;
1078 struct commit_list
*parents
= NULL
;
1079 struct commit_extra_header
*extra
= NULL
;
1080 struct strbuf err
= STRBUF_INIT
;
1081 struct strbuf commit_msg
= STRBUF_INIT
;
1082 char *amend_author
= NULL
;
1083 const char *hook_commit
= NULL
;
1084 enum commit_msg_cleanup_mode cleanup
;
1087 if (parse_head(¤t_head
))
1090 if (flags
& AMEND_MSG
) {
1091 const char *exclude_gpgsig
[] = { "gpgsig", NULL
};
1092 const char *out_enc
= get_commit_output_encoding();
1093 const char *message
= logmsg_reencode(current_head
, NULL
,
1097 const char *orig_message
= NULL
;
1099 find_commit_subject(message
, &orig_message
);
1101 strbuf_addstr(msg
, orig_message
);
1102 hook_commit
= "HEAD";
1104 author
= amend_author
= get_author(message
);
1105 unuse_commit_buffer(current_head
, message
);
1107 res
= error(_("unable to parse commit author"));
1110 parents
= copy_commit_list(current_head
->parents
);
1111 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1112 } else if (current_head
) {
1113 commit_list_insert(current_head
, &parents
);
1116 if (write_cache_as_tree(&tree
, 0, NULL
)) {
1117 res
= error(_("git write-tree failed to write a tree"));
1121 if (!(flags
& ALLOW_EMPTY
) && !oidcmp(current_head
?
1122 ¤t_head
->tree
->object
.oid
:
1123 &empty_tree_oid
, &tree
)) {
1124 res
= 1; /* run 'git commit' to display error message */
1128 if (find_hook("prepare-commit-msg")) {
1129 res
= run_prepare_commit_msg_hook(msg
, hook_commit
);
1132 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1134 res
= error_errno(_("unable to read commit message "
1136 git_path_commit_editmsg());
1142 cleanup
= (flags
& CLEANUP_MSG
) ? COMMIT_MSG_CLEANUP_ALL
:
1143 opts
->default_msg_cleanup
;
1145 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1146 strbuf_stripspace(msg
, cleanup
== COMMIT_MSG_CLEANUP_ALL
);
1147 if (!opts
->allow_empty_message
&& message_is_empty(msg
, cleanup
)) {
1148 res
= 1; /* run 'git commit' to display error message */
1152 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
,
1153 oid
, author
, opts
->gpg_sign
, extra
)) {
1154 res
= error(_("failed to write commit object"));
1158 if (update_head_with_reflog(current_head
, oid
,
1159 getenv("GIT_REFLOG_ACTION"), msg
, &err
)) {
1160 res
= error("%s", err
.buf
);
1164 if (flags
& AMEND_MSG
)
1165 commit_post_rewrite(current_head
, oid
);
1168 free_commit_extra_headers(extra
);
1169 strbuf_release(&err
);
1170 strbuf_release(&commit_msg
);
1176 static int do_commit(const char *msg_file
, const char *author
,
1177 struct replay_opts
*opts
, unsigned int flags
)
1181 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
)) {
1182 struct object_id oid
;
1183 struct strbuf sb
= STRBUF_INIT
;
1185 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1186 return error_errno(_("unable to read commit message "
1190 res
= try_to_commit(msg_file
? &sb
: NULL
, author
, opts
, flags
,
1192 strbuf_release(&sb
);
1194 unlink(git_path_cherry_pick_head());
1195 unlink(git_path_merge_msg());
1196 if (!is_rebase_i(opts
))
1197 print_commit_summary(NULL
, &oid
,
1198 SUMMARY_SHOW_AUTHOR_DATE
);
1203 return run_git_commit(msg_file
, opts
, flags
);
1208 static int is_original_commit_empty(struct commit
*commit
)
1210 const struct object_id
*ptree_oid
;
1212 if (parse_commit(commit
))
1213 return error(_("could not parse commit %s"),
1214 oid_to_hex(&commit
->object
.oid
));
1215 if (commit
->parents
) {
1216 struct commit
*parent
= commit
->parents
->item
;
1217 if (parse_commit(parent
))
1218 return error(_("could not parse parent commit %s"),
1219 oid_to_hex(&parent
->object
.oid
));
1220 ptree_oid
= &parent
->tree
->object
.oid
;
1222 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1225 return !oidcmp(ptree_oid
, &commit
->tree
->object
.oid
);
1229 * Do we run "git commit" with "--allow-empty"?
1231 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
1233 int index_unchanged
, empty_commit
;
1238 * (1) we do not allow empty at all and error out.
1240 * (2) we allow ones that were initially empty, but
1241 * forbid the ones that become empty;
1243 * (3) we allow both.
1245 if (!opts
->allow_empty
)
1246 return 0; /* let "git commit" barf as necessary */
1248 index_unchanged
= is_index_unchanged();
1249 if (index_unchanged
< 0)
1250 return index_unchanged
;
1251 if (!index_unchanged
)
1252 return 0; /* we do not have to say --allow-empty */
1254 if (opts
->keep_redundant_commits
)
1257 empty_commit
= is_original_commit_empty(commit
);
1258 if (empty_commit
< 0)
1259 return empty_commit
;
1267 * Note that ordering matters in this enum. Not only must it match the mapping
1268 * below, it is also divided into several sections that matter. When adding
1269 * new commands, make sure you add it in the right section.
1272 /* commands that handle commits */
1279 /* commands that do something else than handling a single commit */
1281 /* commands that do nothing but are counted for reporting progress */
1284 /* comments (not counted for reporting progress) */
1291 } todo_command_info
[] = {
1304 static const char *command_to_string(const enum todo_command command
)
1306 if (command
< TODO_COMMENT
)
1307 return todo_command_info
[command
].str
;
1308 die("Unknown command: %d", command
);
1311 static char command_to_char(const enum todo_command command
)
1313 if (command
< TODO_COMMENT
&& todo_command_info
[command
].c
)
1314 return todo_command_info
[command
].c
;
1315 return comment_line_char
;
1318 static int is_noop(const enum todo_command command
)
1320 return TODO_NOOP
<= command
;
1323 static int is_fixup(enum todo_command command
)
1325 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1328 static int update_squash_messages(enum todo_command command
,
1329 struct commit
*commit
, struct replay_opts
*opts
)
1331 struct strbuf buf
= STRBUF_INIT
;
1333 const char *message
, *body
;
1335 if (file_exists(rebase_path_squash_msg())) {
1336 struct strbuf header
= STRBUF_INIT
;
1339 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 2048) <= 0)
1340 return error(_("could not read '%s'"),
1341 rebase_path_squash_msg());
1344 eol
= strchrnul(buf
.buf
, '\n');
1345 if (buf
.buf
[0] != comment_line_char
||
1346 (p
+= strcspn(p
, "0123456789\n")) == eol
)
1347 return error(_("unexpected 1st line of squash message:"
1349 (int)(eol
- buf
.buf
), buf
.buf
);
1350 count
= strtol(p
, NULL
, 10);
1353 return error(_("invalid 1st line of squash message:\n"
1355 (int)(eol
- buf
.buf
), buf
.buf
);
1357 strbuf_addf(&header
, "%c ", comment_line_char
);
1358 strbuf_addf(&header
,
1359 _("This is a combination of %d commits."), ++count
);
1360 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1361 strbuf_release(&header
);
1363 struct object_id head
;
1364 struct commit
*head_commit
;
1365 const char *head_message
, *body
;
1367 if (get_oid("HEAD", &head
))
1368 return error(_("need a HEAD to fixup"));
1369 if (!(head_commit
= lookup_commit_reference(&head
)))
1370 return error(_("could not read HEAD"));
1371 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
1372 return error(_("could not read HEAD's commit message"));
1374 find_commit_subject(head_message
, &body
);
1375 if (write_message(body
, strlen(body
),
1376 rebase_path_fixup_msg(), 0)) {
1377 unuse_commit_buffer(head_commit
, head_message
);
1378 return error(_("cannot write '%s'"),
1379 rebase_path_fixup_msg());
1383 strbuf_addf(&buf
, "%c ", comment_line_char
);
1384 strbuf_addf(&buf
, _("This is a combination of %d commits."),
1386 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1387 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
1388 strbuf_addstr(&buf
, "\n\n");
1389 strbuf_addstr(&buf
, body
);
1391 unuse_commit_buffer(head_commit
, head_message
);
1394 if (!(message
= get_commit_buffer(commit
, NULL
)))
1395 return error(_("could not read commit message of %s"),
1396 oid_to_hex(&commit
->object
.oid
));
1397 find_commit_subject(message
, &body
);
1399 if (command
== TODO_SQUASH
) {
1400 unlink(rebase_path_fixup_msg());
1401 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1402 strbuf_addf(&buf
, _("This is the commit message #%d:"), count
);
1403 strbuf_addstr(&buf
, "\n\n");
1404 strbuf_addstr(&buf
, body
);
1405 } else if (command
== TODO_FIXUP
) {
1406 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1407 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
1409 strbuf_addstr(&buf
, "\n\n");
1410 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1412 return error(_("unknown command: %d"), command
);
1413 unuse_commit_buffer(commit
, message
);
1415 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
1416 strbuf_release(&buf
);
1420 static void flush_rewritten_pending(void) {
1421 struct strbuf buf
= STRBUF_INIT
;
1422 struct object_id newoid
;
1425 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
1426 !get_oid("HEAD", &newoid
) &&
1427 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
1428 char *bol
= buf
.buf
, *eol
;
1431 eol
= strchrnul(bol
, '\n');
1432 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
1433 bol
, oid_to_hex(&newoid
));
1439 unlink(rebase_path_rewritten_pending());
1441 strbuf_release(&buf
);
1444 static void record_in_rewritten(struct object_id
*oid
,
1445 enum todo_command next_command
) {
1446 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
1451 fprintf(out
, "%s\n", oid_to_hex(oid
));
1454 if (!is_fixup(next_command
))
1455 flush_rewritten_pending();
1458 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
1459 struct replay_opts
*opts
, int final_fixup
)
1461 unsigned int flags
= opts
->edit
? EDIT_MSG
: 0;
1462 const char *msg_file
= opts
->edit
? NULL
: git_path_merge_msg();
1463 struct object_id head
;
1464 struct commit
*base
, *next
, *parent
;
1465 const char *base_label
, *next_label
;
1466 char *author
= NULL
;
1467 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
1468 struct strbuf msgbuf
= STRBUF_INIT
;
1469 int res
, unborn
= 0, allow
;
1471 if (opts
->no_commit
) {
1473 * We do not intend to commit immediately. We just want to
1474 * merge the differences in, so let's compute the tree
1475 * that represents the "current" state for merge-recursive
1478 if (write_cache_as_tree(&head
, 0, NULL
))
1479 return error(_("your index file is unmerged."));
1481 unborn
= get_oid("HEAD", &head
);
1483 oidcpy(&head
, the_hash_algo
->empty_tree
);
1484 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD",
1486 return error_dirty_index(opts
);
1490 if (!commit
->parents
)
1492 else if (commit
->parents
->next
) {
1493 /* Reverting or cherry-picking a merge commit */
1495 struct commit_list
*p
;
1497 if (!opts
->mainline
)
1498 return error(_("commit %s is a merge but no -m option was given."),
1499 oid_to_hex(&commit
->object
.oid
));
1501 for (cnt
= 1, p
= commit
->parents
;
1502 cnt
!= opts
->mainline
&& p
;
1505 if (cnt
!= opts
->mainline
|| !p
)
1506 return error(_("commit %s does not have parent %d"),
1507 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1509 } else if (0 < opts
->mainline
)
1510 return error(_("mainline was specified but commit %s is not a merge."),
1511 oid_to_hex(&commit
->object
.oid
));
1513 parent
= commit
->parents
->item
;
1515 if (get_message(commit
, &msg
) != 0)
1516 return error(_("cannot get commit message for %s"),
1517 oid_to_hex(&commit
->object
.oid
));
1519 if (opts
->allow_ff
&& !is_fixup(command
) &&
1520 ((parent
&& !oidcmp(&parent
->object
.oid
, &head
)) ||
1521 (!parent
&& unborn
))) {
1522 if (is_rebase_i(opts
))
1523 write_author_script(msg
.message
);
1524 res
= fast_forward_to(&commit
->object
.oid
, &head
, unborn
,
1526 if (res
|| command
!= TODO_REWORD
)
1528 flags
|= EDIT_MSG
| AMEND_MSG
| VERIFY_MSG
;
1530 goto fast_forward_edit
;
1532 if (parent
&& parse_commit(parent
) < 0)
1533 /* TRANSLATORS: The first %s will be a "todo" command like
1534 "revert" or "pick", the second %s a SHA1. */
1535 return error(_("%s: cannot parse parent commit %s"),
1536 command_to_string(command
),
1537 oid_to_hex(&parent
->object
.oid
));
1540 * "commit" is an existing commit. We would want to apply
1541 * the difference it introduces since its first parent "prev"
1542 * on top of the current HEAD if we are cherry-pick. Or the
1543 * reverse of it if we are revert.
1546 if (command
== TODO_REVERT
) {
1548 base_label
= msg
.label
;
1550 next_label
= msg
.parent_label
;
1551 strbuf_addstr(&msgbuf
, "Revert \"");
1552 strbuf_addstr(&msgbuf
, msg
.subject
);
1553 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
1554 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1556 if (commit
->parents
&& commit
->parents
->next
) {
1557 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1558 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1560 strbuf_addstr(&msgbuf
, ".\n");
1565 base_label
= msg
.parent_label
;
1567 next_label
= msg
.label
;
1569 /* Append the commit log message to msgbuf. */
1570 if (find_commit_subject(msg
.message
, &p
))
1571 strbuf_addstr(&msgbuf
, p
);
1573 if (opts
->record_origin
) {
1574 strbuf_complete_line(&msgbuf
);
1575 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1576 strbuf_addch(&msgbuf
, '\n');
1577 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1578 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1579 strbuf_addstr(&msgbuf
, ")\n");
1581 if (!is_fixup(command
))
1582 author
= get_author(msg
.message
);
1585 if (command
== TODO_REWORD
)
1586 flags
|= EDIT_MSG
| VERIFY_MSG
;
1587 else if (is_fixup(command
)) {
1588 if (update_squash_messages(command
, commit
, opts
))
1592 msg_file
= rebase_path_squash_msg();
1593 else if (file_exists(rebase_path_fixup_msg())) {
1594 flags
|= CLEANUP_MSG
;
1595 msg_file
= rebase_path_fixup_msg();
1597 const char *dest
= git_path_squash_msg();
1599 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1600 return error(_("could not rename '%s' to '%s'"),
1601 rebase_path_squash_msg(), dest
);
1602 unlink(git_path_merge_msg());
1609 append_signoff(&msgbuf
, 0, 0);
1611 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1613 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1614 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
1615 &head
, &msgbuf
, opts
);
1618 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
1619 git_path_merge_msg(), 0);
1621 struct commit_list
*common
= NULL
;
1622 struct commit_list
*remotes
= NULL
;
1624 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1625 git_path_merge_msg(), 0);
1627 commit_list_insert(base
, &common
);
1628 commit_list_insert(next
, &remotes
);
1629 res
|= try_merge_command(opts
->strategy
,
1630 opts
->xopts_nr
, (const char **)opts
->xopts
,
1631 common
, oid_to_hex(&head
), remotes
);
1632 free_commit_list(common
);
1633 free_commit_list(remotes
);
1635 strbuf_release(&msgbuf
);
1638 * If the merge was clean or if it failed due to conflict, we write
1639 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1640 * However, if the merge did not even start, then we don't want to
1643 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1644 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
1645 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1647 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1648 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
1649 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1653 error(command
== TODO_REVERT
1654 ? _("could not revert %s... %s")
1655 : _("could not apply %s... %s"),
1656 short_commit_name(commit
), msg
.subject
);
1657 print_advice(res
== 1, opts
);
1658 rerere(opts
->allow_rerere_auto
);
1662 allow
= allow_empty(opts
, commit
);
1667 flags
|= ALLOW_EMPTY
;
1668 if (!opts
->no_commit
) {
1670 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
1671 res
= do_commit(msg_file
, author
, opts
, flags
);
1673 res
= error(_("unable to parse commit author"));
1676 if (!res
&& final_fixup
) {
1677 unlink(rebase_path_fixup_msg());
1678 unlink(rebase_path_squash_msg());
1682 free_message(commit
, &msg
);
1684 update_abort_safety_file();
1689 static int prepare_revs(struct replay_opts
*opts
)
1692 * picking (but not reverting) ranges (but not individual revisions)
1693 * should be done in reverse
1695 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1696 opts
->revs
->reverse
^= 1;
1698 if (prepare_revision_walk(opts
->revs
))
1699 return error(_("revision walk setup failed"));
1701 if (!opts
->revs
->commits
)
1702 return error(_("empty commit set passed"));
1706 static int read_and_refresh_cache(struct replay_opts
*opts
)
1708 static struct lock_file index_lock
;
1709 int index_fd
= hold_locked_index(&index_lock
, 0);
1710 if (read_index_preload(&the_index
, NULL
) < 0) {
1711 rollback_lock_file(&index_lock
);
1712 return error(_("git %s: failed to read the index"),
1713 _(action_name(opts
)));
1715 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1716 if (the_index
.cache_changed
&& index_fd
>= 0) {
1717 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
1718 return error(_("git %s: failed to refresh the index"),
1719 _(action_name(opts
)));
1722 rollback_lock_file(&index_lock
);
1727 enum todo_command command
;
1728 struct commit
*commit
;
1731 size_t offset_in_buf
;
1736 struct todo_item
*items
;
1737 int nr
, alloc
, current
;
1738 int done_nr
, total_nr
;
1739 struct stat_data stat
;
1742 #define TODO_LIST_INIT { STRBUF_INIT }
1744 static void todo_list_release(struct todo_list
*todo_list
)
1746 strbuf_release(&todo_list
->buf
);
1747 FREE_AND_NULL(todo_list
->items
);
1748 todo_list
->nr
= todo_list
->alloc
= 0;
1751 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1753 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1754 return todo_list
->items
+ todo_list
->nr
++;
1757 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1759 struct object_id commit_oid
;
1760 char *end_of_object_name
;
1761 int i
, saved
, status
, padding
;
1764 bol
+= strspn(bol
, " \t");
1766 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1767 item
->command
= TODO_COMMENT
;
1768 item
->commit
= NULL
;
1770 item
->arg_len
= eol
- bol
;
1774 for (i
= 0; i
< TODO_COMMENT
; i
++)
1775 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1778 } else if (bol
[1] == ' ' && *bol
== todo_command_info
[i
].c
) {
1783 if (i
>= TODO_COMMENT
)
1786 /* Eat up extra spaces/ tabs before object name */
1787 padding
= strspn(bol
, " \t");
1790 if (item
->command
== TODO_NOOP
) {
1792 return error(_("%s does not accept arguments: '%s'"),
1793 command_to_string(item
->command
), bol
);
1794 item
->commit
= NULL
;
1796 item
->arg_len
= eol
- bol
;
1801 return error(_("missing arguments for %s"),
1802 command_to_string(item
->command
));
1804 if (item
->command
== TODO_EXEC
) {
1805 item
->commit
= NULL
;
1807 item
->arg_len
= (int)(eol
- bol
);
1811 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
1812 saved
= *end_of_object_name
;
1813 *end_of_object_name
= '\0';
1814 status
= get_oid(bol
, &commit_oid
);
1815 *end_of_object_name
= saved
;
1817 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
1818 item
->arg_len
= (int)(eol
- item
->arg
);
1823 item
->commit
= lookup_commit_reference(&commit_oid
);
1824 return !item
->commit
;
1827 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
1829 struct todo_item
*item
;
1830 char *p
= buf
, *next_p
;
1831 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
1833 for (i
= 1; *p
; i
++, p
= next_p
) {
1834 char *eol
= strchrnul(p
, '\n');
1836 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
1838 if (p
!= eol
&& eol
[-1] == '\r')
1839 eol
--; /* strip Carriage Return */
1841 item
= append_new_todo(todo_list
);
1842 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
1843 if (parse_insn_line(item
, p
, eol
)) {
1844 res
= error(_("invalid line %d: %.*s"),
1845 i
, (int)(eol
- p
), p
);
1846 item
->command
= TODO_NOOP
;
1851 else if (is_fixup(item
->command
))
1852 return error(_("cannot '%s' without a previous commit"),
1853 command_to_string(item
->command
));
1854 else if (!is_noop(item
->command
))
1861 static int count_commands(struct todo_list
*todo_list
)
1865 for (i
= 0; i
< todo_list
->nr
; i
++)
1866 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
1872 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
1877 fd
= open(path
, O_RDONLY
);
1879 return error_errno(_("could not open '%s'"), path
);
1880 len
= strbuf_read(sb
, fd
, 0);
1883 return error(_("could not read '%s'."), path
);
1887 static int read_populate_todo(struct todo_list
*todo_list
,
1888 struct replay_opts
*opts
)
1891 const char *todo_file
= get_todo_path(opts
);
1894 strbuf_reset(&todo_list
->buf
);
1895 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
1898 res
= stat(todo_file
, &st
);
1900 return error(_("could not stat '%s'"), todo_file
);
1901 fill_stat_data(&todo_list
->stat
, &st
);
1903 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
1905 if (is_rebase_i(opts
))
1906 return error(_("please fix this using "
1907 "'git rebase --edit-todo'."));
1908 return error(_("unusable instruction sheet: '%s'"), todo_file
);
1911 if (!todo_list
->nr
&&
1912 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
1913 return error(_("no commits parsed."));
1915 if (!is_rebase_i(opts
)) {
1916 enum todo_command valid
=
1917 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
1920 for (i
= 0; i
< todo_list
->nr
; i
++)
1921 if (valid
== todo_list
->items
[i
].command
)
1923 else if (valid
== TODO_PICK
)
1924 return error(_("cannot cherry-pick during a revert."));
1926 return error(_("cannot revert during a cherry-pick."));
1929 if (is_rebase_i(opts
)) {
1930 struct todo_list done
= TODO_LIST_INIT
;
1931 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
1933 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
1934 !parse_insn_buffer(done
.buf
.buf
, &done
))
1935 todo_list
->done_nr
= count_commands(&done
);
1937 todo_list
->done_nr
= 0;
1939 todo_list
->total_nr
= todo_list
->done_nr
1940 + count_commands(todo_list
);
1941 todo_list_release(&done
);
1944 fprintf(f
, "%d\n", todo_list
->total_nr
);
1952 static int git_config_string_dup(char **dest
,
1953 const char *var
, const char *value
)
1956 return config_error_nonbool(var
);
1958 *dest
= xstrdup(value
);
1962 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
1964 struct replay_opts
*opts
= data
;
1969 else if (!strcmp(key
, "options.no-commit"))
1970 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
1971 else if (!strcmp(key
, "options.edit"))
1972 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
1973 else if (!strcmp(key
, "options.signoff"))
1974 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
1975 else if (!strcmp(key
, "options.record-origin"))
1976 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
1977 else if (!strcmp(key
, "options.allow-ff"))
1978 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
1979 else if (!strcmp(key
, "options.mainline"))
1980 opts
->mainline
= git_config_int(key
, value
);
1981 else if (!strcmp(key
, "options.strategy"))
1982 git_config_string_dup(&opts
->strategy
, key
, value
);
1983 else if (!strcmp(key
, "options.gpg-sign"))
1984 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
1985 else if (!strcmp(key
, "options.strategy-option")) {
1986 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
1987 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
1988 } else if (!strcmp(key
, "options.allow-rerere-auto"))
1989 opts
->allow_rerere_auto
=
1990 git_config_bool_or_int(key
, value
, &error_flag
) ?
1991 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
1993 return error(_("invalid key: %s"), key
);
1996 return error(_("invalid value for %s: %s"), key
, value
);
2001 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
2006 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
2008 opts
->strategy
= strbuf_detach(buf
, NULL
);
2009 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
2012 opts
->xopts_nr
= split_cmdline(buf
->buf
, (const char ***)&opts
->xopts
);
2013 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
2014 const char *arg
= opts
->xopts
[i
];
2016 skip_prefix(arg
, "--", &arg
);
2017 opts
->xopts
[i
] = xstrdup(arg
);
2021 static int read_populate_opts(struct replay_opts
*opts
)
2023 if (is_rebase_i(opts
)) {
2024 struct strbuf buf
= STRBUF_INIT
;
2026 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
2027 if (!starts_with(buf
.buf
, "-S"))
2030 free(opts
->gpg_sign
);
2031 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
2036 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(), 1)) {
2037 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
2038 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
2039 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
2040 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
2044 if (file_exists(rebase_path_verbose()))
2047 read_strategy_opts(opts
, &buf
);
2048 strbuf_release(&buf
);
2053 if (!file_exists(git_path_opts_file()))
2056 * The function git_parse_source(), called from git_config_from_file(),
2057 * may die() in case of a syntactically incorrect file. We do not care
2058 * about this case, though, because we wrote that file ourselves, so we
2059 * are pretty certain that it is syntactically correct.
2061 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
2062 return error(_("malformed options sheet: '%s'"),
2063 git_path_opts_file());
2067 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
2068 struct replay_opts
*opts
)
2070 enum todo_command command
= opts
->action
== REPLAY_PICK
?
2071 TODO_PICK
: TODO_REVERT
;
2072 const char *command_string
= todo_command_info
[command
].str
;
2073 struct commit
*commit
;
2075 if (prepare_revs(opts
))
2078 while ((commit
= get_revision(opts
->revs
))) {
2079 struct todo_item
*item
= append_new_todo(todo_list
);
2080 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2081 const char *subject
;
2084 item
->command
= command
;
2085 item
->commit
= commit
;
2088 item
->offset_in_buf
= todo_list
->buf
.len
;
2089 subject_len
= find_commit_subject(commit_buffer
, &subject
);
2090 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
2091 short_commit_name(commit
), subject_len
, subject
);
2092 unuse_commit_buffer(commit
, commit_buffer
);
2097 static int create_seq_dir(void)
2099 if (file_exists(git_path_seq_dir())) {
2100 error(_("a cherry-pick or revert is already in progress"));
2101 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
2103 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
2104 return error_errno(_("could not create sequencer directory '%s'"),
2105 git_path_seq_dir());
2109 static int save_head(const char *head
)
2111 static struct lock_file head_lock
;
2112 struct strbuf buf
= STRBUF_INIT
;
2116 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
2118 rollback_lock_file(&head_lock
);
2119 return error_errno(_("could not lock HEAD"));
2121 strbuf_addf(&buf
, "%s\n", head
);
2122 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
2123 strbuf_release(&buf
);
2125 rollback_lock_file(&head_lock
);
2126 return error_errno(_("could not write to '%s'"),
2127 git_path_head_file());
2129 if (commit_lock_file(&head_lock
) < 0) {
2130 rollback_lock_file(&head_lock
);
2131 return error(_("failed to finalize '%s'."), git_path_head_file());
2136 static int rollback_is_safe(void)
2138 struct strbuf sb
= STRBUF_INIT
;
2139 struct object_id expected_head
, actual_head
;
2141 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
2143 if (get_oid_hex(sb
.buf
, &expected_head
)) {
2144 strbuf_release(&sb
);
2145 die(_("could not parse %s"), git_path_abort_safety_file());
2147 strbuf_release(&sb
);
2149 else if (errno
== ENOENT
)
2150 oidclr(&expected_head
);
2152 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
2154 if (get_oid("HEAD", &actual_head
))
2155 oidclr(&actual_head
);
2157 return !oidcmp(&actual_head
, &expected_head
);
2160 static int reset_for_rollback(const struct object_id
*oid
)
2162 const char *argv
[4]; /* reset --merge <arg> + NULL */
2165 argv
[1] = "--merge";
2166 argv
[2] = oid_to_hex(oid
);
2168 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2171 static int rollback_single_pick(void)
2173 struct object_id head_oid
;
2175 if (!file_exists(git_path_cherry_pick_head()) &&
2176 !file_exists(git_path_revert_head()))
2177 return error(_("no cherry-pick or revert in progress"));
2178 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
2179 return error(_("cannot resolve HEAD"));
2180 if (is_null_oid(&head_oid
))
2181 return error(_("cannot abort from a branch yet to be born"));
2182 return reset_for_rollback(&head_oid
);
2185 int sequencer_rollback(struct replay_opts
*opts
)
2188 struct object_id oid
;
2189 struct strbuf buf
= STRBUF_INIT
;
2192 f
= fopen(git_path_head_file(), "r");
2193 if (!f
&& errno
== ENOENT
) {
2195 * There is no multiple-cherry-pick in progress.
2196 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
2197 * a single-cherry-pick in progress, abort that.
2199 return rollback_single_pick();
2202 return error_errno(_("cannot open '%s'"), git_path_head_file());
2203 if (strbuf_getline_lf(&buf
, f
)) {
2204 error(_("cannot read '%s': %s"), git_path_head_file(),
2205 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
2210 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
2211 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
2212 git_path_head_file());
2215 if (is_null_oid(&oid
)) {
2216 error(_("cannot abort from a branch yet to be born"));
2220 if (!rollback_is_safe()) {
2221 /* Do not error, just do not rollback */
2222 warning(_("You seem to have moved HEAD. "
2223 "Not rewinding, check your HEAD!"));
2225 if (reset_for_rollback(&oid
))
2227 strbuf_release(&buf
);
2228 return sequencer_remove_state(opts
);
2230 strbuf_release(&buf
);
2234 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2236 static struct lock_file todo_lock
;
2237 const char *todo_path
= get_todo_path(opts
);
2238 int next
= todo_list
->current
, offset
, fd
;
2241 * rebase -i writes "git-rebase-todo" without the currently executing
2242 * command, appending it to "done" instead.
2244 if (is_rebase_i(opts
))
2247 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
2249 return error_errno(_("could not lock '%s'"), todo_path
);
2250 offset
= next
< todo_list
->nr
?
2251 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
2252 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
2253 todo_list
->buf
.len
- offset
) < 0)
2254 return error_errno(_("could not write to '%s'"), todo_path
);
2255 if (commit_lock_file(&todo_lock
) < 0)
2256 return error(_("failed to finalize '%s'."), todo_path
);
2258 if (is_rebase_i(opts
)) {
2259 const char *done_path
= rebase_path_done();
2260 int fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
2261 int prev_offset
= !next
? 0 :
2262 todo_list
->items
[next
- 1].offset_in_buf
;
2264 if (fd
>= 0 && offset
> prev_offset
&&
2265 write_in_full(fd
, todo_list
->buf
.buf
+ prev_offset
,
2266 offset
- prev_offset
) < 0) {
2268 return error_errno(_("could not write to '%s'"),
2277 static int save_opts(struct replay_opts
*opts
)
2279 const char *opts_file
= git_path_opts_file();
2282 if (opts
->no_commit
)
2283 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
2285 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
2287 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
2288 if (opts
->record_origin
)
2289 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
2291 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
2292 if (opts
->mainline
) {
2293 struct strbuf buf
= STRBUF_INIT
;
2294 strbuf_addf(&buf
, "%d", opts
->mainline
);
2295 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
2296 strbuf_release(&buf
);
2299 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
2301 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
2304 for (i
= 0; i
< opts
->xopts_nr
; i
++)
2305 res
|= git_config_set_multivar_in_file_gently(opts_file
,
2306 "options.strategy-option",
2307 opts
->xopts
[i
], "^$", 0);
2309 if (opts
->allow_rerere_auto
)
2310 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-rerere-auto",
2311 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
2316 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
2318 struct strbuf buf
= STRBUF_INIT
;
2319 struct rev_info log_tree_opt
;
2320 const char *subject
, *p
;
2323 p
= short_commit_name(commit
);
2324 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
2326 if (update_ref("rebase", "REBASE_HEAD", &commit
->object
.oid
,
2327 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2328 res
|= error(_("could not update %s"), "REBASE_HEAD");
2330 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
2331 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2332 init_revisions(&log_tree_opt
, NULL
);
2333 log_tree_opt
.abbrev
= 0;
2334 log_tree_opt
.diff
= 1;
2335 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
2336 log_tree_opt
.disable_stdin
= 1;
2337 log_tree_opt
.no_commit_id
= 1;
2338 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
2339 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2340 if (!log_tree_opt
.diffopt
.file
)
2341 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
2343 res
|= log_tree_commit(&log_tree_opt
, commit
);
2344 fclose(log_tree_opt
.diffopt
.file
);
2348 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
2349 if (!file_exists(buf
.buf
)) {
2350 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2351 find_commit_subject(commit_buffer
, &subject
);
2352 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
2353 unuse_commit_buffer(commit
, commit_buffer
);
2355 strbuf_release(&buf
);
2360 static int intend_to_amend(void)
2362 struct object_id head
;
2365 if (get_oid("HEAD", &head
))
2366 return error(_("cannot read HEAD"));
2368 p
= oid_to_hex(&head
);
2369 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
2372 static int error_with_patch(struct commit
*commit
,
2373 const char *subject
, int subject_len
,
2374 struct replay_opts
*opts
, int exit_code
, int to_amend
)
2376 if (make_patch(commit
, opts
))
2380 if (intend_to_amend())
2383 fprintf(stderr
, "You can amend the commit now, with\n"
2385 " git commit --amend %s\n"
2387 "Once you are satisfied with your changes, run\n"
2389 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
2390 } else if (exit_code
)
2391 fprintf(stderr
, "Could not apply %s... %.*s\n",
2392 short_commit_name(commit
), subject_len
, subject
);
2397 static int error_failed_squash(struct commit
*commit
,
2398 struct replay_opts
*opts
, int subject_len
, const char *subject
)
2400 if (rename(rebase_path_squash_msg(), rebase_path_message()))
2401 return error(_("could not rename '%s' to '%s'"),
2402 rebase_path_squash_msg(), rebase_path_message());
2403 unlink(rebase_path_fixup_msg());
2404 unlink(git_path_merge_msg());
2405 if (copy_file(git_path_merge_msg(), rebase_path_message(), 0666))
2406 return error(_("could not copy '%s' to '%s'"),
2407 rebase_path_message(), git_path_merge_msg());
2408 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
2411 static int do_exec(const char *command_line
)
2413 struct argv_array child_env
= ARGV_ARRAY_INIT
;
2414 const char *child_argv
[] = { NULL
, NULL
};
2417 fprintf(stderr
, "Executing: %s\n", command_line
);
2418 child_argv
[0] = command_line
;
2419 argv_array_pushf(&child_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
2420 status
= run_command_v_opt_cd_env(child_argv
, RUN_USING_SHELL
, NULL
,
2423 /* force re-reading of the cache */
2424 if (discard_cache() < 0 || read_cache() < 0)
2425 return error(_("could not read index"));
2427 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
2430 warning(_("execution failed: %s\n%s"
2431 "You can fix the problem, and then run\n"
2433 " git rebase --continue\n"
2436 dirty
? N_("and made changes to the index and/or the "
2437 "working tree\n") : "");
2439 /* command not found */
2442 warning(_("execution succeeded: %s\nbut "
2443 "left changes to the index and/or the working tree\n"
2444 "Commit or stash your changes, and then run\n"
2446 " git rebase --continue\n"
2447 "\n"), command_line
);
2451 argv_array_clear(&child_env
);
2456 static int is_final_fixup(struct todo_list
*todo_list
)
2458 int i
= todo_list
->current
;
2460 if (!is_fixup(todo_list
->items
[i
].command
))
2463 while (++i
< todo_list
->nr
)
2464 if (is_fixup(todo_list
->items
[i
].command
))
2466 else if (!is_noop(todo_list
->items
[i
].command
))
2471 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
2475 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
2476 if (!is_noop(todo_list
->items
[i
].command
))
2477 return todo_list
->items
[i
].command
;
2482 static int apply_autostash(struct replay_opts
*opts
)
2484 struct strbuf stash_sha1
= STRBUF_INIT
;
2485 struct child_process child
= CHILD_PROCESS_INIT
;
2488 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
2489 strbuf_release(&stash_sha1
);
2492 strbuf_trim(&stash_sha1
);
2495 child
.no_stdout
= 1;
2496 child
.no_stderr
= 1;
2497 argv_array_push(&child
.args
, "stash");
2498 argv_array_push(&child
.args
, "apply");
2499 argv_array_push(&child
.args
, stash_sha1
.buf
);
2500 if (!run_command(&child
))
2501 fprintf(stderr
, _("Applied autostash.\n"));
2503 struct child_process store
= CHILD_PROCESS_INIT
;
2506 argv_array_push(&store
.args
, "stash");
2507 argv_array_push(&store
.args
, "store");
2508 argv_array_push(&store
.args
, "-m");
2509 argv_array_push(&store
.args
, "autostash");
2510 argv_array_push(&store
.args
, "-q");
2511 argv_array_push(&store
.args
, stash_sha1
.buf
);
2512 if (run_command(&store
))
2513 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
2516 _("Applying autostash resulted in conflicts.\n"
2517 "Your changes are safe in the stash.\n"
2518 "You can run \"git stash pop\" or"
2519 " \"git stash drop\" at any time.\n"));
2522 strbuf_release(&stash_sha1
);
2526 static const char *reflog_message(struct replay_opts
*opts
,
2527 const char *sub_action
, const char *fmt
, ...)
2530 static struct strbuf buf
= STRBUF_INIT
;
2534 strbuf_addstr(&buf
, action_name(opts
));
2536 strbuf_addf(&buf
, " (%s)", sub_action
);
2538 strbuf_addstr(&buf
, ": ");
2539 strbuf_vaddf(&buf
, fmt
, ap
);
2546 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2550 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
2552 assert(!(opts
->signoff
|| opts
->no_commit
||
2553 opts
->record_origin
|| opts
->edit
));
2554 if (read_and_refresh_cache(opts
))
2557 while (todo_list
->current
< todo_list
->nr
) {
2558 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
2559 if (save_todo(todo_list
, opts
))
2561 if (is_rebase_i(opts
)) {
2562 if (item
->command
!= TODO_COMMENT
) {
2563 FILE *f
= fopen(rebase_path_msgnum(), "w");
2565 todo_list
->done_nr
++;
2568 fprintf(f
, "%d\n", todo_list
->done_nr
);
2571 fprintf(stderr
, "Rebasing (%d/%d)%s",
2573 todo_list
->total_nr
,
2574 opts
->verbose
? "\n" : "\r");
2576 unlink(rebase_path_message());
2577 unlink(rebase_path_author_script());
2578 unlink(rebase_path_stopped_sha());
2579 unlink(rebase_path_amend());
2580 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
2582 if (item
->command
<= TODO_SQUASH
) {
2583 if (is_rebase_i(opts
))
2584 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
2585 command_to_string(item
->command
), NULL
),
2587 res
= do_pick_commit(item
->command
, item
->commit
,
2588 opts
, is_final_fixup(todo_list
));
2589 if (is_rebase_i(opts
) && res
< 0) {
2591 todo_list
->current
--;
2592 if (save_todo(todo_list
, opts
))
2595 if (item
->command
== TODO_EDIT
) {
2596 struct commit
*commit
= item
->commit
;
2599 _("Stopped at %s... %.*s\n"),
2600 short_commit_name(commit
),
2601 item
->arg_len
, item
->arg
);
2602 return error_with_patch(commit
,
2603 item
->arg
, item
->arg_len
, opts
, res
,
2606 if (is_rebase_i(opts
) && !res
)
2607 record_in_rewritten(&item
->commit
->object
.oid
,
2608 peek_command(todo_list
, 1));
2609 if (res
&& is_fixup(item
->command
)) {
2612 return error_failed_squash(item
->commit
, opts
,
2613 item
->arg_len
, item
->arg
);
2614 } else if (res
&& is_rebase_i(opts
))
2615 return res
| error_with_patch(item
->commit
,
2616 item
->arg
, item
->arg_len
, opts
, res
,
2617 item
->command
== TODO_REWORD
);
2618 } else if (item
->command
== TODO_EXEC
) {
2619 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
2620 int saved
= *end_of_arg
;
2624 res
= do_exec(item
->arg
);
2625 *end_of_arg
= saved
;
2627 /* Reread the todo file if it has changed. */
2629 ; /* fall through */
2630 else if (stat(get_todo_path(opts
), &st
))
2631 res
= error_errno(_("could not stat '%s'"),
2632 get_todo_path(opts
));
2633 else if (match_stat_data(&todo_list
->stat
, &st
)) {
2634 todo_list_release(todo_list
);
2635 if (read_populate_todo(todo_list
, opts
))
2636 res
= -1; /* message was printed */
2637 /* `current` will be incremented below */
2638 todo_list
->current
= -1;
2640 } else if (!is_noop(item
->command
))
2641 return error(_("unknown command %d"), item
->command
);
2643 todo_list
->current
++;
2648 if (is_rebase_i(opts
)) {
2649 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
2652 /* Stopped in the middle, as planned? */
2653 if (todo_list
->current
< todo_list
->nr
)
2656 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
2657 starts_with(head_ref
.buf
, "refs/")) {
2659 struct object_id head
, orig
;
2662 if (get_oid("HEAD", &head
)) {
2663 res
= error(_("cannot read HEAD"));
2665 strbuf_release(&head_ref
);
2666 strbuf_release(&buf
);
2669 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
2670 get_oid_hex(buf
.buf
, &orig
)) {
2671 res
= error(_("could not read orig-head"));
2672 goto cleanup_head_ref
;
2675 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
2676 res
= error(_("could not read 'onto'"));
2677 goto cleanup_head_ref
;
2679 msg
= reflog_message(opts
, "finish", "%s onto %s",
2680 head_ref
.buf
, buf
.buf
);
2681 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
2682 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
2683 res
= error(_("could not update %s"),
2685 goto cleanup_head_ref
;
2687 msg
= reflog_message(opts
, "finish", "returning to %s",
2689 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
2690 res
= error(_("could not update HEAD to %s"),
2692 goto cleanup_head_ref
;
2697 if (opts
->verbose
) {
2698 struct rev_info log_tree_opt
;
2699 struct object_id orig
, head
;
2701 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2702 init_revisions(&log_tree_opt
, NULL
);
2703 log_tree_opt
.diff
= 1;
2704 log_tree_opt
.diffopt
.output_format
=
2705 DIFF_FORMAT_DIFFSTAT
;
2706 log_tree_opt
.disable_stdin
= 1;
2708 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
2709 !get_oid(buf
.buf
, &orig
) &&
2710 !get_oid("HEAD", &head
)) {
2711 diff_tree_oid(&orig
, &head
, "",
2712 &log_tree_opt
.diffopt
);
2713 log_tree_diff_flush(&log_tree_opt
);
2716 flush_rewritten_pending();
2717 if (!stat(rebase_path_rewritten_list(), &st
) &&
2719 struct child_process child
= CHILD_PROCESS_INIT
;
2720 const char *post_rewrite_hook
=
2721 find_hook("post-rewrite");
2723 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
2725 argv_array_push(&child
.args
, "notes");
2726 argv_array_push(&child
.args
, "copy");
2727 argv_array_push(&child
.args
, "--for-rewrite=rebase");
2728 /* we don't care if this copying failed */
2729 run_command(&child
);
2731 if (post_rewrite_hook
) {
2732 struct child_process hook
= CHILD_PROCESS_INIT
;
2734 hook
.in
= open(rebase_path_rewritten_list(),
2736 hook
.stdout_to_stderr
= 1;
2737 argv_array_push(&hook
.args
, post_rewrite_hook
);
2738 argv_array_push(&hook
.args
, "rebase");
2739 /* we don't care if this hook failed */
2743 apply_autostash(opts
);
2745 fprintf(stderr
, "Successfully rebased and updated %s.\n",
2748 strbuf_release(&buf
);
2749 strbuf_release(&head_ref
);
2753 * Sequence of picks finished successfully; cleanup by
2754 * removing the .git/sequencer directory
2756 return sequencer_remove_state(opts
);
2759 static int continue_single_pick(void)
2761 const char *argv
[] = { "commit", NULL
};
2763 if (!file_exists(git_path_cherry_pick_head()) &&
2764 !file_exists(git_path_revert_head()))
2765 return error(_("no cherry-pick or revert in progress"));
2766 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2769 static int commit_staged_changes(struct replay_opts
*opts
)
2771 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
2773 if (has_unstaged_changes(1))
2774 return error(_("cannot rebase: You have unstaged changes."));
2775 if (!has_uncommitted_changes(0)) {
2776 const char *cherry_pick_head
= git_path_cherry_pick_head();
2778 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
2779 return error(_("could not remove CHERRY_PICK_HEAD"));
2783 if (file_exists(rebase_path_amend())) {
2784 struct strbuf rev
= STRBUF_INIT
;
2785 struct object_id head
, to_amend
;
2787 if (get_oid("HEAD", &head
))
2788 return error(_("cannot amend non-existing commit"));
2789 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
2790 return error(_("invalid file: '%s'"), rebase_path_amend());
2791 if (get_oid_hex(rev
.buf
, &to_amend
))
2792 return error(_("invalid contents: '%s'"),
2793 rebase_path_amend());
2794 if (oidcmp(&head
, &to_amend
))
2795 return error(_("\nYou have uncommitted changes in your "
2796 "working tree. Please, commit them\n"
2797 "first and then run 'git rebase "
2798 "--continue' again."));
2800 strbuf_release(&rev
);
2804 if (run_git_commit(rebase_path_message(), opts
, flags
))
2805 return error(_("could not commit staged changes."));
2806 unlink(rebase_path_amend());
2810 int sequencer_continue(struct replay_opts
*opts
)
2812 struct todo_list todo_list
= TODO_LIST_INIT
;
2815 if (read_and_refresh_cache(opts
))
2818 if (is_rebase_i(opts
)) {
2819 if (commit_staged_changes(opts
))
2821 } else if (!file_exists(get_todo_path(opts
)))
2822 return continue_single_pick();
2823 if (read_populate_opts(opts
))
2825 if ((res
= read_populate_todo(&todo_list
, opts
)))
2826 goto release_todo_list
;
2828 if (!is_rebase_i(opts
)) {
2829 /* Verify that the conflict has been resolved */
2830 if (file_exists(git_path_cherry_pick_head()) ||
2831 file_exists(git_path_revert_head())) {
2832 res
= continue_single_pick();
2834 goto release_todo_list
;
2836 if (index_differs_from("HEAD", NULL
, 0)) {
2837 res
= error_dirty_index(opts
);
2838 goto release_todo_list
;
2840 todo_list
.current
++;
2841 } else if (file_exists(rebase_path_stopped_sha())) {
2842 struct strbuf buf
= STRBUF_INIT
;
2843 struct object_id oid
;
2845 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
2846 !get_oid_committish(buf
.buf
, &oid
))
2847 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
2848 strbuf_release(&buf
);
2851 res
= pick_commits(&todo_list
, opts
);
2853 todo_list_release(&todo_list
);
2857 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
2859 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
2860 return do_pick_commit(opts
->action
== REPLAY_PICK
?
2861 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
2864 int sequencer_pick_revisions(struct replay_opts
*opts
)
2866 struct todo_list todo_list
= TODO_LIST_INIT
;
2867 struct object_id oid
;
2871 if (read_and_refresh_cache(opts
))
2874 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
2875 struct object_id oid
;
2876 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
2878 /* This happens when using --stdin. */
2882 if (!get_oid(name
, &oid
)) {
2883 if (!lookup_commit_reference_gently(&oid
, 1)) {
2884 enum object_type type
= sha1_object_info(oid
.hash
, NULL
);
2885 return error(_("%s: can't cherry-pick a %s"),
2886 name
, type_name(type
));
2889 return error(_("%s: bad revision"), name
);
2893 * If we were called as "git cherry-pick <commit>", just
2894 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
2895 * REVERT_HEAD, and don't touch the sequencer state.
2896 * This means it is possible to cherry-pick in the middle
2897 * of a cherry-pick sequence.
2899 if (opts
->revs
->cmdline
.nr
== 1 &&
2900 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
2901 opts
->revs
->no_walk
&&
2902 !opts
->revs
->cmdline
.rev
->flags
) {
2903 struct commit
*cmit
;
2904 if (prepare_revision_walk(opts
->revs
))
2905 return error(_("revision walk setup failed"));
2906 cmit
= get_revision(opts
->revs
);
2907 if (!cmit
|| get_revision(opts
->revs
))
2908 return error("BUG: expected exactly one commit from walk");
2909 return single_pick(cmit
, opts
);
2913 * Start a new cherry-pick/ revert sequence; but
2914 * first, make sure that an existing one isn't in
2918 if (walk_revs_populate_todo(&todo_list
, opts
) ||
2919 create_seq_dir() < 0)
2921 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
2922 return error(_("can't revert as initial commit"));
2923 if (save_head(oid_to_hex(&oid
)))
2925 if (save_opts(opts
))
2927 update_abort_safety_file();
2928 res
= pick_commits(&todo_list
, opts
);
2929 todo_list_release(&todo_list
);
2933 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
2935 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
2936 struct strbuf sob
= STRBUF_INIT
;
2939 strbuf_addstr(&sob
, sign_off_header
);
2940 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
2941 getenv("GIT_COMMITTER_EMAIL")));
2942 strbuf_addch(&sob
, '\n');
2945 strbuf_complete_line(msgbuf
);
2948 * If the whole message buffer is equal to the sob, pretend that we
2949 * found a conforming footer with a matching sob
2951 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
2952 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
2955 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
2958 const char *append_newlines
= NULL
;
2959 size_t len
= msgbuf
->len
- ignore_footer
;
2963 * The buffer is completely empty. Leave foom for
2964 * the title and body to be filled in by the user.
2966 append_newlines
= "\n\n";
2967 } else if (len
== 1) {
2969 * Buffer contains a single newline. Add another
2970 * so that we leave room for the title and body.
2972 append_newlines
= "\n";
2973 } else if (msgbuf
->buf
[len
- 2] != '\n') {
2975 * Buffer ends with a single newline. Add another
2976 * so that there is an empty line between the message
2979 append_newlines
= "\n";
2980 } /* else, the buffer already ends with two newlines. */
2982 if (append_newlines
)
2983 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2984 append_newlines
, strlen(append_newlines
));
2987 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
2988 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2991 strbuf_release(&sob
);
2994 int sequencer_make_script(FILE *out
, int argc
, const char **argv
,
2997 char *format
= NULL
;
2998 struct pretty_print_context pp
= {0};
2999 struct strbuf buf
= STRBUF_INIT
;
3000 struct rev_info revs
;
3001 struct commit
*commit
;
3002 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
3003 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
3005 init_revisions(&revs
, NULL
);
3006 revs
.verbose_header
= 1;
3007 revs
.max_parents
= 1;
3008 revs
.cherry_pick
= 1;
3011 revs
.right_only
= 1;
3012 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
3013 revs
.topo_order
= 1;
3015 revs
.pretty_given
= 1;
3016 git_config_get_string("rebase.instructionFormat", &format
);
3017 if (!format
|| !*format
) {
3019 format
= xstrdup("%s");
3021 get_commit_format(format
, &revs
);
3023 pp
.fmt
= revs
.commit_format
;
3024 pp
.output_encoding
= get_log_output_encoding();
3026 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
3027 return error(_("make_script: unhandled options"));
3029 if (prepare_revision_walk(&revs
) < 0)
3030 return error(_("make_script: error preparing revisions"));
3032 while ((commit
= get_revision(&revs
))) {
3034 if (!keep_empty
&& is_original_commit_empty(commit
))
3035 strbuf_addf(&buf
, "%c ", comment_line_char
);
3036 strbuf_addf(&buf
, "%s %s ", insn
,
3037 oid_to_hex(&commit
->object
.oid
));
3038 pretty_print_commit(&pp
, commit
, &buf
);
3039 strbuf_addch(&buf
, '\n');
3040 fputs(buf
.buf
, out
);
3042 strbuf_release(&buf
);
3047 * Add commands after pick and (series of) squash/fixup commands
3050 int sequencer_add_exec_commands(const char *commands
)
3052 const char *todo_file
= rebase_path_todo();
3053 struct todo_list todo_list
= TODO_LIST_INIT
;
3054 struct todo_item
*item
;
3055 struct strbuf
*buf
= &todo_list
.buf
;
3056 size_t offset
= 0, commands_len
= strlen(commands
);
3059 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
3060 return error(_("could not read '%s'."), todo_file
);
3062 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
3063 todo_list_release(&todo_list
);
3064 return error(_("unusable todo list: '%s'"), todo_file
);
3068 /* insert <commands> before every pick except the first one */
3069 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
3070 if (item
->command
== TODO_PICK
&& !first
) {
3071 strbuf_insert(buf
, item
->offset_in_buf
+ offset
,
3072 commands
, commands_len
);
3073 offset
+= commands_len
;
3078 /* append final <commands> */
3079 strbuf_add(buf
, commands
, commands_len
);
3081 i
= write_message(buf
->buf
, buf
->len
, todo_file
, 0);
3082 todo_list_release(&todo_list
);
3086 int transform_todos(unsigned flags
)
3088 const char *todo_file
= rebase_path_todo();
3089 struct todo_list todo_list
= TODO_LIST_INIT
;
3090 struct strbuf buf
= STRBUF_INIT
;
3091 struct todo_item
*item
;
3094 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
3095 return error(_("could not read '%s'."), todo_file
);
3097 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
3098 todo_list_release(&todo_list
);
3099 return error(_("unusable todo list: '%s'"), todo_file
);
3102 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
3103 /* if the item is not a command write it and continue */
3104 if (item
->command
>= TODO_COMMENT
) {
3105 strbuf_addf(&buf
, "%.*s\n", item
->arg_len
, item
->arg
);
3109 /* add command to the buffer */
3110 if (flags
& TODO_LIST_ABBREVIATE_CMDS
)
3111 strbuf_addch(&buf
, command_to_char(item
->command
));
3113 strbuf_addstr(&buf
, command_to_string(item
->command
));
3117 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
3118 short_commit_name(item
->commit
) :
3119 oid_to_hex(&item
->commit
->object
.oid
);
3121 strbuf_addf(&buf
, " %s", oid
);
3123 /* add all the rest */
3125 strbuf_addch(&buf
, '\n');
3127 strbuf_addf(&buf
, " %.*s\n", item
->arg_len
, item
->arg
);
3130 i
= write_message(buf
.buf
, buf
.len
, todo_file
, 0);
3131 todo_list_release(&todo_list
);
3136 CHECK_IGNORE
= 0, CHECK_WARN
, CHECK_ERROR
3139 static enum check_level
get_missing_commit_check_level(void)
3143 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
3144 !strcasecmp("ignore", value
))
3145 return CHECK_IGNORE
;
3146 if (!strcasecmp("warn", value
))
3148 if (!strcasecmp("error", value
))
3150 warning(_("unrecognized setting %s for option "
3151 "rebase.missingCommitsCheck. Ignoring."), value
);
3152 return CHECK_IGNORE
;
3156 * Check if the user dropped some commits by mistake
3157 * Behaviour determined by rebase.missingCommitsCheck.
3158 * Check if there is an unrecognized command or a
3159 * bad SHA-1 in a command.
3161 int check_todo_list(void)
3163 enum check_level check_level
= get_missing_commit_check_level();
3164 struct strbuf todo_file
= STRBUF_INIT
;
3165 struct todo_list todo_list
= TODO_LIST_INIT
;
3166 struct strbuf missing
= STRBUF_INIT
;
3167 int advise_to_edit_todo
= 0, res
= 0, i
;
3169 strbuf_addstr(&todo_file
, rebase_path_todo());
3170 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
3174 advise_to_edit_todo
= res
=
3175 parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
3177 if (res
|| check_level
== CHECK_IGNORE
)
3180 /* Mark the commits in git-rebase-todo as seen */
3181 for (i
= 0; i
< todo_list
.nr
; i
++) {
3182 struct commit
*commit
= todo_list
.items
[i
].commit
;
3184 commit
->util
= (void *)1;
3187 todo_list_release(&todo_list
);
3188 strbuf_addstr(&todo_file
, ".backup");
3189 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
3193 strbuf_release(&todo_file
);
3194 res
= !!parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
3196 /* Find commits in git-rebase-todo.backup yet unseen */
3197 for (i
= todo_list
.nr
- 1; i
>= 0; i
--) {
3198 struct todo_item
*item
= todo_list
.items
+ i
;
3199 struct commit
*commit
= item
->commit
;
3200 if (commit
&& !commit
->util
) {
3201 strbuf_addf(&missing
, " - %s %.*s\n",
3202 short_commit_name(commit
),
3203 item
->arg_len
, item
->arg
);
3204 commit
->util
= (void *)1;
3208 /* Warn about missing commits */
3212 if (check_level
== CHECK_ERROR
)
3213 advise_to_edit_todo
= res
= 1;
3216 _("Warning: some commits may have been dropped accidentally.\n"
3217 "Dropped commits (newer to older):\n"));
3219 /* Make the list user-friendly and display */
3220 fputs(missing
.buf
, stderr
);
3221 strbuf_release(&missing
);
3223 fprintf(stderr
, _("To avoid this message, use \"drop\" to "
3224 "explicitly remove a commit.\n\n"
3225 "Use 'git config rebase.missingCommitsCheck' to change "
3226 "the level of warnings.\n"
3227 "The possible behaviours are: ignore, warn, error.\n\n"));
3230 strbuf_release(&todo_file
);
3231 todo_list_release(&todo_list
);
3233 if (advise_to_edit_todo
)
3235 _("You can fix this with 'git rebase --edit-todo' "
3236 "and then run 'git rebase --continue'.\n"
3237 "Or you can abort the rebase with 'git rebase"
3243 static int rewrite_file(const char *path
, const char *buf
, size_t len
)
3246 int fd
= open(path
, O_WRONLY
| O_TRUNC
);
3248 return error_errno(_("could not open '%s' for writing"), path
);
3249 if (write_in_full(fd
, buf
, len
) < 0)
3250 rc
= error_errno(_("could not write to '%s'"), path
);
3251 if (close(fd
) && !rc
)
3252 rc
= error_errno(_("could not close '%s'"), path
);
3256 /* skip picking commits whose parents are unchanged */
3257 int skip_unnecessary_picks(void)
3259 const char *todo_file
= rebase_path_todo();
3260 struct strbuf buf
= STRBUF_INIT
;
3261 struct todo_list todo_list
= TODO_LIST_INIT
;
3262 struct object_id onto_oid
, *oid
= &onto_oid
, *parent_oid
;
3265 if (!read_oneliner(&buf
, rebase_path_onto(), 0))
3266 return error(_("could not read 'onto'"));
3267 if (get_oid(buf
.buf
, &onto_oid
)) {
3268 strbuf_release(&buf
);
3269 return error(_("need a HEAD to fixup"));
3271 strbuf_release(&buf
);
3273 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
3275 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
3276 todo_list_release(&todo_list
);
3280 for (i
= 0; i
< todo_list
.nr
; i
++) {
3281 struct todo_item
*item
= todo_list
.items
+ i
;
3283 if (item
->command
>= TODO_NOOP
)
3285 if (item
->command
!= TODO_PICK
)
3287 if (parse_commit(item
->commit
)) {
3288 todo_list_release(&todo_list
);
3289 return error(_("could not parse commit '%s'"),
3290 oid_to_hex(&item
->commit
->object
.oid
));
3292 if (!item
->commit
->parents
)
3293 break; /* root commit */
3294 if (item
->commit
->parents
->next
)
3295 break; /* merge commit */
3296 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
3297 if (hashcmp(parent_oid
->hash
, oid
->hash
))
3299 oid
= &item
->commit
->object
.oid
;
3302 int offset
= i
< todo_list
.nr
?
3303 todo_list
.items
[i
].offset_in_buf
: todo_list
.buf
.len
;
3304 const char *done_path
= rebase_path_done();
3306 fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
3308 error_errno(_("could not open '%s' for writing"),
3310 todo_list_release(&todo_list
);
3313 if (write_in_full(fd
, todo_list
.buf
.buf
, offset
) < 0) {
3314 error_errno(_("could not write to '%s'"), done_path
);
3315 todo_list_release(&todo_list
);
3321 if (rewrite_file(rebase_path_todo(), todo_list
.buf
.buf
+ offset
,
3322 todo_list
.buf
.len
- offset
) < 0) {
3323 todo_list_release(&todo_list
);
3327 todo_list
.current
= i
;
3328 if (is_fixup(peek_command(&todo_list
, 0)))
3329 record_in_rewritten(oid
, peek_command(&todo_list
, 0));
3332 todo_list_release(&todo_list
);
3333 printf("%s\n", oid_to_hex(oid
));
3338 struct subject2item_entry
{
3339 struct hashmap_entry entry
;
3341 char subject
[FLEX_ARRAY
];
3344 static int subject2item_cmp(const void *fndata
,
3345 const struct subject2item_entry
*a
,
3346 const struct subject2item_entry
*b
, const void *key
)
3348 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
3352 * Rearrange the todo list that has both "pick commit-id msg" and "pick
3353 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
3354 * after the former, and change "pick" to "fixup"/"squash".
3356 * Note that if the config has specified a custom instruction format, each log
3357 * message will have to be retrieved from the commit (as the oneline in the
3358 * script cannot be trusted) in order to normalize the autosquash arrangement.
3360 int rearrange_squash(void)
3362 const char *todo_file
= rebase_path_todo();
3363 struct todo_list todo_list
= TODO_LIST_INIT
;
3364 struct hashmap subject2item
;
3365 int res
= 0, rearranged
= 0, *next
, *tail
, i
;
3368 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
3370 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
3371 todo_list_release(&todo_list
);
3376 * The hashmap maps onelines to the respective todo list index.
3378 * If any items need to be rearranged, the next[i] value will indicate
3379 * which item was moved directly after the i'th.
3381 * In that case, last[i] will indicate the index of the latest item to
3382 * be moved to appear after the i'th.
3384 hashmap_init(&subject2item
, (hashmap_cmp_fn
) subject2item_cmp
,
3385 NULL
, todo_list
.nr
);
3386 ALLOC_ARRAY(next
, todo_list
.nr
);
3387 ALLOC_ARRAY(tail
, todo_list
.nr
);
3388 ALLOC_ARRAY(subjects
, todo_list
.nr
);
3389 for (i
= 0; i
< todo_list
.nr
; i
++) {
3390 struct strbuf buf
= STRBUF_INIT
;
3391 struct todo_item
*item
= todo_list
.items
+ i
;
3392 const char *commit_buffer
, *subject
, *p
;
3395 struct subject2item_entry
*entry
;
3397 next
[i
] = tail
[i
] = -1;
3398 if (item
->command
>= TODO_EXEC
) {
3403 if (is_fixup(item
->command
)) {
3404 todo_list_release(&todo_list
);
3405 return error(_("the script was already rearranged."));
3408 item
->commit
->util
= item
;
3410 parse_commit(item
->commit
);
3411 commit_buffer
= get_commit_buffer(item
->commit
, NULL
);
3412 find_commit_subject(commit_buffer
, &subject
);
3413 format_subject(&buf
, subject
, " ");
3414 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
3415 unuse_commit_buffer(item
->commit
, commit_buffer
);
3416 if ((skip_prefix(subject
, "fixup! ", &p
) ||
3417 skip_prefix(subject
, "squash! ", &p
))) {
3418 struct commit
*commit2
;
3423 if (!skip_prefix(p
, "fixup! ", &p
) &&
3424 !skip_prefix(p
, "squash! ", &p
))
3428 if ((entry
= hashmap_get_from_hash(&subject2item
,
3430 /* found by title */
3432 else if (!strchr(p
, ' ') &&
3434 lookup_commit_reference_by_name(p
)) &&
3436 /* found by commit name */
3437 i2
= (struct todo_item
*)commit2
->util
3440 /* copy can be a prefix of the commit subject */
3441 for (i2
= 0; i2
< i
; i2
++)
3443 starts_with(subjects
[i2
], p
))
3451 todo_list
.items
[i
].command
=
3452 starts_with(subject
, "fixup!") ?
3453 TODO_FIXUP
: TODO_SQUASH
;
3459 } else if (!hashmap_get_from_hash(&subject2item
,
3460 strhash(subject
), subject
)) {
3461 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
3463 hashmap_entry_init(entry
, strhash(entry
->subject
));
3464 hashmap_put(&subject2item
, entry
);
3469 struct strbuf buf
= STRBUF_INIT
;
3471 for (i
= 0; i
< todo_list
.nr
; i
++) {
3472 enum todo_command command
= todo_list
.items
[i
].command
;
3476 * Initially, all commands are 'pick's. If it is a
3477 * fixup or a squash now, we have rearranged it.
3479 if (is_fixup(command
))
3483 int offset
= todo_list
.items
[cur
].offset_in_buf
;
3484 int end_offset
= cur
+ 1 < todo_list
.nr
?
3485 todo_list
.items
[cur
+ 1].offset_in_buf
:
3487 char *bol
= todo_list
.buf
.buf
+ offset
;
3488 char *eol
= todo_list
.buf
.buf
+ end_offset
;
3490 /* replace 'pick', by 'fixup' or 'squash' */
3491 command
= todo_list
.items
[cur
].command
;
3492 if (is_fixup(command
)) {
3494 todo_command_info
[command
].str
);
3495 bol
+= strcspn(bol
, " \t");
3498 strbuf_add(&buf
, bol
, eol
- bol
);
3504 res
= rewrite_file(todo_file
, buf
.buf
, buf
.len
);
3505 strbuf_release(&buf
);
3510 for (i
= 0; i
< todo_list
.nr
; i
++)
3513 hashmap_free(&subject2item
, 1);
3514 todo_list_release(&todo_list
);