Re: git checkout does not warn about tags without corresponding commits

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Junio C Hamano
Date: Saturday, January 3, 2009 - 4:36 am

Henrik Austad <henrik@austad.us> writes:


I am not surprised.

That one discarded an implementation of "git checkout" in Bourne shell,
with a complete reimplementation in C.

I haven't looked at the code very closely, but I think this should fix
it.  Thorough reviewing (not just running the test suite) is much
appreciated.

-- >8 --
Subject: git-checkout: do not allow switching to a tree-ish

"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.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-checkout.c               |    2 +-
 t/t2011-checkout-invalid-head.sh |    4 ++++
 2 files changed, 5 insertions(+), 1 deletions(-)

diff --git c/builtin-checkout.c w/builtin-checkout.c
index c2c0561..b5dd9c0 100644
--- c/builtin-checkout.c
+++ w/builtin-checkout.c
@@ -681,8 +681,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 c/t/t2011-checkout-invalid-head.sh w/t/t2011-checkout-invalid-head.sh
index 764bb0a..798790d 100755
--- c/t/t2011-checkout-invalid-head.sh
+++ w/t/t2011-checkout-invalid-head.sh
@@ -15,4 +15,8 @@ test_expect_success 'checkout master from invalid HEAD' '
 	git checkout master --
 '
 
+test_expect_success 'checkout should not start branch from a tree' '
+	test_must_fail git checkout -b newbranch master^{tree}
+'
+
 test_done

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: git checkout does not warn about tags without correspo ..., Junio C Hamano, (Sat Jan 3, 4:36 am)