Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 498dbf7

Browse files
committed
storage: git.Storer move to storage.Storer and module handling
1 parent 87a84b1 commit 498dbf7

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

storage/filesystem/internal/dotgit/dotgit.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const (
2020
configPath = "config"
2121
indexPath = "index"
2222
shallowPath = "shallow"
23-
24-
objectsPath = "objects"
25-
packPath = "pack"
26-
refsPath = "refs"
23+
modulePath = "module"
24+
objectsPath = "objects"
25+
packPath = "pack"
26+
refsPath = "refs"
2727

2828
packExt = ".pack"
2929
idxExt = ".idx"
@@ -390,6 +390,11 @@ func (d *DotGit) readReferenceFile(refsPath, refFile string) (ref *plumbing.Refe
390390
return plumbing.NewReferenceFromStrings(refFile, line), nil
391391
}
392392

393+
// Module return a billy.Filesystem poiting to the module folder
394+
func (d *DotGit) Module(name string) billy.Filesystem {
395+
return d.fs.Dir(d.fs.Join(modulePath, name))
396+
}
397+
393398
func isHex(s string) bool {
394399
for _, b := range []byte(s) {
395400
if isNum(b) {

storage/filesystem/module.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package filesystem
2+
3+
import (
4+
"srcd.works/go-git.v4/storage"
5+
"srcd.works/go-git.v4/storage/filesystem/internal/dotgit"
6+
)
7+
8+
type ModuleStorage struct {
9+
dir *dotgit.DotGit
10+
}
11+
12+
func (s *ModuleStorage) Module(name string) (storage.Storer, error) {
13+
return NewStorage(s.dir.Module(name))
14+
}

storage/filesystem/storage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Storage struct {
1616
IndexStorage
1717
ShallowStorage
1818
ConfigStorage
19+
ModuleStorage
1920
}
2021

2122
// NewStorage returns a new Storage backed by a given `fs.Filesystem`
@@ -32,5 +33,6 @@ func NewStorage(fs billy.Filesystem) (*Storage, error) {
3233
IndexStorage: IndexStorage{dir: dir},
3334
ShallowStorage: ShallowStorage{dir: dir},
3435
ConfigStorage: ConfigStorage{dir: dir},
36+
ModuleStorage: ModuleStorage{dir: dir},
3537
}, nil
3638
}

storage/memory/storage.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"srcd.works/go-git.v4/plumbing"
99
"srcd.works/go-git.v4/plumbing/format/index"
1010
"srcd.works/go-git.v4/plumbing/storer"
11+
"srcd.works/go-git.v4/storage"
1112
)
1213

1314
var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type")
@@ -22,6 +23,7 @@ type Storage struct {
2223
ShallowStorage
2324
IndexStorage
2425
ReferenceStorage
26+
ModuleStorage
2527
}
2628

2729
// NewStorage returns a new Storage base on memory
@@ -37,6 +39,7 @@ func NewStorage() *Storage {
3739
Blobs: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
3840
Tags: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
3941
},
42+
ModuleStorage: make(ModuleStorage, 0),
4043
}
4144
}
4245

@@ -227,3 +230,13 @@ func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error {
227230
func (s ShallowStorage) Shallow() ([]plumbing.Hash, error) {
228231
return s, nil
229232
}
233+
234+
type ModuleStorage map[string]*Storage
235+
236+
func (s ModuleStorage) Module(name string) (storage.Storer, error) {
237+
if _, ok := s[name]; !ok {
238+
s[name] = NewStorage()
239+
}
240+
241+
return s[name], nil
242+
}

storage/storer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package storage
2+
3+
import (
4+
"srcd.works/go-git.v4/config"
5+
"srcd.works/go-git.v4/plumbing/storer"
6+
)
7+
8+
// Storer is a generic storage of objects, references and any information
9+
// related to a particular repository. The package srcd.works/go-git.v4/storage
10+
// contains two implementation a filesystem base implementation (such as `.git`)
11+
// and a memory implementations being ephemeral
12+
type Storer interface {
13+
storer.EncodedObjectStorer
14+
storer.ReferenceStorer
15+
storer.ShallowStorer
16+
storer.IndexStorer
17+
config.ConfigStorer
18+
ModuleStorer
19+
}
20+
21+
type ModuleStorer interface {
22+
Module(name string) (Storer, error)
23+
}

storage/test/storage_suite.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"srcd.works/go-git.v4/plumbing"
1212
"srcd.works/go-git.v4/plumbing/format/index"
1313
"srcd.works/go-git.v4/plumbing/storer"
14+
"srcd.works/go-git.v4/storage"
1415

1516
. "gopkg.in/check.v1"
1617
)
@@ -21,6 +22,8 @@ type Storer interface {
2122
storer.ShallowStorer
2223
storer.IndexStorer
2324
config.ConfigStorer
25+
26+
storage.ModuleStorer
2427
}
2528

2629
type TestObject struct {
@@ -326,6 +329,16 @@ func (s *BaseStorageSuite) TestSetConfigInvalid(c *C) {
326329
c.Assert(err, NotNil)
327330
}
328331

332+
func (s *BaseStorageSuite) TestModule(c *C) {
333+
storer, err := s.Storer.Module("foo")
334+
c.Assert(err, IsNil)
335+
c.Assert(storer, NotNil)
336+
337+
storer, err = s.Storer.Module("foo")
338+
c.Assert(err, IsNil)
339+
c.Assert(storer, NotNil)
340+
}
341+
329342
func objectEquals(a plumbing.EncodedObject, b plumbing.EncodedObject) error {
330343
ha := a.Hash()
331344
hb := b.Hash()

0 commit comments

Comments
 (0)