diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-29 12:56:19 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-08-02 09:25:48 +0300 |
commit | 3d33bcaaf471495788b539d2c01f0e775d99304b (patch) | |
tree | cedacc760591be1d99b3c5780d7a0eb94cdbe30e | |
parent | 867b74cdceabbcbf65eb75025b4f3a10de9a4c55 (diff) |
git: Provide function to get hex-encoded length of object hashes
Many of our tests will require the ability to tell how long the current
object hash is supposed to be in its hex-encoded length. Add a new
function `EncodedLen()` that returns this information.
-rw-r--r-- | internal/git/object_id.go | 5 | ||||
-rw-r--r-- | internal/git/object_id_test.go | 6 |
2 files changed, 11 insertions, 0 deletions
diff --git a/internal/git/object_id.go b/internal/git/object_id.go index 88db74d44..2826215d3 100644 --- a/internal/git/object_id.go +++ b/internal/git/object_id.go @@ -71,6 +71,11 @@ func DetectObjectHash(ctx context.Context, repoExecutor RepositoryExecutor) (Obj } } +// EncodedLen returns the length of the hex-encoded string of a full object ID. +func (h ObjectHash) EncodedLen() int { + return hex.EncodedLen(h.Hash().Size()) +} + // FromHex constructs a new ObjectID from the given hex representation of the object ID. Returns // ErrInvalidObjectID if the given object ID is not valid. func (h ObjectHash) FromHex(hex string) (ObjectID, error) { diff --git a/internal/git/object_id_test.go b/internal/git/object_id_test.go index 44ac98a46..8c8502ef4 100644 --- a/internal/git/object_id_test.go +++ b/internal/git/object_id_test.go @@ -267,6 +267,12 @@ func TestObjectHash_FromHex(t *testing.T) { } } +func TestObjectHash_EncodedLen(t *testing.T) { + t.Parallel() + require.Equal(t, 40, git.ObjectHashSHA1.EncodedLen()) + require.Equal(t, 64, git.ObjectHashSHA256.EncodedLen()) +} + func TestObjectID_Bytes(t *testing.T) { for _, tc := range []struct { desc string |