From c4cbb3b16ebbdd26890075a718d59d6e01637d00 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 13 Jun 2017 11:38:14 +0200 Subject: [PATCH] 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`. --- tests/odb/backend/backend_helpers.c | 58 ++++++++++++++++------------- 1 file 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; - return GIT_ENOTFOUND; + *len_p = strlen(obj->content); + *buffer_p = git__strdup(obj->content); + *type_p = GIT_OBJ_BLOB; + + 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)