Esempio n. 1
0
func ExampleDecoder_Decode() {
	// Here is a raw advertised-ref message.
	raw := "" +
		"0065a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 HEAD\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n" +
		"003fa6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n" +
		"00441111111111111111111111111111111111111111 refs/tags/v2.6.11-tree\n" +
		"00475555555555555555555555555555555555555555 refs/tags/v2.6.11-tree^{}\n" +
		"0035shallow 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c\n" +
		"0000"

	// Use the raw message as our input.
	input := strings.NewReader(raw)

	// Create a advref.Decoder reading from our input.
	d := advrefs.NewDecoder(input)

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

	// Do something interesting with the AdvRefs, e.g. print its contents.
	fmt.Println("head =", ar.Head)
	fmt.Println("capabilities =", ar.Capabilities.String())
	fmt.Println("...")
	fmt.Println("shallows =", ar.Shallows)
	// Output: head = a6930aaee06755d1bdcfd943fbf614e4d92bb0c7
	// capabilities = multi_ack ofs-delta symref=HEAD:/refs/heads/master
	// ...
	// shallows = [5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c]
}
Esempio n. 2
0
func testDecoderErrorMatches(c *C, input io.Reader, pattern string) {
	ar := advrefs.New()
	d := advrefs.NewDecoder(input)

	err := d.Decode(ar)
	c.Assert(err, ErrorMatches, pattern)
}
Esempio n. 3
0
func (s *SuiteDecoder) TestEmpty(c *C) {
	ar := advrefs.New()
	var buf bytes.Buffer
	d := advrefs.NewDecoder(&buf)

	err := d.Decode(ar)
	c.Assert(err, Equals, advrefs.ErrEmpty)
}
Esempio n. 4
0
func testDecodeOK(c *C, payloads []string) *advrefs.AdvRefs {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)
	err := e.EncodeString(payloads...)
	c.Assert(err, IsNil)

	ar := advrefs.New()
	d := advrefs.NewDecoder(&buf)

	err = d.Decode(ar)
	c.Assert(err, IsNil)

	return ar
}
Esempio n. 5
0
func (i *GitUploadPackInfo) Decode(r io.Reader) error {
	d := advrefs.NewDecoder(r)
	ar := advrefs.New()
	if err := d.Decode(ar); err != nil {
		if err == advrefs.ErrEmpty {
			return plumbing.NewPermanentError(err)
		}
		return plumbing.NewUnexpectedError(err)
	}

	i.Capabilities = ar.Capabilities

	if err := i.addRefs(ar); err != nil {
		return plumbing.NewUnexpectedError(err)
	}

	return nil
}
Esempio n. 6
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)))
}
Esempio n. 7
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"
}