Debian Codegraph @

plumbing/protocol/packp/updreq_decode.go

raw
1 package packp
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "errors"
7 "fmt"
8 "io"
9
10 "github.com/go-git/go-git/v5/plumbing"
11 "github.com/go-git/go-git/v5/plumbing/format/pktline"
12 )
13
14 var (
15 shallowLineLength = len(shallow) + hashSize
16 minCommandLength = hashSize*2 + 2 + 1
17 minCommandAndCapsLength = minCommandLength + 1
18 )
19
20 var (
21 ErrEmpty = errors.New("empty update-request message")
22 errNoCommands = errors.New("unexpected EOF before any command")
23 errMissingCapabilitiesDelimiter = errors.New("capabilities delimiter not found")
24 )
25
26 func errMalformedRequest(reason string) error {
27 return fmt.Errorf("malformed request: %s", reason)
28 }
29
30 func errInvalidHashSize(got int) error {
31 return fmt.Errorf("invalid hash size: expected %d, got %d",
32 hashSize, got)
33 }
34
35 func errInvalidHash(err error) error {
36 return fmt.Errorf("invalid hash: %s", err.Error())
37 }
38
39 func errInvalidShallowLineLength(got int) error {
40 return errMalformedRequest(fmt.Sprintf(
41 "invalid shallow line length: expected %d, got %d",
42 shallowLineLength, got))
43 }
44
45 func errInvalidCommandCapabilitiesLineLength(got int) error {
46 return errMalformedRequest(fmt.Sprintf(
47 "invalid command and capabilities line length: expected at least %d, got %d",
48 minCommandAndCapsLength, got))
49 }
50
51 func errInvalidCommandLineLength(got int) error {
52 return errMalformedRequest(fmt.Sprintf(
53 "invalid command line length: expected at least %d, got %d",
54 minCommandLength, got))
55 }
56
57 func errInvalidShallowObjId(err error) error {
58 return errMalformedRequest(
59 fmt.Sprintf("invalid shallow object id: %s", err.Error()))
60 }
61
62 func errInvalidOldObjId(err error) error {
63 return errMalformedRequest(
64 fmt.Sprintf("invalid old object id: %s", err.Error()))
65 }
66
67 func errInvalidNewObjId(err error) error {
68 return errMalformedRequest(
69 fmt.Sprintf("invalid new object id: %s", err.Error()))
70 }
71
72 func errMalformedCommand(err error) error {
73 return errMalformedRequest(fmt.Sprintf(
74 "malformed command: %s", err.Error()))
75 }
76
77 // Decode reads the next update-request message form the reader and wr
78 func (req *ReferenceUpdateRequest) Decode(r io.Reader) error {
79 var rc io.ReadCloser
80 var ok bool
81 rc, ok = r.(io.ReadCloser)
82 if !ok {
83 rc = io.NopCloser(r)
84 }
85
86 d := &updReqDecoder{r: rc, s: pktline.NewScanner(r)}
87 return d.Decode(req)
88 }
89
90 type updReqDecoder struct {
91 r io.ReadCloser
92 s *pktline.Scanner
93 req *ReferenceUpdateRequest
94 }
95
96 func (d *updReqDecoder) Decode(req *ReferenceUpdateRequest) error {
97 d.req = req
98 funcs := []func() error{
99 d.scanLine,
100 d.decodeShallow,
101 d.decodeCommandAndCapabilities,
102 d.decodeCommands,
103 d.setPackfile,
104 req.validate,
105 }
106
107 for _, f := range funcs {
108 if err := f(); err != nil {
109 return err
110 }
111 }
112
113 return nil
114 }
115
116 func (d *updReqDecoder) scanLine() error {
117 if ok := d.s.Scan(); !ok {
118 return d.scanErrorOr(ErrEmpty)
119 }
120
121 return nil
122 }
123
124 func (d *updReqDecoder) decodeShallow() error {
125 b := d.s.Bytes()
126
127 if !bytes.HasPrefix(b, shallowNoSp) {
128 return nil
129 }
130
131 if len(b) != shallowLineLength {
132 return errInvalidShallowLineLength(len(b))
133 }
134
135 h, err := parseHash(string(b[len(shallow):]))
136 if err != nil {
137 return errInvalidShallowObjId(err)
138 }
139
140 if ok := d.s.Scan(); !ok {
141 return d.scanErrorOr(errNoCommands)
142 }
143
144 d.req.Shallow = &h
145
146 return nil
147 }
148
149 func (d *updReqDecoder) decodeCommands() error {
150 for {
151 b := d.s.Bytes()
152 if bytes.Equal(b, pktline.Flush) {
153 return nil
154 }
155
156 c, err := parseCommand(b)
157 if err != nil {
158 return err
159 }
160
161 d.req.Commands = append(d.req.Commands, c)
162
163 if ok := d.s.Scan(); !ok {
164 return d.s.Err()
165 }
166 }
167 }
168
169 func (d *updReqDecoder) decodeCommandAndCapabilities() error {
170 b := d.s.Bytes()
171 i := bytes.IndexByte(b, 0)
172 if i == -1 {
173 return errMissingCapabilitiesDelimiter
174 }
175
176 if len(b) < minCommandAndCapsLength {
177 return errInvalidCommandCapabilitiesLineLength(len(b))
178 }
179
180 cmd, err := parseCommand(b[:i])
181 if err != nil {
182 return err
183 }
184
185 d.req.Commands = append(d.req.Commands, cmd)
186
187 if err := d.req.Capabilities.Decode(b[i+1:]); err != nil {
188 return err
189 }
190
191 if err := d.scanLine(); err != nil {
192 return err
193 }
194
195 return nil
196 }
197
198 func (d *updReqDecoder) setPackfile() error {
199 d.req.Packfile = d.r
200
201 return nil
202 }
203
204 func parseCommand(b []byte) (*Command, error) {
205 if len(b) < minCommandLength {
206 return nil, errInvalidCommandLineLength(len(b))
207 }
208
209 var (
210 os, ns string
211 n plumbing.ReferenceName
212 )
213 if _, err := fmt.Sscanf(string(b), "%s %s %s", &os, &ns, &n); err != nil {
214 return nil, errMalformedCommand(err)
215 }
216
217 oh, err := parseHash(os)
218 if err != nil {
219 return nil, errInvalidOldObjId(err)
220 }
221
222 nh, err := parseHash(ns)
223 if err != nil {
224 return nil, errInvalidNewObjId(err)
225 }
226
227 return &Command{Old: oh, New: nh, Name: n}, nil
228 }
229
230 func parseHash(s string) (plumbing.Hash, error) {
231 if len(s) != hashSize {
232 return plumbing.ZeroHash, errInvalidHashSize(len(s))
233 }
234
235 if _, err := hex.DecodeString(s); err != nil {
236 return plumbing.ZeroHash, errInvalidHash(err)
237 }
238
239 h := plumbing.NewHash(s)
240 return h, nil
241 }
242
243 func (d *updReqDecoder) scanErrorOr(origErr error) error {
244 if err := d.s.Err(); err != nil {
245 return err
246 }
247
248 return origErr
249 }