GitLabgitlab-org

cli

main
ply7 git calls246 ms100 KiB5 cachedprotocol v2
333 lines8.9 KiBRaw
1// The .yarnrc lockfile tokenizer, parser and YAML fallback in this file are
2// translated from Yarn 1.22.19's src/lockfile/parse.js:
3// https://github.com/yarnpkg/yarn/blob/v1.22.19/src/lockfile/parse.js
4//
5// BSD 2-Clause License
6//
7// For Yarn software
8//
9// Copyright (c) 2016-present, Yarn Contributors. All rights reserved.
10//
11// Redistribution and use in source and binary forms, with or without modification,
12// are permitted provided that the following conditions are met:
13//
14// * Redistributions of source code must retain the above copyright notice, this
15// list of conditions and the following disclaimer.
16//
17// * Redistributions in binary form must reproduce the above copyright notice,
18// this list of conditions and the following disclaimer in the documentation
19// and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32package pm
34import (
35 "bytes"
36 "encoding/json"
37 "fmt"
38 "slices"
39 "strconv"
40 "strings"
42 "go.yaml.in/yaml/v3"
45// readYarnrc reads the top-level settings of a Yarn Classic .yarnrc as yarn
46// 1.22 does (src/lockfile/parse.js): in yarn's lockfile syntax, else, when
47// that fails, the whole file as YAML, which yields settings only when the
48// file is a mapping. A file that fails both fails the run, as it fails yarn.
49// Errors name the line only, because the file can hold credentials.
50func readYarnrc(files configFiles, path string) (map[string]configSetting, error) {
51 data, ok, err := files.read(path)
52 if !ok {
53 return nil, err
54 }
55 data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
56 values, err := parseYarnLockfile(string(data))
57 if err != nil {
58 var yamlErr error
59 if values, yamlErr = yarnrcYAML(data); yamlErr != nil {
60 return nil, parseError(path, err)
61 }
62 }
63 layer := make(map[string]configSetting, len(values))
64 for k, v := range values {
65 layer[k] = configSetting{source: fileSource(path, k), value: v}
66 }
67 return layer, nil
70// yarnrcYAML reads data as yarn's YAML fallback does, with js-yaml's
71// failsafe schema: the top-level keys of a mapping, every scalar a string.
72// Any other document yields no settings.
73func yarnrcYAML(data []byte) (map[string]string, error) {
74 var doc yaml.Node
75 if err := yaml.Unmarshal(data, &doc); err != nil {
76 return nil, err
77 }
78 values := map[string]string{}
79 if len(doc.Content) == 0 || doc.Content[0].Kind != yaml.MappingNode {
80 return values, nil
81 }
82 m := doc.Content[0].Content
83 seen := map[string]struct{}{}
84 for i := 0; i+1 < len(m); i += 2 {
85 if _, dup := seen[m[i].Value]; dup {
86 // js-yaml rejects a duplicated key.
87 return nil, fmt.Errorf("line %d", m[i].Line)
88 }
89 seen[m[i].Value] = struct{}{}
90 if m[i+1].Kind == yaml.ScalarNode {
91 values[m[i].Value] = m[i+1].Value
92 }
93 }
94 return values, nil
97type yarnTokenKind int
99const (
100 yarnInvalid yarnTokenKind = iota
101 yarnBoolean
102 yarnString
103 yarnNumber
104 yarnColon
105 yarnComma
106 yarnNewline
107 yarnComment
108 yarnIndent
109 yarnEOF
110 // yarnBadIndent stands for the error yarn's tokenizer throws on an odd
111 // indent, which yarn only meets when the parser reads that far.
112 yarnBadIndent
115type yarnToken struct {
116 kind yarnTokenKind
117 value string
118 indent int
119 line int
122// yarnTokens splits input as yarn 1.22's lockfile tokenizer does. It stops
123// after a token the parser always rejects.
124func yarnTokens(input string) []yarnToken {
125 var tokens []yarnToken
126 line, lastNewline := 1, false
127 emit := func(kind yarnTokenKind, value string) {
128 tokens = append(tokens, yarnToken{kind: kind, value: value, line: line})
129 }
130 for input != "" {
131 chop := 0
132 c := input[0]
133 switch {
134 case c == '\n' || c == '\r':
135 chop = 1
136 if len(input) > 1 && input[1] == '\n' {
137 chop = 2
138 }
139 line++
140 emit(yarnNewline, "")
141 case c == '#':
142 end := strings.IndexByte(input[1:], '\n') + 1
143 if end == 0 {
144 end = len(input)
145 }
146 emit(yarnComment, input[1:end])
147 chop = end
148 case c == ' ' && lastNewline:
149 chop = len(input) - len(strings.TrimLeft(input, " "))
150 if chop%2 == 1 {
151 emit(yarnBadIndent, "")
152 return tokens
153 }
154 tokens = append(tokens, yarnToken{kind: yarnIndent, indent: chop / 2, line: line})
155 case c == ' ':
156 chop = 1
157 case c == '"':
158 chop = len(input)
159 for i := 1; i < len(input); i++ {
160 if input[i] == '"' && (input[i-1] != '\\' || (i >= 2 && input[i-2] == '\\')) {
161 chop = i + 1
162 break
163 }
164 }
165 var s string
166 if json.Unmarshal([]byte(input[:chop]), &s) != nil {
167 emit(yarnInvalid, "")
168 return tokens
169 }
170 emit(yarnString, s)
171 case c >= '0' && c <= '9':
172 chop = len(input) - len(strings.TrimLeft(input, "0123456789"))
173 n := strings.TrimLeft(input[:chop], "0")
174 if n == "" {
175 n = "0"
176 }
177 emit(yarnNumber, n)
178 case strings.HasPrefix(input, "true"):
179 emit(yarnBoolean, "true")
180 chop = 4
181 case strings.HasPrefix(input, "false"):
182 emit(yarnBoolean, "false")
183 chop = 5
184 case c == ':':
185 emit(yarnColon, "")
186 chop = 1
187 case c == ',':
188 emit(yarnComma, "")
189 chop = 1
190 case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c == '/', c == '.', c == '-':
191 chop = strings.IndexAny(input, ": \n\r,")
192 if chop == -1 {
193 chop = len(input)
194 }
195 emit(yarnString, input[:chop])
196 default:
197 emit(yarnInvalid, "")
198 return tokens
199 }
200 lastNewline = c == '\n' || (c == '\r' && len(input) > 1 && input[1] == '\n')
201 input = input[chop:]
202 }
203 emit(yarnEOF, "")
204 return tokens
207// yarnSyntaxError is a .yarnrc that yarn's lockfile parser rejects, at line.
208type yarnSyntaxError struct{ line int }
210func (e yarnSyntaxError) Error() string { return "line " + strconv.Itoa(e.line) }
212type yarnParser struct {
213 tokens []yarnToken
214 pos int
215 tok yarnToken
218func parseYarnLockfile(text string) (map[string]string, error) {
219 p := &yarnParser{tokens: yarnTokens(text)}
220 if err := p.next(); err != nil {
221 return nil, err
222 }
223 values := map[string]string{}
224 if err := p.parse(0, values); err != nil {
225 return nil, err
226 }
227 return values, nil
230func (p *yarnParser) next() error {
231 for p.pos < len(p.tokens) {
232 t := p.tokens[p.pos]
233 p.pos++
234 switch t.kind {
235 case yarnComment:
236 continue
237 case yarnBadIndent:
238 return yarnSyntaxError{t.line}
239 }
240 p.tok = t
241 return nil
242 }
243 return yarnSyntaxError{p.tok.line}
246// parse reads the settings at indent, as yarn's Parser.parse does, and
247// records the plain values in values when it is not nil. A line indented
248// less than indent, or any indented line at the top level, ends it.
249func (p *yarnParser) parse(indent int, values map[string]string) error {
250 for {
251 switch t := p.tok; t.kind {
252 case yarnNewline:
253 if err := p.next(); err != nil {
254 return err
255 }
256 if indent == 0 {
257 continue
258 }
259 if p.tok.kind != yarnIndent || p.tok.indent != indent {
260 return nil
261 }
262 if err := p.next(); err != nil {
263 return err
264 }
265 case yarnIndent:
266 if t.indent != indent {
267 return nil
268 }
269 if err := p.next(); err != nil {
270 return err
271 }
272 case yarnEOF:
273 return nil
274 case yarnString:
275 nested, err := p.parseEntry(indent, values)
276 if err != nil {
277 return err
278 }
279 if nested && indent > 0 && p.tok.kind != yarnIndent {
280 return nil
281 }
282 default:
283 return yarnSyntaxError{t.line}
284 }
285 }
288// parseEntry reads one "key value", "key, key value" or "key:" entry, and
289// reports whether it was the last kind, a nested block whose settings are
290// not recorded.
291func (p *yarnParser) parseEntry(indent int, values map[string]string) (bool, error) {
292 keys := []string{p.tok.value}
293 if err := p.next(); err != nil {
294 return false, err
295 }
296 for p.tok.kind == yarnComma {
297 if err := p.next(); err != nil {
298 return false, err
299 }
300 if p.tok.kind != yarnString {
301 return false, yarnSyntaxError{p.tok.line}
302 }
303 keys = append(keys, p.tok.value)
304 if err := p.next(); err != nil {
305 return false, err
306 }
307 }
308 if slices.Contains(keys, "") {
309 return false, yarnSyntaxError{p.tok.line}
310 }
311 wasColon := p.tok.kind == yarnColon
312 if wasColon {
313 if err := p.next(); err != nil {
314 return false, err
315 }
316 }
317 switch {
318 case p.tok.kind == yarnBoolean || p.tok.kind == yarnString || p.tok.kind == yarnNumber:
319 if values != nil {
320 for _, k := range keys {
321 values[k] = p.tok.value
322 }
323 }
324 return false, p.next()
325 case wasColon:
326 for _, k := range keys {
327 delete(values, k)
328 }
329 return true, p.parse(indent+1, nil)
330 default:
331 return false, yarnSyntaxError{p.tok.line}
332 }
Diagnostics7 git calls · 246 ms
CallStatusSizeTime0246 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0200361 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=361
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=361
git.pkt.payload.max=334
git.sideband.band1.bytes=334
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=334
git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.3 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1327
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1327
git.pkt.payload.max=1300
git.sideband.band1.bytes=1300
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1300
git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4
Open pack map
fetch2003.3 KiB237 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=237
git.response.bytes=3421
git.response.cache=MISS
git.response.duration_ms=237
git.fetch.wants=0b1a13fea04e79e40d9ad4f5f0880c2ef790fd69
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3421
git.pkt.payload.max=3394
git.sideband.band1.bytes=3394
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e1bb20a9c26c75
Request
b8cb249a…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format