Skip to content

Commit becadaf

Browse files
author
Edward Thomson
committed
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).
1 parent 56bbdf9 commit becadaf

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/odb.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,9 @@ static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_
5454

5555
static git_otype odb_hardcoded_type(const git_oid *id)
5656
{
57-
static git_oid empty_blob = {{ 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b,
58-
0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91 }};
5957
static git_oid empty_tree = {{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
6058
0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};
6159

62-
if (!git_oid_cmp(id, &empty_blob))
63-
return GIT_OBJ_BLOB;
64-
6560
if (!git_oid_cmp(id, &empty_tree))
6661
return GIT_OBJ_TREE;
6762

tests/odb/emptyobjects.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22
#include "odb.h"
33
#include "filebuf.h"
44

5+
#define TEST_REPO_PATH "redundant.git"
6+
57
git_repository *g_repo;
8+
git_odb *g_odb;
69

710
void test_odb_emptyobjects__initialize(void)
811
{
9-
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
12+
g_repo = cl_git_sandbox_init(TEST_REPO_PATH);
13+
cl_git_pass(git_repository_odb(&g_odb, g_repo));
1014
}
15+
1116
void test_odb_emptyobjects__cleanup(void)
1217
{
13-
git_repository_free(g_repo);
18+
git_odb_free(g_odb);
19+
cl_git_sandbox_cleanup();
1420
}
1521

16-
void test_odb_emptyobjects__read(void)
22+
void test_odb_emptyobjects__blob_notfound(void)
1723
{
18-
git_oid id;
24+
git_oid id, written_id;
1925
git_blob *blob;
2026

2127
cl_git_pass(git_oid_fromstr(&id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
22-
cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
23-
cl_assert_equal_i(GIT_OBJ_BLOB, git_object_type((git_object *) blob));
24-
cl_assert(git_blob_rawcontent(blob));
25-
cl_assert_equal_s("", git_blob_rawcontent(blob));
26-
cl_assert_equal_i(0, git_blob_rawsize(blob));
27-
git_blob_free(blob);
28+
cl_git_fail_with(GIT_ENOTFOUND, git_blob_lookup(&blob, g_repo, &id));
29+
30+
cl_git_pass(git_odb_write(&written_id, g_odb, "", 0, GIT_OBJ_BLOB));
31+
cl_assert(git_path_exists(TEST_REPO_PATH "/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
2832
}
2933

3034
void test_odb_emptyobjects__read_tree(void)
@@ -43,15 +47,12 @@ void test_odb_emptyobjects__read_tree(void)
4347
void test_odb_emptyobjects__read_tree_odb(void)
4448
{
4549
git_oid id;
46-
git_odb *odb;
4750
git_odb_object *tree_odb;
4851

4952
cl_git_pass(git_oid_fromstr(&id, "4b825dc642cb6eb9a060e54bf8d69288fbee4904"));
50-
cl_git_pass(git_repository_odb(&odb, g_repo));
51-
cl_git_pass(git_odb_read(&tree_odb, odb, &id));
53+
cl_git_pass(git_odb_read(&tree_odb, g_odb, &id));
5254
cl_assert(git_odb_object_data(tree_odb));
5355
cl_assert_equal_s("", git_odb_object_data(tree_odb));
5456
cl_assert_equal_i(0, git_odb_object_size(tree_odb));
5557
git_odb_object_free(tree_odb);
56-
git_odb_free(odb);
5758
}

0 commit comments

Comments
 (0)