Skip to content

Commit

Permalink
git checkout: do not allow switching to a tree-ish that is not a commit
Browse files Browse the repository at this point in the history
"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>
  • Loading branch information
Junio C Hamano committed Jan 3, 2009
1 parent 27c03aa commit 3442ea4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builtin-checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions t/t2011-checkout-invalid-head.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down

0 comments on commit 3442ea4

Please sign in to comment.