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 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
diff --git a/tests/odb/backend/backend_helpers.c b/tests/odb/backend/backend_helpers.c
index 2653702..3317136 100644
--- a/tests/odb/backend/backend_helpers.c
+++ b/tests/odb/backend/backend_helpers.c
@@ -4,7 +4,7 @@
static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
{
- const fake_object *obj = fake->objects;
+ const fake_object *obj = fake->objects, *found = NULL;
while (obj && obj->oid) {
git_oid current_oid;
@@ -12,15 +12,18 @@ static int search_object(const fake_object **out, fake_backend *fake, const git_
git_oid_fromstr(¤t_oid, obj->oid);
if (git_oid_ncmp(¤t_oid, oid, len) == 0) {
- if (out)
- *out = obj;
- return 0;
+ if (found)
+ return GIT_EAMBIGUOUS;
+ found = obj;
}
obj++;
}
- return GIT_ENOTFOUND;
+ if (found && out)
+ *out = found;
+
+ return found ? GIT_OK : GIT_ENOTFOUND;
}
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
@@ -40,19 +43,20 @@ static int fake_backend__read(
{
const fake_object *obj;
fake_backend *fake;
+ int error;
fake = (fake_backend *)backend;
fake->read_calls++;
- if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
- *len_p = strlen(obj->content);
- *buffer_p = git__strdup(obj->content);
- *type_p = GIT_OBJ_BLOB;
- return 0;
- }
+ if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
+ return error;
+
+ *len_p = strlen(obj->content);
+ *buffer_p = git__strdup(obj->content);
+ *type_p = GIT_OBJ_BLOB;
- return GIT_ENOTFOUND;
+ return 0;
}
static int fake_backend__read_header(
@@ -61,18 +65,19 @@ static int fake_backend__read_header(
{
const fake_object *obj;
fake_backend *fake;
+ int error;
fake = (fake_backend *)backend;
fake->read_header_calls++;
- if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
- *len_p = strlen(obj->content);
- *type_p = GIT_OBJ_BLOB;
- return 0;
- }
+ if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
+ return error;
- return GIT_ENOTFOUND;
+ *len_p = strlen(obj->content);
+ *type_p = GIT_OBJ_BLOB;
+
+ return 0;
}
static int fake_backend__read_prefix(
@@ -81,20 +86,21 @@ static int fake_backend__read_prefix(
{
const fake_object *obj;
fake_backend *fake;
+ int error;
fake = (fake_backend *)backend;
fake->read_prefix_calls++;
- if (search_object(&obj, fake, short_oid, len) == 0) {
- git_oid_fromstr(out_oid, obj->oid);
- *len_p = strlen(obj->content);
- *buffer_p = git__strdup(obj->content);
- *type_p = GIT_OBJ_BLOB;
- return 0;
- }
+ if ((error = search_object(&obj, fake, short_oid, len)) < 0)
+ return error;
- return GIT_ENOTFOUND;
+ git_oid_fromstr(out_oid, obj->oid);
+ *len_p = strlen(obj->content);
+ *buffer_p = git__strdup(obj->content);
+ *type_p = GIT_OBJ_BLOB;
+
+ return 0;
}
static void fake_backend__free(git_odb_backend *_backend)