diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-30 11:56:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-30 11:56:32 +0100 |
commit | 9b058a30ea07dc237e3cc2ad434b3a6f51be340c (patch) | |
tree | 3e4f96e30bb4db396fb609d61913c6a6397577ad /config | |
parent | 1c2602a791371e76d52f89b2c8193cb200c66ad6 (diff) | |
parent | 7581d7f76a08869ed1ad1384dea1880c635af58b (diff) | |
download | go-git-9b058a30ea07dc237e3cc2ad434b3a6f51be340c.tar.gz |
Merge pull request #227 from mcuadros/submodules
config: marshal and unmarshal done inside of the package, and submodules config file
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 126 | ||||
-rw-r--r-- | config/config_test.go | 48 | ||||
-rw-r--r-- | config/modules.go | 136 | ||||
-rw-r--r-- | config/modules_test.go | 73 |
4 files changed, 378 insertions, 5 deletions
diff --git a/config/config.go b/config/config.go index a2b5012..fc6d84c 100644 --- a/config/config.go +++ b/config/config.go @@ -2,8 +2,11 @@ package config import ( + "bytes" "errors" "fmt" + + format "gopkg.in/src-d/go-git.v4/plumbing/format/config" ) const ( @@ -27,17 +30,28 @@ var ( ) // Config contains the repository configuration +// ftp://www.kernel.org/pub/software/scm/git/docs/git-config.html#FILES type Config struct { + // Core variables Core struct { + // IsBare if true this repository is assumed to be bare and has no + // working directory associated with it IsBare bool } + // Remote list of repository remotes 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 missing + // unsupported features. + raw *format.Config } // NewConfig returns a new empty Config func NewConfig() *Config { return &Config{ Remotes: make(map[string]*RemoteConfig, 0), + raw: format.New(), } } @@ -56,11 +70,87 @@ func (c *Config) Validate() error { return nil } -// RemoteConfig contains the configuration for a given repository +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 := format.NewDecoder(r) + + c.raw = format.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 := format.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(format.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 remote repository type RemoteConfig struct { - Name string - URL string + // Name of the remote + Name string + // URL the URL of a remote repository + URL string + // Fetch the default set of "refspec" for fetch operation Fetch []RefSpec + + // raw representation of the subsection, filled by marshal or unmarshal are + // called + raw *format.Subsection } // Validate validate the fields and set the default values @@ -79,3 +169,33 @@ func (c *RemoteConfig) Validate() error { return nil } + +func (c *RemoteConfig) unmarshal(s *format.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() *format.Subsection { + if c.raw == nil { + c.raw = &format.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/config/modules.go b/config/modules.go new file mode 100644 index 0000000..8b3d2b8 --- /dev/null +++ b/config/modules.go @@ -0,0 +1,136 @@ +package config + +import ( + "bytes" + "errors" + + format "gopkg.in/src-d/go-git.v4/plumbing/format/config" +) + +var ( + ErrModuleEmptyURL = errors.New("module config: empty URL") + ErrModuleEmptyPath = errors.New("module config: empty path") +) + +// Modules defines the submodules properties, represents a .gitmodules file +// https://www.kernel.org/pub/software/scm/git/docs/gitmodules.html +type Modules struct { + // Submodules is a map of submodules being the key the name of the submodule + Submodules map[string]*Submodule + + raw *format.Config +} + +// NewModules returns a new empty Modules +func NewModules() *Modules { + return &Modules{ + Submodules: make(map[string]*Submodule, 0), + raw: format.New(), + } +} + +const ( + submoduleSection = "submodule" + pathKey = "path" + branchKey = "branch" +) + +// Unmarshal parses a git-config file and stores it +func (m *Modules) Unmarshal(b []byte) error { + r := bytes.NewBuffer(b) + d := format.NewDecoder(r) + + m.raw = format.New() + if err := d.Decode(m.raw); err != nil { + return err + } + + s := m.raw.Section(submoduleSection) + for _, sub := range s.Subsections { + mod := &Submodule{} + mod.unmarshal(sub) + + m.Submodules[mod.Path] = mod + } + + return nil +} + +// Marshal returns Modules encoded as a git-config file +func (m *Modules) Marshal() ([]byte, error) { + s := m.raw.Section(submoduleSection) + s.Subsections = make(format.Subsections, len(m.Submodules)) + + var i int + for _, r := range m.Submodules { + s.Subsections[i] = r.marshal() + i++ + } + + buf := bytes.NewBuffer(nil) + if err := format.NewEncoder(buf).Encode(m.raw); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +// Submodule defines a submodule +type Submodule struct { + // Name module name + Name string + // Path defines the path, relative to the top-level directory of the Git + // working tree, + Path string + // URL defines a URL from which the submodule repository can be cloned. + URL string + // Branch is a remote branch name for tracking updates in the upstream + // submodule. Optional value. + Branch string + + // raw representation of the subsection, filled by marshal or unmarshal are + // called + raw *format.Subsection +} + +// Validate validate the fields and set the default values +func (m *Submodule) Validate() error { + if m.Path == "" { + return ErrModuleEmptyPath + } + + if m.URL == "" { + return ErrModuleEmptyURL + } + + return nil +} + +func (m *Submodule) unmarshal(s *format.Subsection) { + m.raw = s + + m.Name = m.raw.Name + m.Path = m.raw.Option(pathKey) + m.URL = m.raw.Option(urlKey) + m.Branch = m.raw.Option(branchKey) +} + +func (m *Submodule) marshal() *format.Subsection { + if m.raw == nil { + m.raw = &format.Subsection{} + } + + m.raw.Name = m.Name + if m.raw.Name == "" { + m.raw.Name = m.Path + } + + m.raw.SetOption(pathKey, m.Path) + m.raw.SetOption(urlKey, m.URL) + + if m.Branch != "" { + m.raw.SetOption(branchKey, m.Branch) + } + + return m.raw +} diff --git a/config/modules_test.go b/config/modules_test.go new file mode 100644 index 0000000..ab7b116 --- /dev/null +++ b/config/modules_test.go @@ -0,0 +1,73 @@ +package config + +import . "gopkg.in/check.v1" + +type ModulesSuite struct{} + +var _ = Suite(&ModulesSuite{}) + +func (s *ModulesSuite) TestValidateMissingURL(c *C) { + m := &Submodule{Path: "foo"} + c.Assert(m.Validate(), Equals, ErrModuleEmptyURL) +} + +func (s *ModulesSuite) TestValidateMissingName(c *C) { + m := &Submodule{URL: "bar"} + c.Assert(m.Validate(), Equals, ErrModuleEmptyPath) +} + +func (s *ModulesSuite) TestMarshall(c *C) { + input := []byte(`[submodule "qux"] + path = qux + url = baz + branch = bar +`) + + cfg := NewModules() + cfg.Submodules["qux"] = &Submodule{Path: "qux", URL: "baz", Branch: "bar"} + + output, err := cfg.Marshal() + c.Assert(err, IsNil) + c.Assert(output, DeepEquals, input) +} + +func (s *ModulesSuite) TestUnmarshall(c *C) { + input := []byte(`[submodule "qux"] + path = qux + url = https://github.com/foo/qux.git +[submodule "foo/bar"] + path = foo/bar + url = https://github.com/foo/bar.git + branch = dev +`) + + cfg := NewModules() + err := cfg.Unmarshal(input) + c.Assert(err, IsNil) + + c.Assert(cfg.Submodules, HasLen, 2) + c.Assert(cfg.Submodules["qux"].Name, Equals, "qux") + c.Assert(cfg.Submodules["qux"].URL, Equals, "https://github.com/foo/qux.git") + c.Assert(cfg.Submodules["foo/bar"].Name, Equals, "foo/bar") + c.Assert(cfg.Submodules["foo/bar"].URL, Equals, "https://github.com/foo/bar.git") + c.Assert(cfg.Submodules["foo/bar"].Branch, Equals, "dev") +} + +func (s *ModulesSuite) TestUnmarshallMarshall(c *C) { + input := []byte(`[submodule "qux"] + path = qux + url = https://github.com/foo/qux.git +[submodule "foo/bar"] + path = foo/bar + url = https://github.com/foo/bar.git + ignore = all +`) + + cfg := NewModules() + err := cfg.Unmarshal(input) + c.Assert(err, IsNil) + + output, err := cfg.Marshal() + c.Assert(err, IsNil) + c.Assert(string(output), DeepEquals, string(input)) +} |