Allow clone to handle empty repos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
diff --git a/src/transports/local.c b/src/transports/local.c
index 53b2494..8aeab29 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -50,9 +50,11 @@ static int add_ref(transport_local *t, const char *name)
GITERR_CHECK_ALLOC(head->name);
if (git_reference_name_to_id(&head->oid, t->repo, name) < 0) {
+ /* This is actually okay. Empty repos often have a HEAD that points to
+ * a nonexistant "refs/haeds/master". */
git__free(head->name);
git__free(head);
- return -1;
+ return 0;
}
if (git_vector_insert(&t->refs, head) < 0)
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 623a068..0f4e77a 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -86,3 +86,42 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo
cl_git_mkfile("./foo/bar", "Baz!");
cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options));
}
+
+void test_clone_nonetwork__can_clone_an_empty_local_repo_barely(void)
+{
+ const char *src = cl_git_fixture_url("empty_bare.git");
+ cl_set_cleanup(&cleanup_repository, "./empty");
+
+ git_remote_free(g_origin);
+ cl_git_pass(git_remote_new(&g_origin, NULL, "origin", src, GIT_REMOTE_DEFAULT_FETCH));
+
+ cl_git_pass(git_clone_bare(&g_repo, g_origin, "./empty", NULL, NULL));
+}
+
+void test_clone_nonetwork__can_clone_an_empty_local_repo(void)
+{
+ const char *src = cl_git_fixture_url("empty_bare.git");
+ cl_set_cleanup(&cleanup_repository, "./empty");
+
+ git_remote_free(g_origin);
+ cl_git_pass(git_remote_new(&g_origin, NULL, "origin", src, GIT_REMOTE_DEFAULT_FETCH));
+
+ cl_git_pass(git_clone(&g_repo, g_origin, "./empty", NULL, NULL, NULL));
+}
+
+void test_clone_nonetwork__can_clone_an_empty_standard_repo(void)
+{
+ const char *src;
+
+ cl_git_sandbox_init("empty_standard_repo");
+ src = cl_git_path_url("./empty_standard_repo");
+
+ git_remote_free(g_origin);
+ cl_git_pass(git_remote_new(&g_origin, NULL, "origin", src, GIT_REMOTE_DEFAULT_FETCH));
+
+ cl_set_cleanup(&cleanup_repository, "./empty");
+
+ cl_git_pass(git_clone(&g_repo, g_origin, "./empty", NULL, NULL, NULL));
+
+ cl_git_sandbox_cleanup();
+}