diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-21 16:07:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-21 16:07:25 +0100 |
commit | 867b10692e5f8a34a82cc0a783bdb63e2b5ff398 (patch) | |
tree | 3ddb2f430ee3c958f0650cb7db6a9df44b1e1361 /storage | |
parent | 0b8b8da617d5a077f282e57d0300dc106a604236 (diff) | |
parent | 790fbdaddc3c9a434f2ad97d9eb56db9b6c99495 (diff) | |
download | go-git-867b10692e5f8a34a82cc0a783bdb63e2b5ff398.tar.gz |
Merge pull request #270 from mcuadros/submodules-init
Submodules init and update
Diffstat (limited to 'storage')
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit.go | 13 | ||||
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit_test.go | 8 | ||||
-rw-r--r-- | storage/filesystem/module.go | 14 | ||||
-rw-r--r-- | storage/filesystem/storage.go | 11 | ||||
-rw-r--r-- | storage/filesystem/storage_test.go | 9 | ||||
-rw-r--r-- | storage/memory/storage.go | 16 | ||||
-rw-r--r-- | storage/storer.go | 26 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 16 |
8 files changed, 108 insertions, 5 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go index 6646e18..b46f827 100644 --- a/storage/filesystem/internal/dotgit/dotgit.go +++ b/storage/filesystem/internal/dotgit/dotgit.go @@ -21,13 +21,13 @@ const ( configPath = "config" indexPath = "index" shallowPath = "shallow" + modulePath = "module" + objectsPath = "objects" + packPath = "pack" + refsPath = "refs" tmpPackedRefsPrefix = "._packed-refs" - objectsPath = "objects" - packPath = "pack" - refsPath = "refs" - packExt = ".pack" idxExt = ".idx" ) @@ -454,6 +454,11 @@ func (d *DotGit) readReferenceFile(refsPath, refFile string) (ref *plumbing.Refe return plumbing.NewReferenceFromStrings(refFile, line), nil } +// Module return a billy.Filesystem poiting to the module folder +func (d *DotGit) Module(name string) billy.Filesystem { + return d.fs.Dir(d.fs.Join(modulePath, name)) +} + func isHex(s string) bool { for _, b := range []byte(s) { if isNum(b) { diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go index 226b299..57dfb53 100644 --- a/storage/filesystem/internal/dotgit/dotgit_test.go +++ b/storage/filesystem/internal/dotgit/dotgit_test.go @@ -434,3 +434,11 @@ func (s *SuiteDotGit) TestObjectNotFound(c *C) { c.Assert(err, NotNil) c.Assert(file, IsNil) } + +func (s *SuiteDotGit) TestSubmodules(c *C) { + fs := fixtures.ByTag("submodule").One().DotGit() + dir := New(fs) + + m := dir.Module("basic") + c.Assert(strings.HasSuffix(m.Base(), ".git/module/basic"), Equals, true) +} diff --git a/storage/filesystem/module.go b/storage/filesystem/module.go new file mode 100644 index 0000000..e8985d8 --- /dev/null +++ b/storage/filesystem/module.go @@ -0,0 +1,14 @@ +package filesystem + +import ( + "srcd.works/go-git.v4/storage" + "srcd.works/go-git.v4/storage/filesystem/internal/dotgit" +) + +type ModuleStorage struct { + dir *dotgit.DotGit +} + +func (s *ModuleStorage) Module(name string) (storage.Storer, error) { + return NewStorage(s.dir.Module(name)) +} diff --git a/storage/filesystem/storage.go b/storage/filesystem/storage.go index 7021d3a..9895507 100644 --- a/storage/filesystem/storage.go +++ b/storage/filesystem/storage.go @@ -11,11 +11,14 @@ import ( // standard git format (this is, the .git directory). Zero values of this type // are not safe to use, see the NewStorage function below. type Storage struct { + fs billy.Filesystem + ObjectStorage ReferenceStorage IndexStorage ShallowStorage ConfigStorage + ModuleStorage } // NewStorage returns a new Storage backed by a given `fs.Filesystem` @@ -27,10 +30,18 @@ func NewStorage(fs billy.Filesystem) (*Storage, error) { } return &Storage{ + fs: fs, + ObjectStorage: o, ReferenceStorage: ReferenceStorage{dir: dir}, IndexStorage: IndexStorage{dir: dir}, ShallowStorage: ShallowStorage{dir: dir}, ConfigStorage: ConfigStorage{dir: dir}, + ModuleStorage: ModuleStorage{dir: dir}, }, nil } + +// Filesystem returns the underlying filesystem +func (s *Storage) Filesystem() billy.Filesystem { + return s.fs +} diff --git a/storage/filesystem/storage_test.go b/storage/filesystem/storage_test.go index e398d22..7300de7 100644 --- a/storage/filesystem/storage_test.go +++ b/storage/filesystem/storage_test.go @@ -6,6 +6,7 @@ import ( "srcd.works/go-git.v4/storage/test" . "gopkg.in/check.v1" + "srcd.works/go-billy.v1/memfs" "srcd.works/go-billy.v1/osfs" ) @@ -23,3 +24,11 @@ func (s *StorageSuite) SetUpTest(c *C) { s.BaseStorageSuite = test.NewBaseStorageSuite(storage) } + +func (s *StorageSuite) TestFilesystem(c *C) { + fs := memfs.New() + storage, err := NewStorage(fs) + c.Assert(err, IsNil) + + c.Assert(storage.Filesystem(), Equals, fs) +} diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 9b55b1f..92aeec9 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -8,6 +8,7 @@ import ( "srcd.works/go-git.v4/plumbing" "srcd.works/go-git.v4/plumbing/format/index" "srcd.works/go-git.v4/plumbing/storer" + "srcd.works/go-git.v4/storage" ) var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type") @@ -22,6 +23,7 @@ type Storage struct { ShallowStorage IndexStorage ReferenceStorage + ModuleStorage } // NewStorage returns a new Storage base on memory @@ -37,6 +39,7 @@ func NewStorage() *Storage { Blobs: make(map[plumbing.Hash]plumbing.EncodedObject, 0), Tags: make(map[plumbing.Hash]plumbing.EncodedObject, 0), }, + ModuleStorage: make(ModuleStorage, 0), } } @@ -232,3 +235,16 @@ func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error { func (s ShallowStorage) Shallow() ([]plumbing.Hash, error) { return s, nil } + +type ModuleStorage map[string]*Storage + +func (s ModuleStorage) Module(name string) (storage.Storer, error) { + if m, ok := s[name]; ok { + return m, nil + } + + m := NewStorage() + s[name] = m + + return m, nil +} diff --git a/storage/storer.go b/storage/storer.go new file mode 100644 index 0000000..d217209 --- /dev/null +++ b/storage/storer.go @@ -0,0 +1,26 @@ +package storage + +import ( + "srcd.works/go-git.v4/config" + "srcd.works/go-git.v4/plumbing/storer" +) + +// Storer is a generic storage of objects, references and any information +// related to a particular repository. The package srcd.works/go-git.v4/storage +// contains two implementation a filesystem base implementation (such as `.git`) +// and a memory implementations being ephemeral +type Storer interface { + storer.EncodedObjectStorer + storer.ReferenceStorer + storer.ShallowStorer + storer.IndexStorer + config.ConfigStorer + ModuleStorer +} + +// ModuleStorer allows interact with the modules' Storers +type ModuleStorer interface { + // Module returns a Storer reprensting a submodule, if not exists returns a + // new empty Storer is returned + Module(name string) (Storer, error) +} diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index e09a673..2a10c78 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -11,6 +11,7 @@ import ( "srcd.works/go-git.v4/plumbing" "srcd.works/go-git.v4/plumbing/format/index" "srcd.works/go-git.v4/plumbing/storer" + "srcd.works/go-git.v4/storage" . "gopkg.in/check.v1" ) @@ -21,6 +22,7 @@ type Storer interface { storer.ShallowStorer storer.IndexStorer config.ConfigStorer + storage.ModuleStorer } type TestObject struct { @@ -321,7 +323,9 @@ func (s *BaseStorageSuite) TestSetConfigAndConfig(c *C) { cfg, err := s.Storer.Config() c.Assert(err, IsNil) - c.Assert(cfg, DeepEquals, expected) + + c.Assert(cfg.Core.IsBare, DeepEquals, expected.Core.IsBare) + c.Assert(cfg.Remotes, DeepEquals, expected.Remotes) } func (s *BaseStorageSuite) TestIndex(c *C) { @@ -353,6 +357,16 @@ func (s *BaseStorageSuite) TestSetConfigInvalid(c *C) { c.Assert(err, NotNil) } +func (s *BaseStorageSuite) TestModule(c *C) { + storer, err := s.Storer.Module("foo") + c.Assert(err, IsNil) + c.Assert(storer, NotNil) + + storer, err = s.Storer.Module("foo") + c.Assert(err, IsNil) + c.Assert(storer, NotNil) +} + func objectEquals(a plumbing.EncodedObject, b plumbing.EncodedObject) error { ha := a.Hash() hb := b.Hash() |