diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-05 15:44:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-05 15:44:50 +0100 |
commit | 11735c3b3aaa8f789dc10739a4de7ad438196000 (patch) | |
tree | c83fa6ddf9748fd0e4e4edb4d0e2692994145ace /plumbing/protocol/packp/ulreq_encode.go | |
parent | 0042bb031676a20ffc789f94e332a6da70e2756d (diff) | |
download | go-git-11735c3b3aaa8f789dc10739a4de7ad438196000.tar.gz |
plumbing/protocol: paktp avoid duplication of haves, wants and shallow (#158)
Diffstat (limited to 'plumbing/protocol/packp/ulreq_encode.go')
-rw-r--r-- | plumbing/protocol/packp/ulreq_encode.go | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/plumbing/protocol/packp/ulreq_encode.go b/plumbing/protocol/packp/ulreq_encode.go index b2ca491..4a26e74 100644 --- a/plumbing/protocol/packp/ulreq_encode.go +++ b/plumbing/protocol/packp/ulreq_encode.go @@ -1,9 +1,9 @@ package packp import ( + "bytes" "fmt" "io" - "sort" "time" "gopkg.in/src-d/go-git.v4/plumbing" @@ -21,10 +21,9 @@ func (u *UploadRequest) Encode(w io.Writer) error { } type ulReqEncoder struct { - pe *pktline.Encoder // where to write the encoded data - data *UploadRequest // the data to encode - sortedWants []string - err error // sticky error + pe *pktline.Encoder // where to write the encoded data + data *UploadRequest // the data to encode + err error // sticky error } func newUlReqEncoder(w io.Writer) *ulReqEncoder { @@ -34,13 +33,13 @@ func newUlReqEncoder(w io.Writer) *ulReqEncoder { } func (e *ulReqEncoder) Encode(v *UploadRequest) error { + e.data = v + if len(v.Wants) == 0 { return fmt.Errorf("empty wants provided") } - e.data = v - e.sortedWants = sortHashes(v.Wants) - + plumbing.HashesSort(e.data.Wants) for state := e.encodeFirstWant; state != nil; { state = state() } @@ -48,27 +47,18 @@ func (e *ulReqEncoder) Encode(v *UploadRequest) error { return e.err } -func sortHashes(list []plumbing.Hash) []string { - sorted := make([]string, len(list)) - for i, hash := range list { - sorted[i] = hash.String() - } - sort.Strings(sorted) - - return sorted -} - func (e *ulReqEncoder) encodeFirstWant() stateFn { var err error if e.data.Capabilities.IsEmpty() { - err = e.pe.Encodef("want %s\n", e.sortedWants[0]) + err = e.pe.Encodef("want %s\n", e.data.Wants[0]) } else { err = e.pe.Encodef( "want %s %s\n", - e.sortedWants[0], + e.data.Wants[0], e.data.Capabilities.String(), ) } + if err != nil { e.err = fmt.Errorf("encoding first want line: %s", err) return nil @@ -78,23 +68,38 @@ func (e *ulReqEncoder) encodeFirstWant() stateFn { } func (e *ulReqEncoder) encodeAditionalWants() stateFn { - for _, w := range e.sortedWants[1:] { + last := e.data.Wants[0] + for _, w := range e.data.Wants[1:] { + if bytes.Compare(last[:], w[:]) == 0 { + continue + } + if err := e.pe.Encodef("want %s\n", w); err != nil { e.err = fmt.Errorf("encoding want %q: %s", w, err) return nil } + + last = w } return e.encodeShallows } func (e *ulReqEncoder) encodeShallows() stateFn { - sorted := sortHashes(e.data.Shallows) - for _, s := range sorted { + 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 |