diff options
author | John Cai <johncai86@gmail.com> | 2023-01-11 23:28:19 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2023-01-12 01:43:18 +0300 |
commit | 68dc97255f0c0e45cee167684f5595a8a39ae004 (patch) | |
tree | bd24c797e591cd472540d5029cf475017d09aff2 | |
parent | 48c5eea7fe10764fbffc02252dcbaba340fb955a (diff) |
catfile: Copy over WriteBlob and WriteBlobs helpersjc-unify-blob-writes
Copy over WriteBlob and WriteBlobs over to the catfile package, as it
cannot use the localrepo helpers MustWriteBlob, MustWriteBlobs due to a
import cycle that would result.
We cannot yet remove the gittest.WriteBlob and gittest.WriteBlobs quite
yet however, as there are still some places that need it in the gittest
package. Namely, TestDeltaIslands, and WriteTree. Once we have
MustWriteTree and MustWriteCommit, then we can also refactor these
functions. At that point, we will be able to delete gittest.WRiteBlob
and gittest.WriteBlobs
-rw-r--r-- | internal/git/catfile/commit_test.go | 2 | ||||
-rw-r--r-- | internal/git/catfile/object_content_reader_test.go | 8 | ||||
-rw-r--r-- | internal/git/catfile/object_info_reader_test.go | 2 | ||||
-rw-r--r-- | internal/git/catfile/testhelper_test.go | 11 | ||||
-rw-r--r-- | internal/git/gittest/objects.go | 5 |
5 files changed, 22 insertions, 6 deletions
diff --git a/internal/git/catfile/commit_test.go b/internal/git/catfile/commit_test.go index 81fceccec..726da6803 100644 --- a/internal/git/catfile/commit_test.go +++ b/internal/git/catfile/commit_test.go @@ -18,7 +18,7 @@ func TestGetCommit(t *testing.T) { cfg, objectReader, _, repoPath := setupObjectReader(t, ctx) - blobID := gittest.WriteBlob(t, cfg, repoPath, []byte("data")) + blobID := writeBlob(t, cfg, repoPath, []byte("data")) treeID := gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ {Path: "file", Mode: "100644", OID: blobID}, }) diff --git a/internal/git/catfile/object_content_reader_test.go b/internal/git/catfile/object_content_reader_test.go index ba8ac1af0..95257ce3b 100644 --- a/internal/git/catfile/object_content_reader_test.go +++ b/internal/git/catfile/object_content_reader_test.go @@ -137,8 +137,8 @@ func TestObjectContentReader_queue(t *testing.T) { SkipCreationViaService: true, }) - foobarBlob := gittest.WriteBlob(t, cfg, repoPath, []byte("foobar")) - barfooBlob := gittest.WriteBlob(t, cfg, repoPath, []byte("barfoo")) + foobarBlob := writeBlob(t, cfg, repoPath, []byte("foobar")) + barfooBlob := writeBlob(t, cfg, repoPath, []byte("barfoo")) t.Run("read single object", func(t *testing.T) { reader, err := newObjectContentReader(ctx, newRepoExecutor(t, cfg, repoProto), nil) @@ -437,8 +437,8 @@ func TestObjectContentReader_replaceRefs(t *testing.T) { SkipCreationViaService: true, }) - originalOID := gittest.WriteBlob(t, cfg, repoPath, []byte("original")) - replacedOID := gittest.WriteBlob(t, cfg, repoPath, []byte("replaced")) + originalOID := writeBlob(t, cfg, repoPath, []byte("original")) + replacedOID := writeBlob(t, cfg, repoPath, []byte("replaced")) gittest.WriteRef(t, cfg, repoPath, git.ReferenceName("refs/replace/"+originalOID.String()), replacedOID) diff --git a/internal/git/catfile/object_info_reader_test.go b/internal/git/catfile/object_info_reader_test.go index 99d088fa3..03f59539c 100644 --- a/internal/git/catfile/object_info_reader_test.go +++ b/internal/git/catfile/object_info_reader_test.go @@ -216,7 +216,7 @@ func TestObjectInfoReader_queue(t *testing.T) { SkipCreationViaService: true, }) - blobOID := gittest.WriteBlob(t, cfg, repoPath, []byte("foobar")) + blobOID := writeBlob(t, cfg, repoPath, []byte("foobar")) blobInfo := ObjectInfo{ Oid: blobOID, Type: "blob", diff --git a/internal/git/catfile/testhelper_test.go b/internal/git/catfile/testhelper_test.go index dd226a468..794ce320b 100644 --- a/internal/git/catfile/testhelper_test.go +++ b/internal/git/catfile/testhelper_test.go @@ -1,6 +1,7 @@ package catfile import ( + "bytes" "context" "io" "strings" @@ -14,6 +15,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/repository" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" @@ -109,3 +111,12 @@ func (o *staticObject) Read(p []byte) (int, error) { func (o *staticObject) WriteTo(w io.Writer) (int64, error) { return io.Copy(w, o.reader) } + +func writeBlob(tb testing.TB, cfg config.Cfg, testRepoPath string, contents []byte) git.ObjectID { + hex := text.ChompBytes(gittest.ExecOpts(tb, cfg, gittest.ExecConfig{Stdin: bytes.NewReader(contents)}, + "-C", testRepoPath, "hash-object", "-w", "--stdin", + )) + oid, err := gittest.DefaultObjectHash.FromHex(hex) + require.NoError(tb, err) + return oid +} diff --git a/internal/git/gittest/objects.go b/internal/git/gittest/objects.go index 7540b83f7..29baf2127 100644 --- a/internal/git/gittest/objects.go +++ b/internal/git/gittest/objects.go @@ -65,6 +65,11 @@ func getGitDirSize(tb testing.TB, repoPath string, subdirs ...string) int64 { return blocks } +// TODO: We can get rid of these functions once there are +// localrepo.MustWriteCommit and localrepo.MustWriteTree, at which point we can +// move TestDeltaIslands into the localrepo package, and get rid of WriteTree. +// After this we can delete WriteBlobs and WriteBlob. + // WriteBlobs writes n distinct blobs into the git repository's object // database. Each object has the current time in nanoseconds as contents. func WriteBlobs(tb testing.TB, cfg config.Cfg, testRepoPath string, n int) []string { |