From: Junio C Hamano Date: Sat, 3 Jan 2009 12:07:32 +0000 (-0800) Subject: git checkout: do not allow switching to a tree-ish that is not a commit X-Git-Tag: v1.6.1.1~29^2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3442ea4a75a3c76f65efac71b414584f765e5a99;p=git.git 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 Acked-by: Daniel Barkalow --- diff --git a/builtin-checkout.c b/builtin-checkout.c index c107fd643..544aa5add 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -515,8 +515,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) argv++; argc--; + new.name = arg; if ((new.commit = lookup_commit_reference_gently(rev, 1))) { - new.name = arg; setup_branch_path(&new); if (resolve_ref(new.path, rev, 1, NULL)) new.commit = lookup_commit_reference(rev); diff --git a/t/t2011-checkout-invalid-head.sh b/t/t2011-checkout-invalid-head.sh index 764bb0a6b..15ebdc26e 100755 --- a/t/t2011-checkout-invalid-head.sh +++ b/t/t2011-checkout-invalid-head.sh @@ -10,6 +10,10 @@ test_expect_success 'setup' ' git commit -m initial ' +test_expect_success 'checkout should not start branch from a tree' ' + test_must_fail git checkout -b newbranch master^{tree} +' + test_expect_success 'checkout master from invalid HEAD' ' echo 0000000000000000000000000000000000000000 >.git/HEAD && git checkout master --