Skip to content

Commit c990a4c

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9645459 commit c990a4c

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

builtin/checkout.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
863863
struct stat st;
864864
struct child_process cp;
865865
int counter = 0, len, ret;
866+
unsigned char rev[20];
866867

867868
if (!new->commit)
868869
die(_("no branch specified"));
@@ -920,13 +921,20 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
920921
real_path(get_git_common_dir()), name);
921922
/*
922923
* This is to keep resolve_ref() happy. We need a valid HEAD
923-
* or is_git_directory() will reject the directory. Any valid
924-
* value would do because this value will be ignored and
925-
* replaced at the next (real) checkout.
924+
* or is_git_directory() will reject the directory. Moreover, HEAD
925+
* in the new worktree must resolve to the same value as HEAD in
926+
* the current tree since the command invoked to populate the new
927+
* worktree will be handed the branch/ref specified by the user.
928+
* For instance, if the user asks for the new worktree to be based
929+
* at HEAD~5, then the resolved HEAD~5 in the new worktree must
930+
* match the resolved HEAD~5 in the current tree in order to match
931+
* the user's expectation.
926932
*/
933+
if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
934+
die(_("unable to resolve HEAD"));
927935
strbuf_reset(&sb);
928936
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
929-
write_file(sb.buf, 1, "%s\n", sha1_to_hex(new->commit->object.sha1));
937+
write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
930938
strbuf_reset(&sb);
931939
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
932940
write_file(sb.buf, 1, "../..\n");

t/t2025-checkout-to.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,14 @@ test_expect_success 'checkout with grafts' '
134134
test_cmp expected actual
135135
'
136136

137+
test_expect_success 'checkout --to from relative HEAD' '
138+
test_commit a &&
139+
test_commit b &&
140+
test_commit c &&
141+
git rev-parse HEAD~1 >expected &&
142+
git checkout --to relhead HEAD~1 &&
143+
git -C relhead rev-parse HEAD >actual &&
144+
test_cmp expected actual
145+
'
146+
137147
test_done

0 commit comments

Comments
 (0)