diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-07 20:29:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-07 20:29:58 +0100 |
commit | 0ff9ef2b44c53e557c78bde0fd9c29847e5f0e23 (patch) | |
tree | b9c7485fe99e6e89fa736ceb0223aeb2ecddb77c /storage | |
parent | f6ed7424cbf33c7013332d7e95b4262a4bc4a523 (diff) | |
download | go-git-0ff9ef2b44c53e557c78bde0fd9c29847e5f0e23.tar.gz |
global storage interface refactor (#112)
* core: ObjectStorage, ReferenceStorage renamed to ObjectStorer and
ReferenceStorer
* rebase
* general, changes request by @alcortes
* general, changes request by @alcortes
Diffstat (limited to 'storage')
-rw-r--r-- | storage/filesystem/config.go | 124 | ||||
-rw-r--r-- | storage/filesystem/config_test.go | 26 | ||||
-rw-r--r-- | storage/filesystem/object.go | 41 | ||||
-rw-r--r-- | storage/filesystem/object_test.go | 8 | ||||
-rw-r--r-- | storage/filesystem/reference.go | 6 | ||||
-rw-r--r-- | storage/filesystem/storage.go | 43 | ||||
-rw-r--r-- | storage/filesystem/storage_test.go | 16 | ||||
-rw-r--r-- | storage/memory/storage.go | 127 | ||||
-rw-r--r-- | storage/memory/storage_test.go | 31 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 163 |
10 files changed, 209 insertions, 376 deletions
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go index b32265f..4b47937 100644 --- a/storage/filesystem/config.go +++ b/storage/filesystem/config.go @@ -18,70 +18,24 @@ type ConfigStorage struct { dir *dotgit.DotGit } -func (c *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) { - cfg, err := c.read() - if err != nil { - return nil, err - } - - s := cfg.Section(remoteSection) - if !s.HasSubsection(name) { - return nil, config.ErrRemoteConfigNotFound - } +func (c *ConfigStorage) Config() (*config.Config, error) { + cfg := config.NewConfig() - return parseRemote(s.Subsection(name)), nil -} - -func (c *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) { - cfg, err := c.read() + ini, err := c.unmarshal() if err != nil { return nil, err } - remotes := []*config.RemoteConfig{} - sect := cfg.Section(remoteSection) + sect := ini.Section(remoteSection) for _, s := range sect.Subsections { - remotes = append(remotes, parseRemote(s)) - } - - return remotes, nil -} - -func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error { - if err := r.Validate(); err != nil { - return err + r := c.unmarshalRemote(s) + cfg.Remotes[r.Name] = r } - cfg, err := c.read() - if err != nil { - return err - } - - s := cfg.Section(remoteSection).Subsection(r.Name) - s.Name = r.Name - if r.URL != "" { - s.SetOption(urlKey, r.URL) - } - s.RemoveOption(fetchKey) - for _, rs := range r.Fetch { - s.AddOption(fetchKey, rs.String()) - } - - return c.write(cfg) -} - -func (c *ConfigStorage) DeleteRemote(name string) error { - cfg, err := c.read() - if err != nil { - return err - } - - cfg = cfg.RemoveSubsection(remoteSection, name) - - return c.write(cfg) + return cfg, nil } -func (c *ConfigStorage) read() (*gitconfig.Config, error) { +func (c *ConfigStorage) unmarshal() (*gitconfig.Config, error) { cfg := gitconfig.New() f, err := c.dir.Config() @@ -103,23 +57,7 @@ func (c *ConfigStorage) read() (*gitconfig.Config, error) { return cfg, nil } -func (c *ConfigStorage) write(cfg *gitconfig.Config) error { - f, err := c.dir.ConfigWriter() - if err != nil { - return err - } - - e := gitconfig.NewEncoder(f) - err = e.Encode(cfg) - if err != nil { - f.Close() - return err - } - - return f.Close() -} - -func parseRemote(s *gitconfig.Subsection) *config.RemoteConfig { +func (c *ConfigStorage) unmarshalRemote(s *gitconfig.Subsection) *config.RemoteConfig { fetch := []config.RefSpec{} for _, f := range s.Options.GetAll(fetchKey) { rs := config.RefSpec(f) @@ -134,3 +72,47 @@ func parseRemote(s *gitconfig.Subsection) *config.RemoteConfig { Fetch: fetch, } } + +func (c *ConfigStorage) SetConfig(cfg *config.Config) error { + if err := cfg.Validate(); err != nil { + return err + } + + ini, err := c.unmarshal() + if err != nil { + return err + } + + s := ini.Section(remoteSection) + s.Subsections = make(gitconfig.Subsections, len(cfg.Remotes)) + + var i int + for _, r := range cfg.Remotes { + s.Subsections[i] = c.marshalRemote(r) + i++ + } + + return c.marshal(ini) +} + +func (c *ConfigStorage) marshal(ini *gitconfig.Config) error { + f, err := c.dir.ConfigWriter() + if err != nil { + return err + } + + defer f.Close() + + e := gitconfig.NewEncoder(f) + return e.Encode(ini) +} + +func (c *ConfigStorage) marshalRemote(r *config.RemoteConfig) *gitconfig.Subsection { + s := &gitconfig.Subsection{Name: r.Name} + s.AddOption(urlKey, r.URL) + for _, rs := range r.Fetch { + s.AddOption(fetchKey, rs.String()) + } + + return s +} diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go index 2eb0ab9..b86eaee 100644 --- a/storage/filesystem/config_test.go +++ b/storage/filesystem/config_test.go @@ -4,7 +4,6 @@ import ( "io/ioutil" stdos "os" - "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/fixtures" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" "gopkg.in/src-d/go-git.v4/utils/fs/os" @@ -29,27 +28,20 @@ func (s *ConfigSuite) SetUpTest(c *C) { s.path = tmp } -func (s *ConfigSuite) TestSetRemote(c *C) { - cfg := &ConfigStorage{s.dir} - err := cfg.SetRemote(&config.RemoteConfig{Name: "foo", URL: "foo"}) - c.Assert(err, IsNil) - - remote, err := cfg.Remote("foo") - c.Assert(err, IsNil) - c.Assert(remote.Name, Equals, "foo") -} - func (s *ConfigSuite) TestRemotes(c *C) { dir := dotgit.New(fixtures.Basic().ByTag(".git").One().DotGit()) - cfg := &ConfigStorage{dir} + storer := &ConfigStorage{dir} - remotes, err := cfg.Remotes() + cfg, err := storer.Config() c.Assert(err, IsNil) + + remotes := cfg.Remotes c.Assert(remotes, HasLen, 1) - c.Assert(remotes[0].Name, Equals, "origin") - c.Assert(remotes[0].URL, Equals, "https://github.com/git-fixtures/basic") - c.Assert(remotes[0].Fetch, HasLen, 1) - c.Assert(remotes[0].Fetch[0].String(), Equals, "+refs/heads/*:refs/remotes/origin/*") + remote := remotes["origin"] + c.Assert(remote.Name, Equals, "origin") + c.Assert(remote.URL, Equals, "https://github.com/git-fixtures/basic") + c.Assert(remote.Fetch, HasLen, 1) + c.Assert(remote.Fetch[0].String(), Equals, "+refs/heads/*:refs/remotes/origin/*") } func (s *ConfigSuite) TearDownTest(c *C) { diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index 6dbeae9..f780183 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -13,25 +13,17 @@ import ( "gopkg.in/src-d/go-git.v4/utils/fs" ) -// ObjectStorage is an implementation of core.ObjectStorage that stores -// data on disk in the standard git format (this is, the .git directory). -// -// Zero values of this type are not safe to use, see the New function below. -// -// Currently only reads are supported, no writting. -// -// Also values from this type are not yet able to track changes on disk, this is, -// Gitdir values will get outdated as soon as repositories change on disk. type ObjectStorage struct { dir *dotgit.DotGit index map[core.Hash]index } -func newObjectStorage(dir *dotgit.DotGit) (*ObjectStorage, error) { - s := &ObjectStorage{ +func newObjectStorage(dir *dotgit.DotGit) (ObjectStorage, error) { + s := ObjectStorage{ dir: dir, index: make(map[core.Hash]index, 0), } + return s, s.loadIdxFiles() } @@ -64,8 +56,7 @@ func (s *ObjectStorage) NewObject() core.Object { return &core.MemoryObject{} } -// Writer method not supported on Memory storage -func (s *ObjectStorage) Writer() (io.WriteCloser, error) { +func (s *ObjectStorage) PackfileWriter() (io.WriteCloser, error) { w, err := s.dir.NewObjectPack() if err != nil { return nil, err @@ -82,7 +73,7 @@ func (s *ObjectStorage) Writer() (io.WriteCloser, error) { } // Set adds a new object to the storage. -func (s *ObjectStorage) Set(o core.Object) (core.Hash, error) { +func (s *ObjectStorage) SetObject(o core.Object) (core.Hash, error) { if o.Type() == core.OFSDeltaObject || o.Type() == core.REFDeltaObject { return core.ZeroHash, core.ErrInvalidType } @@ -114,7 +105,7 @@ func (s *ObjectStorage) Set(o core.Object) (core.Hash, error) { // Get returns the object with the given hash, by searching for it in // the packfile and the git object directories. -func (s *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) { +func (s *ObjectStorage) Object(t core.ObjectType, h core.Hash) (core.Object, error) { obj, err := s.getFromUnpacked(h) if err == core.ErrObjectNotFound { obj, err = s.getFromPackfile(h) @@ -183,7 +174,7 @@ func (s *ObjectStorage) getFromPackfile(h core.Hash) (core.Object, error) { defer f.Close() p := packfile.NewScanner(f) - d, err := packfile.NewDecoder(p, memory.NewStorage().ObjectStorage()) + d, err := packfile.NewDecoder(p, memory.NewStorage()) if err != nil { return nil, err } @@ -204,7 +195,7 @@ func (s *ObjectStorage) findObjectInPackfile(h core.Hash) (core.Hash, int64) { // Iter returns an iterator for all the objects in the packfile with the // given type. -func (s *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { +func (s *ObjectStorage) IterObjects(t core.ObjectType) (core.ObjectIter, error) { objects, err := s.dir.Objects() if err != nil { return nil, err @@ -251,20 +242,6 @@ func (s *ObjectStorage) buildPackfileIters( return iters, nil } -// Begin opens a new transaction. However, this implementation is not -// transactional, so Commit and Rollback have no effect. -func (o *ObjectStorage) Begin() core.TxObjectStorage { - return o -} - -func (tx *ObjectStorage) Commit() error { - return nil -} - -func (tx *ObjectStorage) Rollback() error { - return nil -} - type index map[core.Hash]int64 func (i index) Decode(r io.Reader) error { @@ -299,7 +276,7 @@ func newPackfileIter(f fs.File, t core.ObjectType, seen map[core.Hash]bool) (cor return nil, err } - d, err := packfile.NewDecoder(s, memory.NewStorage().ObjectStorage()) + d, err := packfile.NewDecoder(s, memory.NewStorage()) if err != nil { return nil, err } diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 0136e54..38e4b6d 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -20,7 +20,7 @@ func (s *FsSuite) TestGetFromObjectFile(c *C) { c.Assert(err, IsNil) expected := core.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e") - obj, err := o.Get(core.AnyObject, expected) + obj, err := o.Object(core.AnyObject, expected) c.Assert(err, IsNil) c.Assert(obj.Hash(), Equals, expected) } @@ -32,7 +32,7 @@ func (s *FsSuite) TestGetFromPackfile(c *C) { c.Assert(err, IsNil) expected := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") - obj, err := o.Get(core.AnyObject, expected) + obj, err := o.Object(core.AnyObject, expected) c.Assert(err, IsNil) c.Assert(obj.Hash(), Equals, expected) }) @@ -60,7 +60,7 @@ func (s *FsSuite) TestIter(c *C) { o, err := newObjectStorage(dotgit.New(fs)) c.Assert(err, IsNil) - iter, err := o.Iter(core.AnyObject) + iter, err := o.IterObjects(core.AnyObject) c.Assert(err, IsNil) var count int32 @@ -80,7 +80,7 @@ func (s *FsSuite) TestIterWithType(c *C) { o, err := newObjectStorage(dotgit.New(fs)) c.Assert(err, IsNil) - iter, err := o.Iter(core.CommitObject) + iter, err := o.IterObjects(core.CommitObject) c.Assert(err, IsNil) err = iter.ForEach(func(o core.Object) error { diff --git a/storage/filesystem/reference.go b/storage/filesystem/reference.go index b2aa6d9..0015859 100644 --- a/storage/filesystem/reference.go +++ b/storage/filesystem/reference.go @@ -9,15 +9,15 @@ type ReferenceStorage struct { dir *dotgit.DotGit } -func (r *ReferenceStorage) Set(ref *core.Reference) error { +func (r *ReferenceStorage) SetReference(ref *core.Reference) error { return r.dir.SetRef(ref) } -func (r *ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { +func (r *ReferenceStorage) Reference(n core.ReferenceName) (*core.Reference, error) { return r.dir.Ref(n) } -func (r *ReferenceStorage) Iter() (core.ReferenceIter, error) { +func (r *ReferenceStorage) IterReferences() (core.ReferenceIter, error) { refs, err := r.dir.Refs() if err != nil { return nil, err diff --git a/storage/filesystem/storage.go b/storage/filesystem/storage.go index d39d6e4..7e05cd3 100644 --- a/storage/filesystem/storage.go +++ b/storage/filesystem/storage.go @@ -2,21 +2,20 @@ package filesystem import ( - "gopkg.in/src-d/go-git.v4/config" - "gopkg.in/src-d/go-git.v4/core" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" "gopkg.in/src-d/go-git.v4/utils/fs" ) +// Storage is an implementation of git.Storer that stores data on disk in the +// 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 { - dir *dotgit.DotGit - fs fs.Filesystem - - o *ObjectStorage - r *ReferenceStorage - c *ConfigStorage + ObjectStorage + ReferenceStorage + ConfigStorage } +// NewStorage returns a new Storage backed by a given `fs.Filesystem` func NewStorage(fs fs.Filesystem) (*Storage, error) { dir := dotgit.New(fs) o, err := newObjectStorage(dir) @@ -24,27 +23,9 @@ func NewStorage(fs fs.Filesystem) (*Storage, error) { return nil, err } - return &Storage{dir: dir, fs: fs, o: o}, nil -} - -func (s *Storage) ObjectStorage() core.ObjectStorage { - return s.o -} - -func (s *Storage) ReferenceStorage() core.ReferenceStorage { - if s.r != nil { - return s.r - } - - s.r = &ReferenceStorage{dir: s.dir} - return s.r -} - -func (s *Storage) ConfigStorage() config.ConfigStorage { - if s.c != nil { - return s.c - } - - s.c = &ConfigStorage{dir: s.dir} - return s.c + return &Storage{ + ObjectStorage: o, + ReferenceStorage: ReferenceStorage{dir: dir}, + ConfigStorage: ConfigStorage{dir: dir}, + }, nil } diff --git a/storage/filesystem/storage_test.go b/storage/filesystem/storage_test.go index f55452d..c24d5d5 100644 --- a/storage/filesystem/storage_test.go +++ b/storage/filesystem/storage_test.go @@ -18,20 +18,8 @@ type StorageSuite struct { var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { - path := c.MkDir() - storage, err := NewStorage(os.New(path)) + storage, err := NewStorage(os.New(c.MkDir())) c.Assert(err, IsNil) - s.BaseStorageSuite = test.NewBaseStorageSuite( - storage.ObjectStorage(), - storage.ReferenceStorage(), - storage.ConfigStorage(), - ) -} - -func (s *StorageSuite) TestTxObjectStorageSetAndCommit(c *C) { - c.Skip("tx not supported") -} -func (s *StorageSuite) TestTxObjectStorageSetAndRollback(c *C) { - c.Skip("tx not supported") + s.BaseStorageSuite = test.NewBaseStorageSuite(storage) } diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 97e87c8..8141cfc 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -10,96 +10,52 @@ import ( var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type") -// Storage in memory storage system +// Storage is an implementation of git.Storer that stores data on memory, being +// ephemeral. The use of this storage should be done in controlled envoriments, +// since the representation in memory of some repository can fill the machine +// memory. in the other hand this storage has the best performance. type Storage struct { - c *ConfigStorage - o *ObjectStorage - r *ReferenceStorage + ConfigStorage + ObjectStorage + ReferenceStorage } -// NewStorage returns a new Storage +// NewStorage returns a new Storage base on memory func NewStorage() *Storage { - return &Storage{} -} - -// ConfigStorage return the ConfigStorage, if not exists create a new one -func (s *Storage) ConfigStorage() config.ConfigStorage { - if s.c != nil { - return s.c - } - - s.c = &ConfigStorage{ - RemotesConfig: make(map[string]*config.RemoteConfig), - } - - return s.c -} - -// ObjectStorage returns the ObjectStorage, if not exists creates a new one -func (s *Storage) ObjectStorage() core.ObjectStorage { - if s.o != nil { - return s.o - } - - s.o = &ObjectStorage{ - Objects: make(map[core.Hash]core.Object, 0), - Commits: make(map[core.Hash]core.Object, 0), - Trees: make(map[core.Hash]core.Object, 0), - Blobs: make(map[core.Hash]core.Object, 0), - Tags: make(map[core.Hash]core.Object, 0), + return &Storage{ + ReferenceStorage: make(ReferenceStorage, 0), + ConfigStorage: ConfigStorage{}, + ObjectStorage: ObjectStorage{ + Objects: make(map[core.Hash]core.Object, 0), + Commits: make(map[core.Hash]core.Object, 0), + Trees: make(map[core.Hash]core.Object, 0), + Blobs: make(map[core.Hash]core.Object, 0), + Tags: make(map[core.Hash]core.Object, 0), + }, } - - return s.o -} - -// ReferenceStorage returns the ReferenceStorage if not exists creates a new one -func (s *Storage) ReferenceStorage() core.ReferenceStorage { - if s.r != nil { - return s.r - } - - r := make(ReferenceStorage, 0) - s.r = &r - - return s.r } type ConfigStorage struct { - RemotesConfig map[string]*config.RemoteConfig -} - -func (c *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) { - r, ok := c.RemotesConfig[name] - if ok { - return r, nil - } - - return nil, config.ErrRemoteConfigNotFound + config *config.Config } -func (c *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) { - var o []*config.RemoteConfig - for _, r := range c.RemotesConfig { - o = append(o, r) - } - - return o, nil -} -func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error { - if err := r.Validate(); err != nil { +func (c *ConfigStorage) SetConfig(cfg *config.Config) error { + if err := cfg.Validate(); err != nil { return err } - c.RemotesConfig[r.Name] = r + c.config = cfg return nil } -func (c *ConfigStorage) DeleteRemote(name string) error { - delete(c.RemotesConfig, name) - return nil +func (c *ConfigStorage) Config() (*config.Config, error) { + if c.config == nil { + c.config = config.NewConfig() + } + + return c.config, nil } -// ObjectStorage is the implementation of core.ObjectStorage for memory.Object type ObjectStorage struct { Objects map[core.Hash]core.Object Commits map[core.Hash]core.Object @@ -108,13 +64,11 @@ type ObjectStorage struct { Tags map[core.Hash]core.Object } -// NewObject creates a new MemoryObject func (o *ObjectStorage) NewObject() core.Object { return &core.MemoryObject{} } -// Set stores an object, the object should be properly filled before set it. -func (o *ObjectStorage) Set(obj core.Object) (core.Hash, error) { +func (o *ObjectStorage) SetObject(obj core.Object) (core.Hash, error) { h := obj.Hash() o.Objects[h] = obj @@ -134,8 +88,7 @@ func (o *ObjectStorage) Set(obj core.Object) (core.Hash, error) { return h, nil } -// Get returns a object with the given hash -func (o *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) { +func (o *ObjectStorage) Object(t core.ObjectType, h core.Hash) (core.Object, error) { obj, ok := o.Objects[h] if !ok || (core.AnyObject != t && obj.Type() != t) { return nil, core.ErrObjectNotFound @@ -144,8 +97,7 @@ func (o *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) return obj, nil } -// Iter returns a core.ObjectIter for the given core.ObjectTybe -func (o *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { +func (o *ObjectStorage) IterObjects(t core.ObjectType) (core.ObjectIter, error) { var series []core.Object switch t { case core.AnyObject: @@ -171,7 +123,7 @@ func flattenObjectMap(m map[core.Hash]core.Object) []core.Object { return objects } -func (o *ObjectStorage) Begin() core.TxObjectStorage { +func (o *ObjectStorage) Begin() core.Transaction { return &TxObjectStorage{ Storage: o, Objects: make(map[core.Hash]core.Object, 0), @@ -183,14 +135,14 @@ type TxObjectStorage struct { Objects map[core.Hash]core.Object } -func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) { +func (tx *TxObjectStorage) SetObject(obj core.Object) (core.Hash, error) { h := obj.Hash() tx.Objects[h] = obj return h, nil } -func (tx *TxObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) { +func (tx *TxObjectStorage) Object(t core.ObjectType, h core.Hash) (core.Object, error) { obj, ok := tx.Objects[h] if !ok || (core.AnyObject != t && obj.Type() != t) { return nil, core.ErrObjectNotFound @@ -202,7 +154,7 @@ func (tx *TxObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, err func (tx *TxObjectStorage) Commit() error { for h, obj := range tx.Objects { delete(tx.Objects, h) - if _, err := tx.Storage.Set(obj); err != nil { + if _, err := tx.Storage.SetObject(obj); err != nil { return err } } @@ -217,8 +169,7 @@ func (tx *TxObjectStorage) Rollback() error { type ReferenceStorage map[core.ReferenceName]*core.Reference -// Set stores a reference. -func (r ReferenceStorage) Set(ref *core.Reference) error { +func (r ReferenceStorage) SetReference(ref *core.Reference) error { if ref != nil { r[ref.Name()] = ref } @@ -226,8 +177,7 @@ func (r ReferenceStorage) Set(ref *core.Reference) error { return nil } -// Get returns a stored reference with the given name -func (r ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { +func (r ReferenceStorage) Reference(n core.ReferenceName) (*core.Reference, error) { ref, ok := r[n] if !ok { return nil, core.ErrReferenceNotFound @@ -236,8 +186,7 @@ func (r ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { return ref, nil } -// Iter returns a core.ReferenceIter -func (r ReferenceStorage) Iter() (core.ReferenceIter, error) { +func (r ReferenceStorage) IterReferences() (core.ReferenceIter, error) { var refs []*core.Reference for _, ref := range r { refs = append(refs, ref) diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go index e1d5a33..ee8fa93 100644 --- a/storage/memory/storage_test.go +++ b/storage/memory/storage_test.go @@ -16,34 +16,5 @@ type StorageSuite struct { var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { - storage := NewStorage() - s.BaseStorageSuite = test.NewBaseStorageSuite( - storage.ObjectStorage(), - storage.ReferenceStorage(), - storage.ConfigStorage(), - ) -} - -func (s *StorageSuite) TestStorageObjectStorage(c *C) { - storage := NewStorage() - o := storage.ObjectStorage() - e := storage.ObjectStorage() - - c.Assert(o == e, Equals, true) -} - -func (s *StorageSuite) TestStorageReferenceStorage(c *C) { - storage := NewStorage() - o := storage.ReferenceStorage() - e := storage.ReferenceStorage() - - c.Assert(o == e, Equals, true) -} - -func (s *StorageSuite) TestStorageConfigStorage(c *C) { - storage := NewStorage() - o := storage.ConfigStorage() - e := storage.ConfigStorage() - - c.Assert(o == e, Equals, true) + s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage()) } diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index 8686853..451528f 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "io/ioutil" - "sort" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/core" @@ -14,6 +13,12 @@ import ( . "gopkg.in/check.v1" ) +type storer interface { + core.ObjectStorer + core.ReferenceStorer + config.ConfigStorer +} + type TestObject struct { Object core.Object Hash string @@ -21,19 +26,13 @@ type TestObject struct { } type BaseStorageSuite struct { - ObjectStorage core.ObjectStorage - ReferenceStorage core.ReferenceStorage - ConfigStore config.ConfigStorage + Storer storer validTypes []core.ObjectType testObjects map[core.ObjectType]TestObject } -func NewBaseStorageSuite( - os core.ObjectStorage, - rs core.ReferenceStorage, - cs config.ConfigStorage, -) BaseStorageSuite { +func NewBaseStorageSuite(s storer) BaseStorageSuite { commit := &core.MemoryObject{} commit.SetType(core.CommitObject) tree := &core.MemoryObject{} @@ -44,10 +43,7 @@ func NewBaseStorageSuite( tag.SetType(core.TagObject) return BaseStorageSuite{ - ObjectStorage: os, - ReferenceStorage: rs, - ConfigStore: cs, - + Storer: s, validTypes: []core.ObjectType{ core.CommitObject, core.BlobObject, @@ -62,19 +58,19 @@ func NewBaseStorageSuite( }} } -func (s *BaseStorageSuite) TestObjectStorageSetAndGet(c *C) { +func (s *BaseStorageSuite) TestSetObjectAndGetObject(c *C) { for _, to := range s.testObjects { comment := Commentf("failed for type %s", to.Type.String()) - h, err := s.ObjectStorage.Set(to.Object) + h, err := s.Storer.SetObject(to.Object) c.Assert(err, IsNil) c.Assert(h.String(), Equals, to.Hash, comment) - o, err := s.ObjectStorage.Get(to.Type, h) + o, err := s.Storer.Object(to.Type, h) c.Assert(err, IsNil) c.Assert(objectEquals(o, to.Object), IsNil) - o, err = s.ObjectStorage.Get(core.AnyObject, h) + o, err = s.Storer.Object(core.AnyObject, h) c.Assert(err, IsNil) c.Assert(objectEquals(o, to.Object), IsNil) @@ -83,31 +79,31 @@ func (s *BaseStorageSuite) TestObjectStorageSetAndGet(c *C) { continue } - o, err = s.ObjectStorage.Get(t, h) + o, err = s.Storer.Object(t, h) c.Assert(o, IsNil) c.Assert(err, Equals, core.ErrObjectNotFound) } } } -func (s *BaseStorageSuite) TestObjectStorageGetInvalid(c *C) { - o := s.ObjectStorage.NewObject() +func (s *BaseStorageSuite) TestSetObjectInvalid(c *C) { + o := s.Storer.NewObject() o.SetType(core.REFDeltaObject) - _, err := s.ObjectStorage.Set(o) + _, err := s.Storer.SetObject(o) c.Assert(err, NotNil) } -func (s *BaseStorageSuite) TestObjectStorageIter(c *C) { +func (s *BaseStorageSuite) TestStorerIter(c *C) { for _, o := range s.testObjects { - h, err := s.ObjectStorage.Set(o.Object) + h, err := s.Storer.SetObject(o.Object) c.Assert(err, IsNil) c.Assert(h, Equals, o.Object.Hash()) } for _, t := range s.validTypes { comment := Commentf("failed for type %s)", t.String()) - i, err := s.ObjectStorage.Iter(t) + i, err := s.Storer.IterObjects(t) c.Assert(err, IsNil, comment) o, err := i.Next() @@ -119,7 +115,7 @@ func (s *BaseStorageSuite) TestObjectStorageIter(c *C) { c.Assert(err, Equals, io.EOF, comment) } - i, err := s.ObjectStorage.Iter(core.AnyObject) + i, err := s.Storer.IterObjects(core.AnyObject) c.Assert(err, IsNil) foundObjects := []core.Object{} @@ -141,15 +137,20 @@ func (s *BaseStorageSuite) TestObjectStorageIter(c *C) { } } -func (s *BaseStorageSuite) TestTxObjectStorageSetAndCommit(c *C) { - tx := s.ObjectStorage.Begin() +func (s *BaseStorageSuite) TestObjectStorerTxSetObjectAndCommit(c *C) { + storer, ok := s.Storer.(core.Transactioner) + if !ok { + c.Skip("not a core.ObjectStorerTx") + } + + tx := storer.Begin() for _, o := range s.testObjects { - h, err := tx.Set(o.Object) + h, err := tx.SetObject(o.Object) c.Assert(err, IsNil) c.Assert(h.String(), Equals, o.Hash) } - iter, err := s.ObjectStorage.Iter(core.AnyObject) + iter, err := s.Storer.IterObjects(core.AnyObject) c.Assert(err, IsNil) _, err = iter.Next() c.Assert(err, Equals, io.EOF) @@ -157,7 +158,7 @@ func (s *BaseStorageSuite) TestTxObjectStorageSetAndCommit(c *C) { err = tx.Commit() c.Assert(err, IsNil) - iter, err = s.ObjectStorage.Iter(core.AnyObject) + iter, err = s.Storer.IterObjects(core.AnyObject) c.Assert(err, IsNil) var count int @@ -169,29 +170,44 @@ func (s *BaseStorageSuite) TestTxObjectStorageSetAndCommit(c *C) { c.Assert(count, Equals, 4) } -func (s *BaseStorageSuite) TestTxObjectStorageSetAndGet(c *C) { - tx := s.ObjectStorage.Begin() +func (s *BaseStorageSuite) TestObjectStorerTxSetObjectAndGetObject(c *C) { + storer, ok := s.Storer.(core.Transactioner) + if !ok { + c.Skip("not a core.ObjectStorerTx") + } + + tx := storer.Begin() for _, expected := range s.testObjects { - h, err := tx.Set(expected.Object) + h, err := tx.SetObject(expected.Object) c.Assert(err, IsNil) c.Assert(h.String(), Equals, expected.Hash) - o, err := tx.Get(expected.Type, core.NewHash(expected.Hash)) + o, err := tx.Object(expected.Type, core.NewHash(expected.Hash)) c.Assert(o.Hash().String(), DeepEquals, expected.Hash) } } -func (s *BaseStorageSuite) TestTxObjectStorageGetNotFound(c *C) { - tx := s.ObjectStorage.Begin() - o, err := tx.Get(core.AnyObject, core.ZeroHash) +func (s *BaseStorageSuite) TestObjectStorerTxGetObjectNotFound(c *C) { + storer, ok := s.Storer.(core.Transactioner) + if !ok { + c.Skip("not a core.ObjectStorerTx") + } + + tx := storer.Begin() + o, err := tx.Object(core.AnyObject, core.ZeroHash) c.Assert(o, IsNil) c.Assert(err, Equals, core.ErrObjectNotFound) } -func (s *BaseStorageSuite) TestTxObjectStorageSetAndRollback(c *C) { - tx := s.ObjectStorage.Begin() +func (s *BaseStorageSuite) TestObjectStorerTxSetObjectAndRollback(c *C) { + storer, ok := s.Storer.(core.Transactioner) + if !ok { + c.Skip("not a core.ObjectStorerTx") + } + + tx := storer.Begin() for _, o := range s.testObjects { - h, err := tx.Set(o.Object) + h, err := tx.SetObject(o.Object) c.Assert(err, IsNil) c.Assert(h.String(), Equals, o.Hash) } @@ -199,41 +215,41 @@ func (s *BaseStorageSuite) TestTxObjectStorageSetAndRollback(c *C) { err := tx.Rollback() c.Assert(err, IsNil) - iter, err := s.ObjectStorage.Iter(core.AnyObject) + iter, err := s.Storer.IterObjects(core.AnyObject) c.Assert(err, IsNil) _, err = iter.Next() c.Assert(err, Equals, io.EOF) } -func (s *BaseStorageSuite) TestReferenceStorageSetAndGet(c *C) { - err := s.ReferenceStorage.Set( +func (s *BaseStorageSuite) TestSetReferenceAndGetReference(c *C) { + err := s.Storer.SetReference( core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), ) c.Assert(err, IsNil) - err = s.ReferenceStorage.Set( + err = s.Storer.SetReference( core.NewReferenceFromStrings("bar", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), ) c.Assert(err, IsNil) - e, err := s.ReferenceStorage.Get(core.ReferenceName("foo")) + e, err := s.Storer.Reference(core.ReferenceName("foo")) c.Assert(err, IsNil) c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") } -func (s *BaseStorageSuite) TestReferenceStorageGetNotFound(c *C) { - r, err := s.ReferenceStorage.Get(core.ReferenceName("bar")) +func (s *BaseStorageSuite) TestGetReferenceNotFound(c *C) { + r, err := s.Storer.Reference(core.ReferenceName("bar")) c.Assert(err, Equals, core.ErrReferenceNotFound) c.Assert(r, IsNil) } -func (s *BaseStorageSuite) TestReferenceStorageIter(c *C) { - err := s.ReferenceStorage.Set( +func (s *BaseStorageSuite) TestIterReferences(c *C) { + err := s.Storer.SetReference( core.NewReferenceFromStrings("refs/foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), ) c.Assert(err, IsNil) - i, err := s.ReferenceStorage.Iter() + i, err := s.Storer.IterReferences() c.Assert(err, IsNil) e, err := i.Next() @@ -245,50 +261,27 @@ func (s *BaseStorageSuite) TestReferenceStorageIter(c *C) { c.Assert(err, Equals, io.EOF) } -func (s *BaseStorageSuite) TestConfigStorageSetGetAndDelete(c *C) { - err := s.ConfigStore.SetRemote(&config.RemoteConfig{ +func (s *BaseStorageSuite) TestSetConfigAndConfig(c *C) { + expected := config.NewConfig() + expected.Remotes["foo"] = &config.RemoteConfig{ Name: "foo", URL: "http://foo/bar.git", - }) - - c.Assert(err, IsNil) + } - r, err := s.ConfigStore.Remote("foo") + err := s.Storer.SetConfig(expected) c.Assert(err, IsNil) - c.Assert(r.Name, Equals, "foo") - err = s.ConfigStore.DeleteRemote("foo") + cfg, err := s.Storer.Config() c.Assert(err, IsNil) - - r, err = s.ConfigStore.Remote("foo") - c.Assert(err, Equals, config.ErrRemoteConfigNotFound) - c.Assert(r, IsNil) -} - -func (s *BaseStorageSuite) TestConfigStorageSetInvalid(c *C) { - err := s.ConfigStore.SetRemote(&config.RemoteConfig{}) - c.Assert(err, NotNil) + c.Assert(cfg, DeepEquals, expected) } -func (s *BaseStorageSuite) TestConfigStorageRemotes(c *C) { - s.ConfigStore.SetRemote(&config.RemoteConfig{ - Name: "foo", URL: "http://foo/bar.git", - }) - - s.ConfigStore.SetRemote(&config.RemoteConfig{ - Name: "bar", URL: "http://foo/bar.git", - }) +func (s *BaseStorageSuite) TestSetConfigInvalid(c *C) { + cfg := config.NewConfig() + cfg.Remotes["foo"] = &config.RemoteConfig{} - r, err := s.ConfigStore.Remotes() - c.Assert(err, IsNil) - c.Assert(r, HasLen, 2) - - sorted := make([]string, 0, 2) - sorted = append(sorted, r[0].Name) - sorted = append(sorted, r[1].Name) - sort.Strings(sorted) - c.Assert(sorted[0], Equals, "bar") - c.Assert(sorted[1], Equals, "foo") + err := s.Storer.SetConfig(cfg) + c.Assert(err, NotNil) } func objectEquals(a core.Object, b core.Object) error { |