diff options
author | Alberto Cortés <alcortesm@gmail.com> | 2016-10-26 17:56:26 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-26 15:56:26 +0000 |
commit | 73fa9ef25a8af9c8337a4cf34a67cfe208f1a7c5 (patch) | |
tree | 66886d9b47e373b748c1bceafe1885e6f47868dd /formats/packp/pktline/pktlines.go | |
parent | f3ab3a6c73015b5ae9b2a4756dc646e1211cedb9 (diff) | |
download | go-git-73fa9ef25a8af9c8337a4cf34a67cfe208f1a7c5.tar.gz |
Use advrefs in gituploadpackinfo (#92)
* add advrefs encoder and parser
* modify advrefs encoder to resemble json encoder
* turn advrefs parser into a decoder
* clean code
* improve documentation
* improve documentation
* clean code
* upgrade to new pktline.Add and add Flush const to easy integration
* gometalinter
* Use packp/advrefs for GitUploadPackInfo parsing
- GitUploadPackInfo now uses packp/advrefs instead of parsing the
message by itself.
- Capabilities has been moved from clients/common to packp to avoid a
circular import.
- Cleaning of advrefs_test code.
- Add support for prefix encoding and decoding in advrefs.
* clean advrefs test code
* clean advrefs test code
* clean advrefs test code
* gometalinter
* add pktline encoder
* change pktline.EncodeFlush to pktline.Flush
* make scanner tests use the encoder instead of Pktlines
* check errors on flush and clean constants
* ubstitute the PktLines type with a pktline.Encoder
* use pktline.Encoder in all go-git
* add example of pktline.Encodef()
* add package overview
* documentation
* support symbolic links other than HEAD
* simplify decoding of shallows
* packp: fix mcuadros comments
- all abbreviates removed (by visual inspection, some may remain)
- all empty maps are initialized using make
- simplify readRef with a switch
- make decodeShallow malformed error more verbose
- add pktline.Encoder.encodeLine
- remove infamous panic in checkPayloadLength by refactoring out
the whole function
Diffstat (limited to 'formats/packp/pktline/pktlines.go')
-rw-r--r-- | formats/packp/pktline/pktlines.go | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/formats/packp/pktline/pktlines.go b/formats/packp/pktline/pktlines.go deleted file mode 100644 index c19aa2e..0000000 --- a/formats/packp/pktline/pktlines.go +++ /dev/null @@ -1,140 +0,0 @@ -// Package pktline implements reading payloads form pkt-lines and creating pkt-lines from payloads. -package pktline - -import ( - "bytes" - "errors" - "io" - "strings" -) - -const ( - // MaxPayloadSize is the maximum payload size of a pkt-line in bytes. - MaxPayloadSize = 65516 -) - -var ( - flush = []byte{'0', '0', '0', '0'} -) - -// PktLines values represent a succession of pkt-lines. Values from -// this type are not zero-value safe, use the New function instead. -type PktLines struct { - r io.Reader -} - -var ( - // ErrPayloadTooLong is returned by the Add methods when any of the - // provided payloads is bigger than MaxPayloadSize. - ErrPayloadTooLong = errors.New("payload is too long") - // ErrEmptyPayload is returned by the Add methods when an empty - // payload is provided. - ErrEmptyPayload = errors.New("cannot add empty payloads") -) - -// New returns an empty PktLines (with no payloads) ready to be used. -func New() *PktLines { - return &PktLines{ - r: bytes.NewReader(nil), - } -} - -// AddFlush adds a flush-pkt to p. -func (p *PktLines) AddFlush() { - p.r = io.MultiReader(p.r, bytes.NewReader(flush)) -} - -// Add adds the slices in pp as the payloads of a -// corresponding number of pktlines. -func (p *PktLines) Add(pp ...[]byte) error { - tmp := []io.Reader{p.r} - for _, p := range pp { - if err := add(&tmp, p); err != nil { - return err - } - } - p.r = io.MultiReader(tmp...) - - return nil -} - -func add(dst *[]io.Reader, e []byte) error { - if err := checkPayloadLength(len(e)); err != nil { - return err - } - - n := len(e) + 4 - *dst = append(*dst, bytes.NewReader(asciiHex16(n))) - *dst = append(*dst, bytes.NewReader(e)) - - return nil -} - -func checkPayloadLength(n int) error { - switch { - case n < 0: - panic("unexpected negative payload length") - case n == 0: - return ErrEmptyPayload - case n > MaxPayloadSize: - return ErrPayloadTooLong - default: - return nil - } -} - -// Returns the hexadecimal ascii representation of the 16 less -// significant bits of n. The length of the returned slice will always -// be 4. Example: if n is 1234 (0x4d2), the return value will be -// []byte{'0', '4', 'd', '2'}. -func asciiHex16(n int) []byte { - var ret [4]byte - ret[0] = byteToASCIIHex(byte(n & 0xf000 >> 12)) - ret[1] = byteToASCIIHex(byte(n & 0x0f00 >> 8)) - ret[2] = byteToASCIIHex(byte(n & 0x00f0 >> 4)) - ret[3] = byteToASCIIHex(byte(n & 0x000f)) - - return ret[:] -} - -// turns a byte into its hexadecimal ascii representation. Example: -// from 11 (0xb) to 'b'. -func byteToASCIIHex(n byte) byte { - if n < 10 { - return '0' + n - } - - return 'a' - 10 + n -} - -// AddString adds the strings in pp as payloads of a -// corresponding number of pktlines. -func (p *PktLines) AddString(pp ...string) error { - tmp := []io.Reader{p.r} - for _, p := range pp { - if err := addString(&tmp, p); err != nil { - return err - } - } - - p.r = io.MultiReader(tmp...) - - return nil -} - -func addString(dst *[]io.Reader, s string) error { - if err := checkPayloadLength(len(s)); err != nil { - return err - } - - n := len(s) + 4 - *dst = append(*dst, bytes.NewReader(asciiHex16(n))) - *dst = append(*dst, strings.NewReader(s)) - - return nil -} - -// Read reads the pktlines for the payloads added so far. -func (p *PktLines) Read(b []byte) (n int, err error) { - return p.r.Read(b) -} |