Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 4e78181

Browse files
committed
config: modules, marshal and unmarshal implementation
1 parent e2e4e7f commit 4e78181

File tree

2 files changed

+160
-20
lines changed

2 files changed

+160
-20
lines changed

config/modules.go

+100-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,83 @@
11
package config
22

3-
import "errors"
3+
import (
4+
"bytes"
5+
"errors"
6+
7+
"gopkg.in/src-d/go-git.v4/plumbing/format/config"
8+
)
49

510
var (
611
ErrModuleEmptyURL = errors.New("module config: empty URL")
712
ErrModuleEmptyPath = errors.New("module config: empty path")
813
)
914

10-
const DefaultModuleBranch = "master"
11-
1215
// Modules defines the submodules properties
13-
type Modules map[string]*Module
16+
type Modules struct {
17+
Submodules map[string]*Submodule
18+
19+
raw *config.Config
20+
}
21+
22+
// NewModules returns a new empty Modules
23+
func NewModules() *Modules {
24+
return &Modules{
25+
Submodules: make(map[string]*Submodule, 0),
26+
raw: config.New(),
27+
}
28+
}
29+
30+
const (
31+
submoduleSection = "submodule"
32+
pathKey = "path"
33+
branchKey = "branch"
34+
)
35+
36+
// Unmarshal parses a git-config file and stores it
37+
func (m *Modules) Unmarshal(b []byte) error {
38+
r := bytes.NewBuffer(b)
39+
d := config.NewDecoder(r)
40+
41+
m.raw = config.New()
42+
if err := d.Decode(m.raw); err != nil {
43+
return err
44+
}
45+
46+
s := m.raw.Section(submoduleSection)
47+
for _, sub := range s.Subsections {
48+
mod := &Submodule{}
49+
mod.unmarshal(sub)
50+
51+
m.Submodules[mod.Path] = mod
52+
}
53+
54+
return nil
55+
}
56+
57+
// Marshal returns Modules encoded as a git-config file
58+
func (m *Modules) Marshal() ([]byte, error) {
59+
s := m.raw.Section(submoduleSection)
60+
s.Subsections = make(config.Subsections, len(m.Submodules))
1461

15-
// Module defines a submodule
62+
var i int
63+
for _, r := range m.Submodules {
64+
s.Subsections[i] = r.marshal()
65+
i++
66+
}
67+
68+
buf := bytes.NewBuffer(nil)
69+
if err := config.NewEncoder(buf).Encode(m.raw); err != nil {
70+
return nil, err
71+
}
72+
73+
return buf.Bytes(), nil
74+
}
75+
76+
// Submodule defines a submodule
1677
// https://www.kernel.org/pub/software/scm/git/docs/gitmodules.html
17-
type Module struct {
78+
type Submodule struct {
79+
// Name module name
80+
Name string
1881
// Path defines the path, relative to the top-level directory of the Git
1982
// working tree,
2083
Path string
@@ -23,10 +86,12 @@ type Module struct {
2386
// Branch is a remote branch name for tracking updates in the upstream
2487
// submodule.
2588
Branch string
89+
90+
raw *config.Subsection
2691
}
2792

2893
// Validate validate the fields and set the default values
29-
func (m *Module) Validate() error {
94+
func (m *Submodule) Validate() error {
3095
if m.Path == "" {
3196
return ErrModuleEmptyPath
3297
}
@@ -35,9 +100,34 @@ func (m *Module) Validate() error {
35100
return ErrModuleEmptyURL
36101
}
37102

38-
if m.Branch == "" {
39-
m.Branch = DefaultModuleBranch
103+
return nil
104+
}
105+
106+
func (m *Submodule) unmarshal(s *config.Subsection) {
107+
m.raw = s
108+
109+
m.Name = m.raw.Name
110+
m.Path = m.raw.Option(pathKey)
111+
m.URL = m.raw.Option(urlKey)
112+
m.Branch = m.raw.Option(branchKey)
113+
}
114+
115+
func (m *Submodule) marshal() *config.Subsection {
116+
if m.raw == nil {
117+
m.raw = &config.Subsection{}
40118
}
41119

42-
return nil
120+
m.raw.Name = m.Name
121+
if m.raw.Name == "" {
122+
m.raw.Name = m.Path
123+
}
124+
125+
m.raw.SetOption(pathKey, m.Path)
126+
m.raw.SetOption(urlKey, m.URL)
127+
128+
if m.Branch != "" {
129+
m.raw.SetOption(branchKey, m.Branch)
130+
}
131+
132+
return m.raw
43133
}

config/modules_test.go

+60-10
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,72 @@ package config
22

33
import . "gopkg.in/check.v1"
44

5-
type ModuleSuite struct{}
5+
type ModulesSuite struct{}
66

7-
var _ = Suite(&ModuleSuite{})
7+
var _ = Suite(&ModulesSuite{})
88

9-
func (s *ModuleSuite) TestModuleValidateMissingURL(c *C) {
10-
m := &Module{Path: "foo"}
9+
func (s *ModulesSuite) TestValidateMissingURL(c *C) {
10+
m := &Submodule{Path: "foo"}
1111
c.Assert(m.Validate(), Equals, ErrModuleEmptyURL)
1212
}
1313

14-
func (s *ModuleSuite) TestModuleValidateMissingName(c *C) {
15-
m := &Module{URL: "bar"}
14+
func (s *ModulesSuite) TestValidateMissingName(c *C) {
15+
m := &Submodule{URL: "bar"}
1616
c.Assert(m.Validate(), Equals, ErrModuleEmptyPath)
1717
}
1818

19-
func (s *ModuleSuite) TestModuleValidateDefault(c *C) {
20-
m := &Module{Path: "foo", URL: "http://foo/bar"}
21-
c.Assert(m.Validate(), IsNil)
22-
c.Assert(m.Branch, Equals, DefaultModuleBranch)
19+
func (s *ModulesSuite) TestMarshall(c *C) {
20+
input := []byte(`[submodule "qux"]
21+
path = qux
22+
url = baz
23+
branch = bar
24+
`)
25+
26+
cfg := NewModules()
27+
cfg.Submodules["qux"] = &Submodule{Path: "qux", URL: "baz", Branch: "bar"}
28+
29+
output, err := cfg.Marshal()
30+
c.Assert(err, IsNil)
31+
c.Assert(output, DeepEquals, input)
32+
}
33+
34+
func (s *ModulesSuite) TestUnmarshall(c *C) {
35+
input := []byte(`[submodule "qux"]
36+
path = qux
37+
url = https://github.com/foo/qux.git
38+
[submodule "foo/bar"]
39+
path = foo/bar
40+
url = https://github.com/foo/bar.git
41+
branch = dev
42+
`)
43+
44+
cfg := NewModules()
45+
err := cfg.Unmarshal(input)
46+
c.Assert(err, IsNil)
47+
48+
c.Assert(cfg.Submodules, HasLen, 2)
49+
c.Assert(cfg.Submodules["qux"].Name, Equals, "qux")
50+
c.Assert(cfg.Submodules["qux"].URL, Equals, "https://github.com/foo/qux.git")
51+
c.Assert(cfg.Submodules["foo/bar"].Name, Equals, "foo/bar")
52+
c.Assert(cfg.Submodules["foo/bar"].URL, Equals, "https://github.com/foo/bar.git")
53+
c.Assert(cfg.Submodules["foo/bar"].Branch, Equals, "dev")
54+
}
55+
56+
func (s *ModulesSuite) TestUnmarshallMarshall(c *C) {
57+
input := []byte(`[submodule "qux"]
58+
path = qux
59+
url = https://github.com/foo/qux.git
60+
[submodule "foo/bar"]
61+
path = foo/bar
62+
url = https://github.com/foo/bar.git
63+
ignore = all
64+
`)
65+
66+
cfg := NewModules()
67+
err := cfg.Unmarshal(input)
68+
c.Assert(err, IsNil)
69+
70+
output, err := cfg.Marshal()
71+
c.Assert(err, IsNil)
72+
c.Assert(output, DeepEquals, input)
2373
}

0 commit comments

Comments
 (0)