GitLabgitlab-org

cli

main
ply6 git calls33 ms98 KiB4 cachedprotocol v2
372 lines9.4 KiBRaw
1//go:build !integration
3package git
5import (
6 "bytes"
7 "fmt"
8 "io"
9 "net/url"
10 "path"
11 "path/filepath"
12 "reflect"
13 "slices"
14 "strings"
15 "testing"
17 "github.com/MakeNowJust/heredoc/v2"
18 "github.com/stretchr/testify/require"
21func Test_sshParser_read(t *testing.T) {
22 testFiles := map[string]string{
23 "/etc/ssh/config": heredoc.Doc(`
24 Include sites/*
25 `),
26 "/etc/ssh/sites/cfg1": heredoc.Doc(`
27 Host s1
28 Hostname=site1.net
29 `),
30 "/etc/ssh/sites/cfg2": heredoc.Doc(`
31 Host s2
32 Hostname = site2.net
33 `),
34 "HOME/.ssh/config": heredoc.Doc(`
35 Host *
36 Host gl gitlabopen
37 Hostname gitlab.com
38 #Hostname example.com
39 Host ex
40 Include ex_config/*
41 `),
42 "HOME/.ssh/ex_config/ex_cfg": heredoc.Doc(`
43 Hostname example.com
44 `),
45 }
46 globResults := map[string][]string{
47 "/etc/ssh/sites/*": {"/etc/ssh/sites/cfg1", "/etc/ssh/sites/cfg2"},
48 "HOME/.ssh/ex_config/*": {"HOME/.ssh/ex_config/ex_cfg"},
49 }
51 p := &sshParser{
52 homeDir: "HOME",
53 open: func(s string) (io.Reader, error) {
54 if contents, ok := testFiles[filepath.ToSlash(s)]; ok {
55 return bytes.NewBufferString(contents), nil
56 } else {
57 return nil, fmt.Errorf("no test file stub found: %q", s)
58 }
59 },
60 glob: func(p string) ([]string, error) {
61 if results, ok := globResults[filepath.ToSlash(p)]; ok {
62 return results, nil
63 } else {
64 return nil, fmt.Errorf("no glob stubs found: %q", p)
65 }
66 },
67 }
69 if err := p.read("/etc/ssh/config"); err != nil {
70 t.Fatalf("read(global config) = %v", err)
71 }
72 if err := p.read("HOME/.ssh/config"); err != nil {
73 t.Fatalf("read(user config) = %v", err)
74 }
76 if got := p.aliasMap["gl"]; got != "gitlab.com" {
77 t.Errorf("expected alias %q to expand to %q, got %q", "gl", "gitlab.com", got)
78 }
79 if got := p.aliasMap["gitlabopen"]; got != "gitlab.com" {
80 t.Errorf("expected alias %q to expand to %q, got %q", "gitlabopen", "gitlab.com", got)
81 }
82 if got := p.aliasMap["example.com"]; got != "" {
83 t.Errorf("expected alias %q to expand to %q, got %q", "example.com", "", got)
84 }
85 if got := p.aliasMap["ex"]; got != "example.com" {
86 t.Errorf("expected alias %q to expand to %q, got %q", "ex", "example.com", got)
87 }
88 if got := p.aliasMap["s1"]; got != "site1.net" {
89 t.Errorf("expected alias %q to expand to %q, got %q", "s1", "site1.net", got)
90 }
93func Test_sshParser_readContinuesAfterLongLine(t *testing.T) {
94 config := "#" + strings.Repeat("x", 70*1024) + "\nHost gl\nHostname gitlab.com\n"
95 p := &sshParser{
96 open: func(string) (io.Reader, error) {
97 return strings.NewReader(config), nil
98 },
99 }
101 err := p.read("config")
103 require.NoError(t, err)
104 require.Equal(t, "gitlab.com", p.aliasMap["gl"])
107func Test_sshParser_readFirstHostNameWins(t *testing.T) {
108 tests := map[string]struct {
109 files map[string]string
110 order []string
111 want SSHAliasMap
112 }{
113 "second block for the same alias in one file": {
114 files: map[string]string{
115 "HOME/.ssh/config": heredoc.Doc(`
116 Host gl
117 HostName first.example.com
118 Host gl
119 HostName second.example.com
120 `),
121 },
122 order: []string{"HOME/.ssh/config"},
123 want: SSHAliasMap{"gl": "first.example.com"},
124 },
125 "alias listed again with another alias": {
126 files: map[string]string{
127 "HOME/.ssh/config": heredoc.Doc(`
128 Host gl
129 HostName %h.example.com
130 Host gl gh
131 HostName shared.example.com
132 `),
133 },
134 order: []string{"HOME/.ssh/config"},
135 want: SSHAliasMap{"gl": "gl.example.com", "gh": "shared.example.com"},
136 },
137 "user config wins over system config": {
138 files: map[string]string{
139 "HOME/.ssh/config": heredoc.Doc(`
140 Host gl
141 HostName user.example.com
142 `),
143 "/etc/ssh/ssh_config": heredoc.Doc(`
144 Host gl
145 HostName system.example.com
146 Host sys
147 HostName only-system.example.com
148 `),
149 },
150 order: []string{"HOME/.ssh/config", "/etc/ssh/ssh_config"},
151 want: SSHAliasMap{"gl": "user.example.com", "sys": "only-system.example.com"},
152 },
153 }
155 for name, tt := range tests {
156 t.Run(name, func(t *testing.T) {
157 p := newStubSSHParser(tt.files)
158 for _, file := range tt.order {
159 require.NoError(t, p.read(file))
160 }
161 require.Equal(t, tt.want, p.aliasMap)
162 })
163 }
166func Test_sshParser_readMatchAndInclude(t *testing.T) {
167 tests := map[string]struct {
168 files map[string]string
169 want SSHAliasMap
170 }{
171 "Match line ends the Host block": {
172 files: map[string]string{
173 "HOME/.ssh/config": heredoc.Doc(`
174 Host gl
175 Match user bob
176 HostName special.example.com
177 Host gl
178 HostName normal.example.com
179 `),
180 },
181 want: SSHAliasMap{"gl": "normal.example.com"},
182 },
183 "Include in a Match block is skipped": {
184 files: map[string]string{
185 "HOME/.ssh/config": heredoc.Doc(`
186 Host gl
187 Match user bob
188 Include extra.conf
189 Host gl
190 HostName normal.example.com
191 `),
192 "HOME/.ssh/extra.conf": heredoc.Doc(`
193 Host gl
194 HostName special.example.com
195 `),
196 },
197 want: SSHAliasMap{"gl": "normal.example.com"},
198 },
199 "Include is read where it appears": {
200 files: map[string]string{
201 "HOME/.ssh/config": heredoc.Doc(`
202 Include extra.conf
203 Host gl
204 HostName main.example.com
205 `),
206 "HOME/.ssh/extra.conf": heredoc.Doc(`
207 Host gl
208 HostName included.example.com
209 `),
210 },
211 want: SSHAliasMap{"gl": "included.example.com"},
212 },
213 "Host line in an included file does not end the including block": {
214 files: map[string]string{
215 "HOME/.ssh/config": heredoc.Doc(`
216 Host gl
217 Include extra.conf
218 HostName main.example.com
219 `),
220 "HOME/.ssh/extra.conf": heredoc.Doc(`
221 Host *.internal
222 User git
223 `),
224 },
225 want: SSHAliasMap{"gl": "main.example.com"},
226 },
227 "Match line in an included file does not end the including block": {
228 files: map[string]string{
229 "HOME/.ssh/config": heredoc.Doc(`
230 Host gl
231 Include extra.conf
232 HostName main.example.com
233 `),
234 "HOME/.ssh/extra.conf": heredoc.Doc(`
235 Match user bob
236 HostName special.example.com
237 `),
238 },
239 want: SSHAliasMap{"gl": "main.example.com"},
240 },
241 "Include of two files returns to the including block after each": {
242 files: map[string]string{
243 "HOME/.ssh/config": heredoc.Doc(`
244 Host gl
245 Include a.conf b.conf
246 `),
247 "HOME/.ssh/a.conf": heredoc.Doc(`
248 Host *.internal
249 User git
250 `),
251 "HOME/.ssh/b.conf": heredoc.Doc(`
252 HostName main.example.com
253 `),
254 },
255 want: SSHAliasMap{"gl": "main.example.com"},
256 },
257 "Include pattern matching two files returns to the including block after each": {
258 files: map[string]string{
259 "HOME/.ssh/config": heredoc.Doc(`
260 Host gl
261 Include conf.d/*
262 `),
263 "HOME/.ssh/conf.d/a.conf": heredoc.Doc(`
264 Host *.internal
265 User git
266 `),
267 "HOME/.ssh/conf.d/b.conf": heredoc.Doc(`
268 HostName main.example.com
269 `),
270 },
271 want: SSHAliasMap{"gl": "main.example.com"},
272 },
273 }
275 for name, tt := range tests {
276 t.Run(name, func(t *testing.T) {
277 p := newStubSSHParser(tt.files)
278 require.NoError(t, p.read("HOME/.ssh/config"))
279 require.Equal(t, tt.want, p.aliasMap)
280 })
281 }
284func newStubSSHParser(files map[string]string) *sshParser {
285 return &sshParser{
286 homeDir: "HOME",
287 open: func(s string) (io.Reader, error) {
288 if contents, ok := files[filepath.ToSlash(s)]; ok {
289 return strings.NewReader(contents), nil
290 }
291 return nil, fmt.Errorf("no test file stub found: %q", s)
292 },
293 glob: func(pattern string) ([]string, error) {
294 var matches []string
295 for name := range files {
296 if ok, _ := path.Match(filepath.ToSlash(pattern), name); ok {
297 matches = append(matches, name)
298 }
299 }
300 if len(matches) == 0 {
301 return nil, fmt.Errorf("no glob stub found: %q", pattern)
302 }
303 // Map order is random, and filepath.Glob returns lexical order.
304 slices.Sort(matches)
305 return matches, nil
306 },
307 }
310func Test_sshParser_absolutePath(t *testing.T) {
311 dir := "HOME"
312 p := &sshParser{homeDir: dir}
314 tests := map[string]struct {
315 parentFile string
316 arg string
317 want string
318 wantErr bool
319 }{
320 "absolute path": {
321 parentFile: "/etc/ssh/ssh_config",
322 arg: "/etc/ssh/config",
323 want: "/etc/ssh/config",
324 },
325 "system relative path": {
326 parentFile: "/etc/ssh/config",
327 arg: "configs/*.conf",
328 want: filepath.Join(systemSSHDir, "configs", "*.conf"),
329 },
330 "user relative path": {
331 parentFile: filepath.Join(dir, ".ssh", "ssh_config"),
332 arg: "configs/*.conf",
333 want: filepath.Join(dir, ".ssh", "configs", "*.conf"),
334 },
335 "shell-like ~ rerefence": {
336 parentFile: filepath.Join(dir, ".ssh", "ssh_config"),
337 arg: "~/.ssh/*.conf",
338 want: filepath.Join(dir, ".ssh", "*.conf"),
339 },
340 }
342 for name, tt := range tests {
343 t.Run(name, func(t *testing.T) {
344 if got := p.absolutePath(tt.parentFile, tt.arg); got != tt.want {
345 t.Errorf("absolutePath(): %q, wants %q", got, tt.want)
346 }
347 })
348 }
351func Test_Translator(t *testing.T) {
352 t.Run("alias resolved", func(t *testing.T) {
353 tr := SSHAliasMap{"gl": "gitlab.com"}.Translator()
354 u, err := url.Parse("ssh://gl/o/r")
355 require.NoError(t, err)
356 require.Equal(t, "ssh://gitlab.com/o/r", tr(u).String())
357 })
359 t.Run("non-ssh scheme untouched", func(t *testing.T) {
360 tr := SSHAliasMap{"gl": "gitlab.com"}.Translator()
361 u, err := url.Parse("https://gl/o/r")
362 require.NoError(t, err)
363 require.Equal(t, "https://gl/o/r", tr(u).String())
364 })
367func eq(t *testing.T, got any, expected any) {
368 t.Helper()
369 if !reflect.DeepEqual(got, expected) {
370 t.Errorf("expected: %v, got: %v", expected, got)
371 }
Diagnostics6 git calls · 33 ms
CallStatusSizeTime033 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=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
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 · 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
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=25
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
fetch2002.2 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2204
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=75b9c9d789f32484449215c1cdaa15e72f9c8a67
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=2204
git.pkt.payload.max=2177
git.sideband.band1.bytes=2177
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
a5e1518e0e830acb
Request
e17cbe76…
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