diff options
author | John Cai <jcai@gitlab.com> | 2023-03-26 03:55:18 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2023-05-08 17:44:49 +0300 |
commit | fa39ca0245dd73799d0337c125204d4188e9f4fd (patch) | |
tree | f48c4ecc2a691d746f303fbdac1fef8152a394d3 | |
parent | f1eb814dc4da1d6f97e399aa9b30ba9be7099cc4 (diff) |
localrepo: Add ReadObjectInfo helper
Sometimes it's useful to quickly look up an object's info based on a
revision (which can include a path).
-rw-r--r-- | internal/git/localrepo/objects.go | 19 | ||||
-rw-r--r-- | internal/git/localrepo/objects_test.go | 43 |
2 files changed, 62 insertions, 0 deletions
diff --git a/internal/git/localrepo/objects.go b/internal/git/localrepo/objects.go index c9fb2345b..7586886af 100644 --- a/internal/git/localrepo/objects.go +++ b/internal/git/localrepo/objects.go @@ -185,6 +185,25 @@ type InvalidObjectError string func (err InvalidObjectError) Error() string { return fmt.Sprintf("invalid object %q", string(err)) } +// ReadObjectInfo attempts to read the object info based on a revision. +func (repo *Repo) ReadObjectInfo(ctx context.Context, rev git.Revision) (*catfile.ObjectInfo, error) { + infoReader, cleanup, err := repo.catfileCache.ObjectInfoReader(ctx, repo) + if err != nil { + return nil, fmt.Errorf("getting object info reader: %w", err) + } + defer cleanup() + + objectInfo, err := infoReader.Info(ctx, rev) + if err != nil { + if catfile.IsNotFound(err) { + return nil, InvalidObjectError(rev) + } + return nil, fmt.Errorf("getting object info %w", err) + } + + return objectInfo, nil +} + // ReadObject reads an object from the repository's object database. InvalidObjectError // is returned if the oid does not refer to a valid object. func (repo *Repo) ReadObject(ctx context.Context, oid git.ObjectID) ([]byte, error) { diff --git a/internal/git/localrepo/objects_test.go b/internal/git/localrepo/objects_test.go index c57c66c1b..107ab1f92 100644 --- a/internal/git/localrepo/objects_test.go +++ b/internal/git/localrepo/objects_test.go @@ -280,6 +280,49 @@ func testRepoReadObject(t *testing.T, ctx context.Context) { } } +func TestRepoReadObjectInfo(t *testing.T) { + t.Parallel() + + ctx := testhelper.Context(t) + cfg, repo, repoPath := setupRepo(t) + blobID := gittest.WriteBlob(t, cfg, repoPath, []byte("content")) + objectHash, err := repo.ObjectHash(ctx) + require.NoError(t, err) + + for _, tc := range []struct { + desc string + oid git.ObjectID + content string + expectedErr error + expectedObjectInfo catfile.ObjectInfo + }{ + { + desc: "missing object", + oid: git.ObjectID("abcdefg"), + expectedErr: InvalidObjectError("abcdefg"), + }, + { + desc: "valid object", + oid: blobID, + content: "content", + expectedObjectInfo: catfile.ObjectInfo{ + Oid: blobID, + Type: "blob", + Size: 7, + Format: objectHash.Format, + }, + }, + } { + t.Run(tc.desc, func(t *testing.T) { + info, err := repo.ReadObjectInfo(ctx, git.Revision(tc.oid)) + require.Equal(t, tc.expectedErr, err) + if tc.expectedErr == nil { + require.Equal(t, tc.expectedObjectInfo, *info) + } + }) + } +} + func TestRepo_ReadObject_catfileCount(t *testing.T) { t.Parallel() |