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

Submodules init and update #270

Merged
merged 12 commits into from
Feb 21, 2017
13 changes: 9 additions & 4 deletions storage/filesystem/internal/dotgit/dotgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const (
configPath = "config"
indexPath = "index"
shallowPath = "shallow"

objectsPath = "objects"
packPath = "pack"
refsPath = "refs"
modulePath = "module"
objectsPath = "objects"
packPath = "pack"
refsPath = "refs"

packExt = ".pack"
idxExt = ".idx"
Expand Down Expand Up @@ -390,6 +390,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return d.fs.Dir(d.fs.Join(modulePath, name))
}

func isHex(s string) bool {
for _, b := range []byte(s) {
if isNum(b) {
Expand Down
14 changes: 14 additions & 0 deletions storage/filesystem/module.go
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this type?, can we just make dotgit implement storage.ModuleStorer instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the pattern we are using. I prefer keep dotgit isolate

dir *dotgit.DotGit
}

func (s *ModuleStorage) Module(name string) (storage.Storer, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests and comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is already covered by the tests. And the documentation is on the interface as in the other methods

return NewStorage(s.dir.Module(name))
}
2 changes: 2 additions & 0 deletions storage/filesystem/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Storage struct {
IndexStorage
ShallowStorage
ConfigStorage
ModuleStorage
}

// NewStorage returns a new Storage backed by a given `fs.Filesystem`
Expand All @@ -32,5 +33,6 @@ func NewStorage(fs billy.Filesystem) (*Storage, error) {
IndexStorage: IndexStorage{dir: dir},
ShallowStorage: ShallowStorage{dir: dir},
ConfigStorage: ConfigStorage{dir: dir},
ModuleStorage: ModuleStorage{dir: dir},
}, nil
}
13 changes: 13 additions & 0 deletions storage/memory/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -22,6 +23,7 @@ type Storage struct {
ShallowStorage
IndexStorage
ReferenceStorage
ModuleStorage
}

// NewStorage returns a new Storage base on memory
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -227,3 +230,13 @@ 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 _, ok := s[name]; !ok {
s[name] = NewStorage()
}

return s[name], nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't look up twice in the map, use a variable to store the results of the first lookup.

}
23 changes: 23 additions & 0 deletions storage/storer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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
}

type ModuleStorer interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be explained, What if the module doesn't exists?

Module(name string) (Storer, error)
}
13 changes: 13 additions & 0 deletions storage/test/storage_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -21,6 +22,8 @@ type Storer interface {
storer.ShallowStorer
storer.IndexStorer
config.ConfigStorer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra line here.

storage.ModuleStorer
}

type TestObject struct {
Expand Down Expand Up @@ -326,6 +329,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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point in calling Module twice?
(explain this in the name of the test)

Actually what is the point of this whole test?

Copy link
Contributor Author

@mcuadros mcuadros Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests that returns a module. The function should returns a module if exists or not. The duplicate code is an error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your answer.

Can you explain what you are testing in the name of the test or on a comment?

c.Assert(err, IsNil)
c.Assert(storer, NotNil)
}

func objectEquals(a plumbing.EncodedObject, b plumbing.EncodedObject) error {
ha := a.Hash()
hb := b.Hash()
Expand Down