Commit e3bd48a79341eb2ff3b1cc9f2edb8c84a43a8195

Edward Thomson 2014-11-08T16:52:43

Merge pull request #2696 from libgit2/cmn/empty-objects odb: hardcode the empty blob and tree

diff --git a/src/odb.c b/src/odb.c
index a4fc026..2c19c03 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -752,6 +752,28 @@ 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 = NULL;
+		return 0;
+	} else if (!git_oid_cmp(id, &empty_tree)) {
+		raw->type = GIT_OBJ_TREE;
+		raw->len = 0;
+		raw->data = NULL;
+		return 0;
+	} else {
+		return GIT_ENOTFOUND;
+	}
+}
+
 int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
 {
 	size_t i, reads = 0;
@@ -765,7 +787,7 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
 	if (*out != NULL)
 		return 0;
 
-	error = GIT_ENOTFOUND;
+	error = hardcoded_objects(&raw, id);
 
 	for (i = 0; i < db->backends.length && error < 0; ++i) {
 		backend_internal *internal = git_vector_get(&db->backends, i);
diff --git a/tests/odb/emptyobjects.c b/tests/odb/emptyobjects.c
new file mode 100644
index 0000000..d6d832c
--- /dev/null
+++ b/tests/odb/emptyobjects.c
@@ -0,0 +1,39 @@
+#include "clar_libgit2.h"
+#include "odb.h"
+#include "filebuf.h"
+
+git_repository *g_repo;
+
+void test_odb_emptyobjects__initialize(void)
+{
+	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
+}
+void test_odb_emptyobjects__cleanup(void)
+{
+	git_repository_free(g_repo);
+}
+
+void test_odb_emptyobjects__read(void)
+{
+	git_oid 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_equal_i(0, git_blob_rawsize(blob));
+	git_blob_free(blob);
+}
+
+void test_odb_emptyobjects__read_tree(void)
+{
+	git_oid id;
+	git_tree *tree;
+
+	cl_git_pass(git_oid_fromstr(&id, "4b825dc642cb6eb9a060e54bf8d69288fbee4904"));
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
+	cl_assert_equal_i(GIT_OBJ_TREE, git_object_type((git_object *) tree));
+	cl_assert_equal_i(0, git_tree_entrycount(tree));
+	cl_assert_equal_p(NULL, git_tree_entry_byname(tree, "foo"));
+	git_tree_free(tree);
+}