From f8b5557875513c5b0aff24bb7b8e28f8f680976f Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 13 Feb 2017 23:38:57 +0100 Subject: config: adding Config.Core.Worktree --- config/config.go | 9 +++++++++ config/config_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'config') diff --git a/config/config.go b/config/config.go index 866ae8e..b3a3fcc 100644 --- a/config/config.go +++ b/config/config.go @@ -37,6 +37,8 @@ type Config struct { // IsBare if true this repository is assumed to be bare and has no // working directory associated with it IsBare bool + // Worktree is the path to the root of the working tree + Worktree string } // Remote list of repository remotes Remotes map[string]*RemoteConfig @@ -76,6 +78,7 @@ const ( fetchKey = "fetch" urlKey = "url" bareKey = "bare" + worktreeKey = "worktree" ) // Unmarshal parses a git-config file and stores it @@ -97,6 +100,8 @@ func (c *Config) unmarshalCore() { if s.Options.Get(bareKey) == "true" { c.Core.IsBare = true } + + c.Core.Worktree = s.Options.Get(worktreeKey) } func (c *Config) unmarshalRemotes() error { @@ -129,6 +134,10 @@ func (c *Config) Marshal() ([]byte, error) { func (c *Config) marshalCore() { s := c.raw.Section(coreSection) s.SetOption(bareKey, fmt.Sprintf("%t", c.Core.IsBare)) + + if c.Core.Worktree != "" { + s.SetOption(worktreeKey, c.Core.Worktree) + } } func (c *Config) marshalRemotes() { diff --git a/config/config_test.go b/config/config_test.go index 2bcefe4..313c96c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -9,6 +9,7 @@ var _ = Suite(&ConfigSuite{}) func (s *ConfigSuite) TestUnmarshall(c *C) { input := []byte(`[core] bare = true + worktree = foo [remote "origin"] url = git@github.com:mcuadros/go-git.git fetch = +refs/heads/*:refs/remotes/origin/* @@ -22,15 +23,39 @@ func (s *ConfigSuite) TestUnmarshall(c *C) { c.Assert(err, IsNil) c.Assert(cfg.Core.IsBare, Equals, true) + c.Assert(cfg.Core.Worktree, Equals, "foo") 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) TestMarshall(c *C) { + output := []byte(`[core] + bare = true + worktree = bar +[remote "origin"] + url = git@github.com:mcuadros/go-git.git +`) + + cfg := NewConfig() + cfg.Core.IsBare = true + cfg.Core.Worktree = "bar" + cfg.Remotes["origin"] = &RemoteConfig{ + Name: "origin", + URL: "git@github.com:mcuadros/go-git.git", + } + + b, err := cfg.Marshal() + c.Assert(err, IsNil) + + c.Assert(string(b), Equals, string(output)) +} + func (s *ConfigSuite) TestUnmarshallMarshall(c *C) { input := []byte(`[core] bare = true + worktree = foo custom = ignored [remote "origin"] url = git@github.com:mcuadros/go-git.git -- cgit From 09110d8e6d1ddb6f6a22867dcedeebd8f2262780 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Tue, 14 Feb 2017 15:56:32 +0100 Subject: config: added Config.Submodules --- config/config.go | 49 ++++++++++++++++++++++++++++++++++++++++--------- config/config_test.go | 16 ++++++++++++++++ config/modules.go | 5 ++--- 3 files changed, 58 insertions(+), 12 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index b3a3fcc..65b51eb 100644 --- a/config/config.go +++ b/config/config.go @@ -40,8 +40,10 @@ type Config struct { // Worktree is the path to the root of the working tree Worktree string } - // Remote list of repository remotes + // Remotes list of repository remotes Remotes map[string]*RemoteConfig + // Submodules list of repository submodules + Submodules map[string]*Submodule // contains the raw information of a config file, the main goal is preserve // the parsed information from the original format, to avoid missing @@ -52,8 +54,9 @@ type Config struct { // NewConfig returns a new empty Config func NewConfig() *Config { return &Config{ - Remotes: make(map[string]*RemoteConfig, 0), - raw: format.New(), + Remotes: make(map[string]*RemoteConfig, 0), + Submodules: make(map[string]*Submodule, 0), + raw: format.New(), } } @@ -73,12 +76,13 @@ func (c *Config) Validate() error { } const ( - remoteSection = "remote" - coreSection = "core" - fetchKey = "fetch" - urlKey = "url" - bareKey = "bare" - worktreeKey = "worktree" + remoteSection = "remote" + submoduleSection = "submodule" + coreSection = "core" + fetchKey = "fetch" + urlKey = "url" + bareKey = "bare" + worktreeKey = "worktree" ) // Unmarshal parses a git-config file and stores it @@ -92,6 +96,7 @@ func (c *Config) Unmarshal(b []byte) error { } c.unmarshalCore() + c.unmarshalSubmodules() return c.unmarshalRemotes() } @@ -118,10 +123,21 @@ func (c *Config) unmarshalRemotes() error { return nil } +func (c *Config) unmarshalSubmodules() { + s := c.raw.Section(submoduleSection) + for _, sub := range s.Subsections { + m := &Submodule{} + m.unmarshal(sub) + + c.Submodules[m.Name] = m + } +} + // Marshal returns Config encoded as a git-config file func (c *Config) Marshal() ([]byte, error) { c.marshalCore() c.marshalRemotes() + c.marshalSubmodules() buf := bytes.NewBuffer(nil) if err := format.NewEncoder(buf).Encode(c.raw); err != nil { @@ -151,6 +167,21 @@ func (c *Config) marshalRemotes() { } } +func (c *Config) marshalSubmodules() { + s := c.raw.Section(submoduleSection) + s.Subsections = make(format.Subsections, len(c.Submodules)) + + var i int + for _, r := range c.Submodules { + section := r.marshal() + // the submodule section at config is a subset of the .gitmodule file + // we should remove the non-valid options for the config file. + section.RemoveOption(pathKey) + s.Subsections[i] = section + i++ + } +} + // RemoteConfig contains the configuration for a given remote repository type RemoteConfig struct { // Name of the remote diff --git a/config/config_test.go b/config/config_test.go index 313c96c..cfab36d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -13,6 +13,10 @@ func (s *ConfigSuite) TestUnmarshall(c *C) { [remote "origin"] url = git@github.com:mcuadros/go-git.git fetch = +refs/heads/*:refs/remotes/origin/* +[submodule "qux"] + path = qux + url = https://github.com/foo/qux.git + branch = bar [branch "master"] remote = origin merge = refs/heads/master @@ -28,6 +32,11 @@ func (s *ConfigSuite) TestUnmarshall(c *C) { 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/*"}) + c.Assert(cfg.Submodules, HasLen, 1) + 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["qux"].Branch, Equals, "bar") + } func (s *ConfigSuite) TestMarshall(c *C) { @@ -36,6 +45,8 @@ func (s *ConfigSuite) TestMarshall(c *C) { worktree = bar [remote "origin"] url = git@github.com:mcuadros/go-git.git +[submodule "qux"] + url = https://github.com/foo/qux.git `) cfg := NewConfig() @@ -46,6 +57,11 @@ func (s *ConfigSuite) TestMarshall(c *C) { URL: "git@github.com:mcuadros/go-git.git", } + cfg.Submodules["qux"] = &Submodule{ + Name: "qux", + URL: "https://github.com/foo/qux.git", + } + b, err := cfg.Marshal() c.Assert(err, IsNil) diff --git a/config/modules.go b/config/modules.go index 4d98b16..a17cc27 100644 --- a/config/modules.go +++ b/config/modules.go @@ -30,9 +30,8 @@ func NewModules() *Modules { } const ( - submoduleSection = "submodule" - pathKey = "path" - branchKey = "branch" + pathKey = "path" + branchKey = "branch" ) // Unmarshal parses a git-config file and stores it -- cgit From ed288b30de1ac3dcb3ce675c4b9af89eb4e6fcba Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Tue, 21 Feb 2017 16:03:39 +0100 Subject: documentation and API improvements --- config/config.go | 23 ++++++++++++----------- config/modules.go | 14 +++++++------- config/refspec.go | 12 ++++++------ 3 files changed, 25 insertions(+), 24 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index 65b51eb..259ebf9 100644 --- a/config/config.go +++ b/config/config.go @@ -32,17 +32,18 @@ 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 + // working directory associated with it. IsBare bool - // Worktree is the path to the root of the working tree + // Worktree is the path to the root of the working tree. Worktree string } - // Remotes list of repository remotes + // Remotes list of repository remotes, the key of the map is the name + // of the remote, should equal to RemoteConfig.Name. Remotes map[string]*RemoteConfig - // Submodules list of repository submodules + // Submodules list of repository submodules, the key of the map is the name + // of the submodule, should equal to Submodule.Name. Submodules map[string]*Submodule // contains the raw information of a config file, the main goal is preserve @@ -51,7 +52,7 @@ type Config struct { raw *format.Config } -// NewConfig returns a new empty Config +// NewConfig returns a new empty Config. func NewConfig() *Config { return &Config{ Remotes: make(map[string]*RemoteConfig, 0), @@ -60,7 +61,7 @@ func NewConfig() *Config { } } -// Validate validates the fields and sets the default values +// Validate validates the fields and sets the default values. func (c *Config) Validate() error { for name, r := range c.Remotes { if r.Name != name { @@ -85,7 +86,7 @@ const ( worktreeKey = "worktree" ) -// Unmarshal parses a git-config file and stores it +// Unmarshal parses a git-config file and stores it. func (c *Config) Unmarshal(b []byte) error { r := bytes.NewBuffer(b) d := format.NewDecoder(r) @@ -133,7 +134,7 @@ func (c *Config) unmarshalSubmodules() { } } -// Marshal returns Config encoded as a git-config file +// Marshal returns Config encoded as a git-config file. func (c *Config) Marshal() ([]byte, error) { c.marshalCore() c.marshalRemotes() @@ -182,7 +183,7 @@ func (c *Config) marshalSubmodules() { } } -// RemoteConfig contains the configuration for a given remote repository +// RemoteConfig contains the configuration for a given remote repository. type RemoteConfig struct { // Name of the remote Name string @@ -196,7 +197,7 @@ type RemoteConfig struct { raw *format.Subsection } -// Validate validates the fields and sets the default values +// Validate validates the fields and sets the default values. func (c *RemoteConfig) Validate() error { if c.Name == "" { return ErrRemoteConfigEmptyName diff --git a/config/modules.go b/config/modules.go index a17cc27..3d01117 100644 --- a/config/modules.go +++ b/config/modules.go @@ -15,7 +15,7 @@ var ( // 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 is a map of submodules being the key the name of the submodule. Submodules map[string]*Submodule raw *format.Config @@ -34,7 +34,7 @@ const ( branchKey = "branch" ) -// Unmarshal parses a git-config file and stores it +// Unmarshal parses a git-config file and stores it. func (m *Modules) Unmarshal(b []byte) error { r := bytes.NewBuffer(b) d := format.NewDecoder(r) @@ -55,7 +55,7 @@ func (m *Modules) Unmarshal(b []byte) error { return nil } -// Marshal returns Modules encoded as a git-config file +// 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)) @@ -74,12 +74,12 @@ func (m *Modules) Marshal() ([]byte, error) { return buf.Bytes(), nil } -// Submodule defines a submodule +// 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, + // working tree. Path string // URL defines a URL from which the submodule repository can be cloned. URL string @@ -88,11 +88,11 @@ type Submodule struct { Branch string // raw representation of the subsection, filled by marshal or unmarshal are - // called + // called. raw *format.Subsection } -// Validate validates the fields and sets the default values +// Validate validates the fields and sets the default values. func (m *Submodule) Validate() error { if m.Path == "" { return ErrModuleEmptyPath diff --git a/config/refspec.go b/config/refspec.go index dd68edc..9441df8 100644 --- a/config/refspec.go +++ b/config/refspec.go @@ -49,7 +49,7 @@ func (s RefSpec) Validate() error { return ErrRefSpecMalformedWildcard } -// IsForceUpdate returns if update is allowed in non fast-forward merges +// IsForceUpdate returns if update is allowed in non fast-forward merges. func (s RefSpec) IsForceUpdate() bool { if s[0] == refSpecForce[0] { return true @@ -67,7 +67,7 @@ func (s RefSpec) IsDelete() bool { return false } -// Src return the src side +// Src return the src side. func (s RefSpec) Src() string { spec := string(s) start := strings.Index(spec, refSpecForce) + 1 @@ -76,7 +76,7 @@ func (s RefSpec) Src() string { return spec[start:end] } -// Match match the given plumbing.ReferenceName against the source +// Match match the given plumbing.ReferenceName against the source. func (s RefSpec) Match(n plumbing.ReferenceName) bool { if !s.IsWildcard() { return s.matchExact(n) @@ -85,7 +85,7 @@ func (s RefSpec) Match(n plumbing.ReferenceName) bool { return s.matchGlob(n) } -// IsWildcard returns true if the RefSpec contains a wildcard +// IsWildcard returns true if the RefSpec contains a wildcard. func (s RefSpec) IsWildcard() bool { return strings.Index(string(s), refSpecWildcard) != -1 } @@ -110,7 +110,7 @@ func (s RefSpec) matchGlob(n plumbing.ReferenceName) bool { strings.HasSuffix(name, suffix) } -// Dst returns the destination for the given remote reference +// Dst returns the destination for the given remote reference. func (s RefSpec) Dst(n plumbing.ReferenceName) plumbing.ReferenceName { spec := string(s) start := strings.Index(spec, refSpecSeparator) + 1 @@ -133,7 +133,7 @@ func (s RefSpec) String() string { return string(s) } -// MatchAny returns true if any of the RefSpec match with the given ReferenceName +// MatchAny returns true if any of the RefSpec match with the given ReferenceName. func MatchAny(l []RefSpec, n plumbing.ReferenceName) bool { for _, r := range l { if r.Match(n) { -- cgit