Skip to content

Commit 51ed151

Browse files
authored
plumbing: packp, Avoid duplicate encoding when overriding a Capability 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.
1 parent d875162 commit 51ed151

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

plumbing/protocol/packp/capability/list.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func (l *List) Get(capability Capability) []string {
8686

8787
// Set sets a capability removing the previous values
8888
func (l *List) Set(capability Capability, values ...string) error {
89-
delete(l.m, capability)
89+
if _, ok := l.m[capability]; ok {
90+
l.m[capability].Values = l.m[capability].Values[:0]
91+
}
9092
return l.Add(capability, values...)
9193
}
9294

plumbing/protocol/packp/capability/list_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ func (s *SuiteCapabilities) TestSetEmpty(c *check.C) {
122122
c.Assert(cap.Get(Agent), check.HasLen, 1)
123123
}
124124

125+
func (s *SuiteCapabilities) TestSetDuplicate(c *check.C) {
126+
cap := NewList()
127+
err := cap.Set(Agent, "baz")
128+
c.Assert(err, check.IsNil)
129+
130+
err = cap.Set(Agent, "bar")
131+
c.Assert(err, check.IsNil)
132+
133+
c.Assert(cap.String(), check.Equals, "agent=bar")
134+
}
135+
125136
func (s *SuiteCapabilities) TestGetEmpty(c *check.C) {
126137
cap := NewList()
127138
c.Assert(cap.Get(Agent), check.HasLen, 0)

0 commit comments

Comments
 (0)