GitLabgitlab-org

cli

main
ply7 git calls391 ms99 KiB5 cachedprotocol v2
273 lines8.0 KiBRaw
1package pm
3import (
4 "errors"
5 "fmt"
6 "net"
7 "net/url"
8 "path/filepath"
9 "strconv"
10 "strings"
12 "gitlab.com/gitlab-org/cli/internal/dbg"
15// gradleUpstreamProxy follows how Gradle sets the JVM proxy properties
16// (verified against Gradle 9.8.2). Each of https.proxyHost, proxyPort,
17// proxyUser, proxyPassword and http.nonProxyHosts, which the JVM also applies
18// to https, comes from the first of: -D on the command line, then
19// systemProp.* in gradle.properties in the Gradle user home and in the Gradle
20// installation. An empty https.proxyHost means a direct connection. The JVM
21// ignores HTTPS_PROXY. It doesn't read the project's gradle.properties, or a
22// user home or installation inside the project.
23func gradleUpstreamProxy(environ []string, dir string, args []string) (upstreamProxy, bool, error) {
24 cliProps := map[string]configSetting{}
25 for k, v := range jvmDefines(gradleSystemPropArgs(args)) {
26 cliProps[k] = configSetting{source: "-D" + k, value: v}
27 }
29 files := newConfigFiles(dir)
30 layers := configLayers{cliProps}
31 for _, file := range gradlePropertiesFiles(environ, dir, args, cliProps) {
32 layer, err := readGradleProperties(files, file)
33 if err != nil {
34 return upstreamProxy{}, false, err
35 }
36 layers = append(layers, layer)
37 }
39 host, ok := layers.get("https.proxyHost")
40 if !ok {
41 return upstreamProxy{}, false, nil
42 }
43 if host.value == "" {
44 return upstreamProxy{Source: host.source}, true, nil
45 }
46 port := "443"
47 if s, ok := layers.get("https.proxyPort"); ok {
48 if _, err := strconv.ParseUint(s.value, 10, 16); err != nil {
49 return upstreamProxy{}, false, fmt.Errorf("%s: https.proxyPort is not a number", s.source)
50 }
51 port = s.value
52 }
53 u := url.URL{Scheme: "http", Host: net.JoinHostPort(host.value, port)}
54 proxyUser, hasUser := layers.get("https.proxyUser")
55 if hasUser && proxyUser.value != "" {
56 password, _ := layers.get("https.proxyPassword")
57 u.User = url.UserPassword(proxyUser.value, password.value)
58 }
59 upstream := upstreamProxy{Source: host.source, URL: u.String()}
60 if s, ok := layers.get("http.nonProxyHosts"); ok {
61 upstream.NonProxyHosts = jvmNonProxyHosts(s.value)
62 }
63 return upstream, true, nil
66// gradlePropertiesFiles lists the gradle.properties files of the Gradle user
67// home and installation, highest precedence first.
68func gradlePropertiesFiles(environ []string, dir string, args []string, cliProps map[string]configSetting) []string {
69 userHome := gradleUserHomeArg(args)
70 if userHome == "" {
71 var set bool
72 if s, ok := cliProps["gradle.user.home"]; ok {
73 userHome, set = s.value, true
74 } else {
75 userHome, set = envValue(environ, "GRADLE_USER_HOME")
76 }
77 switch {
78 case !set:
79 userHome = jvmJoin(javaUserHome(environ, nil), ".gradle")
80 case userHome == "":
81 // Gradle takes an empty user home as the working directory
82 // (verified against Gradle 9.4).
83 userHome = dir
84 }
85 }
86 files := []string{jvmJoin(userHome, "gradle.properties")}
87 bin, ok := lookPathIn(environ, "gradle")
88 if !ok {
89 dbg.Debugf("dependency firewall: not reading the gradle installation's gradle.properties, as no gradle is on PATH")
90 return files
91 }
92 resolved, err := filepath.EvalSymlinks(bin)
93 if err != nil {
94 dbg.Debugf("dependency firewall: not reading the gradle installation's gradle.properties, as the installation of %s is unknown: %v", bin, err)
95 return files
96 }
97 return append(files, filepath.Join(filepath.Dir(filepath.Dir(resolved)), "gradle.properties"))
100// gradleUserHomeArg returns the value of the last -g in args, as "-g dir",
101// "--gradle-user-home dir" or "--gradle-user-home=dir".
102func gradleUserHomeArg(args []string) string {
103 const long = "--gradle-user-home"
104 value := ""
105 for i, a := range args {
106 switch {
107 case (a == "-g" || a == long) && i+1 < len(args):
108 value = args[i+1]
109 case strings.HasPrefix(a, long+"="):
110 value = a[len(long)+1:]
111 }
112 }
113 return value
116// gradleSystemPropArgs returns the -D arguments of a Gradle command line,
117// rewriting "--system-prop k=v" and "-D k=v" as "-Dk=v".
118func gradleSystemPropArgs(args []string) []string {
119 var out []string
120 for i := 0; i < len(args); i++ {
121 switch a := args[i]; {
122 case (a == "-D" || a == "--system-prop") && i+1 < len(args):
123 i++
124 out = append(out, "-D"+args[i])
125 case strings.HasPrefix(a, "--system-prop="):
126 out = append(out, "-D"+a[len("--system-prop="):])
127 case strings.HasPrefix(a, "-D"):
128 out = append(out, a)
129 }
130 }
131 return out
134// jvmDefines returns the system properties set with -D in args, the last one
135// winning as in the JVM.
136func jvmDefines(args []string) map[string]string {
137 props := map[string]string{}
138 for _, a := range args {
139 kv, ok := strings.CutPrefix(a, "-D")
140 if !ok {
141 continue
142 }
143 k, v, _ := strings.Cut(kv, "=")
144 props[k] = v
145 }
146 return props
149// readGradleProperties returns the systemProp.* entries of a gradle.properties
150// file, keyed by the system property they set.
151func readGradleProperties(files configFiles, path string) (map[string]configSetting, error) {
152 data, ok, err := files.read(path)
153 if !ok {
154 return nil, err
155 }
156 props, err := parseJavaProperties(string(data))
157 if err != nil {
158 return nil, fmt.Errorf("failed to parse %s: %w", path, err)
159 }
160 layer := map[string]configSetting{}
161 for k, v := range props {
162 if name, ok := strings.CutPrefix(k, "systemProp."); ok {
163 layer[name] = configSetting{source: fileSource(path, k), value: v}
164 }
165 }
166 return layer, nil
169// parseJavaProperties parses the java.util.Properties format: "#" and "!"
170// comments, "=", ":" or whitespace between key and value, a trailing
171// backslash continuing the line, and backslash escapes including \uXXXX.
172// Leading whitespace of a value is dropped and trailing whitespace kept.
173func parseJavaProperties(data string) (map[string]string, error) {
174 props := map[string]string{}
175 lines := strings.Split(strings.ReplaceAll(data, "\r\n", "\n"), "\n")
176 for i := 0; i < len(lines); i++ {
177 line := strings.TrimLeft(lines[i], " \t\f")
178 if line == "" || line[0] == '#' || line[0] == '!' {
179 continue
180 }
181 for continues(line) && i+1 < len(lines) {
182 i++
183 line = line[:len(line)-1] + strings.TrimLeft(lines[i], " \t\f")
184 }
185 if continues(line) {
186 line = line[:len(line)-1]
187 }
188 keyEnd := len(line)
189 for j := 0; j < len(line); j++ {
190 if line[j] == '\\' {
191 j++
192 continue
193 }
194 if strings.IndexByte("=: \t\f", line[j]) >= 0 {
195 keyEnd = j
196 break
197 }
198 }
199 rest := strings.TrimLeft(line[keyEnd:], " \t\f")
200 if rest != "" && (rest[0] == '=' || rest[0] == ':') {
201 rest = strings.TrimLeft(rest[1:], " \t\f")
202 }
203 key, err := javaUnescape(line[:keyEnd])
204 if err != nil {
205 return nil, fmt.Errorf("line %d: %w", i+1, err)
206 }
207 value, err := javaUnescape(rest)
208 if err != nil {
209 return nil, fmt.Errorf("line %d: %w", i+1, err)
210 }
211 props[key] = value
212 }
213 return props, nil
216// continues reports whether line ends in an odd number of backslashes.
217func continues(line string) bool {
218 n := len(line) - len(strings.TrimRight(line, `\`))
219 return n%2 == 1
222func javaUnescape(s string) (string, error) {
223 if !strings.Contains(s, `\`) {
224 return s, nil
225 }
226 var b strings.Builder
227 for i := 0; i < len(s); i++ {
228 c := s[i]
229 if c != '\\' || i+1 == len(s) {
230 b.WriteByte(c)
231 continue
232 }
233 i++
234 switch s[i] {
235 case 't':
236 b.WriteByte('\t')
237 case 'n':
238 b.WriteByte('\n')
239 case 'r':
240 b.WriteByte('\r')
241 case 'f':
242 b.WriteByte('\f')
243 case 'u':
244 if i+5 > len(s) {
245 return "", errors.New(`malformed \uxxxx escape`)
246 }
247 r, err := strconv.ParseUint(s[i+1:i+5], 16, 16)
248 if err != nil {
249 return "", errors.New(`malformed \uxxxx escape`)
250 }
251 b.WriteRune(rune(r))
252 i += 4
253 default:
254 b.WriteByte(s[i])
255 }
256 }
257 return b.String(), nil
260// jvmNonProxyHosts converts a JVM http.nonProxyHosts value to
261// upstreamProxy.NonProxyHosts. The JVM honours "*" only at either end of a
262// pattern, so one with "*" inside never matches a host name and is dropped.
263func jvmNonProxyHosts(value string) []string {
264 var patterns []string
265 for p := range strings.SplitSeq(value, "|") {
266 inner := strings.TrimSuffix(strings.TrimPrefix(p, "*"), "*")
267 if p == "" || strings.Contains(inner, "*") {
268 continue
269 }
270 patterns = append(patterns, p)
271 }
272 return patterns
Diagnostics7 git calls · 391 ms
CallStatusSizeTime0391 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 · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=3
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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
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 · 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=361
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=1327
git.response.cache=HIT
git.response.duration_ms=2
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
fetch2002.9 KiB380 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=380
git.response.bytes=2940
git.response.cache=MISS
git.response.duration_ms=380
git.fetch.wants=bd42e1d18eddf6c8bb68e79ce848b5ddbee6abbf
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=2940
git.pkt.payload.max=2913
git.sideband.band1.bytes=2913
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
a5e1bae21fa46c75
Request
3161d999…
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