1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
8 #include "environment.h"
13 #include "object-file.h"
14 #include "object-name.h"
15 #include "object-store-ll.h"
19 #include "sequencer.h"
20 #include "run-command.h"
23 #include "cache-tree.h"
29 #include "merge-ort.h"
30 #include "merge-ort-wrappers.h"
32 #include "sparse-index.h"
37 #include "wt-status.h"
39 #include "notes-utils.h"
41 #include "unpack-trees.h"
44 #include "commit-slab.h"
46 #include "commit-reach.h"
47 #include "rebase-interactive.h"
51 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
54 * To accommodate common filesystem limitations, where the loose refs' file
55 * names must not exceed `NAME_MAX`, the labels generated by `git rebase
56 * --rebase-merges` need to be truncated if the corresponding commit subjects
58 * Add some margin to stay clear from reaching `NAME_MAX`.
60 #define GIT_MAX_LABEL_LENGTH ((NAME_MAX) - (LOCK_SUFFIX_LEN) - 16)
62 static const char sign_off_header
[] = "Signed-off-by: ";
63 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
65 GIT_PATH_FUNC(git_path_commit_editmsg
, "COMMIT_EDITMSG")
67 static GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
69 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
70 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
71 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
72 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
74 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
76 * The file containing rebase commands, comments, and empty lines.
77 * This file is created by "git rebase -i" then edited by the user. As
78 * the lines are processed, they are removed from the front of this
79 * file and written to the tail of 'done'.
81 GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
82 GIT_PATH_FUNC(rebase_path_todo_backup
, "rebase-merge/git-rebase-todo.backup")
84 GIT_PATH_FUNC(rebase_path_dropped
, "rebase-merge/dropped")
87 * The rebase command lines that have already been processed. A line
88 * is moved here when it is first handled, before any associated user
91 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
93 * The file to keep track of how many commands were already processed (e.g.
96 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum")
98 * The file to keep track of how many commands are to be processed in total
99 * (e.g. for the prompt).
101 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end")
103 * The commit message that is planned to be used for any changes that
104 * need to be committed following a user interaction.
106 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
108 * The file into which is accumulated the suggested commit message for
109 * squash/fixup commands. When the first of a series of squash/fixups
110 * is seen, the file is created and the commit message from the
111 * previous commit and from the first squash/fixup commit are written
112 * to it. The commit message for each subsequent squash/fixup commit
113 * is appended to the file as it is processed.
115 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
117 * If the current series of squash/fixups has not yet included a squash
118 * command, then this file exists and holds the commit message of the
119 * original "pick" commit. (If the series ends without a "squash"
120 * command, then this can be used as the commit message of the combined
121 * commit without opening the editor.)
123 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
125 * This file contains the list fixup/squash commands that have been
126 * accumulated into message-fixup or message-squash so far.
128 static GIT_PATH_FUNC(rebase_path_current_fixups
, "rebase-merge/current-fixups")
130 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
131 * GIT_AUTHOR_DATE that will be used for the commit that is currently
134 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
136 * When an "edit" rebase command is being processed, the SHA1 of the
137 * commit to be edited is recorded in this file. When "git rebase
138 * --continue" is executed, if there are any staged changes then they
139 * will be amended to the HEAD commit, but only provided the HEAD
140 * commit is still the commit to be edited. When any other rebase
141 * command is processed, this file is deleted.
143 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
145 * When we stop at a given patch via the "edit" command, this file contains
146 * the commit object name of the corresponding patch.
148 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
150 * When we stop for the user to resolve conflicts this file contains
151 * the patch of the commit that is being picked.
153 static GIT_PATH_FUNC(rebase_path_patch
, "rebase-merge/patch")
155 * For the post-rewrite hook, we make a list of rewritten commits and
156 * their new sha1s. The rewritten-pending list keeps the sha1s of
157 * commits that have been processed, but not committed yet,
158 * e.g. because they are waiting for a 'squash' command.
160 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
161 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
162 "rebase-merge/rewritten-pending")
165 * The path of the file containing the OID of the "squash onto" commit, i.e.
166 * the dummy commit used for `reset [new root]`.
168 static GIT_PATH_FUNC(rebase_path_squash_onto
, "rebase-merge/squash-onto")
171 * The path of the file listing refs that need to be deleted after the rebase
172 * finishes. This is used by the `label` command to record the need for cleanup.
174 static GIT_PATH_FUNC(rebase_path_refs_to_delete
, "rebase-merge/refs-to-delete")
177 * The update-refs file stores a list of refs that will be updated at the end
178 * of the rebase sequence. The 'update-ref <ref>' commands in the todo file
179 * update the OIDs for the refs in this file, but the refs are not updated
180 * until the end of the rebase sequence.
182 * rebase_path_update_refs() returns the path to this file for a given
183 * worktree directory. For the current worktree, pass the_repository->gitdir.
185 static char *rebase_path_update_refs(const char *wt_git_dir
)
187 return xstrfmt("%s/rebase-merge/update-refs", wt_git_dir
);
191 * The following files are written by git-rebase just after parsing the
194 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
195 static GIT_PATH_FUNC(rebase_path_cdate_is_adate
, "rebase-merge/cdate_is_adate")
196 static GIT_PATH_FUNC(rebase_path_ignore_date
, "rebase-merge/ignore_date")
197 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
198 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
199 static GIT_PATH_FUNC(rebase_path_quiet
, "rebase-merge/quiet")
200 static GIT_PATH_FUNC(rebase_path_signoff
, "rebase-merge/signoff")
201 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
202 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
203 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
204 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
205 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
206 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
207 static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec
, "rebase-merge/reschedule-failed-exec")
208 static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec
, "rebase-merge/no-reschedule-failed-exec")
209 static GIT_PATH_FUNC(rebase_path_drop_redundant_commits
, "rebase-merge/drop_redundant_commits")
210 static GIT_PATH_FUNC(rebase_path_keep_redundant_commits
, "rebase-merge/keep_redundant_commits")
213 * A 'struct replay_ctx' represents the private state of the sequencer.
217 * The commit message that will be used except at the end of a
218 * chain of fixup and squash commands.
220 struct strbuf message
;
222 * The list of completed fixup and squash commands in the
225 struct strbuf current_fixups
;
227 * Stores the reflog message that will be used when creating a
228 * commit. Points to a static buffer and should not be free()'d.
230 const char *reflog_message
;
232 * The number of completed fixup and squash commands in the
235 int current_fixup_count
;
237 * Whether message contains a commit message.
239 unsigned have_message
:1;
242 struct replay_ctx
* replay_ctx_new(void)
244 struct replay_ctx
*ctx
= xcalloc(1, sizeof(*ctx
));
246 strbuf_init(&ctx
->current_fixups
, 0);
247 strbuf_init(&ctx
->message
, 0);
253 * A 'struct update_refs_record' represents a value in the update-refs
254 * list. We use a string_list to map refs to these (before, after) pairs.
256 struct update_ref_record
{
257 struct object_id before
;
258 struct object_id after
;
261 static struct update_ref_record
*init_update_ref_record(const char *ref
)
263 struct update_ref_record
*rec
;
265 CALLOC_ARRAY(rec
, 1);
267 oidcpy(&rec
->before
, null_oid());
268 oidcpy(&rec
->after
, null_oid());
270 /* This may fail, but that's fine, we will keep the null OID. */
271 refs_read_ref(get_main_ref_store(the_repository
), ref
, &rec
->before
);
276 static int git_sequencer_config(const char *k
, const char *v
,
277 const struct config_context
*ctx
, void *cb
)
279 struct replay_opts
*opts
= cb
;
281 if (!strcmp(k
, "commit.cleanup")) {
283 return config_error_nonbool(k
);
285 if (!strcmp(v
, "verbatim")) {
286 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
287 opts
->explicit_cleanup
= 1;
288 } else if (!strcmp(v
, "whitespace")) {
289 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
290 opts
->explicit_cleanup
= 1;
291 } else if (!strcmp(v
, "strip")) {
292 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
293 opts
->explicit_cleanup
= 1;
294 } else if (!strcmp(v
, "scissors")) {
295 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SCISSORS
;
296 opts
->explicit_cleanup
= 1;
298 warning(_("invalid commit message cleanup mode '%s'"),
305 if (!strcmp(k
, "commit.gpgsign")) {
306 free(opts
->gpg_sign
);
307 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
311 if (!opts
->default_strategy
&& !strcmp(k
, "pull.twohead")) {
312 int ret
= git_config_string(&opts
->default_strategy
, k
, v
);
315 * pull.twohead is allowed to be multi-valued; we only
316 * care about the first value.
318 char *tmp
= strchr(opts
->default_strategy
, ' ');
325 if (opts
->action
== REPLAY_REVERT
&& !strcmp(k
, "revert.reference"))
326 opts
->commit_use_reference
= git_config_bool(k
, v
);
328 return git_diff_basic_config(k
, v
, ctx
, NULL
);
331 void sequencer_init_config(struct replay_opts
*opts
)
333 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
334 git_config(git_sequencer_config
, opts
);
337 static inline int is_rebase_i(const struct replay_opts
*opts
)
339 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
342 static const char *get_dir(const struct replay_opts
*opts
)
344 if (is_rebase_i(opts
))
345 return rebase_path();
346 return git_path_seq_dir();
349 static const char *get_todo_path(const struct replay_opts
*opts
)
351 if (is_rebase_i(opts
))
352 return rebase_path_todo();
353 return git_path_todo_file();
357 * Returns 0 for non-conforming footer
358 * Returns 1 for conforming footer
359 * Returns 2 when sob exists within conforming footer
360 * Returns 3 when sob exists within conforming footer as last entry
362 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
363 size_t ignore_footer
)
365 struct trailer_iterator iter
;
367 int found_sob
= 0, found_sob_last
= 0;
371 saved_char
= sb
->buf
[sb
->len
- ignore_footer
];
372 sb
->buf
[sb
->len
- ignore_footer
] = '\0';
375 trailer_iterator_init(&iter
, sb
->buf
);
378 sb
->buf
[sb
->len
- ignore_footer
] = saved_char
;
380 while (trailer_iterator_advance(&iter
)) {
382 if (sob
&& !strncmp(iter
.raw
, sob
->buf
, sob
->len
))
385 trailer_iterator_release(&iter
);
390 found_sob_last
= (int)i
== found_sob
;
399 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
401 static struct strbuf buf
= STRBUF_INIT
;
405 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
409 static void replay_ctx_release(struct replay_ctx
*ctx
)
411 strbuf_release(&ctx
->current_fixups
);
412 strbuf_release(&ctx
->message
);
415 void replay_opts_release(struct replay_opts
*opts
)
417 struct replay_ctx
*ctx
= opts
->ctx
;
419 free(opts
->gpg_sign
);
420 free(opts
->reflog_action
);
421 free(opts
->default_strategy
);
422 free(opts
->strategy
);
423 strvec_clear (&opts
->xopts
);
425 release_revisions(opts
->revs
);
427 replay_ctx_release(ctx
);
431 int sequencer_remove_state(struct replay_opts
*opts
)
433 struct strbuf buf
= STRBUF_INIT
;
436 if (is_rebase_i(opts
) &&
437 strbuf_read_file(&buf
, rebase_path_refs_to_delete(), 0) > 0) {
440 char *eol
= strchr(p
, '\n');
443 if (refs_delete_ref(get_main_ref_store(the_repository
), "(rebase) cleanup", p
, NULL
, 0) < 0) {
444 warning(_("could not delete '%s'"), p
);
454 strbuf_addstr(&buf
, get_dir(opts
));
455 if (remove_dir_recursively(&buf
, 0))
456 ret
= error(_("could not remove '%s'"), buf
.buf
);
457 strbuf_release(&buf
);
462 static const char *action_name(const struct replay_opts
*opts
)
464 switch (opts
->action
) {
468 return N_("cherry-pick");
469 case REPLAY_INTERACTIVE_REBASE
:
472 die(_("unknown action: %d"), opts
->action
);
475 struct commit_message
{
482 static const char *short_commit_name(struct repository
*r
, struct commit
*commit
)
484 return repo_find_unique_abbrev(r
, &commit
->object
.oid
, DEFAULT_ABBREV
);
487 static int get_message(struct commit
*commit
, struct commit_message
*out
)
489 const char *abbrev
, *subject
;
492 out
->message
= repo_logmsg_reencode(the_repository
, commit
, NULL
,
493 get_commit_output_encoding());
494 abbrev
= short_commit_name(the_repository
, commit
);
496 subject_len
= find_commit_subject(out
->message
, &subject
);
498 out
->subject
= xmemdupz(subject
, subject_len
);
499 out
->label
= xstrfmt("%s (%s)", abbrev
, out
->subject
);
500 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
505 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
507 free(msg
->parent_label
);
510 repo_unuse_commit_buffer(the_repository
, commit
, msg
->message
);
513 const char *rebase_resolvemsg
=
514 N_("Resolve all conflicts manually, mark them as resolved with\n"
515 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
516 "You can instead skip this commit: run \"git rebase --skip\".\n"
517 "To abort and get back to the state before \"git rebase\", run "
518 "\"git rebase --abort\".");
520 static void print_advice(struct repository
*r
, int show_hint
,
521 struct replay_opts
*opts
)
525 if (is_rebase_i(opts
))
526 msg
= rebase_resolvemsg
;
528 msg
= getenv("GIT_CHERRY_PICK_HELP");
531 advise_if_enabled(ADVICE_MERGE_CONFLICT
, "%s", msg
);
533 * A conflict has occurred but the porcelain
534 * (typically rebase --interactive) wants to take care
535 * of the commit itself so remove CHERRY_PICK_HEAD
537 refs_delete_ref(get_main_ref_store(r
), "", "CHERRY_PICK_HEAD",
544 advise_if_enabled(ADVICE_MERGE_CONFLICT
,
545 _("after resolving the conflicts, mark the corrected paths\n"
546 "with 'git add <paths>' or 'git rm <paths>'"));
547 else if (opts
->action
== REPLAY_PICK
)
548 advise_if_enabled(ADVICE_MERGE_CONFLICT
,
549 _("After resolving the conflicts, mark them with\n"
550 "\"git add/rm <pathspec>\", then run\n"
551 "\"git cherry-pick --continue\".\n"
552 "You can instead skip this commit with \"git cherry-pick --skip\".\n"
553 "To abort and get back to the state before \"git cherry-pick\",\n"
554 "run \"git cherry-pick --abort\"."));
555 else if (opts
->action
== REPLAY_REVERT
)
556 advise_if_enabled(ADVICE_MERGE_CONFLICT
,
557 _("After resolving the conflicts, mark them with\n"
558 "\"git add/rm <pathspec>\", then run\n"
559 "\"git revert --continue\".\n"
560 "You can instead skip this commit with \"git revert --skip\".\n"
561 "To abort and get back to the state before \"git revert\",\n"
562 "run \"git revert --abort\"."));
564 BUG("unexpected pick action in print_advice()");
568 static int write_message(const void *buf
, size_t len
, const char *filename
,
571 struct lock_file msg_file
= LOCK_INIT
;
573 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
575 return error_errno(_("could not lock '%s'"), filename
);
576 if (write_in_full(msg_fd
, buf
, len
) < 0) {
577 error_errno(_("could not write to '%s'"), filename
);
578 rollback_lock_file(&msg_file
);
581 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
582 error_errno(_("could not write eol to '%s'"), filename
);
583 rollback_lock_file(&msg_file
);
586 if (commit_lock_file(&msg_file
) < 0)
587 return error(_("failed to finalize '%s'"), filename
);
592 int read_oneliner(struct strbuf
*buf
,
593 const char *path
, unsigned flags
)
595 int orig_len
= buf
->len
;
597 if (strbuf_read_file(buf
, path
, 0) < 0) {
598 if ((flags
& READ_ONELINER_WARN_MISSING
) ||
599 (errno
!= ENOENT
&& errno
!= ENOTDIR
))
600 warning_errno(_("could not read '%s'"), path
);
604 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
605 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
607 buf
->buf
[buf
->len
] = '\0';
610 if ((flags
& READ_ONELINER_SKIP_IF_EMPTY
) && buf
->len
== orig_len
)
616 static struct tree
*empty_tree(struct repository
*r
)
618 return lookup_tree(r
, the_hash_algo
->empty_tree
);
621 static int error_dirty_index(struct repository
*repo
, struct replay_opts
*opts
)
623 if (repo_read_index_unmerged(repo
))
624 return error_resolve_conflict(action_name(opts
));
626 error(_("your local changes would be overwritten by %s."),
627 _(action_name(opts
)));
629 if (advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
))
630 advise(_("commit your changes or stash them to proceed."));
634 static void update_abort_safety_file(void)
636 struct object_id head
;
638 /* Do nothing on a single-pick */
639 if (!file_exists(git_path_seq_dir()))
642 if (!repo_get_oid(the_repository
, "HEAD", &head
))
643 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
645 write_file(git_path_abort_safety_file(), "%s", "");
648 static int fast_forward_to(struct repository
*r
,
649 const struct object_id
*to
,
650 const struct object_id
*from
,
652 struct replay_opts
*opts
)
654 struct ref_transaction
*transaction
;
655 struct strbuf sb
= STRBUF_INIT
;
656 struct strbuf err
= STRBUF_INIT
;
659 if (checkout_fast_forward(r
, from
, to
, 1))
660 return -1; /* the callee should have complained already */
662 strbuf_addf(&sb
, "%s: fast-forward", action_name(opts
));
664 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
667 ref_transaction_update(transaction
, "HEAD",
668 to
, unborn
&& !is_rebase_i(opts
) ?
669 null_oid() : from
, NULL
, NULL
,
671 ref_transaction_commit(transaction
, &err
)) {
672 ref_transaction_free(transaction
);
673 error("%s", err
.buf
);
675 strbuf_release(&err
);
680 strbuf_release(&err
);
681 ref_transaction_free(transaction
);
682 update_abort_safety_file();
686 enum commit_msg_cleanup_mode
get_cleanup_mode(const char *cleanup_arg
,
689 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
690 return use_editor
? COMMIT_MSG_CLEANUP_ALL
:
691 COMMIT_MSG_CLEANUP_SPACE
;
692 else if (!strcmp(cleanup_arg
, "verbatim"))
693 return COMMIT_MSG_CLEANUP_NONE
;
694 else if (!strcmp(cleanup_arg
, "whitespace"))
695 return COMMIT_MSG_CLEANUP_SPACE
;
696 else if (!strcmp(cleanup_arg
, "strip"))
697 return COMMIT_MSG_CLEANUP_ALL
;
698 else if (!strcmp(cleanup_arg
, "scissors"))
699 return use_editor
? COMMIT_MSG_CLEANUP_SCISSORS
:
700 COMMIT_MSG_CLEANUP_SPACE
;
702 die(_("Invalid cleanup mode %s"), cleanup_arg
);
706 * NB using int rather than enum cleanup_mode to stop clang's
707 * -Wtautological-constant-out-of-range-compare complaining that the comparison
710 static const char *describe_cleanup_mode(int cleanup_mode
)
712 static const char *modes
[] = { "whitespace",
717 if (cleanup_mode
< ARRAY_SIZE(modes
))
718 return modes
[cleanup_mode
];
720 BUG("invalid cleanup_mode provided (%d)", cleanup_mode
);
723 void append_conflicts_hint(struct index_state
*istate
,
724 struct strbuf
*msgbuf
, enum commit_msg_cleanup_mode cleanup_mode
)
728 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
729 strbuf_addch(msgbuf
, '\n');
730 wt_status_append_cut_line(msgbuf
);
731 strbuf_addstr(msgbuf
, comment_line_str
);
734 strbuf_addch(msgbuf
, '\n');
735 strbuf_commented_addf(msgbuf
, comment_line_str
, "Conflicts:\n");
736 for (i
= 0; i
< istate
->cache_nr
;) {
737 const struct cache_entry
*ce
= istate
->cache
[i
++];
739 strbuf_commented_addf(msgbuf
, comment_line_str
,
741 while (i
< istate
->cache_nr
&&
742 !strcmp(ce
->name
, istate
->cache
[i
]->name
))
748 static int do_recursive_merge(struct repository
*r
,
749 struct commit
*base
, struct commit
*next
,
750 const char *base_label
, const char *next_label
,
751 struct object_id
*head
, struct strbuf
*msgbuf
,
752 struct replay_opts
*opts
)
754 struct merge_options o
;
755 struct merge_result result
;
756 struct tree
*next_tree
, *base_tree
, *head_tree
;
757 int clean
, show_output
;
759 struct lock_file index_lock
= LOCK_INIT
;
761 if (repo_hold_locked_index(r
, &index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
766 init_ui_merge_options(&o
, r
);
767 o
.ancestor
= base
? base_label
: "(empty tree)";
769 o
.branch2
= next
? next_label
: "(empty tree)";
770 if (is_rebase_i(opts
))
772 o
.show_rename_progress
= 1;
774 head_tree
= parse_tree_indirect(head
);
776 return error(_("unable to read tree (%s)"), oid_to_hex(head
));
777 next_tree
= next
? repo_get_commit_tree(r
, next
) : empty_tree(r
);
778 base_tree
= base
? repo_get_commit_tree(r
, base
) : empty_tree(r
);
780 for (i
= 0; i
< opts
->xopts
.nr
; i
++)
781 parse_merge_opt(&o
, opts
->xopts
.v
[i
]);
783 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "ort")) {
784 memset(&result
, 0, sizeof(result
));
785 merge_incore_nonrecursive(&o
, base_tree
, head_tree
, next_tree
,
787 show_output
= !is_rebase_i(opts
) || !result
.clean
;
789 * TODO: merge_switch_to_result will update index/working tree;
790 * we only really want to do that if !result.clean || this is
791 * the final patch to be picked. But determining this is the
792 * final patch would take some work, and "head_tree" would need
793 * to be replace with the tree the index matched before we
794 * started doing any picks.
796 merge_switch_to_result(&o
, head_tree
, &result
, 1, show_output
);
797 clean
= result
.clean
;
799 ensure_full_index(r
->index
);
800 clean
= merge_trees(&o
, head_tree
, next_tree
, base_tree
);
801 if (is_rebase_i(opts
) && clean
<= 0)
802 fputs(o
.obuf
.buf
, stdout
);
803 strbuf_release(&o
.obuf
);
806 rollback_lock_file(&index_lock
);
810 if (write_locked_index(r
->index
, &index_lock
,
811 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
813 * TRANSLATORS: %s will be "revert", "cherry-pick" or
816 return error(_("%s: Unable to write new index file"),
817 _(action_name(opts
)));
820 append_conflicts_hint(r
->index
, msgbuf
,
821 opts
->default_msg_cleanup
);
826 static struct object_id
*get_cache_tree_oid(struct index_state
*istate
)
828 if (!cache_tree_fully_valid(istate
->cache_tree
))
829 if (cache_tree_update(istate
, 0)) {
830 error(_("unable to update cache tree"));
834 return &istate
->cache_tree
->oid
;
837 static int is_index_unchanged(struct repository
*r
)
839 struct object_id head_oid
, *cache_tree_oid
;
840 const struct object_id
*head_tree_oid
;
841 struct commit
*head_commit
;
842 struct index_state
*istate
= r
->index
;
843 const char *head_name
;
845 if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository
), "HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
)) {
846 /* Check to see if this is an unborn branch */
847 head_name
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
849 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
852 !starts_with(head_name
, "refs/heads/") ||
853 !is_null_oid(&head_oid
))
854 return error(_("could not resolve HEAD commit"));
855 head_tree_oid
= the_hash_algo
->empty_tree
;
857 head_commit
= lookup_commit(r
, &head_oid
);
860 * If head_commit is NULL, check_commit, called from
861 * lookup_commit, would have indicated that head_commit is not
862 * a commit object already. repo_parse_commit() will return failure
863 * without further complaints in such a case. Otherwise, if
864 * the commit is invalid, repo_parse_commit() will complain. So
865 * there is nothing for us to say here. Just return failure.
867 if (repo_parse_commit(r
, head_commit
))
870 head_tree_oid
= get_commit_tree_oid(head_commit
);
873 if (!(cache_tree_oid
= get_cache_tree_oid(istate
)))
876 return oideq(cache_tree_oid
, head_tree_oid
);
879 static int write_author_script(const char *message
)
881 struct strbuf buf
= STRBUF_INIT
;
886 if (!*message
|| starts_with(message
, "\n")) {
888 /* Missing 'author' line? */
889 unlink(rebase_path_author_script());
891 } else if (skip_prefix(message
, "author ", &message
))
893 else if ((eol
= strchr(message
, '\n')))
898 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
899 while (*message
&& *message
!= '\n' && *message
!= '\r')
900 if (skip_prefix(message
, " <", &message
))
902 else if (*message
!= '\'')
903 strbuf_addch(&buf
, *(message
++));
905 strbuf_addf(&buf
, "'\\%c'", *(message
++));
906 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
907 while (*message
&& *message
!= '\n' && *message
!= '\r')
908 if (skip_prefix(message
, "> ", &message
))
910 else if (*message
!= '\'')
911 strbuf_addch(&buf
, *(message
++));
913 strbuf_addf(&buf
, "'\\%c'", *(message
++));
914 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
915 while (*message
&& *message
!= '\n' && *message
!= '\r')
916 if (*message
!= '\'')
917 strbuf_addch(&buf
, *(message
++));
919 strbuf_addf(&buf
, "'\\%c'", *(message
++));
920 strbuf_addch(&buf
, '\'');
921 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
922 strbuf_release(&buf
);
927 * Take a series of KEY='VALUE' lines where VALUE part is
928 * sq-quoted, and append <KEY, VALUE> at the end of the string list
930 static int parse_key_value_squoted(char *buf
, struct string_list
*list
)
933 struct string_list_item
*item
;
935 char *cp
= strchr(buf
, '=');
937 np
= strchrnul(buf
, '\n');
938 return error(_("no key present in '%.*s'"),
939 (int) (np
- buf
), buf
);
941 np
= strchrnul(cp
, '\n');
943 item
= string_list_append(list
, buf
);
945 buf
= np
+ (*np
== '\n');
949 return error(_("unable to dequote value of '%s'"),
951 item
->util
= xstrdup(cp
);
957 * Reads and parses the state directory's "author-script" file, and sets name,
958 * email and date accordingly.
959 * Returns 0 on success, -1 if the file could not be parsed.
961 * The author script is of the format:
963 * GIT_AUTHOR_NAME='$author_name'
964 * GIT_AUTHOR_EMAIL='$author_email'
965 * GIT_AUTHOR_DATE='$author_date'
967 * where $author_name, $author_email and $author_date are quoted. We are strict
968 * with our parsing, as the file was meant to be eval'd in the now-removed
969 * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
970 * from what this function expects, it is better to bail out than to do
971 * something that the user does not expect.
973 int read_author_script(const char *path
, char **name
, char **email
, char **date
,
976 struct strbuf buf
= STRBUF_INIT
;
977 struct string_list kv
= STRING_LIST_INIT_DUP
;
978 int retval
= -1; /* assume failure */
979 int i
, name_i
= -2, email_i
= -2, date_i
= -2, err
= 0;
981 if (strbuf_read_file(&buf
, path
, 256) <= 0) {
982 strbuf_release(&buf
);
983 if (errno
== ENOENT
&& allow_missing
)
986 return error_errno(_("could not open '%s' for reading"),
990 if (parse_key_value_squoted(buf
.buf
, &kv
))
993 for (i
= 0; i
< kv
.nr
; i
++) {
994 if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_NAME")) {
996 name_i
= error(_("'GIT_AUTHOR_NAME' already given"));
999 } else if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_EMAIL")) {
1001 email_i
= error(_("'GIT_AUTHOR_EMAIL' already given"));
1004 } else if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_DATE")) {
1006 date_i
= error(_("'GIT_AUTHOR_DATE' already given"));
1010 err
= error(_("unknown variable '%s'"),
1011 kv
.items
[i
].string
);
1015 error(_("missing 'GIT_AUTHOR_NAME'"));
1017 error(_("missing 'GIT_AUTHOR_EMAIL'"));
1019 error(_("missing 'GIT_AUTHOR_DATE'"));
1020 if (name_i
< 0 || email_i
< 0 || date_i
< 0 || err
)
1022 *name
= kv
.items
[name_i
].util
;
1023 *email
= kv
.items
[email_i
].util
;
1024 *date
= kv
.items
[date_i
].util
;
1027 string_list_clear(&kv
, !!retval
);
1028 strbuf_release(&buf
);
1033 * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
1034 * file with shell quoting into struct strvec. Returns -1 on
1035 * error, 0 otherwise.
1037 static int read_env_script(struct strvec
*env
)
1039 char *name
, *email
, *date
;
1041 if (read_author_script(rebase_path_author_script(),
1042 &name
, &email
, &date
, 0))
1045 strvec_pushf(env
, "GIT_AUTHOR_NAME=%s", name
);
1046 strvec_pushf(env
, "GIT_AUTHOR_EMAIL=%s", email
);
1047 strvec_pushf(env
, "GIT_AUTHOR_DATE=%s", date
);
1055 static char *get_author(const char *message
)
1060 a
= find_commit_header(message
, "author", &len
);
1062 return xmemdupz(a
, len
);
1067 static const char *author_date_from_env(const struct strvec
*env
)
1072 for (i
= 0; i
< env
->nr
; i
++)
1073 if (skip_prefix(env
->v
[i
],
1074 "GIT_AUTHOR_DATE=", &date
))
1077 * If GIT_AUTHOR_DATE is missing we should have already errored out when
1078 * reading the script
1080 BUG("GIT_AUTHOR_DATE missing from author script");
1083 static const char staged_changes_advice
[] =
1084 N_("you have staged changes in your working tree\n"
1085 "If these changes are meant to be squashed into the previous commit, run:\n"
1087 " git commit --amend %s\n"
1089 "If they are meant to go into a new commit, run:\n"
1093 "In both cases, once you're done, continue with:\n"
1095 " git rebase --continue\n");
1097 #define ALLOW_EMPTY (1<<0)
1098 #define EDIT_MSG (1<<1)
1099 #define AMEND_MSG (1<<2)
1100 #define CLEANUP_MSG (1<<3)
1101 #define VERIFY_MSG (1<<4)
1102 #define CREATE_ROOT_COMMIT (1<<5)
1103 #define VERBATIM_MSG (1<<6)
1105 static int run_command_silent_on_success(struct child_process
*cmd
)
1107 struct strbuf buf
= STRBUF_INIT
;
1110 cmd
->stdout_to_stderr
= 1;
1111 rc
= pipe_command(cmd
,
1117 fputs(buf
.buf
, stderr
);
1118 strbuf_release(&buf
);
1123 * If we are cherry-pick, and if the merge did not result in
1124 * hand-editing, we will hit this commit and inherit the original
1125 * author date and name.
1127 * If we are revert, or if our cherry-pick results in a hand merge,
1128 * we had better say that the current user is responsible for that.
1130 * An exception is when run_git_commit() is called during an
1131 * interactive rebase: in that case, we will want to retain the
1134 static int run_git_commit(const char *defmsg
,
1135 struct replay_opts
*opts
,
1138 struct replay_ctx
*ctx
= opts
->ctx
;
1139 struct child_process cmd
= CHILD_PROCESS_INIT
;
1141 if ((flags
& CLEANUP_MSG
) && (flags
& VERBATIM_MSG
))
1142 BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
1146 if (is_rebase_i(opts
) &&
1147 ((opts
->committer_date_is_author_date
&& !opts
->ignore_date
) ||
1148 !(!defmsg
&& (flags
& AMEND_MSG
))) &&
1149 read_env_script(&cmd
.env
)) {
1150 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
1152 return error(_(staged_changes_advice
),
1156 strvec_pushf(&cmd
.env
, GIT_REFLOG_ACTION
"=%s", ctx
->reflog_message
);
1158 if (opts
->committer_date_is_author_date
)
1159 strvec_pushf(&cmd
.env
, "GIT_COMMITTER_DATE=%s",
1162 author_date_from_env(&cmd
.env
));
1163 if (opts
->ignore_date
)
1164 strvec_push(&cmd
.env
, "GIT_AUTHOR_DATE=");
1166 strvec_push(&cmd
.args
, "commit");
1168 if (!(flags
& VERIFY_MSG
))
1169 strvec_push(&cmd
.args
, "-n");
1170 if ((flags
& AMEND_MSG
))
1171 strvec_push(&cmd
.args
, "--amend");
1173 strvec_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
1175 strvec_push(&cmd
.args
, "--no-gpg-sign");
1177 strvec_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
1178 else if (!(flags
& EDIT_MSG
))
1179 strvec_pushl(&cmd
.args
, "-C", "HEAD", NULL
);
1180 if ((flags
& CLEANUP_MSG
))
1181 strvec_push(&cmd
.args
, "--cleanup=strip");
1182 if ((flags
& VERBATIM_MSG
))
1183 strvec_push(&cmd
.args
, "--cleanup=verbatim");
1184 if ((flags
& EDIT_MSG
))
1185 strvec_push(&cmd
.args
, "-e");
1186 else if (!(flags
& CLEANUP_MSG
) &&
1187 !opts
->signoff
&& !opts
->record_origin
&&
1188 !opts
->explicit_cleanup
)
1189 strvec_push(&cmd
.args
, "--cleanup=verbatim");
1191 if ((flags
& ALLOW_EMPTY
))
1192 strvec_push(&cmd
.args
, "--allow-empty");
1194 if (!(flags
& EDIT_MSG
))
1195 strvec_push(&cmd
.args
, "--allow-empty-message");
1197 if (is_rebase_i(opts
) && !(flags
& EDIT_MSG
))
1198 return run_command_silent_on_success(&cmd
);
1200 return run_command(&cmd
);
1203 static int rest_is_empty(const struct strbuf
*sb
, int start
)
1208 /* Check if the rest is just whitespace and Signed-off-by's. */
1209 for (i
= start
; i
< sb
->len
; i
++) {
1210 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
1216 if (strlen(sign_off_header
) <= eol
- i
&&
1217 starts_with(sb
->buf
+ i
, sign_off_header
)) {
1222 if (!isspace(sb
->buf
[i
++]))
1229 void cleanup_message(struct strbuf
*msgbuf
,
1230 enum commit_msg_cleanup_mode cleanup_mode
, int verbose
)
1232 if (verbose
|| /* Truncate the message just before the diff, if any. */
1233 cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
1234 strbuf_setlen(msgbuf
, wt_status_locate_end(msgbuf
->buf
, msgbuf
->len
));
1235 if (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
)
1236 strbuf_stripspace(msgbuf
,
1237 cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
? comment_line_str
: NULL
);
1241 * Find out if the message in the strbuf contains only whitespace and
1242 * Signed-off-by lines.
1244 int message_is_empty(const struct strbuf
*sb
,
1245 enum commit_msg_cleanup_mode cleanup_mode
)
1247 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
1249 return rest_is_empty(sb
, 0);
1253 * See if the user edited the message in the editor or left what
1254 * was in the template intact
1256 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
1257 enum commit_msg_cleanup_mode cleanup_mode
)
1259 struct strbuf tmpl
= STRBUF_INIT
;
1262 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
1265 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
1268 strbuf_stripspace(&tmpl
,
1269 cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
? comment_line_str
: NULL
);
1270 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
1272 strbuf_release(&tmpl
);
1273 return rest_is_empty(sb
, start
- sb
->buf
);
1276 int update_head_with_reflog(const struct commit
*old_head
,
1277 const struct object_id
*new_head
,
1278 const char *action
, const struct strbuf
*msg
,
1281 struct ref_transaction
*transaction
;
1282 struct strbuf sb
= STRBUF_INIT
;
1287 strbuf_addstr(&sb
, action
);
1288 strbuf_addstr(&sb
, ": ");
1291 nl
= strchr(msg
->buf
, '\n');
1293 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
1295 strbuf_addbuf(&sb
, msg
);
1296 strbuf_addch(&sb
, '\n');
1299 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
1302 ref_transaction_update(transaction
, "HEAD", new_head
,
1303 old_head
? &old_head
->object
.oid
: null_oid(),
1304 NULL
, NULL
, 0, sb
.buf
, err
) ||
1305 ref_transaction_commit(transaction
, err
)) {
1308 ref_transaction_free(transaction
);
1309 strbuf_release(&sb
);
1314 static int run_rewrite_hook(const struct object_id
*oldoid
,
1315 const struct object_id
*newoid
)
1317 struct child_process proc
= CHILD_PROCESS_INIT
;
1319 struct strbuf sb
= STRBUF_INIT
;
1320 const char *hook_path
= find_hook(the_repository
, "post-rewrite");
1325 strvec_pushl(&proc
.args
, hook_path
, "amend", NULL
);
1327 proc
.stdout_to_stderr
= 1;
1328 proc
.trace2_hook_name
= "post-rewrite";
1330 code
= start_command(&proc
);
1333 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
1334 sigchain_push(SIGPIPE
, SIG_IGN
);
1335 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
1337 strbuf_release(&sb
);
1338 sigchain_pop(SIGPIPE
);
1339 return finish_command(&proc
);
1342 void commit_post_rewrite(struct repository
*r
,
1343 const struct commit
*old_head
,
1344 const struct object_id
*new_head
)
1346 struct notes_rewrite_cfg
*cfg
;
1348 cfg
= init_copy_notes_for_rewrite("amend");
1350 /* we are amending, so old_head is not NULL */
1351 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
1352 finish_copy_notes_for_rewrite(r
, cfg
, "Notes added by 'git commit --amend'");
1354 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
1357 static int run_prepare_commit_msg_hook(struct repository
*r
,
1362 const char *name
, *arg1
= NULL
, *arg2
= NULL
;
1364 name
= git_path_commit_editmsg();
1365 if (write_message(msg
->buf
, msg
->len
, name
, 0))
1374 if (run_commit_hook(0, r
->index_file
, NULL
, "prepare-commit-msg", name
,
1376 ret
= error(_("'prepare-commit-msg' hook failed"));
1381 static const char implicit_ident_advice_noconfig
[] =
1382 N_("Your name and email address were configured automatically based\n"
1383 "on your username and hostname. Please check that they are accurate.\n"
1384 "You can suppress this message by setting them explicitly. Run the\n"
1385 "following command and follow the instructions in your editor to edit\n"
1386 "your configuration file:\n"
1388 " git config --global --edit\n"
1390 "After doing this, you may fix the identity used for this commit with:\n"
1392 " git commit --amend --reset-author\n");
1394 static const char implicit_ident_advice_config
[] =
1395 N_("Your name and email address were configured automatically based\n"
1396 "on your username and hostname. Please check that they are accurate.\n"
1397 "You can suppress this message by setting them explicitly:\n"
1399 " git config --global user.name \"Your Name\"\n"
1400 " git config --global user.email you@example.com\n"
1402 "After doing this, you may fix the identity used for this commit with:\n"
1404 " git commit --amend --reset-author\n");
1406 static const char *implicit_ident_advice(void)
1408 char *user_config
= interpolate_path("~/.gitconfig", 0);
1409 char *xdg_config
= xdg_config_home("config");
1410 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
1416 return _(implicit_ident_advice_config
);
1418 return _(implicit_ident_advice_noconfig
);
1422 void print_commit_summary(struct repository
*r
,
1424 const struct object_id
*oid
,
1427 struct rev_info rev
;
1428 struct commit
*commit
;
1429 struct strbuf format
= STRBUF_INIT
;
1431 struct pretty_print_context pctx
= {0};
1432 struct strbuf author_ident
= STRBUF_INIT
;
1433 struct strbuf committer_ident
= STRBUF_INIT
;
1434 struct ref_store
*refs
;
1436 commit
= lookup_commit(r
, oid
);
1438 die(_("couldn't look up newly created commit"));
1439 if (repo_parse_commit(r
, commit
))
1440 die(_("could not parse newly created commit"));
1442 strbuf_addstr(&format
, "format:%h] %s");
1444 repo_format_commit_message(r
, commit
, "%an <%ae>", &author_ident
,
1446 repo_format_commit_message(r
, commit
, "%cn <%ce>", &committer_ident
,
1448 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1449 strbuf_addstr(&format
, "\n Author: ");
1450 strbuf_addbuf_percentquote(&format
, &author_ident
);
1452 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
1453 struct strbuf date
= STRBUF_INIT
;
1455 repo_format_commit_message(r
, commit
, "%ad", &date
, &pctx
);
1456 strbuf_addstr(&format
, "\n Date: ");
1457 strbuf_addbuf_percentquote(&format
, &date
);
1458 strbuf_release(&date
);
1460 if (!committer_ident_sufficiently_given()) {
1461 strbuf_addstr(&format
, "\n Committer: ");
1462 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1463 if (advice_enabled(ADVICE_IMPLICIT_IDENTITY
)) {
1464 strbuf_addch(&format
, '\n');
1465 strbuf_addstr(&format
, implicit_ident_advice());
1468 strbuf_release(&author_ident
);
1469 strbuf_release(&committer_ident
);
1471 repo_init_revisions(r
, &rev
, prefix
);
1472 setup_revisions(0, NULL
, &rev
, NULL
);
1475 rev
.diffopt
.output_format
=
1476 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1478 rev
.verbose_header
= 1;
1479 rev
.show_root_diff
= 1;
1480 get_commit_format(format
.buf
, &rev
);
1481 rev
.always_show_header
= 0;
1482 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1483 diff_setup_done(&rev
.diffopt
);
1485 refs
= get_main_ref_store(r
);
1486 head
= refs_resolve_ref_unsafe(refs
, "HEAD", 0, NULL
, NULL
);
1488 die(_("unable to resolve HEAD after creating commit"));
1489 if (!strcmp(head
, "HEAD"))
1490 head
= _("detached HEAD");
1492 skip_prefix(head
, "refs/heads/", &head
);
1493 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1494 _(" (root-commit)") : "");
1496 if (!log_tree_commit(&rev
, commit
)) {
1497 rev
.always_show_header
= 1;
1498 rev
.use_terminator
= 1;
1499 log_tree_commit(&rev
, commit
);
1502 release_revisions(&rev
);
1503 strbuf_release(&format
);
1506 static int parse_head(struct repository
*r
, struct commit
**head
)
1508 struct commit
*current_head
;
1509 struct object_id oid
;
1511 if (repo_get_oid(r
, "HEAD", &oid
)) {
1512 current_head
= NULL
;
1514 current_head
= lookup_commit_reference(r
, &oid
);
1516 return error(_("could not parse HEAD"));
1517 if (!oideq(&oid
, ¤t_head
->object
.oid
)) {
1518 warning(_("HEAD %s is not a commit!"),
1521 if (repo_parse_commit(r
, current_head
))
1522 return error(_("could not parse HEAD commit"));
1524 *head
= current_head
;
1530 * Try to commit without forking 'git commit'. In some cases we need
1531 * to run 'git commit' to display an error message
1534 * -1 - error unable to commit
1536 * 1 - run 'git commit'
1538 static int try_to_commit(struct repository
*r
,
1539 struct strbuf
*msg
, const char *author
,
1540 struct replay_opts
*opts
, unsigned int flags
,
1541 struct object_id
*oid
)
1543 struct replay_ctx
*ctx
= opts
->ctx
;
1544 struct object_id tree
;
1545 struct commit
*current_head
= NULL
;
1546 struct commit_list
*parents
= NULL
;
1547 struct commit_extra_header
*extra
= NULL
;
1548 struct strbuf err
= STRBUF_INIT
;
1549 struct strbuf commit_msg
= STRBUF_INIT
;
1550 char *amend_author
= NULL
;
1551 const char *committer
= NULL
;
1552 const char *hook_commit
= NULL
;
1553 enum commit_msg_cleanup_mode cleanup
;
1556 if ((flags
& CLEANUP_MSG
) && (flags
& VERBATIM_MSG
))
1557 BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
1559 if (parse_head(r
, ¤t_head
))
1562 if (flags
& AMEND_MSG
) {
1563 const char *exclude_gpgsig
[] = { "gpgsig", "gpgsig-sha256", NULL
};
1564 const char *out_enc
= get_commit_output_encoding();
1565 const char *message
= repo_logmsg_reencode(r
, current_head
,
1569 const char *orig_message
= NULL
;
1571 find_commit_subject(message
, &orig_message
);
1573 strbuf_addstr(msg
, orig_message
);
1574 hook_commit
= "HEAD";
1576 author
= amend_author
= get_author(message
);
1577 repo_unuse_commit_buffer(r
, current_head
,
1580 res
= error(_("unable to parse commit author"));
1583 parents
= copy_commit_list(current_head
->parents
);
1584 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1585 } else if (current_head
&&
1586 (!(flags
& CREATE_ROOT_COMMIT
) || (flags
& AMEND_MSG
))) {
1587 commit_list_insert(current_head
, &parents
);
1590 if (write_index_as_tree(&tree
, r
->index
, r
->index_file
, 0, NULL
)) {
1591 res
= error(_("git write-tree failed to write a tree"));
1595 if (!(flags
& ALLOW_EMPTY
)) {
1596 struct commit
*first_parent
= current_head
;
1598 if (flags
& AMEND_MSG
) {
1599 if (current_head
->parents
) {
1600 first_parent
= current_head
->parents
->item
;
1601 if (repo_parse_commit(r
, first_parent
)) {
1602 res
= error(_("could not parse HEAD commit"));
1606 first_parent
= NULL
;
1609 if (oideq(first_parent
1610 ? get_commit_tree_oid(first_parent
)
1611 : the_hash_algo
->empty_tree
,
1613 res
= 1; /* run 'git commit' to display error message */
1618 if (hook_exists(r
, "prepare-commit-msg")) {
1619 res
= run_prepare_commit_msg_hook(r
, msg
, hook_commit
);
1622 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1624 res
= error_errno(_("unable to read commit message "
1626 git_path_commit_editmsg());
1632 if (flags
& CLEANUP_MSG
)
1633 cleanup
= COMMIT_MSG_CLEANUP_ALL
;
1634 else if (flags
& VERBATIM_MSG
)
1635 cleanup
= COMMIT_MSG_CLEANUP_NONE
;
1636 else if ((opts
->signoff
|| opts
->record_origin
) &&
1637 !opts
->explicit_cleanup
)
1638 cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
1640 cleanup
= opts
->default_msg_cleanup
;
1642 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1643 strbuf_stripspace(msg
,
1644 cleanup
== COMMIT_MSG_CLEANUP_ALL
? comment_line_str
: NULL
);
1645 if ((flags
& EDIT_MSG
) && message_is_empty(msg
, cleanup
)) {
1646 res
= 1; /* run 'git commit' to display error message */
1650 if (opts
->committer_date_is_author_date
) {
1651 struct ident_split id
;
1652 struct strbuf date
= STRBUF_INIT
;
1654 if (!opts
->ignore_date
) {
1655 if (split_ident_line(&id
, author
, (int)strlen(author
)) < 0) {
1656 res
= error(_("invalid author identity '%s'"),
1660 if (!id
.date_begin
) {
1662 "corrupt author: missing date information"));
1665 strbuf_addf(&date
, "@%.*s %.*s",
1666 (int)(id
.date_end
- id
.date_begin
),
1668 (int)(id
.tz_end
- id
.tz_begin
),
1673 committer
= fmt_ident(getenv("GIT_COMMITTER_NAME"),
1674 getenv("GIT_COMMITTER_EMAIL"),
1675 WANT_COMMITTER_IDENT
,
1676 opts
->ignore_date
? NULL
: date
.buf
,
1678 strbuf_release(&date
);
1683 if (opts
->ignore_date
) {
1684 struct ident_split id
;
1687 if (split_ident_line(&id
, author
, strlen(author
)) < 0) {
1688 error(_("invalid author identity '%s'"), author
);
1691 name
= xmemdupz(id
.name_begin
, id
.name_end
- id
.name_begin
);
1692 email
= xmemdupz(id
.mail_begin
, id
.mail_end
- id
.mail_begin
);
1693 author
= fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, NULL
,
1699 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
, oid
,
1700 author
, committer
, opts
->gpg_sign
, extra
)) {
1701 res
= error(_("failed to write commit object"));
1705 if (update_head_with_reflog(current_head
, oid
, ctx
->reflog_message
,
1707 res
= error("%s", err
.buf
);
1711 run_commit_hook(0, r
->index_file
, NULL
, "post-commit", NULL
);
1712 if (flags
& AMEND_MSG
)
1713 commit_post_rewrite(r
, current_head
, oid
);
1716 free_commit_extra_headers(extra
);
1717 free_commit_list(parents
);
1718 strbuf_release(&err
);
1719 strbuf_release(&commit_msg
);
1725 static int write_rebase_head(struct object_id
*oid
)
1727 if (refs_update_ref(get_main_ref_store(the_repository
), "rebase", "REBASE_HEAD", oid
,
1728 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1729 return error(_("could not update %s"), "REBASE_HEAD");
1734 static int do_commit(struct repository
*r
,
1735 const char *msg_file
, const char *author
,
1736 struct replay_opts
*opts
, unsigned int flags
,
1737 struct object_id
*oid
)
1741 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
)) {
1742 struct object_id oid
;
1743 struct strbuf sb
= STRBUF_INIT
;
1745 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1746 return error_errno(_("unable to read commit message "
1750 res
= try_to_commit(r
, msg_file
? &sb
: NULL
,
1751 author
, opts
, flags
, &oid
);
1752 strbuf_release(&sb
);
1754 refs_delete_ref(get_main_ref_store(r
), "",
1755 "CHERRY_PICK_HEAD", NULL
, REF_NO_DEREF
);
1756 unlink(git_path_merge_msg(r
));
1757 if (!is_rebase_i(opts
))
1758 print_commit_summary(r
, NULL
, &oid
,
1759 SUMMARY_SHOW_AUTHOR_DATE
);
1764 if (is_rebase_i(opts
) && oid
)
1765 if (write_rebase_head(oid
))
1767 return run_git_commit(msg_file
, opts
, flags
);
1773 static int is_original_commit_empty(struct commit
*commit
)
1775 const struct object_id
*ptree_oid
;
1777 if (repo_parse_commit(the_repository
, commit
))
1778 return error(_("could not parse commit %s"),
1779 oid_to_hex(&commit
->object
.oid
));
1780 if (commit
->parents
) {
1781 struct commit
*parent
= commit
->parents
->item
;
1782 if (repo_parse_commit(the_repository
, parent
))
1783 return error(_("could not parse parent commit %s"),
1784 oid_to_hex(&parent
->object
.oid
));
1785 ptree_oid
= get_commit_tree_oid(parent
);
1787 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1790 return oideq(ptree_oid
, get_commit_tree_oid(commit
));
1794 * Should empty commits be allowed? Return status:
1795 * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit)
1796 * 0: Halt on empty commit
1797 * 1: Allow empty commit
1798 * 2: Drop empty commit
1800 static int allow_empty(struct repository
*r
,
1801 struct replay_opts
*opts
,
1802 struct commit
*commit
)
1804 int index_unchanged
, originally_empty
;
1807 * For a commit that is initially empty, allow_empty determines if it
1808 * should be kept or not
1810 * For a commit that becomes empty, keep_redundant_commits and
1811 * drop_redundant_commits determine whether the commit should be kept or
1812 * dropped. If neither is specified, halt.
1814 index_unchanged
= is_index_unchanged(r
);
1815 if (index_unchanged
< 0)
1816 return index_unchanged
;
1817 if (!index_unchanged
)
1818 return 0; /* we do not have to say --allow-empty */
1820 originally_empty
= is_original_commit_empty(commit
);
1821 if (originally_empty
< 0)
1822 return originally_empty
;
1823 if (originally_empty
)
1824 return opts
->allow_empty
;
1825 else if (opts
->keep_redundant_commits
)
1827 else if (opts
->drop_redundant_commits
)
1836 } todo_command_info
[] = {
1837 [TODO_PICK
] = { 'p', "pick" },
1838 [TODO_REVERT
] = { 0, "revert" },
1839 [TODO_EDIT
] = { 'e', "edit" },
1840 [TODO_REWORD
] = { 'r', "reword" },
1841 [TODO_FIXUP
] = { 'f', "fixup" },
1842 [TODO_SQUASH
] = { 's', "squash" },
1843 [TODO_EXEC
] = { 'x', "exec" },
1844 [TODO_BREAK
] = { 'b', "break" },
1845 [TODO_LABEL
] = { 'l', "label" },
1846 [TODO_RESET
] = { 't', "reset" },
1847 [TODO_MERGE
] = { 'm', "merge" },
1848 [TODO_UPDATE_REF
] = { 'u', "update-ref" },
1849 [TODO_NOOP
] = { 0, "noop" },
1850 [TODO_DROP
] = { 'd', "drop" },
1851 [TODO_COMMENT
] = { 0, NULL
},
1854 static const char *command_to_string(const enum todo_command command
)
1856 if (command
< TODO_COMMENT
)
1857 return todo_command_info
[command
].str
;
1858 if (command
== TODO_COMMENT
)
1859 return comment_line_str
;
1860 die(_("unknown command: %d"), command
);
1863 static char command_to_char(const enum todo_command command
)
1865 if (command
< TODO_COMMENT
)
1866 return todo_command_info
[command
].c
;
1870 static int is_noop(const enum todo_command command
)
1872 return TODO_NOOP
<= command
;
1875 static int is_fixup(enum todo_command command
)
1877 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1880 /* Does this command create a (non-merge) commit? */
1881 static int is_pick_or_similar(enum todo_command command
)
1896 enum todo_item_flags
{
1897 TODO_EDIT_MERGE_MSG
= (1 << 0),
1898 TODO_REPLACE_FIXUP_MSG
= (1 << 1),
1899 TODO_EDIT_FIXUP_MSG
= (1 << 2),
1902 static const char first_commit_msg_str
[] = N_("This is the 1st commit message:");
1903 static const char nth_commit_msg_fmt
[] = N_("This is the commit message #%d:");
1904 static const char skip_first_commit_msg_str
[] = N_("The 1st commit message will be skipped:");
1905 static const char skip_nth_commit_msg_fmt
[] = N_("The commit message #%d will be skipped:");
1906 static const char combined_commit_msg_fmt
[] = N_("This is a combination of %d commits.");
1908 static int is_fixup_flag(enum todo_command command
, unsigned flag
)
1910 return command
== TODO_FIXUP
&& ((flag
& TODO_REPLACE_FIXUP_MSG
) ||
1911 (flag
& TODO_EDIT_FIXUP_MSG
));
1915 * Wrapper around strbuf_add_commented_lines() which avoids double
1916 * commenting commit subjects.
1918 static void add_commented_lines(struct strbuf
*buf
, const void *str
, size_t len
)
1920 const char *s
= str
;
1921 while (starts_with_mem(s
, len
, comment_line_str
)) {
1923 const char *n
= memchr(s
, '\n', len
);
1928 strbuf_add(buf
, s
, count
);
1932 strbuf_add_commented_lines(buf
, s
, len
, comment_line_str
);
1935 /* Does the current fixup chain contain a squash command? */
1936 static int seen_squash(struct replay_ctx
*ctx
)
1938 return starts_with(ctx
->current_fixups
.buf
, "squash") ||
1939 strstr(ctx
->current_fixups
.buf
, "\nsquash");
1942 static void update_comment_bufs(struct strbuf
*buf1
, struct strbuf
*buf2
, int n
)
1944 strbuf_setlen(buf1
, 2);
1945 strbuf_addf(buf1
, _(nth_commit_msg_fmt
), n
);
1946 strbuf_addch(buf1
, '\n');
1947 strbuf_setlen(buf2
, 2);
1948 strbuf_addf(buf2
, _(skip_nth_commit_msg_fmt
), n
);
1949 strbuf_addch(buf2
, '\n');
1953 * Comment out any un-commented commit messages, updating the message comments
1954 * to say they will be skipped but do not comment out the empty lines that
1955 * surround commit messages and their comments.
1957 static void update_squash_message_for_fixup(struct strbuf
*msg
)
1959 void (*copy_lines
)(struct strbuf
*, const void *, size_t) = strbuf_add
;
1960 struct strbuf buf1
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1961 const char *s
, *start
;
1963 size_t orig_msg_len
;
1966 strbuf_addf(&buf1
, "# %s\n", _(first_commit_msg_str
));
1967 strbuf_addf(&buf2
, "# %s\n", _(skip_first_commit_msg_str
));
1968 s
= start
= orig_msg
= strbuf_detach(msg
, &orig_msg_len
);
1972 if (skip_prefix(s
, buf1
.buf
, &next
)) {
1974 * Copy the last message, preserving the blank line
1975 * preceding the current line
1977 off
= (s
> start
+ 1 && s
[-2] == '\n') ? 1 : 0;
1978 copy_lines(msg
, start
, s
- start
- off
);
1980 strbuf_addch(msg
, '\n');
1982 * The next message needs to be commented out but the
1983 * message header is already commented out so just copy
1984 * it and the blank line that follows it.
1986 strbuf_addbuf(msg
, &buf2
);
1988 strbuf_addch(msg
, *next
++);
1990 copy_lines
= add_commented_lines
;
1991 update_comment_bufs(&buf1
, &buf2
, ++i
);
1992 } else if (skip_prefix(s
, buf2
.buf
, &next
)) {
1993 off
= (s
> start
+ 1 && s
[-2] == '\n') ? 1 : 0;
1994 copy_lines(msg
, start
, s
- start
- off
);
1997 copy_lines
= strbuf_add
;
1998 update_comment_bufs(&buf1
, &buf2
, ++i
);
2000 s
= strchr(s
, '\n');
2005 copy_lines(msg
, start
, orig_msg_len
- (start
- orig_msg
));
2007 strbuf_release(&buf1
);
2008 strbuf_release(&buf2
);
2011 static int append_squash_message(struct strbuf
*buf
, const char *body
,
2012 enum todo_command command
, struct replay_opts
*opts
,
2015 struct replay_ctx
*ctx
= opts
->ctx
;
2016 const char *fixup_msg
;
2017 size_t commented_len
= 0, fixup_off
;
2019 * amend is non-interactive and not normally used with fixup!
2020 * or squash! commits, so only comment out those subjects when
2021 * squashing commit messages.
2023 if (starts_with(body
, "amend!") ||
2024 ((command
== TODO_SQUASH
|| seen_squash(ctx
)) &&
2025 (starts_with(body
, "squash!") || starts_with(body
, "fixup!"))))
2026 commented_len
= commit_subject_length(body
);
2028 strbuf_addf(buf
, "\n%s ", comment_line_str
);
2029 strbuf_addf(buf
, _(nth_commit_msg_fmt
),
2030 ++ctx
->current_fixup_count
+ 1);
2031 strbuf_addstr(buf
, "\n\n");
2032 strbuf_add_commented_lines(buf
, body
, commented_len
, comment_line_str
);
2033 /* buf->buf may be reallocated so store an offset into the buffer */
2034 fixup_off
= buf
->len
;
2035 strbuf_addstr(buf
, body
+ commented_len
);
2037 /* fixup -C after squash behaves like squash */
2038 if (is_fixup_flag(command
, flag
) && !seen_squash(ctx
)) {
2040 * We're replacing the commit message so we need to
2041 * append the Signed-off-by: trailer if the user
2042 * requested '--signoff'.
2045 append_signoff(buf
, 0, 0);
2047 if ((command
== TODO_FIXUP
) &&
2048 (flag
& TODO_REPLACE_FIXUP_MSG
) &&
2049 (file_exists(rebase_path_fixup_msg()) ||
2050 !file_exists(rebase_path_squash_msg()))) {
2051 fixup_msg
= skip_blank_lines(buf
->buf
+ fixup_off
);
2052 if (write_message(fixup_msg
, strlen(fixup_msg
),
2053 rebase_path_fixup_msg(), 0) < 0)
2054 return error(_("cannot write '%s'"),
2055 rebase_path_fixup_msg());
2057 unlink(rebase_path_fixup_msg());
2060 unlink(rebase_path_fixup_msg());
2066 static int update_squash_messages(struct repository
*r
,
2067 enum todo_command command
,
2068 struct commit
*commit
,
2069 struct replay_opts
*opts
,
2072 struct replay_ctx
*ctx
= opts
->ctx
;
2073 struct strbuf buf
= STRBUF_INIT
;
2075 const char *message
, *body
;
2076 const char *encoding
= get_commit_output_encoding();
2078 if (ctx
->current_fixup_count
> 0) {
2079 struct strbuf header
= STRBUF_INIT
;
2082 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 9) <= 0)
2083 return error(_("could not read '%s'"),
2084 rebase_path_squash_msg());
2086 eol
= !starts_with(buf
.buf
, comment_line_str
) ?
2087 buf
.buf
: strchrnul(buf
.buf
, '\n');
2089 strbuf_addf(&header
, "%s ", comment_line_str
);
2090 strbuf_addf(&header
, _(combined_commit_msg_fmt
),
2091 ctx
->current_fixup_count
+ 2);
2092 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
2093 strbuf_release(&header
);
2094 if (is_fixup_flag(command
, flag
) && !seen_squash(ctx
))
2095 update_squash_message_for_fixup(&buf
);
2097 struct object_id head
;
2098 struct commit
*head_commit
;
2099 const char *head_message
, *body
;
2101 if (repo_get_oid(r
, "HEAD", &head
))
2102 return error(_("need a HEAD to fixup"));
2103 if (!(head_commit
= lookup_commit_reference(r
, &head
)))
2104 return error(_("could not read HEAD"));
2105 if (!(head_message
= repo_logmsg_reencode(r
, head_commit
, NULL
,
2107 return error(_("could not read HEAD's commit message"));
2109 find_commit_subject(head_message
, &body
);
2110 if (command
== TODO_FIXUP
&& !flag
&& write_message(body
, strlen(body
),
2111 rebase_path_fixup_msg(), 0) < 0) {
2112 repo_unuse_commit_buffer(r
, head_commit
, head_message
);
2113 return error(_("cannot write '%s'"), rebase_path_fixup_msg());
2115 strbuf_addf(&buf
, "%s ", comment_line_str
);
2116 strbuf_addf(&buf
, _(combined_commit_msg_fmt
), 2);
2117 strbuf_addf(&buf
, "\n%s ", comment_line_str
);
2118 strbuf_addstr(&buf
, is_fixup_flag(command
, flag
) ?
2119 _(skip_first_commit_msg_str
) :
2120 _(first_commit_msg_str
));
2121 strbuf_addstr(&buf
, "\n\n");
2122 if (is_fixup_flag(command
, flag
))
2123 strbuf_add_commented_lines(&buf
, body
, strlen(body
),
2126 strbuf_addstr(&buf
, body
);
2128 repo_unuse_commit_buffer(r
, head_commit
, head_message
);
2131 if (!(message
= repo_logmsg_reencode(r
, commit
, NULL
, encoding
)))
2132 return error(_("could not read commit message of %s"),
2133 oid_to_hex(&commit
->object
.oid
));
2134 find_commit_subject(message
, &body
);
2136 if (command
== TODO_SQUASH
|| is_fixup_flag(command
, flag
)) {
2137 res
= append_squash_message(&buf
, body
, command
, opts
, flag
);
2138 } else if (command
== TODO_FIXUP
) {
2139 strbuf_addf(&buf
, "\n%s ", comment_line_str
);
2140 strbuf_addf(&buf
, _(skip_nth_commit_msg_fmt
),
2141 ++ctx
->current_fixup_count
+ 1);
2142 strbuf_addstr(&buf
, "\n\n");
2143 strbuf_add_commented_lines(&buf
, body
, strlen(body
),
2146 return error(_("unknown command: %d"), command
);
2147 repo_unuse_commit_buffer(r
, commit
, message
);
2150 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(),
2152 strbuf_release(&buf
);
2155 strbuf_addf(&ctx
->current_fixups
, "%s%s %s",
2156 ctx
->current_fixups
.len
? "\n" : "",
2157 command_to_string(command
),
2158 oid_to_hex(&commit
->object
.oid
));
2159 res
= write_message(ctx
->current_fixups
.buf
,
2160 ctx
->current_fixups
.len
,
2161 rebase_path_current_fixups(), 0);
2167 static void flush_rewritten_pending(void)
2169 struct strbuf buf
= STRBUF_INIT
;
2170 struct object_id newoid
;
2173 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
2174 !repo_get_oid(the_repository
, "HEAD", &newoid
) &&
2175 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
2176 char *bol
= buf
.buf
, *eol
;
2179 eol
= strchrnul(bol
, '\n');
2180 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
2181 bol
, oid_to_hex(&newoid
));
2187 unlink(rebase_path_rewritten_pending());
2189 strbuf_release(&buf
);
2192 static void record_in_rewritten(struct object_id
*oid
,
2193 enum todo_command next_command
)
2195 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
2200 fprintf(out
, "%s\n", oid_to_hex(oid
));
2203 if (!is_fixup(next_command
))
2204 flush_rewritten_pending();
2207 static int should_edit(struct replay_opts
*opts
) {
2210 * Note that we only handle the case of non-conflicted
2211 * commits; continue_single_pick() handles the conflicted
2212 * commits itself instead of calling this function.
2214 return (opts
->action
== REPLAY_REVERT
&& isatty(0)) ? 1 : 0;
2218 static void refer_to_commit(struct replay_opts
*opts
,
2219 struct strbuf
*msgbuf
, struct commit
*commit
)
2221 if (opts
->commit_use_reference
) {
2222 struct pretty_print_context ctx
= {
2223 .abbrev
= DEFAULT_ABBREV
,
2224 .date_mode
.type
= DATE_SHORT
,
2226 repo_format_commit_message(the_repository
, commit
,
2227 "%h (%s, %ad)", msgbuf
, &ctx
);
2229 strbuf_addstr(msgbuf
, oid_to_hex(&commit
->object
.oid
));
2233 static int do_pick_commit(struct repository
*r
,
2234 struct todo_item
*item
,
2235 struct replay_opts
*opts
,
2236 int final_fixup
, int *check_todo
)
2238 struct replay_ctx
*ctx
= opts
->ctx
;
2239 unsigned int flags
= should_edit(opts
) ? EDIT_MSG
: 0;
2240 const char *msg_file
= should_edit(opts
) ? NULL
: git_path_merge_msg(r
);
2241 struct object_id head
;
2242 struct commit
*base
, *next
, *parent
;
2243 const char *base_label
, *next_label
;
2244 char *author
= NULL
;
2245 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
2246 int res
, unborn
= 0, reword
= 0, allow
, drop_commit
;
2247 enum todo_command command
= item
->command
;
2248 struct commit
*commit
= item
->commit
;
2250 if (opts
->no_commit
) {
2252 * We do not intend to commit immediately. We just want to
2253 * merge the differences in, so let's compute the tree
2254 * that represents the "current" state for the merge machinery
2257 if (write_index_as_tree(&head
, r
->index
, r
->index_file
, 0, NULL
))
2258 return error(_("your index file is unmerged."));
2260 unborn
= repo_get_oid(r
, "HEAD", &head
);
2261 /* Do we want to generate a root commit? */
2262 if (is_pick_or_similar(command
) && opts
->have_squash_onto
&&
2263 oideq(&head
, &opts
->squash_onto
)) {
2264 if (is_fixup(command
))
2265 return error(_("cannot fixup root commit"));
2266 flags
|= CREATE_ROOT_COMMIT
;
2269 oidcpy(&head
, the_hash_algo
->empty_tree
);
2270 if (index_differs_from(r
, unborn
? empty_tree_oid_hex(the_repository
->hash_algo
) : "HEAD",
2272 return error_dirty_index(r
, opts
);
2274 discard_index(r
->index
);
2276 if (!commit
->parents
)
2278 else if (commit
->parents
->next
) {
2279 /* Reverting or cherry-picking a merge commit */
2281 struct commit_list
*p
;
2283 if (!opts
->mainline
)
2284 return error(_("commit %s is a merge but no -m option was given."),
2285 oid_to_hex(&commit
->object
.oid
));
2287 for (cnt
= 1, p
= commit
->parents
;
2288 cnt
!= opts
->mainline
&& p
;
2291 if (cnt
!= opts
->mainline
|| !p
)
2292 return error(_("commit %s does not have parent %d"),
2293 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
2295 } else if (1 < opts
->mainline
)
2297 * Non-first parent explicitly specified as mainline for
2300 return error(_("commit %s does not have parent %d"),
2301 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
2303 parent
= commit
->parents
->item
;
2305 if (get_message(commit
, &msg
) != 0)
2306 return error(_("cannot get commit message for %s"),
2307 oid_to_hex(&commit
->object
.oid
));
2309 if (opts
->allow_ff
&& !is_fixup(command
) &&
2310 ((parent
&& oideq(&parent
->object
.oid
, &head
)) ||
2311 (!parent
&& unborn
))) {
2312 if (is_rebase_i(opts
))
2313 write_author_script(msg
.message
);
2314 res
= fast_forward_to(r
, &commit
->object
.oid
, &head
, unborn
,
2316 if (res
|| command
!= TODO_REWORD
)
2320 goto fast_forward_edit
;
2322 if (parent
&& repo_parse_commit(r
, parent
) < 0)
2323 /* TRANSLATORS: The first %s will be a "todo" command like
2324 "revert" or "pick", the second %s a SHA1. */
2325 return error(_("%s: cannot parse parent commit %s"),
2326 command_to_string(command
),
2327 oid_to_hex(&parent
->object
.oid
));
2330 * "commit" is an existing commit. We would want to apply
2331 * the difference it introduces since its first parent "prev"
2332 * on top of the current HEAD if we are cherry-pick. Or the
2333 * reverse of it if we are revert.
2336 if (command
== TODO_REVERT
) {
2337 const char *orig_subject
;
2340 base_label
= msg
.label
;
2342 next_label
= msg
.parent_label
;
2343 if (opts
->commit_use_reference
) {
2344 strbuf_addstr(&ctx
->message
,
2345 "# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
2346 } else if (skip_prefix(msg
.subject
, "Revert \"", &orig_subject
) &&
2348 * We don't touch pre-existing repeated reverts, because
2349 * theoretically these can be nested arbitrarily deeply,
2350 * thus requiring excessive complexity to deal with.
2352 !starts_with(orig_subject
, "Revert \"")) {
2353 strbuf_addstr(&ctx
->message
, "Reapply \"");
2354 strbuf_addstr(&ctx
->message
, orig_subject
);
2356 strbuf_addstr(&ctx
->message
, "Revert \"");
2357 strbuf_addstr(&ctx
->message
, msg
.subject
);
2358 strbuf_addstr(&ctx
->message
, "\"");
2360 strbuf_addstr(&ctx
->message
, "\n\nThis reverts commit ");
2361 refer_to_commit(opts
, &ctx
->message
, commit
);
2363 if (commit
->parents
&& commit
->parents
->next
) {
2364 strbuf_addstr(&ctx
->message
, ", reversing\nchanges made to ");
2365 refer_to_commit(opts
, &ctx
->message
, parent
);
2367 strbuf_addstr(&ctx
->message
, ".\n");
2372 base_label
= msg
.parent_label
;
2374 next_label
= msg
.label
;
2376 /* Append the commit log message to ctx->message. */
2377 if (find_commit_subject(msg
.message
, &p
))
2378 strbuf_addstr(&ctx
->message
, p
);
2380 if (opts
->record_origin
) {
2381 strbuf_complete_line(&ctx
->message
);
2382 if (!has_conforming_footer(&ctx
->message
, NULL
, 0))
2383 strbuf_addch(&ctx
->message
, '\n');
2384 strbuf_addstr(&ctx
->message
, cherry_picked_prefix
);
2385 strbuf_addstr(&ctx
->message
, oid_to_hex(&commit
->object
.oid
));
2386 strbuf_addstr(&ctx
->message
, ")\n");
2388 if (!is_fixup(command
))
2389 author
= get_author(msg
.message
);
2391 ctx
->have_message
= 1;
2393 if (command
== TODO_REWORD
)
2395 else if (is_fixup(command
)) {
2396 if (update_squash_messages(r
, command
, commit
,
2397 opts
, item
->flags
)) {
2403 msg_file
= rebase_path_squash_msg();
2404 else if (file_exists(rebase_path_fixup_msg())) {
2405 flags
|= VERBATIM_MSG
;
2406 msg_file
= rebase_path_fixup_msg();
2408 const char *dest
= git_path_squash_msg(r
);
2410 if (copy_file(dest
, rebase_path_squash_msg(), 0666)) {
2411 res
= error(_("could not copy '%s' to '%s'"),
2412 rebase_path_squash_msg(), dest
);
2415 unlink(git_path_merge_msg(r
));
2421 if (opts
->signoff
&& !is_fixup(command
))
2422 append_signoff(&ctx
->message
, 0, 0);
2424 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
2426 else if (!opts
->strategy
||
2427 !strcmp(opts
->strategy
, "recursive") ||
2428 !strcmp(opts
->strategy
, "ort") ||
2429 command
== TODO_REVERT
) {
2430 res
= do_recursive_merge(r
, base
, next
, base_label
, next_label
,
2431 &head
, &ctx
->message
, opts
);
2435 res
|= write_message(ctx
->message
.buf
, ctx
->message
.len
,
2436 git_path_merge_msg(r
), 0);
2438 struct commit_list
*common
= NULL
;
2439 struct commit_list
*remotes
= NULL
;
2441 res
= write_message(ctx
->message
.buf
, ctx
->message
.len
,
2442 git_path_merge_msg(r
), 0);
2444 commit_list_insert(base
, &common
);
2445 commit_list_insert(next
, &remotes
);
2446 res
|= try_merge_command(r
, opts
->strategy
,
2447 opts
->xopts
.nr
, opts
->xopts
.v
,
2448 common
, oid_to_hex(&head
), remotes
);
2449 free_commit_list(common
);
2450 free_commit_list(remotes
);
2454 * If the merge was clean or if it failed due to conflict, we write
2455 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
2456 * However, if the merge did not even start, then we don't want to
2459 if ((command
== TODO_PICK
|| command
== TODO_REWORD
||
2460 command
== TODO_EDIT
) && !opts
->no_commit
&&
2461 (res
== 0 || res
== 1) &&
2462 refs_update_ref(get_main_ref_store(the_repository
), NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
2463 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2465 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
2466 refs_update_ref(get_main_ref_store(the_repository
), NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
2467 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2471 error(command
== TODO_REVERT
2472 ? _("could not revert %s... %s")
2473 : _("could not apply %s... %s"),
2474 short_commit_name(r
, commit
), msg
.subject
);
2475 print_advice(r
, res
== 1, opts
);
2476 repo_rerere(r
, opts
->allow_rerere_auto
);
2481 allow
= allow_empty(r
, opts
, commit
);
2485 } else if (allow
== 1) {
2486 flags
|= ALLOW_EMPTY
;
2487 } else if (allow
== 2) {
2489 refs_delete_ref(get_main_ref_store(r
), "", "CHERRY_PICK_HEAD",
2490 NULL
, REF_NO_DEREF
);
2491 unlink(git_path_merge_msg(r
));
2492 refs_delete_ref(get_main_ref_store(r
), "", "AUTO_MERGE",
2493 NULL
, REF_NO_DEREF
);
2495 _("dropping %s %s -- patch contents already upstream\n"),
2496 oid_to_hex(&commit
->object
.oid
), msg
.subject
);
2497 } /* else allow == 0 and there's nothing special to do */
2498 if (!opts
->no_commit
&& !drop_commit
) {
2499 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
2500 res
= do_commit(r
, msg_file
, author
, opts
, flags
,
2501 commit
? &commit
->object
.oid
: NULL
);
2503 res
= error(_("unable to parse commit author"));
2504 *check_todo
= !!(flags
& EDIT_MSG
);
2505 if (!res
&& reword
) {
2507 res
= run_git_commit(NULL
, opts
, EDIT_MSG
|
2508 VERIFY_MSG
| AMEND_MSG
|
2509 (flags
& ALLOW_EMPTY
));
2515 if (!res
&& final_fixup
) {
2516 unlink(rebase_path_fixup_msg());
2517 unlink(rebase_path_squash_msg());
2518 unlink(rebase_path_current_fixups());
2519 strbuf_reset(&ctx
->current_fixups
);
2520 ctx
->current_fixup_count
= 0;
2524 free_message(commit
, &msg
);
2526 update_abort_safety_file();
2531 static int prepare_revs(struct replay_opts
*opts
)
2534 * picking (but not reverting) ranges (but not individual revisions)
2535 * should be done in reverse
2537 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
2538 opts
->revs
->reverse
^= 1;
2540 if (prepare_revision_walk(opts
->revs
))
2541 return error(_("revision walk setup failed"));
2546 static int read_and_refresh_cache(struct repository
*r
,
2547 struct replay_opts
*opts
)
2549 struct lock_file index_lock
= LOCK_INIT
;
2550 int index_fd
= repo_hold_locked_index(r
, &index_lock
, 0);
2551 if (repo_read_index(r
) < 0) {
2552 rollback_lock_file(&index_lock
);
2553 return error(_("git %s: failed to read the index"),
2556 refresh_index(r
->index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
2558 if (index_fd
>= 0) {
2559 if (write_locked_index(r
->index
, &index_lock
,
2560 COMMIT_LOCK
| SKIP_IF_UNCHANGED
)) {
2561 return error(_("git %s: failed to refresh the index"),
2567 * If we are resolving merges in any way other than "ort", then
2568 * expand the sparse index.
2570 if (opts
->strategy
&& strcmp(opts
->strategy
, "ort"))
2571 ensure_full_index(r
->index
);
2575 void todo_list_release(struct todo_list
*todo_list
)
2577 strbuf_release(&todo_list
->buf
);
2578 FREE_AND_NULL(todo_list
->items
);
2579 todo_list
->nr
= todo_list
->alloc
= 0;
2582 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
2584 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
2585 return todo_list
->items
+ todo_list
->nr
++;
2588 const char *todo_item_get_arg(struct todo_list
*todo_list
,
2589 struct todo_item
*item
)
2591 return todo_list
->buf
.buf
+ item
->arg_offset
;
2594 static int is_command(enum todo_command command
, const char **bol
)
2596 const char *str
= todo_command_info
[command
].str
;
2597 const char nick
= todo_command_info
[command
].c
;
2598 const char *p
= *bol
;
2600 return (skip_prefix(p
, str
, &p
) || (nick
&& *p
++ == nick
)) &&
2601 (*p
== ' ' || *p
== '\t' || *p
== '\n' || *p
== '\r' || !*p
) &&
2605 static int check_label_or_ref_arg(enum todo_command command
, const char *arg
)
2610 * '#' is not a valid label as the merge command uses it to
2611 * separate merge parents from the commit subject.
2613 if (!strcmp(arg
, "#") ||
2614 check_refname_format(arg
, REFNAME_ALLOW_ONELEVEL
))
2615 return error(_("'%s' is not a valid label"), arg
);
2618 case TODO_UPDATE_REF
:
2619 if (check_refname_format(arg
, REFNAME_ALLOW_ONELEVEL
))
2620 return error(_("'%s' is not a valid refname"), arg
);
2621 if (check_refname_format(arg
, 0))
2622 return error(_("update-ref requires a fully qualified "
2623 "refname e.g. refs/heads/%s"), arg
);
2627 BUG("unexpected todo_command");
2633 static int check_merge_commit_insn(enum todo_command command
)
2637 error(_("'%s' does not accept merge commits"),
2638 todo_command_info
[command
].str
);
2639 advise_if_enabled(ADVICE_REBASE_TODO_ERROR
, _(
2641 * TRANSLATORS: 'pick' and 'merge -C' should not be
2644 "'pick' does not take a merge commit. If you wanted to\n"
2645 "replay the merge, use 'merge -C' on the commit."));
2649 error(_("'%s' does not accept merge commits"),
2650 todo_command_info
[command
].str
);
2651 advise_if_enabled(ADVICE_REBASE_TODO_ERROR
, _(
2653 * TRANSLATORS: 'reword' and 'merge -c' should not be
2656 "'reword' does not take a merge commit. If you wanted to\n"
2657 "replay the merge and reword the commit message, use\n"
2658 "'merge -c' on the commit"));
2662 error(_("'%s' does not accept merge commits"),
2663 todo_command_info
[command
].str
);
2664 advise_if_enabled(ADVICE_REBASE_TODO_ERROR
, _(
2666 * TRANSLATORS: 'edit', 'merge -C' and 'break' should
2667 * not be translated.
2669 "'edit' does not take a merge commit. If you wanted to\n"
2670 "replay the merge, use 'merge -C' on the commit, and then\n"
2671 "'break' to give the control back to you so that you can\n"
2672 "do 'git commit --amend && git rebase --continue'."));
2677 return error(_("cannot squash merge commit into another commit"));
2683 BUG("unexpected todo_command");
2687 static int parse_insn_line(struct repository
*r
, struct replay_opts
*opts
,
2688 struct todo_item
*item
, const char *buf
,
2689 const char *bol
, char *eol
)
2691 struct object_id commit_oid
;
2692 char *end_of_object_name
;
2693 int i
, saved
, status
, padding
;
2698 bol
+= strspn(bol
, " \t");
2700 if (bol
== eol
|| *bol
== '\r' || starts_with_mem(bol
, eol
- bol
, comment_line_str
)) {
2701 item
->command
= TODO_COMMENT
;
2702 item
->commit
= NULL
;
2703 item
->arg_offset
= bol
- buf
;
2704 item
->arg_len
= eol
- bol
;
2708 for (i
= 0; i
< TODO_COMMENT
; i
++)
2709 if (is_command(i
, &bol
)) {
2713 if (i
>= TODO_COMMENT
)
2714 return error(_("invalid command '%.*s'"),
2715 (int)strcspn(bol
, " \t\r\n"), bol
);
2717 /* Eat up extra spaces/ tabs before object name */
2718 padding
= strspn(bol
, " \t");
2721 if (item
->command
== TODO_NOOP
|| item
->command
== TODO_BREAK
) {
2723 return error(_("%s does not accept arguments: '%s'"),
2724 command_to_string(item
->command
), bol
);
2725 item
->commit
= NULL
;
2726 item
->arg_offset
= bol
- buf
;
2727 item
->arg_len
= eol
- bol
;
2732 return error(_("missing arguments for %s"),
2733 command_to_string(item
->command
));
2735 if (item
->command
== TODO_EXEC
|| item
->command
== TODO_LABEL
||
2736 item
->command
== TODO_RESET
|| item
->command
== TODO_UPDATE_REF
) {
2739 item
->commit
= NULL
;
2740 item
->arg_offset
= bol
- buf
;
2741 item
->arg_len
= (int)(eol
- bol
);
2742 if (item
->command
== TODO_LABEL
||
2743 item
->command
== TODO_UPDATE_REF
) {
2746 ret
= check_label_or_ref_arg(item
->command
, bol
);
2752 if (item
->command
== TODO_FIXUP
) {
2753 if (skip_prefix(bol
, "-C", &bol
)) {
2754 bol
+= strspn(bol
, " \t");
2755 item
->flags
|= TODO_REPLACE_FIXUP_MSG
;
2756 } else if (skip_prefix(bol
, "-c", &bol
)) {
2757 bol
+= strspn(bol
, " \t");
2758 item
->flags
|= TODO_EDIT_FIXUP_MSG
;
2762 if (item
->command
== TODO_MERGE
) {
2763 if (skip_prefix(bol
, "-C", &bol
))
2764 bol
+= strspn(bol
, " \t");
2765 else if (skip_prefix(bol
, "-c", &bol
)) {
2766 bol
+= strspn(bol
, " \t");
2767 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2769 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2770 item
->commit
= NULL
;
2771 item
->arg_offset
= bol
- buf
;
2772 item
->arg_len
= (int)(eol
- bol
);
2777 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
2778 saved
= *end_of_object_name
;
2779 *end_of_object_name
= '\0';
2780 status
= repo_get_oid(r
, bol
, &commit_oid
);
2782 error(_("could not parse '%s'"), bol
); /* return later */
2783 *end_of_object_name
= saved
;
2785 bol
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
2786 item
->arg_offset
= bol
- buf
;
2787 item
->arg_len
= (int)(eol
- bol
);
2792 item
->commit
= lookup_commit_reference(r
, &commit_oid
);
2795 if (is_rebase_i(opts
) &&
2796 item
->commit
->parents
&& item
->commit
->parents
->next
)
2797 return check_merge_commit_insn(item
->command
);
2801 int sequencer_get_last_command(struct repository
*r UNUSED
, enum replay_action
*action
)
2803 const char *todo_file
, *bol
;
2804 struct strbuf buf
= STRBUF_INIT
;
2807 todo_file
= git_path_todo_file();
2808 if (strbuf_read_file(&buf
, todo_file
, 0) < 0) {
2809 if (errno
== ENOENT
|| errno
== ENOTDIR
)
2812 return error_errno("unable to open '%s'", todo_file
);
2814 bol
= buf
.buf
+ strspn(buf
.buf
, " \t\r\n");
2815 if (is_command(TODO_PICK
, &bol
) && (*bol
== ' ' || *bol
== '\t'))
2816 *action
= REPLAY_PICK
;
2817 else if (is_command(TODO_REVERT
, &bol
) &&
2818 (*bol
== ' ' || *bol
== '\t'))
2819 *action
= REPLAY_REVERT
;
2823 strbuf_release(&buf
);
2828 int todo_list_parse_insn_buffer(struct repository
*r
, struct replay_opts
*opts
,
2829 char *buf
, struct todo_list
*todo_list
)
2831 struct todo_item
*item
;
2832 char *p
= buf
, *next_p
;
2833 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
2835 todo_list
->current
= todo_list
->nr
= todo_list
->total_nr
= 0;
2837 for (i
= 1; *p
; i
++, p
= next_p
) {
2838 char *eol
= strchrnul(p
, '\n');
2840 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
2842 if (p
!= eol
&& eol
[-1] == '\r')
2843 eol
--; /* strip Carriage Return */
2845 item
= append_new_todo(todo_list
);
2846 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
2847 if (parse_insn_line(r
, opts
, item
, buf
, p
, eol
)) {
2848 res
= error(_("invalid line %d: %.*s"),
2849 i
, (int)(eol
- p
), p
);
2850 item
->command
= TODO_COMMENT
+ 1;
2851 item
->arg_offset
= p
- buf
;
2852 item
->arg_len
= (int)(eol
- p
);
2853 item
->commit
= NULL
;
2856 if (item
->command
!= TODO_COMMENT
)
2857 todo_list
->total_nr
++;
2861 else if (is_fixup(item
->command
))
2862 res
= error(_("cannot '%s' without a previous commit"),
2863 command_to_string(item
->command
));
2864 else if (!is_noop(item
->command
))
2871 static int count_commands(struct todo_list
*todo_list
)
2875 for (i
= 0; i
< todo_list
->nr
; i
++)
2876 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
2882 static int get_item_line_offset(struct todo_list
*todo_list
, int index
)
2884 return index
< todo_list
->nr
?
2885 todo_list
->items
[index
].offset_in_buf
: todo_list
->buf
.len
;
2888 static const char *get_item_line(struct todo_list
*todo_list
, int index
)
2890 return todo_list
->buf
.buf
+ get_item_line_offset(todo_list
, index
);
2893 static int get_item_line_length(struct todo_list
*todo_list
, int index
)
2895 return get_item_line_offset(todo_list
, index
+ 1)
2896 - get_item_line_offset(todo_list
, index
);
2899 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
2904 fd
= open(path
, O_RDONLY
);
2906 return error_errno(_("could not open '%s'"), path
);
2907 len
= strbuf_read(sb
, fd
, 0);
2910 return error(_("could not read '%s'."), path
);
2914 static int have_finished_the_last_pick(void)
2916 struct strbuf buf
= STRBUF_INIT
;
2918 const char *todo_path
= git_path_todo_file();
2921 if (strbuf_read_file(&buf
, todo_path
, 0) < 0) {
2922 if (errno
== ENOENT
) {
2925 error_errno("unable to open '%s'", todo_path
);
2929 /* If there is only one line then we are done */
2930 eol
= strchr(buf
.buf
, '\n');
2931 if (!eol
|| !eol
[1])
2934 strbuf_release(&buf
);
2939 void sequencer_post_commit_cleanup(struct repository
*r
, int verbose
)
2941 struct replay_opts opts
= REPLAY_OPTS_INIT
;
2942 int need_cleanup
= 0;
2944 if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD")) {
2945 if (!refs_delete_ref(get_main_ref_store(r
), "",
2946 "CHERRY_PICK_HEAD", NULL
, REF_NO_DEREF
) &&
2948 warning(_("cancelling a cherry picking in progress"));
2949 opts
.action
= REPLAY_PICK
;
2953 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD")) {
2954 if (!refs_delete_ref(get_main_ref_store(r
), "", "REVERT_HEAD",
2955 NULL
, REF_NO_DEREF
) &&
2957 warning(_("cancelling a revert in progress"));
2958 opts
.action
= REPLAY_REVERT
;
2962 refs_delete_ref(get_main_ref_store(r
), "", "AUTO_MERGE",
2963 NULL
, REF_NO_DEREF
);
2968 if (!have_finished_the_last_pick())
2971 sequencer_remove_state(&opts
);
2973 replay_opts_release(&opts
);
2976 static void todo_list_write_total_nr(struct todo_list
*todo_list
)
2978 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
2981 fprintf(f
, "%d\n", todo_list
->total_nr
);
2986 static int read_populate_todo(struct repository
*r
,
2987 struct todo_list
*todo_list
,
2988 struct replay_opts
*opts
)
2990 const char *todo_file
= get_todo_path(opts
);
2993 strbuf_reset(&todo_list
->buf
);
2994 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
2997 res
= todo_list_parse_insn_buffer(r
, opts
, todo_list
->buf
.buf
, todo_list
);
2999 if (is_rebase_i(opts
))
3000 return error(_("please fix this using "
3001 "'git rebase --edit-todo'."));
3002 return error(_("unusable instruction sheet: '%s'"), todo_file
);
3005 if (!todo_list
->nr
&&
3006 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
3007 return error(_("no commits parsed."));
3009 if (!is_rebase_i(opts
)) {
3010 enum todo_command valid
=
3011 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
3014 for (i
= 0; i
< todo_list
->nr
; i
++)
3015 if (valid
== todo_list
->items
[i
].command
)
3017 else if (valid
== TODO_PICK
)
3018 return error(_("cannot cherry-pick during a revert."));
3020 return error(_("cannot revert during a cherry-pick."));
3023 if (is_rebase_i(opts
)) {
3024 struct todo_list done
= TODO_LIST_INIT
;
3026 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
3027 !todo_list_parse_insn_buffer(r
, opts
, done
.buf
.buf
, &done
))
3028 todo_list
->done_nr
= count_commands(&done
);
3030 todo_list
->done_nr
= 0;
3032 todo_list
->total_nr
= todo_list
->done_nr
3033 + count_commands(todo_list
);
3034 todo_list_release(&done
);
3036 todo_list_write_total_nr(todo_list
);
3042 static int git_config_string_dup(char **dest
,
3043 const char *var
, const char *value
)
3046 return config_error_nonbool(var
);
3048 *dest
= xstrdup(value
);
3052 static int populate_opts_cb(const char *key
, const char *value
,
3053 const struct config_context
*ctx
,
3056 struct replay_opts
*opts
= data
;
3061 else if (!strcmp(key
, "options.no-commit"))
3062 opts
->no_commit
= git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3063 else if (!strcmp(key
, "options.edit"))
3064 opts
->edit
= git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3065 else if (!strcmp(key
, "options.allow-empty"))
3067 git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3068 else if (!strcmp(key
, "options.allow-empty-message"))
3069 opts
->allow_empty_message
=
3070 git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3071 else if (!strcmp(key
, "options.drop-redundant-commits"))
3072 opts
->drop_redundant_commits
=
3073 git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3074 else if (!strcmp(key
, "options.keep-redundant-commits"))
3075 opts
->keep_redundant_commits
=
3076 git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3077 else if (!strcmp(key
, "options.signoff"))
3078 opts
->signoff
= git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3079 else if (!strcmp(key
, "options.record-origin"))
3080 opts
->record_origin
= git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3081 else if (!strcmp(key
, "options.allow-ff"))
3082 opts
->allow_ff
= git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
);
3083 else if (!strcmp(key
, "options.mainline"))
3084 opts
->mainline
= git_config_int(key
, value
, ctx
->kvi
);
3085 else if (!strcmp(key
, "options.strategy"))
3086 git_config_string_dup(&opts
->strategy
, key
, value
);
3087 else if (!strcmp(key
, "options.gpg-sign"))
3088 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
3089 else if (!strcmp(key
, "options.strategy-option")) {
3090 strvec_push(&opts
->xopts
, value
);
3091 } else if (!strcmp(key
, "options.allow-rerere-auto"))
3092 opts
->allow_rerere_auto
=
3093 git_config_bool_or_int(key
, value
, ctx
->kvi
, &error_flag
) ?
3094 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
3095 else if (!strcmp(key
, "options.default-msg-cleanup")) {
3096 opts
->explicit_cleanup
= 1;
3097 opts
->default_msg_cleanup
= get_cleanup_mode(value
, 1);
3099 return error(_("invalid key: %s"), key
);
3102 return error(_("invalid value for '%s': '%s'"), key
, value
);
3107 static void parse_strategy_opts(struct replay_opts
*opts
, char *raw_opts
)
3112 char *strategy_opts_string
= raw_opts
;
3114 if (*strategy_opts_string
== ' ')
3115 strategy_opts_string
++;
3117 count
= split_cmdline(strategy_opts_string
, &argv
);
3119 BUG("could not split '%s': %s", strategy_opts_string
,
3120 split_cmdline_strerror(count
));
3121 for (i
= 0; i
< count
; i
++) {
3122 const char *arg
= argv
[i
];
3124 skip_prefix(arg
, "--", &arg
);
3125 strvec_push(&opts
->xopts
, arg
);
3130 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
3133 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
3135 opts
->strategy
= strbuf_detach(buf
, NULL
);
3136 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
3139 parse_strategy_opts(opts
, buf
->buf
);
3142 static int read_populate_opts(struct replay_opts
*opts
)
3144 struct replay_ctx
*ctx
= opts
->ctx
;
3146 if (is_rebase_i(opts
)) {
3147 struct strbuf buf
= STRBUF_INIT
;
3150 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(),
3151 READ_ONELINER_SKIP_IF_EMPTY
)) {
3152 if (!starts_with(buf
.buf
, "-S"))
3155 free(opts
->gpg_sign
);
3156 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
3161 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(),
3162 READ_ONELINER_SKIP_IF_EMPTY
)) {
3163 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
3164 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
3165 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
3166 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
3170 if (file_exists(rebase_path_verbose()))
3173 if (file_exists(rebase_path_quiet()))
3176 if (file_exists(rebase_path_signoff())) {
3181 if (file_exists(rebase_path_cdate_is_adate())) {
3183 opts
->committer_date_is_author_date
= 1;
3186 if (file_exists(rebase_path_ignore_date())) {
3188 opts
->ignore_date
= 1;
3191 if (file_exists(rebase_path_reschedule_failed_exec()))
3192 opts
->reschedule_failed_exec
= 1;
3193 else if (file_exists(rebase_path_no_reschedule_failed_exec()))
3194 opts
->reschedule_failed_exec
= 0;
3196 if (file_exists(rebase_path_drop_redundant_commits()))
3197 opts
->drop_redundant_commits
= 1;
3199 if (file_exists(rebase_path_keep_redundant_commits()))
3200 opts
->keep_redundant_commits
= 1;
3202 read_strategy_opts(opts
, &buf
);
3205 if (read_oneliner(&ctx
->current_fixups
,
3206 rebase_path_current_fixups(),
3207 READ_ONELINER_SKIP_IF_EMPTY
)) {
3208 const char *p
= ctx
->current_fixups
.buf
;
3209 ctx
->current_fixup_count
= 1;
3210 while ((p
= strchr(p
, '\n'))) {
3211 ctx
->current_fixup_count
++;
3216 if (read_oneliner(&buf
, rebase_path_squash_onto(), 0)) {
3217 if (repo_get_oid_committish(the_repository
, buf
.buf
, &opts
->squash_onto
) < 0) {
3218 ret
= error(_("unusable squash-onto"));
3221 opts
->have_squash_onto
= 1;
3225 strbuf_release(&buf
);
3229 if (!file_exists(git_path_opts_file()))
3232 * The function git_parse_source(), called from git_config_from_file(),
3233 * may die() in case of a syntactically incorrect file. We do not care
3234 * about this case, though, because we wrote that file ourselves, so we
3235 * are pretty certain that it is syntactically correct.
3237 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
3238 return error(_("malformed options sheet: '%s'"),
3239 git_path_opts_file());
3243 static void write_strategy_opts(struct replay_opts
*opts
)
3245 struct strbuf buf
= STRBUF_INIT
;
3248 * Quote strategy options so that they can be read correctly
3249 * by split_cmdline().
3251 quote_cmdline(&buf
, opts
->xopts
.v
);
3252 write_file(rebase_path_strategy_opts(), "%s\n", buf
.buf
);
3253 strbuf_release(&buf
);
3256 int write_basic_state(struct replay_opts
*opts
, const char *head_name
,
3257 struct commit
*onto
, const struct object_id
*orig_head
)
3260 write_file(rebase_path_head_name(), "%s\n", head_name
);
3262 write_file(rebase_path_onto(), "%s\n",
3263 oid_to_hex(&onto
->object
.oid
));
3265 write_file(rebase_path_orig_head(), "%s\n",
3266 oid_to_hex(orig_head
));
3269 write_file(rebase_path_quiet(), "%s", "");
3271 write_file(rebase_path_verbose(), "%s", "");
3273 write_file(rebase_path_strategy(), "%s\n", opts
->strategy
);
3274 if (opts
->xopts
.nr
> 0)
3275 write_strategy_opts(opts
);
3277 if (opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
)
3278 write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
3279 else if (opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
)
3280 write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
3283 write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts
->gpg_sign
);
3285 write_file(rebase_path_signoff(), "--signoff\n");
3286 if (opts
->drop_redundant_commits
)
3287 write_file(rebase_path_drop_redundant_commits(), "%s", "");
3288 if (opts
->keep_redundant_commits
)
3289 write_file(rebase_path_keep_redundant_commits(), "%s", "");
3290 if (opts
->committer_date_is_author_date
)
3291 write_file(rebase_path_cdate_is_adate(), "%s", "");
3292 if (opts
->ignore_date
)
3293 write_file(rebase_path_ignore_date(), "%s", "");
3294 if (opts
->reschedule_failed_exec
)
3295 write_file(rebase_path_reschedule_failed_exec(), "%s", "");
3297 write_file(rebase_path_no_reschedule_failed_exec(), "%s", "");
3302 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
3303 struct replay_opts
*opts
)
3305 enum todo_command command
= opts
->action
== REPLAY_PICK
?
3306 TODO_PICK
: TODO_REVERT
;
3307 const char *command_string
= todo_command_info
[command
].str
;
3308 const char *encoding
;
3309 struct commit
*commit
;
3311 if (prepare_revs(opts
))
3314 encoding
= get_log_output_encoding();
3316 while ((commit
= get_revision(opts
->revs
))) {
3317 struct todo_item
*item
= append_new_todo(todo_list
);
3318 const char *commit_buffer
= repo_logmsg_reencode(the_repository
,
3321 const char *subject
;
3324 item
->command
= command
;
3325 item
->commit
= commit
;
3326 item
->arg_offset
= 0;
3328 item
->offset_in_buf
= todo_list
->buf
.len
;
3329 subject_len
= find_commit_subject(commit_buffer
, &subject
);
3330 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
3331 short_commit_name(the_repository
, commit
),
3332 subject_len
, subject
);
3333 repo_unuse_commit_buffer(the_repository
, commit
,
3338 return error(_("empty commit set passed"));
3343 static int create_seq_dir(struct repository
*r
)
3345 enum replay_action action
;
3346 const char *in_progress_error
= NULL
;
3347 const char *in_progress_advice
= NULL
;
3348 unsigned int advise_skip
=
3349 refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") ||
3350 refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD");
3352 if (!sequencer_get_last_command(r
, &action
)) {
3355 in_progress_error
= _("revert is already in progress");
3356 in_progress_advice
=
3357 _("try \"git revert (--continue | %s--abort | --quit)\"");
3360 in_progress_error
= _("cherry-pick is already in progress");
3361 in_progress_advice
=
3362 _("try \"git cherry-pick (--continue | %s--abort | --quit)\"");
3365 BUG("unexpected action in create_seq_dir");
3368 if (in_progress_error
) {
3369 error("%s", in_progress_error
);
3370 if (advice_enabled(ADVICE_SEQUENCER_IN_USE
))
3371 advise(in_progress_advice
,
3372 advise_skip
? "--skip | " : "");
3375 if (mkdir(git_path_seq_dir(), 0777) < 0)
3376 return error_errno(_("could not create sequencer directory '%s'"),
3377 git_path_seq_dir());
3382 static int save_head(const char *head
)
3384 return write_message(head
, strlen(head
), git_path_head_file(), 1);
3387 static int rollback_is_safe(void)
3389 struct strbuf sb
= STRBUF_INIT
;
3390 struct object_id expected_head
, actual_head
;
3392 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
3394 if (get_oid_hex(sb
.buf
, &expected_head
)) {
3395 strbuf_release(&sb
);
3396 die(_("could not parse %s"), git_path_abort_safety_file());
3398 strbuf_release(&sb
);
3400 else if (errno
== ENOENT
)
3401 oidclr(&expected_head
, the_repository
->hash_algo
);
3403 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
3405 if (repo_get_oid(the_repository
, "HEAD", &actual_head
))
3406 oidclr(&actual_head
, the_repository
->hash_algo
);
3408 return oideq(&actual_head
, &expected_head
);
3411 static int reset_merge(const struct object_id
*oid
)
3413 struct child_process cmd
= CHILD_PROCESS_INIT
;
3416 strvec_pushl(&cmd
.args
, "reset", "--merge", NULL
);
3418 if (!is_null_oid(oid
))
3419 strvec_push(&cmd
.args
, oid_to_hex(oid
));
3421 return run_command(&cmd
);
3424 static int rollback_single_pick(struct repository
*r
)
3426 struct object_id head_oid
;
3428 if (!refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
3429 !refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD"))
3430 return error(_("no cherry-pick or revert in progress"));
3431 if (refs_read_ref_full(get_main_ref_store(the_repository
), "HEAD", 0, &head_oid
, NULL
))
3432 return error(_("cannot resolve HEAD"));
3433 if (is_null_oid(&head_oid
))
3434 return error(_("cannot abort from a branch yet to be born"));
3435 return reset_merge(&head_oid
);
3438 static int skip_single_pick(void)
3440 struct object_id head
;
3442 if (refs_read_ref_full(get_main_ref_store(the_repository
), "HEAD", 0, &head
, NULL
))
3443 return error(_("cannot resolve HEAD"));
3444 return reset_merge(&head
);
3447 int sequencer_rollback(struct repository
*r
, struct replay_opts
*opts
)
3450 struct object_id oid
;
3451 struct strbuf buf
= STRBUF_INIT
;
3454 f
= fopen(git_path_head_file(), "r");
3455 if (!f
&& errno
== ENOENT
) {
3457 * There is no multiple-cherry-pick in progress.
3458 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
3459 * a single-cherry-pick in progress, abort that.
3461 return rollback_single_pick(r
);
3464 return error_errno(_("cannot open '%s'"), git_path_head_file());
3465 if (strbuf_getline_lf(&buf
, f
)) {
3466 error(_("cannot read '%s': %s"), git_path_head_file(),
3467 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
3472 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
3473 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
3474 git_path_head_file());
3477 if (is_null_oid(&oid
)) {
3478 error(_("cannot abort from a branch yet to be born"));
3482 if (!rollback_is_safe()) {
3483 /* Do not error, just do not rollback */
3484 warning(_("You seem to have moved HEAD. "
3485 "Not rewinding, check your HEAD!"));
3487 if (reset_merge(&oid
))
3489 strbuf_release(&buf
);
3490 return sequencer_remove_state(opts
);
3492 strbuf_release(&buf
);
3496 int sequencer_skip(struct repository
*r
, struct replay_opts
*opts
)
3498 enum replay_action action
= -1;
3499 sequencer_get_last_command(r
, &action
);
3502 * Check whether the subcommand requested to skip the commit is actually
3503 * in progress and that it's safe to skip the commit.
3505 * opts->action tells us which subcommand requested to skip the commit.
3506 * If the corresponding .git/<ACTION>_HEAD exists, we know that the
3507 * action is in progress and we can skip the commit.
3509 * Otherwise we check that the last instruction was related to the
3510 * particular subcommand we're trying to execute and barf if that's not
3513 * Finally we check that the rollback is "safe", i.e., has the HEAD
3514 * moved? In this case, it doesn't make sense to "reset the merge" and
3515 * "skip the commit" as the user already handled this by committing. But
3516 * we'd not want to barf here, instead give advice on how to proceed. We
3517 * only need to check that when .git/<ACTION>_HEAD doesn't exist because
3518 * it gets removed when the user commits, so if it still exists we're
3519 * sure the user can't have committed before.
3521 switch (opts
->action
) {
3523 if (!refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD")) {
3524 if (action
!= REPLAY_REVERT
)
3525 return error(_("no revert in progress"));
3526 if (!rollback_is_safe())
3531 if (!refs_ref_exists(get_main_ref_store(r
),
3532 "CHERRY_PICK_HEAD")) {
3533 if (action
!= REPLAY_PICK
)
3534 return error(_("no cherry-pick in progress"));
3535 if (!rollback_is_safe())
3540 BUG("unexpected action in sequencer_skip");
3543 if (skip_single_pick())
3544 return error(_("failed to skip the commit"));
3545 if (!is_directory(git_path_seq_dir()))
3548 return sequencer_continue(r
, opts
);
3551 error(_("there is nothing to skip"));
3553 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
)) {
3554 advise(_("have you committed already?\n"
3555 "try \"git %s --continue\""),
3556 action
== REPLAY_REVERT
? "revert" : "cherry-pick");
3561 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
,
3564 struct lock_file todo_lock
= LOCK_INIT
;
3565 const char *todo_path
= get_todo_path(opts
);
3566 int next
= todo_list
->current
, offset
, fd
;
3569 * rebase -i writes "git-rebase-todo" without the currently executing
3570 * command, appending it to "done" instead.
3572 if (is_rebase_i(opts
) && !reschedule
)
3575 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
3577 return error_errno(_("could not lock '%s'"), todo_path
);
3578 offset
= get_item_line_offset(todo_list
, next
);
3579 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
3580 todo_list
->buf
.len
- offset
) < 0)
3581 return error_errno(_("could not write to '%s'"), todo_path
);
3582 if (commit_lock_file(&todo_lock
) < 0)
3583 return error(_("failed to finalize '%s'"), todo_path
);
3585 if (is_rebase_i(opts
) && !reschedule
&& next
> 0) {
3586 const char *done
= rebase_path_done();
3587 int fd
= open(done
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
3592 if (write_in_full(fd
, get_item_line(todo_list
, next
- 1),
3593 get_item_line_length(todo_list
, next
- 1))
3595 ret
= error_errno(_("could not write to '%s'"), done
);
3597 ret
= error_errno(_("failed to finalize '%s'"), done
);
3603 static int save_opts(struct replay_opts
*opts
)
3605 const char *opts_file
= git_path_opts_file();
3608 if (opts
->no_commit
)
3609 res
|= git_config_set_in_file_gently(opts_file
,
3610 "options.no-commit", NULL
, "true");
3611 if (opts
->edit
>= 0)
3612 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", NULL
,
3613 opts
->edit
? "true" : "false");
3614 if (opts
->allow_empty
)
3615 res
|= git_config_set_in_file_gently(opts_file
,
3616 "options.allow-empty", NULL
, "true");
3617 if (opts
->allow_empty_message
)
3618 res
|= git_config_set_in_file_gently(opts_file
,
3619 "options.allow-empty-message", NULL
, "true");
3620 if (opts
->drop_redundant_commits
)
3621 res
|= git_config_set_in_file_gently(opts_file
,
3622 "options.drop-redundant-commits", NULL
, "true");
3623 if (opts
->keep_redundant_commits
)
3624 res
|= git_config_set_in_file_gently(opts_file
,
3625 "options.keep-redundant-commits", NULL
, "true");
3627 res
|= git_config_set_in_file_gently(opts_file
,
3628 "options.signoff", NULL
, "true");
3629 if (opts
->record_origin
)
3630 res
|= git_config_set_in_file_gently(opts_file
,
3631 "options.record-origin", NULL
, "true");
3633 res
|= git_config_set_in_file_gently(opts_file
,
3634 "options.allow-ff", NULL
, "true");
3635 if (opts
->mainline
) {
3636 struct strbuf buf
= STRBUF_INIT
;
3637 strbuf_addf(&buf
, "%d", opts
->mainline
);
3638 res
|= git_config_set_in_file_gently(opts_file
,
3639 "options.mainline", NULL
, buf
.buf
);
3640 strbuf_release(&buf
);
3643 res
|= git_config_set_in_file_gently(opts_file
,
3644 "options.strategy", NULL
, opts
->strategy
);
3646 res
|= git_config_set_in_file_gently(opts_file
,
3647 "options.gpg-sign", NULL
, opts
->gpg_sign
);
3648 for (size_t i
= 0; i
< opts
->xopts
.nr
; i
++)
3649 res
|= git_config_set_multivar_in_file_gently(opts_file
,
3650 "options.strategy-option",
3651 opts
->xopts
.v
[i
], "^$", NULL
, 0);
3652 if (opts
->allow_rerere_auto
)
3653 res
|= git_config_set_in_file_gently(opts_file
,
3654 "options.allow-rerere-auto", NULL
,
3655 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
3658 if (opts
->explicit_cleanup
)
3659 res
|= git_config_set_in_file_gently(opts_file
,
3660 "options.default-msg-cleanup", NULL
,
3661 describe_cleanup_mode(opts
->default_msg_cleanup
));
3665 static int make_patch(struct repository
*r
,
3666 struct commit
*commit
,
3667 struct replay_opts
*opts
)
3669 struct rev_info log_tree_opt
;
3670 const char *subject
;
3671 char hex
[GIT_MAX_HEXSZ
+ 1];
3674 if (!is_rebase_i(opts
))
3675 BUG("make_patch should only be called when rebasing");
3677 oid_to_hex_r(hex
, &commit
->object
.oid
);
3678 if (write_message(hex
, strlen(hex
), rebase_path_stopped_sha(), 1) < 0)
3680 res
|= write_rebase_head(&commit
->object
.oid
);
3682 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
3683 repo_init_revisions(r
, &log_tree_opt
, NULL
);
3684 log_tree_opt
.abbrev
= 0;
3685 log_tree_opt
.diff
= 1;
3686 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
3687 log_tree_opt
.disable_stdin
= 1;
3688 log_tree_opt
.no_commit_id
= 1;
3689 log_tree_opt
.diffopt
.file
= fopen(rebase_path_patch(), "w");
3690 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
3691 if (!log_tree_opt
.diffopt
.file
)
3692 res
|= error_errno(_("could not open '%s'"),
3693 rebase_path_patch());
3695 res
|= log_tree_commit(&log_tree_opt
, commit
);
3696 fclose(log_tree_opt
.diffopt
.file
);
3699 if (!file_exists(rebase_path_message())) {
3700 const char *encoding
= get_commit_output_encoding();
3701 const char *commit_buffer
= repo_logmsg_reencode(r
,
3704 find_commit_subject(commit_buffer
, &subject
);
3705 res
|= write_message(subject
, strlen(subject
), rebase_path_message(), 1);
3706 repo_unuse_commit_buffer(r
, commit
,
3709 release_revisions(&log_tree_opt
);
3714 static int intend_to_amend(void)
3716 struct object_id head
;
3719 if (repo_get_oid(the_repository
, "HEAD", &head
))
3720 return error(_("cannot read HEAD"));
3722 p
= oid_to_hex(&head
);
3723 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
3726 static int error_with_patch(struct repository
*r
,
3727 struct commit
*commit
,
3728 const char *subject
, int subject_len
,
3729 struct replay_opts
*opts
,
3730 int exit_code
, int to_amend
)
3732 struct replay_ctx
*ctx
= opts
->ctx
;
3735 * Write the commit message to be used by "git rebase
3736 * --continue". If a "fixup" or "squash" command has conflicts
3737 * then we will have already written rebase_path_message() in
3738 * error_failed_squash(). If an "edit" command was
3739 * fast-forwarded then we don't have a message in ctx->message
3740 * and rely on make_patch() to write rebase_path_message()
3743 if (ctx
->have_message
&& !file_exists(rebase_path_message()) &&
3744 write_message(ctx
->message
.buf
, ctx
->message
.len
,
3745 rebase_path_message(), 0))
3746 return error(_("could not write commit message file"));
3748 if (commit
&& make_patch(r
, commit
, opts
))
3752 if (intend_to_amend())
3756 _("You can amend the commit now, with\n"
3758 " git commit --amend %s\n"
3760 "Once you are satisfied with your changes, run\n"
3762 " git rebase --continue\n"),
3763 gpg_sign_opt_quoted(opts
));
3764 } else if (exit_code
) {
3766 fprintf_ln(stderr
, _("Could not apply %s... %.*s"),
3767 short_commit_name(r
, commit
), subject_len
, subject
);
3770 * We don't have the hash of the parent so
3771 * just print the line from the todo file.
3773 fprintf_ln(stderr
, _("Could not merge %.*s"),
3774 subject_len
, subject
);
3780 static int error_failed_squash(struct repository
*r
,
3781 struct commit
*commit
,
3782 struct replay_opts
*opts
,
3784 const char *subject
)
3786 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
3787 return error(_("could not copy '%s' to '%s'"),
3788 rebase_path_squash_msg(), rebase_path_message());
3789 unlink(git_path_merge_msg(r
));
3790 if (copy_file(git_path_merge_msg(r
), rebase_path_message(), 0666))
3791 return error(_("could not copy '%s' to '%s'"),
3792 rebase_path_message(),
3793 git_path_merge_msg(r
));
3794 return error_with_patch(r
, commit
, subject
, subject_len
, opts
, 1, 0);
3797 static int do_exec(struct repository
*r
, const char *command_line
, int quiet
)
3799 struct child_process cmd
= CHILD_PROCESS_INIT
;
3803 fprintf(stderr
, _("Executing: %s\n"), command_line
);
3805 strvec_push(&cmd
.args
, command_line
);
3806 strvec_push(&cmd
.env
, "GIT_CHERRY_PICK_HELP");
3807 status
= run_command(&cmd
);
3809 /* force re-reading of the cache */
3810 discard_index(r
->index
);
3811 if (repo_read_index(r
) < 0)
3812 return error(_("could not read index"));
3814 dirty
= require_clean_work_tree(r
, "rebase", NULL
, 1, 1);
3817 warning(_("execution failed: %s\n%s"
3818 "You can fix the problem, and then run\n"
3820 " git rebase --continue\n"
3823 dirty
? _("and made changes to the index and/or the "
3824 "working tree.\n") : "");
3826 /* command not found */
3829 warning(_("execution succeeded: %s\nbut "
3830 "left changes to the index and/or the working tree.\n"
3831 "Commit or stash your changes, and then run\n"
3833 " git rebase --continue\n"
3834 "\n"), command_line
);
3841 __attribute__((format (printf
, 2, 3)))
3842 static int safe_append(const char *filename
, const char *fmt
, ...)
3845 struct lock_file lock
= LOCK_INIT
;
3846 int fd
= hold_lock_file_for_update(&lock
, filename
,
3847 LOCK_REPORT_ON_ERROR
);
3848 struct strbuf buf
= STRBUF_INIT
;
3853 if (strbuf_read_file(&buf
, filename
, 0) < 0 && errno
!= ENOENT
) {
3854 error_errno(_("could not read '%s'"), filename
);
3855 rollback_lock_file(&lock
);
3858 strbuf_complete(&buf
, '\n');
3860 strbuf_vaddf(&buf
, fmt
, ap
);
3863 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
3864 error_errno(_("could not write to '%s'"), filename
);
3865 strbuf_release(&buf
);
3866 rollback_lock_file(&lock
);
3869 if (commit_lock_file(&lock
) < 0) {
3870 strbuf_release(&buf
);
3871 return error(_("failed to finalize '%s'"), filename
);
3874 strbuf_release(&buf
);
3878 static int do_label(struct repository
*r
, const char *name
, int len
)
3880 struct ref_store
*refs
= get_main_ref_store(r
);
3881 struct ref_transaction
*transaction
;
3882 struct strbuf ref_name
= STRBUF_INIT
, err
= STRBUF_INIT
;
3883 struct strbuf msg
= STRBUF_INIT
;
3885 struct object_id head_oid
;
3887 if (len
== 1 && *name
== '#')
3888 return error(_("illegal label name: '%.*s'"), len
, name
);
3890 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
3891 strbuf_addf(&msg
, "rebase (label) '%.*s'", len
, name
);
3893 transaction
= ref_store_transaction_begin(refs
, &err
);
3895 error("%s", err
.buf
);
3897 } else if (repo_get_oid(r
, "HEAD", &head_oid
)) {
3898 error(_("could not read HEAD"));
3900 } else if (ref_transaction_update(transaction
, ref_name
.buf
,
3901 &head_oid
, NULL
, NULL
, NULL
,
3902 0, msg
.buf
, &err
) < 0 ||
3903 ref_transaction_commit(transaction
, &err
)) {
3904 error("%s", err
.buf
);
3907 ref_transaction_free(transaction
);
3908 strbuf_release(&err
);
3909 strbuf_release(&msg
);
3912 ret
= safe_append(rebase_path_refs_to_delete(),
3913 "%s\n", ref_name
.buf
);
3914 strbuf_release(&ref_name
);
3919 static const char *sequencer_reflog_action(struct replay_opts
*opts
)
3921 if (!opts
->reflog_action
) {
3922 opts
->reflog_action
= getenv(GIT_REFLOG_ACTION
);
3923 opts
->reflog_action
=
3924 xstrdup(opts
->reflog_action
? opts
->reflog_action
3925 : action_name(opts
));
3928 return opts
->reflog_action
;
3931 __attribute__((format (printf
, 3, 4)))
3932 static const char *reflog_message(struct replay_opts
*opts
,
3933 const char *sub_action
, const char *fmt
, ...)
3936 static struct strbuf buf
= STRBUF_INIT
;
3940 strbuf_addstr(&buf
, sequencer_reflog_action(opts
));
3942 strbuf_addf(&buf
, " (%s)", sub_action
);
3944 strbuf_addstr(&buf
, ": ");
3945 strbuf_vaddf(&buf
, fmt
, ap
);
3952 static struct commit
*lookup_label(struct repository
*r
, const char *label
,
3953 int len
, struct strbuf
*buf
)
3955 struct commit
*commit
;
3956 struct object_id oid
;
3959 strbuf_addf(buf
, "refs/rewritten/%.*s", len
, label
);
3960 if (!refs_read_ref(get_main_ref_store(the_repository
), buf
->buf
, &oid
)) {
3961 commit
= lookup_commit_object(r
, &oid
);
3963 /* fall back to non-rewritten ref or commit */
3964 strbuf_splice(buf
, 0, strlen("refs/rewritten/"), "", 0);
3965 commit
= lookup_commit_reference_by_name(buf
->buf
);
3969 error(_("could not resolve '%s'"), buf
->buf
);
3974 static int do_reset(struct repository
*r
,
3975 const char *name
, int len
,
3976 struct replay_opts
*opts
)
3978 struct strbuf ref_name
= STRBUF_INIT
;
3979 struct object_id oid
;
3980 struct lock_file lock
= LOCK_INIT
;
3981 struct tree_desc desc
= { 0 };
3983 struct unpack_trees_options unpack_tree_opts
= { 0 };
3986 if (repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0)
3989 if (len
== 10 && !strncmp("[new root]", name
, len
)) {
3990 if (!opts
->have_squash_onto
) {
3992 if (commit_tree("", 0, the_hash_algo
->empty_tree
,
3993 NULL
, &opts
->squash_onto
,
3995 return error(_("writing fake root commit"));
3996 opts
->have_squash_onto
= 1;
3997 hex
= oid_to_hex(&opts
->squash_onto
);
3998 if (write_message(hex
, strlen(hex
),
3999 rebase_path_squash_onto(), 0))
4000 return error(_("writing squash-onto"));
4002 oidcpy(&oid
, &opts
->squash_onto
);
4005 struct commit
*commit
;
4007 /* Determine the length of the label */
4008 for (i
= 0; i
< len
; i
++)
4009 if (isspace(name
[i
]))
4013 commit
= lookup_label(r
, name
, len
, &ref_name
);
4018 oid
= commit
->object
.oid
;
4021 setup_unpack_trees_porcelain(&unpack_tree_opts
, "reset");
4022 unpack_tree_opts
.head_idx
= 1;
4023 unpack_tree_opts
.src_index
= r
->index
;
4024 unpack_tree_opts
.dst_index
= r
->index
;
4025 unpack_tree_opts
.fn
= oneway_merge
;
4026 unpack_tree_opts
.merge
= 1;
4027 unpack_tree_opts
.update
= 1;
4028 unpack_tree_opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
4029 unpack_tree_opts
.skip_cache_tree_update
= 1;
4030 init_checkout_metadata(&unpack_tree_opts
.meta
, name
, &oid
, NULL
);
4032 if (repo_read_index_unmerged(r
)) {
4033 ret
= error_resolve_conflict(action_name(opts
));
4037 if (!fill_tree_descriptor(r
, &desc
, &oid
)) {
4038 ret
= error(_("failed to find tree of %s"), oid_to_hex(&oid
));
4042 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
4047 tree
= parse_tree_indirect(&oid
);
4049 return error(_("unable to read tree (%s)"), oid_to_hex(&oid
));
4050 prime_cache_tree(r
, r
->index
, tree
);
4052 if (write_locked_index(r
->index
, &lock
, COMMIT_LOCK
) < 0)
4053 ret
= error(_("could not write index"));
4056 ret
= refs_update_ref(get_main_ref_store(the_repository
), reflog_message(opts
, "reset", "'%.*s'",
4059 NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
4061 free((void *)desc
.buffer
);
4063 rollback_lock_file(&lock
);
4064 strbuf_release(&ref_name
);
4065 clear_unpack_trees_porcelain(&unpack_tree_opts
);
4069 static int do_merge(struct repository
*r
,
4070 struct commit
*commit
,
4071 const char *arg
, int arg_len
,
4072 int flags
, int *check_todo
, struct replay_opts
*opts
)
4074 struct replay_ctx
*ctx
= opts
->ctx
;
4075 int run_commit_flags
= 0;
4076 struct strbuf ref_name
= STRBUF_INIT
;
4077 struct commit
*head_commit
, *merge_commit
, *i
;
4078 struct commit_list
*bases
= NULL
, *j
;
4079 struct commit_list
*to_merge
= NULL
, **tail
= &to_merge
;
4080 const char *strategy
= !opts
->xopts
.nr
&&
4082 !strcmp(opts
->strategy
, "recursive") ||
4083 !strcmp(opts
->strategy
, "ort")) ?
4084 NULL
: opts
->strategy
;
4085 struct merge_options o
;
4086 int merge_arg_len
, oneline_offset
, can_fast_forward
, ret
, k
;
4087 static struct lock_file lock
;
4090 if (repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0) {
4095 head_commit
= lookup_commit_reference_by_name("HEAD");
4097 ret
= error(_("cannot merge without a current revision"));
4102 * For octopus merges, the arg starts with the list of revisions to be
4103 * merged. The list is optionally followed by '#' and the oneline.
4105 merge_arg_len
= oneline_offset
= arg_len
;
4106 for (p
= arg
; p
- arg
< arg_len
; p
+= strspn(p
, " \t\n")) {
4109 if (*p
== '#' && (!p
[1] || isspace(p
[1]))) {
4110 p
+= 1 + strspn(p
+ 1, " \t\n");
4111 oneline_offset
= p
- arg
;
4114 k
= strcspn(p
, " \t\n");
4117 merge_commit
= lookup_label(r
, p
, k
, &ref_name
);
4118 if (!merge_commit
) {
4119 ret
= error(_("unable to parse '%.*s'"), k
, p
);
4122 tail
= &commit_list_insert(merge_commit
, tail
)->next
;
4124 merge_arg_len
= p
- arg
;
4128 ret
= error(_("nothing to merge: '%.*s'"), arg_len
, arg
);
4132 if (opts
->have_squash_onto
&&
4133 oideq(&head_commit
->object
.oid
, &opts
->squash_onto
)) {
4135 * When the user tells us to "merge" something into a
4136 * "[new root]", let's simply fast-forward to the merge head.
4138 rollback_lock_file(&lock
);
4140 ret
= error(_("octopus merge cannot be executed on "
4141 "top of a [new root]"));
4143 ret
= fast_forward_to(r
, &to_merge
->item
->object
.oid
,
4144 &head_commit
->object
.oid
, 0,
4150 * If HEAD is not identical to the first parent of the original merge
4151 * commit, we cannot fast-forward.
4153 can_fast_forward
= opts
->allow_ff
&& commit
&& commit
->parents
&&
4154 oideq(&commit
->parents
->item
->object
.oid
,
4155 &head_commit
->object
.oid
);
4158 * If any merge head is different from the original one, we cannot
4161 if (can_fast_forward
) {
4162 struct commit_list
*p
= commit
->parents
->next
;
4164 for (j
= to_merge
; j
&& p
; j
= j
->next
, p
= p
->next
)
4165 if (!oideq(&j
->item
->object
.oid
,
4166 &p
->item
->object
.oid
)) {
4167 can_fast_forward
= 0;
4171 * If the number of merge heads differs from the original merge
4172 * commit, we cannot fast-forward.
4175 can_fast_forward
= 0;
4178 if (can_fast_forward
) {
4179 rollback_lock_file(&lock
);
4180 ret
= fast_forward_to(r
, &commit
->object
.oid
,
4181 &head_commit
->object
.oid
, 0, opts
);
4182 if (flags
& TODO_EDIT_MERGE_MSG
)
4183 goto fast_forward_edit
;
4189 const char *encoding
= get_commit_output_encoding();
4190 const char *message
= repo_logmsg_reencode(r
, commit
, NULL
,
4196 ret
= error(_("could not get commit message of '%s'"),
4197 oid_to_hex(&commit
->object
.oid
));
4200 write_author_script(message
);
4201 find_commit_subject(message
, &body
);
4203 strbuf_add(&ctx
->message
, body
, len
);
4204 repo_unuse_commit_buffer(r
, commit
, message
);
4206 struct strbuf buf
= STRBUF_INIT
;
4208 strbuf_addf(&buf
, "author %s", git_author_info(0));
4209 write_author_script(buf
.buf
);
4210 strbuf_release(&buf
);
4212 if (oneline_offset
< arg_len
) {
4213 strbuf_add(&ctx
->message
, arg
+ oneline_offset
,
4214 arg_len
- oneline_offset
);
4216 strbuf_addf(&ctx
->message
, "Merge %s '%.*s'",
4217 to_merge
->next
? "branches" : "branch",
4218 merge_arg_len
, arg
);
4221 ctx
->have_message
= 1;
4222 if (write_message(ctx
->message
.buf
, ctx
->message
.len
,
4223 git_path_merge_msg(r
), 0)) {
4224 ret
= error_errno(_("could not write '%s'"),
4225 git_path_merge_msg(r
));
4229 if (strategy
|| to_merge
->next
) {
4231 struct child_process cmd
= CHILD_PROCESS_INIT
;
4233 if (read_env_script(&cmd
.env
)) {
4234 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
4236 ret
= error(_(staged_changes_advice
), gpg_opt
, gpg_opt
);
4240 if (opts
->committer_date_is_author_date
)
4241 strvec_pushf(&cmd
.env
, "GIT_COMMITTER_DATE=%s",
4244 author_date_from_env(&cmd
.env
));
4245 if (opts
->ignore_date
)
4246 strvec_push(&cmd
.env
, "GIT_AUTHOR_DATE=");
4249 strvec_push(&cmd
.args
, "merge");
4250 strvec_push(&cmd
.args
, "-s");
4252 strvec_push(&cmd
.args
, "octopus");
4254 strvec_push(&cmd
.args
, strategy
);
4255 for (k
= 0; k
< opts
->xopts
.nr
; k
++)
4256 strvec_pushf(&cmd
.args
,
4257 "-X%s", opts
->xopts
.v
[k
]);
4259 if (!(flags
& TODO_EDIT_MERGE_MSG
))
4260 strvec_push(&cmd
.args
, "--no-edit");
4262 strvec_push(&cmd
.args
, "--edit");
4263 strvec_push(&cmd
.args
, "--no-ff");
4264 strvec_push(&cmd
.args
, "--no-log");
4265 strvec_push(&cmd
.args
, "--no-stat");
4266 strvec_push(&cmd
.args
, "-F");
4267 strvec_push(&cmd
.args
, git_path_merge_msg(r
));
4269 strvec_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
4271 strvec_push(&cmd
.args
, "--no-gpg-sign");
4273 /* Add the tips to be merged */
4274 for (j
= to_merge
; j
; j
= j
->next
)
4275 strvec_push(&cmd
.args
,
4276 oid_to_hex(&j
->item
->object
.oid
));
4278 strbuf_release(&ref_name
);
4279 refs_delete_ref(get_main_ref_store(r
), "", "CHERRY_PICK_HEAD",
4280 NULL
, REF_NO_DEREF
);
4281 rollback_lock_file(&lock
);
4283 ret
= run_command(&cmd
);
4285 /* force re-reading of the cache */
4287 discard_index(r
->index
);
4288 if (repo_read_index(r
) < 0)
4289 ret
= error(_("could not read index"));
4294 merge_commit
= to_merge
->item
;
4295 if (repo_get_merge_bases(r
, head_commit
, merge_commit
, &bases
) < 0) {
4300 if (bases
&& oideq(&merge_commit
->object
.oid
,
4301 &bases
->item
->object
.oid
)) {
4303 /* skip merging an ancestor of HEAD */
4307 write_message(oid_to_hex(&merge_commit
->object
.oid
), the_hash_algo
->hexsz
,
4308 git_path_merge_head(r
), 0);
4309 write_message("no-ff", 5, git_path_merge_mode(r
), 0);
4311 bases
= reverse_commit_list(bases
);
4314 init_ui_merge_options(&o
, r
);
4316 o
.branch2
= ref_name
.buf
;
4317 o
.buffer_output
= 2;
4319 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "ort")) {
4321 * TODO: Should use merge_incore_recursive() and
4322 * merge_switch_to_result(), skipping the call to
4323 * merge_switch_to_result() when we don't actually need to
4324 * update the index and working copy immediately.
4326 ret
= merge_ort_recursive(&o
,
4327 head_commit
, merge_commit
, bases
,
4330 ret
= merge_recursive(&o
, head_commit
, merge_commit
, bases
,
4334 fputs(o
.obuf
.buf
, stdout
);
4335 strbuf_release(&o
.obuf
);
4337 error(_("could not even attempt to merge '%.*s'"),
4338 merge_arg_len
, arg
);
4339 unlink(git_path_merge_msg(r
));
4343 * The return value of merge_recursive() is 1 on clean, and 0 on
4346 * Let's reverse that, so that do_merge() returns 0 upon success and
4347 * 1 upon failed merge (keeping the return value -1 for the cases where
4348 * we will want to reschedule the `merge` command).
4352 if (r
->index
->cache_changed
&&
4353 write_locked_index(r
->index
, &lock
, COMMIT_LOCK
)) {
4354 ret
= error(_("merge: Unable to write new index file"));
4358 rollback_lock_file(&lock
);
4360 repo_rerere(r
, opts
->allow_rerere_auto
);
4363 * In case of problems, we now want to return a positive
4364 * value (a negative one would indicate that the `merge`
4365 * command needs to be rescheduled).
4367 ret
= !!run_git_commit(git_path_merge_msg(r
), opts
,
4370 if (!ret
&& flags
& TODO_EDIT_MERGE_MSG
) {
4373 run_commit_flags
|= AMEND_MSG
| EDIT_MSG
| VERIFY_MSG
;
4374 ret
= !!run_git_commit(NULL
, opts
, run_commit_flags
);
4379 strbuf_release(&ref_name
);
4380 rollback_lock_file(&lock
);
4381 free_commit_list(to_merge
);
4382 free_commit_list(bases
);
4386 static int write_update_refs_state(struct string_list
*refs_to_oids
)
4389 struct lock_file lock
= LOCK_INIT
;
4391 struct string_list_item
*item
;
4394 path
= rebase_path_update_refs(the_repository
->gitdir
);
4396 if (!refs_to_oids
->nr
) {
4397 if (unlink(path
) && errno
!= ENOENT
)
4398 result
= error_errno(_("could not unlink: %s"), path
);
4402 if (safe_create_leading_directories(path
)) {
4403 result
= error(_("unable to create leading directories of %s"),
4408 if (hold_lock_file_for_update(&lock
, path
, 0) < 0) {
4409 result
= error(_("another 'rebase' process appears to be running; "
4410 "'%s.lock' already exists"),
4415 fp
= fdopen_lock_file(&lock
, "w");
4417 result
= error_errno(_("could not open '%s' for writing"), path
);
4418 rollback_lock_file(&lock
);
4422 for_each_string_list_item(item
, refs_to_oids
) {
4423 struct update_ref_record
*rec
= item
->util
;
4424 fprintf(fp
, "%s\n%s\n%s\n", item
->string
,
4425 oid_to_hex(&rec
->before
), oid_to_hex(&rec
->after
));
4428 result
= commit_lock_file(&lock
);
4436 * Parse the update-refs file for the current rebase, then remove the
4437 * refs that do not appear in the todo_list (and have not had updated
4438 * values stored) and add refs that are in the todo_list but not
4439 * represented in the update-refs file.
4441 * If there are changes to the update-refs list, then write the new state
4444 void todo_list_filter_update_refs(struct repository
*r
,
4445 struct todo_list
*todo_list
)
4449 struct string_list update_refs
= STRING_LIST_INIT_DUP
;
4451 sequencer_get_update_refs_state(r
->gitdir
, &update_refs
);
4454 * For each item in the update_refs list, if it has no updated
4455 * value and does not appear in the todo_list, then remove it
4456 * from the update_refs list.
4458 for (i
= 0; i
< update_refs
.nr
; i
++) {
4461 const char *ref
= update_refs
.items
[i
].string
;
4462 size_t reflen
= strlen(ref
);
4463 struct update_ref_record
*rec
= update_refs
.items
[i
].util
;
4465 /* OID already stored as updated. */
4466 if (!is_null_oid(&rec
->after
))
4469 for (j
= 0; !found
&& j
< todo_list
->nr
; j
++) {
4470 struct todo_item
*item
= &todo_list
->items
[j
];
4471 const char *arg
= todo_list
->buf
.buf
+ item
->arg_offset
;
4473 if (item
->command
!= TODO_UPDATE_REF
)
4476 if (item
->arg_len
!= reflen
||
4477 strncmp(arg
, ref
, reflen
))
4484 free(update_refs
.items
[i
].string
);
4485 free(update_refs
.items
[i
].util
);
4488 MOVE_ARRAY(update_refs
.items
+ i
, update_refs
.items
+ i
+ 1, update_refs
.nr
- i
);
4496 * For each todo_item, check if its ref is in the update_refs list.
4497 * If not, then add it as an un-updated ref.
4499 for (i
= 0; i
< todo_list
->nr
; i
++) {
4500 struct todo_item
*item
= &todo_list
->items
[i
];
4501 const char *arg
= todo_list
->buf
.buf
+ item
->arg_offset
;
4504 if (item
->command
!= TODO_UPDATE_REF
)
4507 for (j
= 0; !found
&& j
< update_refs
.nr
; j
++) {
4508 const char *ref
= update_refs
.items
[j
].string
;
4510 found
= strlen(ref
) == item
->arg_len
&&
4511 !strncmp(ref
, arg
, item
->arg_len
);
4515 struct string_list_item
*inserted
;
4516 struct strbuf argref
= STRBUF_INIT
;
4518 strbuf_add(&argref
, arg
, item
->arg_len
);
4519 inserted
= string_list_insert(&update_refs
, argref
.buf
);
4520 inserted
->util
= init_update_ref_record(argref
.buf
);
4521 strbuf_release(&argref
);
4527 write_update_refs_state(&update_refs
);
4528 string_list_clear(&update_refs
, 1);
4531 static int do_update_ref(struct repository
*r
, const char *refname
)
4533 struct string_list_item
*item
;
4534 struct string_list list
= STRING_LIST_INIT_DUP
;
4536 if (sequencer_get_update_refs_state(r
->gitdir
, &list
))
4539 for_each_string_list_item(item
, &list
) {
4540 if (!strcmp(item
->string
, refname
)) {
4541 struct update_ref_record
*rec
= item
->util
;
4542 if (refs_read_ref(get_main_ref_store(the_repository
), "HEAD", &rec
->after
))
4548 write_update_refs_state(&list
);
4549 string_list_clear(&list
, 1);
4553 static int do_update_refs(struct repository
*r
, int quiet
)
4556 struct string_list_item
*item
;
4557 struct string_list refs_to_oids
= STRING_LIST_INIT_DUP
;
4558 struct ref_store
*refs
= get_main_ref_store(r
);
4559 struct strbuf update_msg
= STRBUF_INIT
;
4560 struct strbuf error_msg
= STRBUF_INIT
;
4562 if ((res
= sequencer_get_update_refs_state(r
->gitdir
, &refs_to_oids
)))
4565 for_each_string_list_item(item
, &refs_to_oids
) {
4566 struct update_ref_record
*rec
= item
->util
;
4569 loop_res
= refs_update_ref(refs
, "rewritten during rebase",
4571 &rec
->after
, &rec
->before
,
4572 0, UPDATE_REFS_MSG_ON_ERR
);
4579 strbuf_addf(&error_msg
, "\t%s\n", item
->string
);
4581 strbuf_addf(&update_msg
, "\t%s\n", item
->string
);
4585 (update_msg
.len
|| error_msg
.len
)) {
4587 _("Updated the following refs with %s:\n%s"),
4593 _("Failed to update the following refs with %s:\n%s"),
4598 string_list_clear(&refs_to_oids
, 1);
4599 strbuf_release(&update_msg
);
4600 strbuf_release(&error_msg
);
4604 static int is_final_fixup(struct todo_list
*todo_list
)
4606 int i
= todo_list
->current
;
4608 if (!is_fixup(todo_list
->items
[i
].command
))
4611 while (++i
< todo_list
->nr
)
4612 if (is_fixup(todo_list
->items
[i
].command
))
4614 else if (!is_noop(todo_list
->items
[i
].command
))
4619 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
4623 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
4624 if (!is_noop(todo_list
->items
[i
].command
))
4625 return todo_list
->items
[i
].command
;
4630 static void create_autostash_internal(struct repository
*r
,
4632 const char *refname
)
4634 struct strbuf buf
= STRBUF_INIT
;
4635 struct lock_file lock_file
= LOCK_INIT
;
4638 if (path
&& refname
)
4639 BUG("can only pass path or refname");
4641 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
4642 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
4644 repo_update_index_if_able(r
, &lock_file
);
4645 rollback_lock_file(&lock_file
);
4647 if (has_unstaged_changes(r
, 1) ||
4648 has_uncommitted_changes(r
, 1)) {
4649 struct child_process stash
= CHILD_PROCESS_INIT
;
4650 struct reset_head_opts ropts
= { .flags
= RESET_HEAD_HARD
};
4651 struct object_id oid
;
4653 strvec_pushl(&stash
.args
,
4654 "stash", "create", "autostash", NULL
);
4658 if (capture_command(&stash
, &buf
, GIT_MAX_HEXSZ
))
4659 die(_("Cannot autostash"));
4660 strbuf_trim_trailing_newline(&buf
);
4661 if (repo_get_oid(r
, buf
.buf
, &oid
))
4662 die(_("Unexpected stash response: '%s'"),
4665 strbuf_add_unique_abbrev(&buf
, &oid
, DEFAULT_ABBREV
);
4668 if (safe_create_leading_directories_const(path
))
4669 die(_("Could not create directory for '%s'"),
4671 write_file(path
, "%s", oid_to_hex(&oid
));
4673 refs_update_ref(get_main_ref_store(r
), "", refname
,
4674 &oid
, null_oid(), 0, UPDATE_REFS_DIE_ON_ERR
);
4677 printf(_("Created autostash: %s\n"), buf
.buf
);
4678 if (reset_head(r
, &ropts
) < 0)
4679 die(_("could not reset --hard"));
4680 discard_index(r
->index
);
4681 if (repo_read_index(r
) < 0)
4682 die(_("could not read index"));
4684 strbuf_release(&buf
);
4687 void create_autostash(struct repository
*r
, const char *path
)
4689 create_autostash_internal(r
, path
, NULL
);
4692 void create_autostash_ref(struct repository
*r
, const char *refname
)
4694 create_autostash_internal(r
, NULL
, refname
);
4697 static int apply_save_autostash_oid(const char *stash_oid
, int attempt_apply
)
4699 struct child_process child
= CHILD_PROCESS_INIT
;
4702 if (attempt_apply
) {
4704 child
.no_stdout
= 1;
4705 child
.no_stderr
= 1;
4706 strvec_push(&child
.args
, "stash");
4707 strvec_push(&child
.args
, "apply");
4708 strvec_push(&child
.args
, stash_oid
);
4709 ret
= run_command(&child
);
4712 if (attempt_apply
&& !ret
)
4713 fprintf(stderr
, _("Applied autostash.\n"));
4715 struct child_process store
= CHILD_PROCESS_INIT
;
4718 strvec_push(&store
.args
, "stash");
4719 strvec_push(&store
.args
, "store");
4720 strvec_push(&store
.args
, "-m");
4721 strvec_push(&store
.args
, "autostash");
4722 strvec_push(&store
.args
, "-q");
4723 strvec_push(&store
.args
, stash_oid
);
4724 if (run_command(&store
))
4725 ret
= error(_("cannot store %s"), stash_oid
);
4729 "Your changes are safe in the stash.\n"
4730 "You can run \"git stash pop\" or"
4731 " \"git stash drop\" at any time.\n"),
4733 _("Applying autostash resulted in conflicts.") :
4734 _("Autostash exists; creating a new stash entry."));
4740 static int apply_save_autostash(const char *path
, int attempt_apply
)
4742 struct strbuf stash_oid
= STRBUF_INIT
;
4745 if (!read_oneliner(&stash_oid
, path
,
4746 READ_ONELINER_SKIP_IF_EMPTY
)) {
4747 strbuf_release(&stash_oid
);
4750 strbuf_trim(&stash_oid
);
4752 ret
= apply_save_autostash_oid(stash_oid
.buf
, attempt_apply
);
4755 strbuf_release(&stash_oid
);
4759 int save_autostash(const char *path
)
4761 return apply_save_autostash(path
, 0);
4764 int apply_autostash(const char *path
)
4766 return apply_save_autostash(path
, 1);
4769 int apply_autostash_oid(const char *stash_oid
)
4771 return apply_save_autostash_oid(stash_oid
, 1);
4774 static int apply_save_autostash_ref(struct repository
*r
, const char *refname
,
4777 struct object_id stash_oid
;
4778 char stash_oid_hex
[GIT_MAX_HEXSZ
+ 1];
4781 if (!refs_ref_exists(get_main_ref_store(r
), refname
))
4784 if (!refs_resolve_ref_unsafe(get_main_ref_store(r
), refname
,
4785 RESOLVE_REF_READING
, &stash_oid
, &flag
))
4787 if (flag
& REF_ISSYMREF
)
4788 return error(_("autostash reference is a symref"));
4790 oid_to_hex_r(stash_oid_hex
, &stash_oid
);
4791 ret
= apply_save_autostash_oid(stash_oid_hex
, attempt_apply
);
4793 refs_delete_ref(get_main_ref_store(r
), "", refname
,
4794 &stash_oid
, REF_NO_DEREF
);
4799 int save_autostash_ref(struct repository
*r
, const char *refname
)
4801 return apply_save_autostash_ref(r
, refname
, 0);
4804 int apply_autostash_ref(struct repository
*r
, const char *refname
)
4806 return apply_save_autostash_ref(r
, refname
, 1);
4809 static int checkout_onto(struct repository
*r
, struct replay_opts
*opts
,
4810 const char *onto_name
, const struct object_id
*onto
,
4811 const struct object_id
*orig_head
)
4813 struct reset_head_opts ropts
= {
4815 .orig_head
= orig_head
,
4816 .flags
= RESET_HEAD_DETACH
| RESET_ORIG_HEAD
|
4817 RESET_HEAD_RUN_POST_CHECKOUT_HOOK
,
4818 .head_msg
= reflog_message(opts
, "start", "checkout %s",
4820 .default_reflog_action
= sequencer_reflog_action(opts
)
4822 if (reset_head(r
, &ropts
)) {
4823 apply_autostash(rebase_path_autostash());
4824 sequencer_remove_state(opts
);
4825 return error(_("could not detach HEAD"));
4831 static int stopped_at_head(struct repository
*r
)
4833 struct object_id head
;
4834 struct commit
*commit
;
4835 struct commit_message message
;
4837 if (repo_get_oid(r
, "HEAD", &head
) ||
4838 !(commit
= lookup_commit(r
, &head
)) ||
4839 repo_parse_commit(r
, commit
) || get_message(commit
, &message
))
4840 fprintf(stderr
, _("Stopped at HEAD\n"));
4842 fprintf(stderr
, _("Stopped at %s\n"), message
.label
);
4843 free_message(commit
, &message
);
4849 static int reread_todo_if_changed(struct repository
*r
,
4850 struct todo_list
*todo_list
,
4851 struct replay_opts
*opts
)
4854 struct strbuf buf
= STRBUF_INIT
;
4856 if (strbuf_read_file_or_whine(&buf
, get_todo_path(opts
)) < 0)
4858 offset
= get_item_line_offset(todo_list
, todo_list
->current
+ 1);
4859 if (buf
.len
!= todo_list
->buf
.len
- offset
||
4860 memcmp(buf
.buf
, todo_list
->buf
.buf
+ offset
, buf
.len
)) {
4861 /* Reread the todo file if it has changed. */
4862 todo_list_release(todo_list
);
4863 if (read_populate_todo(r
, todo_list
, opts
))
4864 return -1; /* message was printed */
4865 /* `current` will be incremented on return */
4866 todo_list
->current
= -1;
4868 strbuf_release(&buf
);
4873 static const char rescheduled_advice
[] =
4874 N_("Could not execute the todo command\n"
4878 "It has been rescheduled; To edit the command before continuing, please\n"
4879 "edit the todo list first:\n"
4881 " git rebase --edit-todo\n"
4882 " git rebase --continue\n");
4884 static int pick_one_commit(struct repository
*r
,
4885 struct todo_list
*todo_list
,
4886 struct replay_opts
*opts
,
4887 int *check_todo
, int* reschedule
)
4889 struct replay_ctx
*ctx
= opts
->ctx
;
4891 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
4892 const char *arg
= todo_item_get_arg(todo_list
, item
);
4893 if (is_rebase_i(opts
))
4894 ctx
->reflog_message
= reflog_message(
4895 opts
, command_to_string(item
->command
), NULL
);
4897 res
= do_pick_commit(r
, item
, opts
, is_final_fixup(todo_list
),
4899 if (is_rebase_i(opts
) && res
< 0) {
4904 if (item
->command
== TODO_EDIT
) {
4905 struct commit
*commit
= item
->commit
;
4909 fprintf(stderr
, _("Stopped at %s... %.*s\n"),
4910 short_commit_name(r
, commit
), item
->arg_len
, arg
);
4912 return error_with_patch(r
, commit
,
4913 arg
, item
->arg_len
, opts
, res
, !res
);
4915 if (is_rebase_i(opts
) && !res
)
4916 record_in_rewritten(&item
->commit
->object
.oid
,
4917 peek_command(todo_list
, 1));
4918 if (res
&& is_fixup(item
->command
)) {
4921 return error_failed_squash(r
, item
->commit
, opts
,
4922 item
->arg_len
, arg
);
4923 } else if (res
&& is_rebase_i(opts
) && item
->commit
) {
4925 struct object_id oid
;
4928 * If we are rewording and have either
4929 * fast-forwarded already, or are about to
4930 * create a new root commit, we want to amend,
4931 * otherwise we do not.
4933 if (item
->command
== TODO_REWORD
&&
4934 !repo_get_oid(r
, "HEAD", &oid
) &&
4935 (oideq(&item
->commit
->object
.oid
, &oid
) ||
4936 (opts
->have_squash_onto
&&
4937 oideq(&opts
->squash_onto
, &oid
))))
4940 return res
| error_with_patch(r
, item
->commit
,
4941 arg
, item
->arg_len
, opts
,
4947 static int pick_commits(struct repository
*r
,
4948 struct todo_list
*todo_list
,
4949 struct replay_opts
*opts
)
4951 struct replay_ctx
*ctx
= opts
->ctx
;
4952 int res
= 0, reschedule
= 0;
4954 ctx
->reflog_message
= sequencer_reflog_action(opts
);
4956 assert(!(opts
->signoff
|| opts
->no_commit
||
4957 opts
->record_origin
|| should_edit(opts
) ||
4958 opts
->committer_date_is_author_date
||
4959 opts
->ignore_date
));
4960 if (read_and_refresh_cache(r
, opts
))
4963 unlink(rebase_path_message());
4964 unlink(rebase_path_stopped_sha());
4965 unlink(rebase_path_amend());
4966 unlink(rebase_path_patch());
4968 while (todo_list
->current
< todo_list
->nr
) {
4969 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
4970 const char *arg
= todo_item_get_arg(todo_list
, item
);
4973 if (save_todo(todo_list
, opts
, reschedule
))
4975 if (is_rebase_i(opts
)) {
4976 if (item
->command
!= TODO_COMMENT
) {
4977 FILE *f
= fopen(rebase_path_msgnum(), "w");
4979 todo_list
->done_nr
++;
4982 fprintf(f
, "%d\n", todo_list
->done_nr
);
4986 fprintf(stderr
, _("Rebasing (%d/%d)%s"),
4988 todo_list
->total_nr
,
4989 opts
->verbose
? "\n" : "\r");
4991 unlink(rebase_path_author_script());
4992 unlink(git_path_merge_head(r
));
4993 refs_delete_ref(get_main_ref_store(r
), "", "AUTO_MERGE",
4994 NULL
, REF_NO_DEREF
);
4995 refs_delete_ref(get_main_ref_store(r
), "", "REBASE_HEAD",
4996 NULL
, REF_NO_DEREF
);
4998 if (item
->command
== TODO_BREAK
) {
5001 return stopped_at_head(r
);
5004 strbuf_reset(&ctx
->message
);
5005 ctx
->have_message
= 0;
5006 if (item
->command
<= TODO_SQUASH
) {
5007 res
= pick_one_commit(r
, todo_list
, opts
, &check_todo
,
5009 if (!res
&& item
->command
== TODO_EDIT
)
5011 } else if (item
->command
== TODO_EXEC
) {
5012 char *end_of_arg
= (char *)(arg
+ item
->arg_len
);
5013 int saved
= *end_of_arg
;
5018 res
= do_exec(r
, arg
, opts
->quiet
);
5019 *end_of_arg
= saved
;
5022 if (opts
->reschedule_failed_exec
)
5026 } else if (item
->command
== TODO_LABEL
) {
5027 if ((res
= do_label(r
, arg
, item
->arg_len
)))
5029 } else if (item
->command
== TODO_RESET
) {
5030 if ((res
= do_reset(r
, arg
, item
->arg_len
, opts
)))
5032 } else if (item
->command
== TODO_MERGE
) {
5033 if ((res
= do_merge(r
, item
->commit
, arg
, item
->arg_len
,
5034 item
->flags
, &check_todo
, opts
)) < 0)
5036 else if (item
->commit
)
5037 record_in_rewritten(&item
->commit
->object
.oid
,
5038 peek_command(todo_list
, 1));
5040 /* failed with merge conflicts */
5041 return error_with_patch(r
, item
->commit
,
5044 } else if (item
->command
== TODO_UPDATE_REF
) {
5045 struct strbuf ref
= STRBUF_INIT
;
5046 strbuf_add(&ref
, arg
, item
->arg_len
);
5047 if ((res
= do_update_ref(r
, ref
.buf
)))
5049 strbuf_release(&ref
);
5050 } else if (!is_noop(item
->command
))
5051 return error(_("unknown command %d"), item
->command
);
5054 advise(_(rescheduled_advice
),
5055 get_item_line_length(todo_list
,
5056 todo_list
->current
),
5057 get_item_line(todo_list
, todo_list
->current
));
5058 if (save_todo(todo_list
, opts
, reschedule
))
5061 write_rebase_head(&item
->commit
->object
.oid
);
5062 } else if (is_rebase_i(opts
) && check_todo
&& !res
&&
5063 reread_todo_if_changed(r
, todo_list
, opts
)) {
5070 todo_list
->current
++;
5073 if (is_rebase_i(opts
)) {
5074 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
5077 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
5078 starts_with(head_ref
.buf
, "refs/")) {
5080 struct object_id head
, orig
;
5083 if (repo_get_oid(r
, "HEAD", &head
)) {
5084 res
= error(_("cannot read HEAD"));
5086 strbuf_release(&head_ref
);
5087 strbuf_release(&buf
);
5090 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
5091 get_oid_hex(buf
.buf
, &orig
)) {
5092 res
= error(_("could not read orig-head"));
5093 goto cleanup_head_ref
;
5096 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
5097 res
= error(_("could not read 'onto'"));
5098 goto cleanup_head_ref
;
5100 msg
= reflog_message(opts
, "finish", "%s onto %s",
5101 head_ref
.buf
, buf
.buf
);
5102 if (refs_update_ref(get_main_ref_store(the_repository
), msg
, head_ref
.buf
, &head
, &orig
,
5103 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
5104 res
= error(_("could not update %s"),
5106 goto cleanup_head_ref
;
5108 msg
= reflog_message(opts
, "finish", "returning to %s",
5110 if (refs_update_symref(get_main_ref_store(the_repository
), "HEAD", head_ref
.buf
, msg
)) {
5111 res
= error(_("could not update HEAD to %s"),
5113 goto cleanup_head_ref
;
5118 if (opts
->verbose
) {
5119 struct rev_info log_tree_opt
;
5120 struct object_id orig
, head
;
5122 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
5123 repo_init_revisions(r
, &log_tree_opt
, NULL
);
5124 log_tree_opt
.diff
= 1;
5125 log_tree_opt
.diffopt
.output_format
=
5126 DIFF_FORMAT_DIFFSTAT
;
5127 log_tree_opt
.disable_stdin
= 1;
5129 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
5130 !repo_get_oid(r
, buf
.buf
, &orig
) &&
5131 !repo_get_oid(r
, "HEAD", &head
)) {
5132 diff_tree_oid(&orig
, &head
, "",
5133 &log_tree_opt
.diffopt
);
5134 log_tree_diff_flush(&log_tree_opt
);
5136 release_revisions(&log_tree_opt
);
5138 flush_rewritten_pending();
5139 if (!stat(rebase_path_rewritten_list(), &st
) &&
5141 struct child_process child
= CHILD_PROCESS_INIT
;
5142 struct run_hooks_opt hook_opt
= RUN_HOOKS_OPT_INIT
;
5144 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
5146 strvec_push(&child
.args
, "notes");
5147 strvec_push(&child
.args
, "copy");
5148 strvec_push(&child
.args
, "--for-rewrite=rebase");
5149 /* we don't care if this copying failed */
5150 run_command(&child
);
5152 hook_opt
.path_to_stdin
= rebase_path_rewritten_list();
5153 strvec_push(&hook_opt
.args
, "rebase");
5154 run_hooks_opt(r
, "post-rewrite", &hook_opt
);
5156 apply_autostash(rebase_path_autostash());
5162 _("Successfully rebased and updated %s.\n"),
5166 strbuf_release(&buf
);
5167 strbuf_release(&head_ref
);
5169 if (do_update_refs(r
, opts
->quiet
))
5174 * Sequence of picks finished successfully; cleanup by
5175 * removing the .git/sequencer directory
5177 return sequencer_remove_state(opts
);
5180 static int continue_single_pick(struct repository
*r
, struct replay_opts
*opts
)
5182 struct child_process cmd
= CHILD_PROCESS_INIT
;
5184 if (!refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
5185 !refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD"))
5186 return error(_("no cherry-pick or revert in progress"));
5189 strvec_push(&cmd
.args
, "commit");
5192 * continue_single_pick() handles the case of recovering from a
5193 * conflict. should_edit() doesn't handle that case; for a conflict,
5194 * we want to edit if the user asked for it, or if they didn't specify
5195 * and stdin is a tty.
5197 if (!opts
->edit
|| (opts
->edit
< 0 && !isatty(0)))
5199 * Include --cleanup=strip as well because we don't want the
5200 * "# Conflicts:" messages.
5202 strvec_pushl(&cmd
.args
, "--no-edit", "--cleanup=strip", NULL
);
5204 return run_command(&cmd
);
5207 static int commit_staged_changes(struct repository
*r
,
5208 struct replay_opts
*opts
,
5209 struct todo_list
*todo_list
)
5211 struct replay_ctx
*ctx
= opts
->ctx
;
5212 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
5213 unsigned int final_fixup
= 0, is_clean
;
5214 struct strbuf rev
= STRBUF_INIT
;
5217 if (has_unstaged_changes(r
, 1)) {
5218 ret
= error(_("cannot rebase: You have unstaged changes."));
5222 is_clean
= !has_uncommitted_changes(r
, 0);
5224 if (!is_clean
&& !file_exists(rebase_path_message())) {
5225 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
5226 ret
= error(_(staged_changes_advice
), gpg_opt
, gpg_opt
);
5230 if (file_exists(rebase_path_amend())) {
5231 struct object_id head
, to_amend
;
5233 if (repo_get_oid(r
, "HEAD", &head
)) {
5234 ret
= error(_("cannot amend non-existing commit"));
5238 if (!read_oneliner(&rev
, rebase_path_amend(), 0)) {
5239 ret
= error(_("invalid file: '%s'"), rebase_path_amend());
5243 if (get_oid_hex(rev
.buf
, &to_amend
)) {
5244 ret
= error(_("invalid contents: '%s'"),
5245 rebase_path_amend());
5248 if (!is_clean
&& !oideq(&head
, &to_amend
)) {
5249 ret
= error(_("\nYou have uncommitted changes in your "
5250 "working tree. Please, commit them\n"
5251 "first and then run 'git rebase "
5252 "--continue' again."));
5256 * When skipping a failed fixup/squash, we need to edit the
5257 * commit message, the current fixup list and count, and if it
5258 * was the last fixup/squash in the chain, we need to clean up
5259 * the commit message and if there was a squash, let the user
5262 if (!is_clean
|| !ctx
->current_fixup_count
)
5263 ; /* this is not the final fixup */
5264 else if (!oideq(&head
, &to_amend
) ||
5265 !file_exists(rebase_path_stopped_sha())) {
5266 /* was a final fixup or squash done manually? */
5267 if (!is_fixup(peek_command(todo_list
, 0))) {
5268 unlink(rebase_path_fixup_msg());
5269 unlink(rebase_path_squash_msg());
5270 unlink(rebase_path_current_fixups());
5271 strbuf_reset(&ctx
->current_fixups
);
5272 ctx
->current_fixup_count
= 0;
5275 /* we are in a fixup/squash chain */
5276 const char *p
= ctx
->current_fixups
.buf
;
5277 int len
= ctx
->current_fixups
.len
;
5279 ctx
->current_fixup_count
--;
5281 BUG("Incorrect current_fixups:\n%s", p
);
5282 while (len
&& p
[len
- 1] != '\n')
5284 strbuf_setlen(&ctx
->current_fixups
, len
);
5285 if (write_message(p
, len
, rebase_path_current_fixups(),
5287 ret
= error(_("could not write file: '%s'"),
5288 rebase_path_current_fixups());
5293 * If a fixup/squash in a fixup/squash chain failed, the
5294 * commit message is already correct, no need to commit
5297 * Only if it is the final command in the fixup/squash
5298 * chain, and only if the chain is longer than a single
5299 * fixup/squash command (which was just skipped), do we
5300 * actually need to re-commit with a cleaned up commit
5303 if (ctx
->current_fixup_count
> 0 &&
5304 !is_fixup(peek_command(todo_list
, 0))) {
5307 * If there was not a single "squash" in the
5308 * chain, we only need to clean up the commit
5309 * message, no need to bother the user with
5310 * opening the commit message in the editor.
5312 if (!starts_with(p
, "squash ") &&
5313 !strstr(p
, "\nsquash "))
5314 flags
= (flags
& ~EDIT_MSG
) | CLEANUP_MSG
;
5315 } else if (is_fixup(peek_command(todo_list
, 0))) {
5317 * We need to update the squash message to skip
5318 * the latest commit message.
5320 struct commit
*commit
;
5322 const char *path
= rebase_path_squash_msg();
5323 const char *encoding
= get_commit_output_encoding();
5325 if (parse_head(r
, &commit
)) {
5326 ret
= error(_("could not parse HEAD"));
5330 p
= repo_logmsg_reencode(r
, commit
, NULL
, encoding
);
5332 ret
= error(_("could not parse commit %s"),
5333 oid_to_hex(&commit
->object
.oid
));
5334 goto unuse_commit_buffer
;
5336 find_commit_subject(p
, &msg
);
5337 if (write_message(msg
, strlen(msg
), path
, 0)) {
5338 ret
= error(_("could not write file: "
5340 goto unuse_commit_buffer
;
5345 unuse_commit_buffer
:
5346 repo_unuse_commit_buffer(r
, commit
, p
);
5356 if (refs_ref_exists(get_main_ref_store(r
),
5357 "CHERRY_PICK_HEAD") &&
5358 refs_delete_ref(get_main_ref_store(r
), "",
5359 "CHERRY_PICK_HEAD", NULL
, REF_NO_DEREF
)) {
5360 ret
= error(_("could not remove CHERRY_PICK_HEAD"));
5364 if (unlink(git_path_merge_msg(r
)) && errno
!= ENOENT
) {
5365 ret
= error_errno(_("could not remove '%s'"),
5366 git_path_merge_msg(r
));
5376 if (run_git_commit(final_fixup
? NULL
: rebase_path_message(),
5378 ret
= error(_("could not commit staged changes."));
5382 unlink(rebase_path_amend());
5383 unlink(git_path_merge_head(r
));
5384 refs_delete_ref(get_main_ref_store(r
), "", "AUTO_MERGE",
5385 NULL
, REF_NO_DEREF
);
5387 unlink(rebase_path_fixup_msg());
5388 unlink(rebase_path_squash_msg());
5390 if (ctx
->current_fixup_count
> 0) {
5392 * Whether final fixup or not, we just cleaned up the commit
5395 unlink(rebase_path_current_fixups());
5396 strbuf_reset(&ctx
->current_fixups
);
5397 ctx
->current_fixup_count
= 0;
5403 strbuf_release(&rev
);
5407 int sequencer_continue(struct repository
*r
, struct replay_opts
*opts
)
5409 struct replay_ctx
*ctx
= opts
->ctx
;
5410 struct todo_list todo_list
= TODO_LIST_INIT
;
5413 if (read_and_refresh_cache(r
, opts
))
5416 if (read_populate_opts(opts
))
5418 if (is_rebase_i(opts
)) {
5419 if ((res
= read_populate_todo(r
, &todo_list
, opts
)))
5420 goto release_todo_list
;
5422 if (file_exists(rebase_path_dropped())) {
5423 if ((res
= todo_list_check_against_backup(r
, opts
,
5425 goto release_todo_list
;
5427 unlink(rebase_path_dropped());
5430 ctx
->reflog_message
= reflog_message(opts
, "continue", NULL
);
5431 if (commit_staged_changes(r
, opts
, &todo_list
)) {
5433 goto release_todo_list
;
5435 } else if (!file_exists(get_todo_path(opts
)))
5436 return continue_single_pick(r
, opts
);
5437 else if ((res
= read_populate_todo(r
, &todo_list
, opts
)))
5438 goto release_todo_list
;
5440 if (!is_rebase_i(opts
)) {
5441 /* Verify that the conflict has been resolved */
5442 if (refs_ref_exists(get_main_ref_store(r
),
5443 "CHERRY_PICK_HEAD") ||
5444 refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD")) {
5445 res
= continue_single_pick(r
, opts
);
5447 goto release_todo_list
;
5449 if (index_differs_from(r
, "HEAD", NULL
, 0)) {
5450 res
= error_dirty_index(r
, opts
);
5451 goto release_todo_list
;
5453 todo_list
.current
++;
5454 } else if (file_exists(rebase_path_stopped_sha())) {
5455 struct strbuf buf
= STRBUF_INIT
;
5456 struct object_id oid
;
5458 if (read_oneliner(&buf
, rebase_path_stopped_sha(),
5459 READ_ONELINER_SKIP_IF_EMPTY
) &&
5460 !get_oid_hex(buf
.buf
, &oid
))
5461 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
5462 strbuf_release(&buf
);
5465 res
= pick_commits(r
, &todo_list
, opts
);
5467 todo_list_release(&todo_list
);
5471 static int single_pick(struct repository
*r
,
5472 struct commit
*cmit
,
5473 struct replay_opts
*opts
)
5476 struct todo_item item
;
5478 item
.command
= opts
->action
== REPLAY_PICK
?
5479 TODO_PICK
: TODO_REVERT
;
5482 opts
->ctx
->reflog_message
= sequencer_reflog_action(opts
);
5483 return do_pick_commit(r
, &item
, opts
, 0, &check_todo
);
5486 int sequencer_pick_revisions(struct repository
*r
,
5487 struct replay_opts
*opts
)
5489 struct todo_list todo_list
= TODO_LIST_INIT
;
5490 struct object_id oid
;
5494 if (read_and_refresh_cache(r
, opts
)) {
5499 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
5500 struct object_id oid
;
5501 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
5503 /* This happens when using --stdin. */
5507 if (!repo_get_oid(r
, name
, &oid
)) {
5508 if (!lookup_commit_reference_gently(r
, &oid
, 1)) {
5509 enum object_type type
= oid_object_info(r
,
5512 res
= error(_("%s: can't cherry-pick a %s"),
5513 name
, type_name(type
));
5517 res
= error(_("%s: bad revision"), name
);
5523 * If we were called as "git cherry-pick <commit>", just
5524 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
5525 * REVERT_HEAD, and don't touch the sequencer state.
5526 * This means it is possible to cherry-pick in the middle
5527 * of a cherry-pick sequence.
5529 if (opts
->revs
->cmdline
.nr
== 1 &&
5530 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
5531 opts
->revs
->no_walk
&&
5532 !opts
->revs
->cmdline
.rev
->flags
) {
5533 struct commit
*cmit
;
5535 if (prepare_revision_walk(opts
->revs
)) {
5536 res
= error(_("revision walk setup failed"));
5540 cmit
= get_revision(opts
->revs
);
5542 res
= error(_("empty commit set passed"));
5546 if (get_revision(opts
->revs
))
5547 BUG("unexpected extra commit from walk");
5549 res
= single_pick(r
, cmit
, opts
);
5554 * Start a new cherry-pick/ revert sequence; but
5555 * first, make sure that an existing one isn't in
5559 if (walk_revs_populate_todo(&todo_list
, opts
) ||
5560 create_seq_dir(r
) < 0) {
5565 if (repo_get_oid(r
, "HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
)) {
5566 res
= error(_("can't revert as initial commit"));
5570 if (save_head(oid_to_hex(&oid
))) {
5575 if (save_opts(opts
)) {
5580 update_abort_safety_file();
5581 res
= pick_commits(r
, &todo_list
, opts
);
5584 todo_list_release(&todo_list
);
5588 void append_signoff(struct strbuf
*msgbuf
, size_t ignore_footer
, unsigned flag
)
5590 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
5591 struct strbuf sob
= STRBUF_INIT
;
5594 strbuf_addstr(&sob
, sign_off_header
);
5595 strbuf_addstr(&sob
, fmt_name(WANT_COMMITTER_IDENT
));
5596 strbuf_addch(&sob
, '\n');
5599 strbuf_complete_line(msgbuf
);
5602 * If the whole message buffer is equal to the sob, pretend that we
5603 * found a conforming footer with a matching sob
5605 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
5606 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
5609 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
5612 const char *append_newlines
= NULL
;
5613 size_t len
= msgbuf
->len
- ignore_footer
;
5617 * The buffer is completely empty. Leave foom for
5618 * the title and body to be filled in by the user.
5620 append_newlines
= "\n\n";
5621 } else if (len
== 1) {
5623 * Buffer contains a single newline. Add another
5624 * so that we leave room for the title and body.
5626 append_newlines
= "\n";
5627 } else if (msgbuf
->buf
[len
- 2] != '\n') {
5629 * Buffer ends with a single newline. Add another
5630 * so that there is an empty line between the message
5633 append_newlines
= "\n";
5634 } /* else, the buffer already ends with two newlines. */
5636 if (append_newlines
)
5637 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
5638 append_newlines
, strlen(append_newlines
));
5641 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
5642 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
5645 strbuf_release(&sob
);
5648 struct labels_entry
{
5649 struct hashmap_entry entry
;
5650 char label
[FLEX_ARRAY
];
5653 static int labels_cmp(const void *fndata UNUSED
,
5654 const struct hashmap_entry
*eptr
,
5655 const struct hashmap_entry
*entry_or_key
, const void *key
)
5657 const struct labels_entry
*a
, *b
;
5659 a
= container_of(eptr
, const struct labels_entry
, entry
);
5660 b
= container_of(entry_or_key
, const struct labels_entry
, entry
);
5662 return key
? strcmp(a
->label
, key
) : strcmp(a
->label
, b
->label
);
5665 struct string_entry
{
5666 struct oidmap_entry entry
;
5667 char string
[FLEX_ARRAY
];
5670 struct label_state
{
5671 struct oidmap commit2label
;
5672 struct hashmap labels
;
5674 int max_label_length
;
5677 static const char *label_oid(struct object_id
*oid
, const char *label
,
5678 struct label_state
*state
)
5680 struct labels_entry
*labels_entry
;
5681 struct string_entry
*string_entry
;
5682 struct object_id dummy
;
5685 string_entry
= oidmap_get(&state
->commit2label
, oid
);
5687 return string_entry
->string
;
5690 * For "uninteresting" commits, i.e. commits that are not to be
5691 * rebased, and which can therefore not be labeled, we use a unique
5692 * abbreviation of the commit name. This is slightly more complicated
5693 * than calling repo_find_unique_abbrev() because we also need to make
5694 * sure that the abbreviation does not conflict with any other
5697 * We disallow "interesting" commits to be labeled by a string that
5698 * is a valid full-length hash, to ensure that we always can find an
5699 * abbreviation for any uninteresting commit's names that does not
5700 * clash with any other label.
5702 strbuf_reset(&state
->buf
);
5706 strbuf_grow(&state
->buf
, GIT_MAX_HEXSZ
);
5707 label
= p
= state
->buf
.buf
;
5709 repo_find_unique_abbrev_r(the_repository
, p
, oid
,
5713 * We may need to extend the abbreviated hash so that there is
5714 * no conflicting label.
5716 if (hashmap_get_from_hash(&state
->labels
, strihash(p
), p
)) {
5717 size_t i
= strlen(p
) + 1;
5719 oid_to_hex_r(p
, oid
);
5720 for (; i
< the_hash_algo
->hexsz
; i
++) {
5723 if (!hashmap_get_from_hash(&state
->labels
,
5730 struct strbuf
*buf
= &state
->buf
;
5731 int label_is_utf8
= 1; /* start with this assumption */
5732 size_t max_len
= buf
->len
+ state
->max_label_length
;
5735 * Sanitize labels by replacing non-alpha-numeric characters
5736 * (including white-space ones) by dashes, as they might be
5737 * illegal in file names (and hence in ref names).
5739 * Note that we retain non-ASCII UTF-8 characters (identified
5740 * via the most significant bit). They should be all acceptable
5743 * As we will use the labels as names of (loose) refs, it is
5744 * vital that the name not be longer than the maximum component
5745 * size of the file system (`NAME_MAX`). We are careful to
5746 * truncate the label accordingly, allowing for the `.lock`
5747 * suffix and for the label to be UTF-8 encoded (i.e. we avoid
5748 * truncating in the middle of a character).
5750 for (; *label
&& buf
->len
+ 1 < max_len
; label
++)
5751 if (isalnum(*label
) ||
5752 (!label_is_utf8
&& (*label
& 0x80)))
5753 strbuf_addch(buf
, *label
);
5754 else if (*label
& 0x80) {
5755 const char *p
= label
;
5757 utf8_width(&p
, NULL
);
5759 if (buf
->len
+ (p
- label
) > max_len
)
5761 strbuf_add(buf
, label
, p
- label
);
5765 strbuf_addch(buf
, *label
);
5767 /* avoid leading dash and double-dashes */
5768 } else if (buf
->len
&& buf
->buf
[buf
->len
- 1] != '-')
5769 strbuf_addch(buf
, '-');
5771 strbuf_addstr(buf
, "rev-");
5772 strbuf_add_unique_abbrev(buf
, oid
, default_abbrev
);
5776 if ((buf
->len
== the_hash_algo
->hexsz
&&
5777 !get_oid_hex(label
, &dummy
)) ||
5778 (buf
->len
== 1 && *label
== '#') ||
5779 hashmap_get_from_hash(&state
->labels
,
5780 strihash(label
), label
)) {
5782 * If the label already exists, or if the label is a
5783 * valid full OID, or the label is a '#' (which we use
5784 * as a separator between merge heads and oneline), we
5785 * append a dash and a number to make it unique.
5787 size_t len
= buf
->len
;
5789 for (i
= 2; ; i
++) {
5790 strbuf_setlen(buf
, len
);
5791 strbuf_addf(buf
, "-%d", i
);
5792 if (!hashmap_get_from_hash(&state
->labels
,
5802 FLEX_ALLOC_STR(labels_entry
, label
, label
);
5803 hashmap_entry_init(&labels_entry
->entry
, strihash(label
));
5804 hashmap_add(&state
->labels
, &labels_entry
->entry
);
5806 FLEX_ALLOC_STR(string_entry
, string
, label
);
5807 oidcpy(&string_entry
->entry
.oid
, oid
);
5808 oidmap_put(&state
->commit2label
, string_entry
);
5810 return string_entry
->string
;
5813 static int make_script_with_merges(struct pretty_print_context
*pp
,
5814 struct rev_info
*revs
, struct strbuf
*out
,
5817 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
5818 int rebase_cousins
= flags
& TODO_LIST_REBASE_COUSINS
;
5819 int root_with_onto
= flags
& TODO_LIST_ROOT_WITH_ONTO
;
5820 int skipped_commit
= 0;
5821 struct strbuf buf
= STRBUF_INIT
, oneline
= STRBUF_INIT
;
5822 struct strbuf label_from_message
= STRBUF_INIT
;
5823 struct commit_list
*commits
= NULL
, **tail
= &commits
, *iter
;
5824 struct commit_list
*tips
= NULL
, **tips_tail
= &tips
;
5825 struct commit
*commit
;
5826 struct oidmap commit2todo
= OIDMAP_INIT
;
5827 struct string_entry
*entry
;
5828 struct oidset interesting
= OIDSET_INIT
, child_seen
= OIDSET_INIT
,
5829 shown
= OIDSET_INIT
;
5830 struct label_state state
=
5831 { OIDMAP_INIT
, { NULL
}, STRBUF_INIT
, GIT_MAX_LABEL_LENGTH
};
5833 int abbr
= flags
& TODO_LIST_ABBREVIATE_CMDS
;
5834 const char *cmd_pick
= abbr
? "p" : "pick",
5835 *cmd_label
= abbr
? "l" : "label",
5836 *cmd_reset
= abbr
? "t" : "reset",
5837 *cmd_merge
= abbr
? "m" : "merge";
5839 git_config_get_int("rebase.maxlabellength", &state
.max_label_length
);
5841 oidmap_init(&commit2todo
, 0);
5842 oidmap_init(&state
.commit2label
, 0);
5843 hashmap_init(&state
.labels
, labels_cmp
, NULL
, 0);
5844 strbuf_init(&state
.buf
, 32);
5845 load_branch_decorations();
5847 if (revs
->cmdline
.nr
&& (revs
->cmdline
.rev
[0].flags
& BOTTOM
)) {
5848 struct labels_entry
*onto_label_entry
;
5849 struct object_id
*oid
= &revs
->cmdline
.rev
[0].item
->oid
;
5850 FLEX_ALLOC_STR(entry
, string
, "onto");
5851 oidcpy(&entry
->entry
.oid
, oid
);
5852 oidmap_put(&state
.commit2label
, entry
);
5854 FLEX_ALLOC_STR(onto_label_entry
, label
, "onto");
5855 hashmap_entry_init(&onto_label_entry
->entry
, strihash("onto"));
5856 hashmap_add(&state
.labels
, &onto_label_entry
->entry
);
5861 * - get onelines for all commits
5862 * - gather all branch tips (i.e. 2nd or later parents of merges)
5863 * - label all branch tips
5865 while ((commit
= get_revision(revs
))) {
5866 struct commit_list
*to_merge
;
5867 const char *p1
, *p2
;
5868 struct object_id
*oid
;
5871 tail
= &commit_list_insert(commit
, tail
)->next
;
5872 oidset_insert(&interesting
, &commit
->object
.oid
);
5874 is_empty
= is_original_commit_empty(commit
);
5875 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
)) {
5876 if (flags
& TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
)
5877 warning(_("skipped previously applied commit %s"),
5878 short_commit_name(the_repository
, commit
));
5882 if (is_empty
&& !keep_empty
)
5885 strbuf_reset(&oneline
);
5886 pretty_print_commit(pp
, commit
, &oneline
);
5888 to_merge
= commit
->parents
? commit
->parents
->next
: NULL
;
5890 /* non-merge commit: easy case */
5892 strbuf_addf(&buf
, "%s %s %s", cmd_pick
,
5893 oid_to_hex(&commit
->object
.oid
),
5896 strbuf_addf(&buf
, " %s empty",
5899 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
5900 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
5901 oidmap_put(&commit2todo
, entry
);
5906 /* Create a label from the commit message */
5907 strbuf_reset(&label_from_message
);
5908 if (skip_prefix(oneline
.buf
, "Merge ", &p1
) &&
5909 (p1
= strchr(p1
, '\'')) &&
5910 (p2
= strchr(++p1
, '\'')))
5911 strbuf_add(&label_from_message
, p1
, p2
- p1
);
5912 else if (skip_prefix(oneline
.buf
, "Merge pull request ",
5914 (p1
= strstr(p1
, " from ")))
5915 strbuf_addstr(&label_from_message
, p1
+ strlen(" from "));
5917 strbuf_addbuf(&label_from_message
, &oneline
);
5920 strbuf_addf(&buf
, "%s -C %s",
5921 cmd_merge
, oid_to_hex(&commit
->object
.oid
));
5923 /* label the tips of merged branches */
5924 for (; to_merge
; to_merge
= to_merge
->next
) {
5925 const char *label
= label_from_message
.buf
;
5926 const struct name_decoration
*decoration
=
5927 get_name_decoration(&to_merge
->item
->object
);
5930 skip_prefix(decoration
->name
, "refs/heads/",
5933 oid
= &to_merge
->item
->object
.oid
;
5934 strbuf_addch(&buf
, ' ');
5936 if (!oidset_contains(&interesting
, oid
)) {
5937 strbuf_addstr(&buf
, label_oid(oid
, NULL
,
5942 tips_tail
= &commit_list_insert(to_merge
->item
,
5945 strbuf_addstr(&buf
, label_oid(oid
, label
, &state
));
5947 strbuf_addf(&buf
, " # %s", oneline
.buf
);
5949 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
5950 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
5951 oidmap_put(&commit2todo
, entry
);
5954 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS
,
5955 _("use --reapply-cherry-picks to include skipped commits"));
5959 * - label branch points
5960 * - add HEAD to the branch tips
5962 for (iter
= commits
; iter
; iter
= iter
->next
) {
5963 struct commit_list
*parent
= iter
->item
->parents
;
5964 for (; parent
; parent
= parent
->next
) {
5965 struct object_id
*oid
= &parent
->item
->object
.oid
;
5966 if (!oidset_contains(&interesting
, oid
))
5968 if (oidset_insert(&child_seen
, oid
))
5969 label_oid(oid
, "branch-point", &state
);
5972 /* Add HEAD as implicit "tip of branch" */
5974 tips_tail
= &commit_list_insert(iter
->item
,
5979 * Third phase: output the todo list. This is a bit tricky, as we
5980 * want to avoid jumping back and forth between revisions. To
5981 * accomplish that goal, we walk backwards from the branch tips,
5982 * gathering commits not yet shown, reversing the list on the fly,
5983 * then outputting that list (labeling revisions as needed).
5985 strbuf_addf(out
, "%s onto\n", cmd_label
);
5986 for (iter
= tips
; iter
; iter
= iter
->next
) {
5987 struct commit_list
*list
= NULL
, *iter2
;
5989 commit
= iter
->item
;
5990 if (oidset_contains(&shown
, &commit
->object
.oid
))
5992 entry
= oidmap_get(&state
.commit2label
, &commit
->object
.oid
);
5995 strbuf_addf(out
, "\n%s Branch %s\n", comment_line_str
, entry
->string
);
5997 strbuf_addch(out
, '\n');
5999 while (oidset_contains(&interesting
, &commit
->object
.oid
) &&
6000 !oidset_contains(&shown
, &commit
->object
.oid
)) {
6001 commit_list_insert(commit
, &list
);
6002 if (!commit
->parents
) {
6006 commit
= commit
->parents
->item
;
6010 strbuf_addf(out
, "%s %s\n", cmd_reset
,
6011 rebase_cousins
|| root_with_onto
?
6012 "onto" : "[new root]");
6014 const char *to
= NULL
;
6016 entry
= oidmap_get(&state
.commit2label
,
6017 &commit
->object
.oid
);
6020 else if (!rebase_cousins
)
6021 to
= label_oid(&commit
->object
.oid
, NULL
,
6024 if (!to
|| !strcmp(to
, "onto"))
6025 strbuf_addf(out
, "%s onto\n", cmd_reset
);
6027 strbuf_reset(&oneline
);
6028 pretty_print_commit(pp
, commit
, &oneline
);
6029 strbuf_addf(out
, "%s %s # %s\n",
6030 cmd_reset
, to
, oneline
.buf
);
6034 for (iter2
= list
; iter2
; iter2
= iter2
->next
) {
6035 struct object_id
*oid
= &iter2
->item
->object
.oid
;
6036 entry
= oidmap_get(&commit2todo
, oid
);
6037 /* only show if not already upstream */
6039 strbuf_addf(out
, "%s\n", entry
->string
);
6040 entry
= oidmap_get(&state
.commit2label
, oid
);
6042 strbuf_addf(out
, "%s %s\n",
6043 cmd_label
, entry
->string
);
6044 oidset_insert(&shown
, oid
);
6047 free_commit_list(list
);
6050 free_commit_list(commits
);
6051 free_commit_list(tips
);
6053 strbuf_release(&label_from_message
);
6054 strbuf_release(&oneline
);
6055 strbuf_release(&buf
);
6057 oidset_clear(&interesting
);
6058 oidset_clear(&child_seen
);
6059 oidset_clear(&shown
);
6060 oidmap_free(&commit2todo
, 1);
6061 oidmap_free(&state
.commit2label
, 1);
6062 hashmap_clear_and_free(&state
.labels
, struct labels_entry
, entry
);
6063 strbuf_release(&state
.buf
);
6068 int sequencer_make_script(struct repository
*r
, struct strbuf
*out
, int argc
,
6069 const char **argv
, unsigned flags
)
6071 char *format
= NULL
;
6072 struct pretty_print_context pp
= {0};
6073 struct rev_info revs
;
6074 struct commit
*commit
;
6075 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
6076 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
6077 int rebase_merges
= flags
& TODO_LIST_REBASE_MERGES
;
6078 int reapply_cherry_picks
= flags
& TODO_LIST_REAPPLY_CHERRY_PICKS
;
6079 int skipped_commit
= 0;
6082 repo_init_revisions(r
, &revs
, NULL
);
6083 revs
.verbose_header
= 1;
6085 revs
.max_parents
= 1;
6086 revs
.cherry_mark
= !reapply_cherry_picks
;
6089 revs
.right_only
= 1;
6090 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
6091 revs
.topo_order
= 1;
6093 revs
.pretty_given
= 1;
6094 git_config_get_string("rebase.instructionFormat", &format
);
6095 if (!format
|| !*format
) {
6097 format
= xstrdup("%s");
6099 get_commit_format(format
, &revs
);
6101 pp
.fmt
= revs
.commit_format
;
6102 pp
.output_encoding
= get_log_output_encoding();
6104 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1) {
6105 ret
= error(_("make_script: unhandled options"));
6109 if (prepare_revision_walk(&revs
) < 0) {
6110 ret
= error(_("make_script: error preparing revisions"));
6114 if (rebase_merges
) {
6115 ret
= make_script_with_merges(&pp
, &revs
, out
, flags
);
6119 while ((commit
= get_revision(&revs
))) {
6120 int is_empty
= is_original_commit_empty(commit
);
6122 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
)) {
6123 if (flags
& TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
)
6124 warning(_("skipped previously applied commit %s"),
6125 short_commit_name(r
, commit
));
6129 if (is_empty
&& !keep_empty
)
6131 strbuf_addf(out
, "%s %s ", insn
,
6132 oid_to_hex(&commit
->object
.oid
));
6133 pretty_print_commit(&pp
, commit
, out
);
6135 strbuf_addf(out
, " %s empty", comment_line_str
);
6136 strbuf_addch(out
, '\n');
6139 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS
,
6140 _("use --reapply-cherry-picks to include skipped commits"));
6142 release_revisions(&revs
);
6147 * Add commands after pick and (series of) squash/fixup commands
6150 static void todo_list_add_exec_commands(struct todo_list
*todo_list
,
6151 struct string_list
*commands
)
6153 struct strbuf
*buf
= &todo_list
->buf
;
6154 size_t base_offset
= buf
->len
;
6155 int i
, insert
, nr
= 0, alloc
= 0;
6156 struct todo_item
*items
= NULL
, *base_items
= NULL
;
6158 CALLOC_ARRAY(base_items
, commands
->nr
);
6159 for (i
= 0; i
< commands
->nr
; i
++) {
6160 size_t command_len
= strlen(commands
->items
[i
].string
);
6162 strbuf_addstr(buf
, commands
->items
[i
].string
);
6163 strbuf_addch(buf
, '\n');
6165 base_items
[i
].command
= TODO_EXEC
;
6166 base_items
[i
].offset_in_buf
= base_offset
;
6167 base_items
[i
].arg_offset
= base_offset
;
6168 base_items
[i
].arg_len
= command_len
;
6170 base_offset
+= command_len
+ 1;
6174 * Insert <commands> after every pick. Here, fixup/squash chains
6175 * are considered part of the pick, so we insert the commands *after*
6176 * those chains if there are any.
6178 * As we insert the exec commands immediately after rearranging
6179 * any fixups and before the user edits the list, a fixup chain
6180 * can never contain comments (any comments are empty picks that
6181 * have been commented out because the user did not specify
6182 * --keep-empty). So, it is safe to insert an exec command
6183 * without looking at the command following a comment.
6186 for (i
= 0; i
< todo_list
->nr
; i
++) {
6187 enum todo_command command
= todo_list
->items
[i
].command
;
6188 if (insert
&& !is_fixup(command
)) {
6189 ALLOC_GROW(items
, nr
+ commands
->nr
, alloc
);
6190 COPY_ARRAY(items
+ nr
, base_items
, commands
->nr
);
6196 ALLOC_GROW(items
, nr
+ 1, alloc
);
6197 items
[nr
++] = todo_list
->items
[i
];
6199 if (command
== TODO_PICK
|| command
== TODO_MERGE
)
6203 /* insert or append final <commands> */
6205 ALLOC_GROW(items
, nr
+ commands
->nr
, alloc
);
6206 COPY_ARRAY(items
+ nr
, base_items
, commands
->nr
);
6211 FREE_AND_NULL(todo_list
->items
);
6212 todo_list
->items
= items
;
6214 todo_list
->alloc
= alloc
;
6217 static void todo_list_to_strbuf(struct repository
*r
,
6218 struct todo_list
*todo_list
,
6219 struct strbuf
*buf
, int num
, unsigned flags
)
6221 struct todo_item
*item
;
6222 int i
, max
= todo_list
->nr
;
6224 if (num
> 0 && num
< max
)
6227 for (item
= todo_list
->items
, i
= 0; i
< max
; i
++, item
++) {
6230 /* if the item is not a command write it and continue */
6231 if (item
->command
>= TODO_COMMENT
) {
6232 strbuf_addf(buf
, "%.*s\n", item
->arg_len
,
6233 todo_item_get_arg(todo_list
, item
));
6237 /* add command to the buffer */
6238 cmd
= command_to_char(item
->command
);
6239 if ((flags
& TODO_LIST_ABBREVIATE_CMDS
) && cmd
)
6240 strbuf_addch(buf
, cmd
);
6242 strbuf_addstr(buf
, command_to_string(item
->command
));
6246 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
6247 short_commit_name(r
, item
->commit
) :
6248 oid_to_hex(&item
->commit
->object
.oid
);
6250 if (item
->command
== TODO_FIXUP
) {
6251 if (item
->flags
& TODO_EDIT_FIXUP_MSG
)
6252 strbuf_addstr(buf
, " -c");
6253 else if (item
->flags
& TODO_REPLACE_FIXUP_MSG
) {
6254 strbuf_addstr(buf
, " -C");
6258 if (item
->command
== TODO_MERGE
) {
6259 if (item
->flags
& TODO_EDIT_MERGE_MSG
)
6260 strbuf_addstr(buf
, " -c");
6262 strbuf_addstr(buf
, " -C");
6265 strbuf_addf(buf
, " %s", oid
);
6268 /* add all the rest */
6270 strbuf_addch(buf
, '\n');
6272 strbuf_addf(buf
, " %.*s\n", item
->arg_len
,
6273 todo_item_get_arg(todo_list
, item
));
6277 int todo_list_write_to_file(struct repository
*r
, struct todo_list
*todo_list
,
6278 const char *file
, const char *shortrevisions
,
6279 const char *shortonto
, int num
, unsigned flags
)
6282 struct strbuf buf
= STRBUF_INIT
;
6284 todo_list_to_strbuf(r
, todo_list
, &buf
, num
, flags
);
6285 if (flags
& TODO_LIST_APPEND_TODO_HELP
)
6286 append_todo_help(count_commands(todo_list
),
6287 shortrevisions
, shortonto
, &buf
);
6289 res
= write_message(buf
.buf
, buf
.len
, file
, 0);
6290 strbuf_release(&buf
);
6295 /* skip picking commits whose parents are unchanged */
6296 static int skip_unnecessary_picks(struct repository
*r
,
6297 struct todo_list
*todo_list
,
6298 struct object_id
*base_oid
)
6300 struct object_id
*parent_oid
;
6303 for (i
= 0; i
< todo_list
->nr
; i
++) {
6304 struct todo_item
*item
= todo_list
->items
+ i
;
6306 if (item
->command
>= TODO_NOOP
)
6308 if (item
->command
!= TODO_PICK
)
6310 if (repo_parse_commit(r
, item
->commit
)) {
6311 return error(_("could not parse commit '%s'"),
6312 oid_to_hex(&item
->commit
->object
.oid
));
6314 if (!item
->commit
->parents
)
6315 break; /* root commit */
6316 if (item
->commit
->parents
->next
)
6317 break; /* merge commit */
6318 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
6319 if (!oideq(parent_oid
, base_oid
))
6321 oidcpy(base_oid
, &item
->commit
->object
.oid
);
6324 const char *done_path
= rebase_path_done();
6326 if (todo_list_write_to_file(r
, todo_list
, done_path
, NULL
, NULL
, i
, 0)) {
6327 error_errno(_("could not write to '%s'"), done_path
);
6331 MOVE_ARRAY(todo_list
->items
, todo_list
->items
+ i
, todo_list
->nr
- i
);
6333 todo_list
->current
= 0;
6334 todo_list
->done_nr
+= i
;
6336 if (is_fixup(peek_command(todo_list
, 0)))
6337 record_in_rewritten(base_oid
, peek_command(todo_list
, 0));
6343 struct todo_add_branch_context
{
6344 struct todo_item
*items
;
6348 struct commit
*commit
;
6349 struct string_list refs_to_oids
;
6352 static int add_decorations_to_list(const struct commit
*commit
,
6353 struct todo_add_branch_context
*ctx
)
6355 const struct name_decoration
*decoration
= get_name_decoration(&commit
->object
);
6356 const char *head_ref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
6358 RESOLVE_REF_READING
,
6362 while (decoration
) {
6363 struct todo_item
*item
;
6365 size_t base_offset
= ctx
->buf
->len
;
6368 * If the branch is the current HEAD, then it will be
6369 * updated by the default rebase behavior.
6371 if (head_ref
&& !strcmp(head_ref
, decoration
->name
)) {
6372 decoration
= decoration
->next
;
6376 ALLOC_GROW(ctx
->items
,
6379 item
= &ctx
->items
[ctx
->items_nr
];
6380 memset(item
, 0, sizeof(*item
));
6382 /* If the branch is checked out, then leave a comment instead. */
6383 if ((path
= branch_checked_out(decoration
->name
))) {
6384 item
->command
= TODO_COMMENT
;
6385 strbuf_addf(ctx
->buf
, "# Ref %s checked out at '%s'\n",
6386 decoration
->name
, path
);
6388 struct string_list_item
*sti
;
6389 item
->command
= TODO_UPDATE_REF
;
6390 strbuf_addf(ctx
->buf
, "%s\n", decoration
->name
);
6392 sti
= string_list_insert(&ctx
->refs_to_oids
,
6394 sti
->util
= init_update_ref_record(decoration
->name
);
6397 item
->offset_in_buf
= base_offset
;
6398 item
->arg_offset
= base_offset
;
6399 item
->arg_len
= ctx
->buf
->len
- base_offset
;
6402 decoration
= decoration
->next
;
6409 * For each 'pick' command, find out if the commit has a decoration in
6410 * refs/heads/. If so, then add a 'label for-update-refs/' command.
6412 static int todo_list_add_update_ref_commands(struct todo_list
*todo_list
)
6415 struct todo_add_branch_context ctx
= {
6416 .buf
= &todo_list
->buf
,
6417 .refs_to_oids
= STRING_LIST_INIT_DUP
,
6420 ctx
.items_alloc
= 2 * todo_list
->nr
+ 1;
6421 ALLOC_ARRAY(ctx
.items
, ctx
.items_alloc
);
6423 load_branch_decorations();
6425 for (i
= 0; i
< todo_list
->nr
; ) {
6426 struct todo_item
*item
= &todo_list
->items
[i
];
6428 /* insert ith item into new list */
6429 ALLOC_GROW(ctx
.items
,
6433 ctx
.items
[ctx
.items_nr
++] = todo_list
->items
[i
++];
6436 ctx
.commit
= item
->commit
;
6437 add_decorations_to_list(item
->commit
, &ctx
);
6441 res
= write_update_refs_state(&ctx
.refs_to_oids
);
6443 string_list_clear(&ctx
.refs_to_oids
, 1);
6446 /* we failed, so clean up the new list. */
6451 free(todo_list
->items
);
6452 todo_list
->items
= ctx
.items
;
6453 todo_list
->nr
= ctx
.items_nr
;
6454 todo_list
->alloc
= ctx
.items_alloc
;
6459 int complete_action(struct repository
*r
, struct replay_opts
*opts
, unsigned flags
,
6460 const char *shortrevisions
, const char *onto_name
,
6461 struct commit
*onto
, const struct object_id
*orig_head
,
6462 struct string_list
*commands
, unsigned autosquash
,
6463 unsigned update_refs
,
6464 struct todo_list
*todo_list
)
6466 char shortonto
[GIT_MAX_HEXSZ
+ 1];
6467 const char *todo_file
= rebase_path_todo();
6468 struct todo_list new_todo
= TODO_LIST_INIT
;
6469 struct strbuf
*buf
= &todo_list
->buf
, buf2
= STRBUF_INIT
;
6470 struct object_id oid
= onto
->object
.oid
;
6473 repo_find_unique_abbrev_r(r
, shortonto
, &oid
,
6476 if (buf
->len
== 0) {
6477 struct todo_item
*item
= append_new_todo(todo_list
);
6478 item
->command
= TODO_NOOP
;
6479 item
->commit
= NULL
;
6480 item
->arg_len
= item
->arg_offset
= item
->flags
= item
->offset_in_buf
= 0;
6483 if (update_refs
&& todo_list_add_update_ref_commands(todo_list
))
6486 if (autosquash
&& todo_list_rearrange_squash(todo_list
))
6490 todo_list_add_exec_commands(todo_list
, commands
);
6492 if (count_commands(todo_list
) == 0) {
6493 apply_autostash(rebase_path_autostash());
6494 sequencer_remove_state(opts
);
6496 return error(_("nothing to do"));
6499 res
= edit_todo_list(r
, opts
, todo_list
, &new_todo
, shortrevisions
,
6503 else if (res
== -2) {
6504 apply_autostash(rebase_path_autostash());
6505 sequencer_remove_state(opts
);
6508 } else if (res
== -3) {
6509 apply_autostash(rebase_path_autostash());
6510 sequencer_remove_state(opts
);
6511 todo_list_release(&new_todo
);
6513 return error(_("nothing to do"));
6514 } else if (res
== -4) {
6515 checkout_onto(r
, opts
, onto_name
, &onto
->object
.oid
, orig_head
);
6516 todo_list_release(&new_todo
);
6521 /* Expand the commit IDs */
6522 todo_list_to_strbuf(r
, &new_todo
, &buf2
, -1, 0);
6523 strbuf_swap(&new_todo
.buf
, &buf2
);
6524 strbuf_release(&buf2
);
6525 /* Nothing is done yet, and we're reparsing, so let's reset the count */
6526 new_todo
.total_nr
= 0;
6527 if (todo_list_parse_insn_buffer(r
, opts
, new_todo
.buf
.buf
, &new_todo
) < 0)
6528 BUG("invalid todo list after expanding IDs:\n%s",
6531 if (opts
->allow_ff
&& skip_unnecessary_picks(r
, &new_todo
, &oid
)) {
6532 todo_list_release(&new_todo
);
6533 return error(_("could not skip unnecessary pick commands"));
6536 if (todo_list_write_to_file(r
, &new_todo
, todo_file
, NULL
, NULL
, -1,
6537 flags
& ~(TODO_LIST_SHORTEN_IDS
))) {
6538 todo_list_release(&new_todo
);
6539 return error_errno(_("could not write '%s'"), todo_file
);
6544 if (checkout_onto(r
, opts
, onto_name
, &oid
, orig_head
))
6547 if (require_clean_work_tree(r
, "rebase", NULL
, 1, 1))
6550 todo_list_write_total_nr(&new_todo
);
6551 res
= pick_commits(r
, &new_todo
, opts
);
6554 todo_list_release(&new_todo
);
6559 struct subject2item_entry
{
6560 struct hashmap_entry entry
;
6562 char subject
[FLEX_ARRAY
];
6565 static int subject2item_cmp(const void *fndata UNUSED
,
6566 const struct hashmap_entry
*eptr
,
6567 const struct hashmap_entry
*entry_or_key
,
6570 const struct subject2item_entry
*a
, *b
;
6572 a
= container_of(eptr
, const struct subject2item_entry
, entry
);
6573 b
= container_of(entry_or_key
, const struct subject2item_entry
, entry
);
6575 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
6578 define_commit_slab(commit_todo_item
, struct todo_item
*);
6580 static int skip_fixupish(const char *subject
, const char **p
) {
6581 return skip_prefix(subject
, "fixup! ", p
) ||
6582 skip_prefix(subject
, "amend! ", p
) ||
6583 skip_prefix(subject
, "squash! ", p
);
6587 * Rearrange the todo list that has both "pick commit-id msg" and "pick
6588 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
6589 * after the former, and change "pick" to "fixup"/"squash".
6591 * Note that if the config has specified a custom instruction format, each log
6592 * message will have to be retrieved from the commit (as the oneline in the
6593 * script cannot be trusted) in order to normalize the autosquash arrangement.
6595 int todo_list_rearrange_squash(struct todo_list
*todo_list
)
6597 struct hashmap subject2item
;
6598 int rearranged
= 0, *next
, *tail
, i
, nr
= 0;
6600 struct commit_todo_item commit_todo
;
6601 struct todo_item
*items
= NULL
;
6603 init_commit_todo_item(&commit_todo
);
6605 * The hashmap maps onelines to the respective todo list index.
6607 * If any items need to be rearranged, the next[i] value will indicate
6608 * which item was moved directly after the i'th.
6610 * In that case, last[i] will indicate the index of the latest item to
6611 * be moved to appear after the i'th.
6613 hashmap_init(&subject2item
, subject2item_cmp
, NULL
, todo_list
->nr
);
6614 ALLOC_ARRAY(next
, todo_list
->nr
);
6615 ALLOC_ARRAY(tail
, todo_list
->nr
);
6616 ALLOC_ARRAY(subjects
, todo_list
->nr
);
6617 for (i
= 0; i
< todo_list
->nr
; i
++) {
6618 struct strbuf buf
= STRBUF_INIT
;
6619 struct todo_item
*item
= todo_list
->items
+ i
;
6620 const char *commit_buffer
, *subject
, *p
;
6623 struct subject2item_entry
*entry
;
6625 next
[i
] = tail
[i
] = -1;
6626 if (!item
->commit
|| item
->command
== TODO_DROP
) {
6631 if (is_fixup(item
->command
)) {
6632 clear_commit_todo_item(&commit_todo
);
6633 return error(_("the script was already rearranged."));
6636 repo_parse_commit(the_repository
, item
->commit
);
6637 commit_buffer
= repo_logmsg_reencode(the_repository
,
6640 find_commit_subject(commit_buffer
, &subject
);
6641 format_subject(&buf
, subject
, " ");
6642 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
6643 repo_unuse_commit_buffer(the_repository
, item
->commit
,
6645 if (skip_fixupish(subject
, &p
)) {
6646 struct commit
*commit2
;
6651 if (!skip_fixupish(p
, &p
))
6655 entry
= hashmap_get_entry_from_hash(&subject2item
,
6657 struct subject2item_entry
,
6660 /* found by title */
6662 else if (!strchr(p
, ' ') &&
6664 lookup_commit_reference_by_name(p
)) &&
6665 *commit_todo_item_at(&commit_todo
, commit2
))
6666 /* found by commit name */
6667 i2
= *commit_todo_item_at(&commit_todo
, commit2
)
6670 /* copy can be a prefix of the commit subject */
6671 for (i2
= 0; i2
< i
; i2
++)
6673 starts_with(subjects
[i2
], p
))
6681 if (starts_with(subject
, "fixup!")) {
6682 todo_list
->items
[i
].command
= TODO_FIXUP
;
6683 } else if (starts_with(subject
, "amend!")) {
6684 todo_list
->items
[i
].command
= TODO_FIXUP
;
6685 todo_list
->items
[i
].flags
= TODO_REPLACE_FIXUP_MSG
;
6687 todo_list
->items
[i
].command
= TODO_SQUASH
;
6693 next
[i
] = next
[tail
[i2
]];
6697 } else if (!hashmap_get_from_hash(&subject2item
,
6698 strhash(subject
), subject
)) {
6699 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
6701 hashmap_entry_init(&entry
->entry
,
6702 strhash(entry
->subject
));
6703 hashmap_put(&subject2item
, &entry
->entry
);
6706 *commit_todo_item_at(&commit_todo
, item
->commit
) = item
;
6710 ALLOC_ARRAY(items
, todo_list
->nr
);
6712 for (i
= 0; i
< todo_list
->nr
; i
++) {
6713 enum todo_command command
= todo_list
->items
[i
].command
;
6717 * Initially, all commands are 'pick's. If it is a
6718 * fixup or a squash now, we have rearranged it.
6720 if (is_fixup(command
))
6724 items
[nr
++] = todo_list
->items
[cur
];
6729 assert(nr
== todo_list
->nr
);
6730 todo_list
->alloc
= nr
;
6731 FREE_AND_NULL(todo_list
->items
);
6732 todo_list
->items
= items
;
6737 for (i
= 0; i
< todo_list
->nr
; i
++)
6740 hashmap_clear_and_free(&subject2item
, struct subject2item_entry
, entry
);
6742 clear_commit_todo_item(&commit_todo
);
6747 int sequencer_determine_whence(struct repository
*r
, enum commit_whence
*whence
)
6749 if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD")) {
6750 struct object_id cherry_pick_head
, rebase_head
;
6752 if (file_exists(git_path_seq_dir()))
6753 *whence
= FROM_CHERRY_PICK_MULTI
;
6754 if (file_exists(rebase_path()) &&
6755 !repo_get_oid(r
, "REBASE_HEAD", &rebase_head
) &&
6756 !repo_get_oid(r
, "CHERRY_PICK_HEAD", &cherry_pick_head
) &&
6757 oideq(&rebase_head
, &cherry_pick_head
))
6758 *whence
= FROM_REBASE_PICK
;
6760 *whence
= FROM_CHERRY_PICK_SINGLE
;
6768 int sequencer_get_update_refs_state(const char *wt_dir
,
6769 struct string_list
*refs
)
6773 struct strbuf ref
= STRBUF_INIT
;
6774 struct strbuf hash
= STRBUF_INIT
;
6775 struct update_ref_record
*rec
= NULL
;
6777 char *path
= rebase_path_update_refs(wt_dir
);
6779 fp
= fopen(path
, "r");
6783 while (strbuf_getline(&ref
, fp
) != EOF
) {
6784 struct string_list_item
*item
;
6786 CALLOC_ARRAY(rec
, 1);
6788 if (strbuf_getline(&hash
, fp
) == EOF
||
6789 get_oid_hex(hash
.buf
, &rec
->before
)) {
6790 warning(_("update-refs file at '%s' is invalid"),
6796 if (strbuf_getline(&hash
, fp
) == EOF
||
6797 get_oid_hex(hash
.buf
, &rec
->after
)) {
6798 warning(_("update-refs file at '%s' is invalid"),
6804 item
= string_list_insert(refs
, ref
.buf
);
6814 strbuf_release(&ref
);
6815 strbuf_release(&hash
);