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

capabilities: full integration #151

Merged
merged 9 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 7 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func (c *MockFetchPackSession) AdvertisedReferences() (*packp.AdvRefs, error) {
func (c *MockFetchPackSession) FetchPack(
r *packp.UploadPackRequest) (io.ReadCloser, error) {

if !r.Capabilities.Supports(capability.Agent) {
return nil, fmt.Errorf("" +
"invalid test rquest, missing Agent capability, the request" +
"should be created using NewUploadPackRequestFromCapabilities",
)
}

f := fixtures.ByURL(c.endpoint.String())

if len(r.Wants) == 1 {
Expand Down
7 changes: 4 additions & 3 deletions plumbing/format/pktline/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ func (s *Scanner) Bytes() []byte {
// pkt-len and substracting the pkt-len size.
func (s *Scanner) readPayloadLen() (int, error) {
if _, err := io.ReadFull(s.r, s.len[:]); err != nil {
if err == io.EOF {
return 0, err
if err == io.ErrUnexpectedEOF {
return 0, ErrInvalidPktLen
}
return 0, ErrInvalidPktLen

return 0, err
}

n, err := hexDecode(s.len)
Expand Down
11 changes: 11 additions & 0 deletions plumbing/format/pktline/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pktline_test

import (
"bytes"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -145,6 +146,16 @@ func (s *SuiteScanner) TestEOF(c *C) {
c.Assert(sc.Err(), IsNil)
}

type mockReader struct{}

func (r *mockReader) Read([]byte) (int, error) { return 0, errors.New("foo") }

func (s *SuiteScanner) TestInternalReadError(c *C) {
sc := pktline.NewScanner(&mockReader{})
c.Assert(sc.Scan(), Equals, false)
c.Assert(sc.Err(), ErrorMatches, "foo")
}

// A section are several non flush-pkt lines followed by a flush-pkt, which
// how the git protocol sends long messages.
func (s *SuiteScanner) TestReadSomeSections(c *C) {
Expand Down
98 changes: 80 additions & 18 deletions plumbing/protocol/packp/advrefs_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,89 @@
package packp_test
package packp

import (
"bytes"
"fmt"
"io"
"strings"
"testing"

"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/pktline"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }
type AdvRefSuite struct{}

type SuiteDecodeEncode struct{}
var _ = Suite(&AdvRefSuite{})

var _ = Suite(&SuiteDecodeEncode{})
func (s *AdvRefSuite) TestAddReferenceSymbolic(c *C) {
ref := plumbing.NewSymbolicReference("foo", "bar")

func (s *SuiteDecodeEncode) test(c *C, in []string, exp []string) {
a := NewAdvRefs()
err := a.AddReference(ref)
c.Assert(err, IsNil)

values := a.Capabilities.Get(capability.SymRef)
c.Assert(values, HasLen, 1)
c.Assert(values[0], Equals, "foo:bar")
}

func (s *AdvRefSuite) TestAddReferenceHash(c *C) {
ref := plumbing.NewHashReference("foo", plumbing.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c"))

a := NewAdvRefs()
err := a.AddReference(ref)
c.Assert(err, IsNil)

c.Assert(a.References, HasLen, 1)
c.Assert(a.References["foo"].String(), Equals, "5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c")
}

func (s *AdvRefSuite) TestAllReferences(c *C) {
hash := plumbing.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c")

a := NewAdvRefs()
err := a.AddReference(plumbing.NewSymbolicReference("foo", "bar"))
c.Assert(err, IsNil)
err = a.AddReference(plumbing.NewHashReference("bar", hash))
c.Assert(err, IsNil)

refs, err := a.AllReferences()
c.Assert(err, IsNil)

iter, err := refs.IterReferences()
c.Assert(err, IsNil)

var count int
iter.ForEach(func(ref *plumbing.Reference) error {
count++
switch ref.Name() {
case "bar":
c.Assert(ref.Hash(), Equals, hash)
case "foo":
c.Assert(ref.Target().String(), Equals, "bar")
}
return nil
})

c.Assert(count, Equals, 2)
}

func (s *AdvRefSuite) TestAllReferencesBadSymref(c *C) {
a := NewAdvRefs()
err := a.Capabilities.Set(capability.SymRef, "foo")
c.Assert(err, IsNil)

_, err = a.AllReferences()
c.Assert(err, NotNil)
}

type AdvRefsDecodeEncodeSuite struct{}

var _ = Suite(&AdvRefsDecodeEncodeSuite{})

func (s *AdvRefsDecodeEncodeSuite) test(c *C, in []string, exp []string) {
var err error
var input io.Reader
{
Expand All @@ -44,7 +106,7 @@ func (s *SuiteDecodeEncode) test(c *C, in []string, exp []string) {

var obtained []byte
{
ar := packp.NewAdvRefs()
ar := NewAdvRefs()
c.Assert(ar.Decode(input), IsNil)

var buf bytes.Buffer
Expand All @@ -56,7 +118,7 @@ func (s *SuiteDecodeEncode) test(c *C, in []string, exp []string) {
c.Assert(string(obtained), DeepEquals, string(expected))
}

func (s *SuiteDecodeEncode) TestNoHead(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestNoHead(c *C) {
input := []string{
"0000000000000000000000000000000000000000 capabilities^{}\x00",
pktline.FlushString,
Expand All @@ -70,7 +132,7 @@ func (s *SuiteDecodeEncode) TestNoHead(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestNoHeadSmart(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestNoHeadSmart(c *C) {
input := []string{
"# service=git-upload-pack\n",
"0000000000000000000000000000000000000000 capabilities^{}\x00",
Expand All @@ -86,7 +148,7 @@ func (s *SuiteDecodeEncode) TestNoHeadSmart(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestNoHeadSmartBug(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestNoHeadSmartBug(c *C) {
input := []string{
"# service=git-upload-pack\n",
pktline.FlushString,
Expand All @@ -104,7 +166,7 @@ func (s *SuiteDecodeEncode) TestNoHeadSmartBug(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestRefs(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestRefs(c *C) {
input := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:/refs/heads/master ofs-delta multi_ack",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master",
Expand All @@ -124,7 +186,7 @@ func (s *SuiteDecodeEncode) TestRefs(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestPeeled(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestPeeled(c *C) {
input := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:/refs/heads/master ofs-delta multi_ack",
"7777777777777777777777777777777777777777 refs/tags/v2.6.12-tree\n",
Expand All @@ -148,7 +210,7 @@ func (s *SuiteDecodeEncode) TestPeeled(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestAll(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestAll(c *C) {
input := []string{
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:/refs/heads/master ofs-delta multi_ack\n",
"a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n",
Expand Down Expand Up @@ -176,7 +238,7 @@ func (s *SuiteDecodeEncode) TestAll(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestAllSmart(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestAllSmart(c *C) {
input := []string{
"# service=git-upload-pack\n",
pktline.FlushString,
Expand Down Expand Up @@ -208,7 +270,7 @@ func (s *SuiteDecodeEncode) TestAllSmart(c *C) {
s.test(c, input, expected)
}

func (s *SuiteDecodeEncode) TestAllSmartBug(c *C) {
func (s *AdvRefsDecodeEncodeSuite) TestAllSmartBug(c *C) {
input := []string{
"# service=git-upload-pack\n",
pktline.FlushString,
Expand Down Expand Up @@ -254,7 +316,7 @@ func ExampleDecoder_Decode() {
input := strings.NewReader(raw)

// Decode the input into a newly allocated AdvRefs value.
ar := packp.NewAdvRefs()
ar := NewAdvRefs()
_ = ar.Decode(input) // error check ignored for brevity

// Do something interesting with the AdvRefs, e.g. print its contents.
Expand All @@ -270,7 +332,7 @@ func ExampleDecoder_Decode() {

func ExampleEncoder_Encode() {
// Create an AdvRefs with the contents you want...
ar := packp.NewAdvRefs()
ar := NewAdvRefs()

// ...add a hash for the HEAD...
head := plumbing.NewHash("1111111111111111111111111111111111111111")
Expand Down
17 changes: 17 additions & 0 deletions plumbing/protocol/packp/capability/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ func (l *List) Supports(capability Capability) bool {
return ok
}

// Delete deletes a capability from the List
func (l *List) Delete(capability Capability) {
if !l.Supports(capability) {
return
}

delete(l.m, capability)
for i, c := range l.sort {
if c != string(capability) {
continue
}

l.sort = append(l.sort[:i], l.sort[i+1:]...)
return
}
}

// String generates the capabilities strings, the capabilities are sorted in
// insertion order
func (l *List) String() string {
Expand Down
16 changes: 16 additions & 0 deletions plumbing/protocol/packp/capability/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ func (s *SuiteCapabilities) TestGetEmpty(c *check.C) {
c.Assert(cap.Get(Agent), check.HasLen, 0)
}

func (s *SuiteCapabilities) TestDelete(c *check.C) {
cap := NewList()
cap.Delete(SymRef)

err := cap.Add(Sideband)
c.Assert(err, check.IsNil)
err = cap.Set(SymRef, "bar")
c.Assert(err, check.IsNil)
err = cap.Set(Sideband64k)
c.Assert(err, check.IsNil)

cap.Delete(SymRef)

c.Assert(cap.String(), check.Equals, "side-band side-band-64k")
}

func (s *SuiteCapabilities) TestAdd(c *check.C) {
cap := NewList()
err := cap.Add(SymRef, "foo", "qux")
Expand Down
Loading