aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config/config.go49
-rw-r--r--config/config_test.go16
-rw-r--r--config/modules.go5
3 files changed, 58 insertions, 12 deletions
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