5 #include "object-store.h"
10 #include "run-command.h"
14 #include "cache-tree.h"
18 #include "merge-ort.h"
19 #include "merge-ort-wrappers.h"
25 #include "wt-status.h"
27 #include "notes-utils.h"
29 #include "unpack-trees.h"
33 #include "commit-slab.h"
35 #include "commit-reach.h"
36 #include "rebase-interactive.h"
39 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
41 static const char sign_off_header
[] = "Signed-off-by: ";
42 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
44 GIT_PATH_FUNC(git_path_commit_editmsg
, "COMMIT_EDITMSG")
46 static GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
48 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
49 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
50 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
51 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
53 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
55 * The file containing rebase commands, comments, and empty lines.
56 * This file is created by "git rebase -i" then edited by the user. As
57 * the lines are processed, they are removed from the front of this
58 * file and written to the tail of 'done'.
60 GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
61 GIT_PATH_FUNC(rebase_path_todo_backup
, "rebase-merge/git-rebase-todo.backup")
63 GIT_PATH_FUNC(rebase_path_dropped
, "rebase-merge/dropped")
66 * The rebase command lines that have already been processed. A line
67 * is moved here when it is first handled, before any associated user
70 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
72 * The file to keep track of how many commands were already processed (e.g.
75 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum")
77 * The file to keep track of how many commands are to be processed in total
78 * (e.g. for the prompt).
80 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end")
82 * The commit message that is planned to be used for any changes that
83 * need to be committed following a user interaction.
85 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
87 * The file into which is accumulated the suggested commit message for
88 * squash/fixup commands. When the first of a series of squash/fixups
89 * is seen, the file is created and the commit message from the
90 * previous commit and from the first squash/fixup commit are written
91 * to it. The commit message for each subsequent squash/fixup commit
92 * is appended to the file as it is processed.
94 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
96 * If the current series of squash/fixups has not yet included a squash
97 * command, then this file exists and holds the commit message of the
98 * original "pick" commit. (If the series ends without a "squash"
99 * command, then this can be used as the commit message of the combined
100 * commit without opening the editor.)
102 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
104 * This file contains the list fixup/squash commands that have been
105 * accumulated into message-fixup or message-squash so far.
107 static GIT_PATH_FUNC(rebase_path_current_fixups
, "rebase-merge/current-fixups")
109 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
110 * GIT_AUTHOR_DATE that will be used for the commit that is currently
113 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
115 * When an "edit" rebase command is being processed, the SHA1 of the
116 * commit to be edited is recorded in this file. When "git rebase
117 * --continue" is executed, if there are any staged changes then they
118 * will be amended to the HEAD commit, but only provided the HEAD
119 * commit is still the commit to be edited. When any other rebase
120 * command is processed, this file is deleted.
122 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
124 * When we stop at a given patch via the "edit" command, this file contains
125 * the commit object name of the corresponding patch.
127 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
129 * For the post-rewrite hook, we make a list of rewritten commits and
130 * their new sha1s. The rewritten-pending list keeps the sha1s of
131 * commits that have been processed, but not committed yet,
132 * e.g. because they are waiting for a 'squash' command.
134 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
135 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
136 "rebase-merge/rewritten-pending")
139 * The path of the file containing the OID of the "squash onto" commit, i.e.
140 * the dummy commit used for `reset [new root]`.
142 static GIT_PATH_FUNC(rebase_path_squash_onto
, "rebase-merge/squash-onto")
145 * The path of the file listing refs that need to be deleted after the rebase
146 * finishes. This is used by the `label` command to record the need for cleanup.
148 static GIT_PATH_FUNC(rebase_path_refs_to_delete
, "rebase-merge/refs-to-delete")
151 * The following files are written by git-rebase just after parsing the
154 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
155 static GIT_PATH_FUNC(rebase_path_cdate_is_adate
, "rebase-merge/cdate_is_adate")
156 static GIT_PATH_FUNC(rebase_path_ignore_date
, "rebase-merge/ignore_date")
157 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
158 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
159 static GIT_PATH_FUNC(rebase_path_quiet
, "rebase-merge/quiet")
160 static GIT_PATH_FUNC(rebase_path_signoff
, "rebase-merge/signoff")
161 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
162 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
163 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
164 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
165 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
166 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
167 static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec
, "rebase-merge/reschedule-failed-exec")
168 static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec
, "rebase-merge/no-reschedule-failed-exec")
169 static GIT_PATH_FUNC(rebase_path_drop_redundant_commits
, "rebase-merge/drop_redundant_commits")
170 static GIT_PATH_FUNC(rebase_path_keep_redundant_commits
, "rebase-merge/keep_redundant_commits")
172 static int git_sequencer_config(const char *k
, const char *v
, void *cb
)
174 struct replay_opts
*opts
= cb
;
177 if (!strcmp(k
, "commit.cleanup")) {
180 status
= git_config_string(&s
, k
, v
);
184 if (!strcmp(s
, "verbatim")) {
185 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
186 opts
->explicit_cleanup
= 1;
187 } else if (!strcmp(s
, "whitespace")) {
188 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
189 opts
->explicit_cleanup
= 1;
190 } else if (!strcmp(s
, "strip")) {
191 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
192 opts
->explicit_cleanup
= 1;
193 } else if (!strcmp(s
, "scissors")) {
194 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SCISSORS
;
195 opts
->explicit_cleanup
= 1;
197 warning(_("invalid commit message cleanup mode '%s'"),
205 if (!strcmp(k
, "commit.gpgsign")) {
206 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
210 if (!opts
->default_strategy
&& !strcmp(k
, "pull.twohead")) {
211 int ret
= git_config_string((const char**)&opts
->default_strategy
, k
, v
);
214 * pull.twohead is allowed to be multi-valued; we only
215 * care about the first value.
217 char *tmp
= strchr(opts
->default_strategy
, ' ');
224 status
= git_gpg_config(k
, v
, NULL
);
228 return git_diff_basic_config(k
, v
, NULL
);
231 void sequencer_init_config(struct replay_opts
*opts
)
233 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
234 git_config(git_sequencer_config
, opts
);
237 static inline int is_rebase_i(const struct replay_opts
*opts
)
239 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
242 static const char *get_dir(const struct replay_opts
*opts
)
244 if (is_rebase_i(opts
))
245 return rebase_path();
246 return git_path_seq_dir();
249 static const char *get_todo_path(const struct replay_opts
*opts
)
251 if (is_rebase_i(opts
))
252 return rebase_path_todo();
253 return git_path_todo_file();
257 * Returns 0 for non-conforming footer
258 * Returns 1 for conforming footer
259 * Returns 2 when sob exists within conforming footer
260 * Returns 3 when sob exists within conforming footer as last entry
262 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
263 size_t ignore_footer
)
265 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
266 struct trailer_info info
;
268 int found_sob
= 0, found_sob_last
= 0;
274 saved_char
= sb
->buf
[sb
->len
- ignore_footer
];
275 sb
->buf
[sb
->len
- ignore_footer
] = '\0';
278 trailer_info_get(&info
, sb
->buf
, &opts
);
281 sb
->buf
[sb
->len
- ignore_footer
] = saved_char
;
283 if (info
.trailer_start
== info
.trailer_end
)
286 for (i
= 0; i
< info
.trailer_nr
; i
++)
287 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
289 if (i
== info
.trailer_nr
- 1)
293 trailer_info_release(&info
);
302 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
304 static struct strbuf buf
= STRBUF_INIT
;
308 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
312 int sequencer_remove_state(struct replay_opts
*opts
)
314 struct strbuf buf
= STRBUF_INIT
;
317 if (is_rebase_i(opts
) &&
318 strbuf_read_file(&buf
, rebase_path_refs_to_delete(), 0) > 0) {
321 char *eol
= strchr(p
, '\n');
324 if (delete_ref("(rebase) cleanup", p
, NULL
, 0) < 0) {
325 warning(_("could not delete '%s'"), p
);
334 free(opts
->gpg_sign
);
335 free(opts
->default_strategy
);
336 free(opts
->strategy
);
337 for (i
= 0; i
< opts
->xopts_nr
; i
++)
338 free(opts
->xopts
[i
]);
340 strbuf_release(&opts
->current_fixups
);
343 strbuf_addstr(&buf
, get_dir(opts
));
344 if (remove_dir_recursively(&buf
, 0))
345 ret
= error(_("could not remove '%s'"), buf
.buf
);
346 strbuf_release(&buf
);
351 static const char *action_name(const struct replay_opts
*opts
)
353 switch (opts
->action
) {
357 return N_("cherry-pick");
358 case REPLAY_INTERACTIVE_REBASE
:
361 die(_("unknown action: %d"), opts
->action
);
364 struct commit_message
{
371 static const char *short_commit_name(struct commit
*commit
)
373 return find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
);
376 static int get_message(struct commit
*commit
, struct commit_message
*out
)
378 const char *abbrev
, *subject
;
381 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
382 abbrev
= short_commit_name(commit
);
384 subject_len
= find_commit_subject(out
->message
, &subject
);
386 out
->subject
= xmemdupz(subject
, subject_len
);
387 out
->label
= xstrfmt("%s (%s)", abbrev
, out
->subject
);
388 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
393 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
395 free(msg
->parent_label
);
398 unuse_commit_buffer(commit
, msg
->message
);
401 static void print_advice(struct repository
*r
, int show_hint
,
402 struct replay_opts
*opts
)
404 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
409 * A conflict has occurred but the porcelain
410 * (typically rebase --interactive) wants to take care
411 * of the commit itself so remove CHERRY_PICK_HEAD
413 refs_delete_ref(get_main_ref_store(r
), "", "CHERRY_PICK_HEAD",
420 advise(_("after resolving the conflicts, mark the corrected paths\n"
421 "with 'git add <paths>' or 'git rm <paths>'"));
422 else if (opts
->action
== REPLAY_PICK
)
423 advise(_("After resolving the conflicts, mark them with\n"
424 "\"git add/rm <pathspec>\", then run\n"
425 "\"git cherry-pick --continue\".\n"
426 "You can instead skip this commit with \"git cherry-pick --skip\".\n"
427 "To abort and get back to the state before \"git cherry-pick\",\n"
428 "run \"git cherry-pick --abort\"."));
429 else if (opts
->action
== REPLAY_REVERT
)
430 advise(_("After resolving the conflicts, mark them with\n"
431 "\"git add/rm <pathspec>\", then run\n"
432 "\"git revert --continue\".\n"
433 "You can instead skip this commit with \"git revert --skip\".\n"
434 "To abort and get back to the state before \"git revert\",\n"
435 "run \"git revert --abort\"."));
437 BUG("unexpected pick action in print_advice()");
441 static int write_message(const void *buf
, size_t len
, const char *filename
,
444 struct lock_file msg_file
= LOCK_INIT
;
446 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
448 return error_errno(_("could not lock '%s'"), filename
);
449 if (write_in_full(msg_fd
, buf
, len
) < 0) {
450 error_errno(_("could not write to '%s'"), filename
);
451 rollback_lock_file(&msg_file
);
454 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
455 error_errno(_("could not write eol to '%s'"), filename
);
456 rollback_lock_file(&msg_file
);
459 if (commit_lock_file(&msg_file
) < 0)
460 return error(_("failed to finalize '%s'"), filename
);
465 int read_oneliner(struct strbuf
*buf
,
466 const char *path
, unsigned flags
)
468 int orig_len
= buf
->len
;
470 if (strbuf_read_file(buf
, path
, 0) < 0) {
471 if ((flags
& READ_ONELINER_WARN_MISSING
) ||
472 (errno
!= ENOENT
&& errno
!= ENOTDIR
))
473 warning_errno(_("could not read '%s'"), path
);
477 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
478 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
480 buf
->buf
[buf
->len
] = '\0';
483 if ((flags
& READ_ONELINER_SKIP_IF_EMPTY
) && buf
->len
== orig_len
)
489 static struct tree
*empty_tree(struct repository
*r
)
491 return lookup_tree(r
, the_hash_algo
->empty_tree
);
494 static int error_dirty_index(struct repository
*repo
, struct replay_opts
*opts
)
496 if (repo_read_index_unmerged(repo
))
497 return error_resolve_conflict(_(action_name(opts
)));
499 error(_("your local changes would be overwritten by %s."),
500 _(action_name(opts
)));
502 if (advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
))
503 advise(_("commit your changes or stash them to proceed."));
507 static void update_abort_safety_file(void)
509 struct object_id head
;
511 /* Do nothing on a single-pick */
512 if (!file_exists(git_path_seq_dir()))
515 if (!get_oid("HEAD", &head
))
516 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
518 write_file(git_path_abort_safety_file(), "%s", "");
521 static int fast_forward_to(struct repository
*r
,
522 const struct object_id
*to
,
523 const struct object_id
*from
,
525 struct replay_opts
*opts
)
527 struct ref_transaction
*transaction
;
528 struct strbuf sb
= STRBUF_INIT
;
529 struct strbuf err
= STRBUF_INIT
;
532 if (checkout_fast_forward(r
, from
, to
, 1))
533 return -1; /* the callee should have complained already */
535 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
537 transaction
= ref_transaction_begin(&err
);
539 ref_transaction_update(transaction
, "HEAD",
540 to
, unborn
&& !is_rebase_i(opts
) ?
543 ref_transaction_commit(transaction
, &err
)) {
544 ref_transaction_free(transaction
);
545 error("%s", err
.buf
);
547 strbuf_release(&err
);
552 strbuf_release(&err
);
553 ref_transaction_free(transaction
);
554 update_abort_safety_file();
558 enum commit_msg_cleanup_mode
get_cleanup_mode(const char *cleanup_arg
,
561 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
562 return use_editor
? COMMIT_MSG_CLEANUP_ALL
:
563 COMMIT_MSG_CLEANUP_SPACE
;
564 else if (!strcmp(cleanup_arg
, "verbatim"))
565 return COMMIT_MSG_CLEANUP_NONE
;
566 else if (!strcmp(cleanup_arg
, "whitespace"))
567 return COMMIT_MSG_CLEANUP_SPACE
;
568 else if (!strcmp(cleanup_arg
, "strip"))
569 return COMMIT_MSG_CLEANUP_ALL
;
570 else if (!strcmp(cleanup_arg
, "scissors"))
571 return use_editor
? COMMIT_MSG_CLEANUP_SCISSORS
:
572 COMMIT_MSG_CLEANUP_SPACE
;
574 die(_("Invalid cleanup mode %s"), cleanup_arg
);
578 * NB using int rather than enum cleanup_mode to stop clang's
579 * -Wtautological-constant-out-of-range-compare complaining that the comparison
582 static const char *describe_cleanup_mode(int cleanup_mode
)
584 static const char *modes
[] = { "whitespace",
589 if (cleanup_mode
< ARRAY_SIZE(modes
))
590 return modes
[cleanup_mode
];
592 BUG("invalid cleanup_mode provided (%d)", cleanup_mode
);
595 void append_conflicts_hint(struct index_state
*istate
,
596 struct strbuf
*msgbuf
, enum commit_msg_cleanup_mode cleanup_mode
)
600 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
601 strbuf_addch(msgbuf
, '\n');
602 wt_status_append_cut_line(msgbuf
);
603 strbuf_addch(msgbuf
, comment_line_char
);
606 strbuf_addch(msgbuf
, '\n');
607 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
608 for (i
= 0; i
< istate
->cache_nr
;) {
609 const struct cache_entry
*ce
= istate
->cache
[i
++];
611 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
612 while (i
< istate
->cache_nr
&&
613 !strcmp(ce
->name
, istate
->cache
[i
]->name
))
619 static int do_recursive_merge(struct repository
*r
,
620 struct commit
*base
, struct commit
*next
,
621 const char *base_label
, const char *next_label
,
622 struct object_id
*head
, struct strbuf
*msgbuf
,
623 struct replay_opts
*opts
)
625 struct merge_options o
;
626 struct merge_result result
;
627 struct tree
*next_tree
, *base_tree
, *head_tree
;
628 int clean
, show_output
;
630 struct lock_file index_lock
= LOCK_INIT
;
632 if (repo_hold_locked_index(r
, &index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
637 init_merge_options(&o
, r
);
638 o
.ancestor
= base
? base_label
: "(empty tree)";
640 o
.branch2
= next
? next_label
: "(empty tree)";
641 if (is_rebase_i(opts
))
643 o
.show_rename_progress
= 1;
645 head_tree
= parse_tree_indirect(head
);
646 next_tree
= next
? get_commit_tree(next
) : empty_tree(r
);
647 base_tree
= base
? get_commit_tree(base
) : empty_tree(r
);
649 for (i
= 0; i
< opts
->xopts_nr
; i
++)
650 parse_merge_opt(&o
, opts
->xopts
[i
]);
652 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "ort")) {
653 memset(&result
, 0, sizeof(result
));
654 merge_incore_nonrecursive(&o
, base_tree
, head_tree
, next_tree
,
656 show_output
= !is_rebase_i(opts
) || !result
.clean
;
658 * TODO: merge_switch_to_result will update index/working tree;
659 * we only really want to do that if !result.clean || this is
660 * the final patch to be picked. But determining this is the
661 * final patch would take some work, and "head_tree" would need
662 * to be replace with the tree the index matched before we
663 * started doing any picks.
665 merge_switch_to_result(&o
, head_tree
, &result
, 1, show_output
);
666 clean
= result
.clean
;
668 ensure_full_index(r
->index
);
669 clean
= merge_trees(&o
, head_tree
, next_tree
, base_tree
);
670 if (is_rebase_i(opts
) && clean
<= 0)
671 fputs(o
.obuf
.buf
, stdout
);
672 strbuf_release(&o
.obuf
);
675 rollback_lock_file(&index_lock
);
679 if (write_locked_index(r
->index
, &index_lock
,
680 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
682 * TRANSLATORS: %s will be "revert", "cherry-pick" or
685 return error(_("%s: Unable to write new index file"),
686 _(action_name(opts
)));
689 append_conflicts_hint(r
->index
, msgbuf
,
690 opts
->default_msg_cleanup
);
695 static struct object_id
*get_cache_tree_oid(struct index_state
*istate
)
697 if (!cache_tree_fully_valid(istate
->cache_tree
))
698 if (cache_tree_update(istate
, 0)) {
699 error(_("unable to update cache tree"));
703 return &istate
->cache_tree
->oid
;
706 static int is_index_unchanged(struct repository
*r
)
708 struct object_id head_oid
, *cache_tree_oid
;
709 struct commit
*head_commit
;
710 struct index_state
*istate
= r
->index
;
712 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
713 return error(_("could not resolve HEAD commit"));
715 head_commit
= lookup_commit(r
, &head_oid
);
718 * If head_commit is NULL, check_commit, called from
719 * lookup_commit, would have indicated that head_commit is not
720 * a commit object already. parse_commit() will return failure
721 * without further complaints in such a case. Otherwise, if
722 * the commit is invalid, parse_commit() will complain. So
723 * there is nothing for us to say here. Just return failure.
725 if (parse_commit(head_commit
))
728 if (!(cache_tree_oid
= get_cache_tree_oid(istate
)))
731 return oideq(cache_tree_oid
, get_commit_tree_oid(head_commit
));
734 static int write_author_script(const char *message
)
736 struct strbuf buf
= STRBUF_INIT
;
741 if (!*message
|| starts_with(message
, "\n")) {
743 /* Missing 'author' line? */
744 unlink(rebase_path_author_script());
746 } else if (skip_prefix(message
, "author ", &message
))
748 else if ((eol
= strchr(message
, '\n')))
753 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
754 while (*message
&& *message
!= '\n' && *message
!= '\r')
755 if (skip_prefix(message
, " <", &message
))
757 else if (*message
!= '\'')
758 strbuf_addch(&buf
, *(message
++));
760 strbuf_addf(&buf
, "'\\%c'", *(message
++));
761 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
762 while (*message
&& *message
!= '\n' && *message
!= '\r')
763 if (skip_prefix(message
, "> ", &message
))
765 else if (*message
!= '\'')
766 strbuf_addch(&buf
, *(message
++));
768 strbuf_addf(&buf
, "'\\%c'", *(message
++));
769 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
770 while (*message
&& *message
!= '\n' && *message
!= '\r')
771 if (*message
!= '\'')
772 strbuf_addch(&buf
, *(message
++));
774 strbuf_addf(&buf
, "'\\%c'", *(message
++));
775 strbuf_addch(&buf
, '\'');
776 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
777 strbuf_release(&buf
);
782 * Take a series of KEY='VALUE' lines where VALUE part is
783 * sq-quoted, and append <KEY, VALUE> at the end of the string list
785 static int parse_key_value_squoted(char *buf
, struct string_list
*list
)
788 struct string_list_item
*item
;
790 char *cp
= strchr(buf
, '=');
792 np
= strchrnul(buf
, '\n');
793 return error(_("no key present in '%.*s'"),
794 (int) (np
- buf
), buf
);
796 np
= strchrnul(cp
, '\n');
798 item
= string_list_append(list
, buf
);
800 buf
= np
+ (*np
== '\n');
804 return error(_("unable to dequote value of '%s'"),
806 item
->util
= xstrdup(cp
);
812 * Reads and parses the state directory's "author-script" file, and sets name,
813 * email and date accordingly.
814 * Returns 0 on success, -1 if the file could not be parsed.
816 * The author script is of the format:
818 * GIT_AUTHOR_NAME='$author_name'
819 * GIT_AUTHOR_EMAIL='$author_email'
820 * GIT_AUTHOR_DATE='$author_date'
822 * where $author_name, $author_email and $author_date are quoted. We are strict
823 * with our parsing, as the file was meant to be eval'd in the now-removed
824 * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
825 * from what this function expects, it is better to bail out than to do
826 * something that the user does not expect.
828 int read_author_script(const char *path
, char **name
, char **email
, char **date
,
831 struct strbuf buf
= STRBUF_INIT
;
832 struct string_list kv
= STRING_LIST_INIT_DUP
;
833 int retval
= -1; /* assume failure */
834 int i
, name_i
= -2, email_i
= -2, date_i
= -2, err
= 0;
836 if (strbuf_read_file(&buf
, path
, 256) <= 0) {
837 strbuf_release(&buf
);
838 if (errno
== ENOENT
&& allow_missing
)
841 return error_errno(_("could not open '%s' for reading"),
845 if (parse_key_value_squoted(buf
.buf
, &kv
))
848 for (i
= 0; i
< kv
.nr
; i
++) {
849 if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_NAME")) {
851 name_i
= error(_("'GIT_AUTHOR_NAME' already given"));
854 } else if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_EMAIL")) {
856 email_i
= error(_("'GIT_AUTHOR_EMAIL' already given"));
859 } else if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_DATE")) {
861 date_i
= error(_("'GIT_AUTHOR_DATE' already given"));
865 err
= error(_("unknown variable '%s'"),
870 error(_("missing 'GIT_AUTHOR_NAME'"));
872 error(_("missing 'GIT_AUTHOR_EMAIL'"));
874 error(_("missing 'GIT_AUTHOR_DATE'"));
875 if (date_i
< 0 || email_i
< 0 || date_i
< 0 || err
)
877 *name
= kv
.items
[name_i
].util
;
878 *email
= kv
.items
[email_i
].util
;
879 *date
= kv
.items
[date_i
].util
;
882 string_list_clear(&kv
, !!retval
);
883 strbuf_release(&buf
);
888 * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
889 * file with shell quoting into struct strvec. Returns -1 on
890 * error, 0 otherwise.
892 static int read_env_script(struct strvec
*env
)
894 char *name
, *email
, *date
;
896 if (read_author_script(rebase_path_author_script(),
897 &name
, &email
, &date
, 0))
900 strvec_pushf(env
, "GIT_AUTHOR_NAME=%s", name
);
901 strvec_pushf(env
, "GIT_AUTHOR_EMAIL=%s", email
);
902 strvec_pushf(env
, "GIT_AUTHOR_DATE=%s", date
);
910 static char *get_author(const char *message
)
915 a
= find_commit_header(message
, "author", &len
);
917 return xmemdupz(a
, len
);
922 static const char *author_date_from_env_array(const struct strvec
*env
)
927 for (i
= 0; i
< env
->nr
; i
++)
928 if (skip_prefix(env
->v
[i
],
929 "GIT_AUTHOR_DATE=", &date
))
932 * If GIT_AUTHOR_DATE is missing we should have already errored out when
935 BUG("GIT_AUTHOR_DATE missing from author script");
938 static const char staged_changes_advice
[] =
939 N_("you have staged changes in your working tree\n"
940 "If these changes are meant to be squashed into the previous commit, run:\n"
942 " git commit --amend %s\n"
944 "If they are meant to go into a new commit, run:\n"
948 "In both cases, once you're done, continue with:\n"
950 " git rebase --continue\n");
952 #define ALLOW_EMPTY (1<<0)
953 #define EDIT_MSG (1<<1)
954 #define AMEND_MSG (1<<2)
955 #define CLEANUP_MSG (1<<3)
956 #define VERIFY_MSG (1<<4)
957 #define CREATE_ROOT_COMMIT (1<<5)
958 #define VERBATIM_MSG (1<<6)
960 static int run_command_silent_on_success(struct child_process
*cmd
)
962 struct strbuf buf
= STRBUF_INIT
;
965 cmd
->stdout_to_stderr
= 1;
966 rc
= pipe_command(cmd
,
972 fputs(buf
.buf
, stderr
);
973 strbuf_release(&buf
);
978 * If we are cherry-pick, and if the merge did not result in
979 * hand-editing, we will hit this commit and inherit the original
980 * author date and name.
982 * If we are revert, or if our cherry-pick results in a hand merge,
983 * we had better say that the current user is responsible for that.
985 * An exception is when run_git_commit() is called during an
986 * interactive rebase: in that case, we will want to retain the
989 static int run_git_commit(const char *defmsg
,
990 struct replay_opts
*opts
,
993 struct child_process cmd
= CHILD_PROCESS_INIT
;
995 if ((flags
& CLEANUP_MSG
) && (flags
& VERBATIM_MSG
))
996 BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
1000 if (is_rebase_i(opts
) &&
1001 ((opts
->committer_date_is_author_date
&& !opts
->ignore_date
) ||
1002 !(!defmsg
&& (flags
& AMEND_MSG
))) &&
1003 read_env_script(&cmd
.env_array
)) {
1004 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
1006 return error(_(staged_changes_advice
),
1010 if (opts
->committer_date_is_author_date
)
1011 strvec_pushf(&cmd
.env_array
, "GIT_COMMITTER_DATE=%s",
1014 author_date_from_env_array(&cmd
.env_array
));
1015 if (opts
->ignore_date
)
1016 strvec_push(&cmd
.env_array
, "GIT_AUTHOR_DATE=");
1018 strvec_push(&cmd
.args
, "commit");
1020 if (!(flags
& VERIFY_MSG
))
1021 strvec_push(&cmd
.args
, "-n");
1022 if ((flags
& AMEND_MSG
))
1023 strvec_push(&cmd
.args
, "--amend");
1025 strvec_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
1027 strvec_push(&cmd
.args
, "--no-gpg-sign");
1029 strvec_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
1030 else if (!(flags
& EDIT_MSG
))
1031 strvec_pushl(&cmd
.args
, "-C", "HEAD", NULL
);
1032 if ((flags
& CLEANUP_MSG
))
1033 strvec_push(&cmd
.args
, "--cleanup=strip");
1034 if ((flags
& VERBATIM_MSG
))
1035 strvec_push(&cmd
.args
, "--cleanup=verbatim");
1036 if ((flags
& EDIT_MSG
))
1037 strvec_push(&cmd
.args
, "-e");
1038 else if (!(flags
& CLEANUP_MSG
) &&
1039 !opts
->signoff
&& !opts
->record_origin
&&
1040 !opts
->explicit_cleanup
)
1041 strvec_push(&cmd
.args
, "--cleanup=verbatim");
1043 if ((flags
& ALLOW_EMPTY
))
1044 strvec_push(&cmd
.args
, "--allow-empty");
1046 if (!(flags
& EDIT_MSG
))
1047 strvec_push(&cmd
.args
, "--allow-empty-message");
1049 if (is_rebase_i(opts
) && !(flags
& EDIT_MSG
))
1050 return run_command_silent_on_success(&cmd
);
1052 return run_command(&cmd
);
1055 static int rest_is_empty(const struct strbuf
*sb
, int start
)
1060 /* Check if the rest is just whitespace and Signed-off-by's. */
1061 for (i
= start
; i
< sb
->len
; i
++) {
1062 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
1068 if (strlen(sign_off_header
) <= eol
- i
&&
1069 starts_with(sb
->buf
+ i
, sign_off_header
)) {
1074 if (!isspace(sb
->buf
[i
++]))
1081 void cleanup_message(struct strbuf
*msgbuf
,
1082 enum commit_msg_cleanup_mode cleanup_mode
, int verbose
)
1084 if (verbose
|| /* Truncate the message just before the diff, if any. */
1085 cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
1086 strbuf_setlen(msgbuf
, wt_status_locate_end(msgbuf
->buf
, msgbuf
->len
));
1087 if (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
)
1088 strbuf_stripspace(msgbuf
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
1092 * Find out if the message in the strbuf contains only whitespace and
1093 * Signed-off-by lines.
1095 int message_is_empty(const struct strbuf
*sb
,
1096 enum commit_msg_cleanup_mode cleanup_mode
)
1098 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
1100 return rest_is_empty(sb
, 0);
1104 * See if the user edited the message in the editor or left what
1105 * was in the template intact
1107 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
1108 enum commit_msg_cleanup_mode cleanup_mode
)
1110 struct strbuf tmpl
= STRBUF_INIT
;
1113 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
1116 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
1119 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
1120 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
1122 strbuf_release(&tmpl
);
1123 return rest_is_empty(sb
, start
- sb
->buf
);
1126 int update_head_with_reflog(const struct commit
*old_head
,
1127 const struct object_id
*new_head
,
1128 const char *action
, const struct strbuf
*msg
,
1131 struct ref_transaction
*transaction
;
1132 struct strbuf sb
= STRBUF_INIT
;
1137 strbuf_addstr(&sb
, action
);
1138 strbuf_addstr(&sb
, ": ");
1141 nl
= strchr(msg
->buf
, '\n');
1143 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
1145 strbuf_addbuf(&sb
, msg
);
1146 strbuf_addch(&sb
, '\n');
1149 transaction
= ref_transaction_begin(err
);
1151 ref_transaction_update(transaction
, "HEAD", new_head
,
1152 old_head
? &old_head
->object
.oid
: null_oid(),
1154 ref_transaction_commit(transaction
, err
)) {
1157 ref_transaction_free(transaction
);
1158 strbuf_release(&sb
);
1163 static int run_rewrite_hook(const struct object_id
*oldoid
,
1164 const struct object_id
*newoid
)
1166 struct child_process proc
= CHILD_PROCESS_INIT
;
1168 struct strbuf sb
= STRBUF_INIT
;
1169 const char *hook_path
= find_hook("post-rewrite");
1174 strvec_pushl(&proc
.args
, hook_path
, "amend", NULL
);
1176 proc
.stdout_to_stderr
= 1;
1177 proc
.trace2_hook_name
= "post-rewrite";
1179 code
= start_command(&proc
);
1182 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
1183 sigchain_push(SIGPIPE
, SIG_IGN
);
1184 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
1186 strbuf_release(&sb
);
1187 sigchain_pop(SIGPIPE
);
1188 return finish_command(&proc
);
1191 void commit_post_rewrite(struct repository
*r
,
1192 const struct commit
*old_head
,
1193 const struct object_id
*new_head
)
1195 struct notes_rewrite_cfg
*cfg
;
1197 cfg
= init_copy_notes_for_rewrite("amend");
1199 /* we are amending, so old_head is not NULL */
1200 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
1201 finish_copy_notes_for_rewrite(r
, cfg
, "Notes added by 'git commit --amend'");
1203 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
1206 static int run_prepare_commit_msg_hook(struct repository
*r
,
1211 const char *name
, *arg1
= NULL
, *arg2
= NULL
;
1213 name
= git_path_commit_editmsg();
1214 if (write_message(msg
->buf
, msg
->len
, name
, 0))
1223 if (run_commit_hook(0, r
->index_file
, "prepare-commit-msg", name
,
1225 ret
= error(_("'prepare-commit-msg' hook failed"));
1230 static const char implicit_ident_advice_noconfig
[] =
1231 N_("Your name and email address were configured automatically based\n"
1232 "on your username and hostname. Please check that they are accurate.\n"
1233 "You can suppress this message by setting them explicitly. Run the\n"
1234 "following command and follow the instructions in your editor to edit\n"
1235 "your configuration file:\n"
1237 " git config --global --edit\n"
1239 "After doing this, you may fix the identity used for this commit with:\n"
1241 " git commit --amend --reset-author\n");
1243 static const char implicit_ident_advice_config
[] =
1244 N_("Your name and email address were configured automatically based\n"
1245 "on your username and hostname. Please check that they are accurate.\n"
1246 "You can suppress this message by setting them explicitly:\n"
1248 " git config --global user.name \"Your Name\"\n"
1249 " git config --global user.email you@example.com\n"
1251 "After doing this, you may fix the identity used for this commit with:\n"
1253 " git commit --amend --reset-author\n");
1255 static const char *implicit_ident_advice(void)
1257 char *user_config
= interpolate_path("~/.gitconfig", 0);
1258 char *xdg_config
= xdg_config_home("config");
1259 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
1265 return _(implicit_ident_advice_config
);
1267 return _(implicit_ident_advice_noconfig
);
1271 void print_commit_summary(struct repository
*r
,
1273 const struct object_id
*oid
,
1276 struct rev_info rev
;
1277 struct commit
*commit
;
1278 struct strbuf format
= STRBUF_INIT
;
1280 struct pretty_print_context pctx
= {0};
1281 struct strbuf author_ident
= STRBUF_INIT
;
1282 struct strbuf committer_ident
= STRBUF_INIT
;
1283 struct ref_store
*refs
;
1286 commit
= lookup_commit(r
, oid
);
1288 die(_("couldn't look up newly created commit"));
1289 if (parse_commit(commit
))
1290 die(_("could not parse newly created commit"));
1292 strbuf_addstr(&format
, "format:%h] %s");
1294 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1295 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1296 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1297 strbuf_addstr(&format
, "\n Author: ");
1298 strbuf_addbuf_percentquote(&format
, &author_ident
);
1300 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
1301 struct strbuf date
= STRBUF_INIT
;
1303 format_commit_message(commit
, "%ad", &date
, &pctx
);
1304 strbuf_addstr(&format
, "\n Date: ");
1305 strbuf_addbuf_percentquote(&format
, &date
);
1306 strbuf_release(&date
);
1308 if (!committer_ident_sufficiently_given()) {
1309 strbuf_addstr(&format
, "\n Committer: ");
1310 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1311 if (advice_enabled(ADVICE_IMPLICIT_IDENTITY
)) {
1312 strbuf_addch(&format
, '\n');
1313 strbuf_addstr(&format
, implicit_ident_advice());
1316 strbuf_release(&author_ident
);
1317 strbuf_release(&committer_ident
);
1319 repo_init_revisions(r
, &rev
, prefix
);
1320 setup_revisions(0, NULL
, &rev
, NULL
);
1323 rev
.diffopt
.output_format
=
1324 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1326 rev
.verbose_header
= 1;
1327 rev
.show_root_diff
= 1;
1328 get_commit_format(format
.buf
, &rev
);
1329 rev
.always_show_header
= 0;
1330 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1331 diff_setup_done(&rev
.diffopt
);
1333 refs
= get_main_ref_store(the_repository
);
1334 head
= refs_resolve_ref_unsafe(refs
, "HEAD", 0, NULL
, NULL
,
1337 errno
= resolve_errno
;
1338 die_errno(_("unable to resolve HEAD after creating commit"));
1340 if (!strcmp(head
, "HEAD"))
1341 head
= _("detached HEAD");
1343 skip_prefix(head
, "refs/heads/", &head
);
1344 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1345 _(" (root-commit)") : "");
1347 if (!log_tree_commit(&rev
, commit
)) {
1348 rev
.always_show_header
= 1;
1349 rev
.use_terminator
= 1;
1350 log_tree_commit(&rev
, commit
);
1353 strbuf_release(&format
);
1356 static int parse_head(struct repository
*r
, struct commit
**head
)
1358 struct commit
*current_head
;
1359 struct object_id oid
;
1361 if (get_oid("HEAD", &oid
)) {
1362 current_head
= NULL
;
1364 current_head
= lookup_commit_reference(r
, &oid
);
1366 return error(_("could not parse HEAD"));
1367 if (!oideq(&oid
, ¤t_head
->object
.oid
)) {
1368 warning(_("HEAD %s is not a commit!"),
1371 if (parse_commit(current_head
))
1372 return error(_("could not parse HEAD commit"));
1374 *head
= current_head
;
1380 * Try to commit without forking 'git commit'. In some cases we need
1381 * to run 'git commit' to display an error message
1384 * -1 - error unable to commit
1386 * 1 - run 'git commit'
1388 static int try_to_commit(struct repository
*r
,
1389 struct strbuf
*msg
, const char *author
,
1390 struct replay_opts
*opts
, unsigned int flags
,
1391 struct object_id
*oid
)
1393 struct object_id tree
;
1394 struct commit
*current_head
= NULL
;
1395 struct commit_list
*parents
= NULL
;
1396 struct commit_extra_header
*extra
= NULL
;
1397 struct strbuf err
= STRBUF_INIT
;
1398 struct strbuf commit_msg
= STRBUF_INIT
;
1399 char *amend_author
= NULL
;
1400 const char *committer
= NULL
;
1401 const char *hook_commit
= NULL
;
1402 enum commit_msg_cleanup_mode cleanup
;
1405 if ((flags
& CLEANUP_MSG
) && (flags
& VERBATIM_MSG
))
1406 BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
1408 if (parse_head(r
, ¤t_head
))
1411 if (flags
& AMEND_MSG
) {
1412 const char *exclude_gpgsig
[] = { "gpgsig", "gpgsig-sha256", NULL
};
1413 const char *out_enc
= get_commit_output_encoding();
1414 const char *message
= logmsg_reencode(current_head
, NULL
,
1418 const char *orig_message
= NULL
;
1420 find_commit_subject(message
, &orig_message
);
1422 strbuf_addstr(msg
, orig_message
);
1423 hook_commit
= "HEAD";
1425 author
= amend_author
= get_author(message
);
1426 unuse_commit_buffer(current_head
, message
);
1428 res
= error(_("unable to parse commit author"));
1431 parents
= copy_commit_list(current_head
->parents
);
1432 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1433 } else if (current_head
&&
1434 (!(flags
& CREATE_ROOT_COMMIT
) || (flags
& AMEND_MSG
))) {
1435 commit_list_insert(current_head
, &parents
);
1438 if (write_index_as_tree(&tree
, r
->index
, r
->index_file
, 0, NULL
)) {
1439 res
= error(_("git write-tree failed to write a tree"));
1443 if (!(flags
& ALLOW_EMPTY
)) {
1444 struct commit
*first_parent
= current_head
;
1446 if (flags
& AMEND_MSG
) {
1447 if (current_head
->parents
) {
1448 first_parent
= current_head
->parents
->item
;
1449 if (repo_parse_commit(r
, first_parent
)) {
1450 res
= error(_("could not parse HEAD commit"));
1454 first_parent
= NULL
;
1457 if (oideq(first_parent
1458 ? get_commit_tree_oid(first_parent
)
1459 : the_hash_algo
->empty_tree
,
1461 res
= 1; /* run 'git commit' to display error message */
1466 if (hook_exists("prepare-commit-msg")) {
1467 res
= run_prepare_commit_msg_hook(r
, msg
, hook_commit
);
1470 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1472 res
= error_errno(_("unable to read commit message "
1474 git_path_commit_editmsg());
1480 if (flags
& CLEANUP_MSG
)
1481 cleanup
= COMMIT_MSG_CLEANUP_ALL
;
1482 else if (flags
& VERBATIM_MSG
)
1483 cleanup
= COMMIT_MSG_CLEANUP_NONE
;
1484 else if ((opts
->signoff
|| opts
->record_origin
) &&
1485 !opts
->explicit_cleanup
)
1486 cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
1488 cleanup
= opts
->default_msg_cleanup
;
1490 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1491 strbuf_stripspace(msg
, cleanup
== COMMIT_MSG_CLEANUP_ALL
);
1492 if ((flags
& EDIT_MSG
) && message_is_empty(msg
, cleanup
)) {
1493 res
= 1; /* run 'git commit' to display error message */
1497 if (opts
->committer_date_is_author_date
) {
1498 struct ident_split id
;
1499 struct strbuf date
= STRBUF_INIT
;
1501 if (!opts
->ignore_date
) {
1502 if (split_ident_line(&id
, author
, (int)strlen(author
)) < 0) {
1503 res
= error(_("invalid author identity '%s'"),
1507 if (!id
.date_begin
) {
1509 "corrupt author: missing date information"));
1512 strbuf_addf(&date
, "@%.*s %.*s",
1513 (int)(id
.date_end
- id
.date_begin
),
1515 (int)(id
.tz_end
- id
.tz_begin
),
1520 committer
= fmt_ident(getenv("GIT_COMMITTER_NAME"),
1521 getenv("GIT_COMMITTER_EMAIL"),
1522 WANT_COMMITTER_IDENT
,
1523 opts
->ignore_date
? NULL
: date
.buf
,
1525 strbuf_release(&date
);
1530 if (opts
->ignore_date
) {
1531 struct ident_split id
;
1534 if (split_ident_line(&id
, author
, strlen(author
)) < 0) {
1535 error(_("invalid author identity '%s'"), author
);
1538 name
= xmemdupz(id
.name_begin
, id
.name_end
- id
.name_begin
);
1539 email
= xmemdupz(id
.mail_begin
, id
.mail_end
- id
.mail_begin
);
1540 author
= fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, NULL
,
1546 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
, oid
,
1547 author
, committer
, opts
->gpg_sign
, extra
)) {
1548 res
= error(_("failed to write commit object"));
1552 if (update_head_with_reflog(current_head
, oid
,
1553 getenv("GIT_REFLOG_ACTION"), msg
, &err
)) {
1554 res
= error("%s", err
.buf
);
1558 run_commit_hook(0, r
->index_file
, "post-commit", NULL
);
1559 if (flags
& AMEND_MSG
)
1560 commit_post_rewrite(r
, current_head
, oid
);
1563 free_commit_extra_headers(extra
);
1564 strbuf_release(&err
);
1565 strbuf_release(&commit_msg
);
1571 static int write_rebase_head(struct object_id
*oid
)
1573 if (update_ref("rebase", "REBASE_HEAD", oid
,
1574 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1575 return error(_("could not update %s"), "REBASE_HEAD");
1580 static int do_commit(struct repository
*r
,
1581 const char *msg_file
, const char *author
,
1582 struct replay_opts
*opts
, unsigned int flags
,
1583 struct object_id
*oid
)
1587 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
)) {
1588 struct object_id oid
;
1589 struct strbuf sb
= STRBUF_INIT
;
1591 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1592 return error_errno(_("unable to read commit message "
1596 res
= try_to_commit(r
, msg_file
? &sb
: NULL
,
1597 author
, opts
, flags
, &oid
);
1598 strbuf_release(&sb
);
1600 refs_delete_ref(get_main_ref_store(r
), "",
1601 "CHERRY_PICK_HEAD", NULL
, 0);
1602 unlink(git_path_merge_msg(r
));
1603 if (!is_rebase_i(opts
))
1604 print_commit_summary(r
, NULL
, &oid
,
1605 SUMMARY_SHOW_AUTHOR_DATE
);
1610 if (is_rebase_i(opts
) && oid
)
1611 if (write_rebase_head(oid
))
1613 return run_git_commit(msg_file
, opts
, flags
);
1619 static int is_original_commit_empty(struct commit
*commit
)
1621 const struct object_id
*ptree_oid
;
1623 if (parse_commit(commit
))
1624 return error(_("could not parse commit %s"),
1625 oid_to_hex(&commit
->object
.oid
));
1626 if (commit
->parents
) {
1627 struct commit
*parent
= commit
->parents
->item
;
1628 if (parse_commit(parent
))
1629 return error(_("could not parse parent commit %s"),
1630 oid_to_hex(&parent
->object
.oid
));
1631 ptree_oid
= get_commit_tree_oid(parent
);
1633 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1636 return oideq(ptree_oid
, get_commit_tree_oid(commit
));
1640 * Should empty commits be allowed? Return status:
1641 * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit)
1642 * 0: Halt on empty commit
1643 * 1: Allow empty commit
1644 * 2: Drop empty commit
1646 static int allow_empty(struct repository
*r
,
1647 struct replay_opts
*opts
,
1648 struct commit
*commit
)
1650 int index_unchanged
, originally_empty
;
1655 * (1) we do not allow empty at all and error out.
1657 * (2) we allow ones that were initially empty, and
1658 * just drop the ones that become empty
1660 * (3) we allow ones that were initially empty, but
1661 * halt for the ones that become empty;
1663 * (4) we allow both.
1665 if (!opts
->allow_empty
)
1666 return 0; /* let "git commit" barf as necessary */
1668 index_unchanged
= is_index_unchanged(r
);
1669 if (index_unchanged
< 0)
1670 return index_unchanged
;
1671 if (!index_unchanged
)
1672 return 0; /* we do not have to say --allow-empty */
1674 if (opts
->keep_redundant_commits
)
1677 originally_empty
= is_original_commit_empty(commit
);
1678 if (originally_empty
< 0)
1679 return originally_empty
;
1680 if (originally_empty
)
1682 else if (opts
->drop_redundant_commits
)
1691 } todo_command_info
[] = {
1708 static const char *command_to_string(const enum todo_command command
)
1710 if (command
< TODO_COMMENT
)
1711 return todo_command_info
[command
].str
;
1712 die(_("unknown command: %d"), command
);
1715 static char command_to_char(const enum todo_command command
)
1717 if (command
< TODO_COMMENT
)
1718 return todo_command_info
[command
].c
;
1719 return comment_line_char
;
1722 static int is_noop(const enum todo_command command
)
1724 return TODO_NOOP
<= command
;
1727 static int is_fixup(enum todo_command command
)
1729 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1732 /* Does this command create a (non-merge) commit? */
1733 static int is_pick_or_similar(enum todo_command command
)
1748 enum todo_item_flags
{
1749 TODO_EDIT_MERGE_MSG
= (1 << 0),
1750 TODO_REPLACE_FIXUP_MSG
= (1 << 1),
1751 TODO_EDIT_FIXUP_MSG
= (1 << 2),
1754 static const char first_commit_msg_str
[] = N_("This is the 1st commit message:");
1755 static const char nth_commit_msg_fmt
[] = N_("This is the commit message #%d:");
1756 static const char skip_first_commit_msg_str
[] = N_("The 1st commit message will be skipped:");
1757 static const char skip_nth_commit_msg_fmt
[] = N_("The commit message #%d will be skipped:");
1758 static const char combined_commit_msg_fmt
[] = N_("This is a combination of %d commits.");
1760 static int is_fixup_flag(enum todo_command command
, unsigned flag
)
1762 return command
== TODO_FIXUP
&& ((flag
& TODO_REPLACE_FIXUP_MSG
) ||
1763 (flag
& TODO_EDIT_FIXUP_MSG
));
1767 * Wrapper around strbuf_add_commented_lines() which avoids double
1768 * commenting commit subjects.
1770 static void add_commented_lines(struct strbuf
*buf
, const void *str
, size_t len
)
1772 const char *s
= str
;
1773 while (len
> 0 && s
[0] == comment_line_char
) {
1775 const char *n
= memchr(s
, '\n', len
);
1780 strbuf_add(buf
, s
, count
);
1784 strbuf_add_commented_lines(buf
, s
, len
);
1787 /* Does the current fixup chain contain a squash command? */
1788 static int seen_squash(struct replay_opts
*opts
)
1790 return starts_with(opts
->current_fixups
.buf
, "squash") ||
1791 strstr(opts
->current_fixups
.buf
, "\nsquash");
1794 static void update_comment_bufs(struct strbuf
*buf1
, struct strbuf
*buf2
, int n
)
1796 strbuf_setlen(buf1
, 2);
1797 strbuf_addf(buf1
, _(nth_commit_msg_fmt
), n
);
1798 strbuf_addch(buf1
, '\n');
1799 strbuf_setlen(buf2
, 2);
1800 strbuf_addf(buf2
, _(skip_nth_commit_msg_fmt
), n
);
1801 strbuf_addch(buf2
, '\n');
1805 * Comment out any un-commented commit messages, updating the message comments
1806 * to say they will be skipped but do not comment out the empty lines that
1807 * surround commit messages and their comments.
1809 static void update_squash_message_for_fixup(struct strbuf
*msg
)
1811 void (*copy_lines
)(struct strbuf
*, const void *, size_t) = strbuf_add
;
1812 struct strbuf buf1
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1813 const char *s
, *start
;
1815 size_t orig_msg_len
;
1818 strbuf_addf(&buf1
, "# %s\n", _(first_commit_msg_str
));
1819 strbuf_addf(&buf2
, "# %s\n", _(skip_first_commit_msg_str
));
1820 s
= start
= orig_msg
= strbuf_detach(msg
, &orig_msg_len
);
1824 if (skip_prefix(s
, buf1
.buf
, &next
)) {
1826 * Copy the last message, preserving the blank line
1827 * preceding the current line
1829 off
= (s
> start
+ 1 && s
[-2] == '\n') ? 1 : 0;
1830 copy_lines(msg
, start
, s
- start
- off
);
1832 strbuf_addch(msg
, '\n');
1834 * The next message needs to be commented out but the
1835 * message header is already commented out so just copy
1836 * it and the blank line that follows it.
1838 strbuf_addbuf(msg
, &buf2
);
1840 strbuf_addch(msg
, *next
++);
1842 copy_lines
= add_commented_lines
;
1843 update_comment_bufs(&buf1
, &buf2
, ++i
);
1844 } else if (skip_prefix(s
, buf2
.buf
, &next
)) {
1845 off
= (s
> start
+ 1 && s
[-2] == '\n') ? 1 : 0;
1846 copy_lines(msg
, start
, s
- start
- off
);
1849 copy_lines
= strbuf_add
;
1850 update_comment_bufs(&buf1
, &buf2
, ++i
);
1852 s
= strchr(s
, '\n');
1857 copy_lines(msg
, start
, orig_msg_len
- (start
- orig_msg
));
1859 strbuf_release(&buf1
);
1860 strbuf_release(&buf2
);
1863 static int append_squash_message(struct strbuf
*buf
, const char *body
,
1864 enum todo_command command
, struct replay_opts
*opts
,
1867 const char *fixup_msg
;
1868 size_t commented_len
= 0, fixup_off
;
1870 * amend is non-interactive and not normally used with fixup!
1871 * or squash! commits, so only comment out those subjects when
1872 * squashing commit messages.
1874 if (starts_with(body
, "amend!") ||
1875 ((command
== TODO_SQUASH
|| seen_squash(opts
)) &&
1876 (starts_with(body
, "squash!") || starts_with(body
, "fixup!"))))
1877 commented_len
= commit_subject_length(body
);
1879 strbuf_addf(buf
, "\n%c ", comment_line_char
);
1880 strbuf_addf(buf
, _(nth_commit_msg_fmt
),
1881 ++opts
->current_fixup_count
+ 1);
1882 strbuf_addstr(buf
, "\n\n");
1883 strbuf_add_commented_lines(buf
, body
, commented_len
);
1884 /* buf->buf may be reallocated so store an offset into the buffer */
1885 fixup_off
= buf
->len
;
1886 strbuf_addstr(buf
, body
+ commented_len
);
1888 /* fixup -C after squash behaves like squash */
1889 if (is_fixup_flag(command
, flag
) && !seen_squash(opts
)) {
1891 * We're replacing the commit message so we need to
1892 * append the Signed-off-by: trailer if the user
1893 * requested '--signoff'.
1896 append_signoff(buf
, 0, 0);
1898 if ((command
== TODO_FIXUP
) &&
1899 (flag
& TODO_REPLACE_FIXUP_MSG
) &&
1900 (file_exists(rebase_path_fixup_msg()) ||
1901 !file_exists(rebase_path_squash_msg()))) {
1902 fixup_msg
= skip_blank_lines(buf
->buf
+ fixup_off
);
1903 if (write_message(fixup_msg
, strlen(fixup_msg
),
1904 rebase_path_fixup_msg(), 0) < 0)
1905 return error(_("cannot write '%s'"),
1906 rebase_path_fixup_msg());
1908 unlink(rebase_path_fixup_msg());
1911 unlink(rebase_path_fixup_msg());
1917 static int update_squash_messages(struct repository
*r
,
1918 enum todo_command command
,
1919 struct commit
*commit
,
1920 struct replay_opts
*opts
,
1923 struct strbuf buf
= STRBUF_INIT
;
1925 const char *message
, *body
;
1926 const char *encoding
= get_commit_output_encoding();
1928 if (opts
->current_fixup_count
> 0) {
1929 struct strbuf header
= STRBUF_INIT
;
1932 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 9) <= 0)
1933 return error(_("could not read '%s'"),
1934 rebase_path_squash_msg());
1936 eol
= buf
.buf
[0] != comment_line_char
?
1937 buf
.buf
: strchrnul(buf
.buf
, '\n');
1939 strbuf_addf(&header
, "%c ", comment_line_char
);
1940 strbuf_addf(&header
, _(combined_commit_msg_fmt
),
1941 opts
->current_fixup_count
+ 2);
1942 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1943 strbuf_release(&header
);
1944 if (is_fixup_flag(command
, flag
) && !seen_squash(opts
))
1945 update_squash_message_for_fixup(&buf
);
1947 struct object_id head
;
1948 struct commit
*head_commit
;
1949 const char *head_message
, *body
;
1951 if (get_oid("HEAD", &head
))
1952 return error(_("need a HEAD to fixup"));
1953 if (!(head_commit
= lookup_commit_reference(r
, &head
)))
1954 return error(_("could not read HEAD"));
1955 if (!(head_message
= logmsg_reencode(head_commit
, NULL
, encoding
)))
1956 return error(_("could not read HEAD's commit message"));
1958 find_commit_subject(head_message
, &body
);
1959 if (command
== TODO_FIXUP
&& !flag
&& write_message(body
, strlen(body
),
1960 rebase_path_fixup_msg(), 0) < 0) {
1961 unuse_commit_buffer(head_commit
, head_message
);
1962 return error(_("cannot write '%s'"), rebase_path_fixup_msg());
1964 strbuf_addf(&buf
, "%c ", comment_line_char
);
1965 strbuf_addf(&buf
, _(combined_commit_msg_fmt
), 2);
1966 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1967 strbuf_addstr(&buf
, is_fixup_flag(command
, flag
) ?
1968 _(skip_first_commit_msg_str
) :
1969 _(first_commit_msg_str
));
1970 strbuf_addstr(&buf
, "\n\n");
1971 if (is_fixup_flag(command
, flag
))
1972 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1974 strbuf_addstr(&buf
, body
);
1976 unuse_commit_buffer(head_commit
, head_message
);
1979 if (!(message
= logmsg_reencode(commit
, NULL
, encoding
)))
1980 return error(_("could not read commit message of %s"),
1981 oid_to_hex(&commit
->object
.oid
));
1982 find_commit_subject(message
, &body
);
1984 if (command
== TODO_SQUASH
|| is_fixup_flag(command
, flag
)) {
1985 res
= append_squash_message(&buf
, body
, command
, opts
, flag
);
1986 } else if (command
== TODO_FIXUP
) {
1987 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1988 strbuf_addf(&buf
, _(skip_nth_commit_msg_fmt
),
1989 ++opts
->current_fixup_count
+ 1);
1990 strbuf_addstr(&buf
, "\n\n");
1991 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1993 return error(_("unknown command: %d"), command
);
1994 unuse_commit_buffer(commit
, message
);
1997 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(),
1999 strbuf_release(&buf
);
2002 strbuf_addf(&opts
->current_fixups
, "%s%s %s",
2003 opts
->current_fixups
.len
? "\n" : "",
2004 command_to_string(command
),
2005 oid_to_hex(&commit
->object
.oid
));
2006 res
= write_message(opts
->current_fixups
.buf
,
2007 opts
->current_fixups
.len
,
2008 rebase_path_current_fixups(), 0);
2014 static void flush_rewritten_pending(void)
2016 struct strbuf buf
= STRBUF_INIT
;
2017 struct object_id newoid
;
2020 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
2021 !get_oid("HEAD", &newoid
) &&
2022 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
2023 char *bol
= buf
.buf
, *eol
;
2026 eol
= strchrnul(bol
, '\n');
2027 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
2028 bol
, oid_to_hex(&newoid
));
2034 unlink(rebase_path_rewritten_pending());
2036 strbuf_release(&buf
);
2039 static void record_in_rewritten(struct object_id
*oid
,
2040 enum todo_command next_command
)
2042 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
2047 fprintf(out
, "%s\n", oid_to_hex(oid
));
2050 if (!is_fixup(next_command
))
2051 flush_rewritten_pending();
2054 static int should_edit(struct replay_opts
*opts
) {
2057 * Note that we only handle the case of non-conflicted
2058 * commits; continue_single_pick() handles the conflicted
2059 * commits itself instead of calling this function.
2061 return (opts
->action
== REPLAY_REVERT
&& isatty(0)) ? 1 : 0;
2065 static int do_pick_commit(struct repository
*r
,
2066 struct todo_item
*item
,
2067 struct replay_opts
*opts
,
2068 int final_fixup
, int *check_todo
)
2070 unsigned int flags
= should_edit(opts
) ? EDIT_MSG
: 0;
2071 const char *msg_file
= should_edit(opts
) ? NULL
: git_path_merge_msg(r
);
2072 struct object_id head
;
2073 struct commit
*base
, *next
, *parent
;
2074 const char *base_label
, *next_label
;
2075 char *author
= NULL
;
2076 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
2077 struct strbuf msgbuf
= STRBUF_INIT
;
2078 int res
, unborn
= 0, reword
= 0, allow
, drop_commit
;
2079 enum todo_command command
= item
->command
;
2080 struct commit
*commit
= item
->commit
;
2082 if (opts
->no_commit
) {
2084 * We do not intend to commit immediately. We just want to
2085 * merge the differences in, so let's compute the tree
2086 * that represents the "current" state for the merge machinery
2089 if (write_index_as_tree(&head
, r
->index
, r
->index_file
, 0, NULL
))
2090 return error(_("your index file is unmerged."));
2092 unborn
= get_oid("HEAD", &head
);
2093 /* Do we want to generate a root commit? */
2094 if (is_pick_or_similar(command
) && opts
->have_squash_onto
&&
2095 oideq(&head
, &opts
->squash_onto
)) {
2096 if (is_fixup(command
))
2097 return error(_("cannot fixup root commit"));
2098 flags
|= CREATE_ROOT_COMMIT
;
2101 oidcpy(&head
, the_hash_algo
->empty_tree
);
2102 if (index_differs_from(r
, unborn
? empty_tree_oid_hex() : "HEAD",
2104 return error_dirty_index(r
, opts
);
2106 discard_index(r
->index
);
2108 if (!commit
->parents
)
2110 else if (commit
->parents
->next
) {
2111 /* Reverting or cherry-picking a merge commit */
2113 struct commit_list
*p
;
2115 if (!opts
->mainline
)
2116 return error(_("commit %s is a merge but no -m option was given."),
2117 oid_to_hex(&commit
->object
.oid
));
2119 for (cnt
= 1, p
= commit
->parents
;
2120 cnt
!= opts
->mainline
&& p
;
2123 if (cnt
!= opts
->mainline
|| !p
)
2124 return error(_("commit %s does not have parent %d"),
2125 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
2127 } else if (1 < opts
->mainline
)
2129 * Non-first parent explicitly specified as mainline for
2132 return error(_("commit %s does not have parent %d"),
2133 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
2135 parent
= commit
->parents
->item
;
2137 if (get_message(commit
, &msg
) != 0)
2138 return error(_("cannot get commit message for %s"),
2139 oid_to_hex(&commit
->object
.oid
));
2141 if (opts
->allow_ff
&& !is_fixup(command
) &&
2142 ((parent
&& oideq(&parent
->object
.oid
, &head
)) ||
2143 (!parent
&& unborn
))) {
2144 if (is_rebase_i(opts
))
2145 write_author_script(msg
.message
);
2146 res
= fast_forward_to(r
, &commit
->object
.oid
, &head
, unborn
,
2148 if (res
|| command
!= TODO_REWORD
)
2152 goto fast_forward_edit
;
2154 if (parent
&& parse_commit(parent
) < 0)
2155 /* TRANSLATORS: The first %s will be a "todo" command like
2156 "revert" or "pick", the second %s a SHA1. */
2157 return error(_("%s: cannot parse parent commit %s"),
2158 command_to_string(command
),
2159 oid_to_hex(&parent
->object
.oid
));
2162 * "commit" is an existing commit. We would want to apply
2163 * the difference it introduces since its first parent "prev"
2164 * on top of the current HEAD if we are cherry-pick. Or the
2165 * reverse of it if we are revert.
2168 if (command
== TODO_REVERT
) {
2170 base_label
= msg
.label
;
2172 next_label
= msg
.parent_label
;
2173 strbuf_addstr(&msgbuf
, "Revert \"");
2174 strbuf_addstr(&msgbuf
, msg
.subject
);
2175 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
2176 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
2178 if (commit
->parents
&& commit
->parents
->next
) {
2179 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
2180 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
2182 strbuf_addstr(&msgbuf
, ".\n");
2187 base_label
= msg
.parent_label
;
2189 next_label
= msg
.label
;
2191 /* Append the commit log message to msgbuf. */
2192 if (find_commit_subject(msg
.message
, &p
))
2193 strbuf_addstr(&msgbuf
, p
);
2195 if (opts
->record_origin
) {
2196 strbuf_complete_line(&msgbuf
);
2197 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
2198 strbuf_addch(&msgbuf
, '\n');
2199 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
2200 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
2201 strbuf_addstr(&msgbuf
, ")\n");
2203 if (!is_fixup(command
))
2204 author
= get_author(msg
.message
);
2207 if (command
== TODO_REWORD
)
2209 else if (is_fixup(command
)) {
2210 if (update_squash_messages(r
, command
, commit
,
2215 msg_file
= rebase_path_squash_msg();
2216 else if (file_exists(rebase_path_fixup_msg())) {
2217 flags
|= VERBATIM_MSG
;
2218 msg_file
= rebase_path_fixup_msg();
2220 const char *dest
= git_path_squash_msg(r
);
2222 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
2223 return error(_("could not rename '%s' to '%s'"),
2224 rebase_path_squash_msg(), dest
);
2225 unlink(git_path_merge_msg(r
));
2231 if (opts
->signoff
&& !is_fixup(command
))
2232 append_signoff(&msgbuf
, 0, 0);
2234 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
2236 else if (!opts
->strategy
||
2237 !strcmp(opts
->strategy
, "recursive") ||
2238 !strcmp(opts
->strategy
, "ort") ||
2239 command
== TODO_REVERT
) {
2240 res
= do_recursive_merge(r
, base
, next
, base_label
, next_label
,
2241 &head
, &msgbuf
, opts
);
2245 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
2246 git_path_merge_msg(r
), 0);
2248 struct commit_list
*common
= NULL
;
2249 struct commit_list
*remotes
= NULL
;
2251 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
2252 git_path_merge_msg(r
), 0);
2254 commit_list_insert(base
, &common
);
2255 commit_list_insert(next
, &remotes
);
2256 res
|= try_merge_command(r
, opts
->strategy
,
2257 opts
->xopts_nr
, (const char **)opts
->xopts
,
2258 common
, oid_to_hex(&head
), remotes
);
2259 free_commit_list(common
);
2260 free_commit_list(remotes
);
2262 strbuf_release(&msgbuf
);
2265 * If the merge was clean or if it failed due to conflict, we write
2266 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
2267 * However, if the merge did not even start, then we don't want to
2270 if ((command
== TODO_PICK
|| command
== TODO_REWORD
||
2271 command
== TODO_EDIT
) && !opts
->no_commit
&&
2272 (res
== 0 || res
== 1) &&
2273 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
2274 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2276 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
2277 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
2278 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2282 error(command
== TODO_REVERT
2283 ? _("could not revert %s... %s")
2284 : _("could not apply %s... %s"),
2285 short_commit_name(commit
), msg
.subject
);
2286 print_advice(r
, res
== 1, opts
);
2287 repo_rerere(r
, opts
->allow_rerere_auto
);
2292 allow
= allow_empty(r
, opts
, commit
);
2296 } else if (allow
== 1) {
2297 flags
|= ALLOW_EMPTY
;
2298 } else if (allow
== 2) {
2300 refs_delete_ref(get_main_ref_store(r
), "", "CHERRY_PICK_HEAD",
2302 unlink(git_path_merge_msg(r
));
2303 unlink(git_path_auto_merge(r
));
2305 _("dropping %s %s -- patch contents already upstream\n"),
2306 oid_to_hex(&commit
->object
.oid
), msg
.subject
);
2307 } /* else allow == 0 and there's nothing special to do */
2308 if (!opts
->no_commit
&& !drop_commit
) {
2309 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
2310 res
= do_commit(r
, msg_file
, author
, opts
, flags
,
2311 commit
? &commit
->object
.oid
: NULL
);
2313 res
= error(_("unable to parse commit author"));
2314 *check_todo
= !!(flags
& EDIT_MSG
);
2315 if (!res
&& reword
) {
2317 res
= run_git_commit(NULL
, opts
, EDIT_MSG
|
2318 VERIFY_MSG
| AMEND_MSG
|
2319 (flags
& ALLOW_EMPTY
));
2325 if (!res
&& final_fixup
) {
2326 unlink(rebase_path_fixup_msg());
2327 unlink(rebase_path_squash_msg());
2328 unlink(rebase_path_current_fixups());
2329 strbuf_reset(&opts
->current_fixups
);
2330 opts
->current_fixup_count
= 0;
2334 free_message(commit
, &msg
);
2336 update_abort_safety_file();
2341 static int prepare_revs(struct replay_opts
*opts
)
2344 * picking (but not reverting) ranges (but not individual revisions)
2345 * should be done in reverse
2347 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
2348 opts
->revs
->reverse
^= 1;
2350 if (prepare_revision_walk(opts
->revs
))
2351 return error(_("revision walk setup failed"));
2356 static int read_and_refresh_cache(struct repository
*r
,
2357 struct replay_opts
*opts
)
2359 struct lock_file index_lock
= LOCK_INIT
;
2360 int index_fd
= repo_hold_locked_index(r
, &index_lock
, 0);
2361 if (repo_read_index(r
) < 0) {
2362 rollback_lock_file(&index_lock
);
2363 return error(_("git %s: failed to read the index"),
2364 _(action_name(opts
)));
2366 refresh_index(r
->index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
2368 if (index_fd
>= 0) {
2369 if (write_locked_index(r
->index
, &index_lock
,
2370 COMMIT_LOCK
| SKIP_IF_UNCHANGED
)) {
2371 return error(_("git %s: failed to refresh the index"),
2372 _(action_name(opts
)));
2377 * If we are resolving merges in any way other than "ort", then
2378 * expand the sparse index.
2380 if (opts
->strategy
&& strcmp(opts
->strategy
, "ort"))
2381 ensure_full_index(r
->index
);
2385 void todo_list_release(struct todo_list
*todo_list
)
2387 strbuf_release(&todo_list
->buf
);
2388 FREE_AND_NULL(todo_list
->items
);
2389 todo_list
->nr
= todo_list
->alloc
= 0;
2392 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
2394 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
2395 todo_list
->total_nr
++;
2396 return todo_list
->items
+ todo_list
->nr
++;
2399 const char *todo_item_get_arg(struct todo_list
*todo_list
,
2400 struct todo_item
*item
)
2402 return todo_list
->buf
.buf
+ item
->arg_offset
;
2405 static int is_command(enum todo_command command
, const char **bol
)
2407 const char *str
= todo_command_info
[command
].str
;
2408 const char nick
= todo_command_info
[command
].c
;
2409 const char *p
= *bol
+ 1;
2411 return skip_prefix(*bol
, str
, bol
) ||
2412 ((nick
&& **bol
== nick
) &&
2413 (*p
== ' ' || *p
== '\t' || *p
== '\n' || *p
== '\r' || !*p
) &&
2417 static int parse_insn_line(struct repository
*r
, struct todo_item
*item
,
2418 const char *buf
, const char *bol
, char *eol
)
2420 struct object_id commit_oid
;
2421 char *end_of_object_name
;
2422 int i
, saved
, status
, padding
;
2427 bol
+= strspn(bol
, " \t");
2429 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
2430 item
->command
= TODO_COMMENT
;
2431 item
->commit
= NULL
;
2432 item
->arg_offset
= bol
- buf
;
2433 item
->arg_len
= eol
- bol
;
2437 for (i
= 0; i
< TODO_COMMENT
; i
++)
2438 if (is_command(i
, &bol
)) {
2442 if (i
>= TODO_COMMENT
)
2445 /* Eat up extra spaces/ tabs before object name */
2446 padding
= strspn(bol
, " \t");
2449 if (item
->command
== TODO_NOOP
|| item
->command
== TODO_BREAK
) {
2451 return error(_("%s does not accept arguments: '%s'"),
2452 command_to_string(item
->command
), bol
);
2453 item
->commit
= NULL
;
2454 item
->arg_offset
= bol
- buf
;
2455 item
->arg_len
= eol
- bol
;
2460 return error(_("missing arguments for %s"),
2461 command_to_string(item
->command
));
2463 if (item
->command
== TODO_EXEC
|| item
->command
== TODO_LABEL
||
2464 item
->command
== TODO_RESET
) {
2465 item
->commit
= NULL
;
2466 item
->arg_offset
= bol
- buf
;
2467 item
->arg_len
= (int)(eol
- bol
);
2471 if (item
->command
== TODO_FIXUP
) {
2472 if (skip_prefix(bol
, "-C", &bol
) &&
2473 (*bol
== ' ' || *bol
== '\t')) {
2474 bol
+= strspn(bol
, " \t");
2475 item
->flags
|= TODO_REPLACE_FIXUP_MSG
;
2476 } else if (skip_prefix(bol
, "-c", &bol
) &&
2477 (*bol
== ' ' || *bol
== '\t')) {
2478 bol
+= strspn(bol
, " \t");
2479 item
->flags
|= TODO_EDIT_FIXUP_MSG
;
2483 if (item
->command
== TODO_MERGE
) {
2484 if (skip_prefix(bol
, "-C", &bol
))
2485 bol
+= strspn(bol
, " \t");
2486 else if (skip_prefix(bol
, "-c", &bol
)) {
2487 bol
+= strspn(bol
, " \t");
2488 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2490 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2491 item
->commit
= NULL
;
2492 item
->arg_offset
= bol
- buf
;
2493 item
->arg_len
= (int)(eol
- bol
);
2498 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
2499 saved
= *end_of_object_name
;
2500 *end_of_object_name
= '\0';
2501 status
= get_oid(bol
, &commit_oid
);
2503 error(_("could not parse '%s'"), bol
); /* return later */
2504 *end_of_object_name
= saved
;
2506 bol
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
2507 item
->arg_offset
= bol
- buf
;
2508 item
->arg_len
= (int)(eol
- bol
);
2513 item
->commit
= lookup_commit_reference(r
, &commit_oid
);
2514 return item
->commit
? 0 : -1;
2517 int sequencer_get_last_command(struct repository
*r
, enum replay_action
*action
)
2519 const char *todo_file
, *bol
;
2520 struct strbuf buf
= STRBUF_INIT
;
2523 todo_file
= git_path_todo_file();
2524 if (strbuf_read_file(&buf
, todo_file
, 0) < 0) {
2525 if (errno
== ENOENT
|| errno
== ENOTDIR
)
2528 return error_errno("unable to open '%s'", todo_file
);
2530 bol
= buf
.buf
+ strspn(buf
.buf
, " \t\r\n");
2531 if (is_command(TODO_PICK
, &bol
) && (*bol
== ' ' || *bol
== '\t'))
2532 *action
= REPLAY_PICK
;
2533 else if (is_command(TODO_REVERT
, &bol
) &&
2534 (*bol
== ' ' || *bol
== '\t'))
2535 *action
= REPLAY_REVERT
;
2539 strbuf_release(&buf
);
2544 int todo_list_parse_insn_buffer(struct repository
*r
, char *buf
,
2545 struct todo_list
*todo_list
)
2547 struct todo_item
*item
;
2548 char *p
= buf
, *next_p
;
2549 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
2551 todo_list
->current
= todo_list
->nr
= 0;
2553 for (i
= 1; *p
; i
++, p
= next_p
) {
2554 char *eol
= strchrnul(p
, '\n');
2556 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
2558 if (p
!= eol
&& eol
[-1] == '\r')
2559 eol
--; /* strip Carriage Return */
2561 item
= append_new_todo(todo_list
);
2562 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
2563 if (parse_insn_line(r
, item
, buf
, p
, eol
)) {
2564 res
= error(_("invalid line %d: %.*s"),
2565 i
, (int)(eol
- p
), p
);
2566 item
->command
= TODO_COMMENT
+ 1;
2567 item
->arg_offset
= p
- buf
;
2568 item
->arg_len
= (int)(eol
- p
);
2569 item
->commit
= NULL
;
2574 else if (is_fixup(item
->command
))
2575 return error(_("cannot '%s' without a previous commit"),
2576 command_to_string(item
->command
));
2577 else if (!is_noop(item
->command
))
2584 static int count_commands(struct todo_list
*todo_list
)
2588 for (i
= 0; i
< todo_list
->nr
; i
++)
2589 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
2595 static int get_item_line_offset(struct todo_list
*todo_list
, int index
)
2597 return index
< todo_list
->nr
?
2598 todo_list
->items
[index
].offset_in_buf
: todo_list
->buf
.len
;
2601 static const char *get_item_line(struct todo_list
*todo_list
, int index
)
2603 return todo_list
->buf
.buf
+ get_item_line_offset(todo_list
, index
);
2606 static int get_item_line_length(struct todo_list
*todo_list
, int index
)
2608 return get_item_line_offset(todo_list
, index
+ 1)
2609 - get_item_line_offset(todo_list
, index
);
2612 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
2617 fd
= open(path
, O_RDONLY
);
2619 return error_errno(_("could not open '%s'"), path
);
2620 len
= strbuf_read(sb
, fd
, 0);
2623 return error(_("could not read '%s'."), path
);
2627 static int have_finished_the_last_pick(void)
2629 struct strbuf buf
= STRBUF_INIT
;
2631 const char *todo_path
= git_path_todo_file();
2634 if (strbuf_read_file(&buf
, todo_path
, 0) < 0) {
2635 if (errno
== ENOENT
) {
2638 error_errno("unable to open '%s'", todo_path
);
2642 /* If there is only one line then we are done */
2643 eol
= strchr(buf
.buf
, '\n');
2644 if (!eol
|| !eol
[1])
2647 strbuf_release(&buf
);
2652 void sequencer_post_commit_cleanup(struct repository
*r
, int verbose
)
2654 struct replay_opts opts
= REPLAY_OPTS_INIT
;
2655 int need_cleanup
= 0;
2657 if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD")) {
2658 if (!refs_delete_ref(get_main_ref_store(r
), "",
2659 "CHERRY_PICK_HEAD", NULL
, 0) &&
2661 warning(_("cancelling a cherry picking in progress"));
2662 opts
.action
= REPLAY_PICK
;
2666 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD")) {
2667 if (!refs_delete_ref(get_main_ref_store(r
), "", "REVERT_HEAD",
2670 warning(_("cancelling a revert in progress"));
2671 opts
.action
= REPLAY_REVERT
;
2675 unlink(git_path_auto_merge(r
));
2680 if (!have_finished_the_last_pick())
2683 sequencer_remove_state(&opts
);
2686 static void todo_list_write_total_nr(struct todo_list
*todo_list
)
2688 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
2691 fprintf(f
, "%d\n", todo_list
->total_nr
);
2696 static int read_populate_todo(struct repository
*r
,
2697 struct todo_list
*todo_list
,
2698 struct replay_opts
*opts
)
2700 const char *todo_file
= get_todo_path(opts
);
2703 strbuf_reset(&todo_list
->buf
);
2704 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
2707 res
= todo_list_parse_insn_buffer(r
, todo_list
->buf
.buf
, todo_list
);
2709 if (is_rebase_i(opts
))
2710 return error(_("please fix this using "
2711 "'git rebase --edit-todo'."));
2712 return error(_("unusable instruction sheet: '%s'"), todo_file
);
2715 if (!todo_list
->nr
&&
2716 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
2717 return error(_("no commits parsed."));
2719 if (!is_rebase_i(opts
)) {
2720 enum todo_command valid
=
2721 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
2724 for (i
= 0; i
< todo_list
->nr
; i
++)
2725 if (valid
== todo_list
->items
[i
].command
)
2727 else if (valid
== TODO_PICK
)
2728 return error(_("cannot cherry-pick during a revert."));
2730 return error(_("cannot revert during a cherry-pick."));
2733 if (is_rebase_i(opts
)) {
2734 struct todo_list done
= TODO_LIST_INIT
;
2736 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
2737 !todo_list_parse_insn_buffer(r
, done
.buf
.buf
, &done
))
2738 todo_list
->done_nr
= count_commands(&done
);
2740 todo_list
->done_nr
= 0;
2742 todo_list
->total_nr
= todo_list
->done_nr
2743 + count_commands(todo_list
);
2744 todo_list_release(&done
);
2746 todo_list_write_total_nr(todo_list
);
2752 static int git_config_string_dup(char **dest
,
2753 const char *var
, const char *value
)
2756 return config_error_nonbool(var
);
2758 *dest
= xstrdup(value
);
2762 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
2764 struct replay_opts
*opts
= data
;
2769 else if (!strcmp(key
, "options.no-commit"))
2770 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
2771 else if (!strcmp(key
, "options.edit"))
2772 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
2773 else if (!strcmp(key
, "options.allow-empty"))
2775 git_config_bool_or_int(key
, value
, &error_flag
);
2776 else if (!strcmp(key
, "options.allow-empty-message"))
2777 opts
->allow_empty_message
=
2778 git_config_bool_or_int(key
, value
, &error_flag
);
2779 else if (!strcmp(key
, "options.keep-redundant-commits"))
2780 opts
->keep_redundant_commits
=
2781 git_config_bool_or_int(key
, value
, &error_flag
);
2782 else if (!strcmp(key
, "options.signoff"))
2783 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
2784 else if (!strcmp(key
, "options.record-origin"))
2785 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
2786 else if (!strcmp(key
, "options.allow-ff"))
2787 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
2788 else if (!strcmp(key
, "options.mainline"))
2789 opts
->mainline
= git_config_int(key
, value
);
2790 else if (!strcmp(key
, "options.strategy"))
2791 git_config_string_dup(&opts
->strategy
, key
, value
);
2792 else if (!strcmp(key
, "options.gpg-sign"))
2793 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
2794 else if (!strcmp(key
, "options.strategy-option")) {
2795 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
2796 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
2797 } else if (!strcmp(key
, "options.allow-rerere-auto"))
2798 opts
->allow_rerere_auto
=
2799 git_config_bool_or_int(key
, value
, &error_flag
) ?
2800 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
2801 else if (!strcmp(key
, "options.default-msg-cleanup")) {
2802 opts
->explicit_cleanup
= 1;
2803 opts
->default_msg_cleanup
= get_cleanup_mode(value
, 1);
2805 return error(_("invalid key: %s"), key
);
2808 return error(_("invalid value for %s: %s"), key
, value
);
2813 void parse_strategy_opts(struct replay_opts
*opts
, char *raw_opts
)
2816 char *strategy_opts_string
= raw_opts
;
2818 if (*strategy_opts_string
== ' ')
2819 strategy_opts_string
++;
2821 opts
->xopts_nr
= split_cmdline(strategy_opts_string
,
2822 (const char ***)&opts
->xopts
);
2823 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
2824 const char *arg
= opts
->xopts
[i
];
2826 skip_prefix(arg
, "--", &arg
);
2827 opts
->xopts
[i
] = xstrdup(arg
);
2831 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
2834 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
2836 opts
->strategy
= strbuf_detach(buf
, NULL
);
2837 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
2840 parse_strategy_opts(opts
, buf
->buf
);
2843 static int read_populate_opts(struct replay_opts
*opts
)
2845 if (is_rebase_i(opts
)) {
2846 struct strbuf buf
= STRBUF_INIT
;
2849 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(),
2850 READ_ONELINER_SKIP_IF_EMPTY
)) {
2851 if (!starts_with(buf
.buf
, "-S"))
2854 free(opts
->gpg_sign
);
2855 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
2860 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(),
2861 READ_ONELINER_SKIP_IF_EMPTY
)) {
2862 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
2863 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
2864 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
2865 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
2869 if (file_exists(rebase_path_verbose()))
2872 if (file_exists(rebase_path_quiet()))
2875 if (file_exists(rebase_path_signoff())) {
2880 if (file_exists(rebase_path_cdate_is_adate())) {
2882 opts
->committer_date_is_author_date
= 1;
2885 if (file_exists(rebase_path_ignore_date())) {
2887 opts
->ignore_date
= 1;
2890 if (file_exists(rebase_path_reschedule_failed_exec()))
2891 opts
->reschedule_failed_exec
= 1;
2892 else if (file_exists(rebase_path_no_reschedule_failed_exec()))
2893 opts
->reschedule_failed_exec
= 0;
2895 if (file_exists(rebase_path_drop_redundant_commits()))
2896 opts
->drop_redundant_commits
= 1;
2898 if (file_exists(rebase_path_keep_redundant_commits()))
2899 opts
->keep_redundant_commits
= 1;
2901 read_strategy_opts(opts
, &buf
);
2904 if (read_oneliner(&opts
->current_fixups
,
2905 rebase_path_current_fixups(),
2906 READ_ONELINER_SKIP_IF_EMPTY
)) {
2907 const char *p
= opts
->current_fixups
.buf
;
2908 opts
->current_fixup_count
= 1;
2909 while ((p
= strchr(p
, '\n'))) {
2910 opts
->current_fixup_count
++;
2915 if (read_oneliner(&buf
, rebase_path_squash_onto(), 0)) {
2916 if (get_oid_committish(buf
.buf
, &opts
->squash_onto
) < 0) {
2917 ret
= error(_("unusable squash-onto"));
2920 opts
->have_squash_onto
= 1;
2924 strbuf_release(&buf
);
2928 if (!file_exists(git_path_opts_file()))
2931 * The function git_parse_source(), called from git_config_from_file(),
2932 * may die() in case of a syntactically incorrect file. We do not care
2933 * about this case, though, because we wrote that file ourselves, so we
2934 * are pretty certain that it is syntactically correct.
2936 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
2937 return error(_("malformed options sheet: '%s'"),
2938 git_path_opts_file());
2942 static void write_strategy_opts(struct replay_opts
*opts
)
2945 struct strbuf buf
= STRBUF_INIT
;
2947 for (i
= 0; i
< opts
->xopts_nr
; ++i
)
2948 strbuf_addf(&buf
, " --%s", opts
->xopts
[i
]);
2950 write_file(rebase_path_strategy_opts(), "%s\n", buf
.buf
);
2951 strbuf_release(&buf
);
2954 int write_basic_state(struct replay_opts
*opts
, const char *head_name
,
2955 struct commit
*onto
, const struct object_id
*orig_head
)
2958 write_file(rebase_path_head_name(), "%s\n", head_name
);
2960 write_file(rebase_path_onto(), "%s\n",
2961 oid_to_hex(&onto
->object
.oid
));
2963 write_file(rebase_path_orig_head(), "%s\n",
2964 oid_to_hex(orig_head
));
2967 write_file(rebase_path_quiet(), "%s", "");
2969 write_file(rebase_path_verbose(), "%s", "");
2971 write_file(rebase_path_strategy(), "%s\n", opts
->strategy
);
2972 if (opts
->xopts_nr
> 0)
2973 write_strategy_opts(opts
);
2975 if (opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
)
2976 write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
2977 else if (opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
)
2978 write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
2981 write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts
->gpg_sign
);
2983 write_file(rebase_path_signoff(), "--signoff\n");
2984 if (opts
->drop_redundant_commits
)
2985 write_file(rebase_path_drop_redundant_commits(), "%s", "");
2986 if (opts
->keep_redundant_commits
)
2987 write_file(rebase_path_keep_redundant_commits(), "%s", "");
2988 if (opts
->committer_date_is_author_date
)
2989 write_file(rebase_path_cdate_is_adate(), "%s", "");
2990 if (opts
->ignore_date
)
2991 write_file(rebase_path_ignore_date(), "%s", "");
2992 if (opts
->reschedule_failed_exec
)
2993 write_file(rebase_path_reschedule_failed_exec(), "%s", "");
2995 write_file(rebase_path_no_reschedule_failed_exec(), "%s", "");
3000 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
3001 struct replay_opts
*opts
)
3003 enum todo_command command
= opts
->action
== REPLAY_PICK
?
3004 TODO_PICK
: TODO_REVERT
;
3005 const char *command_string
= todo_command_info
[command
].str
;
3006 const char *encoding
;
3007 struct commit
*commit
;
3009 if (prepare_revs(opts
))
3012 encoding
= get_log_output_encoding();
3014 while ((commit
= get_revision(opts
->revs
))) {
3015 struct todo_item
*item
= append_new_todo(todo_list
);
3016 const char *commit_buffer
= logmsg_reencode(commit
, NULL
, encoding
);
3017 const char *subject
;
3020 item
->command
= command
;
3021 item
->commit
= commit
;
3022 item
->arg_offset
= 0;
3024 item
->offset_in_buf
= todo_list
->buf
.len
;
3025 subject_len
= find_commit_subject(commit_buffer
, &subject
);
3026 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
3027 short_commit_name(commit
), subject_len
, subject
);
3028 unuse_commit_buffer(commit
, commit_buffer
);
3032 return error(_("empty commit set passed"));
3037 static int create_seq_dir(struct repository
*r
)
3039 enum replay_action action
;
3040 const char *in_progress_error
= NULL
;
3041 const char *in_progress_advice
= NULL
;
3042 unsigned int advise_skip
=
3043 refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") ||
3044 refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD");
3046 if (!sequencer_get_last_command(r
, &action
)) {
3049 in_progress_error
= _("revert is already in progress");
3050 in_progress_advice
=
3051 _("try \"git revert (--continue | %s--abort | --quit)\"");
3054 in_progress_error
= _("cherry-pick is already in progress");
3055 in_progress_advice
=
3056 _("try \"git cherry-pick (--continue | %s--abort | --quit)\"");
3059 BUG("unexpected action in create_seq_dir");
3062 if (in_progress_error
) {
3063 error("%s", in_progress_error
);
3064 if (advice_enabled(ADVICE_SEQUENCER_IN_USE
))
3065 advise(in_progress_advice
,
3066 advise_skip
? "--skip | " : "");
3069 if (mkdir(git_path_seq_dir(), 0777) < 0)
3070 return error_errno(_("could not create sequencer directory '%s'"),
3071 git_path_seq_dir());
3076 static int save_head(const char *head
)
3078 struct lock_file head_lock
= LOCK_INIT
;
3079 struct strbuf buf
= STRBUF_INIT
;
3083 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
3085 return error_errno(_("could not lock HEAD"));
3086 strbuf_addf(&buf
, "%s\n", head
);
3087 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
3088 strbuf_release(&buf
);
3090 error_errno(_("could not write to '%s'"), git_path_head_file());
3091 rollback_lock_file(&head_lock
);
3094 if (commit_lock_file(&head_lock
) < 0)
3095 return error(_("failed to finalize '%s'"), git_path_head_file());
3099 static int rollback_is_safe(void)
3101 struct strbuf sb
= STRBUF_INIT
;
3102 struct object_id expected_head
, actual_head
;
3104 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
3106 if (get_oid_hex(sb
.buf
, &expected_head
)) {
3107 strbuf_release(&sb
);
3108 die(_("could not parse %s"), git_path_abort_safety_file());
3110 strbuf_release(&sb
);
3112 else if (errno
== ENOENT
)
3113 oidclr(&expected_head
);
3115 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
3117 if (get_oid("HEAD", &actual_head
))
3118 oidclr(&actual_head
);
3120 return oideq(&actual_head
, &expected_head
);
3123 static int reset_merge(const struct object_id
*oid
)
3126 struct strvec argv
= STRVEC_INIT
;
3128 strvec_pushl(&argv
, "reset", "--merge", NULL
);
3130 if (!is_null_oid(oid
))
3131 strvec_push(&argv
, oid_to_hex(oid
));
3133 ret
= run_command_v_opt(argv
.v
, RUN_GIT_CMD
);
3134 strvec_clear(&argv
);
3139 static int rollback_single_pick(struct repository
*r
)
3141 struct object_id head_oid
;
3143 if (!refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
3144 !refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD"))
3145 return error(_("no cherry-pick or revert in progress"));
3146 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
3147 return error(_("cannot resolve HEAD"));
3148 if (is_null_oid(&head_oid
))
3149 return error(_("cannot abort from a branch yet to be born"));
3150 return reset_merge(&head_oid
);
3153 static int skip_single_pick(void)
3155 struct object_id head
;
3157 if (read_ref_full("HEAD", 0, &head
, NULL
))
3158 return error(_("cannot resolve HEAD"));
3159 return reset_merge(&head
);
3162 int sequencer_rollback(struct repository
*r
, struct replay_opts
*opts
)
3165 struct object_id oid
;
3166 struct strbuf buf
= STRBUF_INIT
;
3169 f
= fopen(git_path_head_file(), "r");
3170 if (!f
&& errno
== ENOENT
) {
3172 * There is no multiple-cherry-pick in progress.
3173 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
3174 * a single-cherry-pick in progress, abort that.
3176 return rollback_single_pick(r
);
3179 return error_errno(_("cannot open '%s'"), git_path_head_file());
3180 if (strbuf_getline_lf(&buf
, f
)) {
3181 error(_("cannot read '%s': %s"), git_path_head_file(),
3182 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
3187 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
3188 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
3189 git_path_head_file());
3192 if (is_null_oid(&oid
)) {
3193 error(_("cannot abort from a branch yet to be born"));
3197 if (!rollback_is_safe()) {
3198 /* Do not error, just do not rollback */
3199 warning(_("You seem to have moved HEAD. "
3200 "Not rewinding, check your HEAD!"));
3202 if (reset_merge(&oid
))
3204 strbuf_release(&buf
);
3205 return sequencer_remove_state(opts
);
3207 strbuf_release(&buf
);
3211 int sequencer_skip(struct repository
*r
, struct replay_opts
*opts
)
3213 enum replay_action action
= -1;
3214 sequencer_get_last_command(r
, &action
);
3217 * Check whether the subcommand requested to skip the commit is actually
3218 * in progress and that it's safe to skip the commit.
3220 * opts->action tells us which subcommand requested to skip the commit.
3221 * If the corresponding .git/<ACTION>_HEAD exists, we know that the
3222 * action is in progress and we can skip the commit.
3224 * Otherwise we check that the last instruction was related to the
3225 * particular subcommand we're trying to execute and barf if that's not
3228 * Finally we check that the rollback is "safe", i.e., has the HEAD
3229 * moved? In this case, it doesn't make sense to "reset the merge" and
3230 * "skip the commit" as the user already handled this by committing. But
3231 * we'd not want to barf here, instead give advice on how to proceed. We
3232 * only need to check that when .git/<ACTION>_HEAD doesn't exist because
3233 * it gets removed when the user commits, so if it still exists we're
3234 * sure the user can't have committed before.
3236 switch (opts
->action
) {
3238 if (!refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD")) {
3239 if (action
!= REPLAY_REVERT
)
3240 return error(_("no revert in progress"));
3241 if (!rollback_is_safe())
3246 if (!refs_ref_exists(get_main_ref_store(r
),
3247 "CHERRY_PICK_HEAD")) {
3248 if (action
!= REPLAY_PICK
)
3249 return error(_("no cherry-pick in progress"));
3250 if (!rollback_is_safe())
3255 BUG("unexpected action in sequencer_skip");
3258 if (skip_single_pick())
3259 return error(_("failed to skip the commit"));
3260 if (!is_directory(git_path_seq_dir()))
3263 return sequencer_continue(r
, opts
);
3266 error(_("there is nothing to skip"));
3268 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
)) {
3269 advise(_("have you committed already?\n"
3270 "try \"git %s --continue\""),
3271 action
== REPLAY_REVERT
? "revert" : "cherry-pick");
3276 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
3278 struct lock_file todo_lock
= LOCK_INIT
;
3279 const char *todo_path
= get_todo_path(opts
);
3280 int next
= todo_list
->current
, offset
, fd
;
3283 * rebase -i writes "git-rebase-todo" without the currently executing
3284 * command, appending it to "done" instead.
3286 if (is_rebase_i(opts
))
3289 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
3291 return error_errno(_("could not lock '%s'"), todo_path
);
3292 offset
= get_item_line_offset(todo_list
, next
);
3293 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
3294 todo_list
->buf
.len
- offset
) < 0)
3295 return error_errno(_("could not write to '%s'"), todo_path
);
3296 if (commit_lock_file(&todo_lock
) < 0)
3297 return error(_("failed to finalize '%s'"), todo_path
);
3299 if (is_rebase_i(opts
) && next
> 0) {
3300 const char *done
= rebase_path_done();
3301 int fd
= open(done
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
3306 if (write_in_full(fd
, get_item_line(todo_list
, next
- 1),
3307 get_item_line_length(todo_list
, next
- 1))
3309 ret
= error_errno(_("could not write to '%s'"), done
);
3311 ret
= error_errno(_("failed to finalize '%s'"), done
);
3317 static int save_opts(struct replay_opts
*opts
)
3319 const char *opts_file
= git_path_opts_file();
3322 if (opts
->no_commit
)
3323 res
|= git_config_set_in_file_gently(opts_file
,
3324 "options.no-commit", "true");
3325 if (opts
->edit
>= 0)
3326 res
|= git_config_set_in_file_gently(opts_file
, "options.edit",
3327 opts
->edit
? "true" : "false");
3328 if (opts
->allow_empty
)
3329 res
|= git_config_set_in_file_gently(opts_file
,
3330 "options.allow-empty", "true");
3331 if (opts
->allow_empty_message
)
3332 res
|= git_config_set_in_file_gently(opts_file
,
3333 "options.allow-empty-message", "true");
3334 if (opts
->keep_redundant_commits
)
3335 res
|= git_config_set_in_file_gently(opts_file
,
3336 "options.keep-redundant-commits", "true");
3338 res
|= git_config_set_in_file_gently(opts_file
,
3339 "options.signoff", "true");
3340 if (opts
->record_origin
)
3341 res
|= git_config_set_in_file_gently(opts_file
,
3342 "options.record-origin", "true");
3344 res
|= git_config_set_in_file_gently(opts_file
,
3345 "options.allow-ff", "true");
3346 if (opts
->mainline
) {
3347 struct strbuf buf
= STRBUF_INIT
;
3348 strbuf_addf(&buf
, "%d", opts
->mainline
);
3349 res
|= git_config_set_in_file_gently(opts_file
,
3350 "options.mainline", buf
.buf
);
3351 strbuf_release(&buf
);
3354 res
|= git_config_set_in_file_gently(opts_file
,
3355 "options.strategy", opts
->strategy
);
3357 res
|= git_config_set_in_file_gently(opts_file
,
3358 "options.gpg-sign", opts
->gpg_sign
);
3361 for (i
= 0; i
< opts
->xopts_nr
; i
++)
3362 res
|= git_config_set_multivar_in_file_gently(opts_file
,
3363 "options.strategy-option",
3364 opts
->xopts
[i
], "^$", 0);
3366 if (opts
->allow_rerere_auto
)
3367 res
|= git_config_set_in_file_gently(opts_file
,
3368 "options.allow-rerere-auto",
3369 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
3372 if (opts
->explicit_cleanup
)
3373 res
|= git_config_set_in_file_gently(opts_file
,
3374 "options.default-msg-cleanup",
3375 describe_cleanup_mode(opts
->default_msg_cleanup
));
3379 static int make_patch(struct repository
*r
,
3380 struct commit
*commit
,
3381 struct replay_opts
*opts
)
3383 struct strbuf buf
= STRBUF_INIT
;
3384 struct rev_info log_tree_opt
;
3385 const char *subject
;
3386 char hex
[GIT_MAX_HEXSZ
+ 1];
3389 oid_to_hex_r(hex
, &commit
->object
.oid
);
3390 if (write_message(hex
, strlen(hex
), rebase_path_stopped_sha(), 1) < 0)
3392 res
|= write_rebase_head(&commit
->object
.oid
);
3394 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
3395 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
3396 repo_init_revisions(r
, &log_tree_opt
, NULL
);
3397 log_tree_opt
.abbrev
= 0;
3398 log_tree_opt
.diff
= 1;
3399 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
3400 log_tree_opt
.disable_stdin
= 1;
3401 log_tree_opt
.no_commit_id
= 1;
3402 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
3403 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
3404 if (!log_tree_opt
.diffopt
.file
)
3405 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
3407 res
|= log_tree_commit(&log_tree_opt
, commit
);
3408 fclose(log_tree_opt
.diffopt
.file
);
3412 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
3413 if (!file_exists(buf
.buf
)) {
3414 const char *encoding
= get_commit_output_encoding();
3415 const char *commit_buffer
= logmsg_reencode(commit
, NULL
, encoding
);
3416 find_commit_subject(commit_buffer
, &subject
);
3417 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
3418 unuse_commit_buffer(commit
, commit_buffer
);
3420 strbuf_release(&buf
);
3425 static int intend_to_amend(void)
3427 struct object_id head
;
3430 if (get_oid("HEAD", &head
))
3431 return error(_("cannot read HEAD"));
3433 p
= oid_to_hex(&head
);
3434 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
3437 static int error_with_patch(struct repository
*r
,
3438 struct commit
*commit
,
3439 const char *subject
, int subject_len
,
3440 struct replay_opts
*opts
,
3441 int exit_code
, int to_amend
)
3444 if (make_patch(r
, commit
, opts
))
3446 } else if (copy_file(rebase_path_message(),
3447 git_path_merge_msg(r
), 0666))
3448 return error(_("unable to copy '%s' to '%s'"),
3449 git_path_merge_msg(r
), rebase_path_message());
3452 if (intend_to_amend())
3456 _("You can amend the commit now, with\n"
3458 " git commit --amend %s\n"
3460 "Once you are satisfied with your changes, run\n"
3462 " git rebase --continue\n"),
3463 gpg_sign_opt_quoted(opts
));
3464 } else if (exit_code
) {
3466 fprintf_ln(stderr
, _("Could not apply %s... %.*s"),
3467 short_commit_name(commit
), subject_len
, subject
);
3470 * We don't have the hash of the parent so
3471 * just print the line from the todo file.
3473 fprintf_ln(stderr
, _("Could not merge %.*s"),
3474 subject_len
, subject
);
3480 static int error_failed_squash(struct repository
*r
,
3481 struct commit
*commit
,
3482 struct replay_opts
*opts
,
3484 const char *subject
)
3486 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
3487 return error(_("could not copy '%s' to '%s'"),
3488 rebase_path_squash_msg(), rebase_path_message());
3489 unlink(git_path_merge_msg(r
));
3490 if (copy_file(git_path_merge_msg(r
), rebase_path_message(), 0666))
3491 return error(_("could not copy '%s' to '%s'"),
3492 rebase_path_message(),
3493 git_path_merge_msg(r
));
3494 return error_with_patch(r
, commit
, subject
, subject_len
, opts
, 1, 0);
3497 static int do_exec(struct repository
*r
, const char *command_line
)
3499 const char *child_argv
[] = { NULL
, NULL
};
3502 fprintf(stderr
, _("Executing: %s\n"), command_line
);
3503 child_argv
[0] = command_line
;
3504 status
= run_command_v_opt(child_argv
, RUN_USING_SHELL
);
3506 /* force re-reading of the cache */
3507 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0)
3508 return error(_("could not read index"));
3510 dirty
= require_clean_work_tree(r
, "rebase", NULL
, 1, 1);
3513 warning(_("execution failed: %s\n%s"
3514 "You can fix the problem, and then run\n"
3516 " git rebase --continue\n"
3519 dirty
? N_("and made changes to the index and/or the "
3520 "working tree\n") : "");
3522 /* command not found */
3525 warning(_("execution succeeded: %s\nbut "
3526 "left changes to the index and/or the working tree\n"
3527 "Commit or stash your changes, and then run\n"
3529 " git rebase --continue\n"
3530 "\n"), command_line
);
3537 __attribute__((format (printf
, 2, 3)))
3538 static int safe_append(const char *filename
, const char *fmt
, ...)
3541 struct lock_file lock
= LOCK_INIT
;
3542 int fd
= hold_lock_file_for_update(&lock
, filename
,
3543 LOCK_REPORT_ON_ERROR
);
3544 struct strbuf buf
= STRBUF_INIT
;
3549 if (strbuf_read_file(&buf
, filename
, 0) < 0 && errno
!= ENOENT
) {
3550 error_errno(_("could not read '%s'"), filename
);
3551 rollback_lock_file(&lock
);
3554 strbuf_complete(&buf
, '\n');
3556 strbuf_vaddf(&buf
, fmt
, ap
);
3559 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
3560 error_errno(_("could not write to '%s'"), filename
);
3561 strbuf_release(&buf
);
3562 rollback_lock_file(&lock
);
3565 if (commit_lock_file(&lock
) < 0) {
3566 strbuf_release(&buf
);
3567 rollback_lock_file(&lock
);
3568 return error(_("failed to finalize '%s'"), filename
);
3571 strbuf_release(&buf
);
3575 static int do_label(struct repository
*r
, const char *name
, int len
)
3577 struct ref_store
*refs
= get_main_ref_store(r
);
3578 struct ref_transaction
*transaction
;
3579 struct strbuf ref_name
= STRBUF_INIT
, err
= STRBUF_INIT
;
3580 struct strbuf msg
= STRBUF_INIT
;
3582 struct object_id head_oid
;
3584 if (len
== 1 && *name
== '#')
3585 return error(_("illegal label name: '%.*s'"), len
, name
);
3587 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
3588 strbuf_addf(&msg
, "rebase (label) '%.*s'", len
, name
);
3590 transaction
= ref_store_transaction_begin(refs
, &err
);
3592 error("%s", err
.buf
);
3594 } else if (get_oid("HEAD", &head_oid
)) {
3595 error(_("could not read HEAD"));
3597 } else if (ref_transaction_update(transaction
, ref_name
.buf
, &head_oid
,
3598 NULL
, 0, msg
.buf
, &err
) < 0 ||
3599 ref_transaction_commit(transaction
, &err
)) {
3600 error("%s", err
.buf
);
3603 ref_transaction_free(transaction
);
3604 strbuf_release(&err
);
3605 strbuf_release(&msg
);
3608 ret
= safe_append(rebase_path_refs_to_delete(),
3609 "%s\n", ref_name
.buf
);
3610 strbuf_release(&ref_name
);
3615 __attribute__((format (printf
, 3, 4)))
3616 static const char *reflog_message(struct replay_opts
*opts
,
3617 const char *sub_action
, const char *fmt
, ...)
3620 static struct strbuf buf
= STRBUF_INIT
;
3621 char *reflog_action
= getenv(GIT_REFLOG_ACTION
);
3625 strbuf_addstr(&buf
, reflog_action
? reflog_action
: action_name(opts
));
3627 strbuf_addf(&buf
, " (%s)", sub_action
);
3629 strbuf_addstr(&buf
, ": ");
3630 strbuf_vaddf(&buf
, fmt
, ap
);
3637 static int do_reset(struct repository
*r
,
3638 const char *name
, int len
,
3639 struct replay_opts
*opts
)
3641 struct strbuf ref_name
= STRBUF_INIT
;
3642 struct object_id oid
;
3643 struct lock_file lock
= LOCK_INIT
;
3644 struct tree_desc desc
= { 0 };
3646 struct unpack_trees_options unpack_tree_opts
= { 0 };
3649 if (repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0)
3652 if (len
== 10 && !strncmp("[new root]", name
, len
)) {
3653 if (!opts
->have_squash_onto
) {
3655 if (commit_tree("", 0, the_hash_algo
->empty_tree
,
3656 NULL
, &opts
->squash_onto
,
3658 return error(_("writing fake root commit"));
3659 opts
->have_squash_onto
= 1;
3660 hex
= oid_to_hex(&opts
->squash_onto
);
3661 if (write_message(hex
, strlen(hex
),
3662 rebase_path_squash_onto(), 0))
3663 return error(_("writing squash-onto"));
3665 oidcpy(&oid
, &opts
->squash_onto
);
3669 /* Determine the length of the label */
3670 for (i
= 0; i
< len
; i
++)
3671 if (isspace(name
[i
]))
3675 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
3676 if (get_oid(ref_name
.buf
, &oid
) &&
3677 get_oid(ref_name
.buf
+ strlen("refs/rewritten/"), &oid
)) {
3678 ret
= error(_("could not read '%s'"), ref_name
.buf
);
3683 setup_unpack_trees_porcelain(&unpack_tree_opts
, "reset");
3684 unpack_tree_opts
.head_idx
= 1;
3685 unpack_tree_opts
.src_index
= r
->index
;
3686 unpack_tree_opts
.dst_index
= r
->index
;
3687 unpack_tree_opts
.fn
= oneway_merge
;
3688 unpack_tree_opts
.merge
= 1;
3689 unpack_tree_opts
.update
= 1;
3690 unpack_tree_opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
3691 init_checkout_metadata(&unpack_tree_opts
.meta
, name
, &oid
, NULL
);
3693 if (repo_read_index_unmerged(r
)) {
3694 ret
= error_resolve_conflict(_(action_name(opts
)));
3698 if (!fill_tree_descriptor(r
, &desc
, &oid
)) {
3699 ret
= error(_("failed to find tree of %s"), oid_to_hex(&oid
));
3703 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
3708 tree
= parse_tree_indirect(&oid
);
3709 prime_cache_tree(r
, r
->index
, tree
);
3711 if (write_locked_index(r
->index
, &lock
, COMMIT_LOCK
) < 0)
3712 ret
= error(_("could not write index"));
3715 ret
= update_ref(reflog_message(opts
, "reset", "'%.*s'",
3716 len
, name
), "HEAD", &oid
,
3717 NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
3719 free((void *)desc
.buffer
);
3721 rollback_lock_file(&lock
);
3722 strbuf_release(&ref_name
);
3723 clear_unpack_trees_porcelain(&unpack_tree_opts
);
3727 static struct commit
*lookup_label(const char *label
, int len
,
3730 struct commit
*commit
;
3733 strbuf_addf(buf
, "refs/rewritten/%.*s", len
, label
);
3734 commit
= lookup_commit_reference_by_name(buf
->buf
);
3736 /* fall back to non-rewritten ref or commit */
3737 strbuf_splice(buf
, 0, strlen("refs/rewritten/"), "", 0);
3738 commit
= lookup_commit_reference_by_name(buf
->buf
);
3742 error(_("could not resolve '%s'"), buf
->buf
);
3747 static int do_merge(struct repository
*r
,
3748 struct commit
*commit
,
3749 const char *arg
, int arg_len
,
3750 int flags
, int *check_todo
, struct replay_opts
*opts
)
3752 int run_commit_flags
= 0;
3753 struct strbuf ref_name
= STRBUF_INIT
;
3754 struct commit
*head_commit
, *merge_commit
, *i
;
3755 struct commit_list
*bases
, *j
, *reversed
= NULL
;
3756 struct commit_list
*to_merge
= NULL
, **tail
= &to_merge
;
3757 const char *strategy
= !opts
->xopts_nr
&&
3759 !strcmp(opts
->strategy
, "recursive") ||
3760 !strcmp(opts
->strategy
, "ort")) ?
3761 NULL
: opts
->strategy
;
3762 struct merge_options o
;
3763 int merge_arg_len
, oneline_offset
, can_fast_forward
, ret
, k
;
3764 static struct lock_file lock
;
3767 if (repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0) {
3772 head_commit
= lookup_commit_reference_by_name("HEAD");
3774 ret
= error(_("cannot merge without a current revision"));
3779 * For octopus merges, the arg starts with the list of revisions to be
3780 * merged. The list is optionally followed by '#' and the oneline.
3782 merge_arg_len
= oneline_offset
= arg_len
;
3783 for (p
= arg
; p
- arg
< arg_len
; p
+= strspn(p
, " \t\n")) {
3786 if (*p
== '#' && (!p
[1] || isspace(p
[1]))) {
3787 p
+= 1 + strspn(p
+ 1, " \t\n");
3788 oneline_offset
= p
- arg
;
3791 k
= strcspn(p
, " \t\n");
3794 merge_commit
= lookup_label(p
, k
, &ref_name
);
3795 if (!merge_commit
) {
3796 ret
= error(_("unable to parse '%.*s'"), k
, p
);
3799 tail
= &commit_list_insert(merge_commit
, tail
)->next
;
3801 merge_arg_len
= p
- arg
;
3805 ret
= error(_("nothing to merge: '%.*s'"), arg_len
, arg
);
3809 if (opts
->have_squash_onto
&&
3810 oideq(&head_commit
->object
.oid
, &opts
->squash_onto
)) {
3812 * When the user tells us to "merge" something into a
3813 * "[new root]", let's simply fast-forward to the merge head.
3815 rollback_lock_file(&lock
);
3817 ret
= error(_("octopus merge cannot be executed on "
3818 "top of a [new root]"));
3820 ret
= fast_forward_to(r
, &to_merge
->item
->object
.oid
,
3821 &head_commit
->object
.oid
, 0,
3827 * If HEAD is not identical to the first parent of the original merge
3828 * commit, we cannot fast-forward.
3830 can_fast_forward
= opts
->allow_ff
&& commit
&& commit
->parents
&&
3831 oideq(&commit
->parents
->item
->object
.oid
,
3832 &head_commit
->object
.oid
);
3835 * If any merge head is different from the original one, we cannot
3838 if (can_fast_forward
) {
3839 struct commit_list
*p
= commit
->parents
->next
;
3841 for (j
= to_merge
; j
&& p
; j
= j
->next
, p
= p
->next
)
3842 if (!oideq(&j
->item
->object
.oid
,
3843 &p
->item
->object
.oid
)) {
3844 can_fast_forward
= 0;
3848 * If the number of merge heads differs from the original merge
3849 * commit, we cannot fast-forward.
3852 can_fast_forward
= 0;
3855 if (can_fast_forward
) {
3856 rollback_lock_file(&lock
);
3857 ret
= fast_forward_to(r
, &commit
->object
.oid
,
3858 &head_commit
->object
.oid
, 0, opts
);
3859 if (flags
& TODO_EDIT_MERGE_MSG
)
3860 goto fast_forward_edit
;
3866 const char *encoding
= get_commit_output_encoding();
3867 const char *message
= logmsg_reencode(commit
, NULL
, encoding
);
3872 ret
= error(_("could not get commit message of '%s'"),
3873 oid_to_hex(&commit
->object
.oid
));
3876 write_author_script(message
);
3877 find_commit_subject(message
, &body
);
3879 ret
= write_message(body
, len
, git_path_merge_msg(r
), 0);
3880 unuse_commit_buffer(commit
, message
);
3882 error_errno(_("could not write '%s'"),
3883 git_path_merge_msg(r
));
3887 struct strbuf buf
= STRBUF_INIT
;
3890 strbuf_addf(&buf
, "author %s", git_author_info(0));
3891 write_author_script(buf
.buf
);
3894 if (oneline_offset
< arg_len
) {
3895 p
= arg
+ oneline_offset
;
3896 len
= arg_len
- oneline_offset
;
3898 strbuf_addf(&buf
, "Merge %s '%.*s'",
3899 to_merge
->next
? "branches" : "branch",
3900 merge_arg_len
, arg
);
3905 ret
= write_message(p
, len
, git_path_merge_msg(r
), 0);
3906 strbuf_release(&buf
);
3908 error_errno(_("could not write '%s'"),
3909 git_path_merge_msg(r
));
3914 if (strategy
|| to_merge
->next
) {
3916 struct child_process cmd
= CHILD_PROCESS_INIT
;
3918 if (read_env_script(&cmd
.env_array
)) {
3919 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
3921 ret
= error(_(staged_changes_advice
), gpg_opt
, gpg_opt
);
3925 if (opts
->committer_date_is_author_date
)
3926 strvec_pushf(&cmd
.env_array
, "GIT_COMMITTER_DATE=%s",
3929 author_date_from_env_array(&cmd
.env_array
));
3930 if (opts
->ignore_date
)
3931 strvec_push(&cmd
.env_array
, "GIT_AUTHOR_DATE=");
3934 strvec_push(&cmd
.args
, "merge");
3935 strvec_push(&cmd
.args
, "-s");
3937 strvec_push(&cmd
.args
, "octopus");
3939 strvec_push(&cmd
.args
, strategy
);
3940 for (k
= 0; k
< opts
->xopts_nr
; k
++)
3941 strvec_pushf(&cmd
.args
,
3942 "-X%s", opts
->xopts
[k
]);
3944 if (!(flags
& TODO_EDIT_MERGE_MSG
))
3945 strvec_push(&cmd
.args
, "--no-edit");
3947 strvec_push(&cmd
.args
, "--edit");
3948 strvec_push(&cmd
.args
, "--no-ff");
3949 strvec_push(&cmd
.args
, "--no-log");
3950 strvec_push(&cmd
.args
, "--no-stat");
3951 strvec_push(&cmd
.args
, "-F");
3952 strvec_push(&cmd
.args
, git_path_merge_msg(r
));
3954 strvec_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
3956 strvec_push(&cmd
.args
, "--no-gpg-sign");
3958 /* Add the tips to be merged */
3959 for (j
= to_merge
; j
; j
= j
->next
)
3960 strvec_push(&cmd
.args
,
3961 oid_to_hex(&j
->item
->object
.oid
));
3963 strbuf_release(&ref_name
);
3964 refs_delete_ref(get_main_ref_store(r
), "", "CHERRY_PICK_HEAD",
3966 rollback_lock_file(&lock
);
3968 ret
= run_command(&cmd
);
3970 /* force re-reading of the cache */
3971 if (!ret
&& (discard_index(r
->index
) < 0 ||
3972 repo_read_index(r
) < 0))
3973 ret
= error(_("could not read index"));
3977 merge_commit
= to_merge
->item
;
3978 bases
= get_merge_bases(head_commit
, merge_commit
);
3979 if (bases
&& oideq(&merge_commit
->object
.oid
,
3980 &bases
->item
->object
.oid
)) {
3982 /* skip merging an ancestor of HEAD */
3986 write_message(oid_to_hex(&merge_commit
->object
.oid
), the_hash_algo
->hexsz
,
3987 git_path_merge_head(r
), 0);
3988 write_message("no-ff", 5, git_path_merge_mode(r
), 0);
3990 for (j
= bases
; j
; j
= j
->next
)
3991 commit_list_insert(j
->item
, &reversed
);
3992 free_commit_list(bases
);
3995 init_merge_options(&o
, r
);
3997 o
.branch2
= ref_name
.buf
;
3998 o
.buffer_output
= 2;
4000 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "ort")) {
4002 * TODO: Should use merge_incore_recursive() and
4003 * merge_switch_to_result(), skipping the call to
4004 * merge_switch_to_result() when we don't actually need to
4005 * update the index and working copy immediately.
4007 ret
= merge_ort_recursive(&o
,
4008 head_commit
, merge_commit
, reversed
,
4011 ret
= merge_recursive(&o
, head_commit
, merge_commit
, reversed
,
4015 fputs(o
.obuf
.buf
, stdout
);
4016 strbuf_release(&o
.obuf
);
4018 error(_("could not even attempt to merge '%.*s'"),
4019 merge_arg_len
, arg
);
4023 * The return value of merge_recursive() is 1 on clean, and 0 on
4026 * Let's reverse that, so that do_merge() returns 0 upon success and
4027 * 1 upon failed merge (keeping the return value -1 for the cases where
4028 * we will want to reschedule the `merge` command).
4032 if (r
->index
->cache_changed
&&
4033 write_locked_index(r
->index
, &lock
, COMMIT_LOCK
)) {
4034 ret
= error(_("merge: Unable to write new index file"));
4038 rollback_lock_file(&lock
);
4040 repo_rerere(r
, opts
->allow_rerere_auto
);
4043 * In case of problems, we now want to return a positive
4044 * value (a negative one would indicate that the `merge`
4045 * command needs to be rescheduled).
4047 ret
= !!run_git_commit(git_path_merge_msg(r
), opts
,
4050 if (!ret
&& flags
& TODO_EDIT_MERGE_MSG
) {
4053 run_commit_flags
|= AMEND_MSG
| EDIT_MSG
| VERIFY_MSG
;
4054 ret
= !!run_git_commit(NULL
, opts
, run_commit_flags
);
4059 strbuf_release(&ref_name
);
4060 rollback_lock_file(&lock
);
4061 free_commit_list(to_merge
);
4065 static int is_final_fixup(struct todo_list
*todo_list
)
4067 int i
= todo_list
->current
;
4069 if (!is_fixup(todo_list
->items
[i
].command
))
4072 while (++i
< todo_list
->nr
)
4073 if (is_fixup(todo_list
->items
[i
].command
))
4075 else if (!is_noop(todo_list
->items
[i
].command
))
4080 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
4084 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
4085 if (!is_noop(todo_list
->items
[i
].command
))
4086 return todo_list
->items
[i
].command
;
4091 void create_autostash(struct repository
*r
, const char *path
,
4092 const char *default_reflog_action
)
4094 struct strbuf buf
= STRBUF_INIT
;
4095 struct lock_file lock_file
= LOCK_INIT
;
4098 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
4099 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
4101 repo_update_index_if_able(r
, &lock_file
);
4102 rollback_lock_file(&lock_file
);
4104 if (has_unstaged_changes(r
, 1) ||
4105 has_uncommitted_changes(r
, 1)) {
4106 struct child_process stash
= CHILD_PROCESS_INIT
;
4107 struct object_id oid
;
4109 strvec_pushl(&stash
.args
,
4110 "stash", "create", "autostash", NULL
);
4114 if (capture_command(&stash
, &buf
, GIT_MAX_HEXSZ
))
4115 die(_("Cannot autostash"));
4116 strbuf_trim_trailing_newline(&buf
);
4117 if (get_oid(buf
.buf
, &oid
))
4118 die(_("Unexpected stash response: '%s'"),
4121 strbuf_add_unique_abbrev(&buf
, &oid
, DEFAULT_ABBREV
);
4123 if (safe_create_leading_directories_const(path
))
4124 die(_("Could not create directory for '%s'"),
4126 write_file(path
, "%s", oid_to_hex(&oid
));
4127 printf(_("Created autostash: %s\n"), buf
.buf
);
4128 if (reset_head(r
, NULL
, "reset --hard",
4129 NULL
, RESET_HEAD_HARD
, NULL
, NULL
,
4130 default_reflog_action
) < 0)
4131 die(_("could not reset --hard"));
4133 if (discard_index(r
->index
) < 0 ||
4134 repo_read_index(r
) < 0)
4135 die(_("could not read index"));
4137 strbuf_release(&buf
);
4140 static int apply_save_autostash_oid(const char *stash_oid
, int attempt_apply
)
4142 struct child_process child
= CHILD_PROCESS_INIT
;
4145 if (attempt_apply
) {
4147 child
.no_stdout
= 1;
4148 child
.no_stderr
= 1;
4149 strvec_push(&child
.args
, "stash");
4150 strvec_push(&child
.args
, "apply");
4151 strvec_push(&child
.args
, stash_oid
);
4152 ret
= run_command(&child
);
4155 if (attempt_apply
&& !ret
)
4156 fprintf(stderr
, _("Applied autostash.\n"));
4158 struct child_process store
= CHILD_PROCESS_INIT
;
4161 strvec_push(&store
.args
, "stash");
4162 strvec_push(&store
.args
, "store");
4163 strvec_push(&store
.args
, "-m");
4164 strvec_push(&store
.args
, "autostash");
4165 strvec_push(&store
.args
, "-q");
4166 strvec_push(&store
.args
, stash_oid
);
4167 if (run_command(&store
))
4168 ret
= error(_("cannot store %s"), stash_oid
);
4172 "Your changes are safe in the stash.\n"
4173 "You can run \"git stash pop\" or"
4174 " \"git stash drop\" at any time.\n"),
4176 _("Applying autostash resulted in conflicts.") :
4177 _("Autostash exists; creating a new stash entry."));
4183 static int apply_save_autostash(const char *path
, int attempt_apply
)
4185 struct strbuf stash_oid
= STRBUF_INIT
;
4188 if (!read_oneliner(&stash_oid
, path
,
4189 READ_ONELINER_SKIP_IF_EMPTY
)) {
4190 strbuf_release(&stash_oid
);
4193 strbuf_trim(&stash_oid
);
4195 ret
= apply_save_autostash_oid(stash_oid
.buf
, attempt_apply
);
4198 strbuf_release(&stash_oid
);
4202 int save_autostash(const char *path
)
4204 return apply_save_autostash(path
, 0);
4207 int apply_autostash(const char *path
)
4209 return apply_save_autostash(path
, 1);
4212 int apply_autostash_oid(const char *stash_oid
)
4214 return apply_save_autostash_oid(stash_oid
, 1);
4217 static int run_git_checkout(struct repository
*r
, struct replay_opts
*opts
,
4218 const char *commit
, const char *action
)
4220 struct child_process cmd
= CHILD_PROCESS_INIT
;
4225 if (startup_info
->original_cwd
) {
4226 cmd
.dir
= startup_info
->original_cwd
;
4227 strvec_pushf(&cmd
.env_array
, "%s=%s",
4228 GIT_WORK_TREE_ENVIRONMENT
, r
->worktree
);
4230 strvec_push(&cmd
.args
, "checkout");
4231 strvec_push(&cmd
.args
, commit
);
4232 strvec_pushf(&cmd
.env_array
, GIT_REFLOG_ACTION
"=%s", action
);
4235 ret
= run_command(&cmd
);
4237 ret
= run_command_silent_on_success(&cmd
);
4240 discard_index(r
->index
);
4245 static int checkout_onto(struct repository
*r
, struct replay_opts
*opts
,
4246 const char *onto_name
, const struct object_id
*onto
,
4247 const struct object_id
*orig_head
)
4249 const char *action
= reflog_message(opts
, "start", "checkout %s", onto_name
);
4251 if (run_git_checkout(r
, opts
, oid_to_hex(onto
), action
)) {
4252 apply_autostash(rebase_path_autostash());
4253 sequencer_remove_state(opts
);
4254 return error(_("could not detach HEAD"));
4257 return update_ref(NULL
, "ORIG_HEAD", orig_head
, NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
4260 static int stopped_at_head(struct repository
*r
)
4262 struct object_id head
;
4263 struct commit
*commit
;
4264 struct commit_message message
;
4266 if (get_oid("HEAD", &head
) ||
4267 !(commit
= lookup_commit(r
, &head
)) ||
4268 parse_commit(commit
) || get_message(commit
, &message
))
4269 fprintf(stderr
, _("Stopped at HEAD\n"));
4271 fprintf(stderr
, _("Stopped at %s\n"), message
.label
);
4272 free_message(commit
, &message
);
4278 static int reread_todo_if_changed(struct repository
*r
,
4279 struct todo_list
*todo_list
,
4280 struct replay_opts
*opts
)
4283 struct strbuf buf
= STRBUF_INIT
;
4285 if (strbuf_read_file_or_whine(&buf
, get_todo_path(opts
)) < 0)
4287 offset
= get_item_line_offset(todo_list
, todo_list
->current
+ 1);
4288 if (buf
.len
!= todo_list
->buf
.len
- offset
||
4289 memcmp(buf
.buf
, todo_list
->buf
.buf
+ offset
, buf
.len
)) {
4290 /* Reread the todo file if it has changed. */
4291 todo_list_release(todo_list
);
4292 if (read_populate_todo(r
, todo_list
, opts
))
4293 return -1; /* message was printed */
4294 /* `current` will be incremented on return */
4295 todo_list
->current
= -1;
4297 strbuf_release(&buf
);
4302 static const char rescheduled_advice
[] =
4303 N_("Could not execute the todo command\n"
4307 "It has been rescheduled; To edit the command before continuing, please\n"
4308 "edit the todo list first:\n"
4310 " git rebase --edit-todo\n"
4311 " git rebase --continue\n");
4313 static int pick_commits(struct repository
*r
,
4314 struct todo_list
*todo_list
,
4315 struct replay_opts
*opts
)
4317 int res
= 0, reschedule
= 0;
4318 char *prev_reflog_action
;
4320 /* Note that 0 for 3rd parameter of setenv means set only if not set */
4321 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
4322 prev_reflog_action
= xstrdup(getenv(GIT_REFLOG_ACTION
));
4324 assert(!(opts
->signoff
|| opts
->no_commit
||
4325 opts
->record_origin
|| should_edit(opts
) ||
4326 opts
->committer_date_is_author_date
||
4327 opts
->ignore_date
));
4328 if (read_and_refresh_cache(r
, opts
))
4331 while (todo_list
->current
< todo_list
->nr
) {
4332 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
4333 const char *arg
= todo_item_get_arg(todo_list
, item
);
4336 if (save_todo(todo_list
, opts
))
4338 if (is_rebase_i(opts
)) {
4339 if (item
->command
!= TODO_COMMENT
) {
4340 FILE *f
= fopen(rebase_path_msgnum(), "w");
4342 todo_list
->done_nr
++;
4345 fprintf(f
, "%d\n", todo_list
->done_nr
);
4349 fprintf(stderr
, _("Rebasing (%d/%d)%s"),
4351 todo_list
->total_nr
,
4352 opts
->verbose
? "\n" : "\r");
4354 unlink(rebase_path_message());
4355 unlink(rebase_path_author_script());
4356 unlink(rebase_path_stopped_sha());
4357 unlink(rebase_path_amend());
4358 unlink(git_path_merge_head(r
));
4359 unlink(git_path_auto_merge(r
));
4360 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
4362 if (item
->command
== TODO_BREAK
) {
4365 return stopped_at_head(r
);
4368 if (item
->command
<= TODO_SQUASH
) {
4369 if (is_rebase_i(opts
))
4370 setenv(GIT_REFLOG_ACTION
, reflog_message(opts
,
4371 command_to_string(item
->command
), NULL
),
4373 res
= do_pick_commit(r
, item
, opts
,
4374 is_final_fixup(todo_list
),
4376 if (is_rebase_i(opts
))
4377 setenv(GIT_REFLOG_ACTION
, prev_reflog_action
, 1);
4378 if (is_rebase_i(opts
) && res
< 0) {
4380 advise(_(rescheduled_advice
),
4381 get_item_line_length(todo_list
,
4382 todo_list
->current
),
4383 get_item_line(todo_list
,
4384 todo_list
->current
));
4385 todo_list
->current
--;
4386 if (save_todo(todo_list
, opts
))
4389 if (item
->command
== TODO_EDIT
) {
4390 struct commit
*commit
= item
->commit
;
4395 _("Stopped at %s... %.*s\n"),
4396 short_commit_name(commit
),
4397 item
->arg_len
, arg
);
4399 return error_with_patch(r
, commit
,
4400 arg
, item
->arg_len
, opts
, res
, !res
);
4402 if (is_rebase_i(opts
) && !res
)
4403 record_in_rewritten(&item
->commit
->object
.oid
,
4404 peek_command(todo_list
, 1));
4405 if (res
&& is_fixup(item
->command
)) {
4408 return error_failed_squash(r
, item
->commit
, opts
,
4409 item
->arg_len
, arg
);
4410 } else if (res
&& is_rebase_i(opts
) && item
->commit
) {
4412 struct object_id oid
;
4415 * If we are rewording and have either
4416 * fast-forwarded already, or are about to
4417 * create a new root commit, we want to amend,
4418 * otherwise we do not.
4420 if (item
->command
== TODO_REWORD
&&
4421 !get_oid("HEAD", &oid
) &&
4422 (oideq(&item
->commit
->object
.oid
, &oid
) ||
4423 (opts
->have_squash_onto
&&
4424 oideq(&opts
->squash_onto
, &oid
))))
4427 return res
| error_with_patch(r
, item
->commit
,
4428 arg
, item
->arg_len
, opts
,
4431 } else if (item
->command
== TODO_EXEC
) {
4432 char *end_of_arg
= (char *)(arg
+ item
->arg_len
);
4433 int saved
= *end_of_arg
;
4438 res
= do_exec(r
, arg
);
4439 *end_of_arg
= saved
;
4442 if (opts
->reschedule_failed_exec
)
4446 } else if (item
->command
== TODO_LABEL
) {
4447 if ((res
= do_label(r
, arg
, item
->arg_len
)))
4449 } else if (item
->command
== TODO_RESET
) {
4450 if ((res
= do_reset(r
, arg
, item
->arg_len
, opts
)))
4452 } else if (item
->command
== TODO_MERGE
) {
4453 if ((res
= do_merge(r
, item
->commit
, arg
, item
->arg_len
,
4454 item
->flags
, &check_todo
, opts
)) < 0)
4456 else if (item
->commit
)
4457 record_in_rewritten(&item
->commit
->object
.oid
,
4458 peek_command(todo_list
, 1));
4460 /* failed with merge conflicts */
4461 return error_with_patch(r
, item
->commit
,
4464 } else if (!is_noop(item
->command
))
4465 return error(_("unknown command %d"), item
->command
);
4468 advise(_(rescheduled_advice
),
4469 get_item_line_length(todo_list
,
4470 todo_list
->current
),
4471 get_item_line(todo_list
, todo_list
->current
));
4472 todo_list
->current
--;
4473 if (save_todo(todo_list
, opts
))
4476 return error_with_patch(r
,
4480 } else if (is_rebase_i(opts
) && check_todo
&& !res
&&
4481 reread_todo_if_changed(r
, todo_list
, opts
)) {
4485 todo_list
->current
++;
4490 if (is_rebase_i(opts
)) {
4491 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
4494 /* Stopped in the middle, as planned? */
4495 if (todo_list
->current
< todo_list
->nr
)
4498 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
4499 starts_with(head_ref
.buf
, "refs/")) {
4501 struct object_id head
, orig
;
4504 if (get_oid("HEAD", &head
)) {
4505 res
= error(_("cannot read HEAD"));
4507 strbuf_release(&head_ref
);
4508 strbuf_release(&buf
);
4511 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
4512 get_oid_hex(buf
.buf
, &orig
)) {
4513 res
= error(_("could not read orig-head"));
4514 goto cleanup_head_ref
;
4517 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
4518 res
= error(_("could not read 'onto'"));
4519 goto cleanup_head_ref
;
4521 msg
= reflog_message(opts
, "finish", "%s onto %s",
4522 head_ref
.buf
, buf
.buf
);
4523 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
4524 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
4525 res
= error(_("could not update %s"),
4527 goto cleanup_head_ref
;
4529 msg
= reflog_message(opts
, "finish", "returning to %s",
4531 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
4532 res
= error(_("could not update HEAD to %s"),
4534 goto cleanup_head_ref
;
4539 if (opts
->verbose
) {
4540 struct rev_info log_tree_opt
;
4541 struct object_id orig
, head
;
4543 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
4544 repo_init_revisions(r
, &log_tree_opt
, NULL
);
4545 log_tree_opt
.diff
= 1;
4546 log_tree_opt
.diffopt
.output_format
=
4547 DIFF_FORMAT_DIFFSTAT
;
4548 log_tree_opt
.disable_stdin
= 1;
4550 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
4551 !get_oid(buf
.buf
, &orig
) &&
4552 !get_oid("HEAD", &head
)) {
4553 diff_tree_oid(&orig
, &head
, "",
4554 &log_tree_opt
.diffopt
);
4555 log_tree_diff_flush(&log_tree_opt
);
4558 flush_rewritten_pending();
4559 if (!stat(rebase_path_rewritten_list(), &st
) &&
4561 struct child_process child
= CHILD_PROCESS_INIT
;
4562 const char *post_rewrite_hook
=
4563 find_hook("post-rewrite");
4565 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
4567 strvec_push(&child
.args
, "notes");
4568 strvec_push(&child
.args
, "copy");
4569 strvec_push(&child
.args
, "--for-rewrite=rebase");
4570 /* we don't care if this copying failed */
4571 run_command(&child
);
4573 if (post_rewrite_hook
) {
4574 struct child_process hook
= CHILD_PROCESS_INIT
;
4576 hook
.in
= open(rebase_path_rewritten_list(),
4578 hook
.stdout_to_stderr
= 1;
4579 hook
.trace2_hook_name
= "post-rewrite";
4580 strvec_push(&hook
.args
, post_rewrite_hook
);
4581 strvec_push(&hook
.args
, "rebase");
4582 /* we don't care if this hook failed */
4586 apply_autostash(rebase_path_autostash());
4592 _("Successfully rebased and updated %s.\n"),
4596 strbuf_release(&buf
);
4597 strbuf_release(&head_ref
);
4601 * Sequence of picks finished successfully; cleanup by
4602 * removing the .git/sequencer directory
4604 return sequencer_remove_state(opts
);
4607 static int continue_single_pick(struct repository
*r
, struct replay_opts
*opts
)
4609 struct strvec argv
= STRVEC_INIT
;
4612 if (!refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
4613 !refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD"))
4614 return error(_("no cherry-pick or revert in progress"));
4616 strvec_push(&argv
, "commit");
4619 * continue_single_pick() handles the case of recovering from a
4620 * conflict. should_edit() doesn't handle that case; for a conflict,
4621 * we want to edit if the user asked for it, or if they didn't specify
4622 * and stdin is a tty.
4624 if (!opts
->edit
|| (opts
->edit
< 0 && !isatty(0)))
4626 * Include --cleanup=strip as well because we don't want the
4627 * "# Conflicts:" messages.
4629 strvec_pushl(&argv
, "--no-edit", "--cleanup=strip", NULL
);
4631 ret
= run_command_v_opt(argv
.v
, RUN_GIT_CMD
);
4632 strvec_clear(&argv
);
4636 static int commit_staged_changes(struct repository
*r
,
4637 struct replay_opts
*opts
,
4638 struct todo_list
*todo_list
)
4640 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
4641 unsigned int final_fixup
= 0, is_clean
;
4643 if (has_unstaged_changes(r
, 1))
4644 return error(_("cannot rebase: You have unstaged changes."));
4646 is_clean
= !has_uncommitted_changes(r
, 0);
4648 if (file_exists(rebase_path_amend())) {
4649 struct strbuf rev
= STRBUF_INIT
;
4650 struct object_id head
, to_amend
;
4652 if (get_oid("HEAD", &head
))
4653 return error(_("cannot amend non-existing commit"));
4654 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
4655 return error(_("invalid file: '%s'"), rebase_path_amend());
4656 if (get_oid_hex(rev
.buf
, &to_amend
))
4657 return error(_("invalid contents: '%s'"),
4658 rebase_path_amend());
4659 if (!is_clean
&& !oideq(&head
, &to_amend
))
4660 return error(_("\nYou have uncommitted changes in your "
4661 "working tree. Please, commit them\n"
4662 "first and then run 'git rebase "
4663 "--continue' again."));
4665 * When skipping a failed fixup/squash, we need to edit the
4666 * commit message, the current fixup list and count, and if it
4667 * was the last fixup/squash in the chain, we need to clean up
4668 * the commit message and if there was a squash, let the user
4671 if (!is_clean
|| !opts
->current_fixup_count
)
4672 ; /* this is not the final fixup */
4673 else if (!oideq(&head
, &to_amend
) ||
4674 !file_exists(rebase_path_stopped_sha())) {
4675 /* was a final fixup or squash done manually? */
4676 if (!is_fixup(peek_command(todo_list
, 0))) {
4677 unlink(rebase_path_fixup_msg());
4678 unlink(rebase_path_squash_msg());
4679 unlink(rebase_path_current_fixups());
4680 strbuf_reset(&opts
->current_fixups
);
4681 opts
->current_fixup_count
= 0;
4684 /* we are in a fixup/squash chain */
4685 const char *p
= opts
->current_fixups
.buf
;
4686 int len
= opts
->current_fixups
.len
;
4688 opts
->current_fixup_count
--;
4690 BUG("Incorrect current_fixups:\n%s", p
);
4691 while (len
&& p
[len
- 1] != '\n')
4693 strbuf_setlen(&opts
->current_fixups
, len
);
4694 if (write_message(p
, len
, rebase_path_current_fixups(),
4696 return error(_("could not write file: '%s'"),
4697 rebase_path_current_fixups());
4700 * If a fixup/squash in a fixup/squash chain failed, the
4701 * commit message is already correct, no need to commit
4704 * Only if it is the final command in the fixup/squash
4705 * chain, and only if the chain is longer than a single
4706 * fixup/squash command (which was just skipped), do we
4707 * actually need to re-commit with a cleaned up commit
4710 if (opts
->current_fixup_count
> 0 &&
4711 !is_fixup(peek_command(todo_list
, 0))) {
4714 * If there was not a single "squash" in the
4715 * chain, we only need to clean up the commit
4716 * message, no need to bother the user with
4717 * opening the commit message in the editor.
4719 if (!starts_with(p
, "squash ") &&
4720 !strstr(p
, "\nsquash "))
4721 flags
= (flags
& ~EDIT_MSG
) | CLEANUP_MSG
;
4722 } else if (is_fixup(peek_command(todo_list
, 0))) {
4724 * We need to update the squash message to skip
4725 * the latest commit message.
4727 struct commit
*commit
;
4728 const char *path
= rebase_path_squash_msg();
4729 const char *encoding
= get_commit_output_encoding();
4731 if (parse_head(r
, &commit
) ||
4732 !(p
= logmsg_reencode(commit
, NULL
, encoding
)) ||
4733 write_message(p
, strlen(p
), path
, 0)) {
4734 unuse_commit_buffer(commit
, p
);
4735 return error(_("could not write file: "
4738 unuse_commit_buffer(commit
, p
);
4742 strbuf_release(&rev
);
4747 if (refs_ref_exists(get_main_ref_store(r
),
4748 "CHERRY_PICK_HEAD") &&
4749 refs_delete_ref(get_main_ref_store(r
), "",
4750 "CHERRY_PICK_HEAD", NULL
, 0))
4751 return error(_("could not remove CHERRY_PICK_HEAD"));
4752 if (unlink(git_path_merge_msg(r
)) && errno
!= ENOENT
)
4753 return error_errno(_("could not remove '%s'"),
4754 git_path_merge_msg(r
));
4759 if (run_git_commit(final_fixup
? NULL
: rebase_path_message(),
4761 return error(_("could not commit staged changes."));
4762 unlink(rebase_path_amend());
4763 unlink(git_path_merge_head(r
));
4764 unlink(git_path_auto_merge(r
));
4766 unlink(rebase_path_fixup_msg());
4767 unlink(rebase_path_squash_msg());
4769 if (opts
->current_fixup_count
> 0) {
4771 * Whether final fixup or not, we just cleaned up the commit
4774 unlink(rebase_path_current_fixups());
4775 strbuf_reset(&opts
->current_fixups
);
4776 opts
->current_fixup_count
= 0;
4781 int sequencer_continue(struct repository
*r
, struct replay_opts
*opts
)
4783 struct todo_list todo_list
= TODO_LIST_INIT
;
4786 if (read_and_refresh_cache(r
, opts
))
4789 if (read_populate_opts(opts
))
4791 if (is_rebase_i(opts
)) {
4792 if ((res
= read_populate_todo(r
, &todo_list
, opts
)))
4793 goto release_todo_list
;
4795 if (file_exists(rebase_path_dropped())) {
4796 if ((res
= todo_list_check_against_backup(r
, &todo_list
)))
4797 goto release_todo_list
;
4799 unlink(rebase_path_dropped());
4802 if (commit_staged_changes(r
, opts
, &todo_list
)) {
4804 goto release_todo_list
;
4806 } else if (!file_exists(get_todo_path(opts
)))
4807 return continue_single_pick(r
, opts
);
4808 else if ((res
= read_populate_todo(r
, &todo_list
, opts
)))
4809 goto release_todo_list
;
4811 if (!is_rebase_i(opts
)) {
4812 /* Verify that the conflict has been resolved */
4813 if (refs_ref_exists(get_main_ref_store(r
),
4814 "CHERRY_PICK_HEAD") ||
4815 refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD")) {
4816 res
= continue_single_pick(r
, opts
);
4818 goto release_todo_list
;
4820 if (index_differs_from(r
, "HEAD", NULL
, 0)) {
4821 res
= error_dirty_index(r
, opts
);
4822 goto release_todo_list
;
4824 todo_list
.current
++;
4825 } else if (file_exists(rebase_path_stopped_sha())) {
4826 struct strbuf buf
= STRBUF_INIT
;
4827 struct object_id oid
;
4829 if (read_oneliner(&buf
, rebase_path_stopped_sha(),
4830 READ_ONELINER_SKIP_IF_EMPTY
) &&
4831 !get_oid_hex(buf
.buf
, &oid
))
4832 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
4833 strbuf_release(&buf
);
4836 res
= pick_commits(r
, &todo_list
, opts
);
4838 todo_list_release(&todo_list
);
4842 static int single_pick(struct repository
*r
,
4843 struct commit
*cmit
,
4844 struct replay_opts
*opts
)
4847 struct todo_item item
;
4849 item
.command
= opts
->action
== REPLAY_PICK
?
4850 TODO_PICK
: TODO_REVERT
;
4853 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
4854 return do_pick_commit(r
, &item
, opts
, 0, &check_todo
);
4857 int sequencer_pick_revisions(struct repository
*r
,
4858 struct replay_opts
*opts
)
4860 struct todo_list todo_list
= TODO_LIST_INIT
;
4861 struct object_id oid
;
4865 if (read_and_refresh_cache(r
, opts
))
4868 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
4869 struct object_id oid
;
4870 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
4872 /* This happens when using --stdin. */
4876 if (!get_oid(name
, &oid
)) {
4877 if (!lookup_commit_reference_gently(r
, &oid
, 1)) {
4878 enum object_type type
= oid_object_info(r
,
4881 return error(_("%s: can't cherry-pick a %s"),
4882 name
, type_name(type
));
4885 return error(_("%s: bad revision"), name
);
4889 * If we were called as "git cherry-pick <commit>", just
4890 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
4891 * REVERT_HEAD, and don't touch the sequencer state.
4892 * This means it is possible to cherry-pick in the middle
4893 * of a cherry-pick sequence.
4895 if (opts
->revs
->cmdline
.nr
== 1 &&
4896 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
4897 opts
->revs
->no_walk
&&
4898 !opts
->revs
->cmdline
.rev
->flags
) {
4899 struct commit
*cmit
;
4900 if (prepare_revision_walk(opts
->revs
))
4901 return error(_("revision walk setup failed"));
4902 cmit
= get_revision(opts
->revs
);
4904 return error(_("empty commit set passed"));
4905 if (get_revision(opts
->revs
))
4906 BUG("unexpected extra commit from walk");
4907 return single_pick(r
, cmit
, opts
);
4911 * Start a new cherry-pick/ revert sequence; but
4912 * first, make sure that an existing one isn't in
4916 if (walk_revs_populate_todo(&todo_list
, opts
) ||
4917 create_seq_dir(r
) < 0)
4919 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
4920 return error(_("can't revert as initial commit"));
4921 if (save_head(oid_to_hex(&oid
)))
4923 if (save_opts(opts
))
4925 update_abort_safety_file();
4926 res
= pick_commits(r
, &todo_list
, opts
);
4927 todo_list_release(&todo_list
);
4931 void append_signoff(struct strbuf
*msgbuf
, size_t ignore_footer
, unsigned flag
)
4933 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
4934 struct strbuf sob
= STRBUF_INIT
;
4937 strbuf_addstr(&sob
, sign_off_header
);
4938 strbuf_addstr(&sob
, fmt_name(WANT_COMMITTER_IDENT
));
4939 strbuf_addch(&sob
, '\n');
4942 strbuf_complete_line(msgbuf
);
4945 * If the whole message buffer is equal to the sob, pretend that we
4946 * found a conforming footer with a matching sob
4948 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
4949 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
4952 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
4955 const char *append_newlines
= NULL
;
4956 size_t len
= msgbuf
->len
- ignore_footer
;
4960 * The buffer is completely empty. Leave foom for
4961 * the title and body to be filled in by the user.
4963 append_newlines
= "\n\n";
4964 } else if (len
== 1) {
4966 * Buffer contains a single newline. Add another
4967 * so that we leave room for the title and body.
4969 append_newlines
= "\n";
4970 } else if (msgbuf
->buf
[len
- 2] != '\n') {
4972 * Buffer ends with a single newline. Add another
4973 * so that there is an empty line between the message
4976 append_newlines
= "\n";
4977 } /* else, the buffer already ends with two newlines. */
4979 if (append_newlines
)
4980 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
4981 append_newlines
, strlen(append_newlines
));
4984 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
4985 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
4988 strbuf_release(&sob
);
4991 struct labels_entry
{
4992 struct hashmap_entry entry
;
4993 char label
[FLEX_ARRAY
];
4996 static int labels_cmp(const void *fndata
, const struct hashmap_entry
*eptr
,
4997 const struct hashmap_entry
*entry_or_key
, const void *key
)
4999 const struct labels_entry
*a
, *b
;
5001 a
= container_of(eptr
, const struct labels_entry
, entry
);
5002 b
= container_of(entry_or_key
, const struct labels_entry
, entry
);
5004 return key
? strcmp(a
->label
, key
) : strcmp(a
->label
, b
->label
);
5007 struct string_entry
{
5008 struct oidmap_entry entry
;
5009 char string
[FLEX_ARRAY
];
5012 struct label_state
{
5013 struct oidmap commit2label
;
5014 struct hashmap labels
;
5018 static const char *label_oid(struct object_id
*oid
, const char *label
,
5019 struct label_state
*state
)
5021 struct labels_entry
*labels_entry
;
5022 struct string_entry
*string_entry
;
5023 struct object_id dummy
;
5026 string_entry
= oidmap_get(&state
->commit2label
, oid
);
5028 return string_entry
->string
;
5031 * For "uninteresting" commits, i.e. commits that are not to be
5032 * rebased, and which can therefore not be labeled, we use a unique
5033 * abbreviation of the commit name. This is slightly more complicated
5034 * than calling find_unique_abbrev() because we also need to make
5035 * sure that the abbreviation does not conflict with any other
5038 * We disallow "interesting" commits to be labeled by a string that
5039 * is a valid full-length hash, to ensure that we always can find an
5040 * abbreviation for any uninteresting commit's names that does not
5041 * clash with any other label.
5043 strbuf_reset(&state
->buf
);
5047 strbuf_grow(&state
->buf
, GIT_MAX_HEXSZ
);
5048 label
= p
= state
->buf
.buf
;
5050 find_unique_abbrev_r(p
, oid
, default_abbrev
);
5053 * We may need to extend the abbreviated hash so that there is
5054 * no conflicting label.
5056 if (hashmap_get_from_hash(&state
->labels
, strihash(p
), p
)) {
5057 size_t i
= strlen(p
) + 1;
5059 oid_to_hex_r(p
, oid
);
5060 for (; i
< the_hash_algo
->hexsz
; i
++) {
5063 if (!hashmap_get_from_hash(&state
->labels
,
5070 struct strbuf
*buf
= &state
->buf
;
5073 * Sanitize labels by replacing non-alpha-numeric characters
5074 * (including white-space ones) by dashes, as they might be
5075 * illegal in file names (and hence in ref names).
5077 * Note that we retain non-ASCII UTF-8 characters (identified
5078 * via the most significant bit). They should be all acceptable
5079 * in file names. We do not validate the UTF-8 here, that's not
5080 * the job of this function.
5082 for (; *label
; label
++)
5083 if ((*label
& 0x80) || isalnum(*label
))
5084 strbuf_addch(buf
, *label
);
5085 /* avoid leading dash and double-dashes */
5086 else if (buf
->len
&& buf
->buf
[buf
->len
- 1] != '-')
5087 strbuf_addch(buf
, '-');
5089 strbuf_addstr(buf
, "rev-");
5090 strbuf_add_unique_abbrev(buf
, oid
, default_abbrev
);
5094 if ((buf
->len
== the_hash_algo
->hexsz
&&
5095 !get_oid_hex(label
, &dummy
)) ||
5096 (buf
->len
== 1 && *label
== '#') ||
5097 hashmap_get_from_hash(&state
->labels
,
5098 strihash(label
), label
)) {
5100 * If the label already exists, or if the label is a
5101 * valid full OID, or the label is a '#' (which we use
5102 * as a separator between merge heads and oneline), we
5103 * append a dash and a number to make it unique.
5105 size_t len
= buf
->len
;
5107 for (i
= 2; ; i
++) {
5108 strbuf_setlen(buf
, len
);
5109 strbuf_addf(buf
, "-%d", i
);
5110 if (!hashmap_get_from_hash(&state
->labels
,
5120 FLEX_ALLOC_STR(labels_entry
, label
, label
);
5121 hashmap_entry_init(&labels_entry
->entry
, strihash(label
));
5122 hashmap_add(&state
->labels
, &labels_entry
->entry
);
5124 FLEX_ALLOC_STR(string_entry
, string
, label
);
5125 oidcpy(&string_entry
->entry
.oid
, oid
);
5126 oidmap_put(&state
->commit2label
, string_entry
);
5128 return string_entry
->string
;
5131 static int make_script_with_merges(struct pretty_print_context
*pp
,
5132 struct rev_info
*revs
, struct strbuf
*out
,
5135 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
5136 int rebase_cousins
= flags
& TODO_LIST_REBASE_COUSINS
;
5137 int root_with_onto
= flags
& TODO_LIST_ROOT_WITH_ONTO
;
5138 int skipped_commit
= 0;
5139 struct strbuf buf
= STRBUF_INIT
, oneline
= STRBUF_INIT
;
5140 struct strbuf label
= STRBUF_INIT
;
5141 struct commit_list
*commits
= NULL
, **tail
= &commits
, *iter
;
5142 struct commit_list
*tips
= NULL
, **tips_tail
= &tips
;
5143 struct commit
*commit
;
5144 struct oidmap commit2todo
= OIDMAP_INIT
;
5145 struct string_entry
*entry
;
5146 struct oidset interesting
= OIDSET_INIT
, child_seen
= OIDSET_INIT
,
5147 shown
= OIDSET_INIT
;
5148 struct label_state state
= { OIDMAP_INIT
, { NULL
}, STRBUF_INIT
};
5150 int abbr
= flags
& TODO_LIST_ABBREVIATE_CMDS
;
5151 const char *cmd_pick
= abbr
? "p" : "pick",
5152 *cmd_label
= abbr
? "l" : "label",
5153 *cmd_reset
= abbr
? "t" : "reset",
5154 *cmd_merge
= abbr
? "m" : "merge";
5156 oidmap_init(&commit2todo
, 0);
5157 oidmap_init(&state
.commit2label
, 0);
5158 hashmap_init(&state
.labels
, labels_cmp
, NULL
, 0);
5159 strbuf_init(&state
.buf
, 32);
5161 if (revs
->cmdline
.nr
&& (revs
->cmdline
.rev
[0].flags
& BOTTOM
)) {
5162 struct labels_entry
*onto_label_entry
;
5163 struct object_id
*oid
= &revs
->cmdline
.rev
[0].item
->oid
;
5164 FLEX_ALLOC_STR(entry
, string
, "onto");
5165 oidcpy(&entry
->entry
.oid
, oid
);
5166 oidmap_put(&state
.commit2label
, entry
);
5168 FLEX_ALLOC_STR(onto_label_entry
, label
, "onto");
5169 hashmap_entry_init(&onto_label_entry
->entry
, strihash("onto"));
5170 hashmap_add(&state
.labels
, &onto_label_entry
->entry
);
5175 * - get onelines for all commits
5176 * - gather all branch tips (i.e. 2nd or later parents of merges)
5177 * - label all branch tips
5179 while ((commit
= get_revision(revs
))) {
5180 struct commit_list
*to_merge
;
5181 const char *p1
, *p2
;
5182 struct object_id
*oid
;
5185 tail
= &commit_list_insert(commit
, tail
)->next
;
5186 oidset_insert(&interesting
, &commit
->object
.oid
);
5188 is_empty
= is_original_commit_empty(commit
);
5189 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
)) {
5190 if (flags
& TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
)
5191 warning(_("skipped previously applied commit %s"),
5192 short_commit_name(commit
));
5196 if (is_empty
&& !keep_empty
)
5199 strbuf_reset(&oneline
);
5200 pretty_print_commit(pp
, commit
, &oneline
);
5202 to_merge
= commit
->parents
? commit
->parents
->next
: NULL
;
5204 /* non-merge commit: easy case */
5206 strbuf_addf(&buf
, "%s %s %s", cmd_pick
,
5207 oid_to_hex(&commit
->object
.oid
),
5210 strbuf_addf(&buf
, " %c empty",
5213 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
5214 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
5215 oidmap_put(&commit2todo
, entry
);
5220 /* Create a label */
5221 strbuf_reset(&label
);
5222 if (skip_prefix(oneline
.buf
, "Merge ", &p1
) &&
5223 (p1
= strchr(p1
, '\'')) &&
5224 (p2
= strchr(++p1
, '\'')))
5225 strbuf_add(&label
, p1
, p2
- p1
);
5226 else if (skip_prefix(oneline
.buf
, "Merge pull request ",
5228 (p1
= strstr(p1
, " from ")))
5229 strbuf_addstr(&label
, p1
+ strlen(" from "));
5231 strbuf_addbuf(&label
, &oneline
);
5234 strbuf_addf(&buf
, "%s -C %s",
5235 cmd_merge
, oid_to_hex(&commit
->object
.oid
));
5237 /* label the tips of merged branches */
5238 for (; to_merge
; to_merge
= to_merge
->next
) {
5239 oid
= &to_merge
->item
->object
.oid
;
5240 strbuf_addch(&buf
, ' ');
5242 if (!oidset_contains(&interesting
, oid
)) {
5243 strbuf_addstr(&buf
, label_oid(oid
, NULL
,
5248 tips_tail
= &commit_list_insert(to_merge
->item
,
5251 strbuf_addstr(&buf
, label_oid(oid
, label
.buf
, &state
));
5253 strbuf_addf(&buf
, " # %s", oneline
.buf
);
5255 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
5256 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
5257 oidmap_put(&commit2todo
, entry
);
5260 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS
,
5261 _("use --reapply-cherry-picks to include skipped commits"));
5265 * - label branch points
5266 * - add HEAD to the branch tips
5268 for (iter
= commits
; iter
; iter
= iter
->next
) {
5269 struct commit_list
*parent
= iter
->item
->parents
;
5270 for (; parent
; parent
= parent
->next
) {
5271 struct object_id
*oid
= &parent
->item
->object
.oid
;
5272 if (!oidset_contains(&interesting
, oid
))
5274 if (oidset_insert(&child_seen
, oid
))
5275 label_oid(oid
, "branch-point", &state
);
5278 /* Add HEAD as implicit "tip of branch" */
5280 tips_tail
= &commit_list_insert(iter
->item
,
5285 * Third phase: output the todo list. This is a bit tricky, as we
5286 * want to avoid jumping back and forth between revisions. To
5287 * accomplish that goal, we walk backwards from the branch tips,
5288 * gathering commits not yet shown, reversing the list on the fly,
5289 * then outputting that list (labeling revisions as needed).
5291 strbuf_addf(out
, "%s onto\n", cmd_label
);
5292 for (iter
= tips
; iter
; iter
= iter
->next
) {
5293 struct commit_list
*list
= NULL
, *iter2
;
5295 commit
= iter
->item
;
5296 if (oidset_contains(&shown
, &commit
->object
.oid
))
5298 entry
= oidmap_get(&state
.commit2label
, &commit
->object
.oid
);
5301 strbuf_addf(out
, "\n%c Branch %s\n", comment_line_char
, entry
->string
);
5303 strbuf_addch(out
, '\n');
5305 while (oidset_contains(&interesting
, &commit
->object
.oid
) &&
5306 !oidset_contains(&shown
, &commit
->object
.oid
)) {
5307 commit_list_insert(commit
, &list
);
5308 if (!commit
->parents
) {
5312 commit
= commit
->parents
->item
;
5316 strbuf_addf(out
, "%s %s\n", cmd_reset
,
5317 rebase_cousins
|| root_with_onto
?
5318 "onto" : "[new root]");
5320 const char *to
= NULL
;
5322 entry
= oidmap_get(&state
.commit2label
,
5323 &commit
->object
.oid
);
5326 else if (!rebase_cousins
)
5327 to
= label_oid(&commit
->object
.oid
, NULL
,
5330 if (!to
|| !strcmp(to
, "onto"))
5331 strbuf_addf(out
, "%s onto\n", cmd_reset
);
5333 strbuf_reset(&oneline
);
5334 pretty_print_commit(pp
, commit
, &oneline
);
5335 strbuf_addf(out
, "%s %s # %s\n",
5336 cmd_reset
, to
, oneline
.buf
);
5340 for (iter2
= list
; iter2
; iter2
= iter2
->next
) {
5341 struct object_id
*oid
= &iter2
->item
->object
.oid
;
5342 entry
= oidmap_get(&commit2todo
, oid
);
5343 /* only show if not already upstream */
5345 strbuf_addf(out
, "%s\n", entry
->string
);
5346 entry
= oidmap_get(&state
.commit2label
, oid
);
5348 strbuf_addf(out
, "%s %s\n",
5349 cmd_label
, entry
->string
);
5350 oidset_insert(&shown
, oid
);
5353 free_commit_list(list
);
5356 free_commit_list(commits
);
5357 free_commit_list(tips
);
5359 strbuf_release(&label
);
5360 strbuf_release(&oneline
);
5361 strbuf_release(&buf
);
5363 oidmap_free(&commit2todo
, 1);
5364 oidmap_free(&state
.commit2label
, 1);
5365 hashmap_clear_and_free(&state
.labels
, struct labels_entry
, entry
);
5366 strbuf_release(&state
.buf
);
5371 int sequencer_make_script(struct repository
*r
, struct strbuf
*out
, int argc
,
5372 const char **argv
, unsigned flags
)
5374 char *format
= NULL
;
5375 struct pretty_print_context pp
= {0};
5376 struct rev_info revs
;
5377 struct commit
*commit
;
5378 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
5379 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
5380 int rebase_merges
= flags
& TODO_LIST_REBASE_MERGES
;
5381 int reapply_cherry_picks
= flags
& TODO_LIST_REAPPLY_CHERRY_PICKS
;
5382 int skipped_commit
= 0;
5384 repo_init_revisions(r
, &revs
, NULL
);
5385 revs
.verbose_header
= 1;
5387 revs
.max_parents
= 1;
5388 revs
.cherry_mark
= !reapply_cherry_picks
;
5391 revs
.right_only
= 1;
5392 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
5393 revs
.topo_order
= 1;
5395 revs
.pretty_given
= 1;
5396 git_config_get_string("rebase.instructionFormat", &format
);
5397 if (!format
|| !*format
) {
5399 format
= xstrdup("%s");
5401 get_commit_format(format
, &revs
);
5403 pp
.fmt
= revs
.commit_format
;
5404 pp
.output_encoding
= get_log_output_encoding();
5406 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
5407 return error(_("make_script: unhandled options"));
5409 if (prepare_revision_walk(&revs
) < 0)
5410 return error(_("make_script: error preparing revisions"));
5413 return make_script_with_merges(&pp
, &revs
, out
, flags
);
5415 while ((commit
= get_revision(&revs
))) {
5416 int is_empty
= is_original_commit_empty(commit
);
5418 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
)) {
5419 if (flags
& TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
)
5420 warning(_("skipped previously applied commit %s"),
5421 short_commit_name(commit
));
5425 if (is_empty
&& !keep_empty
)
5427 strbuf_addf(out
, "%s %s ", insn
,
5428 oid_to_hex(&commit
->object
.oid
));
5429 pretty_print_commit(&pp
, commit
, out
);
5431 strbuf_addf(out
, " %c empty", comment_line_char
);
5432 strbuf_addch(out
, '\n');
5435 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS
,
5436 _("use --reapply-cherry-picks to include skipped commits"));
5441 * Add commands after pick and (series of) squash/fixup commands
5444 static void todo_list_add_exec_commands(struct todo_list
*todo_list
,
5445 struct string_list
*commands
)
5447 struct strbuf
*buf
= &todo_list
->buf
;
5448 size_t base_offset
= buf
->len
;
5449 int i
, insert
, nr
= 0, alloc
= 0;
5450 struct todo_item
*items
= NULL
, *base_items
= NULL
;
5452 CALLOC_ARRAY(base_items
, commands
->nr
);
5453 for (i
= 0; i
< commands
->nr
; i
++) {
5454 size_t command_len
= strlen(commands
->items
[i
].string
);
5456 strbuf_addstr(buf
, commands
->items
[i
].string
);
5457 strbuf_addch(buf
, '\n');
5459 base_items
[i
].command
= TODO_EXEC
;
5460 base_items
[i
].offset_in_buf
= base_offset
;
5461 base_items
[i
].arg_offset
= base_offset
+ strlen("exec ");
5462 base_items
[i
].arg_len
= command_len
- strlen("exec ");
5464 base_offset
+= command_len
+ 1;
5468 * Insert <commands> after every pick. Here, fixup/squash chains
5469 * are considered part of the pick, so we insert the commands *after*
5470 * those chains if there are any.
5472 * As we insert the exec commands immediately after rearranging
5473 * any fixups and before the user edits the list, a fixup chain
5474 * can never contain comments (any comments are empty picks that
5475 * have been commented out because the user did not specify
5476 * --keep-empty). So, it is safe to insert an exec command
5477 * without looking at the command following a comment.
5480 for (i
= 0; i
< todo_list
->nr
; i
++) {
5481 enum todo_command command
= todo_list
->items
[i
].command
;
5482 if (insert
&& !is_fixup(command
)) {
5483 ALLOC_GROW(items
, nr
+ commands
->nr
, alloc
);
5484 COPY_ARRAY(items
+ nr
, base_items
, commands
->nr
);
5490 ALLOC_GROW(items
, nr
+ 1, alloc
);
5491 items
[nr
++] = todo_list
->items
[i
];
5493 if (command
== TODO_PICK
|| command
== TODO_MERGE
)
5497 /* insert or append final <commands> */
5499 ALLOC_GROW(items
, nr
+ commands
->nr
, alloc
);
5500 COPY_ARRAY(items
+ nr
, base_items
, commands
->nr
);
5505 FREE_AND_NULL(todo_list
->items
);
5506 todo_list
->items
= items
;
5508 todo_list
->alloc
= alloc
;
5511 static void todo_list_to_strbuf(struct repository
*r
, struct todo_list
*todo_list
,
5512 struct strbuf
*buf
, int num
, unsigned flags
)
5514 struct todo_item
*item
;
5515 int i
, max
= todo_list
->nr
;
5517 if (num
> 0 && num
< max
)
5520 for (item
= todo_list
->items
, i
= 0; i
< max
; i
++, item
++) {
5523 /* if the item is not a command write it and continue */
5524 if (item
->command
>= TODO_COMMENT
) {
5525 strbuf_addf(buf
, "%.*s\n", item
->arg_len
,
5526 todo_item_get_arg(todo_list
, item
));
5530 /* add command to the buffer */
5531 cmd
= command_to_char(item
->command
);
5532 if ((flags
& TODO_LIST_ABBREVIATE_CMDS
) && cmd
)
5533 strbuf_addch(buf
, cmd
);
5535 strbuf_addstr(buf
, command_to_string(item
->command
));
5539 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
5540 short_commit_name(item
->commit
) :
5541 oid_to_hex(&item
->commit
->object
.oid
);
5543 if (item
->command
== TODO_FIXUP
) {
5544 if (item
->flags
& TODO_EDIT_FIXUP_MSG
)
5545 strbuf_addstr(buf
, " -c");
5546 else if (item
->flags
& TODO_REPLACE_FIXUP_MSG
) {
5547 strbuf_addstr(buf
, " -C");
5551 if (item
->command
== TODO_MERGE
) {
5552 if (item
->flags
& TODO_EDIT_MERGE_MSG
)
5553 strbuf_addstr(buf
, " -c");
5555 strbuf_addstr(buf
, " -C");
5558 strbuf_addf(buf
, " %s", oid
);
5561 /* add all the rest */
5563 strbuf_addch(buf
, '\n');
5565 strbuf_addf(buf
, " %.*s\n", item
->arg_len
,
5566 todo_item_get_arg(todo_list
, item
));
5570 int todo_list_write_to_file(struct repository
*r
, struct todo_list
*todo_list
,
5571 const char *file
, const char *shortrevisions
,
5572 const char *shortonto
, int num
, unsigned flags
)
5575 struct strbuf buf
= STRBUF_INIT
;
5577 todo_list_to_strbuf(r
, todo_list
, &buf
, num
, flags
);
5578 if (flags
& TODO_LIST_APPEND_TODO_HELP
)
5579 append_todo_help(count_commands(todo_list
),
5580 shortrevisions
, shortonto
, &buf
);
5582 res
= write_message(buf
.buf
, buf
.len
, file
, 0);
5583 strbuf_release(&buf
);
5588 /* skip picking commits whose parents are unchanged */
5589 static int skip_unnecessary_picks(struct repository
*r
,
5590 struct todo_list
*todo_list
,
5591 struct object_id
*base_oid
)
5593 struct object_id
*parent_oid
;
5596 for (i
= 0; i
< todo_list
->nr
; i
++) {
5597 struct todo_item
*item
= todo_list
->items
+ i
;
5599 if (item
->command
>= TODO_NOOP
)
5601 if (item
->command
!= TODO_PICK
)
5603 if (parse_commit(item
->commit
)) {
5604 return error(_("could not parse commit '%s'"),
5605 oid_to_hex(&item
->commit
->object
.oid
));
5607 if (!item
->commit
->parents
)
5608 break; /* root commit */
5609 if (item
->commit
->parents
->next
)
5610 break; /* merge commit */
5611 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
5612 if (!oideq(parent_oid
, base_oid
))
5614 oidcpy(base_oid
, &item
->commit
->object
.oid
);
5617 const char *done_path
= rebase_path_done();
5619 if (todo_list_write_to_file(r
, todo_list
, done_path
, NULL
, NULL
, i
, 0)) {
5620 error_errno(_("could not write to '%s'"), done_path
);
5624 MOVE_ARRAY(todo_list
->items
, todo_list
->items
+ i
, todo_list
->nr
- i
);
5626 todo_list
->current
= 0;
5627 todo_list
->done_nr
+= i
;
5629 if (is_fixup(peek_command(todo_list
, 0)))
5630 record_in_rewritten(base_oid
, peek_command(todo_list
, 0));
5636 int complete_action(struct repository
*r
, struct replay_opts
*opts
, unsigned flags
,
5637 const char *shortrevisions
, const char *onto_name
,
5638 struct commit
*onto
, const struct object_id
*orig_head
,
5639 struct string_list
*commands
, unsigned autosquash
,
5640 struct todo_list
*todo_list
)
5642 char shortonto
[GIT_MAX_HEXSZ
+ 1];
5643 const char *todo_file
= rebase_path_todo();
5644 struct todo_list new_todo
= TODO_LIST_INIT
;
5645 struct strbuf
*buf
= &todo_list
->buf
, buf2
= STRBUF_INIT
;
5646 struct object_id oid
= onto
->object
.oid
;
5649 find_unique_abbrev_r(shortonto
, &oid
, DEFAULT_ABBREV
);
5651 if (buf
->len
== 0) {
5652 struct todo_item
*item
= append_new_todo(todo_list
);
5653 item
->command
= TODO_NOOP
;
5654 item
->commit
= NULL
;
5655 item
->arg_len
= item
->arg_offset
= item
->flags
= item
->offset_in_buf
= 0;
5658 if (autosquash
&& todo_list_rearrange_squash(todo_list
))
5662 todo_list_add_exec_commands(todo_list
, commands
);
5664 if (count_commands(todo_list
) == 0) {
5665 apply_autostash(rebase_path_autostash());
5666 sequencer_remove_state(opts
);
5668 return error(_("nothing to do"));
5671 res
= edit_todo_list(r
, todo_list
, &new_todo
, shortrevisions
,
5675 else if (res
== -2) {
5676 apply_autostash(rebase_path_autostash());
5677 sequencer_remove_state(opts
);
5680 } else if (res
== -3) {
5681 apply_autostash(rebase_path_autostash());
5682 sequencer_remove_state(opts
);
5683 todo_list_release(&new_todo
);
5685 return error(_("nothing to do"));
5686 } else if (res
== -4) {
5687 checkout_onto(r
, opts
, onto_name
, &onto
->object
.oid
, orig_head
);
5688 todo_list_release(&new_todo
);
5693 /* Expand the commit IDs */
5694 todo_list_to_strbuf(r
, &new_todo
, &buf2
, -1, 0);
5695 strbuf_swap(&new_todo
.buf
, &buf2
);
5696 strbuf_release(&buf2
);
5697 new_todo
.total_nr
-= new_todo
.nr
;
5698 if (todo_list_parse_insn_buffer(r
, new_todo
.buf
.buf
, &new_todo
) < 0)
5699 BUG("invalid todo list after expanding IDs:\n%s",
5702 if (opts
->allow_ff
&& skip_unnecessary_picks(r
, &new_todo
, &oid
)) {
5703 todo_list_release(&new_todo
);
5704 return error(_("could not skip unnecessary pick commands"));
5707 if (todo_list_write_to_file(r
, &new_todo
, todo_file
, NULL
, NULL
, -1,
5708 flags
& ~(TODO_LIST_SHORTEN_IDS
))) {
5709 todo_list_release(&new_todo
);
5710 return error_errno(_("could not write '%s'"), todo_file
);
5715 if (checkout_onto(r
, opts
, onto_name
, &oid
, orig_head
))
5718 if (require_clean_work_tree(r
, "rebase", "", 1, 1))
5721 todo_list_write_total_nr(&new_todo
);
5722 res
= pick_commits(r
, &new_todo
, opts
);
5725 todo_list_release(&new_todo
);
5730 struct subject2item_entry
{
5731 struct hashmap_entry entry
;
5733 char subject
[FLEX_ARRAY
];
5736 static int subject2item_cmp(const void *fndata
,
5737 const struct hashmap_entry
*eptr
,
5738 const struct hashmap_entry
*entry_or_key
,
5741 const struct subject2item_entry
*a
, *b
;
5743 a
= container_of(eptr
, const struct subject2item_entry
, entry
);
5744 b
= container_of(entry_or_key
, const struct subject2item_entry
, entry
);
5746 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
5749 define_commit_slab(commit_todo_item
, struct todo_item
*);
5751 static int skip_fixupish(const char *subject
, const char **p
) {
5752 return skip_prefix(subject
, "fixup! ", p
) ||
5753 skip_prefix(subject
, "amend! ", p
) ||
5754 skip_prefix(subject
, "squash! ", p
);
5758 * Rearrange the todo list that has both "pick commit-id msg" and "pick
5759 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
5760 * after the former, and change "pick" to "fixup"/"squash".
5762 * Note that if the config has specified a custom instruction format, each log
5763 * message will have to be retrieved from the commit (as the oneline in the
5764 * script cannot be trusted) in order to normalize the autosquash arrangement.
5766 int todo_list_rearrange_squash(struct todo_list
*todo_list
)
5768 struct hashmap subject2item
;
5769 int rearranged
= 0, *next
, *tail
, i
, nr
= 0, alloc
= 0;
5771 struct commit_todo_item commit_todo
;
5772 struct todo_item
*items
= NULL
;
5774 init_commit_todo_item(&commit_todo
);
5776 * The hashmap maps onelines to the respective todo list index.
5778 * If any items need to be rearranged, the next[i] value will indicate
5779 * which item was moved directly after the i'th.
5781 * In that case, last[i] will indicate the index of the latest item to
5782 * be moved to appear after the i'th.
5784 hashmap_init(&subject2item
, subject2item_cmp
, NULL
, todo_list
->nr
);
5785 ALLOC_ARRAY(next
, todo_list
->nr
);
5786 ALLOC_ARRAY(tail
, todo_list
->nr
);
5787 ALLOC_ARRAY(subjects
, todo_list
->nr
);
5788 for (i
= 0; i
< todo_list
->nr
; i
++) {
5789 struct strbuf buf
= STRBUF_INIT
;
5790 struct todo_item
*item
= todo_list
->items
+ i
;
5791 const char *commit_buffer
, *subject
, *p
;
5794 struct subject2item_entry
*entry
;
5796 next
[i
] = tail
[i
] = -1;
5797 if (!item
->commit
|| item
->command
== TODO_DROP
) {
5802 if (is_fixup(item
->command
)) {
5803 clear_commit_todo_item(&commit_todo
);
5804 return error(_("the script was already rearranged."));
5807 *commit_todo_item_at(&commit_todo
, item
->commit
) = item
;
5809 parse_commit(item
->commit
);
5810 commit_buffer
= logmsg_reencode(item
->commit
, NULL
, "UTF-8");
5811 find_commit_subject(commit_buffer
, &subject
);
5812 format_subject(&buf
, subject
, " ");
5813 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
5814 unuse_commit_buffer(item
->commit
, commit_buffer
);
5815 if (skip_fixupish(subject
, &p
)) {
5816 struct commit
*commit2
;
5821 if (!skip_fixupish(p
, &p
))
5825 entry
= hashmap_get_entry_from_hash(&subject2item
,
5827 struct subject2item_entry
,
5830 /* found by title */
5832 else if (!strchr(p
, ' ') &&
5834 lookup_commit_reference_by_name(p
)) &&
5835 *commit_todo_item_at(&commit_todo
, commit2
))
5836 /* found by commit name */
5837 i2
= *commit_todo_item_at(&commit_todo
, commit2
)
5840 /* copy can be a prefix of the commit subject */
5841 for (i2
= 0; i2
< i
; i2
++)
5843 starts_with(subjects
[i2
], p
))
5851 if (starts_with(subject
, "fixup!")) {
5852 todo_list
->items
[i
].command
= TODO_FIXUP
;
5853 } else if (starts_with(subject
, "amend!")) {
5854 todo_list
->items
[i
].command
= TODO_FIXUP
;
5855 todo_list
->items
[i
].flags
= TODO_REPLACE_FIXUP_MSG
;
5857 todo_list
->items
[i
].command
= TODO_SQUASH
;
5863 next
[i
] = next
[tail
[i2
]];
5867 } else if (!hashmap_get_from_hash(&subject2item
,
5868 strhash(subject
), subject
)) {
5869 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
5871 hashmap_entry_init(&entry
->entry
,
5872 strhash(entry
->subject
));
5873 hashmap_put(&subject2item
, &entry
->entry
);
5878 for (i
= 0; i
< todo_list
->nr
; i
++) {
5879 enum todo_command command
= todo_list
->items
[i
].command
;
5883 * Initially, all commands are 'pick's. If it is a
5884 * fixup or a squash now, we have rearranged it.
5886 if (is_fixup(command
))
5890 ALLOC_GROW(items
, nr
+ 1, alloc
);
5891 items
[nr
++] = todo_list
->items
[cur
];
5896 FREE_AND_NULL(todo_list
->items
);
5897 todo_list
->items
= items
;
5899 todo_list
->alloc
= alloc
;
5904 for (i
= 0; i
< todo_list
->nr
; i
++)
5907 hashmap_clear_and_free(&subject2item
, struct subject2item_entry
, entry
);
5909 clear_commit_todo_item(&commit_todo
);
5914 int sequencer_determine_whence(struct repository
*r
, enum commit_whence
*whence
)
5916 if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD")) {
5917 struct object_id cherry_pick_head
, rebase_head
;
5919 if (file_exists(git_path_seq_dir()))
5920 *whence
= FROM_CHERRY_PICK_MULTI
;
5921 if (file_exists(rebase_path()) &&
5922 !get_oid("REBASE_HEAD", &rebase_head
) &&
5923 !get_oid("CHERRY_PICK_HEAD", &cherry_pick_head
) &&
5924 oideq(&rebase_head
, &cherry_pick_head
))
5925 *whence
= FROM_REBASE_PICK
;
5927 *whence
= FROM_CHERRY_PICK_SINGLE
;