5 #include "object-store.h"
10 #include "run-command.h"
13 #include "cache-tree.h"
17 #include "merge-recursive.h"
19 #include "argv-array.h"
23 #include "wt-status.h"
25 #include "notes-utils.h"
27 #include "unpack-trees.h"
31 #include "commit-slab.h"
33 #include "commit-reach.h"
34 #include "rebase-interactive.h"
36 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
38 static const char sign_off_header
[] = "Signed-off-by: ";
39 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
41 GIT_PATH_FUNC(git_path_commit_editmsg
, "COMMIT_EDITMSG")
43 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
45 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
46 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
47 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
48 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
50 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
52 * The file containing rebase commands, comments, and empty lines.
53 * This file is created by "git rebase -i" then edited by the user. As
54 * the lines are processed, they are removed from the front of this
55 * file and written to the tail of 'done'.
57 GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
58 GIT_PATH_FUNC(rebase_path_todo_backup
, "rebase-merge/git-rebase-todo.backup")
61 * The rebase command lines that have already been processed. A line
62 * is moved here when it is first handled, before any associated user
65 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
67 * The file to keep track of how many commands were already processed (e.g.
70 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum")
72 * The file to keep track of how many commands are to be processed in total
73 * (e.g. for the prompt).
75 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end")
77 * The commit message that is planned to be used for any changes that
78 * need to be committed following a user interaction.
80 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
82 * The file into which is accumulated the suggested commit message for
83 * squash/fixup commands. When the first of a series of squash/fixups
84 * is seen, the file is created and the commit message from the
85 * previous commit and from the first squash/fixup commit are written
86 * to it. The commit message for each subsequent squash/fixup commit
87 * is appended to the file as it is processed.
89 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
91 * If the current series of squash/fixups has not yet included a squash
92 * command, then this file exists and holds the commit message of the
93 * original "pick" commit. (If the series ends without a "squash"
94 * command, then this can be used as the commit message of the combined
95 * commit without opening the editor.)
97 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
99 * This file contains the list fixup/squash commands that have been
100 * accumulated into message-fixup or message-squash so far.
102 static GIT_PATH_FUNC(rebase_path_current_fixups
, "rebase-merge/current-fixups")
104 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
105 * GIT_AUTHOR_DATE that will be used for the commit that is currently
108 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
110 * When an "edit" rebase command is being processed, the SHA1 of the
111 * commit to be edited is recorded in this file. When "git rebase
112 * --continue" is executed, if there are any staged changes then they
113 * will be amended to the HEAD commit, but only provided the HEAD
114 * commit is still the commit to be edited. When any other rebase
115 * command is processed, this file is deleted.
117 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
119 * When we stop at a given patch via the "edit" command, this file contains
120 * the abbreviated commit name of the corresponding patch.
122 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
124 * For the post-rewrite hook, we make a list of rewritten commits and
125 * their new sha1s. The rewritten-pending list keeps the sha1s of
126 * commits that have been processed, but not committed yet,
127 * e.g. because they are waiting for a 'squash' command.
129 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
130 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
131 "rebase-merge/rewritten-pending")
134 * The path of the file containing the OID of the "squash onto" commit, i.e.
135 * the dummy commit used for `reset [new root]`.
137 static GIT_PATH_FUNC(rebase_path_squash_onto
, "rebase-merge/squash-onto")
140 * The path of the file listing refs that need to be deleted after the rebase
141 * finishes. This is used by the `label` command to record the need for cleanup.
143 static GIT_PATH_FUNC(rebase_path_refs_to_delete
, "rebase-merge/refs-to-delete")
146 * The following files are written by git-rebase just after parsing the
149 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
150 static GIT_PATH_FUNC(rebase_path_cdate_is_adate
, "rebase-merge/cdate_is_adate")
151 static GIT_PATH_FUNC(rebase_path_ignore_date
, "rebase-merge/ignore_date")
152 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
153 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
154 static GIT_PATH_FUNC(rebase_path_quiet
, "rebase-merge/quiet")
155 static GIT_PATH_FUNC(rebase_path_signoff
, "rebase-merge/signoff")
156 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
157 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
158 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
159 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
160 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
161 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
162 static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec
, "rebase-merge/reschedule-failed-exec")
164 static int git_sequencer_config(const char *k
, const char *v
, void *cb
)
166 struct replay_opts
*opts
= cb
;
169 if (!strcmp(k
, "commit.cleanup")) {
172 status
= git_config_string(&s
, k
, v
);
176 if (!strcmp(s
, "verbatim")) {
177 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
178 opts
->explicit_cleanup
= 1;
179 } else if (!strcmp(s
, "whitespace")) {
180 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
181 opts
->explicit_cleanup
= 1;
182 } else if (!strcmp(s
, "strip")) {
183 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
184 opts
->explicit_cleanup
= 1;
185 } else if (!strcmp(s
, "scissors")) {
186 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SCISSORS
;
187 opts
->explicit_cleanup
= 1;
189 warning(_("invalid commit message cleanup mode '%s'"),
197 if (!strcmp(k
, "commit.gpgsign")) {
198 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
202 status
= git_gpg_config(k
, v
, NULL
);
206 return git_diff_basic_config(k
, v
, NULL
);
209 void sequencer_init_config(struct replay_opts
*opts
)
211 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
212 git_config(git_sequencer_config
, opts
);
215 static inline int is_rebase_i(const struct replay_opts
*opts
)
217 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
220 static const char *get_dir(const struct replay_opts
*opts
)
222 if (is_rebase_i(opts
))
223 return rebase_path();
224 return git_path_seq_dir();
227 static const char *get_todo_path(const struct replay_opts
*opts
)
229 if (is_rebase_i(opts
))
230 return rebase_path_todo();
231 return git_path_todo_file();
235 * Returns 0 for non-conforming footer
236 * Returns 1 for conforming footer
237 * Returns 2 when sob exists within conforming footer
238 * Returns 3 when sob exists within conforming footer as last entry
240 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
241 size_t ignore_footer
)
243 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
244 struct trailer_info info
;
246 int found_sob
= 0, found_sob_last
= 0;
250 trailer_info_get(&info
, sb
->buf
, &opts
);
252 if (info
.trailer_start
== info
.trailer_end
)
255 for (i
= 0; i
< info
.trailer_nr
; i
++)
256 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
258 if (i
== info
.trailer_nr
- 1)
262 trailer_info_release(&info
);
271 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
273 static struct strbuf buf
= STRBUF_INIT
;
277 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
281 int sequencer_remove_state(struct replay_opts
*opts
)
283 struct strbuf buf
= STRBUF_INIT
;
286 if (is_rebase_i(opts
) &&
287 strbuf_read_file(&buf
, rebase_path_refs_to_delete(), 0) > 0) {
290 char *eol
= strchr(p
, '\n');
293 if (delete_ref("(rebase -i) cleanup", p
, NULL
, 0) < 0) {
294 warning(_("could not delete '%s'"), p
);
303 free(opts
->gpg_sign
);
304 free(opts
->strategy
);
305 for (i
= 0; i
< opts
->xopts_nr
; i
++)
306 free(opts
->xopts
[i
]);
308 strbuf_release(&opts
->current_fixups
);
311 strbuf_addstr(&buf
, get_dir(opts
));
312 if (remove_dir_recursively(&buf
, 0))
313 ret
= error(_("could not remove '%s'"), buf
.buf
);
314 strbuf_release(&buf
);
319 static const char *action_name(const struct replay_opts
*opts
)
321 switch (opts
->action
) {
325 return N_("cherry-pick");
326 case REPLAY_INTERACTIVE_REBASE
:
327 return N_("rebase -i");
329 die(_("unknown action: %d"), opts
->action
);
332 struct commit_message
{
339 static const char *short_commit_name(struct commit
*commit
)
341 return find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
);
344 static int get_message(struct commit
*commit
, struct commit_message
*out
)
346 const char *abbrev
, *subject
;
349 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
350 abbrev
= short_commit_name(commit
);
352 subject_len
= find_commit_subject(out
->message
, &subject
);
354 out
->subject
= xmemdupz(subject
, subject_len
);
355 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
356 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
361 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
363 free(msg
->parent_label
);
366 unuse_commit_buffer(commit
, msg
->message
);
369 static void print_advice(struct repository
*r
, int show_hint
,
370 struct replay_opts
*opts
)
372 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
375 fprintf(stderr
, "%s\n", msg
);
377 * A conflict has occurred but the porcelain
378 * (typically rebase --interactive) wants to take care
379 * of the commit itself so remove CHERRY_PICK_HEAD
381 unlink(git_path_cherry_pick_head(r
));
387 advise(_("after resolving the conflicts, mark the corrected paths\n"
388 "with 'git add <paths>' or 'git rm <paths>'"));
390 advise(_("after resolving the conflicts, mark the corrected paths\n"
391 "with 'git add <paths>' or 'git rm <paths>'\n"
392 "and commit the result with 'git commit'"));
396 static int write_message(const void *buf
, size_t len
, const char *filename
,
399 struct lock_file msg_file
= LOCK_INIT
;
401 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
403 return error_errno(_("could not lock '%s'"), filename
);
404 if (write_in_full(msg_fd
, buf
, len
) < 0) {
405 error_errno(_("could not write to '%s'"), filename
);
406 rollback_lock_file(&msg_file
);
409 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
410 error_errno(_("could not write eol to '%s'"), filename
);
411 rollback_lock_file(&msg_file
);
414 if (commit_lock_file(&msg_file
) < 0)
415 return error(_("failed to finalize '%s'"), filename
);
421 * Reads a file that was presumably written by a shell script, i.e. with an
422 * end-of-line marker that needs to be stripped.
424 * Note that only the last end-of-line marker is stripped, consistent with the
425 * behavior of "$(cat path)" in a shell script.
427 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
429 static int read_oneliner(struct strbuf
*buf
,
430 const char *path
, int skip_if_empty
)
432 int orig_len
= buf
->len
;
434 if (!file_exists(path
))
437 if (strbuf_read_file(buf
, path
, 0) < 0) {
438 warning_errno(_("could not read '%s'"), path
);
442 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
443 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
445 buf
->buf
[buf
->len
] = '\0';
448 if (skip_if_empty
&& buf
->len
== orig_len
)
454 static struct tree
*empty_tree(struct repository
*r
)
456 return lookup_tree(r
, the_hash_algo
->empty_tree
);
459 static int error_dirty_index(struct repository
*repo
, struct replay_opts
*opts
)
461 if (repo_read_index_unmerged(repo
))
462 return error_resolve_conflict(_(action_name(opts
)));
464 error(_("your local changes would be overwritten by %s."),
465 _(action_name(opts
)));
467 if (advice_commit_before_merge
)
468 advise(_("commit your changes or stash them to proceed."));
472 static void update_abort_safety_file(void)
474 struct object_id head
;
476 /* Do nothing on a single-pick */
477 if (!file_exists(git_path_seq_dir()))
480 if (!get_oid("HEAD", &head
))
481 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
483 write_file(git_path_abort_safety_file(), "%s", "");
486 static int fast_forward_to(struct repository
*r
,
487 const struct object_id
*to
,
488 const struct object_id
*from
,
490 struct replay_opts
*opts
)
492 struct ref_transaction
*transaction
;
493 struct strbuf sb
= STRBUF_INIT
;
494 struct strbuf err
= STRBUF_INIT
;
497 if (checkout_fast_forward(r
, from
, to
, 1))
498 return -1; /* the callee should have complained already */
500 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
502 transaction
= ref_transaction_begin(&err
);
504 ref_transaction_update(transaction
, "HEAD",
505 to
, unborn
&& !is_rebase_i(opts
) ?
508 ref_transaction_commit(transaction
, &err
)) {
509 ref_transaction_free(transaction
);
510 error("%s", err
.buf
);
512 strbuf_release(&err
);
517 strbuf_release(&err
);
518 ref_transaction_free(transaction
);
519 update_abort_safety_file();
523 enum commit_msg_cleanup_mode
get_cleanup_mode(const char *cleanup_arg
,
526 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
527 return use_editor
? COMMIT_MSG_CLEANUP_ALL
:
528 COMMIT_MSG_CLEANUP_SPACE
;
529 else if (!strcmp(cleanup_arg
, "verbatim"))
530 return COMMIT_MSG_CLEANUP_NONE
;
531 else if (!strcmp(cleanup_arg
, "whitespace"))
532 return COMMIT_MSG_CLEANUP_SPACE
;
533 else if (!strcmp(cleanup_arg
, "strip"))
534 return COMMIT_MSG_CLEANUP_ALL
;
535 else if (!strcmp(cleanup_arg
, "scissors"))
536 return use_editor
? COMMIT_MSG_CLEANUP_SCISSORS
:
537 COMMIT_MSG_CLEANUP_SPACE
;
539 die(_("Invalid cleanup mode %s"), cleanup_arg
);
543 * NB using int rather than enum cleanup_mode to stop clang's
544 * -Wtautological-constant-out-of-range-compare complaining that the comparison
547 static const char *describe_cleanup_mode(int cleanup_mode
)
549 static const char *modes
[] = { "whitespace",
554 if (cleanup_mode
< ARRAY_SIZE(modes
))
555 return modes
[cleanup_mode
];
557 BUG("invalid cleanup_mode provided (%d)", cleanup_mode
);
560 void append_conflicts_hint(struct index_state
*istate
,
561 struct strbuf
*msgbuf
, enum commit_msg_cleanup_mode cleanup_mode
)
565 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
566 strbuf_addch(msgbuf
, '\n');
567 wt_status_append_cut_line(msgbuf
);
568 strbuf_addch(msgbuf
, comment_line_char
);
571 strbuf_addch(msgbuf
, '\n');
572 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
573 for (i
= 0; i
< istate
->cache_nr
;) {
574 const struct cache_entry
*ce
= istate
->cache
[i
++];
576 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
577 while (i
< istate
->cache_nr
&&
578 !strcmp(ce
->name
, istate
->cache
[i
]->name
))
584 static int do_recursive_merge(struct repository
*r
,
585 struct commit
*base
, struct commit
*next
,
586 const char *base_label
, const char *next_label
,
587 struct object_id
*head
, struct strbuf
*msgbuf
,
588 struct replay_opts
*opts
)
590 struct merge_options o
;
591 struct tree
*next_tree
, *base_tree
, *head_tree
;
594 struct lock_file index_lock
= LOCK_INIT
;
596 if (repo_hold_locked_index(r
, &index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
601 init_merge_options(&o
, r
);
602 o
.ancestor
= base
? base_label
: "(empty tree)";
604 o
.branch2
= next
? next_label
: "(empty tree)";
605 if (is_rebase_i(opts
))
607 o
.show_rename_progress
= 1;
609 head_tree
= parse_tree_indirect(head
);
610 next_tree
= next
? get_commit_tree(next
) : empty_tree(r
);
611 base_tree
= base
? get_commit_tree(base
) : empty_tree(r
);
613 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
614 parse_merge_opt(&o
, *xopt
);
616 clean
= merge_trees(&o
,
618 next_tree
, base_tree
);
619 if (is_rebase_i(opts
) && clean
<= 0)
620 fputs(o
.obuf
.buf
, stdout
);
621 strbuf_release(&o
.obuf
);
623 rollback_lock_file(&index_lock
);
627 if (write_locked_index(r
->index
, &index_lock
,
628 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
630 * TRANSLATORS: %s will be "revert", "cherry-pick" or
633 return error(_("%s: Unable to write new index file"),
634 _(action_name(opts
)));
637 append_conflicts_hint(r
->index
, msgbuf
,
638 opts
->default_msg_cleanup
);
643 static struct object_id
*get_cache_tree_oid(struct index_state
*istate
)
645 if (!istate
->cache_tree
)
646 istate
->cache_tree
= cache_tree();
648 if (!cache_tree_fully_valid(istate
->cache_tree
))
649 if (cache_tree_update(istate
, 0)) {
650 error(_("unable to update cache tree"));
654 return &istate
->cache_tree
->oid
;
657 static int is_index_unchanged(struct repository
*r
)
659 struct object_id head_oid
, *cache_tree_oid
;
660 struct commit
*head_commit
;
661 struct index_state
*istate
= r
->index
;
663 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
664 return error(_("could not resolve HEAD commit"));
666 head_commit
= lookup_commit(r
, &head_oid
);
669 * If head_commit is NULL, check_commit, called from
670 * lookup_commit, would have indicated that head_commit is not
671 * a commit object already. parse_commit() will return failure
672 * without further complaints in such a case. Otherwise, if
673 * the commit is invalid, parse_commit() will complain. So
674 * there is nothing for us to say here. Just return failure.
676 if (parse_commit(head_commit
))
679 if (!(cache_tree_oid
= get_cache_tree_oid(istate
)))
682 return oideq(cache_tree_oid
, get_commit_tree_oid(head_commit
));
685 static int write_author_script(const char *message
)
687 struct strbuf buf
= STRBUF_INIT
;
692 if (!*message
|| starts_with(message
, "\n")) {
694 /* Missing 'author' line? */
695 unlink(rebase_path_author_script());
697 } else if (skip_prefix(message
, "author ", &message
))
699 else if ((eol
= strchr(message
, '\n')))
704 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
705 while (*message
&& *message
!= '\n' && *message
!= '\r')
706 if (skip_prefix(message
, " <", &message
))
708 else if (*message
!= '\'')
709 strbuf_addch(&buf
, *(message
++));
711 strbuf_addf(&buf
, "'\\%c'", *(message
++));
712 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
713 while (*message
&& *message
!= '\n' && *message
!= '\r')
714 if (skip_prefix(message
, "> ", &message
))
716 else if (*message
!= '\'')
717 strbuf_addch(&buf
, *(message
++));
719 strbuf_addf(&buf
, "'\\%c'", *(message
++));
720 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
721 while (*message
&& *message
!= '\n' && *message
!= '\r')
722 if (*message
!= '\'')
723 strbuf_addch(&buf
, *(message
++));
725 strbuf_addf(&buf
, "'\\%c'", *(message
++));
726 strbuf_addch(&buf
, '\'');
727 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
728 strbuf_release(&buf
);
733 * Take a series of KEY='VALUE' lines where VALUE part is
734 * sq-quoted, and append <KEY, VALUE> at the end of the string list
736 static int parse_key_value_squoted(char *buf
, struct string_list
*list
)
739 struct string_list_item
*item
;
741 char *cp
= strchr(buf
, '=');
743 np
= strchrnul(buf
, '\n');
744 return error(_("no key present in '%.*s'"),
745 (int) (np
- buf
), buf
);
747 np
= strchrnul(cp
, '\n');
749 item
= string_list_append(list
, buf
);
751 buf
= np
+ (*np
== '\n');
755 return error(_("unable to dequote value of '%s'"),
757 item
->util
= xstrdup(cp
);
763 * Reads and parses the state directory's "author-script" file, and sets name,
764 * email and date accordingly.
765 * Returns 0 on success, -1 if the file could not be parsed.
767 * The author script is of the format:
769 * GIT_AUTHOR_NAME='$author_name'
770 * GIT_AUTHOR_EMAIL='$author_email'
771 * GIT_AUTHOR_DATE='$author_date'
773 * where $author_name, $author_email and $author_date are quoted. We are strict
774 * with our parsing, as the file was meant to be eval'd in the now-removed
775 * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
776 * from what this function expects, it is better to bail out than to do
777 * something that the user does not expect.
779 int read_author_script(const char *path
, char **name
, char **email
, char **date
,
782 struct strbuf buf
= STRBUF_INIT
;
783 struct string_list kv
= STRING_LIST_INIT_DUP
;
784 int retval
= -1; /* assume failure */
785 int i
, name_i
= -2, email_i
= -2, date_i
= -2, err
= 0;
787 if (strbuf_read_file(&buf
, path
, 256) <= 0) {
788 strbuf_release(&buf
);
789 if (errno
== ENOENT
&& allow_missing
)
792 return error_errno(_("could not open '%s' for reading"),
796 if (parse_key_value_squoted(buf
.buf
, &kv
))
799 for (i
= 0; i
< kv
.nr
; i
++) {
800 if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_NAME")) {
802 name_i
= error(_("'GIT_AUTHOR_NAME' already given"));
805 } else if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_EMAIL")) {
807 email_i
= error(_("'GIT_AUTHOR_EMAIL' already given"));
810 } else if (!strcmp(kv
.items
[i
].string
, "GIT_AUTHOR_DATE")) {
812 date_i
= error(_("'GIT_AUTHOR_DATE' already given"));
816 err
= error(_("unknown variable '%s'"),
821 error(_("missing 'GIT_AUTHOR_NAME'"));
823 error(_("missing 'GIT_AUTHOR_EMAIL'"));
825 error(_("missing 'GIT_AUTHOR_DATE'"));
826 if (date_i
< 0 || email_i
< 0 || date_i
< 0 || err
)
830 *name
= kv
.items
[name_i
].util
;
832 free(kv
.items
[name_i
].util
);
834 *email
= kv
.items
[email_i
].util
;
836 free(kv
.items
[email_i
].util
);
838 *date
= kv
.items
[date_i
].util
;
840 free(kv
.items
[date_i
].util
);
843 string_list_clear(&kv
, !!retval
);
844 strbuf_release(&buf
);
849 * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
850 * file with shell quoting into struct argv_array. Returns -1 on
851 * error, 0 otherwise.
853 static int read_env_script(struct argv_array
*env
)
855 char *name
, *email
, *date
;
857 if (read_author_script(rebase_path_author_script(),
858 &name
, &email
, &date
, 0))
861 argv_array_pushf(env
, "GIT_AUTHOR_NAME=%s", name
);
862 argv_array_pushf(env
, "GIT_AUTHOR_EMAIL=%s", email
);
863 argv_array_pushf(env
, "GIT_AUTHOR_DATE=%s", date
);
871 static char *get_author(const char *message
)
876 a
= find_commit_header(message
, "author", &len
);
878 return xmemdupz(a
, len
);
883 /* Returns a "date" string that needs to be free()'d by the caller */
884 static char *read_author_date_or_null(void)
888 if (read_author_script(rebase_path_author_script(),
889 NULL
, NULL
, &date
, 0))
894 /* Construct a free()able author string with current time as the author date */
895 static char *ignore_author_date(const char *author
)
897 int len
= strlen(author
);
898 struct ident_split ident
;
899 struct strbuf new_author
= STRBUF_INIT
;
901 if (split_ident_line(&ident
, author
, len
) < 0) {
902 error(_("malformed ident line"));
905 len
= ident
.mail_end
- ident
.name_begin
+ 1;
907 strbuf_addf(&new_author
, "%.*s ", len
, ident
.name_begin
);
908 datestamp(&new_author
);
909 return strbuf_detach(&new_author
, NULL
);
912 static void push_dates(struct child_process
*child
, int change_committer_date
)
914 time_t now
= time(NULL
);
915 struct strbuf date
= STRBUF_INIT
;
917 strbuf_addf(&date
, "@%"PRIuMAX
, (uintmax_t)now
);
918 argv_array_pushf(&child
->env_array
, "GIT_AUTHOR_DATE=%s", date
.buf
);
919 if (change_committer_date
)
920 argv_array_pushf(&child
->env_array
, "GIT_COMMITTER_DATE=%s", date
.buf
);
921 strbuf_release(&date
);
924 static const char staged_changes_advice
[] =
925 N_("you have staged changes in your working tree\n"
926 "If these changes are meant to be squashed into the previous commit, run:\n"
928 " git commit --amend %s\n"
930 "If they are meant to go into a new commit, run:\n"
934 "In both cases, once you're done, continue with:\n"
936 " git rebase --continue\n");
938 #define ALLOW_EMPTY (1<<0)
939 #define EDIT_MSG (1<<1)
940 #define AMEND_MSG (1<<2)
941 #define CLEANUP_MSG (1<<3)
942 #define VERIFY_MSG (1<<4)
943 #define CREATE_ROOT_COMMIT (1<<5)
945 static int run_command_silent_on_success(struct child_process
*cmd
)
947 struct strbuf buf
= STRBUF_INIT
;
950 cmd
->stdout_to_stderr
= 1;
951 rc
= pipe_command(cmd
,
957 fputs(buf
.buf
, stderr
);
958 strbuf_release(&buf
);
963 * If we are cherry-pick, and if the merge did not result in
964 * hand-editing, we will hit this commit and inherit the original
965 * author date and name.
967 * If we are revert, or if our cherry-pick results in a hand merge,
968 * we had better say that the current user is responsible for that.
970 * An exception is when run_git_commit() is called during an
971 * interactive rebase: in that case, we will want to retain the
974 static int run_git_commit(struct repository
*r
,
976 struct replay_opts
*opts
,
979 struct child_process cmd
= CHILD_PROCESS_INIT
;
983 if (opts
->committer_date_is_author_date
) {
985 struct strbuf datebuf
= STRBUF_INIT
;
986 char *date
= read_author_date_or_null();
991 strbuf_addf(&datebuf
, "@%s", date
);
992 res
= setenv("GIT_COMMITTER_DATE",
993 opts
->ignore_date
? "" : datebuf
.buf
, 1);
995 strbuf_release(&datebuf
);
1002 if (is_rebase_i(opts
) && read_env_script(&cmd
.env_array
)) {
1003 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
1005 return error(_(staged_changes_advice
),
1009 argv_array_push(&cmd
.args
, "commit");
1011 if (!(flags
& VERIFY_MSG
))
1012 argv_array_push(&cmd
.args
, "-n");
1013 if ((flags
& AMEND_MSG
))
1014 argv_array_push(&cmd
.args
, "--amend");
1016 argv_array_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
1017 if (opts
->ignore_date
)
1018 push_dates(&cmd
, opts
->committer_date_is_author_date
);
1020 argv_array_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
1021 else if (!(flags
& EDIT_MSG
))
1022 argv_array_pushl(&cmd
.args
, "-C", "HEAD", NULL
);
1023 if ((flags
& CLEANUP_MSG
))
1024 argv_array_push(&cmd
.args
, "--cleanup=strip");
1025 if ((flags
& EDIT_MSG
))
1026 argv_array_push(&cmd
.args
, "-e");
1027 else if (!(flags
& CLEANUP_MSG
) &&
1028 !opts
->signoff
&& !opts
->record_origin
&&
1029 !opts
->explicit_cleanup
)
1030 argv_array_push(&cmd
.args
, "--cleanup=verbatim");
1032 if ((flags
& ALLOW_EMPTY
))
1033 argv_array_push(&cmd
.args
, "--allow-empty");
1035 if (!(flags
& EDIT_MSG
))
1036 argv_array_push(&cmd
.args
, "--allow-empty-message");
1038 if (is_rebase_i(opts
) && !(flags
& EDIT_MSG
))
1039 return run_command_silent_on_success(&cmd
);
1041 return run_command(&cmd
);
1044 static int rest_is_empty(const struct strbuf
*sb
, int start
)
1049 /* Check if the rest is just whitespace and Signed-off-by's. */
1050 for (i
= start
; i
< sb
->len
; i
++) {
1051 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
1057 if (strlen(sign_off_header
) <= eol
- i
&&
1058 starts_with(sb
->buf
+ i
, sign_off_header
)) {
1063 if (!isspace(sb
->buf
[i
++]))
1070 void cleanup_message(struct strbuf
*msgbuf
,
1071 enum commit_msg_cleanup_mode cleanup_mode
, int verbose
)
1073 if (verbose
|| /* Truncate the message just before the diff, if any. */
1074 cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
1075 strbuf_setlen(msgbuf
, wt_status_locate_end(msgbuf
->buf
, msgbuf
->len
));
1076 if (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
)
1077 strbuf_stripspace(msgbuf
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
1081 * Find out if the message in the strbuf contains only whitespace and
1082 * Signed-off-by lines.
1084 int message_is_empty(const struct strbuf
*sb
,
1085 enum commit_msg_cleanup_mode cleanup_mode
)
1087 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
1089 return rest_is_empty(sb
, 0);
1093 * See if the user edited the message in the editor or left what
1094 * was in the template intact
1096 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
1097 enum commit_msg_cleanup_mode cleanup_mode
)
1099 struct strbuf tmpl
= STRBUF_INIT
;
1102 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
1105 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
1108 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
1109 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
1111 strbuf_release(&tmpl
);
1112 return rest_is_empty(sb
, start
- sb
->buf
);
1115 int update_head_with_reflog(const struct commit
*old_head
,
1116 const struct object_id
*new_head
,
1117 const char *action
, const struct strbuf
*msg
,
1120 struct ref_transaction
*transaction
;
1121 struct strbuf sb
= STRBUF_INIT
;
1126 strbuf_addstr(&sb
, action
);
1127 strbuf_addstr(&sb
, ": ");
1130 nl
= strchr(msg
->buf
, '\n');
1132 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
1134 strbuf_addbuf(&sb
, msg
);
1135 strbuf_addch(&sb
, '\n');
1138 transaction
= ref_transaction_begin(err
);
1140 ref_transaction_update(transaction
, "HEAD", new_head
,
1141 old_head
? &old_head
->object
.oid
: &null_oid
,
1143 ref_transaction_commit(transaction
, err
)) {
1146 ref_transaction_free(transaction
);
1147 strbuf_release(&sb
);
1152 static int run_rewrite_hook(const struct object_id
*oldoid
,
1153 const struct object_id
*newoid
)
1155 struct child_process proc
= CHILD_PROCESS_INIT
;
1156 const char *argv
[3];
1158 struct strbuf sb
= STRBUF_INIT
;
1160 argv
[0] = find_hook("post-rewrite");
1169 proc
.stdout_to_stderr
= 1;
1170 proc
.trace2_hook_name
= "post-rewrite";
1172 code
= start_command(&proc
);
1175 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
1176 sigchain_push(SIGPIPE
, SIG_IGN
);
1177 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
1179 strbuf_release(&sb
);
1180 sigchain_pop(SIGPIPE
);
1181 return finish_command(&proc
);
1184 void commit_post_rewrite(struct repository
*r
,
1185 const struct commit
*old_head
,
1186 const struct object_id
*new_head
)
1188 struct notes_rewrite_cfg
*cfg
;
1190 cfg
= init_copy_notes_for_rewrite("amend");
1192 /* we are amending, so old_head is not NULL */
1193 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
1194 finish_copy_notes_for_rewrite(r
, cfg
, "Notes added by 'git commit --amend'");
1196 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
1199 static int run_prepare_commit_msg_hook(struct repository
*r
,
1204 const char *name
, *arg1
= NULL
, *arg2
= NULL
;
1206 name
= git_path_commit_editmsg();
1207 if (write_message(msg
->buf
, msg
->len
, name
, 0))
1216 if (run_commit_hook(0, r
->index_file
, "prepare-commit-msg", name
,
1218 ret
= error(_("'prepare-commit-msg' hook failed"));
1223 static const char implicit_ident_advice_noconfig
[] =
1224 N_("Your name and email address were configured automatically based\n"
1225 "on your username and hostname. Please check that they are accurate.\n"
1226 "You can suppress this message by setting them explicitly. Run the\n"
1227 "following command and follow the instructions in your editor to edit\n"
1228 "your configuration file:\n"
1230 " git config --global --edit\n"
1232 "After doing this, you may fix the identity used for this commit with:\n"
1234 " git commit --amend --reset-author\n");
1236 static const char implicit_ident_advice_config
[] =
1237 N_("Your name and email address were configured automatically based\n"
1238 "on your username and hostname. Please check that they are accurate.\n"
1239 "You can suppress this message by setting them explicitly:\n"
1241 " git config --global user.name \"Your Name\"\n"
1242 " git config --global user.email you@example.com\n"
1244 "After doing this, you may fix the identity used for this commit with:\n"
1246 " git commit --amend --reset-author\n");
1248 static const char *implicit_ident_advice(void)
1250 char *user_config
= expand_user_path("~/.gitconfig", 0);
1251 char *xdg_config
= xdg_config_home("config");
1252 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
1258 return _(implicit_ident_advice_config
);
1260 return _(implicit_ident_advice_noconfig
);
1264 void print_commit_summary(struct repository
*r
,
1266 const struct object_id
*oid
,
1269 struct rev_info rev
;
1270 struct commit
*commit
;
1271 struct strbuf format
= STRBUF_INIT
;
1273 struct pretty_print_context pctx
= {0};
1274 struct strbuf author_ident
= STRBUF_INIT
;
1275 struct strbuf committer_ident
= STRBUF_INIT
;
1277 commit
= lookup_commit(r
, oid
);
1279 die(_("couldn't look up newly created commit"));
1280 if (parse_commit(commit
))
1281 die(_("could not parse newly created commit"));
1283 strbuf_addstr(&format
, "format:%h] %s");
1285 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1286 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1287 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1288 strbuf_addstr(&format
, "\n Author: ");
1289 strbuf_addbuf_percentquote(&format
, &author_ident
);
1291 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
1292 struct strbuf date
= STRBUF_INIT
;
1294 format_commit_message(commit
, "%ad", &date
, &pctx
);
1295 strbuf_addstr(&format
, "\n Date: ");
1296 strbuf_addbuf_percentquote(&format
, &date
);
1297 strbuf_release(&date
);
1299 if (!committer_ident_sufficiently_given()) {
1300 strbuf_addstr(&format
, "\n Committer: ");
1301 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1302 if (advice_implicit_identity
) {
1303 strbuf_addch(&format
, '\n');
1304 strbuf_addstr(&format
, implicit_ident_advice());
1307 strbuf_release(&author_ident
);
1308 strbuf_release(&committer_ident
);
1310 repo_init_revisions(r
, &rev
, prefix
);
1311 setup_revisions(0, NULL
, &rev
, NULL
);
1314 rev
.diffopt
.output_format
=
1315 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1317 rev
.verbose_header
= 1;
1318 rev
.show_root_diff
= 1;
1319 get_commit_format(format
.buf
, &rev
);
1320 rev
.always_show_header
= 0;
1321 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1322 rev
.diffopt
.break_opt
= 0;
1323 diff_setup_done(&rev
.diffopt
);
1325 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1327 die_errno(_("unable to resolve HEAD after creating commit"));
1328 if (!strcmp(head
, "HEAD"))
1329 head
= _("detached HEAD");
1331 skip_prefix(head
, "refs/heads/", &head
);
1332 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1333 _(" (root-commit)") : "");
1335 if (!log_tree_commit(&rev
, commit
)) {
1336 rev
.always_show_header
= 1;
1337 rev
.use_terminator
= 1;
1338 log_tree_commit(&rev
, commit
);
1341 strbuf_release(&format
);
1344 static int parse_head(struct repository
*r
, struct commit
**head
)
1346 struct commit
*current_head
;
1347 struct object_id oid
;
1349 if (get_oid("HEAD", &oid
)) {
1350 current_head
= NULL
;
1352 current_head
= lookup_commit_reference(r
, &oid
);
1354 return error(_("could not parse HEAD"));
1355 if (!oideq(&oid
, ¤t_head
->object
.oid
)) {
1356 warning(_("HEAD %s is not a commit!"),
1359 if (parse_commit(current_head
))
1360 return error(_("could not parse HEAD commit"));
1362 *head
= current_head
;
1368 * Try to commit without forking 'git commit'. In some cases we need
1369 * to run 'git commit' to display an error message
1372 * -1 - error unable to commit
1374 * 1 - run 'git commit'
1376 static int try_to_commit(struct repository
*r
,
1377 struct strbuf
*msg
, const char *author
,
1378 struct replay_opts
*opts
, unsigned int flags
,
1379 struct object_id
*oid
)
1381 struct object_id tree
;
1382 struct commit
*current_head
= NULL
;
1383 struct commit_list
*parents
= NULL
;
1384 struct commit_extra_header
*extra
= NULL
;
1385 struct strbuf err
= STRBUF_INIT
;
1386 struct strbuf commit_msg
= STRBUF_INIT
;
1387 char *author_to_free
= NULL
;
1388 const char *hook_commit
= NULL
;
1389 enum commit_msg_cleanup_mode cleanup
;
1392 if (parse_head(r
, ¤t_head
))
1394 if (flags
& AMEND_MSG
) {
1395 const char *exclude_gpgsig
[] = { "gpgsig", NULL
};
1396 const char *out_enc
= get_commit_output_encoding();
1397 const char *message
= logmsg_reencode(current_head
, NULL
,
1401 const char *orig_message
= NULL
;
1403 find_commit_subject(message
, &orig_message
);
1405 strbuf_addstr(msg
, orig_message
);
1406 hook_commit
= "HEAD";
1408 author
= author_to_free
= get_author(message
);
1409 unuse_commit_buffer(current_head
, message
);
1411 res
= error(_("unable to parse commit author"));
1414 parents
= copy_commit_list(current_head
->parents
);
1415 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1416 } else if (current_head
&&
1417 (!(flags
& CREATE_ROOT_COMMIT
) || (flags
& AMEND_MSG
))) {
1418 commit_list_insert(current_head
, &parents
);
1421 if (opts
->committer_date_is_author_date
) {
1422 int len
= strlen(author
);
1423 struct ident_split ident
;
1424 struct strbuf date
= STRBUF_INIT
;
1426 if (split_ident_line(&ident
, author
, len
) < 0) {
1427 res
= error(_("malformed ident line"));
1430 if (!ident
.date_begin
) {
1431 res
= error(_("corrupted author without date information"));
1435 strbuf_addf(&date
, "@%.*s %.*s",
1436 (int)(ident
.date_end
- ident
.date_begin
), ident
.date_begin
,
1437 (int)(ident
.tz_end
- ident
.tz_begin
), ident
.tz_begin
);
1438 res
= setenv("GIT_COMMITTER_DATE",
1439 opts
->ignore_date
? "" : date
.buf
, 1);
1440 strbuf_release(&date
);
1446 if (write_index_as_tree(&tree
, r
->index
, r
->index_file
, 0, NULL
)) {
1447 res
= error(_("git write-tree failed to write a tree"));
1451 if (!(flags
& ALLOW_EMPTY
)) {
1452 struct commit
*first_parent
= current_head
;
1454 if (flags
& AMEND_MSG
) {
1455 if (current_head
->parents
) {
1456 first_parent
= current_head
->parents
->item
;
1457 if (repo_parse_commit(r
, first_parent
)) {
1458 res
= error(_("could not parse HEAD commit"));
1462 first_parent
= NULL
;
1465 if (oideq(first_parent
1466 ? get_commit_tree_oid(first_parent
)
1467 : the_hash_algo
->empty_tree
,
1469 res
= 1; /* run 'git commit' to display error message */
1474 if (find_hook("prepare-commit-msg")) {
1475 res
= run_prepare_commit_msg_hook(r
, msg
, hook_commit
);
1478 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1480 res
= error_errno(_("unable to read commit message "
1482 git_path_commit_editmsg());
1488 if (flags
& CLEANUP_MSG
)
1489 cleanup
= COMMIT_MSG_CLEANUP_ALL
;
1490 else if ((opts
->signoff
|| opts
->record_origin
) &&
1491 !opts
->explicit_cleanup
)
1492 cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
1494 cleanup
= opts
->default_msg_cleanup
;
1496 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1497 strbuf_stripspace(msg
, cleanup
== COMMIT_MSG_CLEANUP_ALL
);
1498 if ((flags
& EDIT_MSG
) && message_is_empty(msg
, cleanup
)) {
1499 res
= 1; /* run 'git commit' to display error message */
1505 if (opts
->ignore_date
) {
1506 author
= ignore_author_date(author
);
1511 free(author_to_free
);
1512 author_to_free
= (char *)author
;
1514 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
,
1515 oid
, author
, opts
->gpg_sign
, extra
)) {
1516 res
= error(_("failed to write commit object"));
1520 if (update_head_with_reflog(current_head
, oid
,
1521 getenv("GIT_REFLOG_ACTION"), msg
, &err
)) {
1522 res
= error("%s", err
.buf
);
1526 run_commit_hook(0, r
->index_file
, "post-commit", NULL
);
1527 if (flags
& AMEND_MSG
)
1528 commit_post_rewrite(r
, current_head
, oid
);
1531 free_commit_extra_headers(extra
);
1532 strbuf_release(&err
);
1533 strbuf_release(&commit_msg
);
1534 free(author_to_free
);
1539 static int do_commit(struct repository
*r
,
1540 const char *msg_file
, const char *author
,
1541 struct replay_opts
*opts
, unsigned int flags
)
1545 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
)) {
1546 struct object_id oid
;
1547 struct strbuf sb
= STRBUF_INIT
;
1549 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1550 return error_errno(_("unable to read commit message "
1554 res
= try_to_commit(r
, msg_file
? &sb
: NULL
,
1555 author
, opts
, flags
, &oid
);
1556 strbuf_release(&sb
);
1558 unlink(git_path_cherry_pick_head(r
));
1559 unlink(git_path_merge_msg(r
));
1560 if (!is_rebase_i(opts
))
1561 print_commit_summary(r
, NULL
, &oid
,
1562 SUMMARY_SHOW_AUTHOR_DATE
);
1567 return run_git_commit(r
, msg_file
, opts
, flags
);
1572 static int is_original_commit_empty(struct commit
*commit
)
1574 const struct object_id
*ptree_oid
;
1576 if (parse_commit(commit
))
1577 return error(_("could not parse commit %s"),
1578 oid_to_hex(&commit
->object
.oid
));
1579 if (commit
->parents
) {
1580 struct commit
*parent
= commit
->parents
->item
;
1581 if (parse_commit(parent
))
1582 return error(_("could not parse parent commit %s"),
1583 oid_to_hex(&parent
->object
.oid
));
1584 ptree_oid
= get_commit_tree_oid(parent
);
1586 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1589 return oideq(ptree_oid
, get_commit_tree_oid(commit
));
1593 * Do we run "git commit" with "--allow-empty"?
1595 static int allow_empty(struct repository
*r
,
1596 struct replay_opts
*opts
,
1597 struct commit
*commit
)
1599 int index_unchanged
, empty_commit
;
1604 * (1) we do not allow empty at all and error out.
1606 * (2) we allow ones that were initially empty, but
1607 * forbid the ones that become empty;
1609 * (3) we allow both.
1611 if (!opts
->allow_empty
)
1612 return 0; /* let "git commit" barf as necessary */
1614 index_unchanged
= is_index_unchanged(r
);
1615 if (index_unchanged
< 0)
1616 return index_unchanged
;
1617 if (!index_unchanged
)
1618 return 0; /* we do not have to say --allow-empty */
1620 if (opts
->keep_redundant_commits
)
1623 empty_commit
= is_original_commit_empty(commit
);
1624 if (empty_commit
< 0)
1625 return empty_commit
;
1635 } todo_command_info
[] = {
1652 static const char *command_to_string(const enum todo_command command
)
1654 if (command
< TODO_COMMENT
)
1655 return todo_command_info
[command
].str
;
1656 die(_("unknown command: %d"), command
);
1659 static char command_to_char(const enum todo_command command
)
1661 if (command
< TODO_COMMENT
&& todo_command_info
[command
].c
)
1662 return todo_command_info
[command
].c
;
1663 return comment_line_char
;
1666 static int is_noop(const enum todo_command command
)
1668 return TODO_NOOP
<= command
;
1671 static int is_fixup(enum todo_command command
)
1673 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1676 /* Does this command create a (non-merge) commit? */
1677 static int is_pick_or_similar(enum todo_command command
)
1692 static int update_squash_messages(struct repository
*r
,
1693 enum todo_command command
,
1694 struct commit
*commit
,
1695 struct replay_opts
*opts
)
1697 struct strbuf buf
= STRBUF_INIT
;
1699 const char *message
, *body
;
1700 const char *encoding
= get_commit_output_encoding();
1702 if (opts
->current_fixup_count
> 0) {
1703 struct strbuf header
= STRBUF_INIT
;
1706 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 9) <= 0)
1707 return error(_("could not read '%s'"),
1708 rebase_path_squash_msg());
1710 eol
= buf
.buf
[0] != comment_line_char
?
1711 buf
.buf
: strchrnul(buf
.buf
, '\n');
1713 strbuf_addf(&header
, "%c ", comment_line_char
);
1714 strbuf_addf(&header
, _("This is a combination of %d commits."),
1715 opts
->current_fixup_count
+ 2);
1716 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1717 strbuf_release(&header
);
1719 struct object_id head
;
1720 struct commit
*head_commit
;
1721 const char *head_message
, *body
;
1723 if (get_oid("HEAD", &head
))
1724 return error(_("need a HEAD to fixup"));
1725 if (!(head_commit
= lookup_commit_reference(r
, &head
)))
1726 return error(_("could not read HEAD"));
1727 if (!(head_message
= logmsg_reencode(head_commit
, NULL
, encoding
)))
1728 return error(_("could not read HEAD's commit message"));
1730 find_commit_subject(head_message
, &body
);
1731 if (write_message(body
, strlen(body
),
1732 rebase_path_fixup_msg(), 0)) {
1733 unuse_commit_buffer(head_commit
, head_message
);
1734 return error(_("cannot write '%s'"),
1735 rebase_path_fixup_msg());
1738 strbuf_addf(&buf
, "%c ", comment_line_char
);
1739 strbuf_addf(&buf
, _("This is a combination of %d commits."), 2);
1740 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1741 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
1742 strbuf_addstr(&buf
, "\n\n");
1743 strbuf_addstr(&buf
, body
);
1745 unuse_commit_buffer(head_commit
, head_message
);
1748 if (!(message
= logmsg_reencode(commit
, NULL
, encoding
)))
1749 return error(_("could not read commit message of %s"),
1750 oid_to_hex(&commit
->object
.oid
));
1751 find_commit_subject(message
, &body
);
1753 if (command
== TODO_SQUASH
) {
1754 unlink(rebase_path_fixup_msg());
1755 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1756 strbuf_addf(&buf
, _("This is the commit message #%d:"),
1757 ++opts
->current_fixup_count
+ 1);
1758 strbuf_addstr(&buf
, "\n\n");
1759 strbuf_addstr(&buf
, body
);
1760 } else if (command
== TODO_FIXUP
) {
1761 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1762 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
1763 ++opts
->current_fixup_count
+ 1);
1764 strbuf_addstr(&buf
, "\n\n");
1765 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1767 return error(_("unknown command: %d"), command
);
1768 unuse_commit_buffer(commit
, message
);
1770 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
1771 strbuf_release(&buf
);
1774 strbuf_addf(&opts
->current_fixups
, "%s%s %s",
1775 opts
->current_fixups
.len
? "\n" : "",
1776 command_to_string(command
),
1777 oid_to_hex(&commit
->object
.oid
));
1778 res
= write_message(opts
->current_fixups
.buf
,
1779 opts
->current_fixups
.len
,
1780 rebase_path_current_fixups(), 0);
1786 static void flush_rewritten_pending(void)
1788 struct strbuf buf
= STRBUF_INIT
;
1789 struct object_id newoid
;
1792 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
1793 !get_oid("HEAD", &newoid
) &&
1794 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
1795 char *bol
= buf
.buf
, *eol
;
1798 eol
= strchrnul(bol
, '\n');
1799 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
1800 bol
, oid_to_hex(&newoid
));
1806 unlink(rebase_path_rewritten_pending());
1808 strbuf_release(&buf
);
1811 static void record_in_rewritten(struct object_id
*oid
,
1812 enum todo_command next_command
)
1814 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
1819 fprintf(out
, "%s\n", oid_to_hex(oid
));
1822 if (!is_fixup(next_command
))
1823 flush_rewritten_pending();
1826 static int do_pick_commit(struct repository
*r
,
1827 enum todo_command command
,
1828 struct commit
*commit
,
1829 struct replay_opts
*opts
,
1830 int final_fixup
, int *check_todo
)
1832 unsigned int flags
= opts
->edit
? EDIT_MSG
: 0;
1833 const char *msg_file
= opts
->edit
? NULL
: git_path_merge_msg(r
);
1834 struct object_id head
;
1835 struct commit
*base
, *next
, *parent
;
1836 const char *base_label
, *next_label
;
1837 char *author
= NULL
;
1838 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
1839 struct strbuf msgbuf
= STRBUF_INIT
;
1840 int res
, unborn
= 0, reword
= 0, allow
;
1842 if (opts
->no_commit
) {
1844 * We do not intend to commit immediately. We just want to
1845 * merge the differences in, so let's compute the tree
1846 * that represents the "current" state for merge-recursive
1849 if (write_index_as_tree(&head
, r
->index
, r
->index_file
, 0, NULL
))
1850 return error(_("your index file is unmerged."));
1852 unborn
= get_oid("HEAD", &head
);
1853 /* Do we want to generate a root commit? */
1854 if (is_pick_or_similar(command
) && opts
->have_squash_onto
&&
1855 oideq(&head
, &opts
->squash_onto
)) {
1856 if (is_fixup(command
))
1857 return error(_("cannot fixup root commit"));
1858 flags
|= CREATE_ROOT_COMMIT
;
1861 oidcpy(&head
, the_hash_algo
->empty_tree
);
1862 if (index_differs_from(r
, unborn
? empty_tree_oid_hex() : "HEAD",
1864 return error_dirty_index(r
, opts
);
1866 discard_index(r
->index
);
1868 if (!commit
->parents
)
1870 else if (commit
->parents
->next
) {
1871 /* Reverting or cherry-picking a merge commit */
1873 struct commit_list
*p
;
1875 if (!opts
->mainline
)
1876 return error(_("commit %s is a merge but no -m option was given."),
1877 oid_to_hex(&commit
->object
.oid
));
1879 for (cnt
= 1, p
= commit
->parents
;
1880 cnt
!= opts
->mainline
&& p
;
1883 if (cnt
!= opts
->mainline
|| !p
)
1884 return error(_("commit %s does not have parent %d"),
1885 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1887 } else if (1 < opts
->mainline
)
1889 * Non-first parent explicitly specified as mainline for
1892 return error(_("commit %s does not have parent %d"),
1893 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1895 parent
= commit
->parents
->item
;
1897 if (get_message(commit
, &msg
) != 0)
1898 return error(_("cannot get commit message for %s"),
1899 oid_to_hex(&commit
->object
.oid
));
1901 if (opts
->allow_ff
&& !is_fixup(command
) &&
1902 ((parent
&& oideq(&parent
->object
.oid
, &head
)) ||
1903 (!parent
&& unborn
))) {
1904 if (is_rebase_i(opts
))
1905 write_author_script(msg
.message
);
1906 res
= fast_forward_to(r
, &commit
->object
.oid
, &head
, unborn
,
1908 if (res
|| command
!= TODO_REWORD
)
1912 goto fast_forward_edit
;
1914 if (parent
&& parse_commit(parent
) < 0)
1915 /* TRANSLATORS: The first %s will be a "todo" command like
1916 "revert" or "pick", the second %s a SHA1. */
1917 return error(_("%s: cannot parse parent commit %s"),
1918 command_to_string(command
),
1919 oid_to_hex(&parent
->object
.oid
));
1922 * "commit" is an existing commit. We would want to apply
1923 * the difference it introduces since its first parent "prev"
1924 * on top of the current HEAD if we are cherry-pick. Or the
1925 * reverse of it if we are revert.
1928 if (command
== TODO_REVERT
) {
1930 base_label
= msg
.label
;
1932 next_label
= msg
.parent_label
;
1933 strbuf_addstr(&msgbuf
, "Revert \"");
1934 strbuf_addstr(&msgbuf
, msg
.subject
);
1935 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
1936 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1938 if (commit
->parents
&& commit
->parents
->next
) {
1939 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1940 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1942 strbuf_addstr(&msgbuf
, ".\n");
1947 base_label
= msg
.parent_label
;
1949 next_label
= msg
.label
;
1951 /* Append the commit log message to msgbuf. */
1952 if (find_commit_subject(msg
.message
, &p
))
1953 strbuf_addstr(&msgbuf
, p
);
1955 if (opts
->record_origin
) {
1956 strbuf_complete_line(&msgbuf
);
1957 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1958 strbuf_addch(&msgbuf
, '\n');
1959 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1960 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1961 strbuf_addstr(&msgbuf
, ")\n");
1963 if (!is_fixup(command
))
1964 author
= get_author(msg
.message
);
1967 if (command
== TODO_REWORD
)
1969 else if (is_fixup(command
)) {
1970 if (update_squash_messages(r
, command
, commit
, opts
))
1974 msg_file
= rebase_path_squash_msg();
1975 else if (file_exists(rebase_path_fixup_msg())) {
1976 flags
|= CLEANUP_MSG
;
1977 msg_file
= rebase_path_fixup_msg();
1979 const char *dest
= git_path_squash_msg(r
);
1981 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1982 return error(_("could not rename '%s' to '%s'"),
1983 rebase_path_squash_msg(), dest
);
1984 unlink(git_path_merge_msg(r
));
1990 if (opts
->signoff
&& !is_fixup(command
))
1991 append_signoff(&msgbuf
, 0, 0);
1993 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1995 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1996 res
= do_recursive_merge(r
, base
, next
, base_label
, next_label
,
1997 &head
, &msgbuf
, opts
);
2001 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
2002 git_path_merge_msg(r
), 0);
2004 struct commit_list
*common
= NULL
;
2005 struct commit_list
*remotes
= NULL
;
2007 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
2008 git_path_merge_msg(r
), 0);
2010 commit_list_insert(base
, &common
);
2011 commit_list_insert(next
, &remotes
);
2012 res
|= try_merge_command(r
, opts
->strategy
,
2013 opts
->xopts_nr
, (const char **)opts
->xopts
,
2014 common
, oid_to_hex(&head
), remotes
);
2015 free_commit_list(common
);
2016 free_commit_list(remotes
);
2018 strbuf_release(&msgbuf
);
2021 * If the merge was clean or if it failed due to conflict, we write
2022 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
2023 * However, if the merge did not even start, then we don't want to
2026 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
2027 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
2028 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2030 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
2031 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
2032 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2036 error(command
== TODO_REVERT
2037 ? _("could not revert %s... %s")
2038 : _("could not apply %s... %s"),
2039 short_commit_name(commit
), msg
.subject
);
2040 print_advice(r
, res
== 1, opts
);
2041 repo_rerere(r
, opts
->allow_rerere_auto
);
2045 allow
= allow_empty(r
, opts
, commit
);
2050 flags
|= ALLOW_EMPTY
;
2051 if (!opts
->no_commit
) {
2052 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
2053 res
= do_commit(r
, msg_file
, author
, opts
, flags
);
2055 res
= error(_("unable to parse commit author"));
2056 *check_todo
= !!(flags
& EDIT_MSG
);
2057 if (!res
&& reword
) {
2059 res
= run_git_commit(r
, NULL
, opts
, EDIT_MSG
|
2060 VERIFY_MSG
| AMEND_MSG
|
2061 (flags
& ALLOW_EMPTY
));
2067 if (!res
&& final_fixup
) {
2068 unlink(rebase_path_fixup_msg());
2069 unlink(rebase_path_squash_msg());
2070 unlink(rebase_path_current_fixups());
2071 strbuf_reset(&opts
->current_fixups
);
2072 opts
->current_fixup_count
= 0;
2076 free_message(commit
, &msg
);
2078 update_abort_safety_file();
2083 static int prepare_revs(struct replay_opts
*opts
)
2086 * picking (but not reverting) ranges (but not individual revisions)
2087 * should be done in reverse
2089 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
2090 opts
->revs
->reverse
^= 1;
2092 if (prepare_revision_walk(opts
->revs
))
2093 return error(_("revision walk setup failed"));
2098 static int read_and_refresh_cache(struct repository
*r
,
2099 struct replay_opts
*opts
)
2101 struct lock_file index_lock
= LOCK_INIT
;
2102 int index_fd
= repo_hold_locked_index(r
, &index_lock
, 0);
2103 if (repo_read_index(r
) < 0) {
2104 rollback_lock_file(&index_lock
);
2105 return error(_("git %s: failed to read the index"),
2106 _(action_name(opts
)));
2108 refresh_index(r
->index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
2109 if (index_fd
>= 0) {
2110 if (write_locked_index(r
->index
, &index_lock
,
2111 COMMIT_LOCK
| SKIP_IF_UNCHANGED
)) {
2112 return error(_("git %s: failed to refresh the index"),
2113 _(action_name(opts
)));
2119 enum todo_item_flags
{
2120 TODO_EDIT_MERGE_MSG
= 1
2123 void todo_list_release(struct todo_list
*todo_list
)
2125 strbuf_release(&todo_list
->buf
);
2126 FREE_AND_NULL(todo_list
->items
);
2127 todo_list
->nr
= todo_list
->alloc
= 0;
2130 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
2132 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
2133 return todo_list
->items
+ todo_list
->nr
++;
2136 const char *todo_item_get_arg(struct todo_list
*todo_list
,
2137 struct todo_item
*item
)
2139 return todo_list
->buf
.buf
+ item
->arg_offset
;
2142 static int is_command(enum todo_command command
, const char **bol
)
2144 const char *str
= todo_command_info
[command
].str
;
2145 const char nick
= todo_command_info
[command
].c
;
2146 const char *p
= *bol
+ 1;
2148 return skip_prefix(*bol
, str
, bol
) ||
2149 ((nick
&& **bol
== nick
) &&
2150 (*p
== ' ' || *p
== '\t' || *p
== '\n' || *p
== '\r' || !*p
) &&
2154 static int parse_insn_line(struct repository
*r
, struct todo_item
*item
,
2155 const char *buf
, const char *bol
, char *eol
)
2157 struct object_id commit_oid
;
2158 char *end_of_object_name
;
2159 int i
, saved
, status
, padding
;
2164 bol
+= strspn(bol
, " \t");
2166 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
2167 item
->command
= TODO_COMMENT
;
2168 item
->commit
= NULL
;
2169 item
->arg_offset
= bol
- buf
;
2170 item
->arg_len
= eol
- bol
;
2174 for (i
= 0; i
< TODO_COMMENT
; i
++)
2175 if (is_command(i
, &bol
)) {
2179 if (i
>= TODO_COMMENT
)
2182 /* Eat up extra spaces/ tabs before object name */
2183 padding
= strspn(bol
, " \t");
2186 if (item
->command
== TODO_NOOP
|| item
->command
== TODO_BREAK
) {
2188 return error(_("%s does not accept arguments: '%s'"),
2189 command_to_string(item
->command
), bol
);
2190 item
->commit
= NULL
;
2191 item
->arg_offset
= bol
- buf
;
2192 item
->arg_len
= eol
- bol
;
2197 return error(_("missing arguments for %s"),
2198 command_to_string(item
->command
));
2200 if (item
->command
== TODO_EXEC
|| item
->command
== TODO_LABEL
||
2201 item
->command
== TODO_RESET
) {
2202 item
->commit
= NULL
;
2203 item
->arg_offset
= bol
- buf
;
2204 item
->arg_len
= (int)(eol
- bol
);
2208 if (item
->command
== TODO_MERGE
) {
2209 if (skip_prefix(bol
, "-C", &bol
))
2210 bol
+= strspn(bol
, " \t");
2211 else if (skip_prefix(bol
, "-c", &bol
)) {
2212 bol
+= strspn(bol
, " \t");
2213 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2215 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2216 item
->commit
= NULL
;
2217 item
->arg_offset
= bol
- buf
;
2218 item
->arg_len
= (int)(eol
- bol
);
2223 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
2224 saved
= *end_of_object_name
;
2225 *end_of_object_name
= '\0';
2226 status
= get_oid(bol
, &commit_oid
);
2227 *end_of_object_name
= saved
;
2229 bol
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
2230 item
->arg_offset
= bol
- buf
;
2231 item
->arg_len
= (int)(eol
- bol
);
2234 return error(_("could not parse '%.*s'"),
2235 (int)(end_of_object_name
- bol
), bol
);
2237 item
->commit
= lookup_commit_reference(r
, &commit_oid
);
2238 return !item
->commit
;
2241 int sequencer_get_last_command(struct repository
*r
, enum replay_action
*action
)
2243 const char *todo_file
, *bol
;
2244 struct strbuf buf
= STRBUF_INIT
;
2247 todo_file
= git_path_todo_file();
2248 if (strbuf_read_file(&buf
, todo_file
, 0) < 0) {
2249 if (errno
== ENOENT
|| errno
== ENOTDIR
)
2252 return error_errno("unable to open '%s'", todo_file
);
2254 bol
= buf
.buf
+ strspn(buf
.buf
, " \t\r\n");
2255 if (is_command(TODO_PICK
, &bol
) && (*bol
== ' ' || *bol
== '\t'))
2256 *action
= REPLAY_PICK
;
2257 else if (is_command(TODO_REVERT
, &bol
) &&
2258 (*bol
== ' ' || *bol
== '\t'))
2259 *action
= REPLAY_REVERT
;
2263 strbuf_release(&buf
);
2268 int todo_list_parse_insn_buffer(struct repository
*r
, char *buf
,
2269 struct todo_list
*todo_list
)
2271 struct todo_item
*item
;
2272 char *p
= buf
, *next_p
;
2273 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
2275 todo_list
->current
= todo_list
->nr
= 0;
2277 for (i
= 1; *p
; i
++, p
= next_p
) {
2278 char *eol
= strchrnul(p
, '\n');
2280 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
2282 if (p
!= eol
&& eol
[-1] == '\r')
2283 eol
--; /* strip Carriage Return */
2285 item
= append_new_todo(todo_list
);
2286 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
2287 if (parse_insn_line(r
, item
, buf
, p
, eol
)) {
2288 res
= error(_("invalid line %d: %.*s"),
2289 i
, (int)(eol
- p
), p
);
2290 item
->command
= TODO_COMMENT
+ 1;
2291 item
->arg_offset
= p
- buf
;
2292 item
->arg_len
= (int)(eol
- p
);
2293 item
->commit
= NULL
;
2298 else if (is_fixup(item
->command
))
2299 return error(_("cannot '%s' without a previous commit"),
2300 command_to_string(item
->command
));
2301 else if (!is_noop(item
->command
))
2308 static int count_commands(struct todo_list
*todo_list
)
2312 for (i
= 0; i
< todo_list
->nr
; i
++)
2313 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
2319 static int get_item_line_offset(struct todo_list
*todo_list
, int index
)
2321 return index
< todo_list
->nr
?
2322 todo_list
->items
[index
].offset_in_buf
: todo_list
->buf
.len
;
2325 static const char *get_item_line(struct todo_list
*todo_list
, int index
)
2327 return todo_list
->buf
.buf
+ get_item_line_offset(todo_list
, index
);
2330 static int get_item_line_length(struct todo_list
*todo_list
, int index
)
2332 return get_item_line_offset(todo_list
, index
+ 1)
2333 - get_item_line_offset(todo_list
, index
);
2336 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
2341 fd
= open(path
, O_RDONLY
);
2343 return error_errno(_("could not open '%s'"), path
);
2344 len
= strbuf_read(sb
, fd
, 0);
2347 return error(_("could not read '%s'."), path
);
2351 static int have_finished_the_last_pick(void)
2353 struct strbuf buf
= STRBUF_INIT
;
2355 const char *todo_path
= git_path_todo_file();
2358 if (strbuf_read_file(&buf
, todo_path
, 0) < 0) {
2359 if (errno
== ENOENT
) {
2362 error_errno("unable to open '%s'", todo_path
);
2366 /* If there is only one line then we are done */
2367 eol
= strchr(buf
.buf
, '\n');
2368 if (!eol
|| !eol
[1])
2371 strbuf_release(&buf
);
2376 void sequencer_post_commit_cleanup(struct repository
*r
, int verbose
)
2378 struct replay_opts opts
= REPLAY_OPTS_INIT
;
2379 int need_cleanup
= 0;
2381 if (file_exists(git_path_cherry_pick_head(r
))) {
2382 if (!unlink(git_path_cherry_pick_head(r
)) && verbose
)
2383 warning(_("cancelling a cherry picking in progress"));
2384 opts
.action
= REPLAY_PICK
;
2388 if (file_exists(git_path_revert_head(r
))) {
2389 if (!unlink(git_path_revert_head(r
)) && verbose
)
2390 warning(_("cancelling a revert in progress"));
2391 opts
.action
= REPLAY_REVERT
;
2398 if (!have_finished_the_last_pick())
2401 sequencer_remove_state(&opts
);
2404 static int read_populate_todo(struct repository
*r
,
2405 struct todo_list
*todo_list
,
2406 struct replay_opts
*opts
)
2409 const char *todo_file
= get_todo_path(opts
);
2412 strbuf_reset(&todo_list
->buf
);
2413 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
2416 res
= stat(todo_file
, &st
);
2418 return error(_("could not stat '%s'"), todo_file
);
2419 fill_stat_data(&todo_list
->stat
, &st
);
2421 res
= todo_list_parse_insn_buffer(r
, todo_list
->buf
.buf
, todo_list
);
2423 if (is_rebase_i(opts
))
2424 return error(_("please fix this using "
2425 "'git rebase --edit-todo'."));
2426 return error(_("unusable instruction sheet: '%s'"), todo_file
);
2429 if (!todo_list
->nr
&&
2430 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
2431 return error(_("no commits parsed."));
2433 if (!is_rebase_i(opts
)) {
2434 enum todo_command valid
=
2435 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
2438 for (i
= 0; i
< todo_list
->nr
; i
++)
2439 if (valid
== todo_list
->items
[i
].command
)
2441 else if (valid
== TODO_PICK
)
2442 return error(_("cannot cherry-pick during a revert."));
2444 return error(_("cannot revert during a cherry-pick."));
2447 if (is_rebase_i(opts
)) {
2448 struct todo_list done
= TODO_LIST_INIT
;
2449 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
2451 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
2452 !todo_list_parse_insn_buffer(r
, done
.buf
.buf
, &done
))
2453 todo_list
->done_nr
= count_commands(&done
);
2455 todo_list
->done_nr
= 0;
2457 todo_list
->total_nr
= todo_list
->done_nr
2458 + count_commands(todo_list
);
2459 todo_list_release(&done
);
2462 fprintf(f
, "%d\n", todo_list
->total_nr
);
2470 static int git_config_string_dup(char **dest
,
2471 const char *var
, const char *value
)
2474 return config_error_nonbool(var
);
2476 *dest
= xstrdup(value
);
2480 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
2482 struct replay_opts
*opts
= data
;
2487 else if (!strcmp(key
, "options.no-commit"))
2488 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
2489 else if (!strcmp(key
, "options.edit"))
2490 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
2491 else if (!strcmp(key
, "options.allow-empty"))
2493 git_config_bool_or_int(key
, value
, &error_flag
);
2494 else if (!strcmp(key
, "options.allow-empty-message"))
2495 opts
->allow_empty_message
=
2496 git_config_bool_or_int(key
, value
, &error_flag
);
2497 else if (!strcmp(key
, "options.keep-redundant-commits"))
2498 opts
->keep_redundant_commits
=
2499 git_config_bool_or_int(key
, value
, &error_flag
);
2500 else if (!strcmp(key
, "options.signoff"))
2501 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
2502 else if (!strcmp(key
, "options.record-origin"))
2503 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
2504 else if (!strcmp(key
, "options.allow-ff"))
2505 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
2506 else if (!strcmp(key
, "options.mainline"))
2507 opts
->mainline
= git_config_int(key
, value
);
2508 else if (!strcmp(key
, "options.strategy"))
2509 git_config_string_dup(&opts
->strategy
, key
, value
);
2510 else if (!strcmp(key
, "options.gpg-sign"))
2511 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
2512 else if (!strcmp(key
, "options.strategy-option")) {
2513 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
2514 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
2515 } else if (!strcmp(key
, "options.allow-rerere-auto"))
2516 opts
->allow_rerere_auto
=
2517 git_config_bool_or_int(key
, value
, &error_flag
) ?
2518 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
2519 else if (!strcmp(key
, "options.default-msg-cleanup")) {
2520 opts
->explicit_cleanup
= 1;
2521 opts
->default_msg_cleanup
= get_cleanup_mode(value
, 1);
2523 return error(_("invalid key: %s"), key
);
2526 return error(_("invalid value for %s: %s"), key
, value
);
2531 void parse_strategy_opts(struct replay_opts
*opts
, char *raw_opts
)
2534 char *strategy_opts_string
= raw_opts
;
2536 if (*strategy_opts_string
== ' ')
2537 strategy_opts_string
++;
2539 opts
->xopts_nr
= split_cmdline(strategy_opts_string
,
2540 (const char ***)&opts
->xopts
);
2541 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
2542 const char *arg
= opts
->xopts
[i
];
2544 skip_prefix(arg
, "--", &arg
);
2545 opts
->xopts
[i
] = xstrdup(arg
);
2549 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
2552 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
2554 opts
->strategy
= strbuf_detach(buf
, NULL
);
2555 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
2558 parse_strategy_opts(opts
, buf
->buf
);
2561 static int read_populate_opts(struct replay_opts
*opts
)
2563 if (is_rebase_i(opts
)) {
2564 struct strbuf buf
= STRBUF_INIT
;
2566 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
2567 if (!starts_with(buf
.buf
, "-S"))
2570 free(opts
->gpg_sign
);
2571 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
2576 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(), 1)) {
2577 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
2578 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
2579 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
2580 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
2584 if (file_exists(rebase_path_verbose()))
2587 if (file_exists(rebase_path_quiet()))
2590 if (file_exists(rebase_path_signoff())) {
2595 if (file_exists(rebase_path_cdate_is_adate())) {
2597 opts
->committer_date_is_author_date
= 1;
2600 if (file_exists(rebase_path_ignore_date())) {
2602 opts
->ignore_date
= 1;
2605 if (file_exists(rebase_path_reschedule_failed_exec()))
2606 opts
->reschedule_failed_exec
= 1;
2608 read_strategy_opts(opts
, &buf
);
2609 strbuf_release(&buf
);
2611 if (read_oneliner(&opts
->current_fixups
,
2612 rebase_path_current_fixups(), 1)) {
2613 const char *p
= opts
->current_fixups
.buf
;
2614 opts
->current_fixup_count
= 1;
2615 while ((p
= strchr(p
, '\n'))) {
2616 opts
->current_fixup_count
++;
2621 if (read_oneliner(&buf
, rebase_path_squash_onto(), 0)) {
2622 if (get_oid_hex(buf
.buf
, &opts
->squash_onto
) < 0)
2623 return error(_("unusable squash-onto"));
2624 opts
->have_squash_onto
= 1;
2630 if (!file_exists(git_path_opts_file()))
2633 * The function git_parse_source(), called from git_config_from_file(),
2634 * may die() in case of a syntactically incorrect file. We do not care
2635 * about this case, though, because we wrote that file ourselves, so we
2636 * are pretty certain that it is syntactically correct.
2638 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
2639 return error(_("malformed options sheet: '%s'"),
2640 git_path_opts_file());
2644 static void write_strategy_opts(struct replay_opts
*opts
)
2647 struct strbuf buf
= STRBUF_INIT
;
2649 for (i
= 0; i
< opts
->xopts_nr
; ++i
)
2650 strbuf_addf(&buf
, " --%s", opts
->xopts
[i
]);
2652 write_file(rebase_path_strategy_opts(), "%s\n", buf
.buf
);
2653 strbuf_release(&buf
);
2656 int write_basic_state(struct replay_opts
*opts
, const char *head_name
,
2657 struct commit
*onto
, const char *orig_head
)
2659 const char *quiet
= getenv("GIT_QUIET");
2662 write_file(rebase_path_head_name(), "%s\n", head_name
);
2664 write_file(rebase_path_onto(), "%s\n",
2665 oid_to_hex(&onto
->object
.oid
));
2667 write_file(rebase_path_orig_head(), "%s\n", orig_head
);
2670 write_file(rebase_path_quiet(), "%s\n", quiet
);
2672 write_file(rebase_path_verbose(), "%s", "");
2674 write_file(rebase_path_strategy(), "%s\n", opts
->strategy
);
2675 if (opts
->xopts_nr
> 0)
2676 write_strategy_opts(opts
);
2678 if (opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
)
2679 write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
2680 else if (opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
)
2681 write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
2684 write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts
->gpg_sign
);
2686 write_file(rebase_path_signoff(), "--signoff\n");
2687 if (opts
->committer_date_is_author_date
)
2688 write_file(rebase_path_cdate_is_adate(), "%s", "");
2689 if (opts
->ignore_date
)
2690 write_file(rebase_path_ignore_date(), "%s", "");
2691 if (opts
->reschedule_failed_exec
)
2692 write_file(rebase_path_reschedule_failed_exec(), "%s", "");
2697 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
2698 struct replay_opts
*opts
)
2700 enum todo_command command
= opts
->action
== REPLAY_PICK
?
2701 TODO_PICK
: TODO_REVERT
;
2702 const char *command_string
= todo_command_info
[command
].str
;
2703 const char *encoding
;
2704 struct commit
*commit
;
2706 if (prepare_revs(opts
))
2709 encoding
= get_log_output_encoding();
2711 while ((commit
= get_revision(opts
->revs
))) {
2712 struct todo_item
*item
= append_new_todo(todo_list
);
2713 const char *commit_buffer
= logmsg_reencode(commit
, NULL
, encoding
);
2714 const char *subject
;
2717 item
->command
= command
;
2718 item
->commit
= commit
;
2719 item
->arg_offset
= 0;
2721 item
->offset_in_buf
= todo_list
->buf
.len
;
2722 subject_len
= find_commit_subject(commit_buffer
, &subject
);
2723 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
2724 short_commit_name(commit
), subject_len
, subject
);
2725 unuse_commit_buffer(commit
, commit_buffer
);
2729 return error(_("empty commit set passed"));
2734 static int create_seq_dir(struct repository
*r
)
2736 enum replay_action action
;
2737 const char *in_progress_error
= NULL
;
2738 const char *in_progress_advice
= NULL
;
2739 unsigned int advise_skip
= file_exists(git_path_revert_head(r
)) ||
2740 file_exists(git_path_cherry_pick_head(r
));
2742 if (!sequencer_get_last_command(r
, &action
)) {
2745 in_progress_error
= _("revert is already in progress");
2746 in_progress_advice
=
2747 _("try \"git revert (--continue | %s--abort | --quit)\"");
2750 in_progress_error
= _("cherry-pick is already in progress");
2751 in_progress_advice
=
2752 _("try \"git cherry-pick (--continue | %s--abort | --quit)\"");
2755 BUG("unexpected action in create_seq_dir");
2758 if (in_progress_error
) {
2759 error("%s", in_progress_error
);
2760 if (advice_sequencer_in_use
)
2761 advise(in_progress_advice
,
2762 advise_skip
? "--skip | " : "");
2765 if (mkdir(git_path_seq_dir(), 0777) < 0)
2766 return error_errno(_("could not create sequencer directory '%s'"),
2767 git_path_seq_dir());
2772 static int save_head(const char *head
)
2774 struct lock_file head_lock
= LOCK_INIT
;
2775 struct strbuf buf
= STRBUF_INIT
;
2779 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
2781 return error_errno(_("could not lock HEAD"));
2782 strbuf_addf(&buf
, "%s\n", head
);
2783 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
2784 strbuf_release(&buf
);
2786 error_errno(_("could not write to '%s'"), git_path_head_file());
2787 rollback_lock_file(&head_lock
);
2790 if (commit_lock_file(&head_lock
) < 0)
2791 return error(_("failed to finalize '%s'"), git_path_head_file());
2795 static int rollback_is_safe(void)
2797 struct strbuf sb
= STRBUF_INIT
;
2798 struct object_id expected_head
, actual_head
;
2800 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
2802 if (get_oid_hex(sb
.buf
, &expected_head
)) {
2803 strbuf_release(&sb
);
2804 die(_("could not parse %s"), git_path_abort_safety_file());
2806 strbuf_release(&sb
);
2808 else if (errno
== ENOENT
)
2809 oidclr(&expected_head
);
2811 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
2813 if (get_oid("HEAD", &actual_head
))
2814 oidclr(&actual_head
);
2816 return oideq(&actual_head
, &expected_head
);
2819 static int reset_merge(const struct object_id
*oid
)
2822 struct argv_array argv
= ARGV_ARRAY_INIT
;
2824 argv_array_pushl(&argv
, "reset", "--merge", NULL
);
2826 if (!is_null_oid(oid
))
2827 argv_array_push(&argv
, oid_to_hex(oid
));
2829 ret
= run_command_v_opt(argv
.argv
, RUN_GIT_CMD
);
2830 argv_array_clear(&argv
);
2835 static int rollback_single_pick(struct repository
*r
)
2837 struct object_id head_oid
;
2839 if (!file_exists(git_path_cherry_pick_head(r
)) &&
2840 !file_exists(git_path_revert_head(r
)))
2841 return error(_("no cherry-pick or revert in progress"));
2842 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
2843 return error(_("cannot resolve HEAD"));
2844 if (is_null_oid(&head_oid
))
2845 return error(_("cannot abort from a branch yet to be born"));
2846 return reset_merge(&head_oid
);
2849 static int skip_single_pick(void)
2851 struct object_id head
;
2853 if (read_ref_full("HEAD", 0, &head
, NULL
))
2854 return error(_("cannot resolve HEAD"));
2855 return reset_merge(&head
);
2858 int sequencer_rollback(struct repository
*r
, struct replay_opts
*opts
)
2861 struct object_id oid
;
2862 struct strbuf buf
= STRBUF_INIT
;
2865 f
= fopen(git_path_head_file(), "r");
2866 if (!f
&& errno
== ENOENT
) {
2868 * There is no multiple-cherry-pick in progress.
2869 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
2870 * a single-cherry-pick in progress, abort that.
2872 return rollback_single_pick(r
);
2875 return error_errno(_("cannot open '%s'"), git_path_head_file());
2876 if (strbuf_getline_lf(&buf
, f
)) {
2877 error(_("cannot read '%s': %s"), git_path_head_file(),
2878 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
2883 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
2884 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
2885 git_path_head_file());
2888 if (is_null_oid(&oid
)) {
2889 error(_("cannot abort from a branch yet to be born"));
2893 if (!rollback_is_safe()) {
2894 /* Do not error, just do not rollback */
2895 warning(_("You seem to have moved HEAD. "
2896 "Not rewinding, check your HEAD!"));
2898 if (reset_merge(&oid
))
2900 strbuf_release(&buf
);
2901 return sequencer_remove_state(opts
);
2903 strbuf_release(&buf
);
2907 int sequencer_skip(struct repository
*r
, struct replay_opts
*opts
)
2909 enum replay_action action
= -1;
2910 sequencer_get_last_command(r
, &action
);
2913 * Check whether the subcommand requested to skip the commit is actually
2914 * in progress and that it's safe to skip the commit.
2916 * opts->action tells us which subcommand requested to skip the commit.
2917 * If the corresponding .git/<ACTION>_HEAD exists, we know that the
2918 * action is in progress and we can skip the commit.
2920 * Otherwise we check that the last instruction was related to the
2921 * particular subcommand we're trying to execute and barf if that's not
2924 * Finally we check that the rollback is "safe", i.e., has the HEAD
2925 * moved? In this case, it doesn't make sense to "reset the merge" and
2926 * "skip the commit" as the user already handled this by committing. But
2927 * we'd not want to barf here, instead give advice on how to proceed. We
2928 * only need to check that when .git/<ACTION>_HEAD doesn't exist because
2929 * it gets removed when the user commits, so if it still exists we're
2930 * sure the user can't have committed before.
2932 switch (opts
->action
) {
2934 if (!file_exists(git_path_revert_head(r
))) {
2935 if (action
!= REPLAY_REVERT
)
2936 return error(_("no revert in progress"));
2937 if (!rollback_is_safe())
2942 if (!file_exists(git_path_cherry_pick_head(r
))) {
2943 if (action
!= REPLAY_PICK
)
2944 return error(_("no cherry-pick in progress"));
2945 if (!rollback_is_safe())
2950 BUG("unexpected action in sequencer_skip");
2953 if (skip_single_pick())
2954 return error(_("failed to skip the commit"));
2955 if (!is_directory(git_path_seq_dir()))
2958 return sequencer_continue(r
, opts
);
2961 error(_("there is nothing to skip"));
2963 if (advice_resolve_conflict
) {
2964 advise(_("have you committed already?\n"
2965 "try \"git %s --continue\""),
2966 action
== REPLAY_REVERT
? "revert" : "cherry-pick");
2971 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2973 struct lock_file todo_lock
= LOCK_INIT
;
2974 const char *todo_path
= get_todo_path(opts
);
2975 int next
= todo_list
->current
, offset
, fd
;
2978 * rebase -i writes "git-rebase-todo" without the currently executing
2979 * command, appending it to "done" instead.
2981 if (is_rebase_i(opts
))
2984 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
2986 return error_errno(_("could not lock '%s'"), todo_path
);
2987 offset
= get_item_line_offset(todo_list
, next
);
2988 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
2989 todo_list
->buf
.len
- offset
) < 0)
2990 return error_errno(_("could not write to '%s'"), todo_path
);
2991 if (commit_lock_file(&todo_lock
) < 0)
2992 return error(_("failed to finalize '%s'"), todo_path
);
2994 if (is_rebase_i(opts
) && next
> 0) {
2995 const char *done
= rebase_path_done();
2996 int fd
= open(done
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
3001 if (write_in_full(fd
, get_item_line(todo_list
, next
- 1),
3002 get_item_line_length(todo_list
, next
- 1))
3004 ret
= error_errno(_("could not write to '%s'"), done
);
3006 ret
= error_errno(_("failed to finalize '%s'"), done
);
3012 static int save_opts(struct replay_opts
*opts
)
3014 const char *opts_file
= git_path_opts_file();
3017 if (opts
->no_commit
)
3018 res
|= git_config_set_in_file_gently(opts_file
,
3019 "options.no-commit", "true");
3021 res
|= git_config_set_in_file_gently(opts_file
,
3022 "options.edit", "true");
3023 if (opts
->allow_empty
)
3024 res
|= git_config_set_in_file_gently(opts_file
,
3025 "options.allow-empty", "true");
3026 if (opts
->allow_empty_message
)
3027 res
|= git_config_set_in_file_gently(opts_file
,
3028 "options.allow-empty-message", "true");
3029 if (opts
->keep_redundant_commits
)
3030 res
|= git_config_set_in_file_gently(opts_file
,
3031 "options.keep-redundant-commits", "true");
3033 res
|= git_config_set_in_file_gently(opts_file
,
3034 "options.signoff", "true");
3035 if (opts
->record_origin
)
3036 res
|= git_config_set_in_file_gently(opts_file
,
3037 "options.record-origin", "true");
3039 res
|= git_config_set_in_file_gently(opts_file
,
3040 "options.allow-ff", "true");
3041 if (opts
->mainline
) {
3042 struct strbuf buf
= STRBUF_INIT
;
3043 strbuf_addf(&buf
, "%d", opts
->mainline
);
3044 res
|= git_config_set_in_file_gently(opts_file
,
3045 "options.mainline", buf
.buf
);
3046 strbuf_release(&buf
);
3049 res
|= git_config_set_in_file_gently(opts_file
,
3050 "options.strategy", opts
->strategy
);
3052 res
|= git_config_set_in_file_gently(opts_file
,
3053 "options.gpg-sign", opts
->gpg_sign
);
3056 for (i
= 0; i
< opts
->xopts_nr
; i
++)
3057 res
|= git_config_set_multivar_in_file_gently(opts_file
,
3058 "options.strategy-option",
3059 opts
->xopts
[i
], "^$", 0);
3061 if (opts
->allow_rerere_auto
)
3062 res
|= git_config_set_in_file_gently(opts_file
,
3063 "options.allow-rerere-auto",
3064 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
3067 if (opts
->explicit_cleanup
)
3068 res
|= git_config_set_in_file_gently(opts_file
,
3069 "options.default-msg-cleanup",
3070 describe_cleanup_mode(opts
->default_msg_cleanup
));
3074 static int make_patch(struct repository
*r
,
3075 struct commit
*commit
,
3076 struct replay_opts
*opts
)
3078 struct strbuf buf
= STRBUF_INIT
;
3079 struct rev_info log_tree_opt
;
3080 const char *subject
, *p
;
3083 p
= short_commit_name(commit
);
3084 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
3086 if (update_ref("rebase", "REBASE_HEAD", &commit
->object
.oid
,
3087 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
3088 res
|= error(_("could not update %s"), "REBASE_HEAD");
3090 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
3091 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
3092 repo_init_revisions(r
, &log_tree_opt
, NULL
);
3093 log_tree_opt
.abbrev
= 0;
3094 log_tree_opt
.diff
= 1;
3095 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
3096 log_tree_opt
.disable_stdin
= 1;
3097 log_tree_opt
.no_commit_id
= 1;
3098 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
3099 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
3100 if (!log_tree_opt
.diffopt
.file
)
3101 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
3103 res
|= log_tree_commit(&log_tree_opt
, commit
);
3104 fclose(log_tree_opt
.diffopt
.file
);
3108 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
3109 if (!file_exists(buf
.buf
)) {
3110 const char *encoding
= get_commit_output_encoding();
3111 const char *commit_buffer
= logmsg_reencode(commit
, NULL
, encoding
);
3112 find_commit_subject(commit_buffer
, &subject
);
3113 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
3114 unuse_commit_buffer(commit
, commit_buffer
);
3116 strbuf_release(&buf
);
3121 static int intend_to_amend(void)
3123 struct object_id head
;
3126 if (get_oid("HEAD", &head
))
3127 return error(_("cannot read HEAD"));
3129 p
= oid_to_hex(&head
);
3130 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
3133 static int error_with_patch(struct repository
*r
,
3134 struct commit
*commit
,
3135 const char *subject
, int subject_len
,
3136 struct replay_opts
*opts
,
3137 int exit_code
, int to_amend
)
3140 if (make_patch(r
, commit
, opts
))
3142 } else if (copy_file(rebase_path_message(),
3143 git_path_merge_msg(r
), 0666))
3144 return error(_("unable to copy '%s' to '%s'"),
3145 git_path_merge_msg(r
), rebase_path_message());
3148 if (intend_to_amend())
3152 _("You can amend the commit now, with\n"
3154 " git commit --amend %s\n"
3156 "Once you are satisfied with your changes, run\n"
3158 " git rebase --continue\n"),
3159 gpg_sign_opt_quoted(opts
));
3160 } else if (exit_code
) {
3162 fprintf_ln(stderr
, _("Could not apply %s... %.*s"),
3163 short_commit_name(commit
), subject_len
, subject
);
3166 * We don't have the hash of the parent so
3167 * just print the line from the todo file.
3169 fprintf_ln(stderr
, _("Could not merge %.*s"),
3170 subject_len
, subject
);
3176 static int error_failed_squash(struct repository
*r
,
3177 struct commit
*commit
,
3178 struct replay_opts
*opts
,
3180 const char *subject
)
3182 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
3183 return error(_("could not copy '%s' to '%s'"),
3184 rebase_path_squash_msg(), rebase_path_message());
3185 unlink(git_path_merge_msg(r
));
3186 if (copy_file(git_path_merge_msg(r
), rebase_path_message(), 0666))
3187 return error(_("could not copy '%s' to '%s'"),
3188 rebase_path_message(),
3189 git_path_merge_msg(r
));
3190 return error_with_patch(r
, commit
, subject
, subject_len
, opts
, 1, 0);
3193 static int do_exec(struct repository
*r
, const char *command_line
)
3195 struct argv_array child_env
= ARGV_ARRAY_INIT
;
3196 const char *child_argv
[] = { NULL
, NULL
};
3199 fprintf(stderr
, "Executing: %s\n", command_line
);
3200 child_argv
[0] = command_line
;
3201 argv_array_pushf(&child_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
3202 argv_array_pushf(&child_env
, "GIT_WORK_TREE=%s",
3203 absolute_path(get_git_work_tree()));
3204 status
= run_command_v_opt_cd_env(child_argv
, RUN_USING_SHELL
, NULL
,
3207 /* force re-reading of the cache */
3208 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0)
3209 return error(_("could not read index"));
3211 dirty
= require_clean_work_tree(r
, "rebase", NULL
, 1, 1);
3214 warning(_("execution failed: %s\n%s"
3215 "You can fix the problem, and then run\n"
3217 " git rebase --continue\n"
3220 dirty
? N_("and made changes to the index and/or the "
3221 "working tree\n") : "");
3223 /* command not found */
3226 warning(_("execution succeeded: %s\nbut "
3227 "left changes to the index and/or the working tree\n"
3228 "Commit or stash your changes, and then run\n"
3230 " git rebase --continue\n"
3231 "\n"), command_line
);
3235 argv_array_clear(&child_env
);
3240 static int safe_append(const char *filename
, const char *fmt
, ...)
3243 struct lock_file lock
= LOCK_INIT
;
3244 int fd
= hold_lock_file_for_update(&lock
, filename
,
3245 LOCK_REPORT_ON_ERROR
);
3246 struct strbuf buf
= STRBUF_INIT
;
3251 if (strbuf_read_file(&buf
, filename
, 0) < 0 && errno
!= ENOENT
) {
3252 error_errno(_("could not read '%s'"), filename
);
3253 rollback_lock_file(&lock
);
3256 strbuf_complete(&buf
, '\n');
3258 strbuf_vaddf(&buf
, fmt
, ap
);
3261 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
3262 error_errno(_("could not write to '%s'"), filename
);
3263 strbuf_release(&buf
);
3264 rollback_lock_file(&lock
);
3267 if (commit_lock_file(&lock
) < 0) {
3268 strbuf_release(&buf
);
3269 rollback_lock_file(&lock
);
3270 return error(_("failed to finalize '%s'"), filename
);
3273 strbuf_release(&buf
);
3277 static int do_label(struct repository
*r
, const char *name
, int len
)
3279 struct ref_store
*refs
= get_main_ref_store(r
);
3280 struct ref_transaction
*transaction
;
3281 struct strbuf ref_name
= STRBUF_INIT
, err
= STRBUF_INIT
;
3282 struct strbuf msg
= STRBUF_INIT
;
3284 struct object_id head_oid
;
3286 if (len
== 1 && *name
== '#')
3287 return error(_("illegal label name: '%.*s'"), len
, name
);
3289 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
3290 strbuf_addf(&msg
, "rebase -i (label) '%.*s'", len
, name
);
3292 transaction
= ref_store_transaction_begin(refs
, &err
);
3294 error("%s", err
.buf
);
3296 } else if (get_oid("HEAD", &head_oid
)) {
3297 error(_("could not read HEAD"));
3299 } else if (ref_transaction_update(transaction
, ref_name
.buf
, &head_oid
,
3300 NULL
, 0, msg
.buf
, &err
) < 0 ||
3301 ref_transaction_commit(transaction
, &err
)) {
3302 error("%s", err
.buf
);
3305 ref_transaction_free(transaction
);
3306 strbuf_release(&err
);
3307 strbuf_release(&msg
);
3310 ret
= safe_append(rebase_path_refs_to_delete(),
3311 "%s\n", ref_name
.buf
);
3312 strbuf_release(&ref_name
);
3317 static const char *reflog_message(struct replay_opts
*opts
,
3318 const char *sub_action
, const char *fmt
, ...);
3320 static int do_reset(struct repository
*r
,
3321 const char *name
, int len
,
3322 struct replay_opts
*opts
)
3324 struct strbuf ref_name
= STRBUF_INIT
;
3325 struct object_id oid
;
3326 struct lock_file lock
= LOCK_INIT
;
3327 struct tree_desc desc
;
3329 struct unpack_trees_options unpack_tree_opts
;
3332 if (repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0)
3335 if (len
== 10 && !strncmp("[new root]", name
, len
)) {
3336 if (!opts
->have_squash_onto
) {
3338 if (commit_tree("", 0, the_hash_algo
->empty_tree
,
3339 NULL
, &opts
->squash_onto
,
3341 return error(_("writing fake root commit"));
3342 opts
->have_squash_onto
= 1;
3343 hex
= oid_to_hex(&opts
->squash_onto
);
3344 if (write_message(hex
, strlen(hex
),
3345 rebase_path_squash_onto(), 0))
3346 return error(_("writing squash-onto"));
3348 oidcpy(&oid
, &opts
->squash_onto
);
3352 /* Determine the length of the label */
3353 for (i
= 0; i
< len
; i
++)
3354 if (isspace(name
[i
]))
3358 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
3359 if (get_oid(ref_name
.buf
, &oid
) &&
3360 get_oid(ref_name
.buf
+ strlen("refs/rewritten/"), &oid
)) {
3361 error(_("could not read '%s'"), ref_name
.buf
);
3362 rollback_lock_file(&lock
);
3363 strbuf_release(&ref_name
);
3368 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
3369 setup_unpack_trees_porcelain(&unpack_tree_opts
, "reset");
3370 unpack_tree_opts
.head_idx
= 1;
3371 unpack_tree_opts
.src_index
= r
->index
;
3372 unpack_tree_opts
.dst_index
= r
->index
;
3373 unpack_tree_opts
.fn
= oneway_merge
;
3374 unpack_tree_opts
.merge
= 1;
3375 unpack_tree_opts
.update
= 1;
3377 if (repo_read_index_unmerged(r
)) {
3378 rollback_lock_file(&lock
);
3379 strbuf_release(&ref_name
);
3380 return error_resolve_conflict(_(action_name(opts
)));
3383 if (!fill_tree_descriptor(r
, &desc
, &oid
)) {
3384 error(_("failed to find tree of %s"), oid_to_hex(&oid
));
3385 rollback_lock_file(&lock
);
3386 free((void *)desc
.buffer
);
3387 strbuf_release(&ref_name
);
3391 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
3392 rollback_lock_file(&lock
);
3393 free((void *)desc
.buffer
);
3394 strbuf_release(&ref_name
);
3398 tree
= parse_tree_indirect(&oid
);
3399 prime_cache_tree(r
, r
->index
, tree
);
3401 if (write_locked_index(r
->index
, &lock
, COMMIT_LOCK
) < 0)
3402 ret
= error(_("could not write index"));
3403 free((void *)desc
.buffer
);
3406 ret
= update_ref(reflog_message(opts
, "reset", "'%.*s'",
3407 len
, name
), "HEAD", &oid
,
3408 NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
3410 strbuf_release(&ref_name
);
3414 static struct commit
*lookup_label(const char *label
, int len
,
3417 struct commit
*commit
;
3420 strbuf_addf(buf
, "refs/rewritten/%.*s", len
, label
);
3421 commit
= lookup_commit_reference_by_name(buf
->buf
);
3423 /* fall back to non-rewritten ref or commit */
3424 strbuf_splice(buf
, 0, strlen("refs/rewritten/"), "", 0);
3425 commit
= lookup_commit_reference_by_name(buf
->buf
);
3429 error(_("could not resolve '%s'"), buf
->buf
);
3434 static int do_merge(struct repository
*r
,
3435 struct commit
*commit
,
3436 const char *arg
, int arg_len
,
3437 int flags
, struct replay_opts
*opts
)
3439 int run_commit_flags
= (flags
& TODO_EDIT_MERGE_MSG
) ?
3440 EDIT_MSG
| VERIFY_MSG
: 0;
3441 struct strbuf ref_name
= STRBUF_INIT
;
3442 struct commit
*head_commit
, *merge_commit
, *i
;
3443 struct commit_list
*bases
, *j
, *reversed
= NULL
;
3444 struct commit_list
*to_merge
= NULL
, **tail
= &to_merge
;
3445 const char *strategy
= !opts
->xopts_nr
&&
3446 (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive")) ?
3447 NULL
: opts
->strategy
;
3448 struct merge_options o
;
3449 int merge_arg_len
, oneline_offset
, can_fast_forward
, ret
, k
;
3450 static struct lock_file lock
;
3453 if (repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0) {
3458 head_commit
= lookup_commit_reference_by_name("HEAD");
3460 ret
= error(_("cannot merge without a current revision"));
3465 * For octopus merges, the arg starts with the list of revisions to be
3466 * merged. The list is optionally followed by '#' and the oneline.
3468 merge_arg_len
= oneline_offset
= arg_len
;
3469 for (p
= arg
; p
- arg
< arg_len
; p
+= strspn(p
, " \t\n")) {
3472 if (*p
== '#' && (!p
[1] || isspace(p
[1]))) {
3473 p
+= 1 + strspn(p
+ 1, " \t\n");
3474 oneline_offset
= p
- arg
;
3477 k
= strcspn(p
, " \t\n");
3480 merge_commit
= lookup_label(p
, k
, &ref_name
);
3481 if (!merge_commit
) {
3482 ret
= error(_("unable to parse '%.*s'"), k
, p
);
3485 tail
= &commit_list_insert(merge_commit
, tail
)->next
;
3487 merge_arg_len
= p
- arg
;
3491 ret
= error(_("nothing to merge: '%.*s'"), arg_len
, arg
);
3495 if (opts
->have_squash_onto
&&
3496 oideq(&head_commit
->object
.oid
, &opts
->squash_onto
)) {
3498 * When the user tells us to "merge" something into a
3499 * "[new root]", let's simply fast-forward to the merge head.
3501 rollback_lock_file(&lock
);
3503 ret
= error(_("octopus merge cannot be executed on "
3504 "top of a [new root]"));
3506 ret
= fast_forward_to(r
, &to_merge
->item
->object
.oid
,
3507 &head_commit
->object
.oid
, 0,
3513 const char *encoding
= get_commit_output_encoding();
3514 const char *message
= logmsg_reencode(commit
, NULL
, encoding
);
3519 ret
= error(_("could not get commit message of '%s'"),
3520 oid_to_hex(&commit
->object
.oid
));
3523 write_author_script(message
);
3524 find_commit_subject(message
, &body
);
3526 ret
= write_message(body
, len
, git_path_merge_msg(r
), 0);
3527 unuse_commit_buffer(commit
, message
);
3529 error_errno(_("could not write '%s'"),
3530 git_path_merge_msg(r
));
3534 struct strbuf buf
= STRBUF_INIT
;
3537 strbuf_addf(&buf
, "author %s", git_author_info(0));
3538 write_author_script(buf
.buf
);
3541 if (oneline_offset
< arg_len
) {
3542 p
= arg
+ oneline_offset
;
3543 len
= arg_len
- oneline_offset
;
3545 strbuf_addf(&buf
, "Merge %s '%.*s'",
3546 to_merge
->next
? "branches" : "branch",
3547 merge_arg_len
, arg
);
3552 ret
= write_message(p
, len
, git_path_merge_msg(r
), 0);
3553 strbuf_release(&buf
);
3555 error_errno(_("could not write '%s'"),
3556 git_path_merge_msg(r
));
3562 * If HEAD is not identical to the first parent of the original merge
3563 * commit, we cannot fast-forward.
3565 can_fast_forward
= opts
->allow_ff
&& commit
&& commit
->parents
&&
3566 oideq(&commit
->parents
->item
->object
.oid
,
3567 &head_commit
->object
.oid
);
3570 * If any merge head is different from the original one, we cannot
3573 if (can_fast_forward
) {
3574 struct commit_list
*p
= commit
->parents
->next
;
3576 for (j
= to_merge
; j
&& p
; j
= j
->next
, p
= p
->next
)
3577 if (!oideq(&j
->item
->object
.oid
,
3578 &p
->item
->object
.oid
)) {
3579 can_fast_forward
= 0;
3583 * If the number of merge heads differs from the original merge
3584 * commit, we cannot fast-forward.
3587 can_fast_forward
= 0;
3590 if (can_fast_forward
) {
3591 rollback_lock_file(&lock
);
3592 ret
= fast_forward_to(r
, &commit
->object
.oid
,
3593 &head_commit
->object
.oid
, 0, opts
);
3594 if (flags
& TODO_EDIT_MERGE_MSG
) {
3595 run_commit_flags
|= AMEND_MSG
;
3596 goto fast_forward_edit
;
3601 if (strategy
|| to_merge
->next
) {
3603 struct child_process cmd
= CHILD_PROCESS_INIT
;
3605 if (read_env_script(&cmd
.env_array
)) {
3606 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
3608 ret
= error(_(staged_changes_advice
), gpg_opt
, gpg_opt
);
3613 argv_array_push(&cmd
.args
, "merge");
3614 argv_array_push(&cmd
.args
, "-s");
3616 argv_array_push(&cmd
.args
, "octopus");
3618 argv_array_push(&cmd
.args
, strategy
);
3619 for (k
= 0; k
< opts
->xopts_nr
; k
++)
3620 argv_array_pushf(&cmd
.args
,
3621 "-X%s", opts
->xopts
[k
]);
3623 argv_array_push(&cmd
.args
, "--no-edit");
3624 argv_array_push(&cmd
.args
, "--no-ff");
3625 argv_array_push(&cmd
.args
, "--no-log");
3626 argv_array_push(&cmd
.args
, "--no-stat");
3627 argv_array_push(&cmd
.args
, "-F");
3628 argv_array_push(&cmd
.args
, git_path_merge_msg(r
));
3630 argv_array_push(&cmd
.args
, opts
->gpg_sign
);
3631 if (opts
->ignore_date
)
3632 push_dates(&cmd
, opts
->committer_date_is_author_date
);
3634 /* Add the tips to be merged */
3635 for (j
= to_merge
; j
; j
= j
->next
)
3636 argv_array_push(&cmd
.args
,
3637 oid_to_hex(&j
->item
->object
.oid
));
3639 strbuf_release(&ref_name
);
3640 unlink(git_path_cherry_pick_head(r
));
3641 rollback_lock_file(&lock
);
3643 rollback_lock_file(&lock
);
3644 ret
= run_command(&cmd
);
3646 /* force re-reading of the cache */
3647 if (!ret
&& (discard_index(r
->index
) < 0 ||
3648 repo_read_index(r
) < 0))
3649 ret
= error(_("could not read index"));
3653 merge_commit
= to_merge
->item
;
3654 bases
= get_merge_bases(head_commit
, merge_commit
);
3655 if (bases
&& oideq(&merge_commit
->object
.oid
,
3656 &bases
->item
->object
.oid
)) {
3658 /* skip merging an ancestor of HEAD */
3662 write_message(oid_to_hex(&merge_commit
->object
.oid
), the_hash_algo
->hexsz
,
3663 git_path_merge_head(r
), 0);
3664 write_message("no-ff", 5, git_path_merge_mode(r
), 0);
3666 for (j
= bases
; j
; j
= j
->next
)
3667 commit_list_insert(j
->item
, &reversed
);
3668 free_commit_list(bases
);
3671 init_merge_options(&o
, r
);
3673 o
.branch2
= ref_name
.buf
;
3674 o
.buffer_output
= 2;
3676 ret
= merge_recursive(&o
, head_commit
, merge_commit
, reversed
, &i
);
3678 fputs(o
.obuf
.buf
, stdout
);
3679 strbuf_release(&o
.obuf
);
3681 error(_("could not even attempt to merge '%.*s'"),
3682 merge_arg_len
, arg
);
3686 * The return value of merge_recursive() is 1 on clean, and 0 on
3689 * Let's reverse that, so that do_merge() returns 0 upon success and
3690 * 1 upon failed merge (keeping the return value -1 for the cases where
3691 * we will want to reschedule the `merge` command).
3695 if (r
->index
->cache_changed
&&
3696 write_locked_index(r
->index
, &lock
, COMMIT_LOCK
)) {
3697 ret
= error(_("merge: Unable to write new index file"));
3701 rollback_lock_file(&lock
);
3703 repo_rerere(r
, opts
->allow_rerere_auto
);
3706 * In case of problems, we now want to return a positive
3707 * value (a negative one would indicate that the `merge`
3708 * command needs to be rescheduled).
3711 ret
= !!run_git_commit(r
, git_path_merge_msg(r
), opts
,
3715 strbuf_release(&ref_name
);
3716 rollback_lock_file(&lock
);
3717 free_commit_list(to_merge
);
3721 static int is_final_fixup(struct todo_list
*todo_list
)
3723 int i
= todo_list
->current
;
3725 if (!is_fixup(todo_list
->items
[i
].command
))
3728 while (++i
< todo_list
->nr
)
3729 if (is_fixup(todo_list
->items
[i
].command
))
3731 else if (!is_noop(todo_list
->items
[i
].command
))
3736 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
3740 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
3741 if (!is_noop(todo_list
->items
[i
].command
))
3742 return todo_list
->items
[i
].command
;
3747 static int apply_autostash(struct replay_opts
*opts
)
3749 struct strbuf stash_sha1
= STRBUF_INIT
;
3750 struct child_process child
= CHILD_PROCESS_INIT
;
3753 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
3754 strbuf_release(&stash_sha1
);
3757 strbuf_trim(&stash_sha1
);
3760 child
.no_stdout
= 1;
3761 child
.no_stderr
= 1;
3762 argv_array_push(&child
.args
, "stash");
3763 argv_array_push(&child
.args
, "apply");
3764 argv_array_push(&child
.args
, stash_sha1
.buf
);
3765 if (!run_command(&child
))
3766 fprintf(stderr
, _("Applied autostash.\n"));
3768 struct child_process store
= CHILD_PROCESS_INIT
;
3771 argv_array_push(&store
.args
, "stash");
3772 argv_array_push(&store
.args
, "store");
3773 argv_array_push(&store
.args
, "-m");
3774 argv_array_push(&store
.args
, "autostash");
3775 argv_array_push(&store
.args
, "-q");
3776 argv_array_push(&store
.args
, stash_sha1
.buf
);
3777 if (run_command(&store
))
3778 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
3781 _("Applying autostash resulted in conflicts.\n"
3782 "Your changes are safe in the stash.\n"
3783 "You can run \"git stash pop\" or"
3784 " \"git stash drop\" at any time.\n"));
3787 strbuf_release(&stash_sha1
);
3791 static const char *reflog_message(struct replay_opts
*opts
,
3792 const char *sub_action
, const char *fmt
, ...)
3795 static struct strbuf buf
= STRBUF_INIT
;
3799 strbuf_addstr(&buf
, action_name(opts
));
3801 strbuf_addf(&buf
, " (%s)", sub_action
);
3803 strbuf_addstr(&buf
, ": ");
3804 strbuf_vaddf(&buf
, fmt
, ap
);
3811 static int run_git_checkout(struct repository
*r
, struct replay_opts
*opts
,
3812 const char *commit
, const char *action
)
3814 struct child_process cmd
= CHILD_PROCESS_INIT
;
3819 argv_array_push(&cmd
.args
, "checkout");
3820 argv_array_push(&cmd
.args
, commit
);
3821 argv_array_pushf(&cmd
.env_array
, GIT_REFLOG_ACTION
"=%s", action
);
3824 ret
= run_command(&cmd
);
3826 ret
= run_command_silent_on_success(&cmd
);
3829 discard_index(r
->index
);
3834 int prepare_branch_to_be_rebased(struct repository
*r
, struct replay_opts
*opts
,
3839 if (commit
&& *commit
) {
3840 action
= reflog_message(opts
, "start", "checkout %s", commit
);
3841 if (run_git_checkout(r
, opts
, commit
, action
))
3842 return error(_("could not checkout %s"), commit
);
3848 static int checkout_onto(struct repository
*r
, struct replay_opts
*opts
,
3849 const char *onto_name
, const struct object_id
*onto
,
3850 const char *orig_head
)
3852 struct object_id oid
;
3853 const char *action
= reflog_message(opts
, "start", "checkout %s", onto_name
);
3855 if (get_oid(orig_head
, &oid
))
3856 return error(_("%s: not a valid OID"), orig_head
);
3858 if (run_git_checkout(r
, opts
, oid_to_hex(onto
), action
)) {
3859 apply_autostash(opts
);
3860 sequencer_remove_state(opts
);
3861 return error(_("could not detach HEAD"));
3864 return update_ref(NULL
, "ORIG_HEAD", &oid
, NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
3867 static int stopped_at_head(struct repository
*r
)
3869 struct object_id head
;
3870 struct commit
*commit
;
3871 struct commit_message message
;
3873 if (get_oid("HEAD", &head
) ||
3874 !(commit
= lookup_commit(r
, &head
)) ||
3875 parse_commit(commit
) || get_message(commit
, &message
))
3876 fprintf(stderr
, _("Stopped at HEAD\n"));
3878 fprintf(stderr
, _("Stopped at %s\n"), message
.label
);
3879 free_message(commit
, &message
);
3885 static const char rescheduled_advice
[] =
3886 N_("Could not execute the todo command\n"
3890 "It has been rescheduled; To edit the command before continuing, please\n"
3891 "edit the todo list first:\n"
3893 " git rebase --edit-todo\n"
3894 " git rebase --continue\n");
3896 static int pick_commits(struct repository
*r
,
3897 struct todo_list
*todo_list
,
3898 struct replay_opts
*opts
)
3900 int res
= 0, reschedule
= 0;
3902 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3904 assert(!(opts
->signoff
|| opts
->no_commit
||
3905 opts
->record_origin
|| opts
->edit
||
3906 opts
->committer_date_is_author_date
||
3907 opts
->ignore_date
));
3908 if (read_and_refresh_cache(r
, opts
))
3911 while (todo_list
->current
< todo_list
->nr
) {
3912 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
3913 const char *arg
= todo_item_get_arg(todo_list
, item
);
3916 if (save_todo(todo_list
, opts
))
3918 if (is_rebase_i(opts
)) {
3919 if (item
->command
!= TODO_COMMENT
) {
3920 FILE *f
= fopen(rebase_path_msgnum(), "w");
3922 todo_list
->done_nr
++;
3925 fprintf(f
, "%d\n", todo_list
->done_nr
);
3929 fprintf(stderr
, "Rebasing (%d/%d)%s",
3931 todo_list
->total_nr
,
3932 opts
->verbose
? "\n" : "\r");
3934 unlink(rebase_path_message());
3935 unlink(rebase_path_author_script());
3936 unlink(rebase_path_stopped_sha());
3937 unlink(rebase_path_amend());
3938 unlink(git_path_merge_head(r
));
3939 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
3941 if (item
->command
== TODO_BREAK
) {
3944 return stopped_at_head(r
);
3947 if (item
->command
<= TODO_SQUASH
) {
3948 if (is_rebase_i(opts
))
3949 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
3950 command_to_string(item
->command
), NULL
),
3952 res
= do_pick_commit(r
, item
->command
, item
->commit
,
3953 opts
, is_final_fixup(todo_list
),
3955 if (is_rebase_i(opts
) && res
< 0) {
3957 advise(_(rescheduled_advice
),
3958 get_item_line_length(todo_list
,
3959 todo_list
->current
),
3960 get_item_line(todo_list
,
3961 todo_list
->current
));
3962 todo_list
->current
--;
3963 if (save_todo(todo_list
, opts
))
3966 if (item
->command
== TODO_EDIT
) {
3967 struct commit
*commit
= item
->commit
;
3972 _("Stopped at %s... %.*s\n"),
3973 short_commit_name(commit
),
3974 item
->arg_len
, arg
);
3976 return error_with_patch(r
, commit
,
3977 arg
, item
->arg_len
, opts
, res
, !res
);
3979 if (is_rebase_i(opts
) && !res
)
3980 record_in_rewritten(&item
->commit
->object
.oid
,
3981 peek_command(todo_list
, 1));
3982 if (res
&& is_fixup(item
->command
)) {
3985 return error_failed_squash(r
, item
->commit
, opts
,
3986 item
->arg_len
, arg
);
3987 } else if (res
&& is_rebase_i(opts
) && item
->commit
) {
3989 struct object_id oid
;
3992 * If we are rewording and have either
3993 * fast-forwarded already, or are about to
3994 * create a new root commit, we want to amend,
3995 * otherwise we do not.
3997 if (item
->command
== TODO_REWORD
&&
3998 !get_oid("HEAD", &oid
) &&
3999 (oideq(&item
->commit
->object
.oid
, &oid
) ||
4000 (opts
->have_squash_onto
&&
4001 oideq(&opts
->squash_onto
, &oid
))))
4004 return res
| error_with_patch(r
, item
->commit
,
4005 arg
, item
->arg_len
, opts
,
4008 } else if (item
->command
== TODO_EXEC
) {
4009 char *end_of_arg
= (char *)(arg
+ item
->arg_len
);
4010 int saved
= *end_of_arg
;
4015 res
= do_exec(r
, arg
);
4016 *end_of_arg
= saved
;
4019 if (opts
->reschedule_failed_exec
)
4023 } else if (item
->command
== TODO_LABEL
) {
4024 if ((res
= do_label(r
, arg
, item
->arg_len
)))
4026 } else if (item
->command
== TODO_RESET
) {
4027 if ((res
= do_reset(r
, arg
, item
->arg_len
, opts
)))
4029 } else if (item
->command
== TODO_MERGE
) {
4030 if ((res
= do_merge(r
, item
->commit
,
4032 item
->flags
, opts
)) < 0)
4034 else if (item
->commit
)
4035 record_in_rewritten(&item
->commit
->object
.oid
,
4036 peek_command(todo_list
, 1));
4038 /* failed with merge conflicts */
4039 return error_with_patch(r
, item
->commit
,
4042 } else if (!is_noop(item
->command
))
4043 return error(_("unknown command %d"), item
->command
);
4046 advise(_(rescheduled_advice
),
4047 get_item_line_length(todo_list
,
4048 todo_list
->current
),
4049 get_item_line(todo_list
, todo_list
->current
));
4050 todo_list
->current
--;
4051 if (save_todo(todo_list
, opts
))
4054 return error_with_patch(r
,
4058 } else if (is_rebase_i(opts
) && check_todo
&& !res
) {
4061 if (stat(get_todo_path(opts
), &st
)) {
4062 res
= error_errno(_("could not stat '%s'"),
4063 get_todo_path(opts
));
4064 } else if (match_stat_data(&todo_list
->stat
, &st
)) {
4065 /* Reread the todo file if it has changed. */
4066 todo_list_release(todo_list
);
4067 if (read_populate_todo(r
, todo_list
, opts
))
4068 res
= -1; /* message was printed */
4069 /* `current` will be incremented below */
4070 todo_list
->current
= -1;
4074 todo_list
->current
++;
4079 if (is_rebase_i(opts
)) {
4080 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
4083 /* Stopped in the middle, as planned? */
4084 if (todo_list
->current
< todo_list
->nr
)
4087 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
4088 starts_with(head_ref
.buf
, "refs/")) {
4090 struct object_id head
, orig
;
4093 if (get_oid("HEAD", &head
)) {
4094 res
= error(_("cannot read HEAD"));
4096 strbuf_release(&head_ref
);
4097 strbuf_release(&buf
);
4100 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
4101 get_oid_hex(buf
.buf
, &orig
)) {
4102 res
= error(_("could not read orig-head"));
4103 goto cleanup_head_ref
;
4106 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
4107 res
= error(_("could not read 'onto'"));
4108 goto cleanup_head_ref
;
4110 msg
= reflog_message(opts
, "finish", "%s onto %s",
4111 head_ref
.buf
, buf
.buf
);
4112 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
4113 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
4114 res
= error(_("could not update %s"),
4116 goto cleanup_head_ref
;
4118 msg
= reflog_message(opts
, "finish", "returning to %s",
4120 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
4121 res
= error(_("could not update HEAD to %s"),
4123 goto cleanup_head_ref
;
4128 if (opts
->verbose
) {
4129 struct rev_info log_tree_opt
;
4130 struct object_id orig
, head
;
4132 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
4133 repo_init_revisions(r
, &log_tree_opt
, NULL
);
4134 log_tree_opt
.diff
= 1;
4135 log_tree_opt
.diffopt
.output_format
=
4136 DIFF_FORMAT_DIFFSTAT
;
4137 log_tree_opt
.disable_stdin
= 1;
4139 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
4140 !get_oid(buf
.buf
, &orig
) &&
4141 !get_oid("HEAD", &head
)) {
4142 diff_tree_oid(&orig
, &head
, "",
4143 &log_tree_opt
.diffopt
);
4144 log_tree_diff_flush(&log_tree_opt
);
4147 flush_rewritten_pending();
4148 if (!stat(rebase_path_rewritten_list(), &st
) &&
4150 struct child_process child
= CHILD_PROCESS_INIT
;
4151 const char *post_rewrite_hook
=
4152 find_hook("post-rewrite");
4154 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
4156 argv_array_push(&child
.args
, "notes");
4157 argv_array_push(&child
.args
, "copy");
4158 argv_array_push(&child
.args
, "--for-rewrite=rebase");
4159 /* we don't care if this copying failed */
4160 run_command(&child
);
4162 if (post_rewrite_hook
) {
4163 struct child_process hook
= CHILD_PROCESS_INIT
;
4165 hook
.in
= open(rebase_path_rewritten_list(),
4167 hook
.stdout_to_stderr
= 1;
4168 hook
.trace2_hook_name
= "post-rewrite";
4169 argv_array_push(&hook
.args
, post_rewrite_hook
);
4170 argv_array_push(&hook
.args
, "rebase");
4171 /* we don't care if this hook failed */
4175 apply_autostash(opts
);
4181 "Successfully rebased and updated %s.\n",
4185 strbuf_release(&buf
);
4186 strbuf_release(&head_ref
);
4190 * Sequence of picks finished successfully; cleanup by
4191 * removing the .git/sequencer directory
4193 return sequencer_remove_state(opts
);
4196 static int continue_single_pick(struct repository
*r
)
4198 const char *argv
[] = { "commit", NULL
};
4200 if (!file_exists(git_path_cherry_pick_head(r
)) &&
4201 !file_exists(git_path_revert_head(r
)))
4202 return error(_("no cherry-pick or revert in progress"));
4203 return run_command_v_opt(argv
, RUN_GIT_CMD
);
4206 static int commit_staged_changes(struct repository
*r
,
4207 struct replay_opts
*opts
,
4208 struct todo_list
*todo_list
)
4210 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
4211 unsigned int final_fixup
= 0, is_clean
;
4213 if (has_unstaged_changes(r
, 1))
4214 return error(_("cannot rebase: You have unstaged changes."));
4216 is_clean
= !has_uncommitted_changes(r
, 0);
4218 if (file_exists(rebase_path_amend())) {
4219 struct strbuf rev
= STRBUF_INIT
;
4220 struct object_id head
, to_amend
;
4222 if (get_oid("HEAD", &head
))
4223 return error(_("cannot amend non-existing commit"));
4224 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
4225 return error(_("invalid file: '%s'"), rebase_path_amend());
4226 if (get_oid_hex(rev
.buf
, &to_amend
))
4227 return error(_("invalid contents: '%s'"),
4228 rebase_path_amend());
4229 if (!is_clean
&& !oideq(&head
, &to_amend
))
4230 return error(_("\nYou have uncommitted changes in your "
4231 "working tree. Please, commit them\n"
4232 "first and then run 'git rebase "
4233 "--continue' again."));
4235 * When skipping a failed fixup/squash, we need to edit the
4236 * commit message, the current fixup list and count, and if it
4237 * was the last fixup/squash in the chain, we need to clean up
4238 * the commit message and if there was a squash, let the user
4241 if (!is_clean
|| !opts
->current_fixup_count
)
4242 ; /* this is not the final fixup */
4243 else if (!oideq(&head
, &to_amend
) ||
4244 !file_exists(rebase_path_stopped_sha())) {
4245 /* was a final fixup or squash done manually? */
4246 if (!is_fixup(peek_command(todo_list
, 0))) {
4247 unlink(rebase_path_fixup_msg());
4248 unlink(rebase_path_squash_msg());
4249 unlink(rebase_path_current_fixups());
4250 strbuf_reset(&opts
->current_fixups
);
4251 opts
->current_fixup_count
= 0;
4254 /* we are in a fixup/squash chain */
4255 const char *p
= opts
->current_fixups
.buf
;
4256 int len
= opts
->current_fixups
.len
;
4258 opts
->current_fixup_count
--;
4260 BUG("Incorrect current_fixups:\n%s", p
);
4261 while (len
&& p
[len
- 1] != '\n')
4263 strbuf_setlen(&opts
->current_fixups
, len
);
4264 if (write_message(p
, len
, rebase_path_current_fixups(),
4266 return error(_("could not write file: '%s'"),
4267 rebase_path_current_fixups());
4270 * If a fixup/squash in a fixup/squash chain failed, the
4271 * commit message is already correct, no need to commit
4274 * Only if it is the final command in the fixup/squash
4275 * chain, and only if the chain is longer than a single
4276 * fixup/squash command (which was just skipped), do we
4277 * actually need to re-commit with a cleaned up commit
4280 if (opts
->current_fixup_count
> 0 &&
4281 !is_fixup(peek_command(todo_list
, 0))) {
4284 * If there was not a single "squash" in the
4285 * chain, we only need to clean up the commit
4286 * message, no need to bother the user with
4287 * opening the commit message in the editor.
4289 if (!starts_with(p
, "squash ") &&
4290 !strstr(p
, "\nsquash "))
4291 flags
= (flags
& ~EDIT_MSG
) | CLEANUP_MSG
;
4292 } else if (is_fixup(peek_command(todo_list
, 0))) {
4294 * We need to update the squash message to skip
4295 * the latest commit message.
4297 struct commit
*commit
;
4298 const char *path
= rebase_path_squash_msg();
4299 const char *encoding
= get_commit_output_encoding();
4301 if (parse_head(r
, &commit
) ||
4302 !(p
= logmsg_reencode(commit
, NULL
, encoding
)) ||
4303 write_message(p
, strlen(p
), path
, 0)) {
4304 unuse_commit_buffer(commit
, p
);
4305 return error(_("could not write file: "
4308 unuse_commit_buffer(commit
, p
);
4312 strbuf_release(&rev
);
4317 const char *cherry_pick_head
= git_path_cherry_pick_head(r
);
4319 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
4320 return error(_("could not remove CHERRY_PICK_HEAD"));
4325 if (run_git_commit(r
, final_fixup
? NULL
: rebase_path_message(),
4327 return error(_("could not commit staged changes."));
4328 unlink(rebase_path_amend());
4329 unlink(git_path_merge_head(r
));
4331 unlink(rebase_path_fixup_msg());
4332 unlink(rebase_path_squash_msg());
4334 if (opts
->current_fixup_count
> 0) {
4336 * Whether final fixup or not, we just cleaned up the commit
4339 unlink(rebase_path_current_fixups());
4340 strbuf_reset(&opts
->current_fixups
);
4341 opts
->current_fixup_count
= 0;
4346 int sequencer_continue(struct repository
*r
, struct replay_opts
*opts
)
4348 struct todo_list todo_list
= TODO_LIST_INIT
;
4351 if (read_and_refresh_cache(r
, opts
))
4354 if (read_populate_opts(opts
))
4356 if (is_rebase_i(opts
)) {
4357 if ((res
= read_populate_todo(r
, &todo_list
, opts
)))
4358 goto release_todo_list
;
4359 if (commit_staged_changes(r
, opts
, &todo_list
)) {
4361 goto release_todo_list
;
4363 } else if (!file_exists(get_todo_path(opts
)))
4364 return continue_single_pick(r
);
4365 else if ((res
= read_populate_todo(r
, &todo_list
, opts
)))
4366 goto release_todo_list
;
4368 if (!is_rebase_i(opts
)) {
4369 /* Verify that the conflict has been resolved */
4370 if (file_exists(git_path_cherry_pick_head(r
)) ||
4371 file_exists(git_path_revert_head(r
))) {
4372 res
= continue_single_pick(r
);
4374 goto release_todo_list
;
4376 if (index_differs_from(r
, "HEAD", NULL
, 0)) {
4377 res
= error_dirty_index(r
, opts
);
4378 goto release_todo_list
;
4380 todo_list
.current
++;
4381 } else if (file_exists(rebase_path_stopped_sha())) {
4382 struct strbuf buf
= STRBUF_INIT
;
4383 struct object_id oid
;
4385 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
4386 !get_oid_committish(buf
.buf
, &oid
))
4387 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
4388 strbuf_release(&buf
);
4391 res
= pick_commits(r
, &todo_list
, opts
);
4393 todo_list_release(&todo_list
);
4397 static int single_pick(struct repository
*r
,
4398 struct commit
*cmit
,
4399 struct replay_opts
*opts
)
4403 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
4404 return do_pick_commit(r
, opts
->action
== REPLAY_PICK
?
4405 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0,
4409 int sequencer_pick_revisions(struct repository
*r
,
4410 struct replay_opts
*opts
)
4412 struct todo_list todo_list
= TODO_LIST_INIT
;
4413 struct object_id oid
;
4417 if (read_and_refresh_cache(r
, opts
))
4420 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
4421 struct object_id oid
;
4422 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
4424 /* This happens when using --stdin. */
4428 if (!get_oid(name
, &oid
)) {
4429 if (!lookup_commit_reference_gently(r
, &oid
, 1)) {
4430 enum object_type type
= oid_object_info(r
,
4433 return error(_("%s: can't cherry-pick a %s"),
4434 name
, type_name(type
));
4437 return error(_("%s: bad revision"), name
);
4441 * If we were called as "git cherry-pick <commit>", just
4442 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
4443 * REVERT_HEAD, and don't touch the sequencer state.
4444 * This means it is possible to cherry-pick in the middle
4445 * of a cherry-pick sequence.
4447 if (opts
->revs
->cmdline
.nr
== 1 &&
4448 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
4449 opts
->revs
->no_walk
&&
4450 !opts
->revs
->cmdline
.rev
->flags
) {
4451 struct commit
*cmit
;
4452 if (prepare_revision_walk(opts
->revs
))
4453 return error(_("revision walk setup failed"));
4454 cmit
= get_revision(opts
->revs
);
4456 return error(_("empty commit set passed"));
4457 if (get_revision(opts
->revs
))
4458 BUG("unexpected extra commit from walk");
4459 return single_pick(r
, cmit
, opts
);
4463 * Start a new cherry-pick/ revert sequence; but
4464 * first, make sure that an existing one isn't in
4468 if (walk_revs_populate_todo(&todo_list
, opts
) ||
4469 create_seq_dir(r
) < 0)
4471 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
4472 return error(_("can't revert as initial commit"));
4473 if (save_head(oid_to_hex(&oid
)))
4475 if (save_opts(opts
))
4477 update_abort_safety_file();
4478 res
= pick_commits(r
, &todo_list
, opts
);
4479 todo_list_release(&todo_list
);
4483 void append_signoff(struct strbuf
*msgbuf
, size_t ignore_footer
, unsigned flag
)
4485 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
4486 struct strbuf sob
= STRBUF_INIT
;
4489 strbuf_addstr(&sob
, sign_off_header
);
4490 strbuf_addstr(&sob
, fmt_name(WANT_COMMITTER_IDENT
));
4491 strbuf_addch(&sob
, '\n');
4494 strbuf_complete_line(msgbuf
);
4497 * If the whole message buffer is equal to the sob, pretend that we
4498 * found a conforming footer with a matching sob
4500 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
4501 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
4504 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
4507 const char *append_newlines
= NULL
;
4508 size_t len
= msgbuf
->len
- ignore_footer
;
4512 * The buffer is completely empty. Leave foom for
4513 * the title and body to be filled in by the user.
4515 append_newlines
= "\n\n";
4516 } else if (len
== 1) {
4518 * Buffer contains a single newline. Add another
4519 * so that we leave room for the title and body.
4521 append_newlines
= "\n";
4522 } else if (msgbuf
->buf
[len
- 2] != '\n') {
4524 * Buffer ends with a single newline. Add another
4525 * so that there is an empty line between the message
4528 append_newlines
= "\n";
4529 } /* else, the buffer already ends with two newlines. */
4531 if (append_newlines
)
4532 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
4533 append_newlines
, strlen(append_newlines
));
4536 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
4537 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
4540 strbuf_release(&sob
);
4543 struct labels_entry
{
4544 struct hashmap_entry entry
;
4545 char label
[FLEX_ARRAY
];
4548 static int labels_cmp(const void *fndata
, const struct hashmap_entry
*eptr
,
4549 const struct hashmap_entry
*entry_or_key
, const void *key
)
4551 const struct labels_entry
*a
, *b
;
4553 a
= container_of(eptr
, const struct labels_entry
, entry
);
4554 b
= container_of(entry_or_key
, const struct labels_entry
, entry
);
4556 return key
? strcmp(a
->label
, key
) : strcmp(a
->label
, b
->label
);
4559 struct string_entry
{
4560 struct oidmap_entry entry
;
4561 char string
[FLEX_ARRAY
];
4564 struct label_state
{
4565 struct oidmap commit2label
;
4566 struct hashmap labels
;
4570 static const char *label_oid(struct object_id
*oid
, const char *label
,
4571 struct label_state
*state
)
4573 struct labels_entry
*labels_entry
;
4574 struct string_entry
*string_entry
;
4575 struct object_id dummy
;
4578 string_entry
= oidmap_get(&state
->commit2label
, oid
);
4580 return string_entry
->string
;
4583 * For "uninteresting" commits, i.e. commits that are not to be
4584 * rebased, and which can therefore not be labeled, we use a unique
4585 * abbreviation of the commit name. This is slightly more complicated
4586 * than calling find_unique_abbrev() because we also need to make
4587 * sure that the abbreviation does not conflict with any other
4590 * We disallow "interesting" commits to be labeled by a string that
4591 * is a valid full-length hash, to ensure that we always can find an
4592 * abbreviation for any uninteresting commit's names that does not
4593 * clash with any other label.
4595 strbuf_reset(&state
->buf
);
4599 strbuf_grow(&state
->buf
, GIT_MAX_HEXSZ
);
4600 label
= p
= state
->buf
.buf
;
4602 find_unique_abbrev_r(p
, oid
, default_abbrev
);
4605 * We may need to extend the abbreviated hash so that there is
4606 * no conflicting label.
4608 if (hashmap_get_from_hash(&state
->labels
, strihash(p
), p
)) {
4609 size_t i
= strlen(p
) + 1;
4611 oid_to_hex_r(p
, oid
);
4612 for (; i
< the_hash_algo
->hexsz
; i
++) {
4615 if (!hashmap_get_from_hash(&state
->labels
,
4622 struct strbuf
*buf
= &state
->buf
;
4625 * Sanitize labels by replacing non-alpha-numeric characters
4626 * (including white-space ones) by dashes, as they might be
4627 * illegal in file names (and hence in ref names).
4629 * Note that we retain non-ASCII UTF-8 characters (identified
4630 * via the most significant bit). They should be all acceptable
4631 * in file names. We do not validate the UTF-8 here, that's not
4632 * the job of this function.
4634 for (; *label
; label
++)
4635 if ((*label
& 0x80) || isalnum(*label
))
4636 strbuf_addch(buf
, *label
);
4637 /* avoid leading dash and double-dashes */
4638 else if (buf
->len
&& buf
->buf
[buf
->len
- 1] != '-')
4639 strbuf_addch(buf
, '-');
4641 strbuf_addstr(buf
, "rev-");
4642 strbuf_add_unique_abbrev(buf
, oid
, default_abbrev
);
4646 if ((buf
->len
== the_hash_algo
->hexsz
&&
4647 !get_oid_hex(label
, &dummy
)) ||
4648 (buf
->len
== 1 && *label
== '#') ||
4649 hashmap_get_from_hash(&state
->labels
,
4650 strihash(label
), label
)) {
4652 * If the label already exists, or if the label is a
4653 * valid full OID, or the label is a '#' (which we use
4654 * as a separator between merge heads and oneline), we
4655 * append a dash and a number to make it unique.
4657 size_t len
= buf
->len
;
4659 for (i
= 2; ; i
++) {
4660 strbuf_setlen(buf
, len
);
4661 strbuf_addf(buf
, "-%d", i
);
4662 if (!hashmap_get_from_hash(&state
->labels
,
4672 FLEX_ALLOC_STR(labels_entry
, label
, label
);
4673 hashmap_entry_init(&labels_entry
->entry
, strihash(label
));
4674 hashmap_add(&state
->labels
, &labels_entry
->entry
);
4676 FLEX_ALLOC_STR(string_entry
, string
, label
);
4677 oidcpy(&string_entry
->entry
.oid
, oid
);
4678 oidmap_put(&state
->commit2label
, string_entry
);
4680 return string_entry
->string
;
4683 static int make_script_with_merges(struct pretty_print_context
*pp
,
4684 struct rev_info
*revs
, struct strbuf
*out
,
4687 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
4688 int rebase_cousins
= flags
& TODO_LIST_REBASE_COUSINS
;
4689 int root_with_onto
= flags
& TODO_LIST_ROOT_WITH_ONTO
;
4690 struct strbuf buf
= STRBUF_INIT
, oneline
= STRBUF_INIT
;
4691 struct strbuf label
= STRBUF_INIT
;
4692 struct commit_list
*commits
= NULL
, **tail
= &commits
, *iter
;
4693 struct commit_list
*tips
= NULL
, **tips_tail
= &tips
;
4694 struct commit
*commit
;
4695 struct oidmap commit2todo
= OIDMAP_INIT
;
4696 struct string_entry
*entry
;
4697 struct oidset interesting
= OIDSET_INIT
, child_seen
= OIDSET_INIT
,
4698 shown
= OIDSET_INIT
;
4699 struct label_state state
= { OIDMAP_INIT
, { NULL
}, STRBUF_INIT
};
4701 int abbr
= flags
& TODO_LIST_ABBREVIATE_CMDS
;
4702 const char *cmd_pick
= abbr
? "p" : "pick",
4703 *cmd_label
= abbr
? "l" : "label",
4704 *cmd_reset
= abbr
? "t" : "reset",
4705 *cmd_merge
= abbr
? "m" : "merge";
4707 oidmap_init(&commit2todo
, 0);
4708 oidmap_init(&state
.commit2label
, 0);
4709 hashmap_init(&state
.labels
, labels_cmp
, NULL
, 0);
4710 strbuf_init(&state
.buf
, 32);
4712 if (revs
->cmdline
.nr
&& (revs
->cmdline
.rev
[0].flags
& BOTTOM
)) {
4713 struct labels_entry
*onto_label_entry
;
4714 struct object_id
*oid
= &revs
->cmdline
.rev
[0].item
->oid
;
4715 FLEX_ALLOC_STR(entry
, string
, "onto");
4716 oidcpy(&entry
->entry
.oid
, oid
);
4717 oidmap_put(&state
.commit2label
, entry
);
4719 FLEX_ALLOC_STR(onto_label_entry
, label
, "onto");
4720 hashmap_entry_init(&onto_label_entry
->entry
, strihash("onto"));
4721 hashmap_add(&state
.labels
, &onto_label_entry
->entry
);
4726 * - get onelines for all commits
4727 * - gather all branch tips (i.e. 2nd or later parents of merges)
4728 * - label all branch tips
4730 while ((commit
= get_revision(revs
))) {
4731 struct commit_list
*to_merge
;
4732 const char *p1
, *p2
;
4733 struct object_id
*oid
;
4736 tail
= &commit_list_insert(commit
, tail
)->next
;
4737 oidset_insert(&interesting
, &commit
->object
.oid
);
4739 is_empty
= is_original_commit_empty(commit
);
4740 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
4743 strbuf_reset(&oneline
);
4744 pretty_print_commit(pp
, commit
, &oneline
);
4746 to_merge
= commit
->parents
? commit
->parents
->next
: NULL
;
4748 /* non-merge commit: easy case */
4750 if (!keep_empty
&& is_empty
)
4751 strbuf_addf(&buf
, "%c ", comment_line_char
);
4752 strbuf_addf(&buf
, "%s %s %s", cmd_pick
,
4753 oid_to_hex(&commit
->object
.oid
),
4756 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
4757 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
4758 oidmap_put(&commit2todo
, entry
);
4763 /* Create a label */
4764 strbuf_reset(&label
);
4765 if (skip_prefix(oneline
.buf
, "Merge ", &p1
) &&
4766 (p1
= strchr(p1
, '\'')) &&
4767 (p2
= strchr(++p1
, '\'')))
4768 strbuf_add(&label
, p1
, p2
- p1
);
4769 else if (skip_prefix(oneline
.buf
, "Merge pull request ",
4771 (p1
= strstr(p1
, " from ")))
4772 strbuf_addstr(&label
, p1
+ strlen(" from "));
4774 strbuf_addbuf(&label
, &oneline
);
4777 strbuf_addf(&buf
, "%s -C %s",
4778 cmd_merge
, oid_to_hex(&commit
->object
.oid
));
4780 /* label the tips of merged branches */
4781 for (; to_merge
; to_merge
= to_merge
->next
) {
4782 oid
= &to_merge
->item
->object
.oid
;
4783 strbuf_addch(&buf
, ' ');
4785 if (!oidset_contains(&interesting
, oid
)) {
4786 strbuf_addstr(&buf
, label_oid(oid
, NULL
,
4791 tips_tail
= &commit_list_insert(to_merge
->item
,
4794 strbuf_addstr(&buf
, label_oid(oid
, label
.buf
, &state
));
4796 strbuf_addf(&buf
, " # %s", oneline
.buf
);
4798 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
4799 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
4800 oidmap_put(&commit2todo
, entry
);
4805 * - label branch points
4806 * - add HEAD to the branch tips
4808 for (iter
= commits
; iter
; iter
= iter
->next
) {
4809 struct commit_list
*parent
= iter
->item
->parents
;
4810 for (; parent
; parent
= parent
->next
) {
4811 struct object_id
*oid
= &parent
->item
->object
.oid
;
4812 if (!oidset_contains(&interesting
, oid
))
4814 if (oidset_insert(&child_seen
, oid
))
4815 label_oid(oid
, "branch-point", &state
);
4818 /* Add HEAD as implicit "tip of branch" */
4820 tips_tail
= &commit_list_insert(iter
->item
,
4825 * Third phase: output the todo list. This is a bit tricky, as we
4826 * want to avoid jumping back and forth between revisions. To
4827 * accomplish that goal, we walk backwards from the branch tips,
4828 * gathering commits not yet shown, reversing the list on the fly,
4829 * then outputting that list (labeling revisions as needed).
4831 strbuf_addf(out
, "%s onto\n", cmd_label
);
4832 for (iter
= tips
; iter
; iter
= iter
->next
) {
4833 struct commit_list
*list
= NULL
, *iter2
;
4835 commit
= iter
->item
;
4836 if (oidset_contains(&shown
, &commit
->object
.oid
))
4838 entry
= oidmap_get(&state
.commit2label
, &commit
->object
.oid
);
4841 strbuf_addf(out
, "\n%c Branch %s\n", comment_line_char
, entry
->string
);
4843 strbuf_addch(out
, '\n');
4845 while (oidset_contains(&interesting
, &commit
->object
.oid
) &&
4846 !oidset_contains(&shown
, &commit
->object
.oid
)) {
4847 commit_list_insert(commit
, &list
);
4848 if (!commit
->parents
) {
4852 commit
= commit
->parents
->item
;
4856 strbuf_addf(out
, "%s %s\n", cmd_reset
,
4857 rebase_cousins
|| root_with_onto
?
4858 "onto" : "[new root]");
4860 const char *to
= NULL
;
4862 entry
= oidmap_get(&state
.commit2label
,
4863 &commit
->object
.oid
);
4866 else if (!rebase_cousins
)
4867 to
= label_oid(&commit
->object
.oid
, NULL
,
4870 if (!to
|| !strcmp(to
, "onto"))
4871 strbuf_addf(out
, "%s onto\n", cmd_reset
);
4873 strbuf_reset(&oneline
);
4874 pretty_print_commit(pp
, commit
, &oneline
);
4875 strbuf_addf(out
, "%s %s # %s\n",
4876 cmd_reset
, to
, oneline
.buf
);
4880 for (iter2
= list
; iter2
; iter2
= iter2
->next
) {
4881 struct object_id
*oid
= &iter2
->item
->object
.oid
;
4882 entry
= oidmap_get(&commit2todo
, oid
);
4883 /* only show if not already upstream */
4885 strbuf_addf(out
, "%s\n", entry
->string
);
4886 entry
= oidmap_get(&state
.commit2label
, oid
);
4888 strbuf_addf(out
, "%s %s\n",
4889 cmd_label
, entry
->string
);
4890 oidset_insert(&shown
, oid
);
4893 free_commit_list(list
);
4896 free_commit_list(commits
);
4897 free_commit_list(tips
);
4899 strbuf_release(&label
);
4900 strbuf_release(&oneline
);
4901 strbuf_release(&buf
);
4903 oidmap_free(&commit2todo
, 1);
4904 oidmap_free(&state
.commit2label
, 1);
4905 hashmap_free_entries(&state
.labels
, struct labels_entry
, entry
);
4906 strbuf_release(&state
.buf
);
4911 int sequencer_make_script(struct repository
*r
, struct strbuf
*out
, int argc
,
4912 const char **argv
, unsigned flags
)
4914 char *format
= NULL
;
4915 struct pretty_print_context pp
= {0};
4916 struct rev_info revs
;
4917 struct commit
*commit
;
4918 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
4919 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
4920 int rebase_merges
= flags
& TODO_LIST_REBASE_MERGES
;
4922 repo_init_revisions(r
, &revs
, NULL
);
4923 revs
.verbose_header
= 1;
4925 revs
.max_parents
= 1;
4926 revs
.cherry_mark
= 1;
4929 revs
.right_only
= 1;
4930 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
4931 revs
.topo_order
= 1;
4933 revs
.pretty_given
= 1;
4934 git_config_get_string("rebase.instructionFormat", &format
);
4935 if (!format
|| !*format
) {
4937 format
= xstrdup("%s");
4939 get_commit_format(format
, &revs
);
4941 pp
.fmt
= revs
.commit_format
;
4942 pp
.output_encoding
= get_log_output_encoding();
4944 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
4945 return error(_("make_script: unhandled options"));
4947 if (prepare_revision_walk(&revs
) < 0)
4948 return error(_("make_script: error preparing revisions"));
4951 return make_script_with_merges(&pp
, &revs
, out
, flags
);
4953 while ((commit
= get_revision(&revs
))) {
4954 int is_empty
= is_original_commit_empty(commit
);
4956 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
4958 if (!keep_empty
&& is_empty
)
4959 strbuf_addf(out
, "%c ", comment_line_char
);
4960 strbuf_addf(out
, "%s %s ", insn
,
4961 oid_to_hex(&commit
->object
.oid
));
4962 pretty_print_commit(&pp
, commit
, out
);
4963 strbuf_addch(out
, '\n');
4969 * Add commands after pick and (series of) squash/fixup commands
4972 void todo_list_add_exec_commands(struct todo_list
*todo_list
,
4973 struct string_list
*commands
)
4975 struct strbuf
*buf
= &todo_list
->buf
;
4976 size_t base_offset
= buf
->len
;
4977 int i
, insert
, nr
= 0, alloc
= 0;
4978 struct todo_item
*items
= NULL
, *base_items
= NULL
;
4980 base_items
= xcalloc(commands
->nr
, sizeof(struct todo_item
));
4981 for (i
= 0; i
< commands
->nr
; i
++) {
4982 size_t command_len
= strlen(commands
->items
[i
].string
);
4984 strbuf_addstr(buf
, commands
->items
[i
].string
);
4985 strbuf_addch(buf
, '\n');
4987 base_items
[i
].command
= TODO_EXEC
;
4988 base_items
[i
].offset_in_buf
= base_offset
;
4989 base_items
[i
].arg_offset
= base_offset
+ strlen("exec ");
4990 base_items
[i
].arg_len
= command_len
- strlen("exec ");
4992 base_offset
+= command_len
+ 1;
4996 * Insert <commands> after every pick. Here, fixup/squash chains
4997 * are considered part of the pick, so we insert the commands *after*
4998 * those chains if there are any.
5000 * As we insert the exec commands immediately after rearranging
5001 * any fixups and before the user edits the list, a fixup chain
5002 * can never contain comments (any comments are empty picks that
5003 * have been commented out because the user did not specify
5004 * --keep-empty). So, it is safe to insert an exec command
5005 * without looking at the command following a comment.
5008 for (i
= 0; i
< todo_list
->nr
; i
++) {
5009 enum todo_command command
= todo_list
->items
[i
].command
;
5010 if (insert
&& !is_fixup(command
)) {
5011 ALLOC_GROW(items
, nr
+ commands
->nr
, alloc
);
5012 COPY_ARRAY(items
+ nr
, base_items
, commands
->nr
);
5018 ALLOC_GROW(items
, nr
+ 1, alloc
);
5019 items
[nr
++] = todo_list
->items
[i
];
5021 if (command
== TODO_PICK
|| command
== TODO_MERGE
)
5025 /* insert or append final <commands> */
5026 if (insert
|| nr
== todo_list
->nr
) {
5027 ALLOC_GROW(items
, nr
+ commands
->nr
, alloc
);
5028 COPY_ARRAY(items
+ nr
, base_items
, commands
->nr
);
5033 FREE_AND_NULL(todo_list
->items
);
5034 todo_list
->items
= items
;
5036 todo_list
->alloc
= alloc
;
5039 static void todo_list_to_strbuf(struct repository
*r
, struct todo_list
*todo_list
,
5040 struct strbuf
*buf
, int num
, unsigned flags
)
5042 struct todo_item
*item
;
5043 int i
, max
= todo_list
->nr
;
5045 if (num
> 0 && num
< max
)
5048 for (item
= todo_list
->items
, i
= 0; i
< max
; i
++, item
++) {
5049 /* if the item is not a command write it and continue */
5050 if (item
->command
>= TODO_COMMENT
) {
5051 strbuf_addf(buf
, "%.*s\n", item
->arg_len
,
5052 todo_item_get_arg(todo_list
, item
));
5056 /* add command to the buffer */
5057 if (flags
& TODO_LIST_ABBREVIATE_CMDS
)
5058 strbuf_addch(buf
, command_to_char(item
->command
));
5060 strbuf_addstr(buf
, command_to_string(item
->command
));
5064 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
5065 short_commit_name(item
->commit
) :
5066 oid_to_hex(&item
->commit
->object
.oid
);
5068 if (item
->command
== TODO_MERGE
) {
5069 if (item
->flags
& TODO_EDIT_MERGE_MSG
)
5070 strbuf_addstr(buf
, " -c");
5072 strbuf_addstr(buf
, " -C");
5075 strbuf_addf(buf
, " %s", oid
);
5078 /* add all the rest */
5080 strbuf_addch(buf
, '\n');
5082 strbuf_addf(buf
, " %.*s\n", item
->arg_len
,
5083 todo_item_get_arg(todo_list
, item
));
5087 int todo_list_write_to_file(struct repository
*r
, struct todo_list
*todo_list
,
5088 const char *file
, const char *shortrevisions
,
5089 const char *shortonto
, int num
, unsigned flags
)
5092 struct strbuf buf
= STRBUF_INIT
;
5094 todo_list_to_strbuf(r
, todo_list
, &buf
, num
, flags
);
5095 if (flags
& TODO_LIST_APPEND_TODO_HELP
)
5096 append_todo_help(flags
& TODO_LIST_KEEP_EMPTY
, count_commands(todo_list
),
5097 shortrevisions
, shortonto
, &buf
);
5099 res
= write_message(buf
.buf
, buf
.len
, file
, 0);
5100 strbuf_release(&buf
);
5105 static const char edit_todo_list_advice
[] =
5106 N_("You can fix this with 'git rebase --edit-todo' "
5107 "and then run 'git rebase --continue'.\n"
5108 "Or you can abort the rebase with 'git rebase"
5111 int check_todo_list_from_file(struct repository
*r
)
5113 struct todo_list old_todo
= TODO_LIST_INIT
, new_todo
= TODO_LIST_INIT
;
5116 if (strbuf_read_file_or_whine(&new_todo
.buf
, rebase_path_todo()) < 0) {
5121 if (strbuf_read_file_or_whine(&old_todo
.buf
, rebase_path_todo_backup()) < 0) {
5126 res
= todo_list_parse_insn_buffer(r
, old_todo
.buf
.buf
, &old_todo
);
5128 res
= todo_list_parse_insn_buffer(r
, new_todo
.buf
.buf
, &new_todo
);
5130 res
= todo_list_check(&old_todo
, &new_todo
);
5132 fprintf(stderr
, _(edit_todo_list_advice
));
5134 todo_list_release(&old_todo
);
5135 todo_list_release(&new_todo
);
5140 /* skip picking commits whose parents are unchanged */
5141 static int skip_unnecessary_picks(struct repository
*r
,
5142 struct todo_list
*todo_list
,
5143 struct object_id
*base_oid
)
5145 struct object_id
*parent_oid
;
5148 for (i
= 0; i
< todo_list
->nr
; i
++) {
5149 struct todo_item
*item
= todo_list
->items
+ i
;
5151 if (item
->command
>= TODO_NOOP
)
5153 if (item
->command
!= TODO_PICK
)
5155 if (parse_commit(item
->commit
)) {
5156 return error(_("could not parse commit '%s'"),
5157 oid_to_hex(&item
->commit
->object
.oid
));
5159 if (!item
->commit
->parents
)
5160 break; /* root commit */
5161 if (item
->commit
->parents
->next
)
5162 break; /* merge commit */
5163 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
5164 if (!oideq(parent_oid
, base_oid
))
5166 oidcpy(base_oid
, &item
->commit
->object
.oid
);
5169 const char *done_path
= rebase_path_done();
5171 if (todo_list_write_to_file(r
, todo_list
, done_path
, NULL
, NULL
, i
, 0)) {
5172 error_errno(_("could not write to '%s'"), done_path
);
5176 MOVE_ARRAY(todo_list
->items
, todo_list
->items
+ i
, todo_list
->nr
- i
);
5178 todo_list
->current
= 0;
5180 if (is_fixup(peek_command(todo_list
, 0)))
5181 record_in_rewritten(base_oid
, peek_command(todo_list
, 0));
5187 int complete_action(struct repository
*r
, struct replay_opts
*opts
, unsigned flags
,
5188 const char *shortrevisions
, const char *onto_name
,
5189 struct commit
*onto
, const char *orig_head
,
5190 struct string_list
*commands
, unsigned autosquash
,
5191 struct todo_list
*todo_list
)
5193 const char *shortonto
, *todo_file
= rebase_path_todo();
5194 struct todo_list new_todo
= TODO_LIST_INIT
;
5195 struct strbuf
*buf
= &todo_list
->buf
;
5196 struct object_id oid
= onto
->object
.oid
;
5199 shortonto
= find_unique_abbrev(&oid
, DEFAULT_ABBREV
);
5201 if (buf
->len
== 0) {
5202 struct todo_item
*item
= append_new_todo(todo_list
);
5203 item
->command
= TODO_NOOP
;
5204 item
->commit
= NULL
;
5205 item
->arg_len
= item
->arg_offset
= item
->flags
= item
->offset_in_buf
= 0;
5208 if (autosquash
&& todo_list_rearrange_squash(todo_list
))
5212 todo_list_add_exec_commands(todo_list
, commands
);
5214 if (count_commands(todo_list
) == 0) {
5215 apply_autostash(opts
);
5216 sequencer_remove_state(opts
);
5218 return error(_("nothing to do"));
5221 res
= edit_todo_list(r
, todo_list
, &new_todo
, shortrevisions
,
5225 else if (res
== -2) {
5226 apply_autostash(opts
);
5227 sequencer_remove_state(opts
);
5230 } else if (res
== -3) {
5231 apply_autostash(opts
);
5232 sequencer_remove_state(opts
);
5233 todo_list_release(&new_todo
);
5235 return error(_("nothing to do"));
5238 if (todo_list_parse_insn_buffer(r
, new_todo
.buf
.buf
, &new_todo
) ||
5239 todo_list_check(todo_list
, &new_todo
)) {
5240 fprintf(stderr
, _(edit_todo_list_advice
));
5241 checkout_onto(r
, opts
, onto_name
, &onto
->object
.oid
, orig_head
);
5242 todo_list_release(&new_todo
);
5247 if (opts
->allow_ff
&& skip_unnecessary_picks(r
, &new_todo
, &oid
)) {
5248 todo_list_release(&new_todo
);
5249 return error(_("could not skip unnecessary pick commands"));
5252 if (todo_list_write_to_file(r
, &new_todo
, todo_file
, NULL
, NULL
, -1,
5253 flags
& ~(TODO_LIST_SHORTEN_IDS
))) {
5254 todo_list_release(&new_todo
);
5255 return error_errno(_("could not write '%s'"), todo_file
);
5258 todo_list_release(&new_todo
);
5260 if (checkout_onto(r
, opts
, onto_name
, &oid
, orig_head
))
5263 if (require_clean_work_tree(r
, "rebase", "", 1, 1))
5266 return sequencer_continue(r
, opts
);
5269 struct subject2item_entry
{
5270 struct hashmap_entry entry
;
5272 char subject
[FLEX_ARRAY
];
5275 static int subject2item_cmp(const void *fndata
,
5276 const struct hashmap_entry
*eptr
,
5277 const struct hashmap_entry
*entry_or_key
,
5280 const struct subject2item_entry
*a
, *b
;
5282 a
= container_of(eptr
, const struct subject2item_entry
, entry
);
5283 b
= container_of(entry_or_key
, const struct subject2item_entry
, entry
);
5285 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
5288 define_commit_slab(commit_todo_item
, struct todo_item
*);
5291 * Rearrange the todo list that has both "pick commit-id msg" and "pick
5292 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
5293 * after the former, and change "pick" to "fixup"/"squash".
5295 * Note that if the config has specified a custom instruction format, each log
5296 * message will have to be retrieved from the commit (as the oneline in the
5297 * script cannot be trusted) in order to normalize the autosquash arrangement.
5299 int todo_list_rearrange_squash(struct todo_list
*todo_list
)
5301 struct hashmap subject2item
;
5302 int rearranged
= 0, *next
, *tail
, i
, nr
= 0, alloc
= 0;
5304 struct commit_todo_item commit_todo
;
5305 struct todo_item
*items
= NULL
;
5307 init_commit_todo_item(&commit_todo
);
5309 * The hashmap maps onelines to the respective todo list index.
5311 * If any items need to be rearranged, the next[i] value will indicate
5312 * which item was moved directly after the i'th.
5314 * In that case, last[i] will indicate the index of the latest item to
5315 * be moved to appear after the i'th.
5317 hashmap_init(&subject2item
, subject2item_cmp
, NULL
, todo_list
->nr
);
5318 ALLOC_ARRAY(next
, todo_list
->nr
);
5319 ALLOC_ARRAY(tail
, todo_list
->nr
);
5320 ALLOC_ARRAY(subjects
, todo_list
->nr
);
5321 for (i
= 0; i
< todo_list
->nr
; i
++) {
5322 struct strbuf buf
= STRBUF_INIT
;
5323 struct todo_item
*item
= todo_list
->items
+ i
;
5324 const char *commit_buffer
, *subject
, *p
;
5327 struct subject2item_entry
*entry
;
5329 next
[i
] = tail
[i
] = -1;
5330 if (!item
->commit
|| item
->command
== TODO_DROP
) {
5335 if (is_fixup(item
->command
)) {
5336 clear_commit_todo_item(&commit_todo
);
5337 return error(_("the script was already rearranged."));
5340 *commit_todo_item_at(&commit_todo
, item
->commit
) = item
;
5342 parse_commit(item
->commit
);
5343 commit_buffer
= logmsg_reencode(item
->commit
, NULL
, "UTF-8");
5344 find_commit_subject(commit_buffer
, &subject
);
5345 format_subject(&buf
, subject
, " ");
5346 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
5347 unuse_commit_buffer(item
->commit
, commit_buffer
);
5348 if ((skip_prefix(subject
, "fixup! ", &p
) ||
5349 skip_prefix(subject
, "squash! ", &p
))) {
5350 struct commit
*commit2
;
5355 if (!skip_prefix(p
, "fixup! ", &p
) &&
5356 !skip_prefix(p
, "squash! ", &p
))
5360 entry
= hashmap_get_entry_from_hash(&subject2item
,
5362 struct subject2item_entry
,
5365 /* found by title */
5367 else if (!strchr(p
, ' ') &&
5369 lookup_commit_reference_by_name(p
)) &&
5370 *commit_todo_item_at(&commit_todo
, commit2
))
5371 /* found by commit name */
5372 i2
= *commit_todo_item_at(&commit_todo
, commit2
)
5375 /* copy can be a prefix of the commit subject */
5376 for (i2
= 0; i2
< i
; i2
++)
5378 starts_with(subjects
[i2
], p
))
5386 todo_list
->items
[i
].command
=
5387 starts_with(subject
, "fixup!") ?
5388 TODO_FIXUP
: TODO_SQUASH
;
5394 } else if (!hashmap_get_from_hash(&subject2item
,
5395 strhash(subject
), subject
)) {
5396 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
5398 hashmap_entry_init(&entry
->entry
,
5399 strhash(entry
->subject
));
5400 hashmap_put(&subject2item
, &entry
->entry
);
5405 for (i
= 0; i
< todo_list
->nr
; i
++) {
5406 enum todo_command command
= todo_list
->items
[i
].command
;
5410 * Initially, all commands are 'pick's. If it is a
5411 * fixup or a squash now, we have rearranged it.
5413 if (is_fixup(command
))
5417 ALLOC_GROW(items
, nr
+ 1, alloc
);
5418 items
[nr
++] = todo_list
->items
[cur
];
5423 FREE_AND_NULL(todo_list
->items
);
5424 todo_list
->items
= items
;
5426 todo_list
->alloc
= alloc
;
5431 for (i
= 0; i
< todo_list
->nr
; i
++)
5434 hashmap_free_entries(&subject2item
, struct subject2item_entry
, entry
);
5436 clear_commit_todo_item(&commit_todo
);