GitLabgitlab-org
cli
main
1package pm
3import (
4 "encoding/json"
5 "errors"
6 "io/fs"
7 "os"
8 "os/exec"
9 "os/user"
10 "path/filepath"
11 "regexp"
12 "strconv"
13 "strings"
15 "gitlab.com/gitlab-org/cli/internal/dbg"
16)
18// systemRoot is the root of the machine-wide files pip and git read, such as
19// /etc/gitconfig. Tests point it at an empty directory.
20var systemRoot = string(filepath.Separator)
22// currentUser finds the account whose home directory the JVM uses for
23// user.home. Tests point it at an empty directory.
24var currentUser = user.Current
26// configFiles reads the user and global config files glab takes proxy
27// settings from. It skips any file inside the project being installed: the
28// git work tree that holds the working directory, else the working directory.
29type configFiles struct {
30 // root is the project directory, or nil when it cannot be found, in
31 // which case no file is read.
32 root fs.FileInfo
33}
35func newConfigFiles(dir string) configFiles {
36 resolved, err := filepath.EvalSymlinks(dir)
37 if err != nil {
38 dbg.Debugf("dependency firewall: not reading proxy config files, the project directory is unknown: %v", err)
39 return configFiles{}
40 }
41 root := resolved
42 for p := range ancestors(resolved) {
43 if exists(filepath.Join(p, ".git")) {
44 root = p
45 break
46 }
47 }
48 info, err := os.Stat(root)
49 if err != nil {
50 dbg.Debugf("dependency firewall: not reading proxy config files, the project directory is unknown: %v", err)
51 return configFiles{}
52 }
53 return configFiles{root: info}
54}
56// resolvePath returns path as the OS opens it. A relative path is joined to
57// the working directory without cleaning, so that EvalSymlinks applies a ".."
58// after a symlink to the symlink's target, as the OS does.
59func resolvePath(path string) (string, error) {
60 if !filepath.IsAbs(path) {
61 wd, err := os.Getwd()
62 if err != nil {
63 return "", err
64 }
65 path = wd + string(filepath.Separator) + path
66 }
67 return filepath.EvalSymlinks(path)
68}
70// inProject reports whether path is inside the project. Each directory above
71// the resolved path is compared with the project root by identity, so neither
72// a symlink nor a differently cased spelling hides the project. It reports
73// true when that cannot be decided.
74func (c configFiles) inProject(path string) bool {
75 if c.root == nil {
76 return true
77 }
78 resolved, err := resolvePath(path)
79 if err != nil {
80 return true
81 }
82 for p := range ancestors(resolved) {
83 info, err := os.Stat(p)
84 if err != nil || os.SameFile(info, c.root) {
85 return true
86 }
87 }
88 return false
89}
91// read returns the contents of the config file at path, and false when it
92// does not exist or may be inside the project. It opens the resolved path it
93// checked, so the file read is always the file checked.
94func (c configFiles) read(path string) ([]byte, bool, error) {
95 if _, err := os.Lstat(path); errors.Is(err, fs.ErrNotExist) {
96 return nil, false, nil
97 }
98 resolved, err := resolvePath(path)
99 if err != nil {
100 dbg.Debugf("dependency firewall: not reading proxy settings from %s: %v", path, err)
101 return nil, false, nil
102 }
103 if c.inProject(resolved) {
104 dbg.Debugf("dependency firewall: not reading proxy settings from %s, which may be inside the project", path)
105 return nil, false, nil
106 }
107 return readConfigFile(resolved)
108}
110func isFile(path string) bool {
111 info, err := os.Stat(path)
112 return err == nil && info.Mode().IsRegular()
113}
115// lookPathIn finds the executable named name in the PATH of environ.
116func lookPathIn(environ []string, name string) (string, bool) {
117 for _, dir := range filepath.SplitList(lookupEnv(environ, "PATH")) {
118 if dir == "" {
119 continue
120 }
121 if path, err := exec.LookPath(filepath.Join(dir, name)); err == nil {
122 return path, true
123 }
124 }
125 return "", false
126}
128// envValue returns the last value of key in environ, and whether it is set
129// at all, for the settings where an empty value differs from an absent one.
130func envValue(environ []string, key string) (string, bool) {
131 value, found := "", false
132 for _, e := range environ {
133 if k, v, ok := strings.Cut(e, "="); ok && k == key {
134 value, found = v, true
135 }
136 }
137 return value, found
138}
140// lineRe finds the line number that YAML and other parser errors carry.
141var lineRe = regexp.MustCompile(`line (\d+)`)
143// parseError reports that path failed to parse, at the line err names when it
144// names one. It never includes err's text, which can quote the file, and so
145// credentials in a proxy URL.
146func parseError(path string, err error) error {
147 if m := lineRe.FindStringSubmatch(err.Error()); m != nil {
148 return errors.New("failed to parse " + path + " line " + m[1])
149 }
150 return errors.New("failed to parse " + path)
151}
153// jsonParseError is parseError for encoding/json errors, which carry a byte
154// offset instead of a line.
155func jsonParseError(path string, data []byte, err error) error {
156 var offset int64 = -1
157 if se, ok := errors.AsType[*json.SyntaxError](err); ok {
158 offset = se.Offset
159 } else if te, ok := errors.AsType[*json.UnmarshalTypeError](err); ok {
160 offset = te.Offset
161 }
162 if offset < 0 || offset > int64(len(data)) {
163 return errors.New("failed to parse " + path)
164 }
165 line := 1 + strings.Count(string(data[:offset]), "\n")
166 return errors.New("failed to parse " + path + " line " + strconv.Itoa(line))
167}
169// parseIni reads the top-level keys of an npm-style ini file (.npmrc,
170// auth.ini) as npm's ini package parses them, with values not yet
171// env-expanded. A key written as key[] collects a comma-separated list.
172func parseIni(data []byte) map[string]string {
173 values := map[string]string{}
174 for _, line := range strings.FieldsFunc(string(data), func(r rune) bool { return r == '\r' || r == '\n' }) {
175 trimmed := strings.TrimSpace(line)
176 if trimmed == "" || trimmed[0] == ';' || trimmed[0] == '#' {
177 continue
178 }
179 if line[0] == '[' && strings.HasSuffix(trimmed, "]") {
180 // Keys below a section header belong to that section, never to
181 // the top level the managers read settings from.
182 break
183 }
184 rawKey, rawValue, hasValue := strings.Cut(line, "=")
185 key := iniUnquote(rawKey)
186 value := "true"
187 if hasValue {
188 value = iniUnquote(rawValue)
189 }
190 if k, isList := strings.CutSuffix(key, "[]"); isList && k != "" {
191 key = k
192 if prev, ok := values[key]; ok {
193 value = prev + "," + value
194 }
195 }
196 values[key] = value
197 }
198 return values
199}
201// iniUnquote decodes an ini key or value as npm's ini package does: a quoted
202// string is JSON-decoded; otherwise an unescaped ";" or "#" starts a comment
203// and "\;", "\#" and "\\" are escapes.
204func iniUnquote(s string) string {
205 s = strings.TrimSpace(s)
206 if len(s) >= 2 && (s[0] == '"' && s[len(s)-1] == '"' || s[0] == '\'' && s[len(s)-1] == '\'') {
207 if s[0] == '\'' {
208 s = s[1 : len(s)-1]
209 }
210 var decoded string
211 if json.Unmarshal([]byte(s), &decoded) == nil {
212 return decoded
213 }
214 return s
215 }
216 var b strings.Builder
217 escaped := false
218 for _, c := range s {
219 switch {
220 case escaped:
221 if !strings.ContainsRune(`\;#`, c) {
222 b.WriteByte('\\')
223 }
224 b.WriteRune(c)
225 escaped = false
226 case c == ';' || c == '#':
227 return strings.TrimSpace(b.String())
228 case c == '\\':
229 escaped = true
230 default:
231 b.WriteRune(c)
232 }
233 }
234 if escaped {
235 b.WriteByte('\\')
236 }
237 return strings.TrimSpace(b.String())
238}
240var npmEnvRefRe = regexp.MustCompile(`(\\*)\$\{([^${}?]+)(\?)?\}`)
242// npmEnvReplace expands ${NAME} and ${NAME?} in an npm config value as npm
243// does: an unset NAME stays literal, or becomes empty with "?", and an odd
244// run of backslashes before "$" escapes the reference. It also reports
245// whether a reference without "?" named an unset variable.
246func npmEnvReplace(s string, environ []string) (string, bool) {
247 missing := false
248 out := npmEnvRefRe.ReplaceAllStringFunc(s, func(match string) string {
249 m := npmEnvRefRe.FindStringSubmatch(match)
250 esc, name, optional := m[1], m[2], m[3] == "?"
251 if len(esc)%2 == 1 {
252 return match[(len(esc)+1)/2:]
253 }
254 value, ok := envValue(environ, name)
255 if !ok && !optional {
256 missing = true
257 value = "${" + name + "}"
258 }
259 return esc[:len(esc)/2] + value
260 })
261 return out, missing
262}
264var shellEnvRefRe = regexp.MustCompile(`\$\{(\w+)(?:(:?-)([^}]*))?\}`)
266// errUnsetEnv reports a ${NAME} reference without a default to an unset
267// variable. It does not name the variable, which comes from the file.
268var errUnsetEnv = errors.New("a value names an unset environment variable")
270// shellEnvReplace expands ${NAME}, ${NAME-default} and ${NAME:-default}, the
271// syntax pnpm and Yarn Berry share. ":-" also replaces an empty value.
272func shellEnvReplace(s string, environ []string) (string, error) {
273 var err error
274 out := shellEnvRefRe.ReplaceAllStringFunc(s, func(match string) string {
275 m := shellEnvRefRe.FindStringSubmatch(match)
276 name, op, fallback := m[1], m[2], m[3]
277 value, ok := envValue(environ, name)
278 switch {
279 case op == ":-" && value == "", op == "-" && !ok:
280 return fallback
281 case !ok && op == "":
282 err = errUnsetEnv
283 }
284 return value
285 })
286 return out, err
287}
Diagnostics7 git calls · 319 ms
CallStatusSizeTime0319 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB2 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=UPDATING 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
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: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=1 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
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=310 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.3 KiB310 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3388 git.response.cache=MISS git.response.duration_ms=310 git.fetch.wants=202b7961b3602e3c0dd3bbdb548c7c06e30c4edb 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=3388 git.pkt.payload.max=3361 git.sideband.band1.bytes=3361 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
- a5e2c5a96fc54afc
- Request
- eca384bc…
- 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