diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-11-04 16:30:42 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-11-07 10:21:27 +0300 |
commit | 7077f6d7432fdddfee5073ab9d90997872c278f6 (patch) | |
tree | c772f88535e879b5fd1248d5215a3c4894d3e918 | |
parent | 061bcd50aa4e5bae26265f8f039e6786b059053d (diff) |
git: Extract function to get object hash by format name
Extract a function that allows the caller to retrieve an object hash by
the format name, e.g. "sha1" or "sha256". This functionality will be
required by the `stats` package in a subsequent commit.
-rw-r--r-- | internal/git/object_id.go | 26 | ||||
-rw-r--r-- | internal/git/object_id_test.go | 32 |
2 files changed, 49 insertions, 9 deletions
diff --git a/internal/git/object_id.go b/internal/git/object_id.go index 2826215d3..d03832283 100644 --- a/internal/git/object_id.go +++ b/internal/git/object_id.go @@ -21,6 +21,7 @@ var ( Hash: sha1.New, EmptyTreeOID: ObjectID("4b825dc642cb6eb9a060e54bf8d69288fbee4904"), ZeroOID: ObjectID("0000000000000000000000000000000000000000"), + Format: "sha1", } // ObjectHashSHA256 is the implementation of an object ID via SHA256. @@ -29,6 +30,7 @@ var ( Hash: sha256.New, EmptyTreeOID: ObjectID("6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321"), ZeroOID: ObjectID("0000000000000000000000000000000000000000000000000000000000000000"), + Format: "sha256", } // ErrInvalidObjectID is returned in case an object ID's string @@ -45,6 +47,20 @@ type ObjectHash struct { EmptyTreeOID ObjectID // ZeroOID is the special value that Git uses to signal a ref or object does not exist ZeroOID ObjectID + // Format is the name of the object hash. + Format string +} + +// ObjectHashByFormat looks up the ObjectHash by its format name. +func ObjectHashByFormat(format string) (ObjectHash, error) { + switch format { + case ObjectHashSHA1.Format: + return ObjectHashSHA1, nil + case ObjectHashSHA256.Format: + return ObjectHashSHA256, nil + default: + return ObjectHash{}, fmt.Errorf("unknown object format: %q", format) + } } // DetectObjectHash detects the object-hash used by the given repository. @@ -60,15 +76,7 @@ func DetectObjectHash(ctx context.Context, repoExecutor RepositoryExecutor) (Obj return ObjectHash{}, fmt.Errorf("reading object format: %w, stderr: %q", err, stderr.String()) } - objectFormat := text.ChompBytes(stdout.Bytes()) - switch objectFormat { - case "sha1": - return ObjectHashSHA1, nil - case "sha256": - return ObjectHashSHA256, nil - default: - return ObjectHash{}, fmt.Errorf("unknown object format: %q", objectFormat) - } + return ObjectHashByFormat(text.ChompBytes(stdout.Bytes())) } // EncodedLen returns the length of the hex-encoded string of a full object ID. diff --git a/internal/git/object_id_test.go b/internal/git/object_id_test.go index 69b900cc9..9711e7e2e 100644 --- a/internal/git/object_id_test.go +++ b/internal/git/object_id_test.go @@ -20,6 +20,38 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" ) +func TestObjectHashByFormat(t *testing.T) { + for _, tc := range []struct { + format string + expectedErr error + expectedObjectHash git.ObjectHash + }{ + { + format: "sha1", + expectedObjectHash: git.ObjectHashSHA1, + }, + { + format: "sha256", + expectedObjectHash: git.ObjectHashSHA256, + }, + { + format: "invalid", + expectedErr: fmt.Errorf("unknown object format: %q", "invalid"), + }, + } { + t.Run(tc.format, func(t *testing.T) { + objectHash, err := git.ObjectHashByFormat(tc.format) + require.Equal(t, tc.expectedErr, err) + + // Function pointers cannot be compared, so we need to unset them. + objectHash.Hash = nil + tc.expectedObjectHash.Hash = nil + + require.Equal(t, tc.expectedObjectHash, objectHash) + }) + } +} + func TestDetectObjectHash(t *testing.T) { cfg := testcfg.Build(t) ctx := testhelper.Context(t) |