Skip to content

Commit

Permalink
plumbing: packp, Avoid duplicate encoding when overriding a Capabilit…
Browse files Browse the repository at this point in the history
…y value. (go-git#521)

Previously, calling `Set($CAPABILITY, ...)` on a `capability.List` where `$CAPABILITY`
was already present would correctly replace the existing value of that capability, but
would also result in that capability being listed twice in the internal `l.sort` slice.
This manifested publicly when the `List` was encoded as the same capability appearing
twice with the same value in the encoded output.
  • Loading branch information
tylerchr authored and rohankmr414 committed Oct 12, 2022
1 parent d2bf2a7 commit df659a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plumbing/protocol/packp/capability/list.go
Expand Up @@ -86,7 +86,9 @@ func (l *List) Get(capability Capability) []string {

// Set sets a capability removing the previous values
func (l *List) Set(capability Capability, values ...string) error {
delete(l.m, capability)
if _, ok := l.m[capability]; ok {
l.m[capability].Values = l.m[capability].Values[:0]
}
return l.Add(capability, values...)
}

Expand Down
11 changes: 11 additions & 0 deletions plumbing/protocol/packp/capability/list_test.go
Expand Up @@ -122,6 +122,17 @@ func (s *SuiteCapabilities) TestSetEmpty(c *check.C) {
c.Assert(cap.Get(Agent), check.HasLen, 1)
}

func (s *SuiteCapabilities) TestSetDuplicate(c *check.C) {
cap := NewList()
err := cap.Set(Agent, "baz")
c.Assert(err, check.IsNil)

err = cap.Set(Agent, "bar")
c.Assert(err, check.IsNil)

c.Assert(cap.String(), check.Equals, "agent=bar")
}

func (s *SuiteCapabilities) TestGetEmpty(c *check.C) {
cap := NewList()
c.Assert(cap.Get(Agent), check.HasLen, 0)
Expand Down

0 comments on commit df659a1

Please sign in to comment.