8 #include "run-command.h"
11 #include "cache-tree.h"
15 #include "merge-recursive.h"
17 #include "argv-array.h"
21 #include "wt-status.h"
23 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
25 const char sign_off_header
[] = "Signed-off-by: ";
26 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
28 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
30 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
31 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
32 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
33 static GIT_PATH_FUNC(git_path_abort_safety_file
, "sequencer/abort-safety")
35 static GIT_PATH_FUNC(rebase_path
, "rebase-merge")
37 * The file containing rebase commands, comments, and empty lines.
38 * This file is created by "git rebase -i" then edited by the user. As
39 * the lines are processed, they are removed from the front of this
40 * file and written to the tail of 'done'.
42 static GIT_PATH_FUNC(rebase_path_todo
, "rebase-merge/git-rebase-todo")
44 * The rebase command lines that have already been processed. A line
45 * is moved here when it is first handled, before any associated user
48 static GIT_PATH_FUNC(rebase_path_done
, "rebase-merge/done")
50 * The commit message that is planned to be used for any changes that
51 * need to be committed following a user interaction.
53 static GIT_PATH_FUNC(rebase_path_message
, "rebase-merge/message")
55 * The file into which is accumulated the suggested commit message for
56 * squash/fixup commands. When the first of a series of squash/fixups
57 * is seen, the file is created and the commit message from the
58 * previous commit and from the first squash/fixup commit are written
59 * to it. The commit message for each subsequent squash/fixup commit
60 * is appended to the file as it is processed.
62 * The first line of the file is of the form
63 * # This is a combination of $count commits.
64 * where $count is the number of commits whose messages have been
65 * written to the file so far (including the initial "pick" commit).
66 * Each time that a commit message is processed, this line is read and
67 * updated. It is deleted just before the combined commit is made.
69 static GIT_PATH_FUNC(rebase_path_squash_msg
, "rebase-merge/message-squash")
71 * If the current series of squash/fixups has not yet included a squash
72 * command, then this file exists and holds the commit message of the
73 * original "pick" commit. (If the series ends without a "squash"
74 * command, then this can be used as the commit message of the combined
75 * commit without opening the editor.)
77 static GIT_PATH_FUNC(rebase_path_fixup_msg
, "rebase-merge/message-fixup")
79 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
80 * GIT_AUTHOR_DATE that will be used for the commit that is currently
83 static GIT_PATH_FUNC(rebase_path_author_script
, "rebase-merge/author-script")
85 * When an "edit" rebase command is being processed, the SHA1 of the
86 * commit to be edited is recorded in this file. When "git rebase
87 * --continue" is executed, if there are any staged changes then they
88 * will be amended to the HEAD commit, but only provided the HEAD
89 * commit is still the commit to be edited. When any other rebase
90 * command is processed, this file is deleted.
92 static GIT_PATH_FUNC(rebase_path_amend
, "rebase-merge/amend")
94 * When we stop at a given patch via the "edit" command, this file contains
95 * the abbreviated commit name of the corresponding patch.
97 static GIT_PATH_FUNC(rebase_path_stopped_sha
, "rebase-merge/stopped-sha")
99 * For the post-rewrite hook, we make a list of rewritten commits and
100 * their new sha1s. The rewritten-pending list keeps the sha1s of
101 * commits that have been processed, but not committed yet,
102 * e.g. because they are waiting for a 'squash' command.
104 static GIT_PATH_FUNC(rebase_path_rewritten_list
, "rebase-merge/rewritten-list")
105 static GIT_PATH_FUNC(rebase_path_rewritten_pending
,
106 "rebase-merge/rewritten-pending")
108 * The following files are written by git-rebase just after parsing the
109 * command-line (and are only consumed, not modified, by the sequencer).
111 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt
, "rebase-merge/gpg_sign_opt")
112 static GIT_PATH_FUNC(rebase_path_orig_head
, "rebase-merge/orig-head")
113 static GIT_PATH_FUNC(rebase_path_verbose
, "rebase-merge/verbose")
114 static GIT_PATH_FUNC(rebase_path_head_name
, "rebase-merge/head-name")
115 static GIT_PATH_FUNC(rebase_path_onto
, "rebase-merge/onto")
116 static GIT_PATH_FUNC(rebase_path_autostash
, "rebase-merge/autostash")
117 static GIT_PATH_FUNC(rebase_path_strategy
, "rebase-merge/strategy")
118 static GIT_PATH_FUNC(rebase_path_strategy_opts
, "rebase-merge/strategy_opts")
120 static inline int is_rebase_i(const struct replay_opts
*opts
)
122 return opts
->action
== REPLAY_INTERACTIVE_REBASE
;
125 static const char *get_dir(const struct replay_opts
*opts
)
127 if (is_rebase_i(opts
))
128 return rebase_path();
129 return git_path_seq_dir();
132 static const char *get_todo_path(const struct replay_opts
*opts
)
134 if (is_rebase_i(opts
))
135 return rebase_path_todo();
136 return git_path_todo_file();
140 * Returns 0 for non-conforming footer
141 * Returns 1 for conforming footer
142 * Returns 2 when sob exists within conforming footer
143 * Returns 3 when sob exists within conforming footer as last entry
145 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
148 struct trailer_info info
;
150 int found_sob
= 0, found_sob_last
= 0;
152 trailer_info_get(&info
, sb
->buf
);
154 if (info
.trailer_start
== info
.trailer_end
)
157 for (i
= 0; i
< info
.trailer_nr
; i
++)
158 if (sob
&& !strncmp(info
.trailers
[i
], sob
->buf
, sob
->len
)) {
160 if (i
== info
.trailer_nr
- 1)
164 trailer_info_release(&info
);
173 static const char *gpg_sign_opt_quoted(struct replay_opts
*opts
)
175 static struct strbuf buf
= STRBUF_INIT
;
179 sq_quotef(&buf
, "-S%s", opts
->gpg_sign
);
183 int sequencer_remove_state(struct replay_opts
*opts
)
185 struct strbuf dir
= STRBUF_INIT
;
188 free(opts
->gpg_sign
);
189 free(opts
->strategy
);
190 for (i
= 0; i
< opts
->xopts_nr
; i
++)
191 free(opts
->xopts
[i
]);
194 strbuf_addf(&dir
, "%s", get_dir(opts
));
195 remove_dir_recursively(&dir
, 0);
196 strbuf_release(&dir
);
201 static const char *action_name(const struct replay_opts
*opts
)
203 switch (opts
->action
) {
207 return N_("cherry-pick");
208 case REPLAY_INTERACTIVE_REBASE
:
209 return N_("rebase -i");
211 die(_("Unknown action: %d"), opts
->action
);
214 struct commit_message
{
221 static const char *short_commit_name(struct commit
*commit
)
223 return find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
226 static int get_message(struct commit
*commit
, struct commit_message
*out
)
228 const char *abbrev
, *subject
;
231 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
232 abbrev
= short_commit_name(commit
);
234 subject_len
= find_commit_subject(out
->message
, &subject
);
236 out
->subject
= xmemdupz(subject
, subject_len
);
237 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
238 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
243 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
245 free(msg
->parent_label
);
248 unuse_commit_buffer(commit
, msg
->message
);
251 static void print_advice(int show_hint
, struct replay_opts
*opts
)
253 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
256 fprintf(stderr
, "%s\n", msg
);
258 * A conflict has occurred but the porcelain
259 * (typically rebase --interactive) wants to take care
260 * of the commit itself so remove CHERRY_PICK_HEAD
262 unlink(git_path_cherry_pick_head());
268 advise(_("after resolving the conflicts, mark the corrected paths\n"
269 "with 'git add <paths>' or 'git rm <paths>'"));
271 advise(_("after resolving the conflicts, mark the corrected paths\n"
272 "with 'git add <paths>' or 'git rm <paths>'\n"
273 "and commit the result with 'git commit'"));
277 static int write_message(const void *buf
, size_t len
, const char *filename
,
280 static struct lock_file msg_file
;
282 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
284 return error_errno(_("could not lock '%s'"), filename
);
285 if (write_in_full(msg_fd
, buf
, len
) < 0) {
286 rollback_lock_file(&msg_file
);
287 return error_errno(_("could not write to '%s'"), filename
);
289 if (append_eol
&& write(msg_fd
, "\n", 1) < 0) {
290 rollback_lock_file(&msg_file
);
291 return error_errno(_("could not write eol to '%s'"), filename
);
293 if (commit_lock_file(&msg_file
) < 0) {
294 rollback_lock_file(&msg_file
);
295 return error(_("failed to finalize '%s'."), filename
);
302 * Reads a file that was presumably written by a shell script, i.e. with an
303 * end-of-line marker that needs to be stripped.
305 * Note that only the last end-of-line marker is stripped, consistent with the
306 * behavior of "$(cat path)" in a shell script.
308 * Returns 1 if the file was read, 0 if it could not be read or does not exist.
310 static int read_oneliner(struct strbuf
*buf
,
311 const char *path
, int skip_if_empty
)
313 int orig_len
= buf
->len
;
315 if (!file_exists(path
))
318 if (strbuf_read_file(buf
, path
, 0) < 0) {
319 warning_errno(_("could not read '%s'"), path
);
323 if (buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\n') {
324 if (--buf
->len
> orig_len
&& buf
->buf
[buf
->len
- 1] == '\r')
326 buf
->buf
[buf
->len
] = '\0';
329 if (skip_if_empty
&& buf
->len
== orig_len
)
335 static struct tree
*empty_tree(void)
337 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
340 static int error_dirty_index(struct replay_opts
*opts
)
342 if (read_cache_unmerged())
343 return error_resolve_conflict(_(action_name(opts
)));
345 error(_("your local changes would be overwritten by %s."),
346 _(action_name(opts
)));
348 if (advice_commit_before_merge
)
349 advise(_("commit your changes or stash them to proceed."));
353 static void update_abort_safety_file(void)
355 struct object_id head
;
357 /* Do nothing on a single-pick */
358 if (!file_exists(git_path_seq_dir()))
361 if (!get_oid("HEAD", &head
))
362 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head
));
364 write_file(git_path_abort_safety_file(), "%s", "");
367 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
368 int unborn
, struct replay_opts
*opts
)
370 struct ref_transaction
*transaction
;
371 struct strbuf sb
= STRBUF_INIT
;
372 struct strbuf err
= STRBUF_INIT
;
375 if (checkout_fast_forward(from
, to
, 1))
376 return -1; /* the callee should have complained already */
378 strbuf_addf(&sb
, _("%s: fast-forward"), _(action_name(opts
)));
380 transaction
= ref_transaction_begin(&err
);
382 ref_transaction_update(transaction
, "HEAD",
383 to
, unborn
? null_sha1
: from
,
385 ref_transaction_commit(transaction
, &err
)) {
386 ref_transaction_free(transaction
);
387 error("%s", err
.buf
);
389 strbuf_release(&err
);
394 strbuf_release(&err
);
395 ref_transaction_free(transaction
);
396 update_abort_safety_file();
400 void append_conflicts_hint(struct strbuf
*msgbuf
)
404 strbuf_addch(msgbuf
, '\n');
405 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
406 for (i
= 0; i
< active_nr
;) {
407 const struct cache_entry
*ce
= active_cache
[i
++];
409 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
410 while (i
< active_nr
&& !strcmp(ce
->name
,
411 active_cache
[i
]->name
))
417 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
418 const char *base_label
, const char *next_label
,
419 unsigned char *head
, struct strbuf
*msgbuf
,
420 struct replay_opts
*opts
)
422 struct merge_options o
;
423 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
426 static struct lock_file index_lock
;
428 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
432 init_merge_options(&o
);
433 o
.ancestor
= base
? base_label
: "(empty tree)";
435 o
.branch2
= next
? next_label
: "(empty tree)";
437 head_tree
= parse_tree_indirect(head
);
438 next_tree
= next
? next
->tree
: empty_tree();
439 base_tree
= base
? base
->tree
: empty_tree();
441 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
442 parse_merge_opt(&o
, *xopt
);
444 clean
= merge_trees(&o
,
446 next_tree
, base_tree
, &result
);
447 strbuf_release(&o
.obuf
);
451 if (active_cache_changed
&&
452 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
453 /* TRANSLATORS: %s will be "revert", "cherry-pick" or
456 return error(_("%s: Unable to write new index file"),
457 _(action_name(opts
)));
458 rollback_lock_file(&index_lock
);
461 append_signoff(msgbuf
, 0, 0);
464 append_conflicts_hint(msgbuf
);
469 static int is_index_unchanged(void)
471 unsigned char head_sha1
[20];
472 struct commit
*head_commit
;
474 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
475 return error(_("could not resolve HEAD commit\n"));
477 head_commit
= lookup_commit(head_sha1
);
480 * If head_commit is NULL, check_commit, called from
481 * lookup_commit, would have indicated that head_commit is not
482 * a commit object already. parse_commit() will return failure
483 * without further complaints in such a case. Otherwise, if
484 * the commit is invalid, parse_commit() will complain. So
485 * there is nothing for us to say here. Just return failure.
487 if (parse_commit(head_commit
))
490 if (!active_cache_tree
)
491 active_cache_tree
= cache_tree();
493 if (!cache_tree_fully_valid(active_cache_tree
))
494 if (cache_tree_update(&the_index
, 0))
495 return error(_("unable to update cache tree\n"));
497 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
500 static int write_author_script(const char *message
)
502 struct strbuf buf
= STRBUF_INIT
;
507 if (!*message
|| starts_with(message
, "\n")) {
509 /* Missing 'author' line? */
510 unlink(rebase_path_author_script());
512 } else if (skip_prefix(message
, "author ", &message
))
514 else if ((eol
= strchr(message
, '\n')))
519 strbuf_addstr(&buf
, "GIT_AUTHOR_NAME='");
520 while (*message
&& *message
!= '\n' && *message
!= '\r')
521 if (skip_prefix(message
, " <", &message
))
523 else if (*message
!= '\'')
524 strbuf_addch(&buf
, *(message
++));
526 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
527 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_EMAIL='");
528 while (*message
&& *message
!= '\n' && *message
!= '\r')
529 if (skip_prefix(message
, "> ", &message
))
531 else if (*message
!= '\'')
532 strbuf_addch(&buf
, *(message
++));
534 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
535 strbuf_addstr(&buf
, "'\nGIT_AUTHOR_DATE='@");
536 while (*message
&& *message
!= '\n' && *message
!= '\r')
537 if (*message
!= '\'')
538 strbuf_addch(&buf
, *(message
++));
540 strbuf_addf(&buf
, "'\\\\%c'", *(message
++));
541 res
= write_message(buf
.buf
, buf
.len
, rebase_path_author_script(), 1);
542 strbuf_release(&buf
);
547 * Read the author-script file into an environment block, ready for use in
548 * run_command(), that can be free()d afterwards.
550 static char **read_author_script(void)
552 struct strbuf script
= STRBUF_INIT
;
557 if (strbuf_read_file(&script
, rebase_path_author_script(), 256) <= 0)
560 for (p
= script
.buf
; *p
; p
++)
561 if (skip_prefix(p
, "'\\\\''", (const char **)&p2
))
562 strbuf_splice(&script
, p
- script
.buf
, p2
- p
, "'", 1);
564 strbuf_splice(&script
, p
-- - script
.buf
, 1, "", 0);
565 else if (*p
== '\n') {
570 env_size
= (count
+ 1) * sizeof(*env
);
571 strbuf_grow(&script
, env_size
);
572 memmove(script
.buf
+ env_size
, script
.buf
, script
.len
);
573 p
= script
.buf
+ env_size
;
574 env
= (char **)strbuf_detach(&script
, NULL
);
576 for (i
= 0; i
< count
; i
++) {
585 static const char staged_changes_advice
[] =
586 N_("you have staged changes in your working tree\n"
587 "If these changes are meant to be squashed into the previous commit, run:\n"
589 " git commit --amend %s\n"
591 "If they are meant to go into a new commit, run:\n"
595 "In both cases, once you're done, continue with:\n"
597 " git rebase --continue\n");
600 * If we are cherry-pick, and if the merge did not result in
601 * hand-editing, we will hit this commit and inherit the original
602 * author date and name.
604 * If we are revert, or if our cherry-pick results in a hand merge,
605 * we had better say that the current user is responsible for that.
607 * An exception is when run_git_commit() is called during an
608 * interactive rebase: in that case, we will want to retain the
611 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
612 int allow_empty
, int edit
, int amend
,
613 int cleanup_commit_message
)
616 struct argv_array array
;
620 if (is_rebase_i(opts
)) {
621 env
= read_author_script();
623 const char *gpg_opt
= gpg_sign_opt_quoted(opts
);
625 return error(_(staged_changes_advice
),
630 argv_array_init(&array
);
631 argv_array_push(&array
, "commit");
632 argv_array_push(&array
, "-n");
635 argv_array_push(&array
, "--amend");
637 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
639 argv_array_push(&array
, "-s");
641 argv_array_pushl(&array
, "-F", defmsg
, NULL
);
642 if (cleanup_commit_message
)
643 argv_array_push(&array
, "--cleanup=strip");
645 argv_array_push(&array
, "-e");
646 else if (!cleanup_commit_message
&&
647 !opts
->signoff
&& !opts
->record_origin
&&
648 git_config_get_value("commit.cleanup", &value
))
649 argv_array_push(&array
, "--cleanup=verbatim");
652 argv_array_push(&array
, "--allow-empty");
654 if (opts
->allow_empty_message
)
655 argv_array_push(&array
, "--allow-empty-message");
657 rc
= run_command_v_opt_cd_env(array
.argv
, RUN_GIT_CMD
, NULL
,
658 (const char *const *)env
);
659 argv_array_clear(&array
);
665 static int is_original_commit_empty(struct commit
*commit
)
667 const unsigned char *ptree_sha1
;
669 if (parse_commit(commit
))
670 return error(_("could not parse commit %s\n"),
671 oid_to_hex(&commit
->object
.oid
));
672 if (commit
->parents
) {
673 struct commit
*parent
= commit
->parents
->item
;
674 if (parse_commit(parent
))
675 return error(_("could not parse parent commit %s\n"),
676 oid_to_hex(&parent
->object
.oid
));
677 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
679 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
682 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
686 * Do we run "git commit" with "--allow-empty"?
688 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
690 int index_unchanged
, empty_commit
;
695 * (1) we do not allow empty at all and error out.
697 * (2) we allow ones that were initially empty, but
698 * forbid the ones that become empty;
702 if (!opts
->allow_empty
)
703 return 0; /* let "git commit" barf as necessary */
705 index_unchanged
= is_index_unchanged();
706 if (index_unchanged
< 0)
707 return index_unchanged
;
708 if (!index_unchanged
)
709 return 0; /* we do not have to say --allow-empty */
711 if (opts
->keep_redundant_commits
)
714 empty_commit
= is_original_commit_empty(commit
);
715 if (empty_commit
< 0)
724 * Note that ordering matters in this enum. Not only must it match the mapping
725 * below, it is also divided into several sections that matter. When adding
726 * new commands, make sure you add it in the right section.
729 /* commands that handle commits */
736 /* commands that do something else than handling a single commit */
738 /* commands that do nothing but are counted for reporting progress */
745 } todo_command_info
[] = {
756 static const char *command_to_string(const enum todo_command command
)
758 if ((size_t)command
< ARRAY_SIZE(todo_command_info
))
759 return todo_command_info
[command
].str
;
760 die("Unknown command: %d", command
);
763 static int is_noop(const enum todo_command command
)
765 return TODO_NOOP
<= (size_t)command
;
768 static int is_fixup(enum todo_command command
)
770 return command
== TODO_FIXUP
|| command
== TODO_SQUASH
;
773 static int update_squash_messages(enum todo_command command
,
774 struct commit
*commit
, struct replay_opts
*opts
)
776 struct strbuf buf
= STRBUF_INIT
;
778 const char *message
, *body
;
780 if (file_exists(rebase_path_squash_msg())) {
781 struct strbuf header
= STRBUF_INIT
;
784 if (strbuf_read_file(&buf
, rebase_path_squash_msg(), 2048) <= 0)
785 return error(_("could not read '%s'"),
786 rebase_path_squash_msg());
789 eol
= strchrnul(buf
.buf
, '\n');
790 if (buf
.buf
[0] != comment_line_char
||
791 (p
+= strcspn(p
, "0123456789\n")) == eol
)
792 return error(_("unexpected 1st line of squash message:"
794 (int)(eol
- buf
.buf
), buf
.buf
);
795 count
= strtol(p
, NULL
, 10);
798 return error(_("invalid 1st line of squash message:\n"
800 (int)(eol
- buf
.buf
), buf
.buf
);
802 strbuf_addf(&header
, "%c ", comment_line_char
);
804 _("This is a combination of %d commits."), ++count
);
805 strbuf_splice(&buf
, 0, eol
- buf
.buf
, header
.buf
, header
.len
);
806 strbuf_release(&header
);
808 unsigned char head
[20];
809 struct commit
*head_commit
;
810 const char *head_message
, *body
;
812 if (get_sha1("HEAD", head
))
813 return error(_("need a HEAD to fixup"));
814 if (!(head_commit
= lookup_commit_reference(head
)))
815 return error(_("could not read HEAD"));
816 if (!(head_message
= get_commit_buffer(head_commit
, NULL
)))
817 return error(_("could not read HEAD's commit message"));
819 find_commit_subject(head_message
, &body
);
820 if (write_message(body
, strlen(body
),
821 rebase_path_fixup_msg(), 0)) {
822 unuse_commit_buffer(head_commit
, head_message
);
823 return error(_("cannot write '%s'"),
824 rebase_path_fixup_msg());
828 strbuf_addf(&buf
, "%c ", comment_line_char
);
829 strbuf_addf(&buf
, _("This is a combination of %d commits."),
831 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
832 strbuf_addstr(&buf
, _("This is the 1st commit message:"));
833 strbuf_addstr(&buf
, "\n\n");
834 strbuf_addstr(&buf
, body
);
836 unuse_commit_buffer(head_commit
, head_message
);
839 if (!(message
= get_commit_buffer(commit
, NULL
)))
840 return error(_("could not read commit message of %s"),
841 oid_to_hex(&commit
->object
.oid
));
842 find_commit_subject(message
, &body
);
844 if (command
== TODO_SQUASH
) {
845 unlink(rebase_path_fixup_msg());
846 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
847 strbuf_addf(&buf
, _("This is the commit message #%d:"), count
);
848 strbuf_addstr(&buf
, "\n\n");
849 strbuf_addstr(&buf
, body
);
850 } else if (command
== TODO_FIXUP
) {
851 strbuf_addf(&buf
, "\n%c ", comment_line_char
);
852 strbuf_addf(&buf
, _("The commit message #%d will be skipped:"),
854 strbuf_addstr(&buf
, "\n\n");
855 strbuf_add_commented_lines(&buf
, body
, strlen(body
));
857 return error(_("unknown command: %d"), command
);
858 unuse_commit_buffer(commit
, message
);
860 res
= write_message(buf
.buf
, buf
.len
, rebase_path_squash_msg(), 0);
861 strbuf_release(&buf
);
865 static void flush_rewritten_pending(void) {
866 struct strbuf buf
= STRBUF_INIT
;
867 unsigned char newsha1
[20];
870 if (strbuf_read_file(&buf
, rebase_path_rewritten_pending(), 82) > 0 &&
871 !get_sha1("HEAD", newsha1
) &&
872 (out
= fopen(rebase_path_rewritten_list(), "a"))) {
873 char *bol
= buf
.buf
, *eol
;
876 eol
= strchrnul(bol
, '\n');
877 fprintf(out
, "%.*s %s\n", (int)(eol
- bol
),
878 bol
, sha1_to_hex(newsha1
));
884 unlink(rebase_path_rewritten_pending());
886 strbuf_release(&buf
);
889 static void record_in_rewritten(struct object_id
*oid
,
890 enum todo_command next_command
) {
891 FILE *out
= fopen(rebase_path_rewritten_pending(), "a");
896 fprintf(out
, "%s\n", oid_to_hex(oid
));
899 if (!is_fixup(next_command
))
900 flush_rewritten_pending();
903 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
904 struct replay_opts
*opts
, int final_fixup
)
906 int edit
= opts
->edit
, cleanup_commit_message
= 0;
907 const char *msg_file
= edit
? NULL
: git_path_merge_msg();
908 unsigned char head
[20];
909 struct commit
*base
, *next
, *parent
;
910 const char *base_label
, *next_label
;
911 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
912 struct strbuf msgbuf
= STRBUF_INIT
;
913 int res
, unborn
= 0, amend
= 0, allow
= 0;
915 if (opts
->no_commit
) {
917 * We do not intend to commit immediately. We just want to
918 * merge the differences in, so let's compute the tree
919 * that represents the "current" state for merge-recursive
922 if (write_cache_as_tree(head
, 0, NULL
))
923 return error(_("your index file is unmerged."));
925 unborn
= get_sha1("HEAD", head
);
927 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
928 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0, 0))
929 return error_dirty_index(opts
);
933 if (!commit
->parents
)
935 else if (commit
->parents
->next
) {
936 /* Reverting or cherry-picking a merge commit */
938 struct commit_list
*p
;
941 return error(_("commit %s is a merge but no -m option was given."),
942 oid_to_hex(&commit
->object
.oid
));
944 for (cnt
= 1, p
= commit
->parents
;
945 cnt
!= opts
->mainline
&& p
;
948 if (cnt
!= opts
->mainline
|| !p
)
949 return error(_("commit %s does not have parent %d"),
950 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
952 } else if (0 < opts
->mainline
)
953 return error(_("mainline was specified but commit %s is not a merge."),
954 oid_to_hex(&commit
->object
.oid
));
956 parent
= commit
->parents
->item
;
958 if (get_message(commit
, &msg
) != 0)
959 return error(_("cannot get commit message for %s"),
960 oid_to_hex(&commit
->object
.oid
));
962 if (opts
->allow_ff
&& !is_fixup(command
) &&
963 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
964 (!parent
&& unborn
))) {
965 if (is_rebase_i(opts
))
966 write_author_script(msg
.message
);
967 res
= fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
,
969 if (res
|| command
!= TODO_REWORD
)
973 goto fast_forward_edit
;
975 if (parent
&& parse_commit(parent
) < 0)
976 /* TRANSLATORS: The first %s will be a "todo" command like
977 "revert" or "pick", the second %s a SHA1. */
978 return error(_("%s: cannot parse parent commit %s"),
979 command_to_string(command
),
980 oid_to_hex(&parent
->object
.oid
));
983 * "commit" is an existing commit. We would want to apply
984 * the difference it introduces since its first parent "prev"
985 * on top of the current HEAD if we are cherry-pick. Or the
986 * reverse of it if we are revert.
989 if (command
== TODO_REVERT
) {
991 base_label
= msg
.label
;
993 next_label
= msg
.parent_label
;
994 strbuf_addstr(&msgbuf
, "Revert \"");
995 strbuf_addstr(&msgbuf
, msg
.subject
);
996 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
997 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
999 if (commit
->parents
&& commit
->parents
->next
) {
1000 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
1001 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
1003 strbuf_addstr(&msgbuf
, ".\n");
1008 base_label
= msg
.parent_label
;
1010 next_label
= msg
.label
;
1012 /* Append the commit log message to msgbuf. */
1013 if (find_commit_subject(msg
.message
, &p
))
1014 strbuf_addstr(&msgbuf
, p
);
1016 if (opts
->record_origin
) {
1017 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
1018 strbuf_addch(&msgbuf
, '\n');
1019 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
1020 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
1021 strbuf_addstr(&msgbuf
, ")\n");
1025 if (command
== TODO_REWORD
)
1027 else if (is_fixup(command
)) {
1028 if (update_squash_messages(command
, commit
, opts
))
1032 msg_file
= rebase_path_squash_msg();
1033 else if (file_exists(rebase_path_fixup_msg())) {
1034 cleanup_commit_message
= 1;
1035 msg_file
= rebase_path_fixup_msg();
1037 const char *dest
= git_path("SQUASH_MSG");
1039 if (copy_file(dest
, rebase_path_squash_msg(), 0666))
1040 return error(_("could not rename '%s' to '%s'"),
1041 rebase_path_squash_msg(), dest
);
1042 unlink(git_path("MERGE_MSG"));
1048 if (is_rebase_i(opts
) && write_author_script(msg
.message
) < 0)
1050 else if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
1051 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
1052 head
, &msgbuf
, opts
);
1055 res
|= write_message(msgbuf
.buf
, msgbuf
.len
,
1056 git_path_merge_msg(), 0);
1058 struct commit_list
*common
= NULL
;
1059 struct commit_list
*remotes
= NULL
;
1061 res
= write_message(msgbuf
.buf
, msgbuf
.len
,
1062 git_path_merge_msg(), 0);
1064 commit_list_insert(base
, &common
);
1065 commit_list_insert(next
, &remotes
);
1066 res
|= try_merge_command(opts
->strategy
,
1067 opts
->xopts_nr
, (const char **)opts
->xopts
,
1068 common
, sha1_to_hex(head
), remotes
);
1069 free_commit_list(common
);
1070 free_commit_list(remotes
);
1072 strbuf_release(&msgbuf
);
1075 * If the merge was clean or if it failed due to conflict, we write
1076 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
1077 * However, if the merge did not even start, then we don't want to
1080 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
1081 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
1082 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
1084 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
1085 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
1086 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
1090 error(command
== TODO_REVERT
1091 ? _("could not revert %s... %s")
1092 : _("could not apply %s... %s"),
1093 short_commit_name(commit
), msg
.subject
);
1094 print_advice(res
== 1, opts
);
1095 rerere(opts
->allow_rerere_auto
);
1099 allow
= allow_empty(opts
, commit
);
1104 if (!opts
->no_commit
)
1106 res
= run_git_commit(msg_file
, opts
, allow
, edit
, amend
,
1107 cleanup_commit_message
);
1109 if (!res
&& final_fixup
) {
1110 unlink(rebase_path_fixup_msg());
1111 unlink(rebase_path_squash_msg());
1115 free_message(commit
, &msg
);
1116 update_abort_safety_file();
1121 static int prepare_revs(struct replay_opts
*opts
)
1124 * picking (but not reverting) ranges (but not individual revisions)
1125 * should be done in reverse
1127 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
1128 opts
->revs
->reverse
^= 1;
1130 if (prepare_revision_walk(opts
->revs
))
1131 return error(_("revision walk setup failed"));
1133 if (!opts
->revs
->commits
)
1134 return error(_("empty commit set passed"));
1138 static int read_and_refresh_cache(struct replay_opts
*opts
)
1140 static struct lock_file index_lock
;
1141 int index_fd
= hold_locked_index(&index_lock
, 0);
1142 if (read_index_preload(&the_index
, NULL
) < 0) {
1143 rollback_lock_file(&index_lock
);
1144 return error(_("git %s: failed to read the index"),
1145 _(action_name(opts
)));
1147 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
1148 if (the_index
.cache_changed
&& index_fd
>= 0) {
1149 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
1150 rollback_lock_file(&index_lock
);
1151 return error(_("git %s: failed to refresh the index"),
1152 _(action_name(opts
)));
1155 rollback_lock_file(&index_lock
);
1160 enum todo_command command
;
1161 struct commit
*commit
;
1164 size_t offset_in_buf
;
1169 struct todo_item
*items
;
1170 int nr
, alloc
, current
;
1173 #define TODO_LIST_INIT { STRBUF_INIT }
1175 static void todo_list_release(struct todo_list
*todo_list
)
1177 strbuf_release(&todo_list
->buf
);
1178 free(todo_list
->items
);
1179 todo_list
->items
= NULL
;
1180 todo_list
->nr
= todo_list
->alloc
= 0;
1183 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
1185 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
1186 return todo_list
->items
+ todo_list
->nr
++;
1189 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
1191 unsigned char commit_sha1
[20];
1192 char *end_of_object_name
;
1193 int i
, saved
, status
, padding
;
1196 bol
+= strspn(bol
, " \t");
1198 if (bol
== eol
|| *bol
== '\r' || *bol
== comment_line_char
) {
1199 item
->command
= TODO_NOOP
;
1200 item
->commit
= NULL
;
1202 item
->arg_len
= eol
- bol
;
1206 for (i
= 0; i
< ARRAY_SIZE(todo_command_info
); i
++)
1207 if (skip_prefix(bol
, todo_command_info
[i
].str
, &bol
)) {
1210 } else if (bol
[1] == ' ' && *bol
== todo_command_info
[i
].c
) {
1215 if (i
>= ARRAY_SIZE(todo_command_info
))
1218 if (item
->command
== TODO_NOOP
) {
1219 item
->commit
= NULL
;
1221 item
->arg_len
= eol
- bol
;
1225 /* Eat up extra spaces/ tabs before object name */
1226 padding
= strspn(bol
, " \t");
1231 if (item
->command
== TODO_EXEC
) {
1233 item
->arg_len
= (int)(eol
- bol
);
1237 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
1238 saved
= *end_of_object_name
;
1239 *end_of_object_name
= '\0';
1240 status
= get_sha1(bol
, commit_sha1
);
1241 *end_of_object_name
= saved
;
1243 item
->arg
= end_of_object_name
+ strspn(end_of_object_name
, " \t");
1244 item
->arg_len
= (int)(eol
- item
->arg
);
1249 item
->commit
= lookup_commit_reference(commit_sha1
);
1250 return !item
->commit
;
1253 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
1255 struct todo_item
*item
;
1256 char *p
= buf
, *next_p
;
1257 int i
, res
= 0, fixup_okay
= file_exists(rebase_path_done());
1259 for (i
= 1; *p
; i
++, p
= next_p
) {
1260 char *eol
= strchrnul(p
, '\n');
1262 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
1264 if (p
!= eol
&& eol
[-1] == '\r')
1265 eol
--; /* strip Carriage Return */
1267 item
= append_new_todo(todo_list
);
1268 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
1269 if (parse_insn_line(item
, p
, eol
)) {
1270 res
= error(_("invalid line %d: %.*s"),
1271 i
, (int)(eol
- p
), p
);
1272 item
->command
= TODO_NOOP
;
1277 else if (is_fixup(item
->command
))
1278 return error(_("cannot '%s' without a previous commit"),
1279 command_to_string(item
->command
));
1280 else if (!is_noop(item
->command
))
1287 static int read_populate_todo(struct todo_list
*todo_list
,
1288 struct replay_opts
*opts
)
1290 const char *todo_file
= get_todo_path(opts
);
1293 strbuf_reset(&todo_list
->buf
);
1294 fd
= open(todo_file
, O_RDONLY
);
1296 return error_errno(_("could not open '%s'"), todo_file
);
1297 if (strbuf_read(&todo_list
->buf
, fd
, 0) < 0) {
1299 return error(_("could not read '%s'."), todo_file
);
1303 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
1305 return error(_("unusable instruction sheet: '%s'"), todo_file
);
1307 if (!todo_list
->nr
&&
1308 (!is_rebase_i(opts
) || !file_exists(rebase_path_done())))
1309 return error(_("no commits parsed."));
1311 if (!is_rebase_i(opts
)) {
1312 enum todo_command valid
=
1313 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
1316 for (i
= 0; i
< todo_list
->nr
; i
++)
1317 if (valid
== todo_list
->items
[i
].command
)
1319 else if (valid
== TODO_PICK
)
1320 return error(_("cannot cherry-pick during a revert."));
1322 return error(_("cannot revert during a cherry-pick."));
1328 static int git_config_string_dup(char **dest
,
1329 const char *var
, const char *value
)
1332 return config_error_nonbool(var
);
1334 *dest
= xstrdup(value
);
1338 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
1340 struct replay_opts
*opts
= data
;
1345 else if (!strcmp(key
, "options.no-commit"))
1346 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
1347 else if (!strcmp(key
, "options.edit"))
1348 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
1349 else if (!strcmp(key
, "options.signoff"))
1350 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
1351 else if (!strcmp(key
, "options.record-origin"))
1352 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
1353 else if (!strcmp(key
, "options.allow-ff"))
1354 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
1355 else if (!strcmp(key
, "options.mainline"))
1356 opts
->mainline
= git_config_int(key
, value
);
1357 else if (!strcmp(key
, "options.strategy"))
1358 git_config_string_dup(&opts
->strategy
, key
, value
);
1359 else if (!strcmp(key
, "options.gpg-sign"))
1360 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
1361 else if (!strcmp(key
, "options.strategy-option")) {
1362 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
1363 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
1365 return error(_("invalid key: %s"), key
);
1368 return error(_("invalid value for %s: %s"), key
, value
);
1373 static void read_strategy_opts(struct replay_opts
*opts
, struct strbuf
*buf
)
1378 if (!read_oneliner(buf
, rebase_path_strategy(), 0))
1380 opts
->strategy
= strbuf_detach(buf
, NULL
);
1381 if (!read_oneliner(buf
, rebase_path_strategy_opts(), 0))
1384 opts
->xopts_nr
= split_cmdline(buf
->buf
, (const char ***)&opts
->xopts
);
1385 for (i
= 0; i
< opts
->xopts_nr
; i
++) {
1386 const char *arg
= opts
->xopts
[i
];
1388 skip_prefix(arg
, "--", &arg
);
1389 opts
->xopts
[i
] = xstrdup(arg
);
1393 static int read_populate_opts(struct replay_opts
*opts
)
1395 if (is_rebase_i(opts
)) {
1396 struct strbuf buf
= STRBUF_INIT
;
1398 if (read_oneliner(&buf
, rebase_path_gpg_sign_opt(), 1)) {
1399 if (!starts_with(buf
.buf
, "-S"))
1402 free(opts
->gpg_sign
);
1403 opts
->gpg_sign
= xstrdup(buf
.buf
+ 2);
1407 if (file_exists(rebase_path_verbose()))
1410 read_strategy_opts(opts
, &buf
);
1411 strbuf_release(&buf
);
1416 if (!file_exists(git_path_opts_file()))
1419 * The function git_parse_source(), called from git_config_from_file(),
1420 * may die() in case of a syntactically incorrect file. We do not care
1421 * about this case, though, because we wrote that file ourselves, so we
1422 * are pretty certain that it is syntactically correct.
1424 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
1425 return error(_("malformed options sheet: '%s'"),
1426 git_path_opts_file());
1430 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
1431 struct replay_opts
*opts
)
1433 enum todo_command command
= opts
->action
== REPLAY_PICK
?
1434 TODO_PICK
: TODO_REVERT
;
1435 const char *command_string
= todo_command_info
[command
].str
;
1436 struct commit
*commit
;
1438 if (prepare_revs(opts
))
1441 while ((commit
= get_revision(opts
->revs
))) {
1442 struct todo_item
*item
= append_new_todo(todo_list
);
1443 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1444 const char *subject
;
1447 item
->command
= command
;
1448 item
->commit
= commit
;
1451 item
->offset_in_buf
= todo_list
->buf
.len
;
1452 subject_len
= find_commit_subject(commit_buffer
, &subject
);
1453 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
1454 short_commit_name(commit
), subject_len
, subject
);
1455 unuse_commit_buffer(commit
, commit_buffer
);
1460 static int create_seq_dir(void)
1462 if (file_exists(git_path_seq_dir())) {
1463 error(_("a cherry-pick or revert is already in progress"));
1464 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
1466 } else if (mkdir(git_path_seq_dir(), 0777) < 0)
1467 return error_errno(_("could not create sequencer directory '%s'"),
1468 git_path_seq_dir());
1472 static int save_head(const char *head
)
1474 static struct lock_file head_lock
;
1475 struct strbuf buf
= STRBUF_INIT
;
1478 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
1480 rollback_lock_file(&head_lock
);
1481 return error_errno(_("could not lock HEAD"));
1483 strbuf_addf(&buf
, "%s\n", head
);
1484 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
1485 rollback_lock_file(&head_lock
);
1486 return error_errno(_("could not write to '%s'"),
1487 git_path_head_file());
1489 if (commit_lock_file(&head_lock
) < 0) {
1490 rollback_lock_file(&head_lock
);
1491 return error(_("failed to finalize '%s'."), git_path_head_file());
1496 static int rollback_is_safe(void)
1498 struct strbuf sb
= STRBUF_INIT
;
1499 struct object_id expected_head
, actual_head
;
1501 if (strbuf_read_file(&sb
, git_path_abort_safety_file(), 0) >= 0) {
1503 if (get_oid_hex(sb
.buf
, &expected_head
)) {
1504 strbuf_release(&sb
);
1505 die(_("could not parse %s"), git_path_abort_safety_file());
1507 strbuf_release(&sb
);
1509 else if (errno
== ENOENT
)
1510 oidclr(&expected_head
);
1512 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
1514 if (get_oid("HEAD", &actual_head
))
1515 oidclr(&actual_head
);
1517 return !oidcmp(&actual_head
, &expected_head
);
1520 static int reset_for_rollback(const unsigned char *sha1
)
1522 const char *argv
[4]; /* reset --merge <arg> + NULL */
1525 argv
[1] = "--merge";
1526 argv
[2] = sha1_to_hex(sha1
);
1528 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1531 static int rollback_single_pick(void)
1533 unsigned char head_sha1
[20];
1535 if (!file_exists(git_path_cherry_pick_head()) &&
1536 !file_exists(git_path_revert_head()))
1537 return error(_("no cherry-pick or revert in progress"));
1538 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
1539 return error(_("cannot resolve HEAD"));
1540 if (is_null_sha1(head_sha1
))
1541 return error(_("cannot abort from a branch yet to be born"));
1542 return reset_for_rollback(head_sha1
);
1545 int sequencer_rollback(struct replay_opts
*opts
)
1548 unsigned char sha1
[20];
1549 struct strbuf buf
= STRBUF_INIT
;
1551 f
= fopen(git_path_head_file(), "r");
1552 if (!f
&& errno
== ENOENT
) {
1554 * There is no multiple-cherry-pick in progress.
1555 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
1556 * a single-cherry-pick in progress, abort that.
1558 return rollback_single_pick();
1561 return error_errno(_("cannot open '%s'"), git_path_head_file());
1562 if (strbuf_getline_lf(&buf
, f
)) {
1563 error(_("cannot read '%s': %s"), git_path_head_file(),
1564 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
1569 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
1570 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
1571 git_path_head_file());
1574 if (is_null_sha1(sha1
)) {
1575 error(_("cannot abort from a branch yet to be born"));
1579 if (!rollback_is_safe()) {
1580 /* Do not error, just do not rollback */
1581 warning(_("You seem to have moved HEAD. "
1582 "Not rewinding, check your HEAD!"));
1584 if (reset_for_rollback(sha1
))
1586 strbuf_release(&buf
);
1587 return sequencer_remove_state(opts
);
1589 strbuf_release(&buf
);
1593 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1595 static struct lock_file todo_lock
;
1596 const char *todo_path
= get_todo_path(opts
);
1597 int next
= todo_list
->current
, offset
, fd
;
1600 * rebase -i writes "git-rebase-todo" without the currently executing
1601 * command, appending it to "done" instead.
1603 if (is_rebase_i(opts
))
1606 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
1608 return error_errno(_("could not lock '%s'"), todo_path
);
1609 offset
= next
< todo_list
->nr
?
1610 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
1611 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
1612 todo_list
->buf
.len
- offset
) < 0)
1613 return error_errno(_("could not write to '%s'"), todo_path
);
1614 if (commit_lock_file(&todo_lock
) < 0)
1615 return error(_("failed to finalize '%s'."), todo_path
);
1617 if (is_rebase_i(opts
)) {
1618 const char *done_path
= rebase_path_done();
1619 int fd
= open(done_path
, O_CREAT
| O_WRONLY
| O_APPEND
, 0666);
1620 int prev_offset
= !next
? 0 :
1621 todo_list
->items
[next
- 1].offset_in_buf
;
1623 if (fd
>= 0 && offset
> prev_offset
&&
1624 write_in_full(fd
, todo_list
->buf
.buf
+ prev_offset
,
1625 offset
- prev_offset
) < 0) {
1627 return error_errno(_("could not write to '%s'"),
1636 static int save_opts(struct replay_opts
*opts
)
1638 const char *opts_file
= git_path_opts_file();
1641 if (opts
->no_commit
)
1642 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
1644 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
1646 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
1647 if (opts
->record_origin
)
1648 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
1650 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
1651 if (opts
->mainline
) {
1652 struct strbuf buf
= STRBUF_INIT
;
1653 strbuf_addf(&buf
, "%d", opts
->mainline
);
1654 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
1655 strbuf_release(&buf
);
1658 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
1660 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
1663 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1664 res
|= git_config_set_multivar_in_file_gently(opts_file
,
1665 "options.strategy-option",
1666 opts
->xopts
[i
], "^$", 0);
1671 static int make_patch(struct commit
*commit
, struct replay_opts
*opts
)
1673 struct strbuf buf
= STRBUF_INIT
;
1674 struct rev_info log_tree_opt
;
1675 const char *subject
, *p
;
1678 p
= short_commit_name(commit
);
1679 if (write_message(p
, strlen(p
), rebase_path_stopped_sha(), 1) < 0)
1682 strbuf_addf(&buf
, "%s/patch", get_dir(opts
));
1683 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
1684 init_revisions(&log_tree_opt
, NULL
);
1685 log_tree_opt
.abbrev
= 0;
1686 log_tree_opt
.diff
= 1;
1687 log_tree_opt
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1688 log_tree_opt
.disable_stdin
= 1;
1689 log_tree_opt
.no_commit_id
= 1;
1690 log_tree_opt
.diffopt
.file
= fopen(buf
.buf
, "w");
1691 log_tree_opt
.diffopt
.use_color
= GIT_COLOR_NEVER
;
1692 if (!log_tree_opt
.diffopt
.file
)
1693 res
|= error_errno(_("could not open '%s'"), buf
.buf
);
1695 res
|= log_tree_commit(&log_tree_opt
, commit
);
1696 fclose(log_tree_opt
.diffopt
.file
);
1700 strbuf_addf(&buf
, "%s/message", get_dir(opts
));
1701 if (!file_exists(buf
.buf
)) {
1702 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
1703 find_commit_subject(commit_buffer
, &subject
);
1704 res
|= write_message(subject
, strlen(subject
), buf
.buf
, 1);
1705 unuse_commit_buffer(commit
, commit_buffer
);
1707 strbuf_release(&buf
);
1712 static int intend_to_amend(void)
1714 unsigned char head
[20];
1717 if (get_sha1("HEAD", head
))
1718 return error(_("cannot read HEAD"));
1720 p
= sha1_to_hex(head
);
1721 return write_message(p
, strlen(p
), rebase_path_amend(), 1);
1724 static int error_with_patch(struct commit
*commit
,
1725 const char *subject
, int subject_len
,
1726 struct replay_opts
*opts
, int exit_code
, int to_amend
)
1728 if (make_patch(commit
, opts
))
1732 if (intend_to_amend())
1735 fprintf(stderr
, "You can amend the commit now, with\n"
1737 " git commit --amend %s\n"
1739 "Once you are satisfied with your changes, run\n"
1741 " git rebase --continue\n", gpg_sign_opt_quoted(opts
));
1742 } else if (exit_code
)
1743 fprintf(stderr
, "Could not apply %s... %.*s\n",
1744 short_commit_name(commit
), subject_len
, subject
);
1749 static int error_failed_squash(struct commit
*commit
,
1750 struct replay_opts
*opts
, int subject_len
, const char *subject
)
1752 if (rename(rebase_path_squash_msg(), rebase_path_message()))
1753 return error(_("could not rename '%s' to '%s'"),
1754 rebase_path_squash_msg(), rebase_path_message());
1755 unlink(rebase_path_fixup_msg());
1756 unlink(git_path("MERGE_MSG"));
1757 if (copy_file(git_path("MERGE_MSG"), rebase_path_message(), 0666))
1758 return error(_("could not copy '%s' to '%s'"),
1759 rebase_path_message(), git_path("MERGE_MSG"));
1760 return error_with_patch(commit
, subject
, subject_len
, opts
, 1, 0);
1763 static int do_exec(const char *command_line
)
1765 const char *child_argv
[] = { NULL
, NULL
};
1768 fprintf(stderr
, "Executing: %s\n", command_line
);
1769 child_argv
[0] = command_line
;
1770 status
= run_command_v_opt(child_argv
, RUN_USING_SHELL
);
1772 /* force re-reading of the cache */
1773 if (discard_cache() < 0 || read_cache() < 0)
1774 return error(_("could not read index"));
1776 dirty
= require_clean_work_tree("rebase", NULL
, 1, 1);
1779 warning(_("execution failed: %s\n%s"
1780 "You can fix the problem, and then run\n"
1782 " git rebase --continue\n"
1785 dirty
? N_("and made changes to the index and/or the "
1786 "working tree\n") : "");
1788 /* command not found */
1791 warning(_("execution succeeded: %s\nbut "
1792 "left changes to the index and/or the working tree\n"
1793 "Commit or stash your changes, and then run\n"
1795 " git rebase --continue\n"
1796 "\n"), command_line
);
1803 static int is_final_fixup(struct todo_list
*todo_list
)
1805 int i
= todo_list
->current
;
1807 if (!is_fixup(todo_list
->items
[i
].command
))
1810 while (++i
< todo_list
->nr
)
1811 if (is_fixup(todo_list
->items
[i
].command
))
1813 else if (!is_noop(todo_list
->items
[i
].command
))
1818 static enum todo_command
peek_command(struct todo_list
*todo_list
, int offset
)
1822 for (i
= todo_list
->current
+ offset
; i
< todo_list
->nr
; i
++)
1823 if (!is_noop(todo_list
->items
[i
].command
))
1824 return todo_list
->items
[i
].command
;
1829 static int apply_autostash(struct replay_opts
*opts
)
1831 struct strbuf stash_sha1
= STRBUF_INIT
;
1832 struct child_process child
= CHILD_PROCESS_INIT
;
1835 if (!read_oneliner(&stash_sha1
, rebase_path_autostash(), 1)) {
1836 strbuf_release(&stash_sha1
);
1839 strbuf_trim(&stash_sha1
);
1842 argv_array_push(&child
.args
, "stash");
1843 argv_array_push(&child
.args
, "apply");
1844 argv_array_push(&child
.args
, stash_sha1
.buf
);
1845 if (!run_command(&child
))
1846 printf(_("Applied autostash."));
1848 struct child_process store
= CHILD_PROCESS_INIT
;
1851 argv_array_push(&store
.args
, "stash");
1852 argv_array_push(&store
.args
, "store");
1853 argv_array_push(&store
.args
, "-m");
1854 argv_array_push(&store
.args
, "autostash");
1855 argv_array_push(&store
.args
, "-q");
1856 argv_array_push(&store
.args
, stash_sha1
.buf
);
1857 if (run_command(&store
))
1858 ret
= error(_("cannot store %s"), stash_sha1
.buf
);
1860 printf(_("Applying autostash resulted in conflicts.\n"
1861 "Your changes are safe in the stash.\n"
1862 "You can run \"git stash pop\" or"
1863 " \"git stash drop\" at any time.\n"));
1866 strbuf_release(&stash_sha1
);
1870 static const char *reflog_message(struct replay_opts
*opts
,
1871 const char *sub_action
, const char *fmt
, ...)
1874 static struct strbuf buf
= STRBUF_INIT
;
1878 strbuf_addstr(&buf
, action_name(opts
));
1880 strbuf_addf(&buf
, " (%s)", sub_action
);
1882 strbuf_addstr(&buf
, ": ");
1883 strbuf_vaddf(&buf
, fmt
, ap
);
1890 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1894 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1896 assert(!(opts
->signoff
|| opts
->no_commit
||
1897 opts
->record_origin
|| opts
->edit
));
1898 if (read_and_refresh_cache(opts
))
1901 while (todo_list
->current
< todo_list
->nr
) {
1902 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
1903 if (save_todo(todo_list
, opts
))
1905 if (is_rebase_i(opts
)) {
1906 unlink(rebase_path_message());
1907 unlink(rebase_path_author_script());
1908 unlink(rebase_path_stopped_sha());
1909 unlink(rebase_path_amend());
1911 if (item
->command
<= TODO_SQUASH
) {
1912 if (is_rebase_i(opts
))
1913 setenv("GIT_REFLOG_ACTION", reflog_message(opts
,
1914 command_to_string(item
->command
), NULL
),
1916 res
= do_pick_commit(item
->command
, item
->commit
,
1917 opts
, is_final_fixup(todo_list
));
1918 if (is_rebase_i(opts
) && res
< 0) {
1920 todo_list
->current
--;
1921 if (save_todo(todo_list
, opts
))
1924 if (item
->command
== TODO_EDIT
) {
1925 struct commit
*commit
= item
->commit
;
1927 warning(_("stopped at %s... %.*s"),
1928 short_commit_name(commit
),
1929 item
->arg_len
, item
->arg
);
1930 return error_with_patch(commit
,
1931 item
->arg
, item
->arg_len
, opts
, res
,
1934 if (is_rebase_i(opts
) && !res
)
1935 record_in_rewritten(&item
->commit
->object
.oid
,
1936 peek_command(todo_list
, 1));
1937 if (res
&& is_fixup(item
->command
)) {
1940 return error_failed_squash(item
->commit
, opts
,
1941 item
->arg_len
, item
->arg
);
1942 } else if (res
&& is_rebase_i(opts
))
1943 return res
| error_with_patch(item
->commit
,
1944 item
->arg
, item
->arg_len
, opts
, res
,
1945 item
->command
== TODO_REWORD
);
1946 } else if (item
->command
== TODO_EXEC
) {
1947 char *end_of_arg
= (char *)(item
->arg
+ item
->arg_len
);
1948 int saved
= *end_of_arg
;
1951 res
= do_exec(item
->arg
);
1952 *end_of_arg
= saved
;
1953 } else if (!is_noop(item
->command
))
1954 return error(_("unknown command %d"), item
->command
);
1956 todo_list
->current
++;
1961 if (is_rebase_i(opts
)) {
1962 struct strbuf head_ref
= STRBUF_INIT
, buf
= STRBUF_INIT
;
1965 /* Stopped in the middle, as planned? */
1966 if (todo_list
->current
< todo_list
->nr
)
1969 if (read_oneliner(&head_ref
, rebase_path_head_name(), 0) &&
1970 starts_with(head_ref
.buf
, "refs/")) {
1972 unsigned char head
[20], orig
[20];
1975 if (get_sha1("HEAD", head
)) {
1976 res
= error(_("cannot read HEAD"));
1978 strbuf_release(&head_ref
);
1979 strbuf_release(&buf
);
1982 if (!read_oneliner(&buf
, rebase_path_orig_head(), 0) ||
1983 get_sha1_hex(buf
.buf
, orig
)) {
1984 res
= error(_("could not read orig-head"));
1985 goto cleanup_head_ref
;
1987 if (!read_oneliner(&buf
, rebase_path_onto(), 0)) {
1988 res
= error(_("could not read 'onto'"));
1989 goto cleanup_head_ref
;
1991 msg
= reflog_message(opts
, "finish", "%s onto %s",
1992 head_ref
.buf
, buf
.buf
);
1993 if (update_ref(msg
, head_ref
.buf
, head
, orig
,
1994 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
)) {
1995 res
= error(_("could not update %s"),
1997 goto cleanup_head_ref
;
1999 msg
= reflog_message(opts
, "finish", "returning to %s",
2001 if (create_symref("HEAD", head_ref
.buf
, msg
)) {
2002 res
= error(_("could not update HEAD to %s"),
2004 goto cleanup_head_ref
;
2009 if (opts
->verbose
) {
2010 struct rev_info log_tree_opt
;
2011 struct object_id orig
, head
;
2013 memset(&log_tree_opt
, 0, sizeof(log_tree_opt
));
2014 init_revisions(&log_tree_opt
, NULL
);
2015 log_tree_opt
.diff
= 1;
2016 log_tree_opt
.diffopt
.output_format
=
2017 DIFF_FORMAT_DIFFSTAT
;
2018 log_tree_opt
.disable_stdin
= 1;
2020 if (read_oneliner(&buf
, rebase_path_orig_head(), 0) &&
2021 !get_sha1(buf
.buf
, orig
.hash
) &&
2022 !get_sha1("HEAD", head
.hash
)) {
2023 diff_tree_sha1(orig
.hash
, head
.hash
,
2024 "", &log_tree_opt
.diffopt
);
2025 log_tree_diff_flush(&log_tree_opt
);
2028 flush_rewritten_pending();
2029 if (!stat(rebase_path_rewritten_list(), &st
) &&
2031 struct child_process child
= CHILD_PROCESS_INIT
;
2032 const char *post_rewrite_hook
=
2033 find_hook("post-rewrite");
2035 child
.in
= open(rebase_path_rewritten_list(), O_RDONLY
);
2037 argv_array_push(&child
.args
, "notes");
2038 argv_array_push(&child
.args
, "copy");
2039 argv_array_push(&child
.args
, "--for-rewrite=rebase");
2040 /* we don't care if this copying failed */
2041 run_command(&child
);
2043 if (post_rewrite_hook
) {
2044 struct child_process hook
= CHILD_PROCESS_INIT
;
2046 hook
.in
= open(rebase_path_rewritten_list(),
2048 hook
.stdout_to_stderr
= 1;
2049 argv_array_push(&hook
.args
, post_rewrite_hook
);
2050 argv_array_push(&hook
.args
, "rebase");
2051 /* we don't care if this hook failed */
2055 apply_autostash(opts
);
2057 strbuf_release(&buf
);
2058 strbuf_release(&head_ref
);
2062 * Sequence of picks finished successfully; cleanup by
2063 * removing the .git/sequencer directory
2065 return sequencer_remove_state(opts
);
2068 static int continue_single_pick(void)
2070 const char *argv
[] = { "commit", NULL
};
2072 if (!file_exists(git_path_cherry_pick_head()) &&
2073 !file_exists(git_path_revert_head()))
2074 return error(_("no cherry-pick or revert in progress"));
2075 return run_command_v_opt(argv
, RUN_GIT_CMD
);
2078 static int commit_staged_changes(struct replay_opts
*opts
)
2082 if (has_unstaged_changes(1))
2083 return error(_("cannot rebase: You have unstaged changes."));
2084 if (!has_uncommitted_changes(0)) {
2085 const char *cherry_pick_head
= git_path("CHERRY_PICK_HEAD");
2087 if (file_exists(cherry_pick_head
) && unlink(cherry_pick_head
))
2088 return error(_("could not remove CHERRY_PICK_HEAD"));
2092 if (file_exists(rebase_path_amend())) {
2093 struct strbuf rev
= STRBUF_INIT
;
2094 unsigned char head
[20], to_amend
[20];
2096 if (get_sha1("HEAD", head
))
2097 return error(_("cannot amend non-existing commit"));
2098 if (!read_oneliner(&rev
, rebase_path_amend(), 0))
2099 return error(_("invalid file: '%s'"), rebase_path_amend());
2100 if (get_sha1_hex(rev
.buf
, to_amend
))
2101 return error(_("invalid contents: '%s'"),
2102 rebase_path_amend());
2103 if (hashcmp(head
, to_amend
))
2104 return error(_("\nYou have uncommitted changes in your "
2105 "working tree. Please, commit them\n"
2106 "first and then run 'git rebase "
2107 "--continue' again."));
2109 strbuf_release(&rev
);
2113 if (run_git_commit(rebase_path_message(), opts
, 1, 1, amend
, 0))
2114 return error(_("could not commit staged changes."));
2115 unlink(rebase_path_amend());
2119 int sequencer_continue(struct replay_opts
*opts
)
2121 struct todo_list todo_list
= TODO_LIST_INIT
;
2124 if (read_and_refresh_cache(opts
))
2127 if (is_rebase_i(opts
)) {
2128 if (commit_staged_changes(opts
))
2130 } else if (!file_exists(get_todo_path(opts
)))
2131 return continue_single_pick();
2132 if (read_populate_opts(opts
))
2134 if ((res
= read_populate_todo(&todo_list
, opts
)))
2135 goto release_todo_list
;
2137 if (!is_rebase_i(opts
)) {
2138 /* Verify that the conflict has been resolved */
2139 if (file_exists(git_path_cherry_pick_head()) ||
2140 file_exists(git_path_revert_head())) {
2141 res
= continue_single_pick();
2143 goto release_todo_list
;
2145 if (index_differs_from("HEAD", 0, 0)) {
2146 res
= error_dirty_index(opts
);
2147 goto release_todo_list
;
2149 todo_list
.current
++;
2150 } else if (file_exists(rebase_path_stopped_sha())) {
2151 struct strbuf buf
= STRBUF_INIT
;
2152 struct object_id oid
;
2154 if (read_oneliner(&buf
, rebase_path_stopped_sha(), 1) &&
2155 !get_sha1_committish(buf
.buf
, oid
.hash
))
2156 record_in_rewritten(&oid
, peek_command(&todo_list
, 0));
2157 strbuf_release(&buf
);
2160 res
= pick_commits(&todo_list
, opts
);
2162 todo_list_release(&todo_list
);
2166 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
2168 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
2169 return do_pick_commit(opts
->action
== REPLAY_PICK
?
2170 TODO_PICK
: TODO_REVERT
, cmit
, opts
, 0);
2173 int sequencer_pick_revisions(struct replay_opts
*opts
)
2175 struct todo_list todo_list
= TODO_LIST_INIT
;
2176 unsigned char sha1
[20];
2180 if (read_and_refresh_cache(opts
))
2183 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
2184 unsigned char sha1
[20];
2185 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
2187 /* This happens when using --stdin. */
2191 if (!get_sha1(name
, sha1
)) {
2192 if (!lookup_commit_reference_gently(sha1
, 1)) {
2193 enum object_type type
= sha1_object_info(sha1
, NULL
);
2194 return error(_("%s: can't cherry-pick a %s"),
2195 name
, typename(type
));
2198 return error(_("%s: bad revision"), name
);
2202 * If we were called as "git cherry-pick <commit>", just
2203 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
2204 * REVERT_HEAD, and don't touch the sequencer state.
2205 * This means it is possible to cherry-pick in the middle
2206 * of a cherry-pick sequence.
2208 if (opts
->revs
->cmdline
.nr
== 1 &&
2209 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
2210 opts
->revs
->no_walk
&&
2211 !opts
->revs
->cmdline
.rev
->flags
) {
2212 struct commit
*cmit
;
2213 if (prepare_revision_walk(opts
->revs
))
2214 return error(_("revision walk setup failed"));
2215 cmit
= get_revision(opts
->revs
);
2216 if (!cmit
|| get_revision(opts
->revs
))
2217 return error("BUG: expected exactly one commit from walk");
2218 return single_pick(cmit
, opts
);
2222 * Start a new cherry-pick/ revert sequence; but
2223 * first, make sure that an existing one isn't in
2227 if (walk_revs_populate_todo(&todo_list
, opts
) ||
2228 create_seq_dir() < 0)
2230 if (get_sha1("HEAD", sha1
) && (opts
->action
== REPLAY_REVERT
))
2231 return error(_("can't revert as initial commit"));
2232 if (save_head(sha1_to_hex(sha1
)))
2234 if (save_opts(opts
))
2236 update_abort_safety_file();
2237 res
= pick_commits(&todo_list
, opts
);
2238 todo_list_release(&todo_list
);
2242 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
2244 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
2245 struct strbuf sob
= STRBUF_INIT
;
2248 strbuf_addstr(&sob
, sign_off_header
);
2249 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
2250 getenv("GIT_COMMITTER_EMAIL")));
2251 strbuf_addch(&sob
, '\n');
2254 * If the whole message buffer is equal to the sob, pretend that we
2255 * found a conforming footer with a matching sob
2257 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
2258 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
2261 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
2264 const char *append_newlines
= NULL
;
2265 size_t len
= msgbuf
->len
- ignore_footer
;
2269 * The buffer is completely empty. Leave foom for
2270 * the title and body to be filled in by the user.
2272 append_newlines
= "\n\n";
2273 } else if (msgbuf
->buf
[len
- 1] != '\n') {
2275 * Incomplete line. Complete the line and add a
2276 * blank one so that there is an empty line between
2277 * the message body and the sob.
2279 append_newlines
= "\n\n";
2280 } else if (len
== 1) {
2282 * Buffer contains a single newline. Add another
2283 * so that we leave room for the title and body.
2285 append_newlines
= "\n";
2286 } else if (msgbuf
->buf
[len
- 2] != '\n') {
2288 * Buffer ends with a single newline. Add another
2289 * so that there is an empty line between the message
2292 append_newlines
= "\n";
2293 } /* else, the buffer already ends with two newlines. */
2295 if (append_newlines
)
2296 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2297 append_newlines
, strlen(append_newlines
));
2300 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
2301 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
2304 strbuf_release(&sob
);