1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-10 08:26:10 +02:00

Merge branch 'pw/rebase-r-fixes' into maint

Various bugs in "git rebase -r" have been fixed.

* pw/rebase-r-fixes:
  rebase -r: fix merge -c with a merge strategy
  rebase -r: don't write .git/MERGE_MSG when fast-forwarding
  rebase -i: add another reword test
  rebase -r: make 'merge -c' behave like reword
This commit is contained in:
Junio C Hamano 2021-10-12 13:51:39 -07:00
commit 3fb1d4da5e
4 changed files with 155 additions and 58 deletions

View File

@ -983,7 +983,8 @@ static int run_git_commit(const char *defmsg,
cmd.git_cmd = 1;
if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
if (is_rebase_i(opts) && !(!defmsg && (flags & AMEND_MSG)) &&
read_env_script(&cmd.env_array)) {
const char *gpg_opt = gpg_sign_opt_quoted(opts);
return error(_(staged_changes_advice),
@ -3739,10 +3740,9 @@ static struct commit *lookup_label(const char *label, int len,
static int do_merge(struct repository *r,
struct commit *commit,
const char *arg, int arg_len,
int flags, struct replay_opts *opts)
int flags, int *check_todo, struct replay_opts *opts)
{
int run_commit_flags = (flags & TODO_EDIT_MERGE_MSG) ?
EDIT_MSG | VERIFY_MSG : 0;
int run_commit_flags = 0;
struct strbuf ref_name = STRBUF_INIT;
struct commit *head_commit, *merge_commit, *i;
struct commit_list *bases, *j, *reversed = NULL;
@ -3816,6 +3816,45 @@ static int do_merge(struct repository *r,
goto leave_merge;
}
/*
* If HEAD is not identical to the first parent of the original merge
* commit, we cannot fast-forward.
*/
can_fast_forward = opts->allow_ff && commit && commit->parents &&
oideq(&commit->parents->item->object.oid,
&head_commit->object.oid);
/*
* If any merge head is different from the original one, we cannot
* fast-forward.
*/
if (can_fast_forward) {
struct commit_list *p = commit->parents->next;
for (j = to_merge; j && p; j = j->next, p = p->next)
if (!oideq(&j->item->object.oid,
&p->item->object.oid)) {
can_fast_forward = 0;
break;
}
/*
* If the number of merge heads differs from the original merge
* commit, we cannot fast-forward.
*/
if (j || p)
can_fast_forward = 0;
}
if (can_fast_forward) {
rollback_lock_file(&lock);
ret = fast_forward_to(r, &commit->object.oid,
&head_commit->object.oid, 0, opts);
if (flags & TODO_EDIT_MERGE_MSG)
goto fast_forward_edit;
goto leave_merge;
}
if (commit) {
const char *encoding = get_commit_output_encoding();
const char *message = logmsg_reencode(commit, NULL, encoding);
@ -3865,46 +3904,6 @@ static int do_merge(struct repository *r,
}
}
/*
* If HEAD is not identical to the first parent of the original merge
* commit, we cannot fast-forward.
*/
can_fast_forward = opts->allow_ff && commit && commit->parents &&
oideq(&commit->parents->item->object.oid,
&head_commit->object.oid);
/*
* If any merge head is different from the original one, we cannot
* fast-forward.
*/
if (can_fast_forward) {
struct commit_list *p = commit->parents->next;
for (j = to_merge; j && p; j = j->next, p = p->next)
if (!oideq(&j->item->object.oid,
&p->item->object.oid)) {
can_fast_forward = 0;
break;
}
/*
* If the number of merge heads differs from the original merge
* commit, we cannot fast-forward.
*/
if (j || p)
can_fast_forward = 0;
}
if (can_fast_forward) {
rollback_lock_file(&lock);
ret = fast_forward_to(r, &commit->object.oid,
&head_commit->object.oid, 0, opts);
if (flags & TODO_EDIT_MERGE_MSG) {
run_commit_flags |= AMEND_MSG;
goto fast_forward_edit;
}
goto leave_merge;
}
if (strategy || to_merge->next) {
/* Octopus merge */
struct child_process cmd = CHILD_PROCESS_INIT;
@ -3935,7 +3934,10 @@ static int do_merge(struct repository *r,
strvec_pushf(&cmd.args,
"-X%s", opts->xopts[k]);
}
strvec_push(&cmd.args, "--no-edit");
if (!(flags & TODO_EDIT_MERGE_MSG))
strvec_push(&cmd.args, "--no-edit");
else
strvec_push(&cmd.args, "--edit");
strvec_push(&cmd.args, "--no-ff");
strvec_push(&cmd.args, "--no-log");
strvec_push(&cmd.args, "--no-stat");
@ -4035,10 +4037,17 @@ static int do_merge(struct repository *r,
* value (a negative one would indicate that the `merge`
* command needs to be rescheduled).
*/
fast_forward_edit:
ret = !!run_git_commit(git_path_merge_msg(r), opts,
run_commit_flags);
if (!ret && flags & TODO_EDIT_MERGE_MSG) {
fast_forward_edit:
*check_todo = 1;
run_commit_flags |= AMEND_MSG | EDIT_MSG | VERIFY_MSG;
ret = !!run_git_commit(NULL, opts, run_commit_flags);
}
leave_merge:
strbuf_release(&ref_name);
rollback_lock_file(&lock);
@ -4405,9 +4414,8 @@ static int pick_commits(struct repository *r,
if ((res = do_reset(r, arg, item->arg_len, opts)))
reschedule = 1;
} else if (item->command == TODO_MERGE) {
if ((res = do_merge(r, item->commit,
arg, item->arg_len,
item->flags, opts)) < 0)
if ((res = do_merge(r, item->commit, arg, item->arg_len,
item->flags, &check_todo, opts)) < 0)
reschedule = 1;
else if (item->commit)
record_in_rewritten(&item->commit->object.oid,

View File

@ -151,3 +151,59 @@ test_editor_unchanged () {
EOF
test_cmp expect actual
}
# Set up an editor for testing reword commands
# Checks that there are no uncommitted changes when rewording and that the
# todo-list is reread after each
set_reword_editor () {
>reword-actual &&
>reword-oid &&
# Check rewording keeps the original authorship
GIT_AUTHOR_NAME="Reword Author"
GIT_AUTHOR_EMAIL="reword.author@example.com"
GIT_AUTHOR_DATE=@123456
write_script reword-sequence-editor.sh <<-\EOF &&
todo="$(cat "$1")" &&
echo "exec git log -1 --pretty=format:'%an <%ae> %at%n%B%n' \
>>reword-actual" >"$1" &&
printf "%s\n" "$todo" >>"$1"
EOF
write_script reword-editor.sh <<-EOF &&
# Save the oid of the first reworded commit so we can check rebase
# fast-forwards to it. Also check that we do not write .git/MERGE_MSG
# when fast-forwarding
if ! test -s reword-oid
then
git rev-parse HEAD >reword-oid &&
if test -f .git/MERGE_MSG
then
echo 1>&2 "error: .git/MERGE_MSG exists"
exit 1
fi
fi &&
# There should be no uncommited changes
git diff --exit-code HEAD &&
# The todo-list should be re-read after a reword
GIT_SEQUENCE_EDITOR="\"$PWD/reword-sequence-editor.sh\"" \
git rebase --edit-todo &&
echo edited >>"\$1"
EOF
test_set_editor "$PWD/reword-editor.sh"
}
# Check the results of a rebase after calling set_reword_editor
# Pass the commits that were reworded in the order that they were picked
# Expects the first pick to be a fast-forward
check_reworded_commits () {
test_cmp_rev "$(cat reword-oid)" "$1^{commit}" &&
git log --format="%an <%ae> %at%n%B%nedited%n" --no-walk=unsorted "$@" \
>reword-expected &&
test_cmp reword-expected reword-actual &&
git log --format="%an <%ae> %at%n%B" -n $# --first-parent --reverse \
>reword-log &&
test_cmp reword-expected reword-log
}

View File

@ -839,6 +839,19 @@ test_expect_success 'reword' '
git show HEAD~2 | grep "C changed"
'
test_expect_success 'no uncommited changes when rewording the todo list is reloaded' '
git checkout E &&
test_when_finished "git checkout @{-1}" &&
(
set_fake_editor &&
GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
export GIT_SEQUENCE_EDITOR &&
set_reword_editor &&
FAKE_LINES="reword 1 reword 2" git rebase -i C
) &&
check_reworded_commits D E
'
test_expect_success 'rebase -i can copy notes' '
git config notes.rewrite.rebase true &&
git config notes.rewriteRef "refs/notes/*" &&

View File

@ -172,19 +172,39 @@ test_expect_success 'failed `merge <branch>` does not crash' '
grep "^Merge branch ${SQ}G${SQ}$" .git/rebase-merge/message
'
test_expect_success 'fast-forward merge -c still rewords' '
git checkout -b fast-forward-merge-c H &&
test_expect_success 'merge -c commits before rewording and reloads todo-list' '
cat >script-from-scratch <<-\EOF &&
merge -c E B
merge -c H G
EOF
git checkout -b merge-c H &&
(
set_fake_editor &&
FAKE_COMMIT_MESSAGE=edited \
GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
git rebase -ir @^
set_reword_editor &&
GIT_SEQUENCE_EDITOR="\"$PWD/replace-editor.sh\"" \
git rebase -i -r D
) &&
echo edited >expected &&
git log --pretty=format:%B -1 >actual &&
test_cmp expected actual
check_reworded_commits E H
'
test_expect_success 'merge -c rewords when a strategy is given' '
git checkout -b merge-c-with-strategy H &&
write_script git-merge-override <<-\EOF &&
echo overridden$1 >G.t
git add G.t
EOF
PATH="$PWD:$PATH" \
GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
GIT_EDITOR="echo edited >>" \
git rebase --no-ff -ir -s override -Xxopt E &&
test_write_lines overridden--xopt >expect &&
test_cmp expect G.t &&
test_write_lines H "" edited "" >expect &&
git log --format=%B -1 >actual &&
test_cmp expect actual
'
test_expect_success 'with a branch tip that was cherry-picked already' '
git checkout -b already-upstream main &&
base="$(git rev-parse --verify HEAD)" &&