Commit e128a1af6ec552f880051b67d72ab0a4c4c7d074

Carlos Martín Nieto 2014-09-02T13:10:19

clone: correct handling of an unborn HEAD If the remote does not advertise HEAD, then it is unborn and we cannot checkout that branch. Handle it the same way as an empty repo.

diff --git a/src/clone.c b/src/clone.c
index 3dce67d..4472651 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -138,23 +138,6 @@ static int update_head_to_new_branch(
 	return error;
 }
 
-/**
- * Check whether there are any branches among the listed
- * references. It's possible for a repository to have a long list of
- * references without us downloading any of them.
- */
-static bool remote_has_branches(const git_remote_head **refs, size_t len)
-{
-	size_t i;
-
-	for (i = 0; i < len; i++) {
-		if (!git__prefixcmp(refs[i]->name, GIT_REFS_HEADS_DIR))
-			return true;
-	}
-
-	return false;
-}
-
 static int update_head_to_remote(
 		git_repository *repo,
 		git_remote *remote,
@@ -172,8 +155,8 @@ static int update_head_to_remote(
 	if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
 		return error;
 
-	/* Did we just clone an empty repository? */
-	if (!remote_has_branches(refs, refs_len))
+	/* We cloned an empty repository or one with an unborn HEAD */
+	if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
 		return setup_tracking_config(
 			repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
 
diff --git a/tests/network/remote/defaultbranch.c b/tests/network/remote/defaultbranch.c
index 4a49bd8..243369f 100644
--- a/tests/network/remote/defaultbranch.c
+++ b/tests/network/remote/defaultbranch.c
@@ -91,3 +91,18 @@ void test_network_remote_defaultbranch__detached_sharing_nonbranch_id(void)
 
 	git_repository_free(cloned_repo);
 }
+
+void test_network_remote_defaultbranch__unborn_HEAD_with_branches(void)
+{
+	git_reference *ref;
+	git_repository *cloned_repo;
+
+	cl_git_pass(git_reference_symbolic_create(&ref, g_repo_a, "HEAD", "refs/heads/i-dont-exist", 1, NULL, NULL));
+	git_reference_free(ref);
+
+	cl_git_pass(git_clone(&cloned_repo, git_repository_path(g_repo_a), "./semi-empty", NULL));
+
+	cl_assert(git_repository_head_unborn(cloned_repo));
+
+	git_repository_free(cloned_repo);
+}