diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-21 01:06:59 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-21 01:06:59 +0200 |
commit | c9f0c29f423f9bb26f32d6e8c7098f275171afb9 (patch) | |
tree | 9c746b16c92a6820f34d504a2a2dd6881d65f559 /storage | |
parent | 4652c0e753c88e63ba111d49aa58edc655806c03 (diff) | |
download | go-git-c9f0c29f423f9bb26f32d6e8c7098f275171afb9.tar.gz |
storage/filesystem: ConfigStore implementation
Diffstat (limited to 'storage')
-rw-r--r-- | storage/filesystem/config.go | 76 | ||||
-rw-r--r-- | storage/filesystem/config_test.go | 39 | ||||
-rw-r--r-- | storage/filesystem/object_test.go | 17 | ||||
-rw-r--r-- | storage/filesystem/storage.go | 11 |
4 files changed, 135 insertions, 8 deletions
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go new file mode 100644 index 0000000..c83a59e --- /dev/null +++ b/storage/filesystem/config.go @@ -0,0 +1,76 @@ +package filesystem + +import ( + "fmt" + "io" + + "gopkg.in/gcfg.v1" + "gopkg.in/src-d/go-git.v4/config" + "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" +) + +type ConfigStorage struct { + dir *dotgit.DotGit +} + +func (c *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) { + file, err := c.read() + if err != nil { + return nil, err + } + + r, ok := file.Remotes[name] + if ok { + return r, nil + } + + return nil, config.ErrRemoteConfigNotFound +} + +func (c *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) { + file, err := c.read() + if err != nil { + return nil, err + } + + remotes := make([]*config.RemoteConfig, len(file.Remotes)) + + var i int + for _, r := range file.Remotes { + remotes[i] = r + } + + return remotes, nil +} + +func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error { + return fmt.Errorf("not implemented yet") +} + +func (c *ConfigStorage) DeleteRemote(name string) error { + return fmt.Errorf("not implemented yet") +} + +func (c *ConfigStorage) read() (*ConfigFile, error) { + fs, path, err := c.dir.Config() + if err != nil { + return nil, err + } + + r, err := fs.Open(path) + if err != nil { + return nil, err + } + + f := &ConfigFile{} + return f, f.Decode(r) +} + +type ConfigFile struct { + Remotes map[string]*config.RemoteConfig `gcfg:"remote"` +} + +// Decode decode a git config file intro the ConfigStore +func (c *ConfigFile) Decode(r io.Reader) error { + return gcfg.FatalOnly(gcfg.ReadInto(c, r)) +} diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go new file mode 100644 index 0000000..bf7eda5 --- /dev/null +++ b/storage/filesystem/config_test.go @@ -0,0 +1,39 @@ +package filesystem + +import ( + "bytes" + + . "gopkg.in/check.v1" +) + +type ConfigSuite struct{} + +var _ = Suite(&ConfigSuite{}) + +func (s *ConfigSuite) TestConfigFileDecode(c *C) { + config := &ConfigFile{} + + err := config.Decode(bytes.NewBuffer(configFixture)) + c.Assert(err, IsNil) + + c.Assert(config.Remotes, HasLen, 2) + c.Assert(config.Remotes["origin"].URL, Equals, "git@github.com:src-d/go-git.git") + c.Assert(config.Remotes["origin"].Fetch.String(), Equals, "+refs/heads/*:refs/remotes/origin/*") +} + +var configFixture = []byte(` +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true +[remote "origin"] + url = git@github.com:src-d/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* +[branch "v4"] + remote = origin + merge = refs/heads/v4 +[remote "mcuadros"] + url = git@github.com:mcuadros/go-git.git + fetch = +refs/heads/*:refs/remotes/mcuadros/* +`) diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 8bca5c6..692a69b 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -104,7 +104,7 @@ func (s *FsSuite) TestGetCompareWithMemoryStorage(c *C) { } } -func memStorageFromGitDir(fs fs.FS, path string) (*memory.ObjectStorage, error) { +func memStorageFromGitDir(fs fs.FS, path string) (core.ObjectStorage, error) { dir, err := dotgit.New(fs, path) if err != nil { return nil, err @@ -120,10 +120,11 @@ func memStorageFromGitDir(fs fs.FS, path string) (*memory.ObjectStorage, error) return nil, err } - sto := memory.NewObjectStorage() + sto := memory.NewStorage() r := packfile.NewStream(f) d := packfile.NewDecoder(r) - err = d.Decode(sto) + + err = d.Decode(sto.ObjectStorage()) if err != nil { return nil, err } @@ -133,7 +134,7 @@ func memStorageFromGitDir(fs fs.FS, path string) (*memory.ObjectStorage, error) return nil, err } - return sto, nil + return sto.ObjectStorage(), nil } func equalsStorages(a, b core.ObjectStorage) (bool, string, error) { @@ -246,7 +247,7 @@ func (s *FsSuite) TestIterCompareWithMemoryStorage(c *C) { } } -func memStorageFromDirPath(fs fs.FS, path string) (*memory.ObjectStorage, error) { +func memStorageFromDirPath(fs fs.FS, path string) (core.ObjectStorage, error) { dir, err := dotgit.New(fs, path) if err != nil { return nil, err @@ -257,7 +258,7 @@ func memStorageFromDirPath(fs fs.FS, path string) (*memory.ObjectStorage, error) return nil, err } - sto := memory.NewObjectStorage() + sto := memory.NewStorage() f, err := fs.Open(packfilePath) if err != nil { return nil, err @@ -265,7 +266,7 @@ func memStorageFromDirPath(fs fs.FS, path string) (*memory.ObjectStorage, error) r := packfile.NewStream(f) d := packfile.NewDecoder(r) - err = d.Decode(sto) + err = d.Decode(sto.ObjectStorage()) if err != nil { return nil, err } @@ -274,7 +275,7 @@ func memStorageFromDirPath(fs fs.FS, path string) (*memory.ObjectStorage, error) return nil, err } - return sto, nil + return sto.ObjectStorage(), nil } func iterToSortedSlice(storage core.ObjectStorage, typ core.ObjectType) ([]core.Object, diff --git a/storage/filesystem/storage.go b/storage/filesystem/storage.go index 63d0215..c483218 100644 --- a/storage/filesystem/storage.go +++ b/storage/filesystem/storage.go @@ -1,6 +1,7 @@ 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" @@ -11,6 +12,7 @@ type Storage struct { o *ObjectStorage r *ReferenceStorage + c *ConfigStorage } func NewStorage(fs fs.FS, path string) (*Storage, error) { @@ -40,3 +42,12 @@ func (s *Storage) ReferenceStorage() core.ReferenceStorage { 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 +} |