rebase: set REF_HEAD_DETACH in checkout_up_to_date()

"git rebase A B" where B is not a commit should behave as if the HEAD got detached at B and then the detached HEAD got rebased on top of A. A bug however overwrites the current branch to point at B, when B is a descendant of A (i.e. the rebase ends up being a fast-forward). See [1] for the original bug report. The callstack from checkout_up_to_date() is the following: cmd_rebase() -> checkout_up_to_date() -> reset_head() -> update_refs() -> update_ref() When B is not a valid branch but an oid, rebase sets the head_name of rebase_options to NULL. This value gets passed down this call chain through the branch member of reset_head_opts also getting set to NULL all the way to update_refs(). Then update_refs() checks ropts.branch to decide whether or not to switch branches. If ropts.branch is NULL, it calls update_ref() to update HEAD. At this point however, from rebase's point of view, we want a detached HEAD. But, since checkout_up_to_date() does not set the RESET_HEAD_DETACH flag, the update_ref() call will deference HEAD and update the branch its pointing to. We want the HEAD detached at B instead. Fix this bug by adding the RESET_HEAD_DETACH flag in checkout_up_to_date if B is not a valid branch, so that once reset_head() calls update_refs(), it calls update_ref() with REF_NO_DEREF which updates HEAD directly intead of deferencing it and updating the branch that HEAD points to. Also add a test to ensure the correct behavior. [1] https://lore.kernel.org/git/YiokTm3GxIZQQUow@newk/ Reported-by: Michael McClimon <michael@mcclimon.org> Signed-off-by: John Cai <johncai86@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Cai committed Mar 18, 2022 at 13:54 UTC bdff97a3f6e66107abd136815c8fa1eda5c67aca
2 files changed +11
builtin/rebase.c
+2
@@ -829,6 +829,8 @@ static int checkout_up_to_date(struct rebase_options *options)
829 ropts.oid = &options->orig_head;
830 ropts.branch = options->head_name;
831 ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
832 + if (!ropts.branch)
833 + ropts.flags |= RESET_HEAD_DETACH;
834 ropts.head_msg = buf.buf;
835 if (reset_head(the_repository, &ropts) < 0)
836 ret = error(_("could not switch to %s"), options->switch_to);
t/t3400-rebase.sh
+9
@@ -394,6 +394,15 @@ test_expect_success 'switch to branch not checked out' '
394 git rebase main other
395 '
396
397 +test_expect_success 'switch to non-branch detaches HEAD' '
398 + git checkout main &&
399 + old_main=$(git rev-parse HEAD) &&
400 + git rebase First Second^0 &&
401 + test_cmp_rev HEAD Second &&
402 + test_cmp_rev main $old_main &&
403 + test_must_fail git symbolic-ref HEAD
404 +'
405 +
406 test_expect_success 'refuse to switch to branch checked out elsewhere' '
407 git checkout main &&
408 git worktree add wt &&