GitLabgitlab-org
cli
main
1package pm
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "runtime"
8 "slices"
9 "strings"
10)
12// pipUpstreamProxy follows pip 26's proxy lookup: --isolated aside,
13// PIP_PROXY, then the command's section ([install], [download], ...) of the
14// config files, then their [global] section. Within a section the last file
15// in pip's load order wins, and an empty value there leaves the section unset.
16// A proxy from any of these makes pip ignore HTTPS_PROXY and NO_PROXY.
17//
18// command is "" for pipenv, whose resolver builds pip's options under a
19// command name no config section matches.
20func pipUpstreamProxy(environ []string, dir, binary, command string, isolated bool) (upstreamProxy, bool, error) {
21 if !isolated {
22 if key, value, ok := firstSetEnv(environ, false, "PIP_PROXY"); ok {
23 return upstreamProxy{Source: key, URL: value}, true, nil
24 }
25 }
27 files := newConfigFiles(dir)
28 var global, commandProxy configSetting
29 for _, file := range pipConfigFiles(environ, dir, binary, isolated) {
30 data, ok, err := files.read(file)
31 if err != nil {
32 return upstreamProxy{}, false, err
33 }
34 if !ok {
35 continue
36 }
37 sections, err := parsePipConfig(data)
38 if err != nil {
39 return upstreamProxy{}, false, parseError(file, err)
40 }
41 if v, ok := sections["global"]["proxy"]; ok {
42 global = configSetting{source: fileSource(file, "global.proxy"), value: v}
43 }
44 if v, ok := sections[command]["proxy"]; ok && command != "" {
45 commandProxy = configSetting{source: fileSource(file, command+".proxy"), value: v}
46 }
47 }
48 for _, s := range []configSetting{commandProxy, global} {
49 if s.value != "" {
50 return s.proxy(), true, nil
51 }
52 }
53 return upstreamProxy{}, false, nil
54}
56// pipValueOptions are pip's general options that take a value (pip 26.0).
57var pipValueOptions = map[string]struct{}{
58 "--cache-dir": {}, "--cert": {}, "--client-cert": {}, "--default-timeout": {},
59 "--exists-action": {}, "--keyring-provider": {}, "--local-log": {}, "--log": {},
60 "--log-file": {}, "--proxy": {}, "--python": {}, "--resume-retries": {},
61 "--retries": {}, "--timeout": {}, "--trusted-host": {}, "--use-deprecated": {},
62 "--use-feature": {},
63}
65// pipCommand returns pip's subcommand, the first argument that is neither an
66// option nor an option's value, which names the config section pip reads
67// after [global].
68func pipCommand(args []string) string {
69 for i := 0; i < len(args); i++ {
70 if !strings.HasPrefix(args[i], "-") {
71 return args[i]
72 }
73 if _, ok := pipValueOptions[args[i]]; ok {
74 i++
75 }
76 }
77 return ""
78}
80// isPipProxyOption reports whether name is --proxy or an abbreviation pip can
81// read as it, which leaves out --p because --python shares it.
82func isPipProxyOption(name string) bool {
83 const option = "--proxy"
84 return len(name) >= len("--pr") && len(name) <= len(option) && name == option[:len(name)]
85}
87// pipProxyArg returns the value of the last --proxy in args and args without
88// any --proxy, or false when there is none. It takes an abbreviation for every
89// subcommand, even where pip calls it ambiguous, so no subcommand or pip
90// version lets a --proxy through. Only the full names of pip's general options
91// are modeled: a --proxy that pip reads as another option's value, as after
92// -r, is still taken, and so is one after --, which pip reads as an option's
93// value after --cache-d or -t. Both are harmless because traffic stays
94// inspected.
95func pipProxyArg(args []string) (string, []string, bool) {
96 var value string
97 var found bool
98 rest := make([]string, 0, len(args))
99 for i := 0; i < len(args); i++ {
100 name, inline, hasInline := strings.Cut(args[i], "=")
101 switch {
102 case !isPipProxyOption(name):
103 rest = append(rest, args[i])
104 if _, ok := pipValueOptions[args[i]]; ok && i+1 < len(args) {
105 i++
106 rest = append(rest, args[i])
107 }
108 case hasInline:
109 value, found = inline, true
110 case i+1 < len(args):
111 i++
112 value, found = args[i], true
113 default:
114 rest = append(rest, args[i])
115 }
116 }
117 return value, rest, found
118}
120// pipConfigFiles lists the files pip reads, lowest precedence first: the
121// global files, the user files, the site file of the virtualenv the pip on
122// PATH belongs to, then PIP_CONFIG_FILE. PIP_CONFIG_FILE set to the null
123// device disables every file; set to an existing file, it replaces the user
124// files. --isolated also skips the user files.
125func pipConfigFiles(environ []string, dir, binary string, isolated bool) []string {
126 configFile := lookupEnv(environ, "PIP_CONFIG_FILE")
127 if configFile == os.DevNull {
128 return nil
129 }
130 if configFile != "" && !filepath.IsAbs(configFile) {
131 configFile = filepath.Join(dir, configFile)
132 }
134 name := "pip.conf"
135 if runtime.GOOS == "windows" {
136 name = "pip.ini"
137 }
138 home := homeDir(environ)
139 var files, user []string
140 switch runtime.GOOS {
141 case "windows":
142 if programData := lookupEnv(environ, "PROGRAMDATA"); programData != "" {
143 files = append(files, filepath.Join(programData, "pip", name))
144 }
145 if home != "" {
146 user = append(user, filepath.Join(home, "pip", name))
147 }
148 if appData := lookupEnv(environ, "APPDATA"); appData != "" {
149 user = append(user, filepath.Join(appData, "pip", name))
150 }
151 case "darwin":
152 files = append(files, filepath.Join(systemRoot, "Library", "Application Support", "pip", name))
153 if home != "" {
154 // pip uses the Application Support file whenever its directory
155 // exists, and ~/.config only otherwise.
156 user = append(user, filepath.Join(home, ".pip", name))
157 appSupport := filepath.Join(home, "Library", "Application Support", "pip")
158 if isDir(appSupport) {
159 user = append(user, filepath.Join(appSupport, name))
160 } else {
161 user = append(user, filepath.Join(home, ".config", "pip", name))
162 }
163 }
164 default:
165 xdgDirs := lookupEnv(environ, "XDG_CONFIG_DIRS")
166 if xdgDirs == "" {
167 xdgDirs = filepath.Join(systemRoot, "etc", "xdg")
168 }
169 for _, d := range filepath.SplitList(xdgDirs) {
170 files = append(files, filepath.Join(d, "pip", name))
171 }
172 files = append(files, filepath.Join(systemRoot, "etc", name))
173 if home != "" {
174 user = append(user, filepath.Join(home, ".pip", name))
175 }
176 if xdgHome := lookupEnv(environ, "XDG_CONFIG_HOME"); xdgHome != "" {
177 user = append(user, filepath.Join(xdgHome, "pip", name))
178 } else if home != "" {
179 user = append(user, filepath.Join(home, ".config", "pip", name))
180 }
181 }
183 if !isolated && (configFile == "" || !isFile(configFile)) {
184 files = append(files, user...)
185 }
186 if prefix, ok := pipVirtualenv(environ, binary); ok {
187 files = append(files, filepath.Join(prefix, name))
188 }
189 if configFile != "" {
190 files = append(files, configFile)
191 }
192 return files
193}
195// pipVirtualenv returns the virtualenv holding the binary found on PATH,
196// which is pip's sys.prefix and so where its site config file lives. pip
197// outside a virtualenv has a site file under the Python installation, which
198// is not read here.
199func pipVirtualenv(environ []string, binary string) (string, bool) {
200 if binary != "pip" {
201 return "", false
202 }
203 path, ok := lookPathIn(environ, binary)
204 if !ok {
205 return "", false
206 }
207 prefix := filepath.Dir(filepath.Dir(path))
208 return prefix, isFile(filepath.Join(prefix, "pyvenv.cfg"))
209}
211// parsePipConfig parses a pip config file as Python's RawConfigParser does,
212// with pip's key normalization: keys are lowercased, "_" means "-" and a
213// leading "--" is dropped. Like pip, it rejects a duplicate section or key.
214func parsePipConfig(data []byte) (map[string]map[string]string, error) {
215 sections := map[string]map[string]string{}
216 var section map[string]string
217 var rawKeys map[string]struct{}
218 var lastKey string
219 lastIndent := -1
220 for n, line := range strings.Split(string(data), "\n") {
221 line = strings.TrimRight(line, "\r")
222 trimmed := strings.TrimSpace(line)
223 indent := len(line) - len(strings.TrimLeft(line, " \t"))
224 switch {
225 case trimmed == "" || trimmed[0] == '#' || trimmed[0] == ';':
226 continue
227 case lastKey != "" && indent > lastIndent:
228 section[lastKey] += "\n" + trimmed
229 continue
230 }
231 lastKey = ""
232 if strings.HasPrefix(trimmed, "[") {
233 end := strings.LastIndex(trimmed, "]")
234 if end < 2 {
235 return nil, fmt.Errorf("line %d: invalid section header", n+1)
236 }
237 name := trimmed[1:end]
238 if _, dup := sections[name]; dup {
239 return nil, fmt.Errorf("line %d: duplicate section", n+1)
240 }
241 section, rawKeys = map[string]string{}, map[string]struct{}{}
242 sections[name] = section
243 continue
244 }
245 if section == nil {
246 return nil, fmt.Errorf("line %d: key outside a section", n+1)
247 }
248 i := strings.IndexAny(trimmed, "=:")
249 if i <= 0 {
250 return nil, fmt.Errorf("line %d: expected key = value", n+1)
251 }
252 raw := strings.ToLower(strings.TrimSpace(trimmed[:i]))
253 if _, dup := rawKeys[raw]; dup {
254 return nil, fmt.Errorf("line %d: duplicate key", n+1)
255 }
256 rawKeys[raw] = struct{}{}
257 key := strings.ReplaceAll(strings.TrimPrefix(raw, "--"), "_", "-")
258 section[key] = strings.TrimSpace(trimmed[i+1:])
259 lastKey, lastIndent = key, indent
260 }
261 return sections, nil
262}
264// pipIsolated reports whether args carry pip's --isolated, which pip checks
265// for literally.
266func pipIsolated(args []string) bool { return slices.Contains(args, "--isolated") }
Diagnostics7 git calls · 228 ms
CallStatusSizeTime0228 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 KiB3 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=UPDATING 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=44Open 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=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=2 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
fetch2003.1 KiB216 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=216 git.response.bytes=3222 git.response.cache=MISS git.response.duration_ms=216 git.fetch.wants=0548de9d6873c2577b898fe9684cd80211d548ac 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=3222 git.pkt.payload.max=3195 git.sideband.band1.bytes=3195 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
- a5e1bdb11b436c75
- Request
- 1497a789…
- 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