Esempio n. 1
0
// Encode encodes the UploadHaves into the Writer.
func (u *UploadHaves) Encode(w io.Writer) error {
	e := pktline.NewEncoder(w)

	plumbing.HashesSort(u.Haves)

	var last plumbing.Hash
	for _, have := range u.Haves {
		if bytes.Compare(last[:], have[:]) == 0 {
			continue
		}

		if err := e.Encodef("have %s\n", have); err != nil {
			return fmt.Errorf("sending haves for %q: %s", have, err)
		}

		last = have
	}

	if len(u.Haves) != 0 {
		if err := e.Flush(); err != nil {
			return fmt.Errorf("sending flush-pkt after haves: %s", err)
		}
	}

	return nil
}
Esempio n. 2
0
func (e *ulReqEncoder) Encode(v *UploadRequest) error {
	e.data = v

	if len(v.Wants) == 0 {
		return fmt.Errorf("empty wants provided")
	}

	plumbing.HashesSort(e.data.Wants)
	for state := e.encodeFirstWant; state != nil; {
		state = state()
	}

	return e.err
}
Esempio n. 3
0
func (e *ulReqEncoder) encodeShallows() stateFn {
	plumbing.HashesSort(e.data.Shallows)

	var last plumbing.Hash
	for _, s := range e.data.Shallows {
		if bytes.Compare(last[:], s[:]) == 0 {
			continue
		}

		if err := e.pe.Encodef("shallow %s\n", s); err != nil {
			e.err = fmt.Errorf("encoding shallow %q: %s", s, err)
			return nil
		}

		last = s
	}

	return e.encodeDepth
}
Esempio n. 4
0
func (s *ShallowUpdateSuite) TestDecodeUnshallow(c *C) {
	raw := "" +
		"0036unshallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
		"0036unshallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
		"0000"

	su := &ShallowUpdate{}
	err := su.Decode(bytes.NewBufferString(raw))
	c.Assert(err, IsNil)

	plumbing.HashesSort(su.Unshallows)

	c.Assert(su.Shallows, HasLen, 0)
	c.Assert(su.Unshallows, HasLen, 2)
	c.Assert(su.Unshallows, DeepEquals, []plumbing.Hash{
		plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
		plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
	})
}