From 1e70916ca7e4d5c0ad00edbfd1877e06d7587fc6 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Thu, 27 Jul 2017 17:16:34 +0200 Subject: format/config: add GoString This is more convenient for testing and debugging. --- plumbing/format/config/option.go | 10 ++++++++++ plumbing/format/config/section.go | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) (limited to 'plumbing/format/config') diff --git a/plumbing/format/config/option.go b/plumbing/format/config/option.go index 3c391c6..481af02 100644 --- a/plumbing/format/config/option.go +++ b/plumbing/format/config/option.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "strings" ) @@ -21,6 +22,15 @@ func (o *Option) IsKey(key string) bool { return strings.ToLower(o.Key) == strings.ToLower(key) } +func (opts Options) GoString() string { + var strs []string + for _, opt := range opts { + strs = append(strs, fmt.Sprintf("%#v", opt)) + } + + return strings.Join(strs, ", ") +} + // Get gets the value for the given key if set, // otherwise it returns the empty string. // diff --git a/plumbing/format/config/section.go b/plumbing/format/config/section.go index 547da1e..eeefe84 100644 --- a/plumbing/format/config/section.go +++ b/plumbing/format/config/section.go @@ -1,6 +1,9 @@ package config -import "strings" +import ( + "fmt" + "strings" +) // Section is the representation of a section inside git configuration files. // Each Section contains Options that are used by both the Git plumbing @@ -36,8 +39,26 @@ type Subsection struct { type Sections []*Section +func (s Sections) GoString() string { + var strs []string + for _, ss := range s { + strs = append(strs, fmt.Sprintf("%#v", ss)) + } + + return strings.Join(strs, ", ") +} + type Subsections []*Subsection +func (s Subsections) GoString() string { + var strs []string + for _, ss := range s { + strs = append(strs, fmt.Sprintf("%#v", ss)) + } + + return strings.Join(strs, ", ") +} + // IsName checks if the name provided is equals to the Section name, case insensitive. func (s *Section) IsName(name string) bool { return strings.ToLower(s.Name) == strings.ToLower(name) -- cgit From e5c6fa237776870483cbe227d7f7ea943f35cb12 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Thu, 27 Jul 2017 17:17:34 +0200 Subject: 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. --- plumbing/format/config/decoder_test.go | 5 +++- plumbing/format/config/fixtures_test.go | 14 +++++++++++ plumbing/format/config/option.go | 41 +++++++++++++++++++++++++-------- plumbing/format/config/section.go | 4 ++-- plumbing/format/config/section_test.go | 19 +++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) (limited to 'plumbing/format/config') diff --git a/plumbing/format/config/decoder_test.go b/plumbing/format/config/decoder_test.go index 412549f..0a8e92c 100644 --- a/plumbing/format/config/decoder_test.go +++ b/plumbing/format/config/decoder_test.go @@ -17,7 +17,10 @@ func (s *DecoderSuite) TestDecode(c *C) { cfg := &Config{} err := d.Decode(cfg) c.Assert(err, IsNil, Commentf("decoder error for fixture: %d", idx)) - c.Assert(cfg, DeepEquals, fixture.Config, Commentf("bad result for fixture: %d", idx)) + buf := bytes.NewBuffer(nil) + e := NewEncoder(buf) + _ = e.Encode(cfg) + c.Assert(cfg, DeepEquals, fixture.Config, Commentf("bad result for fixture: %d, %s", idx, buf.String())) } } diff --git a/plumbing/format/config/fixtures_test.go b/plumbing/format/config/fixtures_test.go index 12ff288..f3533df 100644 --- a/plumbing/format/config/fixtures_test.go +++ b/plumbing/format/config/fixtures_test.go @@ -87,4 +87,18 @@ var fixtures = []*Fixture{ AddOption("sect1", "subsect1", "opt2", "value2b"). AddOption("sect1", "subsect2", "opt2", "value2"), }, + { + Raw: ` + [sect1] + opt1 = value1 + opt1 = value2 + `, + Text: `[sect1] + opt1 = value1 + opt1 = value2 +`, + Config: New(). + AddOption("sect1", "", "opt1", "value1"). + AddOption("sect1", "", "opt1", "value2"), + }, } diff --git a/plumbing/format/config/option.go b/plumbing/format/config/option.go index 481af02..d4775e4 100644 --- a/plumbing/format/config/option.go +++ b/plumbing/format/config/option.go @@ -79,16 +79,39 @@ func (opts Options) withAddedOption(key string, value string) Options { return append(opts, &Option{key, value}) } -func (opts Options) withSettedOption(key string, value string) Options { - for i := len(opts) - 1; i >= 0; i-- { - o := opts[i] - if o.IsKey(key) { - result := make(Options, len(opts)) - copy(result, opts) - result[i] = &Option{key, value} - return result +func (opts Options) withSettedOption(key string, values ...string) Options { + var result Options + var added []string + for _, o := range opts { + if !o.IsKey(key) { + result = append(result, o) + continue + } + + if contains(values, o.Value) { + added = append(added, o.Value) + result = append(result, o) + continue + } + } + + for _, value := range values { + if contains(added, value) { + continue + } + + result = result.withAddedOption(key, value) + } + + return result +} + +func contains(haystack []string, needle string) bool { + for _, s := range haystack { + if s == needle { + return true } } - return opts.withAddedOption(key, value) + return false } diff --git a/plumbing/format/config/section.go b/plumbing/format/config/section.go index eeefe84..4a17e3b 100644 --- a/plumbing/format/config/section.go +++ b/plumbing/format/config/section.go @@ -134,8 +134,8 @@ func (s *Subsection) AddOption(key string, value string) *Subsection { // SetOption adds a new Option to the Subsection. If the option already exists, is replaced. // The updated Subsection is returned. -func (s *Subsection) SetOption(key string, value string) *Subsection { - s.Options = s.Options.withSettedOption(key, value) +func (s *Subsection) SetOption(key string, value ...string) *Subsection { + s.Options = s.Options.withSettedOption(key, value...) return s } diff --git a/plumbing/format/config/section_test.go b/plumbing/format/config/section_test.go index cfd9f3f..0290386 100644 --- a/plumbing/format/config/section_test.go +++ b/plumbing/format/config/section_test.go @@ -69,3 +69,22 @@ func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { } c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) } + +func (s *SectionSuite) TestSubsection_SetOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + + expected := &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value4"}, + }, + } + c.Assert(sect.SetOption("key1", "value1", "value4"), DeepEquals, expected) +} -- cgit