diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-22 23:09:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-22 23:09:32 +0100 |
commit | 8a62616f43fe5ea37d41296f40293ff97aa88cfa (patch) | |
tree | edb5683195bb8989814ca610e6745745b361efb9 /src/libgit2/object.c | |
parent | abb0b313172d1b4477fe0c6e88102ce4bb8db90c (diff) | |
parent | b899fda3d88dc92f50e73544fb7524a1c3c70354 (diff) | |
download | libgit2-8a62616f43fe5ea37d41296f40293ff97aa88cfa.tar.gz |
Merge pull request #6549 from libgit2/ethomson/sha256_experimental
sha256: less hardcoded SHA1 types and lengths
Diffstat (limited to 'src/libgit2/object.c')
-rw-r--r-- | src/libgit2/object.c | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/libgit2/object.c b/src/libgit2/object.c index d87d40cf1..b7796f057 100644 --- a/src/libgit2/object.c +++ b/src/libgit2/object.c @@ -181,6 +181,7 @@ int git_object_lookup_prefix( git_object *object = NULL; git_odb *odb = NULL; git_odb_object *odb_obj = NULL; + size_t oid_hexsize; int error = 0; GIT_ASSERT_ARG(repo); @@ -196,10 +197,12 @@ int git_object_lookup_prefix( if (error < 0) return error; - if (len > GIT_OID_SHA1_HEXSIZE) - len = GIT_OID_SHA1_HEXSIZE; + oid_hexsize = git_oid_hexsize(repo->oid_type); - if (len == GIT_OID_SHA1_HEXSIZE) { + if (len > oid_hexsize) + len = oid_hexsize; + + if (len == oid_hexsize) { git_cached_obj *cached = NULL; /* We want to match the full id : we can first look up in the cache, @@ -233,8 +236,9 @@ int git_object_lookup_prefix( error = git_odb_read(&odb_obj, odb, id); } } else { - git_oid short_oid = GIT_OID_SHA1_ZERO; + git_oid short_oid; + git_oid_clear(&short_oid, repo->oid_type); git_oid__cpy_prefix(&short_oid, id, len); /* If len < GIT_OID_SHA1_HEXSIZE (a strict short oid was given), we have @@ -262,7 +266,8 @@ int git_object_lookup_prefix( } int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) { - return git_object_lookup_prefix(object_out, repo, id, GIT_OID_SHA1_HEXSIZE, type); + return git_object_lookup_prefix(object_out, + repo, id, git_oid_hexsize(repo->oid_type), type); } void git_object_free(git_object *object) @@ -503,31 +508,36 @@ cleanup: static int git_object__short_id(git_str *out, const git_object *obj) { git_repository *repo; - int len = GIT_ABBREV_DEFAULT, error; - git_oid id = GIT_OID_SHA1_ZERO; + git_oid id; git_odb *odb; + size_t oid_hexsize; + int len = GIT_ABBREV_DEFAULT, error; GIT_ASSERT_ARG(out); GIT_ASSERT_ARG(obj); repo = git_object_owner(obj); + git_oid_clear(&id, repo->oid_type); + oid_hexsize = git_oid_hexsize(repo->oid_type); + if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0) return error; + if (len < 0 || (size_t)len > oid_hexsize) { + git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len); + return -1; + } + if ((error = git_repository_odb(&odb, repo)) < 0) return error; - while (len < GIT_OID_SHA1_HEXSIZE) { + while ((size_t)len < oid_hexsize) { /* set up short oid */ memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2); if (len & 1) id.id[len / 2] &= 0xf0; -#ifdef GIT_EXPERIMENTAL_SHA256 - id.type = GIT_OID_SHA1; -#endif - error = git_odb_exists_prefix(NULL, odb, &id, len); if (error != GIT_EAMBIGUOUS) break; |