diff options
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/format/config/decoder_test.go | 5 | ||||
-rw-r--r-- | plumbing/format/config/fixtures_test.go | 14 | ||||
-rw-r--r-- | plumbing/format/config/option.go | 51 | ||||
-rw-r--r-- | plumbing/format/config/section.go | 27 | ||||
-rw-r--r-- | plumbing/format/config/section_test.go | 19 | ||||
-rw-r--r-- | plumbing/reference.go | 40 | ||||
-rw-r--r-- | plumbing/reference_test.go | 8 | ||||
-rw-r--r-- | plumbing/transport/file/receive_pack_test.go | 2 | ||||
-rw-r--r-- | plumbing/transport/test/receive_pack.go | 1 |
9 files changed, 129 insertions, 38 deletions
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 3c391c6..d4775e4 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. // @@ -69,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 547da1e..4a17e3b 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) @@ -113,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) +} diff --git a/plumbing/reference.go b/plumbing/reference.go index 5d477b9..2c30fe0 100644 --- a/plumbing/reference.go +++ b/plumbing/reference.go @@ -55,6 +55,26 @@ func (r ReferenceType) String() string { // ReferenceName reference name's type ReferenceName string +// IsBranch check if a reference is a branch +func (r ReferenceName) IsBranch() bool { + return strings.HasPrefix(string(r), refHeadPrefix) +} + +// IsNote check if a reference is a note +func (r ReferenceName) IsNote() bool { + return strings.HasPrefix(string(r), refNotePrefix) +} + +// IsRemote check if a reference is a remote +func (r ReferenceName) IsRemote() bool { + return strings.HasPrefix(string(r), refRemotePrefix) +} + +// IsTag check if a reference is a tag +func (r ReferenceName) IsTag() bool { + return strings.HasPrefix(string(r), refTagPrefix) +} + func (r ReferenceName) String() string { return string(r) } @@ -138,26 +158,6 @@ func (r *Reference) Target() ReferenceName { return r.target } -// IsBranch check if a reference is a branch -func (r *Reference) IsBranch() bool { - return strings.HasPrefix(string(r.n), refHeadPrefix) -} - -// IsNote check if a reference is a note -func (r *Reference) IsNote() bool { - return strings.HasPrefix(string(r.n), refNotePrefix) -} - -// IsRemote check if a reference is a remote -func (r *Reference) IsRemote() bool { - return strings.HasPrefix(string(r.n), refRemotePrefix) -} - -// IsTag check if a reference is a tag -func (r *Reference) IsTag() bool { - return strings.HasPrefix(string(r.n), refTagPrefix) -} - // Strings dump a reference as a [2]string func (r *Reference) Strings() [2]string { var o [2]string diff --git a/plumbing/reference_test.go b/plumbing/reference_test.go index 97c8772..47919ef 100644 --- a/plumbing/reference_test.go +++ b/plumbing/reference_test.go @@ -55,21 +55,21 @@ func (s *ReferenceSuite) TestNewHashReference(c *C) { } func (s *ReferenceSuite) TestIsBranch(c *C) { - r := NewHashReference(ExampleReferenceName, ZeroHash) + r := ExampleReferenceName c.Assert(r.IsBranch(), Equals, true) } func (s *ReferenceSuite) TestIsNote(c *C) { - r := NewHashReference(ReferenceName("refs/notes/foo"), ZeroHash) + r := ReferenceName("refs/notes/foo") c.Assert(r.IsNote(), Equals, true) } func (s *ReferenceSuite) TestIsRemote(c *C) { - r := NewHashReference(ReferenceName("refs/remotes/origin/master"), ZeroHash) + r := ReferenceName("refs/remotes/origin/master") c.Assert(r.IsRemote(), Equals, true) } func (s *ReferenceSuite) TestIsTag(c *C) { - r := NewHashReference(ReferenceName("refs/tags/v3.1."), ZeroHash) + r := ReferenceName("refs/tags/v3.1.") c.Assert(r.IsTag(), Equals, true) } diff --git a/plumbing/transport/file/receive_pack_test.go b/plumbing/transport/file/receive_pack_test.go index ab21aea..a7dc399 100644 --- a/plumbing/transport/file/receive_pack_test.go +++ b/plumbing/transport/file/receive_pack_test.go @@ -70,6 +70,6 @@ func (s *ReceivePackSuite) TestNonExistentCommand(c *C) { cmd := "/non-existent-git" client := NewClient(cmd, cmd) session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) - c.Assert(err, ErrorMatches, ".*no such file or directory.*") + c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.") c.Assert(session, IsNil) } diff --git a/plumbing/transport/test/receive_pack.go b/plumbing/transport/test/receive_pack.go index 6309ef0..d29d9ca 100644 --- a/plumbing/transport/test/receive_pack.go +++ b/plumbing/transport/test/receive_pack.go @@ -61,6 +61,7 @@ func (s *ReceivePackSuite) TestAdvertisedReferencesNotExists(c *C) { func (s *ReceivePackSuite) TestCallAdvertisedReferenceTwice(c *C) { r, err := s.Client.NewReceivePackSession(s.Endpoint, s.EmptyAuth) + defer func() { c.Assert(r.Close(), IsNil) }() c.Assert(err, IsNil) ar1, err := r.AdvertisedReferences() c.Assert(err, IsNil) |