diff options
author | Patrick Steinhardt <ps@pks.im> | 2017-06-13 11:38:14 +0200 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2017-06-13 11:40:05 +0200 |
commit | c4cbb3b16ebbdd26890075a718d59d6e01637d00 (patch) | |
tree | d2fe32c48b9f169a6e9a2aafc9edc325f14a55c3 | |
parent | 95170294a17083f89e7c30cd1d3f6b888d951c5a (diff) | |
download | libgit2-c4cbb3b16ebbdd26890075a718d59d6e01637d00.tar.gz |
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`.
-rw-r--r-- | tests/odb/backend/backend_helpers.c | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/tests/odb/backend/backend_helpers.c b/tests/odb/backend/backend_helpers.c index 26537029d..331713680 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) |