Пример #1
0
func testEncode(c *C, input *advrefs.AdvRefs, expected []byte) {
	var buf bytes.Buffer
	e := advrefs.NewEncoder(&buf)
	err := e.Encode(input)
	c.Assert(err, IsNil)
	obtained := buf.Bytes()

	comment := Commentf("\nobtained = %s\nexpected = %s\n", string(obtained), string(expected))

	c.Assert(obtained, DeepEquals, expected, comment)
}
Пример #2
0
func (s *SuiteEncoder) TestErrorTooLong(c *C) {
	references := map[string]plumbing.Hash{
		strings.Repeat("a", pktline.MaxPayloadSize): plumbing.NewHash("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7"),
	}
	ar := &advrefs.AdvRefs{
		References: references,
	}

	var buf bytes.Buffer
	e := advrefs.NewEncoder(&buf)
	err := e.Encode(ar)
	c.Assert(err, ErrorMatches, ".*payload is too long.*")
}
Пример #3
0
func (s *SuiteDecodeEncode) test(c *C, in []string, exp []string) {
	var err error
	var input io.Reader
	{
		var buf bytes.Buffer
		p := pktline.NewEncoder(&buf)
		err = p.EncodeString(in...)
		c.Assert(err, IsNil)
		input = &buf
	}

	var expected []byte
	{
		var buf bytes.Buffer
		p := pktline.NewEncoder(&buf)
		err = p.EncodeString(exp...)
		c.Assert(err, IsNil)

		expected = buf.Bytes()
	}

	var obtained []byte
	{
		ar := advrefs.New()
		d := advrefs.NewDecoder(input)
		err = d.Decode(ar)
		c.Assert(err, IsNil)

		var buf bytes.Buffer
		e := advrefs.NewEncoder(&buf)
		err := e.Encode(ar)
		c.Assert(err, IsNil)

		obtained = buf.Bytes()
	}

	c.Assert(obtained, DeepEquals, expected,
		Commentf("input = %v\nobtained = %q\nexpected = %q\n",
			in, string(obtained), string(expected)))
}
Пример #4
0
func ExampleEncoder_Encode() {
	// Create an AdvRefs with the contents you want...
	ar := advrefs.New()

	// ...add a hash for the HEAD...
	head := plumbing.NewHash("1111111111111111111111111111111111111111")
	ar.Head = &head

	// ...add some server capabilities...
	ar.Capabilities.Add("symref", "HEAD:/refs/heads/master")
	ar.Capabilities.Add("ofs-delta")
	ar.Capabilities.Add("multi_ack")

	// ...add a couple of references...
	ar.References["refs/heads/master"] = plumbing.NewHash("2222222222222222222222222222222222222222")
	ar.References["refs/tags/v1"] = plumbing.NewHash("3333333333333333333333333333333333333333")

	// ...including a peeled ref...
	ar.Peeled["refs/tags/v1"] = plumbing.NewHash("4444444444444444444444444444444444444444")

	// ...and finally add a shallow
	ar.Shallows = append(ar.Shallows, plumbing.NewHash("5555555555555555555555555555555555555555"))

	// Encode the advrefs.Contents to a bytes.Buffer.
	// You can encode into stdout too, but you will not be able
	// see the '\x00' after "HEAD".
	var buf bytes.Buffer
	e := advrefs.NewEncoder(&buf)
	_ = e.Encode(ar) // error checks ignored for brevity

	// Print the contents of the buffer as a quoted string.
	// Printing is as a non-quoted string will be prettier but you
	// will miss the '\x00' after "HEAD".
	fmt.Printf("%q", buf.String())
	// Output:
	// "00651111111111111111111111111111111111111111 HEAD\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n003f2222222222222222222222222222222222222222 refs/heads/master\n003a3333333333333333333333333333333333333333 refs/tags/v1\n003d4444444444444444444444444444444444444444 refs/tags/v1^{}\n0035shallow 5555555555555555555555555555555555555555\n0000"
}