GitLabgitlab-org

cli

main
ply6 git calls27 ms101 KiB5 cachedprotocol v2
880 lines27 KiBRaw
1//go:build !integration
3package cmdutils
5import (
6 "cmp"
7 "testing"
9 "github.com/MakeNowJust/heredoc/v2"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
13 "gitlab.com/gitlab-org/cli/internal/config"
14 "gitlab.com/gitlab-org/cli/internal/git"
17// noSSHAliases injects an empty ~/.ssh/config into the resolver, so tests that do not
18// exercise SSH-alias translation stay independent of the developer's real config.
19func noSSHAliases() git.SSHAliasMap { return nil }
21func Test_remoteResolver(t *testing.T) {
22 rr := &remoteResolver{
23 readRemotes: func() (git.RemoteSet, error) {
24 return git.RemoteSet{
25 git.NewRemote("fork", "https://example.org/owner/fork.git"),
26 git.NewRemote("origin", "https://gitlab.com/owner/repo.git"),
27 git.NewRemote("upstream", "https://example.org/owner/repo.git"),
28 }, nil
29 },
30 getConfig: func() config.Config {
31 return config.NewFromString(heredoc.Doc(`
32 hosts:
33 example.org:
34 oauth_token: OTOKEN
35 `))
36 },
37 parseSSHConfig: noSSHAliases,
38 }
40 resolver := rr.Resolver("")
41 remotes, err := resolver()
42 require.NoError(t, err)
43 require.Len(t, remotes, 2)
45 assert.Equal(t, "upstream", remotes[0].Name)
46 assert.Equal(t, "fork", remotes[1].Name)
49func Test_remoteResolverOverride(t *testing.T) {
50 rr := &remoteResolver{
51 readRemotes: func() (git.RemoteSet, error) {
52 return git.RemoteSet{
53 git.NewRemote("fork", "https://example.org/ghe-owner/ghe-fork.git"),
54 git.NewRemote("origin", "https://gitlab.com/owner/repo.git"),
55 git.NewRemote("upstream", "https://example.org/ghe-owner/ghe-repo.git"),
56 }, nil
57 },
58 getConfig: func() config.Config {
59 return config.NewFromString(heredoc.Doc(`
60 hosts:
61 example.org:
62 oauth_token: GHETOKEN
63 `))
64 },
65 parseSSHConfig: noSSHAliases,
66 }
68 resolver := rr.Resolver("gitlab.com")
69 remotes, err := resolver()
70 require.NoError(t, err)
71 require.Len(t, remotes, 1)
73 assert.Equal(t, "origin", remotes[0].Name)
76func Test_remoteResolverConfigHostOverrideWithoutMatch(t *testing.T) {
77 rr := &remoteResolver{
78 readRemotes: func() (git.RemoteSet, error) {
79 return git.RemoteSet{
80 git.NewRemote("origin", "https://gitlab.com/gitlab-org/cli.git"),
81 }, nil
82 },
83 getConfig: func() config.Config {
84 return config.NewFromString(heredoc.Doc(`
85 host: gitlab.tuwien.ac.at
86 `))
87 },
88 parseSSHConfig: noSSHAliases,
89 }
91 resolver := rr.Resolver("gitlab.tuwien.ac.at")
92 remotes, err := resolver()
93 require.NoError(t, err)
94 require.Len(t, remotes, 1)
96 assert.Equal(t, "origin", remotes[0].Name)
97 assert.Equal(t, "gitlab.com", remotes[0].RepoHost())
100func Test_remoteResolverConfigHostOverrideWithMatch(t *testing.T) {
101 rr := &remoteResolver{
102 readRemotes: func() (git.RemoteSet, error) {
103 return git.RemoteSet{
104 git.NewRemote("origin", "https://gitlab.com/owner/repo.git"),
105 git.NewRemote("upstream", "https://example.org/owner/repo.git"),
106 }, nil
107 },
108 getConfig: func() config.Config {
109 return config.NewFromString(heredoc.Doc(`
110 host: example.org
111 hosts:
112 example.org:
113 oauth_token: OTOKEN
114 `))
115 },
116 parseSSHConfig: noSSHAliases,
117 }
119 resolver := rr.Resolver("example.org")
120 remotes, err := resolver()
121 require.NoError(t, err)
122 require.Len(t, remotes, 1)
124 assert.Equal(t, "upstream", remotes[0].Name)
125 assert.Equal(t, "example.org", remotes[0].RepoHost())
128func Test_remoteResolverSSHHostMapping(t *testing.T) {
129 rr := &remoteResolver{
130 readRemotes: func() (git.RemoteSet, error) {
131 return git.RemoteSet{
132 git.NewRemote("origin", "ssh://git@ssh.gitlab.example.com/owner/repo.git"),
133 }, nil
134 },
135 getConfig: func() config.Config {
136 return config.NewFromString(heredoc.Doc(`
137 hosts:
138 gitlab.example.com:
139 oauth_token: OTOKEN
140 ssh_host: ssh.gitlab.example.com
141 `))
142 },
143 parseSSHConfig: noSSHAliases,
144 }
146 resolver := rr.Resolver("")
147 remotes, err := resolver()
148 require.NoError(t, err)
149 require.Len(t, remotes, 1)
151 assert.Equal(t, "origin", remotes[0].Name)
152 // The remote's RepoHost should be rewritten to the API hostname
153 assert.Equal(t, "gitlab.example.com", remotes[0].RepoHost())
154 assert.Equal(t, "owner", remotes[0].RepoOwner())
155 assert.Equal(t, "repo", remotes[0].RepoName())
158func Test_remoteResolverSameHostSSHAlias(t *testing.T) {
159 // A remote whose literal SSH host is already a configured host (a config key or an
160 // entry's ssh_host) must resolve to that account, regardless of what ~/.ssh/config
161 // would rewrite it to; genuine aliases for unknown hosts still translate. See #8394.
162 //
163 // The sshAliases map is what ParseSSHConfig would return for the corresponding
164 // ~/.ssh/config, injected through the parseSSHConfig seam.
165 tests := []struct {
166 name string
167 sshAliases git.SSHAliasMap
168 remoteURL string
169 config string
170 wantHost string
171 }{
172 {
173 name: "second same-host account resolves to its own key (#8394)",
174 sshAliases: git.SSHAliasMap{"gitlab.com-work": "gitlab.com"},
175 remoteURL: "git@gitlab.com-work:owner/repo.git",
176 config: heredoc.Doc(`
177 hosts:
178 gitlab.com:
179 token: PRIMARY
180 gitlab.com-work:
181 api_host: gitlab.com
182 ssh_host: gitlab.com
183 token: WORK
184 `),
185 wantHost: "gitlab.com-work",
186 },
187 {
188 name: "primary account still resolves over the shared host (#8394)",
189 sshAliases: git.SSHAliasMap{"gitlab.com-work": "gitlab.com"},
190 remoteURL: "git@gitlab.com:owner/repo.git",
191 config: heredoc.Doc(`
192 hosts:
193 gitlab.com:
194 token: PRIMARY
195 gitlab.com-work:
196 api_host: gitlab.com
197 ssh_host: gitlab.com
198 token: WORK
199 `),
200 wantHost: "gitlab.com",
201 },
202 {
203 name: "genuine alias for an unknown host still translates",
204 sshAliases: git.SSHAliasMap{"gl": "gitlab.example.com"},
205 remoteURL: "git@gl:owner/repo.git",
206 config: heredoc.Doc(`
207 hosts:
208 gitlab.example.com:
209 token: OTOKEN
210 `),
211 wantHost: "gitlab.example.com",
212 },
213 {
214 name: "configured host aliased away with no ssh_host resolves to that host",
215 sshAliases: git.SSHAliasMap{"gitlab.example.com": "internal.example.com"},
216 remoteURL: "git@gitlab.example.com:owner/repo.git",
217 config: heredoc.Doc(`
218 hosts:
219 gitlab.example.com:
220 token: OTOKEN
221 `),
222 wantHost: "gitlab.example.com",
223 },
224 {
225 name: "gitlab.com aliased to a non-official SSH endpoint resolves to gitlab.com",
226 sshAliases: git.SSHAliasMap{"gitlab.com": "gitlab-proxy.corp.example.com"},
227 remoteURL: "git@gitlab.com:owner/repo.git",
228 config: heredoc.Doc(`
229 hosts:
230 gitlab.com:
231 token: OTOKEN
232 `),
233 wantHost: "gitlab.com",
234 },
235 {
236 name: "ssh_host value is matched before ssh_config translation",
237 sshAliases: git.SSHAliasMap{"git.example.com": "git-internal.example.com"},
238 remoteURL: "git@git.example.com:owner/repo.git",
239 config: heredoc.Doc(`
240 hosts:
241 api.example.com:
242 token: OTOKEN
243 ssh_host: git.example.com
244 `),
245 wantHost: "api.example.com",
246 },
247 {
248 // A mixed-case ssh_host value must still map, exercising NormalizeHostname
249 // on the sshHostMapping populate side. (Config keys, by contrast, are
250 // matched case-sensitively, so they are intentionally not normalized.)
251 name: "mixed-case ssh_host value still maps to its config key",
252 sshAliases: git.SSHAliasMap{},
253 remoteURL: "git@ssh.example.com:owner/repo.git",
254 config: heredoc.Doc(`
255 hosts:
256 api.example.com:
257 token: OTOKEN
258 ssh_host: SSH.Example.com
259 `),
260 wantHost: "api.example.com",
261 },
262 {
263 // gitlab.com is not a configured host here, so the alias is a redirect to
264 // another instance, not an identity alias: follow it, as git does.
265 name: "gitlab.com aliased to a configured host follows the alias",
266 sshAliases: git.SSHAliasMap{"gitlab.com": "gitlab.corp.com"},
267 remoteURL: "git@gitlab.com:owner/repo.git",
268 config: heredoc.Doc(`
269 hosts:
270 gitlab.corp.com:
271 token: OTOKEN
272 `),
273 wantHost: "gitlab.corp.com",
274 },
275 {
276 // Same redirect, reached through the target's ssh_host rather than its key.
277 name: "gitlab.com aliased to a configured host's ssh_host follows the alias",
278 sshAliases: git.SSHAliasMap{"gitlab.com": "ssh.gitlab.corp.com"},
279 remoteURL: "git@gitlab.com:owner/repo.git",
280 config: heredoc.Doc(`
281 hosts:
282 gitlab.corp.com:
283 token: OTOKEN
284 ssh_host: ssh.gitlab.corp.com
285 `),
286 wantHost: "gitlab.corp.com",
287 },
288 }
290 for _, tt := range tests {
291 t.Run(tt.name, func(t *testing.T) {
292 sshURL, err := git.ParseURL(tt.remoteURL)
293 require.NoError(t, err)
295 rr := &remoteResolver{
296 readRemotes: func() (git.RemoteSet, error) {
297 return git.RemoteSet{
298 &git.Remote{Name: "origin", FetchURL: sshURL, PushURL: sshURL},
299 }, nil
300 },
301 getConfig: func() config.Config {
302 return config.NewFromString(tt.config)
303 },
304 parseSSHConfig: func() git.SSHAliasMap { return tt.sshAliases },
305 defaultHostname: "gitlab.com",
306 }
308 resolver := rr.Resolver("")
309 remotes, err := resolver()
310 require.NoError(t, err)
311 require.Len(t, remotes, 1)
313 assert.Equal(t, "origin", remotes[0].Name)
314 assert.Equal(t, tt.wantHost, remotes[0].RepoHost())
315 assert.Equal(t, "owner", remotes[0].RepoOwner())
316 assert.Equal(t, "repo", remotes[0].RepoName())
317 })
318 }
321func Test_remoteResolverSSHHostMappingEdgeCases(t *testing.T) {
322 tests := []struct {
323 name string
324 remotes git.RemoteSet
325 config string
326 hostOverride string
327 expectedCount int
328 expectedHost string
329 expectedName string
330 expectedError string
331 }{
332 {
333 name: "SSH and HTTPS remotes coexist, same project",
334 remotes: git.RemoteSet{
335 git.NewRemote("origin", "ssh://git@ssh.gitlab.example.com/owner/repo.git"),
336 git.NewRemote("https-origin", "https://gitlab.example.com/owner/repo.git"),
337 },
338 config: heredoc.Doc(`
339 hosts:
340 gitlab.example.com:
341 oauth_token: OTOKEN
342 ssh_host: ssh.gitlab.example.com
343 `),
344 expectedCount: 2,
345 expectedHost: "gitlab.example.com",
346 expectedName: "origin",
347 },
348 {
349 name: "SSH host matches but no ssh_host config — remote skipped",
350 remotes: git.RemoteSet{
351 git.NewRemote("origin", "ssh://git@ssh.gitlab.example.com/owner/repo.git"),
352 },
353 config: heredoc.Doc(`
354 hosts:
355 gitlab.example.com:
356 oauth_token: OTOKEN
357 `),
358 expectedError: "none of the git remotes configured for this repository point to a known GitLab host",
359 },
360 {
361 name: "Standard setup without ssh_host — no change in behavior",
362 remotes: git.RemoteSet{
363 git.NewRemote("origin", "https://gitlab.com/owner/repo.git"),
364 },
365 config: heredoc.Doc(`
366 hosts:
367 gitlab.com:
368 oauth_token: OTOKEN
369 `),
370 expectedCount: 1,
371 expectedHost: "gitlab.com",
372 expectedName: "origin",
373 },
374 {
375 name: "Self-managed with matching SSH and API hostname — no rewrite needed",
376 remotes: git.RemoteSet{
377 git.NewRemote("origin", "ssh://git@gitlab.corp.com/owner/repo.git"),
378 },
379 config: heredoc.Doc(`
380 hosts:
381 gitlab.corp.com:
382 oauth_token: OTOKEN
383 `),
384 expectedCount: 1,
385 expectedHost: "gitlab.corp.com",
386 expectedName: "origin",
387 },
388 {
389 name: "SSH host mapping with host override matching API host",
390 remotes: git.RemoteSet{
391 git.NewRemote("origin", "ssh://git@ssh.gitlab.example.com/owner/repo.git"),
392 },
393 config: heredoc.Doc(`
394 hosts:
395 gitlab.example.com:
396 oauth_token: OTOKEN
397 ssh_host: ssh.gitlab.example.com
398 `),
399 hostOverride: "gitlab.example.com",
400 expectedCount: 1,
401 expectedHost: "gitlab.example.com",
402 expectedName: "origin",
403 },
404 {
405 name: "SSH host mapping with host override matching SSH host — matches raw remote",
406 remotes: git.RemoteSet{
407 git.NewRemote("origin", "ssh://git@ssh.gitlab.example.com/owner/repo.git"),
408 },
409 config: heredoc.Doc(`
410 hosts:
411 gitlab.example.com:
412 oauth_token: OTOKEN
413 ssh_host: ssh.gitlab.example.com
414 `),
415 hostOverride: "ssh.gitlab.example.com",
416 expectedCount: 1,
417 expectedHost: "ssh.gitlab.example.com",
418 expectedName: "origin",
419 },
420 {
421 name: "Multiple hosts, SSH mapping only for one",
422 remotes: git.RemoteSet{
423 git.NewRemote("corp", "ssh://git@ssh.corp.example.com/team/project.git"),
424 git.NewRemote("community", "https://gitlab.com/team/project.git"),
425 },
426 config: heredoc.Doc(`
427 hosts:
428 corp.example.com:
429 oauth_token: CORPTOKEN
430 ssh_host: ssh.corp.example.com
431 gitlab.com:
432 oauth_token: COMTOKEN
433 `),
434 // First match wins (sorted by remote name priority)
435 expectedCount: 1,
436 expectedHost: "corp.example.com",
437 expectedName: "corp",
438 },
439 }
441 for _, tt := range tests {
442 t.Run(tt.name, func(t *testing.T) {
443 rr := &remoteResolver{
444 readRemotes: func() (git.RemoteSet, error) {
445 return tt.remotes, nil
446 },
447 getConfig: func() config.Config {
448 return config.NewFromString(tt.config)
449 },
450 parseSSHConfig: noSSHAliases,
451 }
453 resolver := rr.Resolver(tt.hostOverride)
454 remotes, err := resolver()
456 if tt.expectedError != "" {
457 require.Error(t, err)
458 assert.Contains(t, err.Error(), tt.expectedError)
459 return
460 }
462 require.NoError(t, err)
463 require.Len(t, remotes, tt.expectedCount)
464 assert.Equal(t, tt.expectedName, remotes[0].Name)
465 assert.Equal(t, tt.expectedHost, remotes[0].RepoHost())
466 })
467 }
470func Test_remoteResolverErrors(t *testing.T) {
471 testRemotes := git.RemoteSet{
472 git.NewRemote("origin", "https://example3.org/owner/fork.git"),
473 git.NewRemote("fork", "https://example.org/owner/fork.git"),
474 git.NewRemote("upstream", "https://example.org/owner/repo.git"),
475 git.NewRemote("foo", "https://example2.org/owner/repo.git"),
476 }
478 tests := []struct {
479 name string
480 remotes git.RemoteSet
481 hostOverride string
482 hostOverrideEnvVar string
483 config string
484 expectedError string
485 expectedErrorIs error
486 }{
487 {
488 name: "No remotes",
489 remotes: git.RemoteSet{},
490 expectedError: "no git remotes found",
491 expectedErrorIs: ErrNoGitRemotes,
492 },
493 {
494 name: "No match with host override",
495 remotes: testRemotes,
496 hostOverride: "nomatch.org",
497 hostOverrideEnvVar: "GITLAB_HOST",
498 expectedError: "none of the git remotes configured for this repository correspond to the GITLAB_HOST environment variable. " +
499 "Try adding a matching remote or unsetting the variable.\n\n" +
500 "GITLAB_HOST is currently set to nomatch.org\n\n" +
501 "Configured remotes: example.org, example3.org, example2.org",
502 },
503 {
504 name: "No match with GL_HOST override",
505 remotes: testRemotes,
506 hostOverride: "nomatch.org",
507 hostOverrideEnvVar: "GL_HOST",
508 expectedError: "none of the git remotes configured for this repository correspond to the GL_HOST environment variable. " +
509 "Try adding a matching remote or unsetting the variable.\n\n" +
510 "GL_HOST is currently set to nomatch.org\n\n" +
511 "Configured remotes: example.org, example3.org, example2.org",
512 },
513 {
514 name: "No match with config host override",
515 remotes: git.RemoteSet{git.NewRemote("origin", "https://other.example.org/owner/repo.git")},
516 hostOverride: "gitlab.example.com",
517 expectedError: "none of the git remotes configured for this repository point to a known GitLab host. " +
518 "Please use `glab auth login` to authenticate and configure a new host for glab.\n\n" +
519 "Configured remotes: other.example.org",
520 },
521 {
522 name: "No match",
523 remotes: testRemotes,
524 expectedError: "none of the git remotes configured for this repository point to a known GitLab host. " +
525 "Please use `glab auth login` to authenticate and configure a new host for glab.\n\n" +
526 "Configured remotes: example.org, example3.org, example2.org",
527 },
528 {
529 name: "Remote on a subdomain of a configured host suggests ssh_host",
530 remotes: git.RemoteSet{
531 git.NewRemote("origin", "ssh://git@ssh.my-gitlab.org/owner/repo.git"),
532 git.NewRemote("fork", "ssh://git@ssh.my-gitlab.org/me/repo.git"),
533 },
534 expectedError: "none of the git remotes configured for this repository point to a known GitLab host.\n\n" +
535 "If ssh.my-gitlab.org is the SSH host of my-gitlab.org, run:\n\n" +
536 " glab config set ssh_host ssh.my-gitlab.org --host my-gitlab.org\n\n" +
537 "Configured remotes: ssh.my-gitlab.org",
538 },
539 {
540 name: "SSH remote whose host only ends with a configured host gets no ssh_host hint",
541 remotes: git.RemoteSet{git.NewRemote("origin", "ssh://git@notmy-gitlab.org/owner/repo.git")},
542 expectedError: "none of the git remotes configured for this repository point to a known GitLab host. " +
543 "Please use `glab auth login` to authenticate and configure a new host for glab.\n\n" +
544 "Configured remotes: notmy-gitlab.org",
545 },
546 {
547 name: "HTTPS remote on a subdomain of a configured host gets no ssh_host hint",
548 remotes: git.RemoteSet{git.NewRemote("origin", "https://dev.my-gitlab.org/owner/repo.git")},
549 expectedError: "none of the git remotes configured for this repository point to a known GitLab host. " +
550 "Please use `glab auth login` to authenticate and configure a new host for glab.\n\n" +
551 "Configured remotes: dev.my-gitlab.org",
552 },
553 {
554 name: "ssh_host hint names the most specific configured host",
555 remotes: git.RemoteSet{git.NewRemote("origin", "ssh://git@ssh.gitlab.my-gitlab.org/owner/repo.git")},
556 config: heredoc.Doc(`
557 hosts:
558 my-gitlab.org:
559 oauth_token: OTOKEN
560 gitlab.my-gitlab.org:
561 oauth_token: OTOKEN
562 `),
563 expectedError: "none of the git remotes configured for this repository point to a known GitLab host.\n\n" +
564 "If ssh.gitlab.my-gitlab.org is the SSH host of gitlab.my-gitlab.org, run:\n\n" +
565 " glab config set ssh_host ssh.gitlab.my-gitlab.org --host gitlab.my-gitlab.org\n\n" +
566 "Configured remotes: ssh.gitlab.my-gitlab.org",
567 },
568 }
569 for _, test := range tests {
570 t.Run(test.name, func(t *testing.T) {
571 rr := &remoteResolver{
572 readRemotes: func() (git.RemoteSet, error) {
573 return test.remotes, nil
574 },
575 getConfig: func() config.Config {
576 return config.NewFromString(cmp.Or(test.config, heredoc.Doc(`
577 hosts:
578 my-gitlab.org:
579 oauth_token: OTOKEN
580 `)))
581 },
582 parseSSHConfig: noSSHAliases,
583 hostOverrideEnvVar: test.hostOverrideEnvVar,
584 }
586 resolver := rr.Resolver(test.hostOverride)
587 _, err := resolver()
588 require.Error(t, err)
589 assert.Equal(t, test.expectedError, err.Error())
590 if test.expectedErrorIs != nil {
591 require.ErrorIs(t, err, test.expectedErrorIs)
592 }
593 })
594 }
597func Test_remoteResolverSplitHostWithSubfolder(t *testing.T) {
598 // This test reproduces Issue #8197: split-host + subfolder bug
599 // where SSH host mapping happens but r.Repo is never updated with the mapped hostname
600 t.Run("SSH remote with split-host config and subfolder", func(t *testing.T) {
601 // Parse SSH URL using git.ParseURL to properly normalize SCP-style format
602 sshURL, err := git.ParseURL("git@git.example.com:owner/repo.git")
603 require.NoError(t, err)
605 rr := &remoteResolver{
606 readRemotes: func() (git.RemoteSet, error) {
607 // SSH remote URL using git.example.com
608 return git.RemoteSet{
609 &git.Remote{
610 Name: "origin",
611 FetchURL: sshURL,
612 PushURL: sshURL,
613 },
614 }, nil
615 },
616 getConfig: func() config.Config {
617 // Config keyed under api.example.com with ssh_host pointing to git.example.com
618 return config.NewFromString(heredoc.Doc(`
619 hosts:
620 api.example.com:
621 token: TEST_TOKEN
622 ssh_host: git.example.com
623 subfolder: gitlab
624 `))
625 },
626 parseSSHConfig: noSSHAliases,
627 defaultHostname: "gitlab.com",
628 }
630 resolver := rr.Resolver("")
631 remotes, err := resolver()
632 require.NoError(t, err)
633 require.Len(t, remotes, 1)
635 // The remote should have RepoHost() = "api.example.com" (the config key),
636 // not "git.example.com" (the SSH hostname), so downstream config lookups
637 // (subfolder, token, etc.) resolve correctly.
638 assert.Equal(t, "api.example.com", remotes[0].RepoHost(),
639 "Remote.Repo should be updated to use the config key hostname, not the SSH hostname")
641 // Verify the owner/repo are correct
642 assert.Equal(t, "owner", remotes[0].RepoOwner())
643 assert.Equal(t, "repo", remotes[0].RepoName())
644 })
646 t.Run("SSH + HTTPS remotes with split-host: filtering bug in MR #2924", func(t *testing.T) {
647 // This test exposes the filtering bug in MR #2924:
648 // When both SSH (split-host) and HTTPS remotes exist,
649 // MR #2924 incorrectly filters out the HTTPS remote
651 sshURL, err := git.ParseURL("git@git.example.com:owner/repo.git")
652 require.NoError(t, err)
654 httpsURL, err := git.ParseURL("https://api.example.com/owner/upstream.git")
655 require.NoError(t, err)
657 rr := &remoteResolver{
658 readRemotes: func() (git.RemoteSet, error) {
659 return git.RemoteSet{
660 &git.Remote{Name: "origin", FetchURL: sshURL, PushURL: sshURL},
661 &git.Remote{Name: "upstream", FetchURL: httpsURL, PushURL: httpsURL},
662 }, nil
663 },
664 getConfig: func() config.Config {
665 return config.NewFromString(heredoc.Doc(`
666 hosts:
667 api.example.com:
668 token: TEST_TOKEN
669 ssh_host: git.example.com
670 subfolder: gitlab
671 `))
672 },
673 parseSSHConfig: noSSHAliases,
674 defaultHostname: "gitlab.com",
675 }
677 resolver := rr.Resolver("")
678 remotes, err := resolver()
679 require.NoError(t, err)
681 // Both remotes should be returned (MR #2924 bug returned only 1).
682 assert.Len(t, remotes, 2, "Both SSH and HTTPS remotes should be included")
684 // Verify both remotes use the correct API hostname
685 assert.Equal(t, "api.example.com", remotes[0].RepoHost())
686 assert.Equal(t, "api.example.com", remotes[1].RepoHost())
688 // Verify both remote names are present (order may vary due to sorting)
689 names := []string{remotes[0].Name, remotes[1].Name}
690 assert.Contains(t, names, "origin")
691 assert.Contains(t, names, "upstream")
692 })
695// remoteResolver is the single path from raw git remotes to resolved repos that
696// every command inherits through the factory, so exercising the port/subfolder
697// matrix here covers all of them at once. Command-level tests cannot: they stub
698// Factory.Remotes with repos that are already resolved, which is why a
699// resolution bug could reach a release with the command suites green.
700//
701// Each case asserts the resolved project path and that RepoHost still addresses
702// the configured host entry. RepoHost is what the API client keys its own
703// lookups on, so a remote that resolves to a host with no entry sends
704// unauthenticated requests over the default protocol.
705func Test_remoteResolver_PortAndSubfolderMatrix(t *testing.T) {
706 cases := []struct {
707 name string
708 cfg string
709 remoteURL string
710 wantOwner string
711 wantName string
712 wantHost string
713 }{
714 {
715 name: "custom port with subfolder",
716 cfg: heredoc.Doc(`
717 hosts:
718 example.com:8080:
719 token: TEST_TOKEN
720 api_protocol: http
721 subfolder: gitlab
722 `),
723 remoteURL: "http://example.com:8080/gitlab/owner/repo.git",
724 wantOwner: "owner",
725 wantName: "repo",
726 wantHost: "example.com:8080",
727 },
728 {
729 name: "custom port without subfolder",
730 cfg: heredoc.Doc(`
731 hosts:
732 example.com:8080:
733 token: TEST_TOKEN
734 api_protocol: http
735 `),
736 remoteURL: "http://example.com:8080/owner/repo.git",
737 wantOwner: "owner",
738 wantName: "repo",
739 wantHost: "example.com:8080",
740 },
741 {
742 name: "subfolder without custom port",
743 cfg: heredoc.Doc(`
744 hosts:
745 example.com:
746 token: TEST_TOKEN
747 subfolder: gitlab
748 `),
749 remoteURL: "https://example.com/gitlab/owner/repo.git",
750 wantOwner: "owner",
751 wantName: "repo",
752 wantHost: "example.com",
753 },
754 {
755 name: "custom port with nested subfolder",
756 cfg: heredoc.Doc(`
757 hosts:
758 example.com:8080:
759 token: TEST_TOKEN
760 api_protocol: http
761 subfolder: apps/gitlab
762 `),
763 remoteURL: "http://example.com:8080/apps/gitlab/owner/repo.git",
764 wantOwner: "owner",
765 wantName: "repo",
766 wantHost: "example.com:8080",
767 },
768 {
769 name: "custom port with subfolder from legacy api_host",
770 cfg: heredoc.Doc(`
771 hosts:
772 example.com:8080:
773 token: TEST_TOKEN
774 api_protocol: http
775 api_host: example.com:8080/gitlab
776 `),
777 remoteURL: "http://example.com:8080/gitlab/owner/repo.git",
778 wantOwner: "owner",
779 wantName: "repo",
780 wantHost: "example.com:8080",
781 },
782 {
783 name: "custom port with subfolder and a subgroup",
784 cfg: heredoc.Doc(`
785 hosts:
786 example.com:8080:
787 token: TEST_TOKEN
788 api_protocol: http
789 subfolder: gitlab
790 `),
791 remoteURL: "http://example.com:8080/gitlab/group/subgroup/repo.git",
792 wantOwner: "group/subgroup",
793 wantName: "repo",
794 wantHost: "example.com:8080",
795 },
796 }
798 for _, tc := range cases {
799 t.Run(tc.name, func(t *testing.T) {
800 remoteURL, err := git.ParseURL(tc.remoteURL)
801 require.NoError(t, err)
803 cfg := config.NewFromString(tc.cfg)
804 rr := &remoteResolver{
805 readRemotes: func() (git.RemoteSet, error) {
806 return git.RemoteSet{
807 &git.Remote{Name: "origin", FetchURL: remoteURL, PushURL: remoteURL},
808 }, nil
809 },
810 getConfig: func() config.Config { return cfg },
811 parseSSHConfig: noSSHAliases,
812 defaultHostname: "gitlab.com",
813 }
815 resolver := rr.Resolver("")
816 remotes, err := resolver()
817 require.NoError(t, err)
818 require.Len(t, remotes, 1)
820 assert.Equal(t, tc.wantOwner, remotes[0].RepoOwner())
821 assert.Equal(t, tc.wantName, remotes[0].RepoName())
822 assert.Equal(t, tc.wantHost, remotes[0].RepoHost())
824 token, _ := cfg.Get(remotes[0].RepoHost(), "token")
825 assert.Equal(t, "TEST_TOKEN", token,
826 "RepoHost must address the configured host entry, or the API client authenticates as nobody")
827 })
828 }
831// An SSH remote on a subfolder instance resolves to the ported host entry that
832// names it through ssh_host, so downstream config lookups address the same
833// entry an HTTPS remote would.
834//
835// The subfolder is deliberately absent from the SSH path. A relative URL root
836// is an HTTP concept: GitLab reports ssh_url_to_repo as git@host:group/repo.git
837// whatever prefix the web UI is served under, which is why the `subfolder` key
838// is documented as applying to HTTP/HTTPS only. It stays in the fixture because
839// a subfolder instance really does hand out SSH remotes like this, and the
840// assertion pins that it is not mistakenly stripped from — or prepended to — a
841// path that never carried it.
842func Test_remoteResolver_SSHRemoteOnPortedSubfolderHost(t *testing.T) {
843 t.Run("ssh_host mapping to a ported host entry", func(t *testing.T) {
844 sshURL, err := git.ParseURL("git@git.example.com:owner/repo.git")
845 require.NoError(t, err)
847 cfg := config.NewFromString(heredoc.Doc(`
848 hosts:
849 example.com:8080:
850 token: TEST_TOKEN
851 api_protocol: http
852 ssh_host: git.example.com
853 subfolder: gitlab
854 `))
856 rr := &remoteResolver{
857 readRemotes: func() (git.RemoteSet, error) {
858 return git.RemoteSet{
859 &git.Remote{Name: "origin", FetchURL: sshURL, PushURL: sshURL},
860 }, nil
861 },
862 getConfig: func() config.Config { return cfg },
863 parseSSHConfig: noSSHAliases,
864 defaultHostname: "gitlab.com",
865 }
867 resolver := rr.Resolver("")
868 remotes, err := resolver()
869 require.NoError(t, err)
870 require.Len(t, remotes, 1)
872 assert.Equal(t, "owner/repo", remotes[0].FullName(),
873 "an SSH path carries no subfolder segment, so none is stripped and none is added")
874 assert.Equal(t, "example.com:8080", remotes[0].RepoHost(),
875 "the SSH remote should resolve to the config key, port included")
877 token, _ := cfg.Get(remotes[0].RepoHost(), "token")
878 assert.Equal(t, "TEST_TOKEN", token)
879 })
Diagnostics6 git calls · 27 ms
CallStatusSizeTime027 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=3
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
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=3
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=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:02001.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=17
git.response.bytes=1238
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17
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=1238
git.pkt.payload.max=1211
git.sideband.band1.bytes=1211
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1211
git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27ca
Open pack map
fetch2005.0 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=5168
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=8844bf469d98fbf87cf20bc3b63037413c547bd7
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=5168
git.pkt.payload.max=5141
git.sideband.band1.bytes=5141
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
a5e259e95e97a23e
Request
cbe5959a…
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