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 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 static GIT_PATH_FUNC(rebase_path_todo_backup
,
59 "rebase-merge/git-rebase-todo.backup")
62 * The rebase command lines that have already been processed. A line
63 * is moved here when it is first handled, before any associated user
66 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
68 * The file to keep track of how many commands were already processed (e.g.
71 static GIT_PATH_FUNC(rebase_path_msgnum
, "rebase-merge/msgnum")
73 * The file to keep track of how many commands are to be processed in total
74 * (e.g. for the prompt).
76 static GIT_PATH_FUNC(rebase_path_msgtotal
, "rebase-merge/end")
78 * The commit message that is planned to be used for any changes that
79 * need to be committed following a user interaction.
81 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
83 * The file into which is accumulated the suggested commit message for
84 * squash/fixup commands. When the first of a series of squash/fixups
85 * is seen, the file is created and the commit message from the
86 * previous commit and from the first squash/fixup commit are written
87 * to it. The commit message for each subsequent squash/fixup commit
88 * is appended to the file as it is processed.
90 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
92 * If the current series of squash/fixups has not yet included a squash
93 * command, then this file exists and holds the commit message of the
94 * original "pick" commit. (If the series ends without a "squash"
95 * command, then this can be used as the commit message of the combined
96 * commit without opening the editor.)
98 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
100 * This file contains the list fixup/squash commands that have been
101 * accumulated into message-fixup or message-squash so far.
103 static GIT_PATH_FUNC(rebase_path_current_fixups
, "rebase-merge/current-fixups")
105 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
106 * GIT_AUTHOR_DATE that will be used for the commit that is currently
109 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
111 * When an "edit" rebase command is being processed, the SHA1 of the
112 * commit to be edited is recorded in this file. When "git rebase
113 * --continue" is executed, if there are any staged changes then they
114 * will be amended to the HEAD commit, but only provided the HEAD
115 * commit is still the commit to be edited. When any other rebase
116 * command is processed, this file is deleted.
118 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
120 * When we stop at a given patch via the "edit" command, this file contains
121 * the abbreviated commit name of the corresponding patch.
123 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
125 * For the post-rewrite hook, we make a list of rewritten commits and
126 * their new sha1s. The rewritten-pending list keeps the sha1s of
127 * commits that have been processed, but not committed yet,
128 * e.g. because they are waiting for a 'squash' command.
130 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
131 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
132 "rebase-merge/rewritten-pending")
135 * The path of the file containig the OID of the "squash onto" commit, i.e.
136 * the dummy commit used for `reset [new root]`.
138 static GIT_PATH_FUNC(rebase_path_squash_onto
, "rebase-merge/squash-onto")
141 * The path of the file listing refs that need to be deleted after the rebase
142 * finishes. This is used by the `label` command to record the need for cleanup.
144 static GIT_PATH_FUNC(rebase_path_refs_to_delete
, "rebase-merge/refs-to-delete")
147 * The following files are written by git-rebase just after parsing the
150 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
151 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
152 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
153 static GIT_PATH_FUNC(rebase_path_signoff
, "rebase-merge/signoff")
154 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
155 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
156 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
157 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
158 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
159 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate
, "rebase-merge/allow_rerere_autoupdate")
160 static GIT_PATH_FUNC(rebase_path_quiet
, "rebase-merge/quiet")
162 static int git_sequencer_config(const char *k
, const char *v
, void *cb
)
164 struct replay_opts
*opts
= cb
;
167 if (!strcmp(k
, "commit.cleanup")) {
170 status
= git_config_string(&s
, k
, v
);
174 if (!strcmp(s
, "verbatim"))
175 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
176 else if (!strcmp(s
, "whitespace"))
177 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
178 else if (!strcmp(s
, "strip"))
179 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_ALL
;
180 else if (!strcmp(s
, "scissors"))
181 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_SPACE
;
183 warning(_("invalid commit message cleanup mode '%s'"),
190 if (!strcmp(k
, "commit.gpgsign")) {
191 opts
->gpg_sign
= git_config_bool(k
, v
) ? xstrdup("") : NULL
;
195 status
= git_gpg_config(k
, v
, NULL
);
199 return git_diff_basic_config(k
, v
, NULL
);
202 void sequencer_init_config(struct replay_opts
*opts
)
204 opts
->default_msg_cleanup
= COMMIT_MSG_CLEANUP_NONE
;
205 git_config(git_sequencer_config
, opts
);
208 static inline int is_rebase_i(const struct replay_opts
*opts
)
210 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
213 static const char *get_dir(const struct replay_opts
*opts
)
215 if (is_rebase_i(opts
))
216 return rebase_path();
217 return git_path_seq_dir();
220 static const char *get_todo_path(const struct replay_opts
*opts
)
222 if (is_rebase_i(opts
))
223 return rebase_path_todo();
224 return git_path_todo_file();
228 * Returns 0 for non-conforming footer
229 * Returns 1 for conforming footer
230 * Returns 2 when sob exists within conforming footer
231 * Returns 3 when sob exists within conforming footer as last entry
233 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
234 size_t ignore_footer
)
236 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
237 struct trailer_info info
;
239 int found_sob
= 0, found_sob_last
= 0;
243 trailer_info_get(&info
, sb
->buf
, &opts
);
245 if (info
.trailer_start
== info
.trailer_end
)
248 for (i
= 0; i
< info
.trailer_nr
; i
++)
249 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
251 if (i
== info
.trailer_nr
- 1)
255 trailer_info_release(&info
);
264 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
266 static struct strbuf buf
= STRBUF_INIT
;
270 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
274 int sequencer_remove_state(struct replay_opts
*opts
)
276 struct strbuf buf
= STRBUF_INIT
;
279 if (is_rebase_i(opts
) &&
280 strbuf_read_file(&buf
, rebase_path_refs_to_delete(), 0) > 0) {
283 char *eol
= strchr(p
, '\n');
286 if (delete_ref("(rebase -i) cleanup", p
, NULL
, 0) < 0)
287 warning(_("could not delete '%s'"), p
);
294 free(opts
->gpg_sign
);
295 free(opts
->strategy
);
296 for (i
= 0; i
< opts
->xopts_nr
; i
++)
297 free(opts
->xopts
[i
]);
299 strbuf_release(&opts
->current_fixups
);
302 strbuf_addstr(&buf
, get_dir(opts
));
303 remove_dir_recursively(&buf
, 0);
304 strbuf_release(&buf
);
309 static const char *action_name(const struct replay_opts
*opts
)
311 switch (opts
->action
) {
315 return N_("cherry-pick");
316 case REPLAY_INTERACTIVE_REBASE
:
317 return N_("rebase -i");
319 die(_("unknown action: %d"), opts
->action
);
322 struct commit_message
{
329 static const char *short_commit_name(struct commit
*commit
)
331 return find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
);
334 static int get_message(struct commit
*commit
, struct commit_message
*out
)
336 const char *abbrev
, *subject
;
339 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
340 abbrev
= short_commit_name(commit
);
342 subject_len
= find_commit_subject(out
->message
, &subject
);
344 out
->subject
= xmemdupz(subject
, subject_len
);
345 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
346 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
351 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
353 free(msg
->parent_label
);
356 unuse_commit_buffer(commit
, msg
->message
);
359 static void print_advice(int show_hint
, struct replay_opts
*opts
)
361 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
364 fprintf(stderr
, "%s\n", msg
);
366 * A conflict has occurred but the porcelain
367 * (typically rebase --interactive) wants to take care
368 * of the commit itself so remove CHERRY_PICK_HEAD
370 unlink(git_path_cherry_pick_head(the_repository
));
376 advise(_("after resolving the conflicts, mark the corrected paths\n"
377 "with 'git add <paths>' or 'git rm <paths>'"));
379 advise(_("after resolving the conflicts, mark the corrected paths\n"
380 "with 'git add <paths>' or 'git rm <paths>'\n"
381 "and commit the result with 'git commit'"));
385 int write_message(const void *buf
, size_t len
, const char *filename
,
388 struct lock_file msg_file
= LOCK_INIT
;
390 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
392 return error_errno(_("could not lock '%s'"), filename
);
393 if (write_in_full(msg_fd
, buf
, len
) < 0) {
394 error_errno(_("could not write to '%s'"), filename
);
395 rollback_lock_file(&msg_file
);
398 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
399 error_errno(_("could not write eol to '%s'"), filename
);
400 rollback_lock_file(&msg_file
);
403 if (commit_lock_file(&msg_file
) < 0)
404 return error(_("failed to finalize '%s'"), filename
);
410 * Reads a file that was presumably written by a shell script, i.e. with an
411 * end-of-line marker that needs to be stripped.
413 * Note that only the last end-of-line marker is stripped, consistent with the
414 * behavior of "$(cat path)" in a shell script.
416 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
418 static int read_oneliner(struct strbuf
*buf
,
419 const char *path
, int skip_if_empty
)
421 int orig_len
= buf
->len
;
423 if (!file_exists(path
))
426 if (strbuf_read_file(buf
, path
, 0) < 0) {
427 warning_errno(_("could not read '%s'"), path
);
431 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
432 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
434 buf
->buf
[buf
->len
] = '\0';
437 if (skip_if_empty
&& buf
->len
== orig_len
)
443 static struct tree
*empty_tree(void)
445 return lookup_tree(the_repository
, the_repository
->hash_algo
->empty_tree
);
448 static int error_dirty_index(struct replay_opts
*opts
)
450 if (read_cache_unmerged())
451 return error_resolve_conflict(_(action_name(opts
)));
453 error(_("your local changes would be overwritten by %s."),
454 _(action_name(opts
)));
456 if (advice_commit_before_merge
)
457 advise(_("commit your changes or stash them to proceed."));
461 static void update_abort_safety_file(void)
463 struct object_id head
;
465 /* Do nothing on a single-pick */
466 if (!file_exists(git_path_seq_dir()))
469 if (!get_oid("HEAD", &head
))
470 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
472 write_file(git_path_abort_safety_file(), "%s", "");
475 static int fast_forward_to(const struct object_id
*to
, const struct object_id
*from
,
476 int unborn
, struct replay_opts
*opts
)
478 struct ref_transaction
*transaction
;
479 struct strbuf sb
= STRBUF_INIT
;
480 struct strbuf err
= STRBUF_INIT
;
482 read_index(&the_index
);
483 if (checkout_fast_forward(the_repository
, from
, to
, 1))
484 return -1; /* the callee should have complained already */
486 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
488 transaction
= ref_transaction_begin(&err
);
490 ref_transaction_update(transaction
, "HEAD",
491 to
, unborn
&& !is_rebase_i(opts
) ?
494 ref_transaction_commit(transaction
, &err
)) {
495 ref_transaction_free(transaction
);
496 error("%s", err
.buf
);
498 strbuf_release(&err
);
503 strbuf_release(&err
);
504 ref_transaction_free(transaction
);
505 update_abort_safety_file();
509 void append_conflicts_hint(struct strbuf
*msgbuf
)
513 strbuf_addch(msgbuf
, '\n');
514 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
515 for (i
= 0; i
< active_nr
;) {
516 const struct cache_entry
*ce
= active_cache
[i
++];
518 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
519 while (i
< active_nr
&& !strcmp(ce
->name
,
520 active_cache
[i
]->name
))
526 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
527 const char *base_label
, const char *next_label
,
528 struct object_id
*head
, struct strbuf
*msgbuf
,
529 struct replay_opts
*opts
)
531 struct merge_options o
;
532 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
535 struct lock_file index_lock
= LOCK_INIT
;
537 if (hold_locked_index(&index_lock
, LOCK_REPORT_ON_ERROR
) < 0)
542 init_merge_options(&o
);
543 o
.ancestor
= base
? base_label
: "(empty tree)";
545 o
.branch2
= next
? next_label
: "(empty tree)";
546 if (is_rebase_i(opts
))
548 o
.show_rename_progress
= 1;
550 head_tree
= parse_tree_indirect(head
);
551 next_tree
= next
? get_commit_tree(next
) : empty_tree();
552 base_tree
= base
? get_commit_tree(base
) : empty_tree();
554 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
555 parse_merge_opt(&o
, *xopt
);
557 clean
= merge_trees(&o
,
559 next_tree
, base_tree
, &result
);
560 if (is_rebase_i(opts
) && clean
<= 0)
561 fputs(o
.obuf
.buf
, stdout
);
562 strbuf_release(&o
.obuf
);
563 diff_warn_rename_limit("merge.renamelimit", o
.needed_rename_limit
, 0);
565 rollback_lock_file(&index_lock
);
569 if (write_locked_index(&the_index
, &index_lock
,
570 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
572 * TRANSLATORS: %s will be "revert", "cherry-pick" or
575 return error(_("%s: Unable to write new index file"),
576 _(action_name(opts
)));
579 append_conflicts_hint(msgbuf
);
584 static struct object_id
*get_cache_tree_oid(void)
586 if (!active_cache_tree
)
587 active_cache_tree
= cache_tree();
589 if (!cache_tree_fully_valid(active_cache_tree
))
590 if (cache_tree_update(&the_index
, 0)) {
591 error(_("unable to update cache tree"));
595 return &active_cache_tree
->oid
;
598 static int is_index_unchanged(void)
600 struct object_id head_oid
, *cache_tree_oid
;
601 struct commit
*head_commit
;
603 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
604 return error(_("could not resolve HEAD commit"));
606 head_commit
= lookup_commit(the_repository
, &head_oid
);
609 * If head_commit is NULL, check_commit, called from
610 * lookup_commit, would have indicated that head_commit is not
611 * a commit object already. parse_commit() will return failure
612 * without further complaints in such a case. Otherwise, if
613 * the commit is invalid, parse_commit() will complain. So
614 * there is nothing for us to say here. Just return failure.
616 if (parse_commit(head_commit
))
619 if (!(cache_tree_oid
= get_cache_tree_oid()))
622 return oideq(cache_tree_oid
, get_commit_tree_oid(head_commit
));
625 static int write_author_script(const char *message
)
627 struct strbuf buf
= STRBUF_INIT
;
632 if (!*message
|| starts_with(message
, "\n")) {
634 /* Missing 'author' line? */
635 unlink(rebase_path_author_script());
637 } else if (skip_prefix(message
, "author ", &message
))
639 else if ((eol
= strchr(message
, '\n')))
644 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
645 while (*message
&& *message
!= '\n' && *message
!= '\r')
646 if (skip_prefix(message
, " <", &message
))
648 else if (*message
!= '\'')
649 strbuf_addch(&buf
, *(message
++));
651 strbuf_addf(&buf
, "'\\%c'", *(message
++));
652 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
653 while (*message
&& *message
!= '\n' && *message
!= '\r')
654 if (skip_prefix(message
, "> ", &message
))
656 else if (*message
!= '\'')
657 strbuf_addch(&buf
, *(message
++));
659 strbuf_addf(&buf
, "'\\%c'", *(message
++));
660 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
661 while (*message
&& *message
!= '\n' && *message
!= '\r')
662 if (*message
!= '\'')
663 strbuf_addch(&buf
, *(message
++));
665 strbuf_addf(&buf
, "'\\%c'", *(message
++));
666 strbuf_addch(&buf
, '\'');
667 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
668 strbuf_release(&buf
);
674 * write_author_script() used to fail to terminate the last line with a "'" and
675 * also escaped "'" incorrectly as "'\\\\''" rather than "'\\''". We check for
676 * the terminating "'" on the last line to see how "'" has been escaped in case
677 * git was upgraded while rebase was stopped.
679 static int quoting_is_broken(const char *s
, size_t n
)
681 /* Skip any empty lines in case the file was hand edited */
682 while (n
> 0 && s
[--n
] == '\n')
684 if (n
> 0 && s
[n
] != '\'')
691 * Read a list of environment variable assignments (such as the author-script
692 * file) into an environment block. Returns -1 on error, 0 otherwise.
694 static int read_env_script(struct argv_array
*env
)
696 struct strbuf script
= STRBUF_INIT
;
697 int i
, count
= 0, sq_bug
;
701 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
703 /* write_author_script() used to quote incorrectly */
704 sq_bug
= quoting_is_broken(script
.buf
, script
.len
);
705 for (p
= script
.buf
; *p
; p
++)
706 if (sq_bug
&& skip_prefix(p
, "'\\\\''", &p2
))
707 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
708 else if (skip_prefix(p
, "'\\''", &p2
))
709 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
711 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
712 else if (*p
== '\n') {
717 for (i
= 0, p
= script
.buf
; i
< count
; i
++) {
718 argv_array_push(env
, p
);
725 static char *get_author(const char *message
)
730 a
= find_commit_header(message
, "author", &len
);
732 return xmemdupz(a
, len
);
737 /* Read author-script and return an ident line (author <email> timestamp) */
738 static const char *read_author_ident(struct strbuf
*buf
)
740 const char *keys
[] = {
741 "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE="
743 struct strbuf out
= STRBUF_INIT
;
748 if (strbuf_read_file(buf
, rebase_path_author_script(), 256) <= 0)
751 /* dequote values and construct ident line in-place */
752 for (in
= buf
->buf
; i
< 3 && in
- buf
->buf
< buf
->len
; i
++) {
753 if (!skip_prefix(in
, keys
[i
], (const char **)&in
)) {
754 warning(_("could not parse '%s' (looking for '%s')"),
755 rebase_path_author_script(), keys
[i
]);
759 eol
= strchrnul(in
, '\n');
761 if (!sq_dequote(in
)) {
762 warning(_("bad quoting on %s value in '%s'"),
763 keys
[i
], rebase_path_author_script());
771 warning(_("could not parse '%s' (looking for '%s')"),
772 rebase_path_author_script(), keys
[i
]);
776 /* validate date since fmt_ident() will die() on bad value */
777 if (parse_date(val
[2], &out
)){
778 warning(_("invalid date format '%s' in '%s'"),
779 val
[2], rebase_path_author_script());
780 strbuf_release(&out
);
785 strbuf_addstr(&out
, fmt_ident(val
[0], val
[1], val
[2], 0));
786 strbuf_swap(buf
, &out
);
787 strbuf_release(&out
);
791 static const char staged_changes_advice
[] =
792 N_("you have staged changes in your working tree\n"
793 "If these changes are meant to be squashed into the previous commit, run:\n"
795 " git commit --amend %s\n"
797 "If they are meant to go into a new commit, run:\n"
801 "In both cases, once you're done, continue with:\n"
803 " git rebase --continue\n");
805 #define ALLOW_EMPTY (1<<0)
806 #define EDIT_MSG (1<<1)
807 #define AMEND_MSG (1<<2)
808 #define CLEANUP_MSG (1<<3)
809 #define VERIFY_MSG (1<<4)
810 #define CREATE_ROOT_COMMIT (1<<5)
812 static int run_command_silent_on_success(struct child_process
*cmd
)
814 struct strbuf buf
= STRBUF_INIT
;
817 cmd
->stdout_to_stderr
= 1;
818 rc
= pipe_command(cmd
,
824 fputs(buf
.buf
, stderr
);
825 strbuf_release(&buf
);
830 * If we are cherry-pick, and if the merge did not result in
831 * hand-editing, we will hit this commit and inherit the original
832 * author date and name.
834 * If we are revert, or if our cherry-pick results in a hand merge,
835 * we had better say that the current user is responsible for that.
837 * An exception is when run_git_commit() is called during an
838 * interactive rebase: in that case, we will want to retain the
841 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
844 struct child_process cmd
= CHILD_PROCESS_INIT
;
847 if ((flags
& CREATE_ROOT_COMMIT
) && !(flags
& AMEND_MSG
)) {
848 struct strbuf msg
= STRBUF_INIT
, script
= STRBUF_INIT
;
849 const char *author
= NULL
;
850 struct object_id root_commit
, *cache_tree_oid
;
853 if (is_rebase_i(opts
)) {
854 author
= read_author_ident(&script
);
856 strbuf_release(&script
);
862 BUG("root commit without message");
864 if (!(cache_tree_oid
= get_cache_tree_oid()))
868 res
= strbuf_read_file(&msg
, defmsg
, 0);
871 res
= error_errno(_("could not read '%s'"), defmsg
);
873 res
= commit_tree(msg
.buf
, msg
.len
, cache_tree_oid
,
874 NULL
, &root_commit
, author
,
877 strbuf_release(&msg
);
878 strbuf_release(&script
);
880 update_ref(NULL
, "CHERRY_PICK_HEAD", &root_commit
, NULL
,
881 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
);
882 res
= update_ref(NULL
, "HEAD", &root_commit
, NULL
, 0,
883 UPDATE_REFS_MSG_ON_ERR
);
885 return res
< 0 ? error(_("writing root commit")) : 0;
890 if (is_rebase_i(opts
) && read_env_script(&cmd
.env_array
)) {
891 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
893 return error(_(staged_changes_advice
),
897 argv_array_push(&cmd
.args
, "commit");
899 if (!(flags
& VERIFY_MSG
))
900 argv_array_push(&cmd
.args
, "-n");
901 if ((flags
& AMEND_MSG
))
902 argv_array_push(&cmd
.args
, "--amend");
904 argv_array_pushf(&cmd
.args
, "-S%s", opts
->gpg_sign
);
906 argv_array_pushl(&cmd
.args
, "-F", defmsg
, NULL
);
907 else if (!(flags
& EDIT_MSG
))
908 argv_array_pushl(&cmd
.args
, "-C", "HEAD", NULL
);
909 if ((flags
& CLEANUP_MSG
))
910 argv_array_push(&cmd
.args
, "--cleanup=strip");
911 if ((flags
& EDIT_MSG
))
912 argv_array_push(&cmd
.args
, "-e");
913 else if (!(flags
& CLEANUP_MSG
) &&
914 !opts
->signoff
&& !opts
->record_origin
&&
915 git_config_get_value("commit.cleanup", &value
))
916 argv_array_push(&cmd
.args
, "--cleanup=verbatim");
918 if ((flags
& ALLOW_EMPTY
))
919 argv_array_push(&cmd
.args
, "--allow-empty");
921 if (!(flags
& EDIT_MSG
))
922 argv_array_push(&cmd
.args
, "--allow-empty-message");
924 if (is_rebase_i(opts
) && !(flags
& EDIT_MSG
))
925 return run_command_silent_on_success(&cmd
);
927 return run_command(&cmd
);
930 static int rest_is_empty(const struct strbuf
*sb
, int start
)
935 /* Check if the rest is just whitespace and Signed-off-by's. */
936 for (i
= start
; i
< sb
->len
; i
++) {
937 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
943 if (strlen(sign_off_header
) <= eol
- i
&&
944 starts_with(sb
->buf
+ i
, sign_off_header
)) {
949 if (!isspace(sb
->buf
[i
++]))
957 * Find out if the message in the strbuf contains only whitespace and
958 * Signed-off-by lines.
960 int message_is_empty(const struct strbuf
*sb
,
961 enum commit_msg_cleanup_mode cleanup_mode
)
963 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
965 return rest_is_empty(sb
, 0);
969 * See if the user edited the message in the editor or left what
970 * was in the template intact
972 int template_untouched(const struct strbuf
*sb
, const char *template_file
,
973 enum commit_msg_cleanup_mode cleanup_mode
)
975 struct strbuf tmpl
= STRBUF_INIT
;
978 if (cleanup_mode
== COMMIT_MSG_CLEANUP_NONE
&& sb
->len
)
981 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
984 strbuf_stripspace(&tmpl
, cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
);
985 if (!skip_prefix(sb
->buf
, tmpl
.buf
, &start
))
987 strbuf_release(&tmpl
);
988 return rest_is_empty(sb
, start
- sb
->buf
);
991 int update_head_with_reflog(const struct commit
*old_head
,
992 const struct object_id
*new_head
,
993 const char *action
, const struct strbuf
*msg
,
996 struct ref_transaction
*transaction
;
997 struct strbuf sb
= STRBUF_INIT
;
1002 strbuf_addstr(&sb
, action
);
1003 strbuf_addstr(&sb
, ": ");
1006 nl
= strchr(msg
->buf
, '\n');
1008 strbuf_add(&sb
, msg
->buf
, nl
+ 1 - msg
->buf
);
1010 strbuf_addbuf(&sb
, msg
);
1011 strbuf_addch(&sb
, '\n');
1014 transaction
= ref_transaction_begin(err
);
1016 ref_transaction_update(transaction
, "HEAD", new_head
,
1017 old_head
? &old_head
->object
.oid
: &null_oid
,
1019 ref_transaction_commit(transaction
, err
)) {
1022 ref_transaction_free(transaction
);
1023 strbuf_release(&sb
);
1028 static int run_rewrite_hook(const struct object_id
*oldoid
,
1029 const struct object_id
*newoid
)
1031 struct child_process proc
= CHILD_PROCESS_INIT
;
1032 const char *argv
[3];
1034 struct strbuf sb
= STRBUF_INIT
;
1036 argv
[0] = find_hook("post-rewrite");
1045 proc
.stdout_to_stderr
= 1;
1047 code
= start_command(&proc
);
1050 strbuf_addf(&sb
, "%s %s\n", oid_to_hex(oldoid
), oid_to_hex(newoid
));
1051 sigchain_push(SIGPIPE
, SIG_IGN
);
1052 write_in_full(proc
.in
, sb
.buf
, sb
.len
);
1054 strbuf_release(&sb
);
1055 sigchain_pop(SIGPIPE
);
1056 return finish_command(&proc
);
1059 void commit_post_rewrite(const struct commit
*old_head
,
1060 const struct object_id
*new_head
)
1062 struct notes_rewrite_cfg
*cfg
;
1064 cfg
= init_copy_notes_for_rewrite("amend");
1066 /* we are amending, so old_head is not NULL */
1067 copy_note_for_rewrite(cfg
, &old_head
->object
.oid
, new_head
);
1068 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
1070 run_rewrite_hook(&old_head
->object
.oid
, new_head
);
1073 static int run_prepare_commit_msg_hook(struct strbuf
*msg
, const char *commit
)
1075 struct argv_array hook_env
= ARGV_ARRAY_INIT
;
1079 name
= git_path_commit_editmsg();
1080 if (write_message(msg
->buf
, msg
->len
, name
, 0))
1083 argv_array_pushf(&hook_env
, "GIT_INDEX_FILE=%s", get_index_file());
1084 argv_array_push(&hook_env
, "GIT_EDITOR=:");
1086 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
1087 "commit", commit
, NULL
);
1089 ret
= run_hook_le(hook_env
.argv
, "prepare-commit-msg", name
,
1092 ret
= error(_("'prepare-commit-msg' hook failed"));
1093 argv_array_clear(&hook_env
);
1098 static const char implicit_ident_advice_noconfig
[] =
1099 N_("Your name and email address were configured automatically based\n"
1100 "on your username and hostname. Please check that they are accurate.\n"
1101 "You can suppress this message by setting them explicitly. Run the\n"
1102 "following command and follow the instructions in your editor to edit\n"
1103 "your configuration file:\n"
1105 " git config --global --edit\n"
1107 "After doing this, you may fix the identity used for this commit with:\n"
1109 " git commit --amend --reset-author\n");
1111 static const char implicit_ident_advice_config
[] =
1112 N_("Your name and email address were configured automatically based\n"
1113 "on your username and hostname. Please check that they are accurate.\n"
1114 "You can suppress this message by setting them explicitly:\n"
1116 " git config --global user.name \"Your Name\"\n"
1117 " git config --global user.email you@example.com\n"
1119 "After doing this, you may fix the identity used for this commit with:\n"
1121 " git commit --amend --reset-author\n");
1123 static const char *implicit_ident_advice(void)
1125 char *user_config
= expand_user_path("~/.gitconfig", 0);
1126 char *xdg_config
= xdg_config_home("config");
1127 int config_exists
= file_exists(user_config
) || file_exists(xdg_config
);
1133 return _(implicit_ident_advice_config
);
1135 return _(implicit_ident_advice_noconfig
);
1139 void print_commit_summary(const char *prefix
, const struct object_id
*oid
,
1142 struct rev_info rev
;
1143 struct commit
*commit
;
1144 struct strbuf format
= STRBUF_INIT
;
1146 struct pretty_print_context pctx
= {0};
1147 struct strbuf author_ident
= STRBUF_INIT
;
1148 struct strbuf committer_ident
= STRBUF_INIT
;
1150 commit
= lookup_commit(the_repository
, oid
);
1152 die(_("couldn't look up newly created commit"));
1153 if (parse_commit(commit
))
1154 die(_("could not parse newly created commit"));
1156 strbuf_addstr(&format
, "format:%h] %s");
1158 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1159 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1160 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1161 strbuf_addstr(&format
, "\n Author: ");
1162 strbuf_addbuf_percentquote(&format
, &author_ident
);
1164 if (flags
& SUMMARY_SHOW_AUTHOR_DATE
) {
1165 struct strbuf date
= STRBUF_INIT
;
1167 format_commit_message(commit
, "%ad", &date
, &pctx
);
1168 strbuf_addstr(&format
, "\n Date: ");
1169 strbuf_addbuf_percentquote(&format
, &date
);
1170 strbuf_release(&date
);
1172 if (!committer_ident_sufficiently_given()) {
1173 strbuf_addstr(&format
, "\n Committer: ");
1174 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1175 if (advice_implicit_identity
) {
1176 strbuf_addch(&format
, '\n');
1177 strbuf_addstr(&format
, implicit_ident_advice());
1180 strbuf_release(&author_ident
);
1181 strbuf_release(&committer_ident
);
1183 repo_init_revisions(the_repository
, &rev
, prefix
);
1184 setup_revisions(0, NULL
, &rev
, NULL
);
1187 rev
.diffopt
.output_format
=
1188 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1190 rev
.verbose_header
= 1;
1191 rev
.show_root_diff
= 1;
1192 get_commit_format(format
.buf
, &rev
);
1193 rev
.always_show_header
= 0;
1194 rev
.diffopt
.detect_rename
= DIFF_DETECT_RENAME
;
1195 rev
.diffopt
.break_opt
= 0;
1196 diff_setup_done(&rev
.diffopt
);
1198 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1200 die_errno(_("unable to resolve HEAD after creating commit"));
1201 if (!strcmp(head
, "HEAD"))
1202 head
= _("detached HEAD");
1204 skip_prefix(head
, "refs/heads/", &head
);
1205 printf("[%s%s ", head
, (flags
& SUMMARY_INITIAL_COMMIT
) ?
1206 _(" (root-commit)") : "");
1208 if (!log_tree_commit(&rev
, commit
)) {
1209 rev
.always_show_header
= 1;
1210 rev
.use_terminator
= 1;
1211 log_tree_commit(&rev
, commit
);
1214 strbuf_release(&format
);
1217 static int parse_head(struct commit
**head
)
1219 struct commit
*current_head
;
1220 struct object_id oid
;
1222 if (get_oid("HEAD", &oid
)) {
1223 current_head
= NULL
;
1225 current_head
= lookup_commit_reference(the_repository
, &oid
);
1227 return error(_("could not parse HEAD"));
1228 if (!oideq(&oid
, ¤t_head
->object
.oid
)) {
1229 warning(_("HEAD %s is not a commit!"),
1232 if (parse_commit(current_head
))
1233 return error(_("could not parse HEAD commit"));
1235 *head
= current_head
;
1241 * Try to commit without forking 'git commit'. In some cases we need
1242 * to run 'git commit' to display an error message
1245 * -1 - error unable to commit
1247 * 1 - run 'git commit'
1249 static int try_to_commit(struct strbuf
*msg
, const char *author
,
1250 struct replay_opts
*opts
, unsigned int flags
,
1251 struct object_id
*oid
)
1253 struct object_id tree
;
1254 struct commit
*current_head
;
1255 struct commit_list
*parents
= NULL
;
1256 struct commit_extra_header
*extra
= NULL
;
1257 struct strbuf err
= STRBUF_INIT
;
1258 struct strbuf commit_msg
= STRBUF_INIT
;
1259 char *amend_author
= NULL
;
1260 const char *hook_commit
= NULL
;
1261 enum commit_msg_cleanup_mode cleanup
;
1264 if (parse_head(¤t_head
))
1267 if (flags
& AMEND_MSG
) {
1268 const char *exclude_gpgsig
[] = { "gpgsig", NULL
};
1269 const char *out_enc
= get_commit_output_encoding();
1270 const char *message
= logmsg_reencode(current_head
, NULL
,
1274 const char *orig_message
= NULL
;
1276 find_commit_subject(message
, &orig_message
);
1278 strbuf_addstr(msg
, orig_message
);
1279 hook_commit
= "HEAD";
1281 author
= amend_author
= get_author(message
);
1282 unuse_commit_buffer(current_head
, message
);
1284 res
= error(_("unable to parse commit author"));
1287 parents
= copy_commit_list(current_head
->parents
);
1288 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1289 } else if (current_head
) {
1290 commit_list_insert(current_head
, &parents
);
1293 if (write_index_as_tree(&tree
, &the_index
, get_index_file(), 0, NULL
)) {
1294 res
= error(_("git write-tree failed to write a tree"));
1298 if (!(flags
& ALLOW_EMPTY
) && oideq(current_head
?
1299 get_commit_tree_oid(current_head
) :
1300 the_hash_algo
->empty_tree
, &tree
)) {
1301 res
= 1; /* run 'git commit' to display error message */
1305 if (find_hook("prepare-commit-msg")) {
1306 res
= run_prepare_commit_msg_hook(msg
, hook_commit
);
1309 if (strbuf_read_file(&commit_msg
, git_path_commit_editmsg(),
1311 res
= error_errno(_("unable to read commit message "
1313 git_path_commit_editmsg());
1319 cleanup
= (flags
& CLEANUP_MSG
) ? COMMIT_MSG_CLEANUP_ALL
:
1320 opts
->default_msg_cleanup
;
1322 if (cleanup
!= COMMIT_MSG_CLEANUP_NONE
)
1323 strbuf_stripspace(msg
, cleanup
== COMMIT_MSG_CLEANUP_ALL
);
1324 if ((flags
& EDIT_MSG
) && message_is_empty(msg
, cleanup
)) {
1325 res
= 1; /* run 'git commit' to display error message */
1331 if (commit_tree_extended(msg
->buf
, msg
->len
, &tree
, parents
,
1332 oid
, author
, opts
->gpg_sign
, extra
)) {
1333 res
= error(_("failed to write commit object"));
1337 if (update_head_with_reflog(current_head
, oid
,
1338 getenv("GIT_REFLOG_ACTION"), msg
, &err
)) {
1339 res
= error("%s", err
.buf
);
1343 if (flags
& AMEND_MSG
)
1344 commit_post_rewrite(current_head
, oid
);
1347 free_commit_extra_headers(extra
);
1348 strbuf_release(&err
);
1349 strbuf_release(&commit_msg
);
1355 static int do_commit(const char *msg_file
, const char *author
,
1356 struct replay_opts
*opts
, unsigned int flags
)
1360 if (!(flags
& EDIT_MSG
) && !(flags
& VERIFY_MSG
) &&
1361 !(flags
& CREATE_ROOT_COMMIT
)) {
1362 struct object_id oid
;
1363 struct strbuf sb
= STRBUF_INIT
;
1365 if (msg_file
&& strbuf_read_file(&sb
, msg_file
, 2048) < 0)
1366 return error_errno(_("unable to read commit message "
1370 res
= try_to_commit(msg_file
? &sb
: NULL
, author
, opts
, flags
,
1372 strbuf_release(&sb
);
1374 unlink(git_path_cherry_pick_head(the_repository
));
1375 unlink(git_path_merge_msg(the_repository
));
1376 if (!is_rebase_i(opts
))
1377 print_commit_summary(NULL
, &oid
,
1378 SUMMARY_SHOW_AUTHOR_DATE
);
1383 return run_git_commit(msg_file
, opts
, flags
);
1388 static int is_original_commit_empty(struct commit
*commit
)
1390 const struct object_id
*ptree_oid
;
1392 if (parse_commit(commit
))
1393 return error(_("could not parse commit %s"),
1394 oid_to_hex(&commit
->object
.oid
));
1395 if (commit
->parents
) {
1396 struct commit
*parent
= commit
->parents
->item
;
1397 if (parse_commit(parent
))
1398 return error(_("could not parse parent commit %s"),
1399 oid_to_hex(&parent
->object
.oid
));
1400 ptree_oid
= get_commit_tree_oid(parent
);
1402 ptree_oid
= the_hash_algo
->empty_tree
; /* commit is root */
1405 return oideq(ptree_oid
, get_commit_tree_oid(commit
));
1409 * Do we run "git commit" with "--allow-empty"?
1411 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
1413 int index_unchanged
, empty_commit
;
1418 * (1) we do not allow empty at all and error out.
1420 * (2) we allow ones that were initially empty, but
1421 * forbid the ones that become empty;
1423 * (3) we allow both.
1425 if (!opts
->allow_empty
)
1426 return 0; /* let "git commit" barf as necessary */
1428 index_unchanged
= is_index_unchanged();
1429 if (index_unchanged
< 0)
1430 return index_unchanged
;
1431 if (!index_unchanged
)
1432 return 0; /* we do not have to say --allow-empty */
1434 if (opts
->keep_redundant_commits
)
1437 empty_commit
= is_original_commit_empty(commit
);
1438 if (empty_commit
< 0)
1439 return empty_commit
;
1447 * Note that ordering matters in this enum. Not only must it match the mapping
1448 * below, it is also divided into several sections that matter. When adding
1449 * new commands, make sure you add it in the right section.
1452 /* commands that handle commits */
1459 /* commands that do something else than handling a single commit */
1465 /* commands that do nothing but are counted for reporting progress */
1468 /* comments (not counted for reporting progress) */
1475 } todo_command_info
[] = {
1492 static const char *command_to_string(const enum todo_command command
)
1494 if (command
< TODO_COMMENT
)
1495 return todo_command_info
[command
].str
;
1496 die(_("unknown command: %d"), command
);
1499 static char command_to_char(const enum todo_command command
)
1501 if (command
< TODO_COMMENT
&& todo_command_info
[command
].c
)
1502 return todo_command_info
[command
].c
;
1503 return comment_line_char
;
1506 static int is_noop(const enum todo_command command
)
1508 return TODO_NOOP
<= command
;
1511 static int is_fixup(enum todo_command command
)
1513 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
1516 /* Does this command create a (non-merge) commit? */
1517 static int is_pick_or_similar(enum todo_command command
)
1532 static int update_squash_messages(enum todo_command command
,
1533 struct commit
*commit
, struct replay_opts
*opts
)
1535 struct strbuf buf
= STRBUF_INIT
;
1537 const char *message
, *body
;
1539 if (opts
->current_fixup_count
> 0) {
1540 struct strbuf header
= STRBUF_INIT
;
1543 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 9) <= 0)
1544 return error(_("could not read '%s'"),
1545 rebase_path_squash_msg());
1547 eol
= buf
.buf
[0] != comment_line_char
?
1548 buf
.buf
: strchrnul(buf
.buf
, '\n');
1550 strbuf_addf(&header
, "%c ", comment_line_char
);
1551 strbuf_addf(&header
, _("This is a combination of %d commits."),
1552 opts
->current_fixup_count
+ 2);
1553 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
1554 strbuf_release(&header
);
1556 struct object_id head
;
1557 struct commit
*head_commit
;
1558 const char *head_message
, *body
;
1560 if (get_oid("HEAD", &head
))
1561 return error(_("need a HEAD to fixup"));
1562 if (!(head_commit
= lookup_commit_reference(the_repository
, &head
)))
1563 return error(_("could not read HEAD"));
1564 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
1565 return error(_("could not read HEAD's commit message"));
1567 find_commit_subject(head_message
, &body
);
1568 if (write_message(body
, strlen(body
),
1569 rebase_path_fixup_msg(), 0)) {
1570 unuse_commit_buffer(head_commit
, head_message
);
1571 return error(_("cannot write '%s'"),
1572 rebase_path_fixup_msg());
1575 strbuf_addf(&buf
, "%c ", comment_line_char
);
1576 strbuf_addf(&buf
, _("This is a combination of %d commits."), 2);
1577 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1578 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
1579 strbuf_addstr(&buf
, "\n\n");
1580 strbuf_addstr(&buf
, body
);
1582 unuse_commit_buffer(head_commit
, head_message
);
1585 if (!(message
= get_commit_buffer(commit
, NULL
)))
1586 return error(_("could not read commit message of %s"),
1587 oid_to_hex(&commit
->object
.oid
));
1588 find_commit_subject(message
, &body
);
1590 if (command
== TODO_SQUASH
) {
1591 unlink(rebase_path_fixup_msg());
1592 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1593 strbuf_addf(&buf
, _("This is the commit message #%d:"),
1594 ++opts
->current_fixup_count
+ 1);
1595 strbuf_addstr(&buf
, "\n\n");
1596 strbuf_addstr(&buf
, body
);
1597 } else if (command
== TODO_FIXUP
) {
1598 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
1599 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
1600 ++opts
->current_fixup_count
+ 1);
1601 strbuf_addstr(&buf
, "\n\n");
1602 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
1604 return error(_("unknown command: %d"), command
);
1605 unuse_commit_buffer(commit
, message
);
1607 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
1608 strbuf_release(&buf
);
1611 strbuf_addf(&opts
->current_fixups
, "%s%s %s",
1612 opts
->current_fixups
.len
? "\n" : "",
1613 command_to_string(command
),
1614 oid_to_hex(&commit
->object
.oid
));
1615 res
= write_message(opts
->current_fixups
.buf
,
1616 opts
->current_fixups
.len
,
1617 rebase_path_current_fixups(), 0);
1623 static void flush_rewritten_pending(void) {
1624 struct strbuf buf
= STRBUF_INIT
;
1625 struct object_id newoid
;
1628 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ
+ 1) * 2) > 0 &&
1629 !get_oid("HEAD", &newoid
) &&
1630 (out
= fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
1631 char *bol
= buf
.buf
, *eol
;
1634 eol
= strchrnul(bol
, '\n');
1635 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
1636 bol
, oid_to_hex(&newoid
));
1642 unlink(rebase_path_rewritten_pending());
1644 strbuf_release(&buf
);
1647 static void record_in_rewritten(struct object_id
*oid
,
1648 enum todo_command next_command
) {
1649 FILE *out
= fopen_or_warn(rebase_path_rewritten_pending(), "a");
1654 fprintf(out
, "%s\n", oid_to_hex(oid
));
1657 if (!is_fixup(next_command
))
1658 flush_rewritten_pending();
1661 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
1662 struct replay_opts
*opts
, int final_fixup
)
1664 unsigned int flags
= opts
->edit
? EDIT_MSG
: 0;
1665 const char *msg_file
= opts
->edit
? NULL
: git_path_merge_msg(the_repository
);
1666 struct object_id head
;
1667 struct commit
*base
, *next
, *parent
;
1668 const char *base_label
, *next_label
;
1669 char *author
= NULL
;
1670 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
1671 struct strbuf msgbuf
= STRBUF_INIT
;
1672 int res
, unborn
= 0, allow
;
1674 if (opts
->no_commit
) {
1676 * We do not intend to commit immediately. We just want to
1677 * merge the differences in, so let's compute the tree
1678 * that represents the "current" state for merge-recursive
1681 if (write_index_as_tree(&head
, &the_index
, get_index_file(), 0, NULL
))
1682 return error(_("your index file is unmerged."));
1684 unborn
= get_oid("HEAD", &head
);
1685 /* Do we want to generate a root commit? */
1686 if (is_pick_or_similar(command
) && opts
->have_squash_onto
&&
1687 oideq(&head
, &opts
->squash_onto
)) {
1688 if (is_fixup(command
))
1689 return error(_("cannot fixup root commit"));
1690 flags
|= CREATE_ROOT_COMMIT
;
1693 oidcpy(&head
, the_hash_algo
->empty_tree
);
1694 if (index_differs_from(unborn
? empty_tree_oid_hex() : "HEAD",
1696 return error_dirty_index(opts
);
1700 if (!commit
->parents
)
1702 else if (commit
->parents
->next
) {
1703 /* Reverting or cherry-picking a merge commit */
1705 struct commit_list
*p
;
1707 if (!opts
->mainline
)
1708 return error(_("commit %s is a merge but no -m option was given."),
1709 oid_to_hex(&commit
->object
.oid
));
1711 for (cnt
= 1, p
= commit
->parents
;
1712 cnt
!= opts
->mainline
&& p
;
1715 if (cnt
!= opts
->mainline
|| !p
)
1716 return error(_("commit %s does not have parent %d"),
1717 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
1719 } else if (0 < opts
->mainline
)
1720 return error(_("mainline was specified but commit %s is not a merge."),
1721 oid_to_hex(&commit
->object
.oid
));
1723 parent
= commit
->parents
->item
;
1725 if (get_message(commit
, &msg
) != 0)
1726 return error(_("cannot get commit message for %s"),
1727 oid_to_hex(&commit
->object
.oid
));
1729 if (opts
->allow_ff
&& !is_fixup(command
) &&
1730 ((parent
&& oideq(&parent
->object
.oid
, &head
)) ||
1731 (!parent
&& unborn
))) {
1732 if (is_rebase_i(opts
))
1733 write_author_script(msg
.message
);
1734 res
= fast_forward_to(&commit
->object
.oid
, &head
, unborn
,
1736 if (res
|| command
!= TODO_REWORD
)
1738 flags
|= EDIT_MSG
| AMEND_MSG
| VERIFY_MSG
;
1740 goto fast_forward_edit
;
1742 if (parent
&& parse_commit(parent
) < 0)
1743 /* TRANSLATORS: The first %s will be a "todo" command like
1744 "revert" or "pick", the second %s a SHA1. */
1745 return error(_("%s: cannot parse parent commit %s"),
1746 command_to_string(command
),
1747 oid_to_hex(&parent
->object
.oid
));
1750 * "commit" is an existing commit. We would want to apply
1751 * the difference it introduces since its first parent "prev"
1752 * on top of the current HEAD if we are cherry-pick. Or the
1753 * reverse of it if we are revert.
1756 if (command
== TODO_REVERT
) {
1758 base_label
= msg
.label
;
1760 next_label
= msg
.parent_label
;
1761 strbuf_addstr(&msgbuf
, "Revert \"");
1762 strbuf_addstr(&msgbuf
, msg
.subject
);
1763 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
1764 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1766 if (commit
->parents
&& commit
->parents
->next
) {
1767 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1768 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1770 strbuf_addstr(&msgbuf
, ".\n");
1775 base_label
= msg
.parent_label
;
1777 next_label
= msg
.label
;
1779 /* Append the commit log message to msgbuf. */
1780 if (find_commit_subject(msg
.message
, &p
))
1781 strbuf_addstr(&msgbuf
, p
);
1783 if (opts
->record_origin
) {
1784 strbuf_complete_line(&msgbuf
);
1785 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1786 strbuf_addch(&msgbuf
, '\n');
1787 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1788 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1789 strbuf_addstr(&msgbuf
, ")\n");
1791 if (!is_fixup(command
))
1792 author
= get_author(msg
.message
);
1795 if (command
== TODO_REWORD
)
1796 flags
|= EDIT_MSG
| VERIFY_MSG
;
1797 else if (is_fixup(command
)) {
1798 if (update_squash_messages(command
, commit
, opts
))
1802 msg_file
= rebase_path_squash_msg();
1803 else if (file_exists(rebase_path_fixup_msg())) {
1804 flags
|= CLEANUP_MSG
;
1805 msg_file
= rebase_path_fixup_msg();
1807 const char *dest
= git_path_squash_msg(the_repository
);
1809 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1810 return error(_("could not rename '%s' to '%s'"),
1811 rebase_path_squash_msg(), dest
);
1812 unlink(git_path_merge_msg(the_repository
));
1818 if (opts
->signoff
&& !is_fixup(command
))
1819 append_signoff(&msgbuf
, 0, 0);
1821 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1823 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1824 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
1825 &head
, &msgbuf
, opts
);
1829 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
1830 git_path_merge_msg(the_repository
), 0);
1832 struct commit_list
*common
= NULL
;
1833 struct commit_list
*remotes
= NULL
;
1835 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1836 git_path_merge_msg(the_repository
), 0);
1838 commit_list_insert(base
, &common
);
1839 commit_list_insert(next
, &remotes
);
1840 res
|= try_merge_command(the_repository
, opts
->strategy
,
1841 opts
->xopts_nr
, (const char **)opts
->xopts
,
1842 common
, oid_to_hex(&head
), remotes
);
1843 free_commit_list(common
);
1844 free_commit_list(remotes
);
1846 strbuf_release(&msgbuf
);
1849 * If the merge was clean or if it failed due to conflict, we write
1850 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1851 * However, if the merge did not even start, then we don't want to
1854 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1855 update_ref(NULL
, "CHERRY_PICK_HEAD", &commit
->object
.oid
, NULL
,
1856 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1858 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1859 update_ref(NULL
, "REVERT_HEAD", &commit
->object
.oid
, NULL
,
1860 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
1864 error(command
== TODO_REVERT
1865 ? _("could not revert %s... %s")
1866 : _("could not apply %s... %s"),
1867 short_commit_name(commit
), msg
.subject
);
1868 print_advice(res
== 1, opts
);
1869 repo_rerere(the_repository
, opts
->allow_rerere_auto
);
1873 allow
= allow_empty(opts
, commit
);
1878 flags
|= ALLOW_EMPTY
;
1879 if (!opts
->no_commit
) {
1881 if (author
|| command
== TODO_REVERT
|| (flags
& AMEND_MSG
))
1882 res
= do_commit(msg_file
, author
, opts
, flags
);
1884 res
= error(_("unable to parse commit author"));
1887 if (!res
&& final_fixup
) {
1888 unlink(rebase_path_fixup_msg());
1889 unlink(rebase_path_squash_msg());
1890 unlink(rebase_path_current_fixups());
1891 strbuf_reset(&opts
->current_fixups
);
1892 opts
->current_fixup_count
= 0;
1896 free_message(commit
, &msg
);
1898 update_abort_safety_file();
1903 static int prepare_revs(struct replay_opts
*opts
)
1906 * picking (but not reverting) ranges (but not individual revisions)
1907 * should be done in reverse
1909 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1910 opts
->revs
->reverse
^= 1;
1912 if (prepare_revision_walk(opts
->revs
))
1913 return error(_("revision walk setup failed"));
1918 static int read_and_refresh_cache(struct replay_opts
*opts
)
1920 struct lock_file index_lock
= LOCK_INIT
;
1921 int index_fd
= hold_locked_index(&index_lock
, 0);
1922 if (read_index_preload(&the_index
, NULL
, 0) < 0) {
1923 rollback_lock_file(&index_lock
);
1924 return error(_("git %s: failed to read the index"),
1925 _(action_name(opts
)));
1927 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1928 if (index_fd
>= 0) {
1929 if (write_locked_index(&the_index
, &index_lock
,
1930 COMMIT_LOCK
| SKIP_IF_UNCHANGED
)) {
1931 return error(_("git %s: failed to refresh the index"),
1932 _(action_name(opts
)));
1938 enum todo_item_flags
{
1939 TODO_EDIT_MERGE_MSG
= 1
1943 enum todo_command command
;
1944 struct commit
*commit
;
1948 size_t offset_in_buf
;
1953 struct todo_item
*items
;
1954 int nr
, alloc
, current
;
1955 int done_nr
, total_nr
;
1956 struct stat_data stat
;
1959 #define TODO_LIST_INIT { STRBUF_INIT }
1961 static void todo_list_release(struct todo_list
*todo_list
)
1963 strbuf_release(&todo_list
->buf
);
1964 FREE_AND_NULL(todo_list
->items
);
1965 todo_list
->nr
= todo_list
->alloc
= 0;
1968 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1970 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1971 return todo_list
->items
+ todo_list
->nr
++;
1974 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1976 struct object_id commit_oid
;
1977 char *end_of_object_name
;
1978 int i
, saved
, status
, padding
;
1983 bol
+= strspn(bol
, " \t");
1985 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1986 item
->command
= TODO_COMMENT
;
1987 item
->commit
= NULL
;
1989 item
->arg_len
= eol
- bol
;
1993 for (i
= 0; i
< TODO_COMMENT
; i
++)
1994 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1997 } else if ((bol
+ 1 == eol
|| bol
[1] == ' ') &&
1998 *bol
== todo_command_info
[i
].c
) {
2003 if (i
>= TODO_COMMENT
)
2006 /* Eat up extra spaces/ tabs before object name */
2007 padding
= strspn(bol
, " \t");
2010 if (item
->command
== TODO_NOOP
|| item
->command
== TODO_BREAK
) {
2012 return error(_("%s does not accept arguments: '%s'"),
2013 command_to_string(item
->command
), bol
);
2014 item
->commit
= NULL
;
2016 item
->arg_len
= eol
- bol
;
2021 return error(_("missing arguments for %s"),
2022 command_to_string(item
->command
));
2024 if (item
->command
== TODO_EXEC
|| item
->command
== TODO_LABEL
||
2025 item
->command
== TODO_RESET
) {
2026 item
->commit
= NULL
;
2028 item
->arg_len
= (int)(eol
- bol
);
2032 if (item
->command
== TODO_MERGE
) {
2033 if (skip_prefix(bol
, "-C", &bol
))
2034 bol
+= strspn(bol
, " \t");
2035 else if (skip_prefix(bol
, "-c", &bol
)) {
2036 bol
+= strspn(bol
, " \t");
2037 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2039 item
->flags
|= TODO_EDIT_MERGE_MSG
;
2040 item
->commit
= NULL
;
2042 item
->arg_len
= (int)(eol
- bol
);
2047 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
2048 saved
= *end_of_object_name
;
2049 *end_of_object_name
= '\0';
2050 status
= get_oid(bol
, &commit_oid
);
2051 *end_of_object_name
= saved
;
2053 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
2054 item
->arg_len
= (int)(eol
- item
->arg
);
2059 item
->commit
= lookup_commit_reference(the_repository
, &commit_oid
);
2060 return !item
->commit
;
2063 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
2065 struct todo_item
*item
;
2066 char *p
= buf
, *next_p
;
2067 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
2069 for (i
= 1; *p
; i
++, p
= next_p
) {
2070 char *eol
= strchrnul(p
, '\n');
2072 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
2074 if (p
!= eol
&& eol
[-1] == '\r')
2075 eol
--; /* strip Carriage Return */
2077 item
= append_new_todo(todo_list
);
2078 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
2079 if (parse_insn_line(item
, p
, eol
)) {
2080 res
= error(_("invalid line %d: %.*s"),
2081 i
, (int)(eol
- p
), p
);
2082 item
->command
= TODO_NOOP
;
2087 else if (is_fixup(item
->command
))
2088 return error(_("cannot '%s' without a previous commit"),
2089 command_to_string(item
->command
));
2090 else if (!is_noop(item
->command
))
2097 static int count_commands(struct todo_list
*todo_list
)
2101 for (i
= 0; i
< todo_list
->nr
; i
++)
2102 if (todo_list
->items
[i
].command
!= TODO_COMMENT
)
2108 static int get_item_line_offset(struct todo_list
*todo_list
, int index
)
2110 return index
< todo_list
->nr
?
2111 todo_list
->items
[index
].offset_in_buf
: todo_list
->buf
.len
;
2114 static const char *get_item_line(struct todo_list
*todo_list
, int index
)
2116 return todo_list
->buf
.buf
+ get_item_line_offset(todo_list
, index
);
2119 static int get_item_line_length(struct todo_list
*todo_list
, int index
)
2121 return get_item_line_offset(todo_list
, index
+ 1)
2122 - get_item_line_offset(todo_list
, index
);
2125 static ssize_t
strbuf_read_file_or_whine(struct strbuf
*sb
, const char *path
)
2130 fd
= open(path
, O_RDONLY
);
2132 return error_errno(_("could not open '%s'"), path
);
2133 len
= strbuf_read(sb
, fd
, 0);
2136 return error(_("could not read '%s'."), path
);
2140 static int read_populate_todo(struct todo_list
*todo_list
,
2141 struct replay_opts
*opts
)
2144 const char *todo_file
= get_todo_path(opts
);
2147 strbuf_reset(&todo_list
->buf
);
2148 if (strbuf_read_file_or_whine(&todo_list
->buf
, todo_file
) < 0)
2151 res
= stat(todo_file
, &st
);
2153 return error(_("could not stat '%s'"), todo_file
);
2154 fill_stat_data(&todo_list
->stat
, &st
);
2156 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
2158 if (is_rebase_i(opts
))
2159 return error(_("please fix this using "
2160 "'git rebase --edit-todo'."));
2161 return error(_("unusable instruction sheet: '%s'"), todo_file
);
2164 if (!todo_list
->nr
&&
2165 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
2166 return error(_("no commits parsed."));
2168 if (!is_rebase_i(opts
)) {
2169 enum todo_command valid
=
2170 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
2173 for (i
= 0; i
< todo_list
->nr
; i
++)
2174 if (valid
== todo_list
->items
[i
].command
)
2176 else if (valid
== TODO_PICK
)
2177 return error(_("cannot cherry-pick during a revert."));
2179 return error(_("cannot revert during a cherry-pick."));
2182 if (is_rebase_i(opts
)) {
2183 struct todo_list done
= TODO_LIST_INIT
;
2184 FILE *f
= fopen_or_warn(rebase_path_msgtotal(), "w");
2186 if (strbuf_read_file(&done
.buf
, rebase_path_done(), 0) > 0 &&
2187 !parse_insn_buffer(done
.buf
.buf
, &done
))
2188 todo_list
->done_nr
= count_commands(&done
);
2190 todo_list
->done_nr
= 0;
2192 todo_list
->total_nr
= todo_list
->done_nr
2193 + count_commands(todo_list
);
2194 todo_list_release(&done
);
2197 fprintf(f
, "%d\n", todo_list
->total_nr
);
2205 static int git_config_string_dup(char **dest
,
2206 const char *var
, const char *value
)
2209 return config_error_nonbool(var
);
2211 *dest
= xstrdup(value
);
2215 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
2217 struct replay_opts
*opts
= data
;
2222 else if (!strcmp(key
, "options.no-commit"))
2223 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
2224 else if (!strcmp(key
, "options.edit"))
2225 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
2226 else if (!strcmp(key
, "options.signoff"))
2227 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
2228 else if (!strcmp(key
, "options.record-origin"))
2229 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
2230 else if (!strcmp(key
, "options.allow-ff"))
2231 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
2232 else if (!strcmp(key
, "options.mainline"))
2233 opts
->mainline
= git_config_int(key
, value
);
2234 else if (!strcmp(key
, "options.strategy"))
2235 git_config_string_dup(&opts
->strategy
, key
, value
);
2236 else if (!strcmp(key
, "options.gpg-sign"))
2237 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
2238 else if (!strcmp(key
, "options.strategy-option")) {
2239 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
2240 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
2241 } else if (!strcmp(key
, "options.allow-rerere-auto"))
2242 opts
->allow_rerere_auto
=
2243 git_config_bool_or_int(key
, value
, &error_flag
) ?
2244 RERERE_AUTOUPDATE
: RERERE_NOAUTOUPDATE
;
2246 return error(_("invalid key: %s"), key
);
2249 return error(_("invalid value for %s: %s"), key
, value
);
2254 void parse_strategy_opts(struct replay_opts
*opts
, char *raw_opts
)
2257 char *strategy_opts_string
= raw_opts
;
2259 if (*strategy_opts_string
== ' ')
2260 strategy_opts_string
++;
2262 opts
->xopts_nr
= split_cmdline(strategy_opts_string
,
2263 (const char ***)&opts
->xopts
);
2264 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
2265 const char *arg
= opts
->xopts
[i
];
2267 skip_prefix(arg
, "--", &arg
);
2268 opts
->xopts
[i
] = xstrdup(arg
);
2272 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
2275 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
2277 opts
->strategy
= strbuf_detach(buf
, NULL
);
2278 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
2281 parse_strategy_opts(opts
, buf
->buf
);
2284 static int read_populate_opts(struct replay_opts
*opts
)
2286 if (is_rebase_i(opts
)) {
2287 struct strbuf buf
= STRBUF_INIT
;
2289 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
2290 if (!starts_with(buf
.buf
, "-S"))
2293 free(opts
->gpg_sign
);
2294 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
2299 if (read_oneliner(&buf
, rebase_path_allow_rerere_autoupdate(), 1)) {
2300 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
2301 opts
->allow_rerere_auto
= RERERE_AUTOUPDATE
;
2302 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
2303 opts
->allow_rerere_auto
= RERERE_NOAUTOUPDATE
;
2307 if (file_exists(rebase_path_verbose()))
2310 if (file_exists(rebase_path_signoff())) {
2315 read_strategy_opts(opts
, &buf
);
2316 strbuf_release(&buf
);
2318 if (read_oneliner(&opts
->current_fixups
,
2319 rebase_path_current_fixups(), 1)) {
2320 const char *p
= opts
->current_fixups
.buf
;
2321 opts
->current_fixup_count
= 1;
2322 while ((p
= strchr(p
, '\n'))) {
2323 opts
->current_fixup_count
++;
2328 if (read_oneliner(&buf
, rebase_path_squash_onto(), 0)) {
2329 if (get_oid_hex(buf
.buf
, &opts
->squash_onto
) < 0)
2330 return error(_("unusable squash-onto"));
2331 opts
->have_squash_onto
= 1;
2337 if (!file_exists(git_path_opts_file()))
2340 * The function git_parse_source(), called from git_config_from_file(),
2341 * may die() in case of a syntactically incorrect file. We do not care
2342 * about this case, though, because we wrote that file ourselves, so we
2343 * are pretty certain that it is syntactically correct.
2345 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
2346 return error(_("malformed options sheet: '%s'"),
2347 git_path_opts_file());
2351 static void write_strategy_opts(struct replay_opts
*opts
)
2354 struct strbuf buf
= STRBUF_INIT
;
2356 for (i
= 0; i
< opts
->xopts_nr
; ++i
)
2357 strbuf_addf(&buf
, " --%s", opts
->xopts
[i
]);
2359 write_file(rebase_path_strategy_opts(), "%s\n", buf
.buf
);
2360 strbuf_release(&buf
);
2363 int write_basic_state(struct replay_opts
*opts
, const char *head_name
,
2364 const char *onto
, const char *orig_head
)
2366 const char *quiet
= getenv("GIT_QUIET");
2369 write_file(rebase_path_head_name(), "%s\n", head_name
);
2371 write_file(rebase_path_onto(), "%s\n", onto
);
2373 write_file(rebase_path_orig_head(), "%s\n", orig_head
);
2376 write_file(rebase_path_quiet(), "%s\n", quiet
);
2378 write_file(rebase_path_quiet(), "\n");
2381 write_file(rebase_path_verbose(), "%s", "");
2383 write_file(rebase_path_strategy(), "%s\n", opts
->strategy
);
2384 if (opts
->xopts_nr
> 0)
2385 write_strategy_opts(opts
);
2387 if (opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
)
2388 write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
2389 else if (opts
->allow_rerere_auto
== RERERE_NOAUTOUPDATE
)
2390 write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
2393 write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts
->gpg_sign
);
2395 write_file(rebase_path_signoff(), "--signoff\n");
2400 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
2401 struct replay_opts
*opts
)
2403 enum todo_command command
= opts
->action
== REPLAY_PICK
?
2404 TODO_PICK
: TODO_REVERT
;
2405 const char *command_string
= todo_command_info
[command
].str
;
2406 struct commit
*commit
;
2408 if (prepare_revs(opts
))
2411 while ((commit
= get_revision(opts
->revs
))) {
2412 struct todo_item
*item
= append_new_todo(todo_list
);
2413 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2414 const char *subject
;
2417 item
->command
= command
;
2418 item
->commit
= commit
;
2421 item
->offset_in_buf
= todo_list
->buf
.len
;
2422 subject_len
= find_commit_subject(commit_buffer
, &subject
);
2423 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
2424 short_commit_name(commit
), subject_len
, subject
);
2425 unuse_commit_buffer(commit
, commit_buffer
);
2429 return error(_("empty commit set passed"));
2434 static int create_seq_dir(void)
2436 if (file_exists(git_path_seq_dir())) {
2437 error(_("a cherry-pick or revert is already in progress"));
2438 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
2440 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
2441 return error_errno(_("could not create sequencer directory '%s'"),
2442 git_path_seq_dir());
2446 static int save_head(const char *head
)
2448 struct lock_file head_lock
= LOCK_INIT
;
2449 struct strbuf buf
= STRBUF_INIT
;
2453 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
2455 return error_errno(_("could not lock HEAD"));
2456 strbuf_addf(&buf
, "%s\n", head
);
2457 written
= write_in_full(fd
, buf
.buf
, buf
.len
);
2458 strbuf_release(&buf
);
2460 error_errno(_("could not write to '%s'"), git_path_head_file());
2461 rollback_lock_file(&head_lock
);
2464 if (commit_lock_file(&head_lock
) < 0)
2465 return error(_("failed to finalize '%s'"), git_path_head_file());
2469 static int rollback_is_safe(void)
2471 struct strbuf sb
= STRBUF_INIT
;
2472 struct object_id expected_head
, actual_head
;
2474 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
2476 if (get_oid_hex(sb
.buf
, &expected_head
)) {
2477 strbuf_release(&sb
);
2478 die(_("could not parse %s"), git_path_abort_safety_file());
2480 strbuf_release(&sb
);
2482 else if (errno
== ENOENT
)
2483 oidclr(&expected_head
);
2485 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
2487 if (get_oid("HEAD", &actual_head
))
2488 oidclr(&actual_head
);
2490 return oideq(&actual_head
, &expected_head
);
2493 static int reset_for_rollback(const struct object_id
*oid
)
2495 const char *argv
[4]; /* reset --merge <arg> + NULL */
2498 argv
[1] = "--merge";
2499 argv
[2] = oid_to_hex(oid
);
2501 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2504 static int rollback_single_pick(void)
2506 struct object_id head_oid
;
2508 if (!file_exists(git_path_cherry_pick_head(the_repository
)) &&
2509 !file_exists(git_path_revert_head(the_repository
)))
2510 return error(_("no cherry-pick or revert in progress"));
2511 if (read_ref_full("HEAD", 0, &head_oid
, NULL
))
2512 return error(_("cannot resolve HEAD"));
2513 if (is_null_oid(&head_oid
))
2514 return error(_("cannot abort from a branch yet to be born"));
2515 return reset_for_rollback(&head_oid
);
2518 int sequencer_rollback(struct replay_opts
*opts
)
2521 struct object_id oid
;
2522 struct strbuf buf
= STRBUF_INIT
;
2525 f
= fopen(git_path_head_file(), "r");
2526 if (!f
&& errno
== ENOENT
) {
2528 * There is no multiple-cherry-pick in progress.
2529 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
2530 * a single-cherry-pick in progress, abort that.
2532 return rollback_single_pick();
2535 return error_errno(_("cannot open '%s'"), git_path_head_file());
2536 if (strbuf_getline_lf(&buf
, f
)) {
2537 error(_("cannot read '%s': %s"), git_path_head_file(),
2538 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
2543 if (parse_oid_hex(buf
.buf
, &oid
, &p
) || *p
!= '\0') {
2544 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
2545 git_path_head_file());
2548 if (is_null_oid(&oid
)) {
2549 error(_("cannot abort from a branch yet to be born"));
2553 if (!rollback_is_safe()) {
2554 /* Do not error, just do not rollback */
2555 warning(_("You seem to have moved HEAD. "
2556 "Not rewinding, check your HEAD!"));
2558 if (reset_for_rollback(&oid
))
2560 strbuf_release(&buf
);
2561 return sequencer_remove_state(opts
);
2563 strbuf_release(&buf
);
2567 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
2569 struct lock_file todo_lock
= LOCK_INIT
;
2570 const char *todo_path
= get_todo_path(opts
);
2571 int next
= todo_list
->current
, offset
, fd
;
2574 * rebase -i writes "git-rebase-todo" without the currently executing
2575 * command, appending it to "done" instead.
2577 if (is_rebase_i(opts
))
2580 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
2582 return error_errno(_("could not lock '%s'"), todo_path
);
2583 offset
= get_item_line_offset(todo_list
, next
);
2584 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
2585 todo_list
->buf
.len
- offset
) < 0)
2586 return error_errno(_("could not write to '%s'"), todo_path
);
2587 if (commit_lock_file(&todo_lock
) < 0)
2588 return error(_("failed to finalize '%s'"), todo_path
);
2590 if (is_rebase_i(opts
) && next
> 0) {
2591 const char *done
= rebase_path_done();
2592 int fd
= open(done
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
2597 if (write_in_full(fd
, get_item_line(todo_list
, next
- 1),
2598 get_item_line_length(todo_list
, next
- 1))
2600 ret
= error_errno(_("could not write to '%s'"), done
);
2602 ret
= error_errno(_("failed to finalize '%s'"), done
);
2608 static int save_opts(struct replay_opts
*opts
)
2610 const char *opts_file
= git_path_opts_file();
2613 if (opts
->no_commit
)
2614 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
2616 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
2618 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
2619 if (opts
->record_origin
)
2620 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
2622 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
2623 if (opts
->mainline
) {
2624 struct strbuf buf
= STRBUF_INIT
;
2625 strbuf_addf(&buf
, "%d", opts
->mainline
);
2626 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
2627 strbuf_release(&buf
);
2630 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
2632 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
2635 for (i
= 0; i
< opts
->xopts_nr
; i
++)
2636 res
|= git_config_set_multivar_in_file_gently(opts_file
,
2637 "options.strategy-option",
2638 opts
->xopts
[i
], "^$", 0);
2640 if (opts
->allow_rerere_auto
)
2641 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-rerere-auto",
2642 opts
->allow_rerere_auto
== RERERE_AUTOUPDATE
?
2647 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
2649 struct strbuf buf
= STRBUF_INIT
;
2650 struct rev_info log_tree_opt
;
2651 const char *subject
, *p
;
2654 p
= short_commit_name(commit
);
2655 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
2657 if (update_ref("rebase", "REBASE_HEAD", &commit
->object
.oid
,
2658 NULL
, REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
))
2659 res
|= error(_("could not update %s"), "REBASE_HEAD");
2661 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
2662 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2663 repo_init_revisions(the_repository
, &log_tree_opt
, NULL
);
2664 log_tree_opt
.abbrev
= 0;
2665 log_tree_opt
.diff
= 1;
2666 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
2667 log_tree_opt
.disable_stdin
= 1;
2668 log_tree_opt
.no_commit_id
= 1;
2669 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
2670 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
2671 if (!log_tree_opt
.diffopt
.file
)
2672 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
2674 res
|= log_tree_commit(&log_tree_opt
, commit
);
2675 fclose(log_tree_opt
.diffopt
.file
);
2679 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
2680 if (!file_exists(buf
.buf
)) {
2681 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
2682 find_commit_subject(commit_buffer
, &subject
);
2683 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
2684 unuse_commit_buffer(commit
, commit_buffer
);
2686 strbuf_release(&buf
);
2691 static int intend_to_amend(void)
2693 struct object_id head
;
2696 if (get_oid("HEAD", &head
))
2697 return error(_("cannot read HEAD"));
2699 p
= oid_to_hex(&head
);
2700 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
2703 static int error_with_patch(struct commit
*commit
,
2704 const char *subject
, int subject_len
,
2705 struct replay_opts
*opts
, int exit_code
, int to_amend
)
2708 if (make_patch(commit
, opts
))
2710 } else if (copy_file(rebase_path_message(),
2711 git_path_merge_msg(the_repository
), 0666))
2712 return error(_("unable to copy '%s' to '%s'"),
2713 git_path_merge_msg(the_repository
), rebase_path_message());
2716 if (intend_to_amend())
2720 _("You can amend the commit now, with\n"
2722 " git commit --amend %s\n"
2724 "Once you are satisfied with your changes, run\n"
2726 " git rebase --continue\n"),
2727 gpg_sign_opt_quoted(opts
));
2728 } else if (exit_code
) {
2730 fprintf_ln(stderr
, _("Could not apply %s... %.*s"),
2731 short_commit_name(commit
), subject_len
, subject
);
2734 * We don't have the hash of the parent so
2735 * just print the line from the todo file.
2737 fprintf_ln(stderr
, _("Could not merge %.*s"),
2738 subject_len
, subject
);
2744 static int error_failed_squash(struct commit
*commit
,
2745 struct replay_opts
*opts
, int subject_len
, const char *subject
)
2747 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
2748 return error(_("could not copy '%s' to '%s'"),
2749 rebase_path_squash_msg(), rebase_path_message());
2750 unlink(git_path_merge_msg(the_repository
));
2751 if (copy_file(git_path_merge_msg(the_repository
), rebase_path_message(), 0666))
2752 return error(_("could not copy '%s' to '%s'"),
2753 rebase_path_message(),
2754 git_path_merge_msg(the_repository
));
2755 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
2758 static int do_exec(const char *command_line
)
2760 struct argv_array child_env
= ARGV_ARRAY_INIT
;
2761 const char *child_argv
[] = { NULL
, NULL
};
2764 fprintf(stderr
, "Executing: %s\n", command_line
);
2765 child_argv
[0] = command_line
;
2766 argv_array_pushf(&child_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
2767 argv_array_pushf(&child_env
, "GIT_WORK_TREE=%s",
2768 absolute_path(get_git_work_tree()));
2769 status
= run_command_v_opt_cd_env(child_argv
, RUN_USING_SHELL
, NULL
,
2772 /* force re-reading of the cache */
2773 if (discard_cache() < 0 || read_cache() < 0)
2774 return error(_("could not read index"));
2776 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
2779 warning(_("execution failed: %s\n%s"
2780 "You can fix the problem, and then run\n"
2782 " git rebase --continue\n"
2785 dirty
? N_("and made changes to the index and/or the "
2786 "working tree\n") : "");
2788 /* command not found */
2791 warning(_("execution succeeded: %s\nbut "
2792 "left changes to the index and/or the working tree\n"
2793 "Commit or stash your changes, and then run\n"
2795 " git rebase --continue\n"
2796 "\n"), command_line
);
2800 argv_array_clear(&child_env
);
2805 static int safe_append(const char *filename
, const char *fmt
, ...)
2808 struct lock_file lock
= LOCK_INIT
;
2809 int fd
= hold_lock_file_for_update(&lock
, filename
,
2810 LOCK_REPORT_ON_ERROR
);
2811 struct strbuf buf
= STRBUF_INIT
;
2816 if (strbuf_read_file(&buf
, filename
, 0) < 0 && errno
!= ENOENT
) {
2817 error_errno(_("could not read '%s'"), filename
);
2818 rollback_lock_file(&lock
);
2821 strbuf_complete(&buf
, '\n');
2823 strbuf_vaddf(&buf
, fmt
, ap
);
2826 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
2827 error_errno(_("could not write to '%s'"), filename
);
2828 strbuf_release(&buf
);
2829 rollback_lock_file(&lock
);
2832 if (commit_lock_file(&lock
) < 0) {
2833 strbuf_release(&buf
);
2834 rollback_lock_file(&lock
);
2835 return error(_("failed to finalize '%s'"), filename
);
2838 strbuf_release(&buf
);
2842 static int do_label(const char *name
, int len
)
2844 struct ref_store
*refs
= get_main_ref_store(the_repository
);
2845 struct ref_transaction
*transaction
;
2846 struct strbuf ref_name
= STRBUF_INIT
, err
= STRBUF_INIT
;
2847 struct strbuf msg
= STRBUF_INIT
;
2849 struct object_id head_oid
;
2851 if (len
== 1 && *name
== '#')
2852 return error(_("illegal label name: '%.*s'"), len
, name
);
2854 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
2855 strbuf_addf(&msg
, "rebase -i (label) '%.*s'", len
, name
);
2857 transaction
= ref_store_transaction_begin(refs
, &err
);
2859 error("%s", err
.buf
);
2861 } else if (get_oid("HEAD", &head_oid
)) {
2862 error(_("could not read HEAD"));
2864 } else if (ref_transaction_update(transaction
, ref_name
.buf
, &head_oid
,
2865 NULL
, 0, msg
.buf
, &err
) < 0 ||
2866 ref_transaction_commit(transaction
, &err
)) {
2867 error("%s", err
.buf
);
2870 ref_transaction_free(transaction
);
2871 strbuf_release(&err
);
2872 strbuf_release(&msg
);
2875 ret
= safe_append(rebase_path_refs_to_delete(),
2876 "%s\n", ref_name
.buf
);
2877 strbuf_release(&ref_name
);
2882 static const char *reflog_message(struct replay_opts
*opts
,
2883 const char *sub_action
, const char *fmt
, ...);
2885 static int do_reset(const char *name
, int len
, struct replay_opts
*opts
)
2887 struct strbuf ref_name
= STRBUF_INIT
;
2888 struct object_id oid
;
2889 struct lock_file lock
= LOCK_INIT
;
2890 struct tree_desc desc
;
2892 struct unpack_trees_options unpack_tree_opts
;
2895 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0)
2898 if (len
== 10 && !strncmp("[new root]", name
, len
)) {
2899 if (!opts
->have_squash_onto
) {
2901 if (commit_tree("", 0, the_hash_algo
->empty_tree
,
2902 NULL
, &opts
->squash_onto
,
2904 return error(_("writing fake root commit"));
2905 opts
->have_squash_onto
= 1;
2906 hex
= oid_to_hex(&opts
->squash_onto
);
2907 if (write_message(hex
, strlen(hex
),
2908 rebase_path_squash_onto(), 0))
2909 return error(_("writing squash-onto"));
2911 oidcpy(&oid
, &opts
->squash_onto
);
2913 /* Determine the length of the label */
2914 for (i
= 0; i
< len
; i
++)
2915 if (isspace(name
[i
]))
2918 strbuf_addf(&ref_name
, "refs/rewritten/%.*s", len
, name
);
2919 if (get_oid(ref_name
.buf
, &oid
) &&
2920 get_oid(ref_name
.buf
+ strlen("refs/rewritten/"), &oid
)) {
2921 error(_("could not read '%s'"), ref_name
.buf
);
2922 rollback_lock_file(&lock
);
2923 strbuf_release(&ref_name
);
2928 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
2929 setup_unpack_trees_porcelain(&unpack_tree_opts
, "reset");
2930 unpack_tree_opts
.head_idx
= 1;
2931 unpack_tree_opts
.src_index
= &the_index
;
2932 unpack_tree_opts
.dst_index
= &the_index
;
2933 unpack_tree_opts
.fn
= oneway_merge
;
2934 unpack_tree_opts
.merge
= 1;
2935 unpack_tree_opts
.update
= 1;
2937 if (read_cache_unmerged()) {
2938 rollback_lock_file(&lock
);
2939 strbuf_release(&ref_name
);
2940 return error_resolve_conflict(_(action_name(opts
)));
2943 if (!fill_tree_descriptor(&desc
, &oid
)) {
2944 error(_("failed to find tree of %s"), oid_to_hex(&oid
));
2945 rollback_lock_file(&lock
);
2946 free((void *)desc
.buffer
);
2947 strbuf_release(&ref_name
);
2951 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
2952 rollback_lock_file(&lock
);
2953 free((void *)desc
.buffer
);
2954 strbuf_release(&ref_name
);
2958 tree
= parse_tree_indirect(&oid
);
2959 prime_cache_tree(&the_index
, tree
);
2961 if (write_locked_index(&the_index
, &lock
, COMMIT_LOCK
) < 0)
2962 ret
= error(_("could not write index"));
2963 free((void *)desc
.buffer
);
2966 ret
= update_ref(reflog_message(opts
, "reset", "'%.*s'",
2967 len
, name
), "HEAD", &oid
,
2968 NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
2970 strbuf_release(&ref_name
);
2974 static struct commit
*lookup_label(const char *label
, int len
,
2977 struct commit
*commit
;
2980 strbuf_addf(buf
, "refs/rewritten/%.*s", len
, label
);
2981 commit
= lookup_commit_reference_by_name(buf
->buf
);
2983 /* fall back to non-rewritten ref or commit */
2984 strbuf_splice(buf
, 0, strlen("refs/rewritten/"), "", 0);
2985 commit
= lookup_commit_reference_by_name(buf
->buf
);
2989 error(_("could not resolve '%s'"), buf
->buf
);
2994 static int do_merge(struct commit
*commit
, const char *arg
, int arg_len
,
2995 int flags
, struct replay_opts
*opts
)
2997 int run_commit_flags
= (flags
& TODO_EDIT_MERGE_MSG
) ?
2998 EDIT_MSG
| VERIFY_MSG
: 0;
2999 struct strbuf ref_name
= STRBUF_INIT
;
3000 struct commit
*head_commit
, *merge_commit
, *i
;
3001 struct commit_list
*bases
, *j
, *reversed
= NULL
;
3002 struct commit_list
*to_merge
= NULL
, **tail
= &to_merge
;
3003 struct merge_options o
;
3004 int merge_arg_len
, oneline_offset
, can_fast_forward
, ret
, k
;
3005 static struct lock_file lock
;
3008 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0) {
3013 head_commit
= lookup_commit_reference_by_name("HEAD");
3015 ret
= error(_("cannot merge without a current revision"));
3020 * For octopus merges, the arg starts with the list of revisions to be
3021 * merged. The list is optionally followed by '#' and the oneline.
3023 merge_arg_len
= oneline_offset
= arg_len
;
3024 for (p
= arg
; p
- arg
< arg_len
; p
+= strspn(p
, " \t\n")) {
3027 if (*p
== '#' && (!p
[1] || isspace(p
[1]))) {
3028 p
+= 1 + strspn(p
+ 1, " \t\n");
3029 oneline_offset
= p
- arg
;
3032 k
= strcspn(p
, " \t\n");
3035 merge_commit
= lookup_label(p
, k
, &ref_name
);
3036 if (!merge_commit
) {
3037 ret
= error(_("unable to parse '%.*s'"), k
, p
);
3040 tail
= &commit_list_insert(merge_commit
, tail
)->next
;
3042 merge_arg_len
= p
- arg
;
3046 ret
= error(_("nothing to merge: '%.*s'"), arg_len
, arg
);
3050 if (opts
->have_squash_onto
&&
3051 oideq(&head_commit
->object
.oid
, &opts
->squash_onto
)) {
3053 * When the user tells us to "merge" something into a
3054 * "[new root]", let's simply fast-forward to the merge head.
3056 rollback_lock_file(&lock
);
3058 ret
= error(_("octopus merge cannot be executed on "
3059 "top of a [new root]"));
3061 ret
= fast_forward_to(&to_merge
->item
->object
.oid
,
3062 &head_commit
->object
.oid
, 0,
3068 const char *message
= get_commit_buffer(commit
, NULL
);
3073 ret
= error(_("could not get commit message of '%s'"),
3074 oid_to_hex(&commit
->object
.oid
));
3077 write_author_script(message
);
3078 find_commit_subject(message
, &body
);
3080 ret
= write_message(body
, len
, git_path_merge_msg(the_repository
), 0);
3081 unuse_commit_buffer(commit
, message
);
3083 error_errno(_("could not write '%s'"),
3084 git_path_merge_msg(the_repository
));
3088 struct strbuf buf
= STRBUF_INIT
;
3091 strbuf_addf(&buf
, "author %s", git_author_info(0));
3092 write_author_script(buf
.buf
);
3095 if (oneline_offset
< arg_len
) {
3096 p
= arg
+ oneline_offset
;
3097 len
= arg_len
- oneline_offset
;
3099 strbuf_addf(&buf
, "Merge %s '%.*s'",
3100 to_merge
->next
? "branches" : "branch",
3101 merge_arg_len
, arg
);
3106 ret
= write_message(p
, len
, git_path_merge_msg(the_repository
), 0);
3107 strbuf_release(&buf
);
3109 error_errno(_("could not write '%s'"),
3110 git_path_merge_msg(the_repository
));
3116 * If HEAD is not identical to the first parent of the original merge
3117 * commit, we cannot fast-forward.
3119 can_fast_forward
= opts
->allow_ff
&& commit
&& commit
->parents
&&
3120 oideq(&commit
->parents
->item
->object
.oid
,
3121 &head_commit
->object
.oid
);
3124 * If any merge head is different from the original one, we cannot
3127 if (can_fast_forward
) {
3128 struct commit_list
*p
= commit
->parents
->next
;
3130 for (j
= to_merge
; j
&& p
; j
= j
->next
, p
= p
->next
)
3131 if (!oideq(&j
->item
->object
.oid
,
3132 &p
->item
->object
.oid
)) {
3133 can_fast_forward
= 0;
3137 * If the number of merge heads differs from the original merge
3138 * commit, we cannot fast-forward.
3141 can_fast_forward
= 0;
3144 if (can_fast_forward
) {
3145 rollback_lock_file(&lock
);
3146 ret
= fast_forward_to(&commit
->object
.oid
,
3147 &head_commit
->object
.oid
, 0, opts
);
3151 if (to_merge
->next
) {
3153 struct child_process cmd
= CHILD_PROCESS_INIT
;
3155 if (read_env_script(&cmd
.env_array
)) {
3156 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
3158 ret
= error(_(staged_changes_advice
), gpg_opt
, gpg_opt
);
3163 argv_array_push(&cmd
.args
, "merge");
3164 argv_array_push(&cmd
.args
, "-s");
3165 argv_array_push(&cmd
.args
, "octopus");
3166 argv_array_push(&cmd
.args
, "--no-edit");
3167 argv_array_push(&cmd
.args
, "--no-ff");
3168 argv_array_push(&cmd
.args
, "--no-log");
3169 argv_array_push(&cmd
.args
, "--no-stat");
3170 argv_array_push(&cmd
.args
, "-F");
3171 argv_array_push(&cmd
.args
, git_path_merge_msg(the_repository
));
3173 argv_array_push(&cmd
.args
, opts
->gpg_sign
);
3175 /* Add the tips to be merged */
3176 for (j
= to_merge
; j
; j
= j
->next
)
3177 argv_array_push(&cmd
.args
,
3178 oid_to_hex(&j
->item
->object
.oid
));
3180 strbuf_release(&ref_name
);
3181 unlink(git_path_cherry_pick_head(the_repository
));
3182 rollback_lock_file(&lock
);
3184 rollback_lock_file(&lock
);
3185 ret
= run_command(&cmd
);
3187 /* force re-reading of the cache */
3188 if (!ret
&& (discard_cache() < 0 || read_cache() < 0))
3189 ret
= error(_("could not read index"));
3193 merge_commit
= to_merge
->item
;
3194 write_message(oid_to_hex(&merge_commit
->object
.oid
), GIT_SHA1_HEXSZ
,
3195 git_path_merge_head(the_repository
), 0);
3196 write_message("no-ff", 5, git_path_merge_mode(the_repository
), 0);
3198 bases
= get_merge_bases(head_commit
, merge_commit
);
3199 if (bases
&& oideq(&merge_commit
->object
.oid
,
3200 &bases
->item
->object
.oid
)) {
3202 /* skip merging an ancestor of HEAD */
3206 for (j
= bases
; j
; j
= j
->next
)
3207 commit_list_insert(j
->item
, &reversed
);
3208 free_commit_list(bases
);
3211 init_merge_options(&o
);
3213 o
.branch2
= ref_name
.buf
;
3214 o
.buffer_output
= 2;
3216 ret
= merge_recursive(&o
, head_commit
, merge_commit
, reversed
, &i
);
3218 fputs(o
.obuf
.buf
, stdout
);
3219 strbuf_release(&o
.obuf
);
3221 error(_("could not even attempt to merge '%.*s'"),
3222 merge_arg_len
, arg
);
3226 * The return value of merge_recursive() is 1 on clean, and 0 on
3229 * Let's reverse that, so that do_merge() returns 0 upon success and
3230 * 1 upon failed merge (keeping the return value -1 for the cases where
3231 * we will want to reschedule the `merge` command).
3235 if (active_cache_changed
&&
3236 write_locked_index(&the_index
, &lock
, COMMIT_LOCK
)) {
3237 ret
= error(_("merge: Unable to write new index file"));
3241 rollback_lock_file(&lock
);
3243 repo_rerere(the_repository
, opts
->allow_rerere_auto
);
3246 * In case of problems, we now want to return a positive
3247 * value (a negative one would indicate that the `merge`
3248 * command needs to be rescheduled).
3250 ret
= !!run_git_commit(git_path_merge_msg(the_repository
), opts
,
3254 strbuf_release(&ref_name
);
3255 rollback_lock_file(&lock
);
3256 free_commit_list(to_merge
);
3260 static int is_final_fixup(struct todo_list
*todo_list
)
3262 int i
= todo_list
->current
;
3264 if (!is_fixup(todo_list
->items
[i
].command
))
3267 while (++i
< todo_list
->nr
)
3268 if (is_fixup(todo_list
->items
[i
].command
))
3270 else if (!is_noop(todo_list
->items
[i
].command
))
3275 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
3279 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
3280 if (!is_noop(todo_list
->items
[i
].command
))
3281 return todo_list
->items
[i
].command
;
3286 static int apply_autostash(struct replay_opts
*opts
)
3288 struct strbuf stash_sha1
= STRBUF_INIT
;
3289 struct child_process child
= CHILD_PROCESS_INIT
;
3292 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
3293 strbuf_release(&stash_sha1
);
3296 strbuf_trim(&stash_sha1
);
3299 child
.no_stdout
= 1;
3300 child
.no_stderr
= 1;
3301 argv_array_push(&child
.args
, "stash");
3302 argv_array_push(&child
.args
, "apply");
3303 argv_array_push(&child
.args
, stash_sha1
.buf
);
3304 if (!run_command(&child
))
3305 fprintf(stderr
, _("Applied autostash.\n"));
3307 struct child_process store
= CHILD_PROCESS_INIT
;
3310 argv_array_push(&store
.args
, "stash");
3311 argv_array_push(&store
.args
, "store");
3312 argv_array_push(&store
.args
, "-m");
3313 argv_array_push(&store
.args
, "autostash");
3314 argv_array_push(&store
.args
, "-q");
3315 argv_array_push(&store
.args
, stash_sha1
.buf
);
3316 if (run_command(&store
))
3317 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
3320 _("Applying autostash resulted in conflicts.\n"
3321 "Your changes are safe in the stash.\n"
3322 "You can run \"git stash pop\" or"
3323 " \"git stash drop\" at any time.\n"));
3326 strbuf_release(&stash_sha1
);
3330 static const char *reflog_message(struct replay_opts
*opts
,
3331 const char *sub_action
, const char *fmt
, ...)
3334 static struct strbuf buf
= STRBUF_INIT
;
3338 strbuf_addstr(&buf
, action_name(opts
));
3340 strbuf_addf(&buf
, " (%s)", sub_action
);
3342 strbuf_addstr(&buf
, ": ");
3343 strbuf_vaddf(&buf
, fmt
, ap
);
3350 static int run_git_checkout(struct replay_opts
*opts
, const char *commit
,
3353 struct child_process cmd
= CHILD_PROCESS_INIT
;
3357 argv_array_push(&cmd
.args
, "checkout");
3358 argv_array_push(&cmd
.args
, commit
);
3359 argv_array_pushf(&cmd
.env_array
, GIT_REFLOG_ACTION
"=%s", action
);
3362 return run_command(&cmd
);
3364 return run_command_silent_on_success(&cmd
);
3367 int prepare_branch_to_be_rebased(struct replay_opts
*opts
, const char *commit
)
3371 if (commit
&& *commit
) {
3372 action
= reflog_message(opts
, "start", "checkout %s", commit
);
3373 if (run_git_checkout(opts
, commit
, action
))
3374 return error(_("could not checkout %s"), commit
);
3380 static int checkout_onto(struct replay_opts
*opts
,
3381 const char *onto_name
, const char *onto
,
3382 const char *orig_head
)
3384 struct object_id oid
;
3385 const char *action
= reflog_message(opts
, "start", "checkout %s", onto_name
);
3387 if (get_oid(orig_head
, &oid
))
3388 return error(_("%s: not a valid OID"), orig_head
);
3390 if (run_git_checkout(opts
, onto
, action
)) {
3391 apply_autostash(opts
);
3392 sequencer_remove_state(opts
);
3393 return error(_("could not detach HEAD"));
3396 return update_ref(NULL
, "ORIG_HEAD", &oid
, NULL
, 0, UPDATE_REFS_MSG_ON_ERR
);
3399 static int stopped_at_head(void)
3401 struct object_id head
;
3402 struct commit
*commit
;
3403 struct commit_message message
;
3405 if (get_oid("HEAD", &head
) ||
3406 !(commit
= lookup_commit(the_repository
, &head
)) ||
3407 parse_commit(commit
) || get_message(commit
, &message
))
3408 fprintf(stderr
, _("Stopped at HEAD\n"));
3410 fprintf(stderr
, _("Stopped at %s\n"), message
.label
);
3411 free_message(commit
, &message
);
3417 static const char rescheduled_advice
[] =
3418 N_("Could not execute the todo command\n"
3422 "It has been rescheduled; To edit the command before continuing, please\n"
3423 "edit the todo list first:\n"
3425 " git rebase --edit-todo\n"
3426 " git rebase --continue\n");
3428 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
3430 int res
= 0, reschedule
= 0;
3432 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3434 assert(!(opts
->signoff
|| opts
->no_commit
||
3435 opts
->record_origin
|| opts
->edit
));
3436 if (read_and_refresh_cache(opts
))
3439 while (todo_list
->current
< todo_list
->nr
) {
3440 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
3441 if (save_todo(todo_list
, opts
))
3443 if (is_rebase_i(opts
)) {
3444 if (item
->command
!= TODO_COMMENT
) {
3445 FILE *f
= fopen(rebase_path_msgnum(), "w");
3447 todo_list
->done_nr
++;
3450 fprintf(f
, "%d\n", todo_list
->done_nr
);
3453 fprintf(stderr
, "Rebasing (%d/%d)%s",
3455 todo_list
->total_nr
,
3456 opts
->verbose
? "\n" : "\r");
3458 unlink(rebase_path_message());
3459 unlink(rebase_path_author_script());
3460 unlink(rebase_path_stopped_sha());
3461 unlink(rebase_path_amend());
3462 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
3464 if (item
->command
== TODO_BREAK
)
3465 return stopped_at_head();
3467 if (item
->command
<= TODO_SQUASH
) {
3468 if (is_rebase_i(opts
))
3469 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
3470 command_to_string(item
->command
), NULL
),
3472 res
= do_pick_commit(item
->command
, item
->commit
,
3473 opts
, is_final_fixup(todo_list
));
3474 if (is_rebase_i(opts
) && res
< 0) {
3476 advise(_(rescheduled_advice
),
3477 get_item_line_length(todo_list
,
3478 todo_list
->current
),
3479 get_item_line(todo_list
,
3480 todo_list
->current
));
3481 todo_list
->current
--;
3482 if (save_todo(todo_list
, opts
))
3485 if (item
->command
== TODO_EDIT
) {
3486 struct commit
*commit
= item
->commit
;
3489 _("Stopped at %s... %.*s\n"),
3490 short_commit_name(commit
),
3491 item
->arg_len
, item
->arg
);
3492 return error_with_patch(commit
,
3493 item
->arg
, item
->arg_len
, opts
, res
,
3496 if (is_rebase_i(opts
) && !res
)
3497 record_in_rewritten(&item
->commit
->object
.oid
,
3498 peek_command(todo_list
, 1));
3499 if (res
&& is_fixup(item
->command
)) {
3502 return error_failed_squash(item
->commit
, opts
,
3503 item
->arg_len
, item
->arg
);
3504 } else if (res
&& is_rebase_i(opts
) && item
->commit
) {
3506 struct object_id oid
;
3509 * If we are rewording and have either
3510 * fast-forwarded already, or are about to
3511 * create a new root commit, we want to amend,
3512 * otherwise we do not.
3514 if (item
->command
== TODO_REWORD
&&
3515 !get_oid("HEAD", &oid
) &&
3516 (oideq(&item
->commit
->object
.oid
, &oid
) ||
3517 (opts
->have_squash_onto
&&
3518 oideq(&opts
->squash_onto
, &oid
))))
3521 return res
| error_with_patch(item
->commit
,
3522 item
->arg
, item
->arg_len
, opts
,
3525 } else if (item
->command
== TODO_EXEC
) {
3526 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
3527 int saved
= *end_of_arg
;
3531 res
= do_exec(item
->arg
);
3532 *end_of_arg
= saved
;
3534 /* Reread the todo file if it has changed. */
3536 ; /* fall through */
3537 else if (stat(get_todo_path(opts
), &st
))
3538 res
= error_errno(_("could not stat '%s'"),
3539 get_todo_path(opts
));
3540 else if (match_stat_data(&todo_list
->stat
, &st
)) {
3541 todo_list_release(todo_list
);
3542 if (read_populate_todo(todo_list
, opts
))
3543 res
= -1; /* message was printed */
3544 /* `current` will be incremented below */
3545 todo_list
->current
= -1;
3547 } else if (item
->command
== TODO_LABEL
) {
3548 if ((res
= do_label(item
->arg
, item
->arg_len
)))
3550 } else if (item
->command
== TODO_RESET
) {
3551 if ((res
= do_reset(item
->arg
, item
->arg_len
, opts
)))
3553 } else if (item
->command
== TODO_MERGE
) {
3554 if ((res
= do_merge(item
->commit
,
3555 item
->arg
, item
->arg_len
,
3556 item
->flags
, opts
)) < 0)
3558 else if (item
->commit
)
3559 record_in_rewritten(&item
->commit
->object
.oid
,
3560 peek_command(todo_list
, 1));
3562 /* failed with merge conflicts */
3563 return error_with_patch(item
->commit
,
3565 item
->arg_len
, opts
,
3567 } else if (!is_noop(item
->command
))
3568 return error(_("unknown command %d"), item
->command
);
3571 advise(_(rescheduled_advice
),
3572 get_item_line_length(todo_list
,
3573 todo_list
->current
),
3574 get_item_line(todo_list
, todo_list
->current
));
3575 todo_list
->current
--;
3576 if (save_todo(todo_list
, opts
))
3579 return error_with_patch(item
->commit
,
3581 item
->arg_len
, opts
,
3585 todo_list
->current
++;
3590 if (is_rebase_i(opts
)) {
3591 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
3594 /* Stopped in the middle, as planned? */
3595 if (todo_list
->current
< todo_list
->nr
)
3598 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
3599 starts_with(head_ref
.buf
, "refs/")) {
3601 struct object_id head
, orig
;
3604 if (get_oid("HEAD", &head
)) {
3605 res
= error(_("cannot read HEAD"));
3607 strbuf_release(&head_ref
);
3608 strbuf_release(&buf
);
3611 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
3612 get_oid_hex(buf
.buf
, &orig
)) {
3613 res
= error(_("could not read orig-head"));
3614 goto cleanup_head_ref
;
3617 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
3618 res
= error(_("could not read 'onto'"));
3619 goto cleanup_head_ref
;
3621 msg
= reflog_message(opts
, "finish", "%s onto %s",
3622 head_ref
.buf
, buf
.buf
);
3623 if (update_ref(msg
, head_ref
.buf
, &head
, &orig
,
3624 REF_NO_DEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
3625 res
= error(_("could not update %s"),
3627 goto cleanup_head_ref
;
3629 msg
= reflog_message(opts
, "finish", "returning to %s",
3631 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
3632 res
= error(_("could not update HEAD to %s"),
3634 goto cleanup_head_ref
;
3639 if (opts
->verbose
) {
3640 struct rev_info log_tree_opt
;
3641 struct object_id orig
, head
;
3643 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
3644 repo_init_revisions(the_repository
, &log_tree_opt
, NULL
);
3645 log_tree_opt
.diff
= 1;
3646 log_tree_opt
.diffopt
.output_format
=
3647 DIFF_FORMAT_DIFFSTAT
;
3648 log_tree_opt
.disable_stdin
= 1;
3650 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
3651 !get_oid(buf
.buf
, &orig
) &&
3652 !get_oid("HEAD", &head
)) {
3653 diff_tree_oid(&orig
, &head
, "",
3654 &log_tree_opt
.diffopt
);
3655 log_tree_diff_flush(&log_tree_opt
);
3658 flush_rewritten_pending();
3659 if (!stat(rebase_path_rewritten_list(), &st
) &&
3661 struct child_process child
= CHILD_PROCESS_INIT
;
3662 const char *post_rewrite_hook
=
3663 find_hook("post-rewrite");
3665 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
3667 argv_array_push(&child
.args
, "notes");
3668 argv_array_push(&child
.args
, "copy");
3669 argv_array_push(&child
.args
, "--for-rewrite=rebase");
3670 /* we don't care if this copying failed */
3671 run_command(&child
);
3673 if (post_rewrite_hook
) {
3674 struct child_process hook
= CHILD_PROCESS_INIT
;
3676 hook
.in
= open(rebase_path_rewritten_list(),
3678 hook
.stdout_to_stderr
= 1;
3679 argv_array_push(&hook
.args
, post_rewrite_hook
);
3680 argv_array_push(&hook
.args
, "rebase");
3681 /* we don't care if this hook failed */
3685 apply_autostash(opts
);
3687 fprintf(stderr
, "Successfully rebased and updated %s.\n",
3690 strbuf_release(&buf
);
3691 strbuf_release(&head_ref
);
3695 * Sequence of picks finished successfully; cleanup by
3696 * removing the .git/sequencer directory
3698 return sequencer_remove_state(opts
);
3701 static int continue_single_pick(void)
3703 const char *argv
[] = { "commit", NULL
};
3705 if (!file_exists(git_path_cherry_pick_head(the_repository
)) &&
3706 !file_exists(git_path_revert_head(the_repository
)))
3707 return error(_("no cherry-pick or revert in progress"));
3708 return run_command_v_opt(argv
, RUN_GIT_CMD
);
3711 static int commit_staged_changes(struct replay_opts
*opts
,
3712 struct todo_list
*todo_list
)
3714 unsigned int flags
= ALLOW_EMPTY
| EDIT_MSG
;
3715 unsigned int final_fixup
= 0, is_clean
;
3717 if (has_unstaged_changes(1))
3718 return error(_("cannot rebase: You have unstaged changes."));
3720 is_clean
= !has_uncommitted_changes(0);
3722 if (file_exists(rebase_path_amend())) {
3723 struct strbuf rev
= STRBUF_INIT
;
3724 struct object_id head
, to_amend
;
3726 if (get_oid("HEAD", &head
))
3727 return error(_("cannot amend non-existing commit"));
3728 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
3729 return error(_("invalid file: '%s'"), rebase_path_amend());
3730 if (get_oid_hex(rev
.buf
, &to_amend
))
3731 return error(_("invalid contents: '%s'"),
3732 rebase_path_amend());
3733 if (!is_clean
&& !oideq(&head
, &to_amend
))
3734 return error(_("\nYou have uncommitted changes in your "
3735 "working tree. Please, commit them\n"
3736 "first and then run 'git rebase "
3737 "--continue' again."));
3739 * When skipping a failed fixup/squash, we need to edit the
3740 * commit message, the current fixup list and count, and if it
3741 * was the last fixup/squash in the chain, we need to clean up
3742 * the commit message and if there was a squash, let the user
3745 if (!is_clean
|| !opts
->current_fixup_count
)
3746 ; /* this is not the final fixup */
3747 else if (!oideq(&head
, &to_amend
) ||
3748 !file_exists(rebase_path_stopped_sha())) {
3749 /* was a final fixup or squash done manually? */
3750 if (!is_fixup(peek_command(todo_list
, 0))) {
3751 unlink(rebase_path_fixup_msg());
3752 unlink(rebase_path_squash_msg());
3753 unlink(rebase_path_current_fixups());
3754 strbuf_reset(&opts
->current_fixups
);
3755 opts
->current_fixup_count
= 0;
3758 /* we are in a fixup/squash chain */
3759 const char *p
= opts
->current_fixups
.buf
;
3760 int len
= opts
->current_fixups
.len
;
3762 opts
->current_fixup_count
--;
3764 BUG("Incorrect current_fixups:\n%s", p
);
3765 while (len
&& p
[len
- 1] != '\n')
3767 strbuf_setlen(&opts
->current_fixups
, len
);
3768 if (write_message(p
, len
, rebase_path_current_fixups(),
3770 return error(_("could not write file: '%s'"),
3771 rebase_path_current_fixups());
3774 * If a fixup/squash in a fixup/squash chain failed, the
3775 * commit message is already correct, no need to commit
3778 * Only if it is the final command in the fixup/squash
3779 * chain, and only if the chain is longer than a single
3780 * fixup/squash command (which was just skipped), do we
3781 * actually need to re-commit with a cleaned up commit
3784 if (opts
->current_fixup_count
> 0 &&
3785 !is_fixup(peek_command(todo_list
, 0))) {
3788 * If there was not a single "squash" in the
3789 * chain, we only need to clean up the commit
3790 * message, no need to bother the user with
3791 * opening the commit message in the editor.
3793 if (!starts_with(p
, "squash ") &&
3794 !strstr(p
, "\nsquash "))
3795 flags
= (flags
& ~EDIT_MSG
) | CLEANUP_MSG
;
3796 } else if (is_fixup(peek_command(todo_list
, 0))) {
3798 * We need to update the squash message to skip
3799 * the latest commit message.
3801 struct commit
*commit
;
3802 const char *path
= rebase_path_squash_msg();
3804 if (parse_head(&commit
) ||
3805 !(p
= get_commit_buffer(commit
, NULL
)) ||
3806 write_message(p
, strlen(p
), path
, 0)) {
3807 unuse_commit_buffer(commit
, p
);
3808 return error(_("could not write file: "
3811 unuse_commit_buffer(commit
, p
);
3815 strbuf_release(&rev
);
3820 const char *cherry_pick_head
= git_path_cherry_pick_head(the_repository
);
3822 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
3823 return error(_("could not remove CHERRY_PICK_HEAD"));
3828 if (run_git_commit(final_fixup
? NULL
: rebase_path_message(),
3830 return error(_("could not commit staged changes."));
3831 unlink(rebase_path_amend());
3833 unlink(rebase_path_fixup_msg());
3834 unlink(rebase_path_squash_msg());
3836 if (opts
->current_fixup_count
> 0) {
3838 * Whether final fixup or not, we just cleaned up the commit
3841 unlink(rebase_path_current_fixups());
3842 strbuf_reset(&opts
->current_fixups
);
3843 opts
->current_fixup_count
= 0;
3848 int sequencer_continue(struct replay_opts
*opts
)
3850 struct todo_list todo_list
= TODO_LIST_INIT
;
3853 if (read_and_refresh_cache(opts
))
3856 if (read_populate_opts(opts
))
3858 if (is_rebase_i(opts
)) {
3859 if ((res
= read_populate_todo(&todo_list
, opts
)))
3860 goto release_todo_list
;
3861 if (commit_staged_changes(opts
, &todo_list
))
3863 } else if (!file_exists(get_todo_path(opts
)))
3864 return continue_single_pick();
3865 else if ((res
= read_populate_todo(&todo_list
, opts
)))
3866 goto release_todo_list
;
3868 if (!is_rebase_i(opts
)) {
3869 /* Verify that the conflict has been resolved */
3870 if (file_exists(git_path_cherry_pick_head(the_repository
)) ||
3871 file_exists(git_path_revert_head(the_repository
))) {
3872 res
= continue_single_pick();
3874 goto release_todo_list
;
3876 if (index_differs_from("HEAD", NULL
, 0)) {
3877 res
= error_dirty_index(opts
);
3878 goto release_todo_list
;
3880 todo_list
.current
++;
3881 } else if (file_exists(rebase_path_stopped_sha())) {
3882 struct strbuf buf
= STRBUF_INIT
;
3883 struct object_id oid
;
3885 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
3886 !get_oid_committish(buf
.buf
, &oid
))
3887 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
3888 strbuf_release(&buf
);
3891 res
= pick_commits(&todo_list
, opts
);
3893 todo_list_release(&todo_list
);
3897 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
3899 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
3900 return do_pick_commit(opts
->action
== REPLAY_PICK
?
3901 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
3904 int sequencer_pick_revisions(struct replay_opts
*opts
)
3906 struct todo_list todo_list
= TODO_LIST_INIT
;
3907 struct object_id oid
;
3911 if (read_and_refresh_cache(opts
))
3914 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
3915 struct object_id oid
;
3916 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
3918 /* This happens when using --stdin. */
3922 if (!get_oid(name
, &oid
)) {
3923 if (!lookup_commit_reference_gently(the_repository
, &oid
, 1)) {
3924 enum object_type type
= oid_object_info(the_repository
,
3927 return error(_("%s: can't cherry-pick a %s"),
3928 name
, type_name(type
));
3931 return error(_("%s: bad revision"), name
);
3935 * If we were called as "git cherry-pick <commit>", just
3936 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
3937 * REVERT_HEAD, and don't touch the sequencer state.
3938 * This means it is possible to cherry-pick in the middle
3939 * of a cherry-pick sequence.
3941 if (opts
->revs
->cmdline
.nr
== 1 &&
3942 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
3943 opts
->revs
->no_walk
&&
3944 !opts
->revs
->cmdline
.rev
->flags
) {
3945 struct commit
*cmit
;
3946 if (prepare_revision_walk(opts
->revs
))
3947 return error(_("revision walk setup failed"));
3948 cmit
= get_revision(opts
->revs
);
3950 return error(_("empty commit set passed"));
3951 if (get_revision(opts
->revs
))
3952 BUG("unexpected extra commit from walk");
3953 return single_pick(cmit
, opts
);
3957 * Start a new cherry-pick/ revert sequence; but
3958 * first, make sure that an existing one isn't in
3962 if (walk_revs_populate_todo(&todo_list
, opts
) ||
3963 create_seq_dir() < 0)
3965 if (get_oid("HEAD", &oid
) && (opts
->action
== REPLAY_REVERT
))
3966 return error(_("can't revert as initial commit"));
3967 if (save_head(oid_to_hex(&oid
)))
3969 if (save_opts(opts
))
3971 update_abort_safety_file();
3972 res
= pick_commits(&todo_list
, opts
);
3973 todo_list_release(&todo_list
);
3977 void append_signoff(struct strbuf
*msgbuf
, size_t ignore_footer
, unsigned flag
)
3979 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
3980 struct strbuf sob
= STRBUF_INIT
;
3983 strbuf_addstr(&sob
, sign_off_header
);
3984 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
3985 getenv("GIT_COMMITTER_EMAIL")));
3986 strbuf_addch(&sob
, '\n');
3989 strbuf_complete_line(msgbuf
);
3992 * If the whole message buffer is equal to the sob, pretend that we
3993 * found a conforming footer with a matching sob
3995 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
3996 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
3999 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
4002 const char *append_newlines
= NULL
;
4003 size_t len
= msgbuf
->len
- ignore_footer
;
4007 * The buffer is completely empty. Leave foom for
4008 * the title and body to be filled in by the user.
4010 append_newlines
= "\n\n";
4011 } else if (len
== 1) {
4013 * Buffer contains a single newline. Add another
4014 * so that we leave room for the title and body.
4016 append_newlines
= "\n";
4017 } else if (msgbuf
->buf
[len
- 2] != '\n') {
4019 * Buffer ends with a single newline. Add another
4020 * so that there is an empty line between the message
4023 append_newlines
= "\n";
4024 } /* else, the buffer already ends with two newlines. */
4026 if (append_newlines
)
4027 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
4028 append_newlines
, strlen(append_newlines
));
4031 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
4032 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
4035 strbuf_release(&sob
);
4038 struct labels_entry
{
4039 struct hashmap_entry entry
;
4040 char label
[FLEX_ARRAY
];
4043 static int labels_cmp(const void *fndata
, const struct labels_entry
*a
,
4044 const struct labels_entry
*b
, const void *key
)
4046 return key
? strcmp(a
->label
, key
) : strcmp(a
->label
, b
->label
);
4049 struct string_entry
{
4050 struct oidmap_entry entry
;
4051 char string
[FLEX_ARRAY
];
4054 struct label_state
{
4055 struct oidmap commit2label
;
4056 struct hashmap labels
;
4060 static const char *label_oid(struct object_id
*oid
, const char *label
,
4061 struct label_state
*state
)
4063 struct labels_entry
*labels_entry
;
4064 struct string_entry
*string_entry
;
4065 struct object_id dummy
;
4069 string_entry
= oidmap_get(&state
->commit2label
, oid
);
4071 return string_entry
->string
;
4074 * For "uninteresting" commits, i.e. commits that are not to be
4075 * rebased, and which can therefore not be labeled, we use a unique
4076 * abbreviation of the commit name. This is slightly more complicated
4077 * than calling find_unique_abbrev() because we also need to make
4078 * sure that the abbreviation does not conflict with any other
4081 * We disallow "interesting" commits to be labeled by a string that
4082 * is a valid full-length hash, to ensure that we always can find an
4083 * abbreviation for any uninteresting commit's names that does not
4084 * clash with any other label.
4089 strbuf_reset(&state
->buf
);
4090 strbuf_grow(&state
->buf
, GIT_SHA1_HEXSZ
);
4091 label
= p
= state
->buf
.buf
;
4093 find_unique_abbrev_r(p
, oid
, default_abbrev
);
4096 * We may need to extend the abbreviated hash so that there is
4097 * no conflicting label.
4099 if (hashmap_get_from_hash(&state
->labels
, strihash(p
), p
)) {
4100 size_t i
= strlen(p
) + 1;
4102 oid_to_hex_r(p
, oid
);
4103 for (; i
< GIT_SHA1_HEXSZ
; i
++) {
4106 if (!hashmap_get_from_hash(&state
->labels
,
4112 } else if (((len
= strlen(label
)) == the_hash_algo
->hexsz
&&
4113 !get_oid_hex(label
, &dummy
)) ||
4114 (len
== 1 && *label
== '#') ||
4115 hashmap_get_from_hash(&state
->labels
,
4116 strihash(label
), label
)) {
4118 * If the label already exists, or if the label is a valid full
4119 * OID, or the label is a '#' (which we use as a separator
4120 * between merge heads and oneline), we append a dash and a
4121 * number to make it unique.
4123 struct strbuf
*buf
= &state
->buf
;
4126 strbuf_add(buf
, label
, len
);
4128 for (i
= 2; ; i
++) {
4129 strbuf_setlen(buf
, len
);
4130 strbuf_addf(buf
, "-%d", i
);
4131 if (!hashmap_get_from_hash(&state
->labels
,
4140 FLEX_ALLOC_STR(labels_entry
, label
, label
);
4141 hashmap_entry_init(labels_entry
, strihash(label
));
4142 hashmap_add(&state
->labels
, labels_entry
);
4144 FLEX_ALLOC_STR(string_entry
, string
, label
);
4145 oidcpy(&string_entry
->entry
.oid
, oid
);
4146 oidmap_put(&state
->commit2label
, string_entry
);
4148 return string_entry
->string
;
4151 static int make_script_with_merges(struct pretty_print_context
*pp
,
4152 struct rev_info
*revs
, FILE *out
,
4155 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
4156 int rebase_cousins
= flags
& TODO_LIST_REBASE_COUSINS
;
4157 struct strbuf buf
= STRBUF_INIT
, oneline
= STRBUF_INIT
;
4158 struct strbuf label
= STRBUF_INIT
;
4159 struct commit_list
*commits
= NULL
, **tail
= &commits
, *iter
;
4160 struct commit_list
*tips
= NULL
, **tips_tail
= &tips
;
4161 struct commit
*commit
;
4162 struct oidmap commit2todo
= OIDMAP_INIT
;
4163 struct string_entry
*entry
;
4164 struct oidset interesting
= OIDSET_INIT
, child_seen
= OIDSET_INIT
,
4165 shown
= OIDSET_INIT
;
4166 struct label_state state
= { OIDMAP_INIT
, { NULL
}, STRBUF_INIT
};
4168 int abbr
= flags
& TODO_LIST_ABBREVIATE_CMDS
;
4169 const char *cmd_pick
= abbr
? "p" : "pick",
4170 *cmd_label
= abbr
? "l" : "label",
4171 *cmd_reset
= abbr
? "t" : "reset",
4172 *cmd_merge
= abbr
? "m" : "merge";
4174 oidmap_init(&commit2todo
, 0);
4175 oidmap_init(&state
.commit2label
, 0);
4176 hashmap_init(&state
.labels
, (hashmap_cmp_fn
) labels_cmp
, NULL
, 0);
4177 strbuf_init(&state
.buf
, 32);
4179 if (revs
->cmdline
.nr
&& (revs
->cmdline
.rev
[0].flags
& BOTTOM
)) {
4180 struct object_id
*oid
= &revs
->cmdline
.rev
[0].item
->oid
;
4181 FLEX_ALLOC_STR(entry
, string
, "onto");
4182 oidcpy(&entry
->entry
.oid
, oid
);
4183 oidmap_put(&state
.commit2label
, entry
);
4188 * - get onelines for all commits
4189 * - gather all branch tips (i.e. 2nd or later parents of merges)
4190 * - label all branch tips
4192 while ((commit
= get_revision(revs
))) {
4193 struct commit_list
*to_merge
;
4194 const char *p1
, *p2
;
4195 struct object_id
*oid
;
4198 tail
= &commit_list_insert(commit
, tail
)->next
;
4199 oidset_insert(&interesting
, &commit
->object
.oid
);
4201 is_empty
= is_original_commit_empty(commit
);
4202 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
4205 strbuf_reset(&oneline
);
4206 pretty_print_commit(pp
, commit
, &oneline
);
4208 to_merge
= commit
->parents
? commit
->parents
->next
: NULL
;
4210 /* non-merge commit: easy case */
4212 if (!keep_empty
&& is_empty
)
4213 strbuf_addf(&buf
, "%c ", comment_line_char
);
4214 strbuf_addf(&buf
, "%s %s %s", cmd_pick
,
4215 oid_to_hex(&commit
->object
.oid
),
4218 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
4219 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
4220 oidmap_put(&commit2todo
, entry
);
4225 /* Create a label */
4226 strbuf_reset(&label
);
4227 if (skip_prefix(oneline
.buf
, "Merge ", &p1
) &&
4228 (p1
= strchr(p1
, '\'')) &&
4229 (p2
= strchr(++p1
, '\'')))
4230 strbuf_add(&label
, p1
, p2
- p1
);
4231 else if (skip_prefix(oneline
.buf
, "Merge pull request ",
4233 (p1
= strstr(p1
, " from ")))
4234 strbuf_addstr(&label
, p1
+ strlen(" from "));
4236 strbuf_addbuf(&label
, &oneline
);
4238 for (p1
= label
.buf
; *p1
; p1
++)
4243 strbuf_addf(&buf
, "%s -C %s",
4244 cmd_merge
, oid_to_hex(&commit
->object
.oid
));
4246 /* label the tips of merged branches */
4247 for (; to_merge
; to_merge
= to_merge
->next
) {
4248 oid
= &to_merge
->item
->object
.oid
;
4249 strbuf_addch(&buf
, ' ');
4251 if (!oidset_contains(&interesting
, oid
)) {
4252 strbuf_addstr(&buf
, label_oid(oid
, NULL
,
4257 tips_tail
= &commit_list_insert(to_merge
->item
,
4260 strbuf_addstr(&buf
, label_oid(oid
, label
.buf
, &state
));
4262 strbuf_addf(&buf
, " # %s", oneline
.buf
);
4264 FLEX_ALLOC_STR(entry
, string
, buf
.buf
);
4265 oidcpy(&entry
->entry
.oid
, &commit
->object
.oid
);
4266 oidmap_put(&commit2todo
, entry
);
4271 * - label branch points
4272 * - add HEAD to the branch tips
4274 for (iter
= commits
; iter
; iter
= iter
->next
) {
4275 struct commit_list
*parent
= iter
->item
->parents
;
4276 for (; parent
; parent
= parent
->next
) {
4277 struct object_id
*oid
= &parent
->item
->object
.oid
;
4278 if (!oidset_contains(&interesting
, oid
))
4280 if (oidset_insert(&child_seen
, oid
))
4281 label_oid(oid
, "branch-point", &state
);
4284 /* Add HEAD as implict "tip of branch" */
4286 tips_tail
= &commit_list_insert(iter
->item
,
4291 * Third phase: output the todo list. This is a bit tricky, as we
4292 * want to avoid jumping back and forth between revisions. To
4293 * accomplish that goal, we walk backwards from the branch tips,
4294 * gathering commits not yet shown, reversing the list on the fly,
4295 * then outputting that list (labeling revisions as needed).
4297 fprintf(out
, "%s onto\n", cmd_label
);
4298 for (iter
= tips
; iter
; iter
= iter
->next
) {
4299 struct commit_list
*list
= NULL
, *iter2
;
4301 commit
= iter
->item
;
4302 if (oidset_contains(&shown
, &commit
->object
.oid
))
4304 entry
= oidmap_get(&state
.commit2label
, &commit
->object
.oid
);
4307 fprintf(out
, "\n%c Branch %s\n", comment_line_char
, entry
->string
);
4311 while (oidset_contains(&interesting
, &commit
->object
.oid
) &&
4312 !oidset_contains(&shown
, &commit
->object
.oid
)) {
4313 commit_list_insert(commit
, &list
);
4314 if (!commit
->parents
) {
4318 commit
= commit
->parents
->item
;
4322 fprintf(out
, "%s %s\n", cmd_reset
,
4323 rebase_cousins
? "onto" : "[new root]");
4325 const char *to
= NULL
;
4327 entry
= oidmap_get(&state
.commit2label
,
4328 &commit
->object
.oid
);
4331 else if (!rebase_cousins
)
4332 to
= label_oid(&commit
->object
.oid
, NULL
,
4335 if (!to
|| !strcmp(to
, "onto"))
4336 fprintf(out
, "%s onto\n", cmd_reset
);
4338 strbuf_reset(&oneline
);
4339 pretty_print_commit(pp
, commit
, &oneline
);
4340 fprintf(out
, "%s %s # %s\n",
4341 cmd_reset
, to
, oneline
.buf
);
4345 for (iter2
= list
; iter2
; iter2
= iter2
->next
) {
4346 struct object_id
*oid
= &iter2
->item
->object
.oid
;
4347 entry
= oidmap_get(&commit2todo
, oid
);
4348 /* only show if not already upstream */
4350 fprintf(out
, "%s\n", entry
->string
);
4351 entry
= oidmap_get(&state
.commit2label
, oid
);
4353 fprintf(out
, "%s %s\n",
4354 cmd_label
, entry
->string
);
4355 oidset_insert(&shown
, oid
);
4358 free_commit_list(list
);
4361 free_commit_list(commits
);
4362 free_commit_list(tips
);
4364 strbuf_release(&label
);
4365 strbuf_release(&oneline
);
4366 strbuf_release(&buf
);
4368 oidmap_free(&commit2todo
, 1);
4369 oidmap_free(&state
.commit2label
, 1);
4370 hashmap_free(&state
.labels
, 1);
4371 strbuf_release(&state
.buf
);
4376 int sequencer_make_script(FILE *out
, int argc
, const char **argv
,
4379 char *format
= NULL
;
4380 struct pretty_print_context pp
= {0};
4381 struct strbuf buf
= STRBUF_INIT
;
4382 struct rev_info revs
;
4383 struct commit
*commit
;
4384 int keep_empty
= flags
& TODO_LIST_KEEP_EMPTY
;
4385 const char *insn
= flags
& TODO_LIST_ABBREVIATE_CMDS
? "p" : "pick";
4386 int rebase_merges
= flags
& TODO_LIST_REBASE_MERGES
;
4388 repo_init_revisions(the_repository
, &revs
, NULL
);
4389 revs
.verbose_header
= 1;
4391 revs
.max_parents
= 1;
4392 revs
.cherry_mark
= 1;
4395 revs
.right_only
= 1;
4396 revs
.sort_order
= REV_SORT_IN_GRAPH_ORDER
;
4397 revs
.topo_order
= 1;
4399 revs
.pretty_given
= 1;
4400 git_config_get_string("rebase.instructionFormat", &format
);
4401 if (!format
|| !*format
) {
4403 format
= xstrdup("%s");
4405 get_commit_format(format
, &revs
);
4407 pp
.fmt
= revs
.commit_format
;
4408 pp
.output_encoding
= get_log_output_encoding();
4410 if (setup_revisions(argc
, argv
, &revs
, NULL
) > 1)
4411 return error(_("make_script: unhandled options"));
4413 if (prepare_revision_walk(&revs
) < 0)
4414 return error(_("make_script: error preparing revisions"));
4417 return make_script_with_merges(&pp
, &revs
, out
, flags
);
4419 while ((commit
= get_revision(&revs
))) {
4420 int is_empty
= is_original_commit_empty(commit
);
4422 if (!is_empty
&& (commit
->object
.flags
& PATCHSAME
))
4425 if (!keep_empty
&& is_empty
)
4426 strbuf_addf(&buf
, "%c ", comment_line_char
);
4427 strbuf_addf(&buf
, "%s %s ", insn
,
4428 oid_to_hex(&commit
->object
.oid
));
4429 pretty_print_commit(&pp
, commit
, &buf
);
4430 strbuf_addch(&buf
, '\n');
4431 fputs(buf
.buf
, out
);
4433 strbuf_release(&buf
);
4438 * Add commands after pick and (series of) squash/fixup commands
4441 int sequencer_add_exec_commands(const char *commands
)
4443 const char *todo_file
= rebase_path_todo();
4444 struct todo_list todo_list
= TODO_LIST_INIT
;
4445 struct strbuf
*buf
= &todo_list
.buf
;
4446 size_t offset
= 0, commands_len
= strlen(commands
);
4449 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
4450 return error(_("could not read '%s'."), todo_file
);
4452 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
4453 todo_list_release(&todo_list
);
4454 return error(_("unusable todo list: '%s'"), todo_file
);
4458 * Insert <commands> after every pick. Here, fixup/squash chains
4459 * are considered part of the pick, so we insert the commands *after*
4460 * those chains if there are any.
4463 for (i
= 0; i
< todo_list
.nr
; i
++) {
4464 enum todo_command command
= todo_list
.items
[i
].command
;
4467 /* skip fixup/squash chains */
4468 if (command
== TODO_COMMENT
)
4470 else if (is_fixup(command
)) {
4475 todo_list
.items
[insert
].offset_in_buf
+
4476 offset
, commands
, commands_len
);
4477 offset
+= commands_len
;
4481 if (command
== TODO_PICK
|| command
== TODO_MERGE
)
4485 /* insert or append final <commands> */
4486 if (insert
>= 0 && insert
< todo_list
.nr
)
4487 strbuf_insert(buf
, todo_list
.items
[insert
].offset_in_buf
+
4488 offset
, commands
, commands_len
);
4489 else if (insert
>= 0 || !offset
)
4490 strbuf_add(buf
, commands
, commands_len
);
4492 i
= write_message(buf
->buf
, buf
->len
, todo_file
, 0);
4493 todo_list_release(&todo_list
);
4497 int transform_todos(unsigned flags
)
4499 const char *todo_file
= rebase_path_todo();
4500 struct todo_list todo_list
= TODO_LIST_INIT
;
4501 struct strbuf buf
= STRBUF_INIT
;
4502 struct todo_item
*item
;
4505 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
4506 return error(_("could not read '%s'."), todo_file
);
4508 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
)) {
4509 todo_list_release(&todo_list
);
4510 return error(_("unusable todo list: '%s'"), todo_file
);
4513 for (item
= todo_list
.items
, i
= 0; i
< todo_list
.nr
; i
++, item
++) {
4514 /* if the item is not a command write it and continue */
4515 if (item
->command
>= TODO_COMMENT
) {
4516 strbuf_addf(&buf
, "%.*s\n", item
->arg_len
, item
->arg
);
4520 /* add command to the buffer */
4521 if (flags
& TODO_LIST_ABBREVIATE_CMDS
)
4522 strbuf_addch(&buf
, command_to_char(item
->command
));
4524 strbuf_addstr(&buf
, command_to_string(item
->command
));
4528 const char *oid
= flags
& TODO_LIST_SHORTEN_IDS
?
4529 short_commit_name(item
->commit
) :
4530 oid_to_hex(&item
->commit
->object
.oid
);
4532 if (item
->command
== TODO_MERGE
) {
4533 if (item
->flags
& TODO_EDIT_MERGE_MSG
)
4534 strbuf_addstr(&buf
, " -c");
4536 strbuf_addstr(&buf
, " -C");
4539 strbuf_addf(&buf
, " %s", oid
);
4542 /* add all the rest */
4544 strbuf_addch(&buf
, '\n');
4546 strbuf_addf(&buf
, " %.*s\n", item
->arg_len
, item
->arg
);
4549 i
= write_message(buf
.buf
, buf
.len
, todo_file
, 0);
4550 todo_list_release(&todo_list
);
4554 enum missing_commit_check_level
get_missing_commit_check_level(void)
4558 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
4559 !strcasecmp("ignore", value
))
4560 return MISSING_COMMIT_CHECK_IGNORE
;
4561 if (!strcasecmp("warn", value
))
4562 return MISSING_COMMIT_CHECK_WARN
;
4563 if (!strcasecmp("error", value
))
4564 return MISSING_COMMIT_CHECK_ERROR
;
4565 warning(_("unrecognized setting %s for option "
4566 "rebase.missingCommitsCheck. Ignoring."), value
);
4567 return MISSING_COMMIT_CHECK_IGNORE
;
4570 define_commit_slab(commit_seen
, unsigned char);
4572 * Check if the user dropped some commits by mistake
4573 * Behaviour determined by rebase.missingCommitsCheck.
4574 * Check if there is an unrecognized command or a
4575 * bad SHA-1 in a command.
4577 int check_todo_list(void)
4579 enum missing_commit_check_level check_level
= get_missing_commit_check_level();
4580 struct strbuf todo_file
= STRBUF_INIT
;
4581 struct todo_list todo_list
= TODO_LIST_INIT
;
4582 struct strbuf missing
= STRBUF_INIT
;
4583 int advise_to_edit_todo
= 0, res
= 0, i
;
4584 struct commit_seen commit_seen
;
4586 init_commit_seen(&commit_seen
);
4588 strbuf_addstr(&todo_file
, rebase_path_todo());
4589 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
4593 advise_to_edit_todo
= res
=
4594 parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
4596 if (res
|| check_level
== MISSING_COMMIT_CHECK_IGNORE
)
4599 /* Mark the commits in git-rebase-todo as seen */
4600 for (i
= 0; i
< todo_list
.nr
; i
++) {
4601 struct commit
*commit
= todo_list
.items
[i
].commit
;
4603 *commit_seen_at(&commit_seen
, commit
) = 1;
4606 todo_list_release(&todo_list
);
4607 strbuf_addstr(&todo_file
, ".backup");
4608 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
.buf
) < 0) {
4612 strbuf_release(&todo_file
);
4613 res
= !!parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
);
4615 /* Find commits in git-rebase-todo.backup yet unseen */
4616 for (i
= todo_list
.nr
- 1; i
>= 0; i
--) {
4617 struct todo_item
*item
= todo_list
.items
+ i
;
4618 struct commit
*commit
= item
->commit
;
4619 if (commit
&& !*commit_seen_at(&commit_seen
, commit
)) {
4620 strbuf_addf(&missing
, " - %s %.*s\n",
4621 short_commit_name(commit
),
4622 item
->arg_len
, item
->arg
);
4623 *commit_seen_at(&commit_seen
, commit
) = 1;
4627 /* Warn about missing commits */
4631 if (check_level
== MISSING_COMMIT_CHECK_ERROR
)
4632 advise_to_edit_todo
= res
= 1;
4635 _("Warning: some commits may have been dropped accidentally.\n"
4636 "Dropped commits (newer to older):\n"));
4638 /* Make the list user-friendly and display */
4639 fputs(missing
.buf
, stderr
);
4640 strbuf_release(&missing
);
4642 fprintf(stderr
, _("To avoid this message, use \"drop\" to "
4643 "explicitly remove a commit.\n\n"
4644 "Use 'git config rebase.missingCommitsCheck' to change "
4645 "the level of warnings.\n"
4646 "The possible behaviours are: ignore, warn, error.\n\n"));
4649 clear_commit_seen(&commit_seen
);
4650 strbuf_release(&todo_file
);
4651 todo_list_release(&todo_list
);
4653 if (advise_to_edit_todo
)
4655 _("You can fix this with 'git rebase --edit-todo' "
4656 "and then run 'git rebase --continue'.\n"
4657 "Or you can abort the rebase with 'git rebase"
4663 static int rewrite_file(const char *path
, const char *buf
, size_t len
)
4666 int fd
= open(path
, O_WRONLY
| O_TRUNC
);
4668 return error_errno(_("could not open '%s' for writing"), path
);
4669 if (write_in_full(fd
, buf
, len
) < 0)
4670 rc
= error_errno(_("could not write to '%s'"), path
);
4671 if (close(fd
) && !rc
)
4672 rc
= error_errno(_("could not close '%s'"), path
);
4676 /* skip picking commits whose parents are unchanged */
4677 static int skip_unnecessary_picks(struct object_id
*output_oid
)
4679 const char *todo_file
= rebase_path_todo();
4680 struct strbuf buf
= STRBUF_INIT
;
4681 struct todo_list todo_list
= TODO_LIST_INIT
;
4682 struct object_id
*parent_oid
;
4685 if (!read_oneliner(&buf
, rebase_path_onto(), 0))
4686 return error(_("could not read 'onto'"));
4687 if (get_oid(buf
.buf
, output_oid
)) {
4688 strbuf_release(&buf
);
4689 return error(_("need a HEAD to fixup"));
4691 strbuf_release(&buf
);
4693 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
4695 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
4696 todo_list_release(&todo_list
);
4700 for (i
= 0; i
< todo_list
.nr
; i
++) {
4701 struct todo_item
*item
= todo_list
.items
+ i
;
4703 if (item
->command
>= TODO_NOOP
)
4705 if (item
->command
!= TODO_PICK
)
4707 if (parse_commit(item
->commit
)) {
4708 todo_list_release(&todo_list
);
4709 return error(_("could not parse commit '%s'"),
4710 oid_to_hex(&item
->commit
->object
.oid
));
4712 if (!item
->commit
->parents
)
4713 break; /* root commit */
4714 if (item
->commit
->parents
->next
)
4715 break; /* merge commit */
4716 parent_oid
= &item
->commit
->parents
->item
->object
.oid
;
4717 if (!oideq(parent_oid
, output_oid
))
4719 oidcpy(output_oid
, &item
->commit
->object
.oid
);
4722 int offset
= get_item_line_offset(&todo_list
, i
);
4723 const char *done_path
= rebase_path_done();
4725 fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
4727 error_errno(_("could not open '%s' for writing"),
4729 todo_list_release(&todo_list
);
4732 if (write_in_full(fd
, todo_list
.buf
.buf
, offset
) < 0) {
4733 error_errno(_("could not write to '%s'"), done_path
);
4734 todo_list_release(&todo_list
);
4740 if (rewrite_file(rebase_path_todo(), todo_list
.buf
.buf
+ offset
,
4741 todo_list
.buf
.len
- offset
) < 0) {
4742 todo_list_release(&todo_list
);
4746 todo_list
.current
= i
;
4747 if (is_fixup(peek_command(&todo_list
, 0)))
4748 record_in_rewritten(output_oid
, peek_command(&todo_list
, 0));
4751 todo_list_release(&todo_list
);
4756 int complete_action(struct replay_opts
*opts
, unsigned flags
,
4757 const char *shortrevisions
, const char *onto_name
,
4758 const char *onto
, const char *orig_head
, const char *cmd
,
4759 unsigned autosquash
)
4761 const char *shortonto
, *todo_file
= rebase_path_todo();
4762 struct todo_list todo_list
= TODO_LIST_INIT
;
4763 struct strbuf
*buf
= &(todo_list
.buf
);
4764 struct object_id oid
;
4767 get_oid(onto
, &oid
);
4768 shortonto
= find_unique_abbrev(&oid
, DEFAULT_ABBREV
);
4770 if (!lstat(todo_file
, &st
) && st
.st_size
== 0 &&
4771 write_message("noop\n", 5, todo_file
, 0))
4774 if (autosquash
&& rearrange_squash())
4778 sequencer_add_exec_commands(cmd
);
4780 if (strbuf_read_file(buf
, todo_file
, 0) < 0)
4781 return error_errno(_("could not read '%s'."), todo_file
);
4783 if (parse_insn_buffer(buf
->buf
, &todo_list
)) {
4784 todo_list_release(&todo_list
);
4785 return error(_("unusable todo list: '%s'"), todo_file
);
4788 if (count_commands(&todo_list
) == 0) {
4789 apply_autostash(opts
);
4790 sequencer_remove_state(opts
);
4791 todo_list_release(&todo_list
);
4793 return error(_("nothing to do"));
4796 strbuf_addch(buf
, '\n');
4797 strbuf_commented_addf(buf
, Q_("Rebase %s onto %s (%d command)",
4798 "Rebase %s onto %s (%d commands)",
4799 count_commands(&todo_list
)),
4800 shortrevisions
, shortonto
, count_commands(&todo_list
));
4801 append_todo_help(0, flags
& TODO_LIST_KEEP_EMPTY
, buf
);
4803 if (write_message(buf
->buf
, buf
->len
, todo_file
, 0)) {
4804 todo_list_release(&todo_list
);
4808 if (copy_file(rebase_path_todo_backup(), todo_file
, 0666))
4809 return error(_("could not copy '%s' to '%s'."), todo_file
,
4810 rebase_path_todo_backup());
4812 if (transform_todos(flags
| TODO_LIST_SHORTEN_IDS
))
4813 return error(_("could not transform the todo list"));
4817 if (launch_sequence_editor(todo_file
, buf
, NULL
)) {
4818 apply_autostash(opts
);
4819 sequencer_remove_state(opts
);
4820 todo_list_release(&todo_list
);
4825 strbuf_stripspace(buf
, 1);
4826 if (buf
->len
== 0) {
4827 apply_autostash(opts
);
4828 sequencer_remove_state(opts
);
4829 todo_list_release(&todo_list
);
4831 return error(_("nothing to do"));
4834 todo_list_release(&todo_list
);
4836 if (check_todo_list()) {
4837 checkout_onto(opts
, onto_name
, onto
, orig_head
);
4841 if (transform_todos(flags
& ~(TODO_LIST_SHORTEN_IDS
)))
4842 return error(_("could not transform the todo list"));
4844 if (opts
->allow_ff
&& skip_unnecessary_picks(&oid
))
4845 return error(_("could not skip unnecessary pick commands"));
4847 if (checkout_onto(opts
, onto_name
, oid_to_hex(&oid
), orig_head
))
4850 if (require_clean_work_tree("rebase", "", 1, 1))
4853 return sequencer_continue(opts
);
4856 struct subject2item_entry
{
4857 struct hashmap_entry entry
;
4859 char subject
[FLEX_ARRAY
];
4862 static int subject2item_cmp(const void *fndata
,
4863 const struct subject2item_entry
*a
,
4864 const struct subject2item_entry
*b
, const void *key
)
4866 return key
? strcmp(a
->subject
, key
) : strcmp(a
->subject
, b
->subject
);
4869 define_commit_slab(commit_todo_item
, struct todo_item
*);
4872 * Rearrange the todo list that has both "pick commit-id msg" and "pick
4873 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
4874 * after the former, and change "pick" to "fixup"/"squash".
4876 * Note that if the config has specified a custom instruction format, each log
4877 * message will have to be retrieved from the commit (as the oneline in the
4878 * script cannot be trusted) in order to normalize the autosquash arrangement.
4880 int rearrange_squash(void)
4882 const char *todo_file
= rebase_path_todo();
4883 struct todo_list todo_list
= TODO_LIST_INIT
;
4884 struct hashmap subject2item
;
4885 int res
= 0, rearranged
= 0, *next
, *tail
, i
;
4887 struct commit_todo_item commit_todo
;
4889 if (strbuf_read_file_or_whine(&todo_list
.buf
, todo_file
) < 0)
4891 if (parse_insn_buffer(todo_list
.buf
.buf
, &todo_list
) < 0) {
4892 todo_list_release(&todo_list
);
4896 init_commit_todo_item(&commit_todo
);
4898 * The hashmap maps onelines to the respective todo list index.
4900 * If any items need to be rearranged, the next[i] value will indicate
4901 * which item was moved directly after the i'th.
4903 * In that case, last[i] will indicate the index of the latest item to
4904 * be moved to appear after the i'th.
4906 hashmap_init(&subject2item
, (hashmap_cmp_fn
) subject2item_cmp
,
4907 NULL
, todo_list
.nr
);
4908 ALLOC_ARRAY(next
, todo_list
.nr
);
4909 ALLOC_ARRAY(tail
, todo_list
.nr
);
4910 ALLOC_ARRAY(subjects
, todo_list
.nr
);
4911 for (i
= 0; i
< todo_list
.nr
; i
++) {
4912 struct strbuf buf
= STRBUF_INIT
;
4913 struct todo_item
*item
= todo_list
.items
+ i
;
4914 const char *commit_buffer
, *subject
, *p
;
4917 struct subject2item_entry
*entry
;
4919 next
[i
] = tail
[i
] = -1;
4920 if (!item
->commit
|| item
->command
== TODO_DROP
) {
4925 if (is_fixup(item
->command
)) {
4926 todo_list_release(&todo_list
);
4927 clear_commit_todo_item(&commit_todo
);
4928 return error(_("the script was already rearranged."));
4931 *commit_todo_item_at(&commit_todo
, item
->commit
) = item
;
4933 parse_commit(item
->commit
);
4934 commit_buffer
= get_commit_buffer(item
->commit
, NULL
);
4935 find_commit_subject(commit_buffer
, &subject
);
4936 format_subject(&buf
, subject
, " ");
4937 subject
= subjects
[i
] = strbuf_detach(&buf
, &subject_len
);
4938 unuse_commit_buffer(item
->commit
, commit_buffer
);
4939 if ((skip_prefix(subject
, "fixup! ", &p
) ||
4940 skip_prefix(subject
, "squash! ", &p
))) {
4941 struct commit
*commit2
;
4946 if (!skip_prefix(p
, "fixup! ", &p
) &&
4947 !skip_prefix(p
, "squash! ", &p
))
4951 if ((entry
= hashmap_get_from_hash(&subject2item
,
4953 /* found by title */
4955 else if (!strchr(p
, ' ') &&
4957 lookup_commit_reference_by_name(p
)) &&
4958 *commit_todo_item_at(&commit_todo
, commit2
))
4959 /* found by commit name */
4960 i2
= *commit_todo_item_at(&commit_todo
, commit2
)
4963 /* copy can be a prefix of the commit subject */
4964 for (i2
= 0; i2
< i
; i2
++)
4966 starts_with(subjects
[i2
], p
))
4974 todo_list
.items
[i
].command
=
4975 starts_with(subject
, "fixup!") ?
4976 TODO_FIXUP
: TODO_SQUASH
;
4982 } else if (!hashmap_get_from_hash(&subject2item
,
4983 strhash(subject
), subject
)) {
4984 FLEX_ALLOC_MEM(entry
, subject
, subject
, subject_len
);
4986 hashmap_entry_init(entry
, strhash(entry
->subject
));
4987 hashmap_put(&subject2item
, entry
);
4992 struct strbuf buf
= STRBUF_INIT
;
4994 for (i
= 0; i
< todo_list
.nr
; i
++) {
4995 enum todo_command command
= todo_list
.items
[i
].command
;
4999 * Initially, all commands are 'pick's. If it is a
5000 * fixup or a squash now, we have rearranged it.
5002 if (is_fixup(command
))
5007 get_item_line(&todo_list
, cur
);
5009 get_item_line(&todo_list
, cur
+ 1);
5011 /* replace 'pick', by 'fixup' or 'squash' */
5012 command
= todo_list
.items
[cur
].command
;
5013 if (is_fixup(command
)) {
5015 todo_command_info
[command
].str
);
5016 bol
+= strcspn(bol
, " \t");
5019 strbuf_add(&buf
, bol
, eol
- bol
);
5025 res
= rewrite_file(todo_file
, buf
.buf
, buf
.len
);
5026 strbuf_release(&buf
);
5031 for (i
= 0; i
< todo_list
.nr
; i
++)
5034 hashmap_free(&subject2item
, 1);
5035 todo_list_release(&todo_list
);
5037 clear_commit_todo_item(&commit_todo
);