checkout: fix bug with --to and relative HEAD
commitc990a4c11dd7bb671da1b30e14568ad986621488
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 6 Jul 2015 17:30:45 +0000 (6 13:30 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 6 Jul 2015 18:07:44 +0000 (6 11:07 -0700)
treeec516c7ba3f8aa6d76c6f03bfd7146cdb9388c03
parent96454597562dade701fbfee19891482a97456138
checkout: fix bug with --to and relative HEAD

Given "git checkout --to <path> HEAD~1", the new worktree's HEAD should
begin life at the current branch's HEAD~1, however, it actually ends up
at HEAD~2. This happens because:

    1. git-checkout resolves HEAD~1

    2. to satisfy is_git_directory(), prepare_linked_worktree() creates
       a HEAD for the new worktree with the value of the resolved HEAD~1

    3. git-checkout re-invokes itself with the same arguments within the
       new worktree to populate the worktree

    4. the sub git-checkout resolves HEAD~1 relative to its own HEAD,
       which is the resolved HEAD~1 from the original invocation,
       resulting unexpectedly and incorrectly in HEAD~2 (relative to the
       original)

Fix this by unconditionally assigning the current worktree's HEAD as the
value of the new worktree's HEAD.

As a side-effect, this change also eliminates a dependence within
prepare_linked_checkout() upon 'struct branch_info'. The plan is to
eventually relocate "git checkout --to" functionality to "git worktree
add", and worktree.c won't have knowledge of 'struct branch_info', so
removal of this dependency is a step toward that goal.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t2025-checkout-to.sh