Skip to content

Commit c4cbb3b

Browse files
committed
tests: odb: have the fake backend detect ambiguous prefixes
In order to be able to test the ODB prefix functions, we need to be able to detect ambiguous prefixes in case multiple objects with the same prefix exist in the fake ODB. Extend `search_object` to detect ambiguous queries and have callers return its error code instead of always returning `GIT_ENOTFOUND`.
1 parent 9517029 commit c4cbb3b

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

tests/odb/backend/backend_helpers.c

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44

55
static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
66
{
7-
const fake_object *obj = fake->objects;
7+
const fake_object *obj = fake->objects, *found = NULL;
88

99
while (obj && obj->oid) {
1010
git_oid current_oid;
1111

1212
git_oid_fromstr(&current_oid, obj->oid);
1313

1414
if (git_oid_ncmp(&current_oid, oid, len) == 0) {
15-
if (out)
16-
*out = obj;
17-
return 0;
15+
if (found)
16+
return GIT_EAMBIGUOUS;
17+
found = obj;
1818
}
1919

2020
obj++;
2121
}
2222

23-
return GIT_ENOTFOUND;
23+
if (found && out)
24+
*out = found;
25+
26+
return found ? GIT_OK : GIT_ENOTFOUND;
2427
}
2528

2629
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
@@ -40,19 +43,20 @@ static int fake_backend__read(
4043
{
4144
const fake_object *obj;
4245
fake_backend *fake;
46+
int error;
4347

4448
fake = (fake_backend *)backend;
4549

4650
fake->read_calls++;
4751

48-
if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
49-
*len_p = strlen(obj->content);
50-
*buffer_p = git__strdup(obj->content);
51-
*type_p = GIT_OBJ_BLOB;
52-
return 0;
53-
}
52+
if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
53+
return error;
54+
55+
*len_p = strlen(obj->content);
56+
*buffer_p = git__strdup(obj->content);
57+
*type_p = GIT_OBJ_BLOB;
5458

55-
return GIT_ENOTFOUND;
59+
return 0;
5660
}
5761

5862
static int fake_backend__read_header(
@@ -61,18 +65,19 @@ static int fake_backend__read_header(
6165
{
6266
const fake_object *obj;
6367
fake_backend *fake;
68+
int error;
6469

6570
fake = (fake_backend *)backend;
6671

6772
fake->read_header_calls++;
6873

69-
if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
70-
*len_p = strlen(obj->content);
71-
*type_p = GIT_OBJ_BLOB;
72-
return 0;
73-
}
74+
if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
75+
return error;
7476

75-
return GIT_ENOTFOUND;
77+
*len_p = strlen(obj->content);
78+
*type_p = GIT_OBJ_BLOB;
79+
80+
return 0;
7681
}
7782

7883
static int fake_backend__read_prefix(
@@ -81,20 +86,21 @@ static int fake_backend__read_prefix(
8186
{
8287
const fake_object *obj;
8388
fake_backend *fake;
89+
int error;
8490

8591
fake = (fake_backend *)backend;
8692

8793
fake->read_prefix_calls++;
8894

89-
if (search_object(&obj, fake, short_oid, len) == 0) {
90-
git_oid_fromstr(out_oid, obj->oid);
91-
*len_p = strlen(obj->content);
92-
*buffer_p = git__strdup(obj->content);
93-
*type_p = GIT_OBJ_BLOB;
94-
return 0;
95-
}
95+
if ((error = search_object(&obj, fake, short_oid, len)) < 0)
96+
return error;
9697

97-
return GIT_ENOTFOUND;
98+
git_oid_fromstr(out_oid, obj->oid);
99+
*len_p = strlen(obj->content);
100+
*buffer_p = git__strdup(obj->content);
101+
*type_p = GIT_OBJ_BLOB;
102+
103+
return 0;
98104
}
99105

100106
static void fake_backend__free(git_odb_backend *_backend)

0 commit comments

Comments
 (0)