GitLabgitlab-org

cli

main
ply6 git calls210 ms97 KiB4 cachedprotocol v2
186 lines4.1 KiBRaw
1package git
3import (
4 "bufio"
5 "io"
6 "net/url"
7 "os"
8 "path/filepath"
9 "regexp"
10 "strings"
13// Named rather than inlined so filepath.Join does not receive a literal
14// carrying its own separators, which gocritic's filepathJoin rejects.
15const systemSSHDir = "/etc/ssh"
17var (
18 sshConfigLineRE = regexp.MustCompile(`\A\s*(?P<keyword>[A-Za-z][A-Za-z0-9]*)(?:\s+|\s*=\s*)(?P<argument>.+)`)
19 sshTokenRE = regexp.MustCompile(`%[%h]`)
22// SSHAliasMap encapsulates the translation of SSH hostname aliases
23type SSHAliasMap map[string]string
25// Translator returns a function that applies hostname aliases to URLs
26func (m SSHAliasMap) Translator() func(*url.URL) *url.URL {
27 return func(u *url.URL) *url.URL {
28 if u.Scheme != "ssh" {
29 return u
30 }
31 resolvedHost, ok := m[u.Hostname()]
32 if !ok {
33 return u
34 }
35 newURL, _ := url.Parse(u.String())
36 newURL.Host = resolvedHost
37 return newURL
38 }
41type sshParser struct {
42 homeDir string
44 aliasMap SSHAliasMap
45 hosts []string
47 open func(string) (io.Reader, error)
48 glob func(string) ([]string, error)
51func (p *sshParser) read(fileName string) error {
52 var file io.Reader
53 if p.open == nil {
54 f, err := os.Open(fileName)
55 if err != nil {
56 return err
57 }
58 defer f.Close()
59 file = f
60 } else {
61 var err error
62 file, err = p.open(fileName)
63 if err != nil {
64 return err
65 }
66 }
68 if len(p.hosts) == 0 {
69 p.hosts = []string{"*"}
70 }
72 scanner := bufio.NewScanner(file)
73 scanner.Buffer(nil, 1024*1024)
74 for scanner.Scan() {
75 m := sshConfigLineRE.FindStringSubmatch(scanner.Text())
76 if len(m) < 3 {
77 continue
78 }
80 keyword, arguments := strings.ToLower(m[1]), m[2]
81 switch keyword {
82 case "host":
83 p.hosts = strings.Fields(arguments)
84 case "match":
85 // ssh applies a Match block only when its criteria hold, which the
86 // parser does not evaluate, so no host takes its HostName.
87 p.hosts = nil
88 case "hostname":
89 for _, host := range p.hosts {
90 for name := range strings.FieldsSeq(arguments) {
91 if p.aliasMap == nil {
92 p.aliasMap = make(SSHAliasMap)
93 }
94 // Like ssh, keep the first value obtained for a host.
95 if _, ok := p.aliasMap[host]; !ok {
96 p.aliasMap[host] = sshExpandTokens(name, host)
97 }
98 }
99 }
100 case "include":
101 // A file included from a Match block is as conditional as the block.
102 if p.hosts == nil {
103 continue
104 }
105 // Like ssh, return to the including block after each file, so a
106 // Host or Match line in an included file does not end it.
107 hosts := p.hosts
108 for arg := range strings.FieldsSeq(arguments) {
109 path := p.absolutePath(fileName, arg)
111 var fileNames []string
112 if p.glob == nil {
113 paths, _ := filepath.Glob(path)
114 for _, p := range paths {
115 if s, err := os.Stat(p); err == nil && !s.IsDir() {
116 fileNames = append(fileNames, p)
117 }
118 }
119 } else {
120 var err error
121 fileNames, err = p.glob(path)
122 if err != nil {
123 continue
124 }
125 }
127 for _, fileName := range fileNames {
128 _ = p.read(fileName)
129 p.hosts = hosts
130 }
131 }
132 }
133 }
135 return scanner.Err()
138func (p *sshParser) absolutePath(parentFile, path string) string {
139 if filepath.IsAbs(path) || strings.HasPrefix(filepath.ToSlash(path), "/") {
140 return path
141 }
143 if after, ok := strings.CutPrefix(path, "~"); ok {
144 return filepath.Join(p.homeDir, after)
145 }
147 if strings.HasPrefix(filepath.ToSlash(parentFile), systemSSHDir) {
148 return filepath.Join(systemSSHDir, path)
149 }
151 return filepath.Join(p.homeDir, ".ssh", path)
154// ParseSSHConfig constructs a map of SSH hostname aliases based on user and
155// system configuration files
156func ParseSSHConfig() SSHAliasMap {
157 configFiles := []string{
158 "/etc/ssh_config",
159 "/etc/ssh/ssh_config",
160 }
162 p := sshParser{}
164 if homeDir, err := os.UserHomeDir(); err == nil {
165 userConfig := filepath.Join(homeDir, ".ssh", "config")
166 configFiles = append([]string{userConfig}, configFiles...)
167 p.homeDir = homeDir
168 }
170 for _, file := range configFiles {
171 _ = p.read(file)
172 }
173 return p.aliasMap
176func sshExpandTokens(text, host string) string {
177 return sshTokenRE.ReplaceAllStringFunc(text, func(match string) string {
178 switch match {
179 case "%h":
180 return host
181 case "%%":
182 return "%"
183 }
184 return ""
185 })
Diagnostics6 git calls · 210 ms
CallStatusSizeTime0210 ms
discover200185 B1 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=1
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 · hit1 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=1
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=1
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 · 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:0200510 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=510
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a3147172443266d011121808a125248c5c915972
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=510
git.pkt.payload.max=483
git.sideband.band1.bytes=483
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=483
git.pack.trailer=4ef234d86a93490a6a3da3549aa13a474a5c9eac
Open pack map
fetch2001.7 KiB203 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=203
git.response.bytes=1778
git.response.cache=MISS
git.response.duration_ms=203
git.fetch.wants=fb542dec73bd54a99c7e007acd3e838847ed8ff1
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=1778
git.pkt.payload.max=1751
git.sideband.band1.bytes=1751
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
a5e13016cd36a9fe
Request
b1d27a76…
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