diff options
author | Santiago M. Mola <santi@mola.io> | 2017-07-27 17:17:34 +0200 |
---|---|---|
committer | Santiago M. Mola <santi@mola.io> | 2017-08-01 13:01:59 +0200 |
commit | e5c6fa237776870483cbe227d7f7ea943f35cb12 (patch) | |
tree | d8708626c6e4bdacdf3d3b796a17025908b6606b /config | |
parent | 1e70916ca7e4d5c0ad00edbfd1877e06d7587fc6 (diff) | |
download | go-git-e5c6fa237776870483cbe227d7f7ea943f35cb12.tar.gz |
config: preserve option order on config marshalling
Do not change order of options (e.g. in RemoteConfig)
when serializing for any option whose value has not changed.
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 38 | ||||
-rw-r--r-- | config/config_test.go | 2 |
2 files changed, 28 insertions, 12 deletions
diff --git a/config/config.go b/config/config.go index 1f3cd77..cb10738 100644 --- a/config/config.go +++ b/config/config.go @@ -159,13 +159,22 @@ func (c *Config) marshalCore() { func (c *Config) marshalRemotes() { s := c.Raw.Section(remoteSection) - s.Subsections = make(format.Subsections, len(c.Remotes)) + newSubsections := make(format.Subsections, 0, len(c.Remotes)) + added := make(map[string]bool) + for _, subsection := range s.Subsections { + if remote, ok := c.Remotes[subsection.Name]; ok { + newSubsections = append(newSubsections, remote.marshal()) + added[subsection.Name] = true + } + } - var i int - for _, r := range c.Remotes { - s.Subsections[i] = r.marshal() - i++ + for name, remote := range c.Remotes { + if !added[name] { + newSubsections = append(newSubsections, remote.marshal()) + } } + + s.Subsections = newSubsections } func (c *Config) marshalSubmodules() { @@ -252,14 +261,21 @@ func (c *RemoteConfig) marshal() *format.Subsection { } c.raw.Name = c.Name - c.raw.RemoveOption(urlKey) - for _, url := range c.URLs { - c.raw.AddOption(urlKey, url) + if len(c.URLs) == 0 { + c.raw.RemoveOption(urlKey) + } else { + c.raw.SetOption(urlKey, c.URLs...) } - c.raw.RemoveOption(fetchKey) - for _, rs := range c.Fetch { - c.raw.AddOption(fetchKey, rs.String()) + if len(c.Fetch) == 0 { + c.raw.RemoveOption(fetchKey) + } else { + var values []string + for _, rs := range c.Fetch { + values = append(values, rs.String()) + } + + c.raw.SetOption(fetchKey, values...) } return c.raw diff --git a/config/config_test.go b/config/config_test.go index e958677..97f4bbf 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -107,7 +107,7 @@ func (s *ConfigSuite) TestUnmarshallMarshall(c *C) { output, err := cfg.Marshal() c.Assert(err, IsNil) - c.Assert(output, DeepEquals, input) + c.Assert(string(output), DeepEquals, string(input)) } func (s *ConfigSuite) TestValidateInvalidRemote(c *C) { |