Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plumbing/protocol/packp/capability/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const (

const DefaultAgent = "go-git/4.x"

var valid = map[Capability]bool{
var known = map[Capability]bool{
MultiACK: true, MultiACKDetailed: true, NoDone: true, ThinPack: true,
Sideband: true, Sideband64k: true, OFSDelta: true, Agent: true,
Shallow: true, DeepenSince: true, DeepenNot: true, DeepenRelative: true,
Expand Down
23 changes: 14 additions & 9 deletions plumbing/protocol/packp/capability/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,27 @@ func (l *List) Add(c Capability, values ...string) error {
return nil
}

if !multipleArgument[c] && len(l.m[c].Values) > 0 {
if known[c] && !multipleArgument[c] && len(l.m[c].Values) > 0 {
return ErrMultipleArguments
}

l.m[c].Values = append(l.m[c].Values, values...)
return nil
}

func (l *List) validateNoEmptyArgs(values []string) error {
for _, v := range values {
if v == "" {
return ErrEmtpyArgument
}
}
return nil
}

func (l *List) validate(c Capability, values []string) error {
if !known[c] {
return l.validateNoEmptyArgs(values)
}
if requiresArgument[c] && len(values) == 0 {
return ErrArgumentsRequired
}
Expand All @@ -128,14 +140,7 @@ func (l *List) validate(c Capability, values []string) error {
if !multipleArgument[c] && len(values) > 1 {
return ErrMultipleArguments
}

for _, v := range values {
if v == "" {
return ErrEmtpyArgument
}
}

return nil
return l.validateNoEmptyArgs(values)
}

// Supports returns true if capability is present
Expand Down
22 changes: 21 additions & 1 deletion plumbing/protocol/packp/capability/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ func (s *SuiteCapabilities) TestDecodeWithUnknownCapability(c *check.C) {
c.Assert(cap.Supports(Capability("foo")), check.Equals, true)
}

func (s *SuiteCapabilities) TestDecodeWithUnknownCapabilityWithArgument(c *check.C) {
cap := NewList()
err := cap.Decode([]byte("oldref=HEAD:refs/heads/v2 thin-pack"))
c.Assert(err, check.IsNil)

c.Assert(cap.m, check.HasLen, 2)
c.Assert(cap.Get("oldref"), check.DeepEquals, []string{"HEAD:refs/heads/v2"})
c.Assert(cap.Get(ThinPack), check.IsNil)
}

func (s *SuiteCapabilities) TestDecodeWithUnknownCapabilityWithMultipleArgument(c *check.C) {
cap := NewList()
err := cap.Decode([]byte("foo=HEAD:refs/heads/v2 foo=HEAD:refs/heads/v1 thin-pack"))
c.Assert(err, check.IsNil)

c.Assert(cap.m, check.HasLen, 2)
c.Assert(cap.Get("foo"), check.DeepEquals, []string{"HEAD:refs/heads/v2", "HEAD:refs/heads/v1"})
c.Assert(cap.Get(ThinPack), check.IsNil)
}

func (s *SuiteCapabilities) TestString(c *check.C) {
cap := NewList()
cap.Set(Agent, "bar")
Expand Down Expand Up @@ -153,7 +173,7 @@ func (s *SuiteCapabilities) TestAddErrArgumentsNotAllowed(c *check.C) {
c.Assert(err, check.Equals, ErrArguments)
}

func (s *SuiteCapabilities) TestAddErrArgumendts(c *check.C) {
func (s *SuiteCapabilities) TestAddErrArguments(c *check.C) {
cap := NewList()
err := cap.Add(SymRef, "")
c.Assert(err, check.Equals, ErrEmtpyArgument)
Expand Down