@matheusbsbs / matheus-bagliano / commits / 3442ea4a75
Fork 0

git checkout: do not allow switching to a tree-ish that is not a commit

"git checkout -b newbranch $commit^{tree}" mistakenly created a new branch rooted at the current HEAD, because in that case, the two structure fields used to see if the command was invoked without any argument (hence it needs to default to checking out the HEAD) were populated incorrectly. Upon seeing a command line argument that we took as a rev, we should store that string in new.name, even if that does not name a commit. This will correctly trigger the existing safety logic. Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Daniel Barkalow <barkalow@iabervon.org>

Junio C Hamano committed Jan 3, 2009 at 04:07 UTC 3442ea4a75a3c76f65efac71b414584f765e5a99
2 files changed +5 -1
builtin-checkout.c
+1 -1
@@ -515,8 +515,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
515 argv++;
516 argc--;
517
518 + new.name = arg;
519 if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
519 - new.name = arg;
520 setup_branch_path(&new);
521 if (resolve_ref(new.path, rev, 1, NULL))
522 new.commit = lookup_commit_reference(rev);
t/t2011-checkout-invalid-head.sh
+4
@@ -10,6 +10,10 @@ test_expect_success 'setup' '
10 git commit -m initial
11 '
12
13 +test_expect_success 'checkout should not start branch from a tree' '
14 + test_must_fail git checkout -b newbranch master^{tree}
15 +'
16 +
17 test_expect_success 'checkout master from invalid HEAD' '
18 echo 0000000000000000000000000000000000000000 >.git/HEAD &&
19 git checkout master --