diff options
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit.go | 21 | ||||
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit_test.go | 8 | ||||
-rw-r--r-- | storage/memory/storage.go | 54 |
3 files changed, 68 insertions, 15 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go index f365f13..448f6a2 100644 --- a/storage/filesystem/internal/dotgit/dotgit.go +++ b/storage/filesystem/internal/dotgit/dotgit.go @@ -12,17 +12,18 @@ import ( const ( suffix = ".git" packedRefsPath = "packed-refs" + configPath = "config" ) var ( // ErrNotFound is returned by New when the path is not found. ErrNotFound = errors.New("path not found") - // ErrIdxNotFound is returned by Idxfile when the idx file is not found on the - // repository. + // ErrIdxNotFound is returned by Idxfile when the idx file is not found ErrIdxNotFound = errors.New("idx file not found") // ErrPackfileNotFound is returned by Packfile when the packfile is not found - // on the repository. ErrPackfileNotFound = errors.New("packfile not found") + // ErrConfigNotFound is returned by Config when the config is not found + ErrConfigNotFound = errors.New("config file not found") ) // The DotGit type represents a local git repository on disk. This @@ -103,3 +104,17 @@ func (d *DotGit) Idxfile() (fs.FS, string, error) { return nil, "", ErrIdxNotFound } + +// Config returns the path of the config file +func (d *DotGit) Config() (fs.FS, string, error) { + configFile := d.fs.Join(d.path, configPath) + if _, err := d.fs.Stat(configFile); err != nil { + if os.IsNotExist(err) { + return nil, "", ErrNotFound + } + + return nil, "", err + } + + return d.fs, configFile, nil +} diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go index 6125114..7c39c87 100644 --- a/storage/filesystem/internal/dotgit/dotgit_test.go +++ b/storage/filesystem/internal/dotgit/dotgit_test.go @@ -143,6 +143,14 @@ func (s *SuiteDotGit) TestRefsFromHEADFile(c *C) { c.Assert(string(ref.Target()), Equals, "refs/heads/master") } +func (s *SuiteDotGit) TestConfig(c *C) { + _, d := s.newFixtureDir(c, "spinnaker") + fs, path, err := d.Config() + c.Assert(err, IsNil) + c.Assert(fs, NotNil) + c.Assert(path, Not(Equals), "") +} + func findReference(refs []*core.Reference, name string) *core.Reference { n := core.ReferenceName(name) for _, ref := range refs { diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 32c2973..e242008 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -3,6 +3,7 @@ package memory import ( "fmt" + "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/core" ) @@ -10,6 +11,7 @@ var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type") // Storage in memory storage system type Storage struct { + c *ConfigStorage o *ObjectStorage r *ReferenceStorage } @@ -19,7 +21,20 @@ func NewStorage() *Storage { return &Storage{} } -// ObjectStorage returns the ObjectStorage if not exists creates a new one +// 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 @@ -48,6 +63,32 @@ func (s *Storage) ReferenceStorage() core.ReferenceStorage { 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 +} + +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 { + c.RemotesConfig[r.Name] = r + return nil +} + // ObjectStorage is the implementation of core.ObjectStorage for memory.Object type ObjectStorage struct { Objects map[core.Hash]core.Object @@ -57,17 +98,6 @@ type ObjectStorage struct { Tags map[core.Hash]core.Object } -// NewObjectStorage returns a new empty ObjectStorage -func NewObjectStorage() *ObjectStorage { - return &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), - } -} - // NewObject creates a new MemoryObject func (o *ObjectStorage) NewObject() core.Object { return &core.MemoryObject{} |