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.
This commit is contained in:
Carlos Martín Nieto 2014-09-02 13:10:19 +02:00
parent 538f908175
commit e128a1af6e
2 changed files with 17 additions and 19 deletions

View File

@ -138,23 +138,6 @@ static int update_head_to_new_branch(
return error; 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( static int update_head_to_remote(
git_repository *repo, git_repository *repo,
git_remote *remote, git_remote *remote,
@ -172,8 +155,8 @@ static int update_head_to_remote(
if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0) if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
return error; return error;
/* Did we just clone an empty repository? */ /* We cloned an empty repository or one with an unborn HEAD */
if (!remote_has_branches(refs, refs_len)) if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
return setup_tracking_config( return setup_tracking_config(
repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE); repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);

View File

@ -91,3 +91,18 @@ void test_network_remote_defaultbranch__detached_sharing_nonbranch_id(void)
git_repository_free(cloned_repo); 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);
}