| 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 |
// Decode reads the next advertised-refs message form its input and |
| 15 |
// stores it in the AdvRefs. |
| 16 |
func (a *AdvRefs) Decode(r io.Reader) error { |
| 17 |
d := newAdvRefsDecoder(r) |
| 18 |
return d.Decode(a) |
| 19 |
} |
| 20 |
|
| 21 |
type advRefsDecoder struct { |
| 22 |
s *pktline.Scanner // a pkt-line scanner from the input stream |
| 23 |
line []byte // current pkt-line contents, use parser.nextLine() to make it advance |
| 24 |
nLine int // current pkt-line number for debugging, begins at 1 |
| 25 |
hash plumbing.Hash // last hash read |
| 26 |
err error // sticky error, use the parser.error() method to fill this out |
| 27 |
data *AdvRefs // parsed data is stored here |
| 28 |
} |
| 29 |
|
| 30 |
var ( |
| 31 |
// ErrEmptyAdvRefs is returned by Decode if it gets an empty advertised |
| 32 |
// references message. |
| 33 |
ErrEmptyAdvRefs = errors.New("empty advertised-ref message") |
| 34 |
// ErrEmptyInput is returned by Decode if the input is empty. |
| 35 |
ErrEmptyInput = errors.New("empty input") |
| 36 |
) |
| 37 |
|
| 38 |
func newAdvRefsDecoder(r io.Reader) *advRefsDecoder { |
| 39 |
return &advRefsDecoder{ |
| 40 |
s: pktline.NewScanner(r), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func (d *advRefsDecoder) Decode(v *AdvRefs) error { |
| 45 |
d.data = v |
| 46 |
|
| 47 |
for state := decodePrefix; state != nil; { |
| 48 |
state = state(d) |
| 49 |
} |
| 50 |
|
| 51 |
return d.err |
| 52 |
} |
| 53 |
|
| 54 |
type decoderStateFn func(*advRefsDecoder) decoderStateFn |
| 55 |
|
| 56 |
// fills out the parser sticky error |
| 57 |
func (d *advRefsDecoder) error(format string, a ...interface{}) { |
| 58 |
msg := fmt.Sprintf( |
| 59 |
"pkt-line %d: %s", d.nLine, |
| 60 |
fmt.Sprintf(format, a...), |
| 61 |
) |
| 62 |
|
| 63 |
d.err = NewErrUnexpectedData(msg, d.line) |
| 64 |
} |
| 65 |
|
| 66 |
// Reads a new pkt-line from the scanner, makes its payload available as |
| 67 |
// p.line and increments p.nLine. A successful invocation returns true, |
| 68 |
// otherwise, false is returned and the sticky error is filled out |
| 69 |
// accordingly. Trims eols at the end of the payloads. |
| 70 |
func (d *advRefsDecoder) nextLine() bool { |
| 71 |
d.nLine++ |
| 72 |
|
| 73 |
if !d.s.Scan() { |
| 74 |
if d.err = d.s.Err(); d.err != nil { |
| 75 |
return false |
| 76 |
} |
| 77 |
|
| 78 |
if d.nLine == 1 { |
| 79 |
d.err = ErrEmptyInput |
| 80 |
return false |
| 81 |
} |
| 82 |
|
| 83 |
d.error("EOF") |
| 84 |
return false |
| 85 |
} |
| 86 |
|
| 87 |
d.line = d.s.Bytes() |
| 88 |
d.line = bytes.TrimSuffix(d.line, eol) |
| 89 |
|
| 90 |
return true |
| 91 |
} |
| 92 |
|
| 93 |
// The HTTP smart prefix is often followed by a flush-pkt. |
| 94 |
func decodePrefix(d *advRefsDecoder) decoderStateFn { |
| 95 |
if ok := d.nextLine(); !ok { |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
if !isPrefix(d.line) { |
| 100 |
return decodeFirstHash |
| 101 |
} |
| 102 |
|
| 103 |
tmp := make([]byte, len(d.line)) |
| 104 |
copy(tmp, d.line) |
| 105 |
d.data.Prefix = append(d.data.Prefix, tmp) |
| 106 |
if ok := d.nextLine(); !ok { |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
if !isFlush(d.line) { |
| 111 |
return decodeFirstHash |
| 112 |
} |
| 113 |
|
| 114 |
d.data.Prefix = append(d.data.Prefix, pktline.Flush) |
| 115 |
if ok := d.nextLine(); !ok { |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
return decodeFirstHash |
| 120 |
} |
| 121 |
|
| 122 |
func isPrefix(payload []byte) bool { |
| 123 |
return len(payload) > 0 && payload[0] == '#' |
| 124 |
} |
| 125 |
|
| 126 |
// If the first hash is zero, then a no-refs is coming. Otherwise, a |
| 127 |
// list-of-refs is coming, and the hash will be followed by the first |
| 128 |
// advertised ref. |
| 129 |
func decodeFirstHash(p *advRefsDecoder) decoderStateFn { |
| 130 |
// If the repository is empty, we receive a flush here (HTTP). |
| 131 |
if isFlush(p.line) { |
| 132 |
p.err = ErrEmptyAdvRefs |
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
// TODO: Use object-format (when available) for hash size. Git 2.41+ |
| 137 |
if len(p.line) < hashSize { |
| 138 |
p.error("cannot read hash, pkt-line too short") |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
if _, err := hex.Decode(p.hash[:], p.line[:hashSize]); err != nil { |
| 143 |
p.error("invalid hash text: %s", err) |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
p.line = p.line[hashSize:] |
| 148 |
|
| 149 |
if p.hash.IsZero() { |
| 150 |
return decodeSkipNoRefs |
| 151 |
} |
| 152 |
|
| 153 |
return decodeFirstRef |
| 154 |
} |
| 155 |
|
| 156 |
// Skips SP "capabilities^{}" NUL |
| 157 |
func decodeSkipNoRefs(p *advRefsDecoder) decoderStateFn { |
| 158 |
if len(p.line) < len(noHeadMark) { |
| 159 |
p.error("too short zero-id ref") |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
if !bytes.HasPrefix(p.line, noHeadMark) { |
| 164 |
p.error("malformed zero-id ref") |
| 165 |
return nil |
| 166 |
} |
| 167 |
|
| 168 |
p.line = p.line[len(noHeadMark):] |
| 169 |
|
| 170 |
return decodeCaps |
| 171 |
} |
| 172 |
|
| 173 |
// decode the refname, expects SP refname NULL |
| 174 |
func decodeFirstRef(l *advRefsDecoder) decoderStateFn { |
| 175 |
if len(l.line) < 3 { |
| 176 |
l.error("line too short after hash") |
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
if !bytes.HasPrefix(l.line, sp) { |
| 181 |
l.error("no space after hash") |
| 182 |
return nil |
| 183 |
} |
| 184 |
l.line = l.line[1:] |
| 185 |
|
| 186 |
chunks := bytes.SplitN(l.line, null, 2) |
| 187 |
if len(chunks) < 2 { |
| 188 |
l.error("NULL not found") |
| 189 |
return nil |
| 190 |
} |
| 191 |
ref := chunks[0] |
| 192 |
l.line = chunks[1] |
| 193 |
|
| 194 |
if bytes.Equal(ref, []byte(head)) { |
| 195 |
l.data.Head = &l.hash |
| 196 |
} else { |
| 197 |
l.data.References[string(ref)] = l.hash |
| 198 |
} |
| 199 |
|
| 200 |
return decodeCaps |
| 201 |
} |
| 202 |
|
| 203 |
func decodeCaps(p *advRefsDecoder) decoderStateFn { |
| 204 |
if err := p.data.Capabilities.Decode(p.line); err != nil { |
| 205 |
p.error("invalid capabilities: %s", err) |
| 206 |
return nil |
| 207 |
} |
| 208 |
|
| 209 |
return decodeOtherRefs |
| 210 |
} |
| 211 |
|
| 212 |
// The refs are either tips (obj-id SP refname) or a peeled (obj-id SP refname^{}). |
| 213 |
// If there are no refs, then there might be a shallow or flush-ptk. |
| 214 |
func decodeOtherRefs(p *advRefsDecoder) decoderStateFn { |
| 215 |
if ok := p.nextLine(); !ok { |
| 216 |
return nil |
| 217 |
} |
| 218 |
|
| 219 |
if bytes.HasPrefix(p.line, shallow) { |
| 220 |
return decodeShallow |
| 221 |
} |
| 222 |
|
| 223 |
if len(p.line) == 0 { |
| 224 |
return nil |
| 225 |
} |
| 226 |
|
| 227 |
saveTo := p.data.References |
| 228 |
if bytes.HasSuffix(p.line, peeled) { |
| 229 |
p.line = bytes.TrimSuffix(p.line, peeled) |
| 230 |
saveTo = p.data.Peeled |
| 231 |
} |
| 232 |
|
| 233 |
ref, hash, err := readRef(p.line) |
| 234 |
if err != nil { |
| 235 |
p.error("%s", err) |
| 236 |
return nil |
| 237 |
} |
| 238 |
saveTo[ref] = hash |
| 239 |
|
| 240 |
return decodeOtherRefs |
| 241 |
} |
| 242 |
|
| 243 |
// Reads a ref-name |
| 244 |
func readRef(data []byte) (string, plumbing.Hash, error) { |
| 245 |
chunks := bytes.Split(data, sp) |
| 246 |
switch { |
| 247 |
case len(chunks) == 1: |
| 248 |
return "", plumbing.ZeroHash, fmt.Errorf("malformed ref data: no space was found") |
| 249 |
case len(chunks) > 2: |
| 250 |
return "", plumbing.ZeroHash, fmt.Errorf("malformed ref data: more than one space found") |
| 251 |
default: |
| 252 |
return string(chunks[1]), plumbing.NewHash(string(chunks[0])), nil |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// Keeps reading shallows until a flush-pkt is found |
| 257 |
func decodeShallow(p *advRefsDecoder) decoderStateFn { |
| 258 |
if !bytes.HasPrefix(p.line, shallow) { |
| 259 |
p.error("malformed shallow prefix, found %q... instead", p.line[:len(shallow)]) |
| 260 |
return nil |
| 261 |
} |
| 262 |
p.line = bytes.TrimPrefix(p.line, shallow) |
| 263 |
|
| 264 |
if len(p.line) != hashSize { |
| 265 |
p.error("malformed shallow hash: wrong length, expected 40 bytes, read %d bytes", |
| 266 |
len(p.line)) |
| 267 |
return nil |
| 268 |
} |
| 269 |
|
| 270 |
text := p.line[:hashSize] |
| 271 |
var h plumbing.Hash |
| 272 |
if _, err := hex.Decode(h[:], text); err != nil { |
| 273 |
p.error("invalid hash text: %s", err) |
| 274 |
return nil |
| 275 |
} |
| 276 |
|
| 277 |
p.data.Shallows = append(p.data.Shallows, h) |
| 278 |
|
| 279 |
if ok := p.nextLine(); !ok { |
| 280 |
return nil |
| 281 |
} |
| 282 |
|
| 283 |
if len(p.line) == 0 { |
| 284 |
return nil // successful parse of the advertised-refs message |
| 285 |
} |
| 286 |
|
| 287 |
return decodeShallow |
| 288 |
} |