diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-26 20:08:45 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-26 20:08:45 +0000 |
commit | c66f495b35b12c6827507b9933b39a10fa94379b (patch) | |
tree | aeae5054df4a0ba635ddf77743d7ee449f79cf17 /storage | |
parent | 61f0188edcea55dbcfa1c3a35da0c34fed10fd54 (diff) | |
download | go-git-c66f495b35b12c6827507b9933b39a10fa94379b.tar.gz |
storage: filesystem fix config on new repositories (#100)
Diffstat (limited to 'storage')
-rw-r--r-- | storage/filesystem/config.go | 14 | ||||
-rw-r--r-- | storage/filesystem/config_test.go | 66 |
2 files changed, 56 insertions, 24 deletions
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go index 84252eb..ba7e40b 100644 --- a/storage/filesystem/config.go +++ b/storage/filesystem/config.go @@ -1,6 +1,8 @@ package filesystem import ( + "os" + "gopkg.in/src-d/go-git.v4/config" gitconfig "gopkg.in/src-d/go-git.v4/formats/config" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" @@ -74,17 +76,21 @@ func (c *ConfigStorage) DeleteRemote(name string) error { } func (c *ConfigStorage) read() (*gitconfig.Config, error) { + cfg := gitconfig.New() + f, err := c.dir.Config() if err != nil { + if os.IsNotExist(err) { + return cfg, nil + } + return nil, err } defer f.Close() - cfg := gitconfig.New() d := gitconfig.NewDecoder(f) - err = d.Decode(cfg) - if err != nil { + if err := d.Decode(cfg); err != nil { return nil, err } @@ -92,7 +98,7 @@ func (c *ConfigStorage) read() (*gitconfig.Config, error) { } func (c *ConfigStorage) write(cfg *gitconfig.Config) error { - f, err := c.dir.Config() + f, err := c.dir.ConfigWriter() if err != nil { return err } diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go index 20af595..7759f4e 100644 --- a/storage/filesystem/config_test.go +++ b/storage/filesystem/config_test.go @@ -1,31 +1,57 @@ package filesystem import ( - "gopkg.in/src-d/go-git.v4/formats/config" + "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" . "gopkg.in/check.v1" ) -type ConfigSuite struct{} +type ConfigSuite struct { + fixtures.Suite + + dir *dotgit.DotGit + path string +} var _ = Suite(&ConfigSuite{}) -func (s *ConfigSuite) TestParseRemote(c *C) { - remote := parseRemote(&config.Subsection{ - Name: "origin", - Options: []*config.Option{ - { - Key: "url", - Value: "git@github.com:src-d/go-git.git", - }, - { - Key: "fetch", - Value: "+refs/heads/*:refs/remotes/origin/*", - }, - }, - }) - - c.Assert(remote.URL, Equals, "git@github.com:src-d/go-git.git") - c.Assert(remote.Fetch, HasLen, 1) - c.Assert(remote.Fetch[0].String(), Equals, "+refs/heads/*:refs/remotes/origin/*") +func (s *ConfigSuite) SetUpTest(c *C) { + tmp, err := ioutil.TempDir("", "go-git-filestystem-config") + c.Assert(err, IsNil) + + s.dir = dotgit.New(os.NewOS(tmp)) + s.path = tmp +} + +func (s *ConfigSuite) TestSetRemote(c *C) { + cfg := &ConfigStorage{s.dir} + err := cfg.SetRemote(&config.RemoteConfig{Name: "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} + + remotes, err := cfg.Remotes() + c.Assert(err, IsNil) + 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/*") +} + +func (s *ConfigSuite) TearDownTest(c *C) { + defer stdos.RemoveAll(s.path) } |