GitLabgitlab-org
cli
main
1package pm
3import (
4 "errors"
5 "fmt"
6 "path/filepath"
7 "runtime"
8 "strings"
10 "github.com/pelletier/go-toml/v2"
11)
13// cargoUpstreamProxy follows cargo's http.proxy lookup (verified against
14// cargo 1.99): CARGO_HTTP_PROXY, then $CARGO_HOME's config, then git's
15// http.proxy. The .cargo/config(.toml) files from dir up are not read, and
16// neither is a $CARGO_HOME or git config inside the project. An empty value
17// means a direct connection. libcurl still skips the proxy for a host in
18// no_proxy or NO_PROXY. Config includes and --config are not read.
19func cargoUpstreamProxy(environ []string, dir string) (upstreamProxy, bool, error) {
20 upstream, ok, err := cargoConfiguredProxy(environ, dir)
21 if !ok || err != nil {
22 return upstreamProxy{}, false, err
23 }
24 upstream.NoProxy = envNoProxy(environ)
25 return upstream, true, nil
26}
28func cargoConfiguredProxy(environ []string, dir string) (upstreamProxy, bool, error) {
29 if value, ok := envValue(environ, "CARGO_HTTP_PROXY"); ok {
30 return upstreamProxy{Source: "CARGO_HTTP_PROXY", URL: value}, true, nil
31 }
33 cargoHome := lookupEnv(environ, "CARGO_HOME")
34 switch {
35 case cargoHome != "" && !filepath.IsAbs(cargoHome):
36 cargoHome = filepath.Join(dir, cargoHome)
37 case cargoHome == "":
38 if home := homeDir(environ); home != "" {
39 cargoHome = filepath.Join(home, ".cargo")
40 }
41 }
42 files := newConfigFiles(dir)
43 if cargoHome != "" {
44 file, data, ok, err := readCargoConfig(files, cargoHome)
45 if err != nil {
46 return upstreamProxy{}, false, err
47 }
48 if ok {
49 var config struct {
50 HTTP struct {
51 Proxy *string `toml:"proxy"`
52 } `toml:"http"`
53 }
54 if err := toml.Unmarshal(data, &config); err != nil {
55 return upstreamProxy{}, false, tomlParseError(file, err)
56 }
57 if config.HTTP.Proxy != nil {
58 return upstreamProxy{Source: fileSource(file, "http.proxy"), URL: *config.HTTP.Proxy}, true, nil
59 }
60 }
61 }
62 return gitConfigProxy(environ, files)
63}
65// tomlParseError is parseError for go-toml, whose errors can quote the file.
66func tomlParseError(file string, err error) error {
67 if de, ok := errors.AsType[*toml.DecodeError](err); ok {
68 row, _ := de.Position()
69 return fmt.Errorf("failed to parse %s line %d", file, row)
70 }
71 return fmt.Errorf("failed to parse %s", file)
72}
74// readCargoConfig reads dir/config, which cargo prefers, else
75// dir/config.toml.
76func readCargoConfig(files configFiles, dir string) (string, []byte, bool, error) {
77 for _, name := range []string{"config", "config.toml"} {
78 file := filepath.Join(dir, name)
79 data, ok, err := files.read(file)
80 if ok || err != nil {
81 return file, data, ok, err
82 }
83 }
84 return "", nil, false, nil
85}
87// gitConfigProxy returns http.proxy from the git config files cargo's libgit2
88// reads when cargo has no proxy of its own: ~/.gitconfig over the XDG git
89// config over /etc/gitconfig. include and includeIf are not followed.
90func gitConfigProxy(environ []string, files configFiles) (upstreamProxy, bool, error) {
91 var paths []string
92 home := homeDir(environ)
93 if home != "" {
94 paths = append(paths, filepath.Join(home, ".gitconfig"))
95 }
96 if xdg := lookupEnv(environ, "XDG_CONFIG_HOME"); xdg != "" {
97 paths = append(paths, filepath.Join(xdg, "git", "config"))
98 } else if home != "" {
99 paths = append(paths, filepath.Join(home, ".config", "git", "config"))
100 }
101 if runtime.GOOS != "windows" {
102 paths = append(paths, filepath.Join(systemRoot, "etc", "gitconfig"))
103 }
104 for _, file := range paths {
105 data, ok, err := files.read(file)
106 if err != nil {
107 return upstreamProxy{}, false, err
108 }
109 if !ok {
110 continue
111 }
112 value, found, err := gitConfigValue(data, "http", "proxy")
113 if err != nil {
114 return upstreamProxy{}, false, parseError(file, err)
115 }
116 if found {
117 return upstreamProxy{Source: fileSource(file, "http.proxy"), URL: value}, true, nil
118 }
119 }
120 return upstreamProxy{}, false, nil
121}
123// gitConfigValue returns the last value of section.key in a git config file,
124// outside any subsection. Section and key names match case-insensitively.
125func gitConfigValue(data []byte, section, key string) (string, bool, error) {
126 var value string
127 var found, inSection bool
128 lines := strings.Split(string(data), "\n")
129 for i := 0; i < len(lines); i++ {
130 n, line := i, lines[i]
131 // n stays the line the entry starts on.
132 for i+1 < len(lines) && gitLineContinues(line) {
133 i++
134 line = strings.TrimSuffix(strings.TrimSuffix(line, "\r"), `\`) + lines[i]
135 }
136 line = strings.TrimSpace(line)
137 if line == "" || line[0] == '#' || line[0] == ';' {
138 continue
139 }
140 if line[0] == '[' {
141 end := strings.IndexByte(line, ']')
142 if end < 0 {
143 return "", false, fmt.Errorf("line %d: invalid section header", n+1)
144 }
145 inSection = strings.EqualFold(strings.TrimSpace(line[1:end]), section)
146 line = strings.TrimSpace(line[end+1:])
147 if line == "" || line[0] == '#' || line[0] == ';' {
148 continue
149 }
150 }
151 name, raw, hasValue := strings.Cut(line, "=")
152 if !inSection || !strings.EqualFold(strings.TrimSpace(name), key) {
153 continue
154 }
155 if !hasValue {
156 value, found = "true", true
157 continue
158 }
159 v, err := gitConfigUnquote(raw)
160 if err != nil {
161 return "", false, fmt.Errorf("line %d: %w", n+1, err)
162 }
163 value, found = v, true
164 }
165 return value, found, nil
166}
168// gitLineContinues reports whether line ends in a backslash that joins it to
169// the next line, as in git: an unescaped one that isn't inside a comment.
170func gitLineContinues(line string) bool {
171 quoted, escaped := false, false
172 for _, c := range strings.TrimSuffix(line, "\r") {
173 switch {
174 case escaped:
175 escaped = false
176 case c == '\\':
177 escaped = true
178 case c == '"':
179 quoted = !quoted
180 case !quoted && (c == '#' || c == ';'):
181 return false
182 }
183 }
184 return escaped
185}
187// gitConfigUnquote decodes a git config value: double quotes group text,
188// backslash escapes \\, \", \n, \t and \b, and an unquoted "#" or ";"
189// starts a comment.
190func gitConfigUnquote(raw string) (string, error) {
191 var b strings.Builder
192 quoted, escaped := false, false
193 for _, c := range strings.TrimSpace(raw) {
194 switch {
195 case escaped:
196 escaped = false
197 switch c {
198 case '\\', '"':
199 b.WriteRune(c)
200 case 'n':
201 b.WriteByte('\n')
202 case 't':
203 b.WriteByte('\t')
204 case 'b':
205 if s := b.String(); s != "" {
206 b.Reset()
207 b.WriteString(s[:len(s)-1])
208 }
209 default:
210 return "", errors.New("invalid escape")
211 }
212 case c == '\\':
213 escaped = true
214 case c == '"':
215 quoted = !quoted
216 case !quoted && (c == '#' || c == ';'):
217 return strings.TrimSpace(b.String()), nil
218 default:
219 b.WriteRune(c)
220 }
221 }
222 if quoted || escaped {
223 return "", errors.New("unterminated value")
224 }
225 return strings.TrimSpace(b.String()), nil
226}
Diagnostics7 git calls · 190 ms
CallStatusSizeTime0190 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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=2 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=2 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: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=1 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=44Open pack map
fetchtree:0200361 B · hit1 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=1 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=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:02001.3 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=181 git.response.bytes=1327 git.response.cache=HIT git.response.duration_ms=3 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=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2002.3 KiB181 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2315 git.response.cache=MISS git.response.duration_ms=181 git.fetch.wants=05344efe3846a45ddb45c320911bc62d3ed12b12 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=2315 git.pkt.payload.max=2288 git.sideband.band1.bytes=2288 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
- a5e2c5c63ca74afc
- Request
- 921a2348…
- 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