| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 11 |
) |
| 12 |
|
| 13 |
// Encode writes the UlReq encoding of u to the stream. |
| 14 |
// |
| 15 |
// All the payloads will end with a newline character. Wants and |
| 16 |
// shallows are sorted alphabetically. A depth of 0 means no depth |
| 17 |
// request is sent. |
| 18 |
func (req *UploadRequest) Encode(w io.Writer) error { |
| 19 |
e := newUlReqEncoder(w) |
| 20 |
return e.Encode(req) |
| 21 |
} |
| 22 |
|
| 23 |
type ulReqEncoder struct { |
| 24 |
pe *pktline.Encoder // where to write the encoded data |
| 25 |
data *UploadRequest // the data to encode |
| 26 |
err error // sticky error |
| 27 |
} |
| 28 |
|
| 29 |
func newUlReqEncoder(w io.Writer) *ulReqEncoder { |
| 30 |
return &ulReqEncoder{ |
| 31 |
pe: pktline.NewEncoder(w), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func (e *ulReqEncoder) Encode(v *UploadRequest) error { |
| 36 |
e.data = v |
| 37 |
|
| 38 |
if len(v.Wants) == 0 { |
| 39 |
return fmt.Errorf("empty wants provided") |
| 40 |
} |
| 41 |
|
| 42 |
plumbing.HashesSort(e.data.Wants) |
| 43 |
for state := e.encodeFirstWant; state != nil; { |
| 44 |
state = state() |
| 45 |
} |
| 46 |
|
| 47 |
return e.err |
| 48 |
} |
| 49 |
|
| 50 |
func (e *ulReqEncoder) encodeFirstWant() stateFn { |
| 51 |
var err error |
| 52 |
if e.data.Capabilities.IsEmpty() { |
| 53 |
err = e.pe.Encodef("want %s\n", e.data.Wants[0]) |
| 54 |
} else { |
| 55 |
err = e.pe.Encodef( |
| 56 |
"want %s %s\n", |
| 57 |
e.data.Wants[0], |
| 58 |
e.data.Capabilities.String(), |
| 59 |
) |
| 60 |
} |
| 61 |
|
| 62 |
if err != nil { |
| 63 |
e.err = fmt.Errorf("encoding first want line: %s", err) |
| 64 |
return nil |
| 65 |
} |
| 66 |
|
| 67 |
return e.encodeAdditionalWants |
| 68 |
} |
| 69 |
|
| 70 |
func (e *ulReqEncoder) encodeAdditionalWants() stateFn { |
| 71 |
last := e.data.Wants[0] |
| 72 |
for _, w := range e.data.Wants[1:] { |
| 73 |
if bytes.Equal(last[:], w[:]) { |
| 74 |
continue |
| 75 |
} |
| 76 |
|
| 77 |
if err := e.pe.Encodef("want %s\n", w); err != nil { |
| 78 |
e.err = fmt.Errorf("encoding want %q: %s", w, err) |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
last = w |
| 83 |
} |
| 84 |
|
| 85 |
return e.encodeShallows |
| 86 |
} |
| 87 |
|
| 88 |
func (e *ulReqEncoder) encodeShallows() stateFn { |
| 89 |
plumbing.HashesSort(e.data.Shallows) |
| 90 |
|
| 91 |
var last plumbing.Hash |
| 92 |
for _, s := range e.data.Shallows { |
| 93 |
if bytes.Equal(last[:], s[:]) { |
| 94 |
continue |
| 95 |
} |
| 96 |
|
| 97 |
if err := e.pe.Encodef("shallow %s\n", s); err != nil { |
| 98 |
e.err = fmt.Errorf("encoding shallow %q: %s", s, err) |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
last = s |
| 103 |
} |
| 104 |
|
| 105 |
return e.encodeDepth |
| 106 |
} |
| 107 |
|
| 108 |
func (e *ulReqEncoder) encodeDepth() stateFn { |
| 109 |
switch depth := e.data.Depth.(type) { |
| 110 |
case DepthCommits: |
| 111 |
if depth != 0 { |
| 112 |
commits := int(depth) |
| 113 |
if err := e.pe.Encodef("deepen %d\n", commits); err != nil { |
| 114 |
e.err = fmt.Errorf("encoding depth %d: %s", depth, err) |
| 115 |
return nil |
| 116 |
} |
| 117 |
} |
| 118 |
case DepthSince: |
| 119 |
when := time.Time(depth).UTC() |
| 120 |
if err := e.pe.Encodef("deepen-since %d\n", when.Unix()); err != nil { |
| 121 |
e.err = fmt.Errorf("encoding depth %s: %s", when, err) |
| 122 |
return nil |
| 123 |
} |
| 124 |
case DepthReference: |
| 125 |
reference := string(depth) |
| 126 |
if err := e.pe.Encodef("deepen-not %s\n", reference); err != nil { |
| 127 |
e.err = fmt.Errorf("encoding depth %s: %s", reference, err) |
| 128 |
return nil |
| 129 |
} |
| 130 |
default: |
| 131 |
e.err = fmt.Errorf("unsupported depth type") |
| 132 |
return nil |
| 133 |
} |
| 134 |
|
| 135 |
return e.encodeFilter |
| 136 |
} |
| 137 |
|
| 138 |
func (e *ulReqEncoder) encodeFilter() stateFn { |
| 139 |
if filter := e.data.Filter; filter != "" { |
| 140 |
if err := e.pe.Encodef("filter %s\n", filter); err != nil { |
| 141 |
e.err = fmt.Errorf("encoding filter %s: %s", filter, err) |
| 142 |
return nil |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return e.encodeFlush |
| 147 |
} |
| 148 |
|
| 149 |
func (e *ulReqEncoder) encodeFlush() stateFn { |
| 150 |
if err := e.pe.Flush(); err != nil { |
| 151 |
e.err = fmt.Errorf("encoding flush-pkt: %s", err) |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
return nil |
| 156 |
} |