aboutsummaryrefslogtreecommitdiff
path: root/objectstore/loose
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 13:38:02 +0800
committerGravatar Runxi Yu2026-02-21 14:28:15 +0800
commit94482cb2c97aa215f83940643c5d4c0933727dcb (patch)
treebee22fa113542abd1b863ee251fdcf0f9bd409b5 /objectstore/loose
parentdiff: Add package-level doc comment (diff)
signatureNo signature
*: Modernize and lint; add CI v0.1.17
Diffstat (limited to 'objectstore/loose')
-rw-r--r--objectstore/loose/read_test.go7
-rw-r--r--objectstore/loose/write_test.go28
-rw-r--r--objectstore/loose/write_writer.go84
3 files changed, 69 insertions, 50 deletions
diff --git a/objectstore/loose/read_test.go b/objectstore/loose/read_test.go
index d125629a..d8166c9e 100644
--- a/objectstore/loose/read_test.go
+++ b/objectstore/loose/read_test.go
@@ -14,7 +14,8 @@ import (
)
func TestLooseStoreReadAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
blobID := testRepo.HashObject(t, "blob", []byte("blob body\n"))
_, treeID, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
@@ -93,7 +94,8 @@ func TestLooseStoreReadAgainstGit(t *testing.T) {
}
func TestLooseStoreErrors(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
store := openLooseStore(t, testRepo.Dir(), algo)
@@ -136,6 +138,7 @@ func TestLooseStoreErrors(t *testing.T) {
}
func TestLooseStoreNewValidation(t *testing.T) {
+ t.Parallel()
root, err := os.OpenRoot(t.TempDir())
if err != nil {
t.Fatalf("OpenRoot: %v", err)
diff --git a/objectstore/loose/write_test.go b/objectstore/loose/write_test.go
index b9a318d2..411868a6 100644
--- a/objectstore/loose/write_test.go
+++ b/objectstore/loose/write_test.go
@@ -12,7 +12,8 @@ import (
)
func TestLooseStoreWriteWriterContentAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
store := openLooseStore(t, testRepo.Dir(), algo)
@@ -68,7 +69,8 @@ func TestLooseStoreWriteWriterContentAgainstGit(t *testing.T) {
}
func TestLooseStoreWriteWriterFullAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
store := openLooseStore(t, testRepo.Dir(), algo)
@@ -108,11 +110,13 @@ func TestLooseStoreWriteWriterFullAgainstGit(t *testing.T) {
}
func TestLooseStoreWriterValidationErrors(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
- testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
- store := openLooseStore(t, testRepo.Dir(), algo)
-
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
t.Run("content overflow", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterContent(objecttype.TypeBlob, 1)
if err != nil {
t.Fatalf("WriteWriterContent: %v", err)
@@ -127,6 +131,10 @@ func TestLooseStoreWriterValidationErrors(t *testing.T) {
})
t.Run("content short", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterContent(objecttype.TypeBlob, 5)
if err != nil {
t.Fatalf("WriteWriterContent: %v", err)
@@ -143,6 +151,10 @@ func TestLooseStoreWriterValidationErrors(t *testing.T) {
})
t.Run("full malformed header", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterFull()
if err != nil {
t.Fatalf("WriteWriterFull: %v", err)
@@ -159,6 +171,10 @@ func TestLooseStoreWriterValidationErrors(t *testing.T) {
})
t.Run("full size mismatch", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterFull()
if err != nil {
t.Fatalf("WriteWriterFull: %v", err)
diff --git a/objectstore/loose/write_writer.go b/objectstore/loose/write_writer.go
index abbbae31..e8f03f19 100644
--- a/objectstore/loose/write_writer.go
+++ b/objectstore/loose/write_writer.go
@@ -137,48 +137,6 @@ func (writer *streamWriter) Write(src []byte) (int, error) {
return len(src), nil
}
-// acceptFull validates and accounts raw full-object input.
-func (writer *streamWriter) acceptFull(src []byte) error {
- if !writer.headerDone {
- if nul := bytes.IndexByte(src, 0); nul >= 0 {
- headerChunkLen := nul + 1
- writer.headerBuf = append(writer.headerBuf, src[:headerChunkLen]...)
- _, size, _, ok := objectheader.Parse(writer.headerBuf)
- if !ok {
- return errors.New("objectstore/loose: malformed object header")
- }
- writer.headerDone = true
- writer.expectedContentLeft = size
- return writer.acceptContent(int64(len(src) - headerChunkLen))
- }
-
- writer.headerBuf = append(writer.headerBuf, src...)
- return nil
- }
-
- return writer.acceptContent(int64(len(src)))
-}
-
-// acceptContent validates and accounts content byte counts.
-func (writer *streamWriter) acceptContent(n int64) error {
- if n > writer.expectedContentLeft {
- return errors.New("objectstore/loose: object content exceeds declared size")
- }
- writer.expectedContentLeft -= n
- return nil
-}
-
-// writeRawChunk forwards raw bytes to the hash and deflate pipeline.
-func (writer *streamWriter) writeRawChunk(src []byte) error {
- if _, err := writer.hash.Write(src); err != nil {
- return err
- }
- if _, err := writer.zw.Write(src); err != nil {
- return err
- }
- return nil
-}
-
// Close flushes and closes the underlying zlib stream and temp file.
// It is safe to call multiple times.
func (writer *streamWriter) Close() error {
@@ -263,6 +221,48 @@ func (writer *streamWriter) Finalize() (objectid.ObjectID, error) {
return id, nil
}
+// acceptFull validates and accounts raw full-object input.
+func (writer *streamWriter) acceptFull(src []byte) error {
+ if !writer.headerDone {
+ if nul := bytes.IndexByte(src, 0); nul >= 0 {
+ headerChunkLen := nul + 1
+ writer.headerBuf = append(writer.headerBuf, src[:headerChunkLen]...)
+ _, size, _, ok := objectheader.Parse(writer.headerBuf)
+ if !ok {
+ return errors.New("objectstore/loose: malformed object header")
+ }
+ writer.headerDone = true
+ writer.expectedContentLeft = size
+ return writer.acceptContent(int64(len(src) - headerChunkLen))
+ }
+
+ writer.headerBuf = append(writer.headerBuf, src...)
+ return nil
+ }
+
+ return writer.acceptContent(int64(len(src)))
+}
+
+// acceptContent validates and accounts content byte counts.
+func (writer *streamWriter) acceptContent(n int64) error {
+ if n > writer.expectedContentLeft {
+ return errors.New("objectstore/loose: object content exceeds declared size")
+ }
+ writer.expectedContentLeft -= n
+ return nil
+}
+
+// writeRawChunk forwards raw bytes to the hash and deflate pipeline.
+func (writer *streamWriter) writeRawChunk(src []byte) error {
+ if _, err := writer.hash.Write(src); err != nil {
+ return err
+ }
+ if _, err := writer.zw.Write(src); err != nil {
+ return err
+ }
+ return nil
+}
+
// createTempObjectFile creates a unique temporary object file within dir.
// The returned path is relative to the objects root.
func (store *Store) createTempObjectFile(dir string) (string, *os.File, error) {