Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit b054b98

Browse files
smolamcuadros
authored andcommitted
capability: support empty input on Decode. (#153)
Calling capability.List's Decode with nil input will have no effect. This is useful in other decoders, where an empty capability list is received as an empty byte slice.
1 parent 5cdb09c commit b054b98

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

plumbing/protocol/packp/advrefs_decode.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,6 @@ func decodeFirstRef(l *advRefsDecoder) decoderStateFn {
197197
}
198198

199199
func decodeCaps(p *advRefsDecoder) decoderStateFn {
200-
if len(p.line) == 0 {
201-
return decodeOtherRefs
202-
}
203-
204200
if err := p.data.Capabilities.Decode(p.line); err != nil {
205201
p.error("invalid capabilities: %s", err)
206202
}

plumbing/protocol/packp/capability/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (l *List) IsEmpty() bool {
4848

4949
// Decode decodes list of capabilities from raw into the list
5050
func (l *List) Decode(raw []byte) error {
51+
if len(raw) == 0 {
52+
return nil
53+
}
54+
5155
for _, data := range bytes.Split(raw, []byte{' '}) {
5256
pair := bytes.SplitN(data, []byte{'='}, 2)
5357

plumbing/protocol/packp/capability/list_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func (s *SuiteCapabilities) TestDecode(c *check.C) {
2727
c.Assert(cap.Get(ThinPack), check.IsNil)
2828
}
2929

30+
func (s *SuiteCapabilities) TestDecodeEmpty(c *check.C) {
31+
cap := NewList()
32+
err := cap.Decode(nil)
33+
c.Assert(err, check.IsNil)
34+
c.Assert(cap, check.DeepEquals, NewList())
35+
}
36+
3037
func (s *SuiteCapabilities) TestDecodeWithErrArguments(c *check.C) {
3138
cap := NewList()
3239
err := cap.Decode([]byte("thin-pack=foo"))

plumbing/protocol/packp/ulreq_decode.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ func (d *ulReqDecoder) readHash() (plumbing.Hash, bool) {
110110

111111
// Expected format: sp cap1 sp cap2 sp cap3...
112112
func (d *ulReqDecoder) decodeCaps() stateFn {
113-
if len(d.line) == 0 {
114-
return d.decodeOtherWants
115-
}
116-
117113
d.line = bytes.TrimPrefix(d.line, sp)
118114
if err := d.data.Capabilities.Decode(d.line); err != nil {
119115
d.error("invalid capabilities: %s", err)

0 commit comments

Comments
 (0)