Skip to content

Commit fedfac8

Browse files
committed
backend: add standard backend types for filesystem and memory
Add two basic concrete backend types, one based on the filesystem using fileStorer and pack.Storage, and the other based on memory using memoryStorer. This provides a standard set of backends that we can use for reading and writing actual Git repositories or for testing using memory locations. Note that this code cannot live in the storage package, where one would logically expect it to be, because the concrete types implementing it are private to the package.
1 parent 39dda21 commit fedfac8

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

backend.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gitobj
2+
3+
import (
4+
"io"
5+
6+
"github.com/git-lfs/gitobj/pack"
7+
"github.com/git-lfs/gitobj/storage"
8+
)
9+
10+
// NewFilesystemBackend initializes a new filesystem-based backend.
11+
func NewFilesystemBackend(root, tmp string) (storage.Backend, error) {
12+
fsobj := newFileStorer(root, tmp)
13+
packs, err := pack.NewStorage(root)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
return &filesystemBackend{fs: fsobj, packs: packs}, nil
19+
}
20+
21+
// NewMemoryBackend initializes a new memory-based backend.
22+
//
23+
// A value of "nil" is acceptable and indicates that no entries should be added
24+
// to the memory backend at construction time.
25+
func NewMemoryBackend(m map[string]io.ReadWriter) (storage.Backend, error) {
26+
return &memoryBackend{ms: newMemoryStorer(m)}, nil
27+
}
28+
29+
type filesystemBackend struct {
30+
fs *fileStorer
31+
packs *pack.Storage
32+
}
33+
34+
func (b *filesystemBackend) Storage() (storage.Storage, storage.WritableStorage) {
35+
return storage.MultiStorage(b.fs, b.packs), b.fs
36+
}
37+
38+
type memoryBackend struct {
39+
ms *memoryStorer
40+
}
41+
42+
func (b *memoryBackend) Storage() (storage.Storage, storage.WritableStorage) {
43+
return b.ms, b.ms
44+
}

backend_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package gitobj
2+
3+
import (
4+
"bytes"
5+
"encoding/hex"
6+
"io"
7+
"io/ioutil"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestNewMemoryBackend(t *testing.T) {
14+
backend, err := NewMemoryBackend(nil)
15+
assert.NoError(t, err)
16+
17+
ro, rw := backend.Storage()
18+
assert.Equal(t, ro, rw)
19+
assert.NotNil(t, ro.(*memoryStorer))
20+
}
21+
22+
func TestNewMemoryBackendWithReadOnlyData(t *testing.T) {
23+
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24+
oid, err := hex.DecodeString(sha)
25+
26+
assert.Nil(t, err)
27+
28+
m := map[string]io.ReadWriter{
29+
sha: bytes.NewBuffer([]byte{0x1}),
30+
}
31+
32+
backend, err := NewMemoryBackend(m)
33+
assert.NoError(t, err)
34+
35+
ro, _ := backend.Storage()
36+
reader, err := ro.Open(oid)
37+
assert.NoError(t, err)
38+
39+
contents, err := ioutil.ReadAll(reader)
40+
assert.NoError(t, err)
41+
assert.Equal(t, []byte{0x1}, contents)
42+
}
43+
44+
func TestNewMemoryBackendWithWritableData(t *testing.T) {
45+
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
46+
oid, err := hex.DecodeString(sha)
47+
48+
assert.Nil(t, err)
49+
50+
backend, err := NewMemoryBackend(make(map[string]io.ReadWriter))
51+
assert.NoError(t, err)
52+
53+
buf := bytes.NewBuffer([]byte{0x1})
54+
55+
ro, rw := backend.Storage()
56+
rw.Store(oid, buf)
57+
58+
reader, err := ro.Open(oid)
59+
assert.NoError(t, err)
60+
61+
contents, err := ioutil.ReadAll(reader)
62+
assert.NoError(t, err)
63+
assert.Equal(t, []byte{0x1}, contents)
64+
}

0 commit comments

Comments
 (0)