Commit 5a9d850e76c0ff5bc3118f3d129e8a9b6c07c262

Edward Thomson 2016-08-05T19:30:56

odb: only provide the empty tree Only provide the empty tree internally, which matches git's behavior. If we provide the empty blob then any users trying to write it with libgit2 would omit it from actually landing in the odb, which appear to git proper as a broken repository (missing that object).

diff --git a/src/odb.c b/src/odb.c
index f396149..1ecfe93 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -803,19 +803,12 @@ int git_odb__read_header_or_object(
 	return 0;
 }
 
-static git_oid empty_blob = {{ 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b,
-			       0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91 }};
 static git_oid empty_tree = {{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
 			       0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};
 
 static int hardcoded_objects(git_rawobj *raw, const git_oid *id)
 {
-	if (!git_oid_cmp(id, &empty_blob)) {
-		raw->type = GIT_OBJ_BLOB;
-		raw->len = 0;
-		raw->data = git__calloc(1, sizeof(uint8_t));
-		return 0;
-	} else if (!git_oid_cmp(id, &empty_tree)) {
+	if (!git_oid_cmp(id, &empty_tree)) {
 		raw->type = GIT_OBJ_TREE;
 		raw->len = 0;
 		raw->data = git__calloc(1, sizeof(uint8_t));
diff --git a/tests/odb/emptyobjects.c b/tests/odb/emptyobjects.c
index 783d051..61bb2b8 100644
--- a/tests/odb/emptyobjects.c
+++ b/tests/odb/emptyobjects.c
@@ -2,29 +2,33 @@
 #include "odb.h"
 #include "filebuf.h"
 
+#define TEST_REPO_PATH "redundant.git"
+
 git_repository *g_repo;
+git_odb *g_odb;
 
 void test_odb_emptyobjects__initialize(void)
 {
-	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
+	g_repo = cl_git_sandbox_init(TEST_REPO_PATH);
+	cl_git_pass(git_repository_odb(&g_odb, g_repo));
 }
+
 void test_odb_emptyobjects__cleanup(void)
 {
-	git_repository_free(g_repo);
+	git_odb_free(g_odb);
+	cl_git_sandbox_cleanup();
 }
 
-void test_odb_emptyobjects__read(void)
+void test_odb_emptyobjects__blob_notfound(void)
 {
-	git_oid id;
+	git_oid id, written_id;
 	git_blob *blob;
 
 	cl_git_pass(git_oid_fromstr(&id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
-	cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
-	cl_assert_equal_i(GIT_OBJ_BLOB, git_object_type((git_object *) blob));
-	cl_assert(git_blob_rawcontent(blob));
-	cl_assert_equal_s("", git_blob_rawcontent(blob));
-	cl_assert_equal_i(0, git_blob_rawsize(blob));
-	git_blob_free(blob);
+	cl_git_fail_with(GIT_ENOTFOUND, git_blob_lookup(&blob, g_repo, &id));
+
+	cl_git_pass(git_odb_write(&written_id, g_odb, "", 0, GIT_OBJ_BLOB));
+	cl_assert(git_path_exists(TEST_REPO_PATH "/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
 }
 
 void test_odb_emptyobjects__read_tree(void)
@@ -43,15 +47,12 @@ void test_odb_emptyobjects__read_tree(void)
 void test_odb_emptyobjects__read_tree_odb(void)
 {
 	git_oid id;
-	git_odb *odb;
 	git_odb_object *tree_odb;
 
 	cl_git_pass(git_oid_fromstr(&id, "4b825dc642cb6eb9a060e54bf8d69288fbee4904"));
-	cl_git_pass(git_repository_odb(&odb, g_repo));
-	cl_git_pass(git_odb_read(&tree_odb, odb, &id));
+	cl_git_pass(git_odb_read(&tree_odb, g_odb, &id));
 	cl_assert(git_odb_object_data(tree_odb));
 	cl_assert_equal_s("", git_odb_object_data(tree_odb));
 	cl_assert_equal_i(0, git_odb_object_size(tree_odb));
 	git_odb_object_free(tree_odb);
-	git_odb_free(odb);
 }