aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config/config.go111
-rw-r--r--config/config_test.go48
-rw-r--r--storage/filesystem/config.go105
3 files changed, 169 insertions, 95 deletions
diff --git a/config/config.go b/config/config.go
index a2b5012..bffb125 100644
--- a/config/config.go
+++ b/config/config.go
@@ -2,8 +2,11 @@
package config
import (
+ "bytes"
"errors"
"fmt"
+
+ "gopkg.in/src-d/go-git.v4/plumbing/format/config"
)
const (
@@ -27,17 +30,24 @@ var (
)
// Config contains the repository configuration
+// https://www.kernel.org/pub/software/scm/git/docs/git-config.html
type Config struct {
Core struct {
IsBare bool
}
Remotes map[string]*RemoteConfig
+
+ // contains the raw information of a config file, the main goal is preserve
+ // the parsed information from the original format, to avoid miss not
+ // supported properties
+ raw *config.Config
}
// NewConfig returns a new empty Config
func NewConfig() *Config {
return &Config{
Remotes: make(map[string]*RemoteConfig, 0),
+ raw: config.New(),
}
}
@@ -56,11 +66,82 @@ func (c *Config) Validate() error {
return nil
}
+const (
+ remoteSection = "remote"
+ coreSection = "core"
+ fetchKey = "fetch"
+ urlKey = "url"
+ bareKey = "bare"
+)
+
+// Unmarshal parses a git-config file and stores it
+func (c *Config) Unmarshal(b []byte) error {
+ r := bytes.NewBuffer(b)
+ d := config.NewDecoder(r)
+
+ c.raw = config.New()
+ if err := d.Decode(c.raw); err != nil {
+ return err
+ }
+
+ c.unmarshalCore()
+ c.unmarshalRemotes()
+ return nil
+}
+
+func (c *Config) unmarshalCore() {
+ s := c.raw.Section(coreSection)
+ if s.Options.Get(bareKey) == "true" {
+ c.Core.IsBare = true
+ }
+}
+
+func (c *Config) unmarshalRemotes() {
+ s := c.raw.Section(remoteSection)
+ for _, sub := range s.Subsections {
+ r := &RemoteConfig{}
+ r.unmarshal(sub)
+
+ c.Remotes[r.Name] = r
+ }
+}
+
+// Marshal returns Config encoded as a git-config file
+func (c *Config) Marshal() ([]byte, error) {
+ c.marshalCore()
+ c.marshalRemotes()
+
+ buf := bytes.NewBuffer(nil)
+ if err := config.NewEncoder(buf).Encode(c.raw); err != nil {
+ return nil, err
+ }
+
+ return buf.Bytes(), nil
+}
+
+func (c *Config) marshalCore() {
+ s := c.raw.Section(coreSection)
+ s.SetOption(bareKey, fmt.Sprintf("%t", c.Core.IsBare))
+}
+
+func (c *Config) marshalRemotes() {
+ s := c.raw.Section(remoteSection)
+ s.Subsections = make(config.Subsections, len(c.Remotes))
+
+ var i int
+ for _, r := range c.Remotes {
+ s.Subsections[i] = r.marshal()
+ i++
+ }
+}
+
// RemoteConfig contains the configuration for a given repository
type RemoteConfig struct {
Name string
URL string
Fetch []RefSpec
+
+ raw *config.Subsection
}
// Validate validate the fields and set the default values
@@ -79,3 +160,33 @@ func (c *RemoteConfig) Validate() error {
return nil
}
+
+func (c *RemoteConfig) unmarshal(s *config.Subsection) {
+ c.raw = s
+
+ fetch := []RefSpec{}
+ for _, f := range c.raw.Options.GetAll(fetchKey) {
+ rs := RefSpec(f)
+ if rs.IsValid() {
+ fetch = append(fetch, rs)
+ }
+ }
+
+ c.Name = c.raw.Name
+ c.URL = c.raw.Option(urlKey)
+ c.Fetch = fetch
+}
+
+func (c *RemoteConfig) marshal() *config.Subsection {
+ if c.raw == nil {
+ c.raw = &config.Subsection{}
+ }
+
+ c.raw.Name = c.Name
+ c.raw.SetOption(urlKey, c.URL)
+ for _, rs := range c.Fetch {
+ c.raw.SetOption(fetchKey, rs.String())
+ }
+
+ return c.raw
+}
diff --git a/config/config_test.go b/config/config_test.go
index f2539d0..2bcefe4 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -6,7 +6,51 @@ type ConfigSuite struct{}
var _ = Suite(&ConfigSuite{})
-func (s *ConfigSuite) TestConfigValidateInvalidRemote(c *C) {
+func (s *ConfigSuite) TestUnmarshall(c *C) {
+ input := []byte(`[core]
+ bare = true
+[remote "origin"]
+ url = git@github.com:mcuadros/go-git.git
+ fetch = +refs/heads/*:refs/remotes/origin/*
+[branch "master"]
+ remote = origin
+ merge = refs/heads/master
+`)
+
+ cfg := NewConfig()
+ err := cfg.Unmarshal(input)
+ c.Assert(err, IsNil)
+
+ c.Assert(cfg.Core.IsBare, Equals, true)
+ c.Assert(cfg.Remotes, HasLen, 1)
+ c.Assert(cfg.Remotes["origin"].Name, Equals, "origin")
+ c.Assert(cfg.Remotes["origin"].URL, Equals, "git@github.com:mcuadros/go-git.git")
+ c.Assert(cfg.Remotes["origin"].Fetch, DeepEquals, []RefSpec{"+refs/heads/*:refs/remotes/origin/*"})
+}
+
+func (s *ConfigSuite) TestUnmarshallMarshall(c *C) {
+ input := []byte(`[core]
+ bare = true
+ custom = ignored
+[remote "origin"]
+ url = git@github.com:mcuadros/go-git.git
+ fetch = +refs/heads/*:refs/remotes/origin/*
+ mirror = true
+[branch "master"]
+ remote = origin
+ merge = refs/heads/master
+`)
+
+ cfg := NewConfig()
+ err := cfg.Unmarshal(input)
+ c.Assert(err, IsNil)
+
+ output, err := cfg.Marshal()
+ c.Assert(err, IsNil)
+ c.Assert(output, DeepEquals, input)
+}
+
+func (s *ConfigSuite) TestValidateInvalidRemote(c *C) {
config := &Config{
Remotes: map[string]*RemoteConfig{
"foo": {Name: "foo"},
@@ -16,7 +60,7 @@ func (s *ConfigSuite) TestConfigValidateInvalidRemote(c *C) {
c.Assert(config.Validate(), Equals, ErrRemoteConfigEmptyURL)
}
-func (s *ConfigSuite) TestConfigValidateInvalidKey(c *C) {
+func (s *ConfigSuite) TestValidateInvalidKey(c *C) {
config := &Config{
Remotes: map[string]*RemoteConfig{
"bar": {Name: "foo"},
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go
index 07e0433..7693fd4 100644
--- a/storage/filesystem/config.go
+++ b/storage/filesystem/config.go
@@ -1,22 +1,14 @@
package filesystem
import (
- "fmt"
"os"
+ "io/ioutil"
+
"gopkg.in/src-d/go-git.v4/config"
- gitconfig "gopkg.in/src-d/go-git.v4/plumbing/format/config"
"gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit"
)
-const (
- remoteSection = "remote"
- coreSection = "core"
- fetchKey = "fetch"
- urlKey = "url"
- bareKey = "bare"
-)
-
type ConfigStorage struct {
dir *dotgit.DotGit
}
@@ -24,20 +16,6 @@ type ConfigStorage struct {
func (c *ConfigStorage) Config() (*config.Config, error) {
cfg := config.NewConfig()
- ini, err := c.unmarshal()
- if err != nil {
- return nil, err
- }
-
- c.unmarshalCore(cfg, ini)
- c.unmarshalRemotes(cfg, ini)
-
- return cfg, nil
-}
-
-func (c *ConfigStorage) unmarshal() (*gitconfig.Config, error) {
- cfg := gitconfig.New()
-
f, err := c.dir.Config()
if err != nil {
if os.IsNotExist(err) {
@@ -49,43 +27,16 @@ func (c *ConfigStorage) unmarshal() (*gitconfig.Config, error) {
defer f.Close()
- d := gitconfig.NewDecoder(f)
- if err := d.Decode(cfg); err != nil {
+ b, err := ioutil.ReadAll(f)
+ if err != nil {
return nil, err
}
- return cfg, nil
-}
-
-func (c *ConfigStorage) unmarshalCore(cfg *config.Config, ini *gitconfig.Config) {
- s := ini.Section(coreSection)
- if s.Options.Get(bareKey) == "true" {
- cfg.Core.IsBare = true
- }
-}
-
-func (c *ConfigStorage) unmarshalRemotes(cfg *config.Config, ini *gitconfig.Config) {
- s := ini.Section(remoteSection)
- for _, sub := range s.Subsections {
- r := c.unmarshalRemote(sub)
- cfg.Remotes[r.Name] = r
- }
-}
-
-func (c *ConfigStorage) unmarshalRemote(s *gitconfig.Subsection) *config.RemoteConfig {
- fetch := []config.RefSpec{}
- for _, f := range s.Options.GetAll(fetchKey) {
- rs := config.RefSpec(f)
- if rs.IsValid() {
- fetch = append(fetch, rs)
- }
+ if err := cfg.Unmarshal(b); err != nil {
+ return nil, err
}
- return &config.RemoteConfig{
- Name: s.Name,
- URL: s.Option(urlKey),
- Fetch: fetch,
- }
+ return cfg, nil
}
func (c *ConfigStorage) SetConfig(cfg *config.Config) error {
@@ -93,50 +44,18 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) error {
return err
}
- ini, err := c.unmarshal()
+ f, err := c.dir.ConfigWriter()
if err != nil {
return err
}
- c.marshalCore(cfg, ini)
- c.marshalRemotes(cfg, ini)
- return c.marshal(ini)
-}
-
-func (c *ConfigStorage) marshalCore(cfg *config.Config, ini *gitconfig.Config) {
- s := ini.Section(coreSection)
- s.AddOption(bareKey, fmt.Sprintf("%t", cfg.Core.IsBare))
-}
-
-func (c *ConfigStorage) marshalRemotes(cfg *config.Config, ini *gitconfig.Config) {
- 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++
- }
-}
-
-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
-}
+ defer f.Close()
-func (c *ConfigStorage) marshal(ini *gitconfig.Config) error {
- f, err := c.dir.ConfigWriter()
+ b, err := cfg.Marshal()
if err != nil {
return err
}
- defer f.Close()
-
- e := gitconfig.NewEncoder(f)
- return e.Encode(ini)
+ _, err = f.Write(b)
+ return err
}