rebase -r: don't write .git/MERGE_MSG when fast-forwarding

When fast-forwarding we do not create a new commit so .git/MERGE_MSG is not removed and can end up seeding the message of a commit made after the rebase has finished. Avoid writing .git/MERGE_MSG when we are fast-forwarding by writing the file after the fast-forward checks. Note that there are no changes to the fast-forward code, it is simply moved. Note that the way this change is implemented means we no longer write the author script when fast-forwarding either. I believe this is safe for the reasons below but it is a departure from what we do when fast-forwarding a non-merge commit. If we reword the merge then 'git commit --amend' will keep the authorship of the commit we're rewording as it ignores GIT_AUTHOR_* unless --reset-author is passed. It will also export the correct GIT_AUTHOR_* variables to any hooks and we already test the authorship of the reworded commit. If we are not rewording then we no longer call spilt_ident() which means we are no longer checking the commit author header looks sane. However this is what we already do when fast-forwarding non-merge commits in skip_unnecessary_picks() so I don't think we're breaking any promises by not checking the author here. Reported-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Aug 20, 2021 at 15:40 UTC baf8ec8d3a0327307f79efd31d5717a0c91c4d8c
2 files changed +49 -42
sequencer.c
+41 -40
@@ -983,7 +983,8 @@ static int run_git_commit(const char *defmsg,
983
984 cmd.git_cmd = 1;
985
986 - if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
986 + if (is_rebase_i(opts) && !(!defmsg && (flags & AMEND_MSG)) &&
987 + read_env_script(&cmd.env_array)) {
988 const char *gpg_opt = gpg_sign_opt_quoted(opts);
989
990 return error(_(staged_changes_advice),
@@ -3815,6 +3816,45 @@ static int do_merge(struct repository *r,
3816 goto leave_merge;
3817 }
3818
3819 + /*
3820 + * If HEAD is not identical to the first parent of the original merge
3821 + * commit, we cannot fast-forward.
3822 + */
3823 + can_fast_forward = opts->allow_ff && commit && commit->parents &&
3824 + oideq(&commit->parents->item->object.oid,
3825 + &head_commit->object.oid);
3826 +
3827 + /*
3828 + * If any merge head is different from the original one, we cannot
3829 + * fast-forward.
3830 + */
3831 + if (can_fast_forward) {
3832 + struct commit_list *p = commit->parents->next;
3833 +
3834 + for (j = to_merge; j && p; j = j->next, p = p->next)
3835 + if (!oideq(&j->item->object.oid,
3836 + &p->item->object.oid)) {
3837 + can_fast_forward = 0;
3838 + break;
3839 + }
3840 + /*
3841 + * If the number of merge heads differs from the original merge
3842 + * commit, we cannot fast-forward.
3843 + */
3844 + if (j || p)
3845 + can_fast_forward = 0;
3846 + }
3847 +
3848 + if (can_fast_forward) {
3849 + rollback_lock_file(&lock);
3850 + ret = fast_forward_to(r, &commit->object.oid,
3851 + &head_commit->object.oid, 0, opts);
3852 + if (flags & TODO_EDIT_MERGE_MSG)
3853 + goto fast_forward_edit;
3854 +
3855 + goto leave_merge;
3856 + }
3857 +
3858 if (commit) {
3859 const char *encoding = get_commit_output_encoding();
3860 const char *message = logmsg_reencode(commit, NULL, encoding);
@@ -3864,45 +3904,6 @@ static int do_merge(struct repository *r,
3904 }
3905 }
3906
3867 - /*
3868 - * If HEAD is not identical to the first parent of the original merge
3869 - * commit, we cannot fast-forward.
3870 - */
3871 - can_fast_forward = opts->allow_ff && commit && commit->parents &&
3872 - oideq(&commit->parents->item->object.oid,
3873 - &head_commit->object.oid);
3874 -
3875 - /*
3876 - * If any merge head is different from the original one, we cannot
3877 - * fast-forward.
3878 - */
3879 - if (can_fast_forward) {
3880 - struct commit_list *p = commit->parents->next;
3881 -
3882 - for (j = to_merge; j && p; j = j->next, p = p->next)
3883 - if (!oideq(&j->item->object.oid,
3884 - &p->item->object.oid)) {
3885 - can_fast_forward = 0;
3886 - break;
3887 - }
3888 - /*
3889 - * If the number of merge heads differs from the original merge
3890 - * commit, we cannot fast-forward.
3891 - */
3892 - if (j || p)
3893 - can_fast_forward = 0;
3894 - }
3895 -
3896 - if (can_fast_forward) {
3897 - rollback_lock_file(&lock);
3898 - ret = fast_forward_to(r, &commit->object.oid,
3899 - &head_commit->object.oid, 0, opts);
3900 - if (flags & TODO_EDIT_MERGE_MSG)
3901 - goto fast_forward_edit;
3902 -
3903 - goto leave_merge;
3904 - }
3905 -
3907 if (strategy || to_merge->next) {
3908 /* Octopus merge */
3909 struct child_process cmd = CHILD_PROCESS_INIT;
t/lib-rebase.sh
+8 -2
@@ -173,10 +173,16 @@ set_reword_editor () {
173
174 write_script reword-editor.sh <<-EOF &&
175 # Save the oid of the first reworded commit so we can check rebase
176 - # fast-forwards to it
176 + # fast-forwards to it. Also check that we do not write .git/MERGE_MSG
177 + # when fast-forwarding
178 if ! test -s reword-oid
179 then
179 - git rev-parse HEAD >reword-oid
180 + git rev-parse HEAD >reword-oid &&
181 + if test -f .git/MERGE_MSG
182 + then
183 + echo 1>&2 "error: .git/MERGE_MSG exists"
184 + exit 1
185 + fi
186 fi &&
187 # There should be no uncommited changes
188 git diff --exit-code HEAD &&