Skip to content

Commit 72a144e

Browse files
committed
Fix "checkout A..." synonym for "checkout A...HEAD" on Windows
When switching to a different commit, we first see the named rev exists as a commit using lookup_commit_reference_gently(), and set new.path to a string "refs/heads/" followed by the name the user gave us (but after taking into special short-hands like @{-1} == "previous branch" and "@{upstream}" == "the branch we merge with" into account). If the resulting string names an existsing ref, then we are switching to that branch (and will be building new commits on top of it); otherwise we are detaching HEAD at that commit. When the "master..." syntax is used as a short-hand for "master...HEAD", we do want to detach HEAD at the merge base. However, on Windows, when asked if ".git/refs/heads/master..." exists, the filesystem happily says "it does" when ".git/refs/heads/master" exists. Work this issue around by first calling check_ref_format(new.path) to see if the string can possibly be a valid ref under "refs/heads/", before asking resolve_ref(). We used to run another lookup_commit_reference(rev) even though we know it succeeded and we have a good commit in new.commit already; this has been with us from 782c2d6 (Build in checkout, 2008-02-07), the first version we had "git checkout" implemented in C. Drop it. Noticed by Alex Riesen. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 230a456 commit 72a144e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

builtin-checkout.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
745745
new.name = arg;
746746
if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
747747
setup_branch_path(&new);
748-
if (resolve_ref(new.path, rev, 1, NULL))
749-
new.commit = lookup_commit_reference(rev);
748+
749+
if ((check_ref_format(new.path) == CHECK_REF_FORMAT_OK) &&
750+
resolve_ref(new.path, rev, 1, NULL))
751+
;
750752
else
751753
new.path = NULL;
752754
parse_commit(new.commit);

0 commit comments

Comments
 (0)