GitLabgitlab-org
cli
main
1//go:build !integration
3package glrepo
5import (
6 "errors"
7 "net/url"
8 "reflect"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
14 "gitlab.com/gitlab-org/cli/internal/config"
15 "gitlab.com/gitlab-org/cli/internal/git"
16 "gitlab.com/gitlab-org/cli/internal/glinstance"
17)
19func eq(t *testing.T, got any, expected any) {
20 t.Helper()
21 if !reflect.DeepEqual(got, expected) {
22 t.Errorf("expected: %v, got: %v", expected, got)
23 }
24}
26func TestFindByName(t *testing.T) {
27 list := Remotes{
28 &Remote{Remote: &git.Remote{Name: "mona"}, Repo: New("monalisa", "myfork", glinstance.DefaultHostname)},
29 &Remote{Remote: &git.Remote{Name: "origin"}, Repo: New("monalisa", "octo-cat", glinstance.DefaultHostname)},
30 &Remote{Remote: &git.Remote{Name: "upstream"}, Repo: New("hubot", "tools", glinstance.DefaultHostname)},
31 }
33 r, err := list.FindByName("upstream", "origin")
34 eq(t, err, nil)
35 eq(t, r.Name, "upstream")
37 r, err = list.FindByName("nonexist", "*")
38 eq(t, err, nil)
39 eq(t, r.Name, "mona")
41 _, err = list.FindByName("nonexist")
42 eq(t, err, errors.New(`no GitLab remotes found`))
43}
45func TestTranslateRemotes(t *testing.T) {
46 publicURL, _ := url.Parse("https://gitlab.com/monalisa/hello")
47 originURL, _ := url.Parse("http://example.com/repo")
48 upstreamURL, _ := url.Parse("https://gitlab.com/profclems/glab")
50 gitRemotes := git.RemoteSet{
51 &git.Remote{
52 Name: "origin",
53 FetchURL: originURL,
54 },
55 &git.Remote{
56 Name: "public",
57 FetchURL: publicURL,
58 },
59 &git.Remote{
60 Name: "upstream",
61 PushURL: upstreamURL,
62 },
63 }
65 identityURL := func(u *url.URL) *url.URL {
66 return u
67 }
68 result := TranslateRemotes(gitRemotes, identityURL, glinstance.DefaultHostname, config.NewBlankConfig())
70 if len(result) != 2 {
71 t.Errorf("got %d results", len(result))
72 }
73 if result[0].Name != "public" {
74 t.Errorf("got %q", result[0].Name)
75 }
76 if result[0].RepoName() != "hello" {
77 t.Errorf("got %q", result[0].RepoName())
78 }
79 if result[1].Name != "upstream" {
80 t.Errorf("got %q", result[1].Name)
81 }
82 if result[1].RepoName() != "glab" {
83 t.Errorf("got %q", result[1].Name)
84 }
85}
87func Test_remoteNameSortingScore(t *testing.T) {
88 testCases := []struct {
89 name string
90 input string
91 output int
92 }{
93 {
94 name: "upstream",
95 input: "upstream",
96 output: 3,
97 },
98 {
99 name: "gitlab",
100 input: "gitlab",
101 output: 2,
102 },
103 {
104 name: "origin",
105 input: "origin",
106 output: 1,
107 },
108 {
109 name: "else",
110 input: "anyOtherName",
111 output: 0,
112 },
113 }
114 for _, tC := range testCases {
115 t.Run(tC.name, func(t *testing.T) {
116 got := remoteNameSortScore(tC.input)
117 assert.Equal(t, tC.output, got)
118 })
119 }
120}
122func Test_FindByRepo(t *testing.T) {
123 r := Remotes{
124 &Remote{
125 Remote: &git.Remote{
126 Name: "origin",
127 },
128 Repo: NewWithHost("profclems", "glab", "gitlab.com"),
129 },
130 }
132 t.Run("success", func(t *testing.T) {
133 got, err := r.FindByRepo("profclems", "glab")
134 require.NoError(t, err)
136 assert.Equal(t, r[0].FullName(), got.FullName())
137 })
139 t.Run("fail/owner", func(t *testing.T) {
140 got, err := r.FindByRepo("maxice8", "glab")
141 assert.Nil(t, got)
142 assert.EqualError(t, err, "no matching remote found")
143 })
145 t.Run("fail/project", func(t *testing.T) {
146 got, err := r.FindByRepo("profclems", "balg")
147 assert.Nil(t, got)
148 assert.EqualError(t, err, "no matching remote found")
149 })
151 t.Run("fail/owner and project", func(t *testing.T) {
152 got, err := r.FindByRepo("maxice8", "balg")
153 assert.Nil(t, got)
154 assert.EqualError(t, err, "no matching remote found")
155 })
156}
158func Test_RepoFuncs(t *testing.T) {
159 testCases := []struct {
160 name string
161 input []string
162 wantHostname string
163 wantOwner string
164 wantGroup string
165 wantNamespace string
166 wantName string
167 wantFullname string
168 }{
169 {
170 name: "Simple",
171 input: []string{"profclems", "glab", "gitlab.com"},
172 wantHostname: "gitlab.com",
173 wantNamespace: "profclems",
174 wantOwner: "profclems",
175 wantName: "glab",
176 wantFullname: "profclems/glab",
177 },
178 {
179 name: "group",
180 input: []string{"company/profclems", "glab", "gitlab.com"},
181 wantHostname: "gitlab.com",
182 wantNamespace: "profclems",
183 wantOwner: "company/profclems",
184 wantGroup: "company",
185 wantName: "glab",
186 wantFullname: "company/profclems/glab",
187 },
188 }
189 for _, tC := range testCases {
190 t.Run(tC.name, func(t *testing.T) {
191 got := Remote{
192 Repo: NewWithHost(tC.input[0], tC.input[1], tC.input[2]),
193 }
194 if tC.wantHostname != "" {
195 assert.Equal(t, tC.wantHostname, got.RepoHost())
196 }
197 if tC.wantOwner != "" {
198 assert.Equal(t, tC.wantOwner, got.RepoOwner())
199 }
200 if tC.wantGroup != "" {
201 assert.Equal(t, tC.wantGroup, got.RepoGroup())
202 }
203 if tC.wantNamespace != "" {
204 assert.Equal(t, tC.wantNamespace, got.RepoNamespace())
205 }
206 if tC.wantName != "" {
207 assert.Equal(t, tC.wantName, got.RepoName())
208 }
209 if tC.wantFullname != "" {
210 assert.Equal(t, tC.wantFullname, got.FullName())
211 }
212 })
213 }
214}
216func Test_Swap(t *testing.T) {
217 r := Remotes{
218 &Remote{
219 Remote: &git.Remote{
220 Name: "origin",
221 },
222 Repo: NewWithHost("maxice8", "glab", "gitlab.com"),
223 },
224 &Remote{
225 Remote: &git.Remote{
226 Name: "upstream",
227 },
228 Repo: NewWithHost("profclems", "glab", "gitlab.com"),
229 },
230 }
232 assert.Equal(t, "origin", r[0].Remote.Name)
233 assert.Equal(t, "upstream", r[1].Remote.Name)
235 assert.Equal(t, "maxice8/glab", r[0].Repo.FullName())
236 assert.Equal(t, "profclems/glab", r[1].Repo.FullName())
238 r.Swap(0, 1)
240 assert.Equal(t, "upstream", r[0].Remote.Name)
241 assert.Equal(t, "origin", r[1].Remote.Name)
243 assert.Equal(t, "profclems/glab", r[0].Repo.FullName())
244 assert.Equal(t, "maxice8/glab", r[1].Repo.FullName())
245}
247func Test_Less(t *testing.T) {
248 r := Remotes{
249 &Remote{
250 Remote: &git.Remote{
251 Name: "else",
252 },
253 Repo: NewWithHost("somebody", "glab", "gitlab.com"),
254 },
255 &Remote{
256 Remote: &git.Remote{
257 Name: "origin",
258 },
259 Repo: NewWithHost("maxice8", "glab", "gitlab.com"),
260 },
261 &Remote{
262 Remote: &git.Remote{
263 Name: "gitlab",
264 },
265 Repo: NewWithHost("profclems", "glab", "gitlab.com"),
266 },
267 &Remote{
268 Remote: &git.Remote{
269 Name: "upstream",
270 },
271 Repo: NewWithHost("profclems", "glab", "gitlab.com"),
272 },
273 }
275 assert.True(t, r.Less(3, 2))
276 assert.True(t, r.Less(3, 1))
277 assert.True(t, r.Less(3, 0))
278 assert.True(t, r.Less(2, 1))
279 assert.True(t, r.Less(2, 0))
280 assert.True(t, r.Less(1, 0))
282 assert.False(t, r.Less(0, 1))
283 assert.False(t, r.Less(0, 2))
284 assert.False(t, r.Less(0, 3))
285 assert.False(t, r.Less(1, 2))
286 assert.False(t, r.Less(1, 3))
287 assert.False(t, r.Less(2, 3))
288}
290func TestTranslateRemotes_SplitHostSubfolder(t *testing.T) {
291 // This test verifies the split-host + subfolder bug (Issue #8197)
292 // where FromURL() fails to strip the subfolder because the URL hostname
293 // doesn't match the config key hostname in split-host setups
295 t.Run("split-host with subfolder - correct config pattern", func(t *testing.T) {
296 // Setup: Config keyed under API hostname with ssh_host and subfolder
297 cfg := config.NewFromString(`---
298hosts:
299 api.example.com:
300 token: TEST_TOKEN
301 ssh_host: git.example.com
302 subfolder: gitlab
303`)
305 // Git remote URL includes the subfolder in the path
306 remoteURL, _ := url.Parse("https://api.example.com/gitlab/owner/repo.git")
308 gitRemotes := git.RemoteSet{
309 &git.Remote{
310 Name: "origin",
311 FetchURL: remoteURL,
312 },
313 }
315 identityURL := func(u *url.URL) *url.URL {
316 return u
317 }
319 result := TranslateRemotes(gitRemotes, identityURL, "gitlab.com", cfg)
321 // Verify results
322 assert.Len(t, result, 1, "should translate one remote")
323 assert.Equal(t, "origin", result[0].Name, "remote name should be origin")
325 // The key assertion: FullName should be "owner/repo" (subfolder stripped)
326 // NOT "gitlab/owner/repo" (with subfolder included)
327 assert.Equal(t, "owner/repo", result[0].FullName(), "subfolder should be stripped from project path")
328 assert.Equal(t, "api.example.com", result[0].RepoHost(), "should use URL hostname as RepoHost")
329 })
330}
332// TranslateRemotes is the path `mr create` takes to resolve the head repo from
333// git remotes, which is why the ported-host lookup bug surfaces there while
334// `-R`-driven commands are unaffected.
335func TestTranslateRemotes_SubfolderOnPortedHost(t *testing.T) {
336 t.Run("host entry keyed with port", func(t *testing.T) {
337 cfg := config.NewFromString(`---
338hosts:
339 example.com:8080:
340 token: TEST_TOKEN
341 api_protocol: http
342 subfolder: gitlab
343`)
345 remoteURL, _ := url.Parse("http://example.com:8080/gitlab/owner/repo.git")
347 gitRemotes := git.RemoteSet{
348 &git.Remote{
349 Name: "origin",
350 FetchURL: remoteURL,
351 },
352 }
354 identityURL := func(u *url.URL) *url.URL { return u }
356 result := TranslateRemotes(gitRemotes, identityURL, "gitlab.com", cfg)
358 assert.Len(t, result, 1, "should translate one remote")
359 assert.Equal(t, "owner/repo", result[0].FullName(), "subfolder should be stripped from project path")
360 assert.Equal(t, "example.com:8080", result[0].RepoHost(), "RepoHost should retain the port")
362 // RepoHost is the key the API client resolves its own settings under, so
363 // it has to address the same host entry FromURL just read the subfolder
364 // from. When the two disagree, requests are built with the wrong project
365 // path, the default protocol, and no token.
366 token, _ := cfg.Get(result[0].RepoHost(), "token")
367 protocol, _ := cfg.Get(result[0].RepoHost(), "api_protocol")
368 assert.Equal(t, "TEST_TOKEN", token, "RepoHost must address the configured host entry")
369 assert.Equal(t, "http", protocol, "RepoHost must address the configured host entry")
370 })
371}
373func TestRemotes_UniqueHosts(t *testing.T) {
374 tests := []struct {
375 name string
376 r Remotes
377 want string
378 }{
379 {
380 name: "multiple remotes with duplicates",
381 r: Remotes{
382 &Remote{
383 Remote: &git.Remote{
384 Name: "origin",
385 },
386 Repo: NewWithHost("owner", "repo", "gitlab.com"),
387 },
388 &Remote{
389 Remote: &git.Remote{
390 Name: "fork",
391 },
392 Repo: NewWithHost("forkowner", "repo", "gitlab.com"),
393 },
394 &Remote{
395 Remote: &git.Remote{
396 Name: "upstream",
397 },
398 Repo: NewWithHost("owner", "repo", "mygitlab-xyz.com"),
399 },
400 },
401 want: "gitlab.com, mygitlab-xyz.com",
402 },
403 {
404 name: "single remote",
405 r: Remotes{
406 &Remote{
407 Remote: &git.Remote{
408 Name: "origin",
409 },
410 Repo: NewWithHost("owner", "repo", "gitlab.com"),
411 },
412 },
413 want: "gitlab.com",
414 },
415 {
416 name: "no remotes",
417 r: Remotes{},
418 want: "",
419 },
420 }
421 for _, tt := range tests {
422 t.Run(tt.name, func(t *testing.T) {
423 assert.Equal(t, tt.want, tt.r.UniqueHosts())
424 })
425 }
426}
Diagnostics6 git calls · 32 ms
CallStatusSizeTime032 ms
discover200185 B4 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 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 · hit2 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=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=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=2 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:0200314 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=22 git.response.bytes=314 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=93a3fb44940eceae81f0a58fd4df1d50fbd7ccb5 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=314 git.pkt.payload.max=287 git.sideband.band1.bytes=287 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=287 git.pack.trailer=0a0d545706824b859dbb83ba13169780ef407392Open pack map
fetch2002.6 KiB · hit22 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2676 git.response.cache=HIT git.response.duration_ms=22 git.fetch.wants=09b8b048b7e47e1f3c5a8f2982c86b20ecedcf14 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=2676 git.pkt.payload.max=2649 git.sideband.band1.bytes=2649 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
- a5e254fcab321ae0
- Request
- 79cfcf76…
- 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