GitLabgitlab-org
cli
main
1//go:build !integration
3package clone
5import (
6 "errors"
7 "slices"
8 "strings"
9 "testing"
11 "github.com/google/shlex"
12 "github.com/spf13/cobra"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "go.uber.org/mock/gomock"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
20 "gitlab.com/gitlab-org/cli/internal/api"
21 "gitlab.com/gitlab-org/cli/internal/cmdutils"
22 "gitlab.com/gitlab-org/cli/internal/config"
23 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
24 "gitlab.com/gitlab-org/cli/test"
25)
27func TestMain(m *testing.M) {
28 cmdtest.InitTest(m, "repo_clone_test")
29}
31func TestNewCmdClone(t *testing.T) {
32 t.Parallel()
34 testCases := []struct {
35 name string
36 args string
37 wantOpts options
38 wantCtxOpts ContextOpts
39 wantErr string
40 }{
41 {
42 name: "no arguments",
43 args: "",
44 wantErr: "specify repository argument, or use the --group flag to specify a group to clone all repos from the group",
45 },
46 {
47 name: "repo argument",
48 args: "NAMESPACE/REPO",
49 wantOpts: options{
50 gitFlags: []string{},
51 },
52 wantCtxOpts: ContextOpts{
53 Repo: "NAMESPACE/REPO",
54 },
55 },
56 {
57 name: "directory argument",
58 args: "NAMESPACE/REPO mydir",
59 wantOpts: options{
60 gitFlags: []string{},
61 dir: "mydir",
62 },
63 wantCtxOpts: ContextOpts{
64 Repo: "NAMESPACE/REPO",
65 },
66 },
67 {
68 name: "wiki repository",
69 args: "--wiki NAMESPACE/REPO",
70 wantOpts: options{
71 gitFlags: []string{},
72 wiki: true,
73 },
74 wantCtxOpts: ContextOpts{
75 Repo: "NAMESPACE/REPO",
76 },
77 },
78 {
79 name: "git clone arguments",
80 args: "NAMESPACE/REPO -- --depth 1 --recurse-submodules",
81 wantOpts: options{
82 gitFlags: []string{"--depth", "1", "--recurse-submodules"},
83 },
84 wantCtxOpts: ContextOpts{
85 Repo: "NAMESPACE/REPO",
86 },
87 },
88 {
89 name: "group clone arguments",
90 args: "-g NAMESPACE/REPO -- --depth 1 --recurse-submodules",
91 wantOpts: options{
92 gitFlags: []string{"--depth", "1", "--recurse-submodules"},
93 groupName: "NAMESPACE/REPO",
94 },
95 wantCtxOpts: ContextOpts{
96 Repo: "",
97 },
98 },
99 {
100 name: "group clone with directory argument",
101 args: "-g NAMESPACE/REPO mydir",
102 wantOpts: options{
103 gitFlags: []string{},
104 groupName: "NAMESPACE/REPO",
105 dir: "mydir",
106 },
107 wantCtxOpts: ContextOpts{
108 Repo: "",
109 },
110 },
111 {
112 name: "nested group clone with directory argument",
113 args: "-g NAMESPACE/NESTED/SUBGROUP mydir",
114 wantOpts: options{
115 gitFlags: []string{},
116 groupName: "NAMESPACE/NESTED/SUBGROUP",
117 dir: "mydir",
118 },
119 wantCtxOpts: ContextOpts{
120 Repo: "",
121 },
122 },
123 {
124 name: "group clone with directory argument and preserve namespace",
125 args: "-p -g NAMESPACE/REPO mydir",
126 wantOpts: options{
127 gitFlags: []string{},
128 groupName: "NAMESPACE/REPO",
129 dir: "mydir",
130 preserveNamespace: true,
131 },
132 wantCtxOpts: ContextOpts{
133 Repo: "",
134 },
135 },
136 {
137 name: "nested group clone with directory argument and preserve namespace",
138 args: "-p -g NAMESPACE/NESTED/SUBGROUP mydir",
139 wantOpts: options{
140 gitFlags: []string{},
141 groupName: "NAMESPACE/NESTED/SUBGROUP",
142 dir: "mydir",
143 preserveNamespace: true,
144 },
145 wantCtxOpts: ContextOpts{
146 Repo: "",
147 },
148 },
149 {
150 name: "unknown argument",
151 args: "NAMESPACE/REPO --depth 1",
152 wantErr: "unknown flag: --depth\nseparate Git clone flags with '--'",
153 },
154 {
155 name: "group clone with active=true",
156 args: "-g mygroup --active=true",
157 wantOpts: options{
158 gitFlags: []string{},
159 groupName: "mygroup",
160 active: true,
161 activeSet: true,
162 },
163 wantCtxOpts: ContextOpts{
164 Repo: "",
165 },
166 },
167 {
168 name: "group clone with active=false",
169 args: "-g mygroup --active=false",
170 wantOpts: options{
171 gitFlags: []string{},
172 groupName: "mygroup",
173 active: false,
174 activeSet: true,
175 },
176 wantCtxOpts: ContextOpts{
177 Repo: "",
178 },
179 },
180 {
181 name: "group clone with repeated exclude-subgroup",
182 args: "-g mygroup --exclude-subgroup domain/backend --exclude-subgroup docs",
183 wantOpts: options{
184 gitFlags: []string{},
185 groupName: "mygroup",
186 excludeSubgroups: []string{"domain/backend", "docs"},
187 },
188 },
189 {
190 name: "group clone with comma-separated exclude-subgroup",
191 args: "-g mygroup --exclude-subgroup domain/backend,docs",
192 wantOpts: options{
193 gitFlags: []string{},
194 groupName: "mygroup",
195 excludeSubgroups: []string{"domain/backend", "docs"},
196 },
197 },
198 {
199 name: "group clone without active flag",
200 args: "-g mygroup",
201 wantOpts: options{
202 gitFlags: []string{},
203 groupName: "mygroup",
204 active: false,
205 activeSet: false,
206 },
207 wantCtxOpts: ContextOpts{
208 Repo: "",
209 },
210 },
211 }
212 for _, tt := range testCases {
213 t.Run(tt.name, func(t *testing.T) {
214 t.Parallel()
216 io, stdin, stdout, stderr := cmdtest.TestIOStreams()
217 fac := cmdtest.NewTestFactory(io)
219 var opts *options
220 var ctxOpts *ContextOpts
221 cmd := NewCmdClone(fac, func(co *options, cx *ContextOpts) error {
222 opts = co
223 ctxOpts = cx
224 return nil
225 })
227 argv, err := shlex.Split(tt.args)
228 require.NoError(t, err)
229 cmd.SetArgs(argv)
231 cmd.SetIn(stdin)
232 cmd.SetOut(stdout)
233 cmd.SetErr(stderr)
235 _, err = cmd.ExecuteC()
236 if tt.wantErr != "" {
237 require.Error(t, err)
238 assert.Equal(t, tt.wantErr, err.Error())
239 return
240 }
242 require.NoError(t, err)
243 assert.Empty(t, stdout.String())
244 assert.Empty(t, stderr.String())
246 assert.Equal(t, tt.wantCtxOpts.Repo, ctxOpts.Repo)
247 assert.Equal(t, tt.wantOpts.gitFlags, opts.gitFlags)
248 assert.Equal(t, tt.wantOpts.groupName, opts.groupName)
249 assert.Equal(t, tt.wantOpts.active, opts.active)
250 assert.Equal(t, tt.wantOpts.activeSet, opts.activeSet)
251 assert.Equal(t, tt.wantOpts.dir, opts.dir)
252 assert.Equal(t, tt.wantOpts.preserveNamespace, opts.preserveNamespace)
253 assert.Equal(t, tt.wantOpts.wiki, opts.wiki)
254 assert.Equal(t, tt.wantOpts.excludeSubgroups, opts.excludeSubgroups)
255 })
256 }
257}
259func TestCloneRepo(t *testing.T) {
260 t.Parallel()
262 const defaultHost = "gitlab.com"
264 testCases := []struct {
265 name string
266 repoArg string
267 subfolder string
268 wantHost string
269 wantFullName string
270 wantErr bool
271 }{
272 {name: "default host URL", repoArg: "https://gitlab.com/gitlab-org/cli", wantHost: defaultHost, wantFullName: "gitlab-org/cli"},
273 {name: "self-managed URL uses its own host", repoArg: "https://mygitlab.com/mygroup/myrepo", wantHost: "mygitlab.com", wantFullName: "mygroup/myrepo"},
274 {name: "scp-style URL uses its own host", repoArg: "git@mygitlab.com:mygroup/myrepo.git", wantHost: "mygitlab.com", wantFullName: "mygroup/myrepo"},
275 {name: "ported host keeps its port", repoArg: "https://mygitlab.com:8080/mygroup/myrepo", wantHost: "mygitlab.com:8080", wantFullName: "mygroup/myrepo"},
276 {name: "URL resolves without a configured subfolder", repoArg: "https://example.com/gitlab/myrepo", wantHost: "example.com", wantFullName: "gitlab/myrepo"},
277 {name: "configured subfolder is stripped from the full name", repoArg: "https://example.com/gitlab/mygroup/myrepo", subfolder: "gitlab", wantHost: "example.com", wantFullName: "mygroup/myrepo"},
278 {name: "configured subfolder leaving no owner is rejected", repoArg: "https://example.com/gitlab/myrepo", subfolder: "gitlab", wantErr: true},
279 {name: "URL without a repo segment is rejected", repoArg: "https://gitlab.com/group", wantErr: true},
280 {name: "trailing-slash URL is rejected", repoArg: "https://gitlab.com/mygroup/", wantErr: true},
281 }
282 for _, tt := range testCases {
283 t.Run(tt.name, func(t *testing.T) {
284 t.Parallel()
286 cfg := config.NewBlankConfig()
287 if tt.subfolder != "" {
288 require.NoError(t, cfg.Set("example.com", "subfolder", tt.subfolder))
289 }
291 repo, err := cloneRepo(tt.repoArg, defaultHost, cfg)
292 if tt.wantErr {
293 require.Error(t, err)
294 return
295 }
296 require.NoError(t, err)
297 assert.Equal(t, tt.wantHost, repo.RepoHost())
298 assert.Equal(t, tt.wantFullName, repo.FullName())
299 })
300 }
301}
303func TestCloneFullURLMakesNoAPICall(t *testing.T) {
304 io, _, _, _ := cmdtest.TestIOStreams()
305 fac := cmdtest.NewTestFactory(io)
307 apiCalled := false
308 fac.ApiClientStub = func(string) (*api.Client, error) {
309 apiCalled = true
310 return &api.Client{}, nil
311 }
313 cs, restore := test.InitCmdStubber()
314 defer restore()
315 cs.Stub("")
317 cmd := NewCmdClone(fac, nil)
318 cmd.SetArgs([]string{"https://gitlab.example.com/mygroup/myrepo"})
319 cmd.SetIn(io.In)
320 cmd.SetOut(io.StdOut)
321 cmd.SetErr(io.StdErr)
323 _, err := cmd.ExecuteC()
324 require.NoError(t, err)
325 assert.False(t, apiCalled, "cloning a full URL must not create an API client")
326 require.Len(t, cs.Calls, 1)
327 assert.Equal(t, "git clone -- https://gitlab.example.com/mygroup/myrepo.git", strings.Join(cs.Calls[0].Args, " "))
328}
330func TestCloneWikiFromFullURLUsesURLHost(t *testing.T) {
331 io, _, _, _ := cmdtest.TestIOStreams()
333 var apiClientHost string
334 fac := cmdtest.NewTestFactory(io, func(f *cmdtest.Factory) {
335 f.ApiClientStub = func(repoHost string) (*api.Client, error) {
336 apiClientHost = repoHost
337 tc := gitlabtesting.NewTestClient(t)
338 tc.MockProjects.EXPECT().GetProject("mygroup/myrepo", gomock.Any()).Return(
339 &gitlab.Project{
340 PathWithNamespace: "mygroup/myrepo",
341 SSHURLToRepo: "git@gitlab.example.com:mygroup/myrepo.git",
342 HTTPURLToRepo: "https://gitlab.example.com/mygroup/myrepo.git",
343 WikiAccessLevel: gitlab.EnabledAccessControl,
344 }, nil, nil)
345 return cmdtest.NewTestApiClient(t, nil, "", repoHost, api.WithGitLabClient(tc.Client)), nil
346 }
347 })
349 cs, restore := test.InitCmdStubber()
350 defer restore()
351 cs.Stub("")
353 cmd := NewCmdClone(fac, nil)
354 cmd.SetArgs([]string{"--wiki", "https://gitlab.example.com/mygroup/myrepo"})
355 cmd.SetIn(io.In)
356 cmd.SetOut(io.StdOut)
357 cmd.SetErr(io.StdErr)
359 _, err := cmd.ExecuteC()
360 require.NoError(t, err)
361 assert.Equal(t, "gitlab.example.com", apiClientHost, "a full URL wiki clone must build the API client for the URL's host, not the default")
362}
364func TestCloneWikiFromFullURLStripsConfiguredSubfolder(t *testing.T) {
365 io, _, _, _ := cmdtest.TestIOStreams()
367 cfg := config.NewBlankConfig()
368 require.NoError(t, cfg.Set("example.com", "subfolder", "gitlab"))
370 fac := cmdtest.NewTestFactory(io, func(f *cmdtest.Factory) {
371 f.ConfigStub = func() config.Config { return cfg }
372 f.ApiClientStub = func(repoHost string) (*api.Client, error) {
373 tc := gitlabtesting.NewTestClient(t)
374 tc.MockProjects.EXPECT().GetProject("mygroup/myrepo", gomock.Any()).Return(
375 &gitlab.Project{
376 PathWithNamespace: "mygroup/myrepo",
377 SSHURLToRepo: "git@example.com:gitlab/mygroup/myrepo.git",
378 HTTPURLToRepo: "https://example.com/gitlab/mygroup/myrepo.git",
379 WikiAccessLevel: gitlab.EnabledAccessControl,
380 }, nil, nil)
381 return cmdtest.NewTestApiClient(t, nil, "", repoHost, api.WithGitLabClient(tc.Client)), nil
382 }
383 })
385 cs, restore := test.InitCmdStubber()
386 defer restore()
387 cs.Stub("")
389 cmd := NewCmdClone(fac, nil)
390 cmd.SetArgs([]string{"--wiki", "https://example.com/gitlab/mygroup/myrepo"})
391 cmd.SetIn(io.In)
392 cmd.SetOut(io.StdOut)
393 cmd.SetErr(io.StdErr)
395 _, err := cmd.ExecuteC()
396 require.NoError(t, err)
397}
399func TestCloneWikiRepository(t *testing.T) {
400 opts := &options{
401 currentUser: &gitlab.User{Username: "OWNER"},
402 protocol: "ssh",
403 wiki: true,
404 }
405 ctxOpts := &ContextOpts{
406 Project: &gitlab.Project{
407 PathWithNamespace: "OWNER/REPO",
408 SSHURLToRepo: "git@gitlab.com:OWNER/REPO.git",
409 WikiAccessLevel: gitlab.EnabledAccessControl,
410 },
411 Repo: "OWNER/REPO",
412 }
414 cs, restore := test.InitCmdStubber()
415 defer restore()
416 cs.Stub("")
418 require.NoError(t, cloneRun(opts, ctxOpts))
419 require.Len(t, cs.Calls, 1)
420 assert.Equal(t, "git clone -- git@gitlab.com:OWNER/REPO.wiki.git", strings.Join(cs.Calls[0].Args, " "))
421}
423func TestCloneWikiRepositoryRejectsDisabledWiki(t *testing.T) {
424 t.Parallel()
426 opts := &options{
427 currentUser: &gitlab.User{Username: "OWNER"},
428 protocol: "ssh",
429 wiki: true,
430 }
431 ctxOpts := &ContextOpts{
432 Project: &gitlab.Project{
433 PathWithNamespace: "OWNER/REPO",
434 SSHURLToRepo: "git@gitlab.com:OWNER/REPO.git",
435 WikiAccessLevel: gitlab.DisabledAccessControl,
436 },
437 Repo: "OWNER/REPO",
438 }
440 err := cloneRun(opts, ctxOpts)
442 require.EqualError(t, err, "wiki is not enabled for OWNER/REPO")
443}
445func TestCloneRejectsInvalidRemoteURL(t *testing.T) {
446 opts := &options{
447 currentUser: &gitlab.User{Username: "OWNER"},
448 protocol: "https",
449 }
450 ctxOpts := &ContextOpts{
451 Project: &gitlab.Project{
452 PathWithNamespace: "OWNER/REPO",
453 HTTPURLToRepo: "--upload-pack=touch x",
454 },
455 Repo: "OWNER/REPO",
456 }
458 cs, restore := test.InitCmdStubber()
459 defer restore()
461 err := cloneRun(opts, ctxOpts)
463 require.ErrorContains(t, err, "invalid git remote URL")
464 assert.Zero(t, cs.Count)
465}
467func TestCloneProjectNamedWiki(t *testing.T) {
468 opts := &options{
469 currentUser: &gitlab.User{Username: "OWNER"},
470 protocol: "ssh",
471 }
472 ctxOpts := &ContextOpts{
473 Project: &gitlab.Project{
474 PathWithNamespace: "OWNER/docs.wiki",
475 SSHURLToRepo: "git@gitlab.com:OWNER/docs.wiki.git",
476 },
477 Repo: "OWNER/docs.wiki",
478 }
480 cs, restore := test.InitCmdStubber()
481 defer restore()
482 cs.Stub("")
484 require.NoError(t, cloneRun(opts, ctxOpts))
485 require.Len(t, cs.Calls, 1)
486 assert.Equal(t, "git clone -- git@gitlab.com:OWNER/docs.wiki.git", strings.Join(cs.Calls[0].Args, " "))
487}
489func TestCloneRejectsInvalidAPIPaths(t *testing.T) {
490 tests := []struct {
491 name string
492 opts *options
493 project *gitlab.Project
494 }{
495 {
496 name: "preserve namespace with traversal",
497 opts: &options{
498 currentUser: &gitlab.User{Username: "OWNER"},
499 protocol: "https",
500 preserveNamespace: true,
501 dir: "work",
502 },
503 project: &gitlab.Project{
504 PathWithNamespace: "../../evil",
505 Path: "evil",
506 HTTPURLToRepo: "https://gitlab.com/OWNER/REPO.git",
507 },
508 },
509 {
510 name: "group clone with dir and dotdot path",
511 opts: &options{
512 currentUser: &gitlab.User{Username: "OWNER"},
513 protocol: "https",
514 groupName: "group",
515 dir: "work",
516 },
517 project: &gitlab.Project{
518 PathWithNamespace: "group/evil",
519 Path: "..",
520 HTTPURLToRepo: "https://gitlab.com/group/evil.git",
521 },
522 },
523 {
524 name: "fork upstream dir with absolute path",
525 opts: &options{
526 currentUser: &gitlab.User{Username: "OWNER"},
527 protocol: "https",
528 },
529 project: &gitlab.Project{
530 PathWithNamespace: "OWNER/evil",
531 Path: "/abs",
532 HTTPURLToRepo: "https://gitlab.com/OWNER/evil.git",
533 ForkedFromProject: &gitlab.ForkParent{PathWithNamespace: "up/evil"},
534 },
535 },
536 }
538 for _, tt := range tests {
539 t.Run(tt.name, func(t *testing.T) {
540 cs, restore := test.InitCmdStubber()
541 defer restore()
543 err := cloneRun(tt.opts, &ContextOpts{Project: tt.project, Repo: "OWNER/REPO"})
545 require.ErrorContains(t, err, "invalid project path")
546 assert.Zero(t, cs.Count)
547 })
548 }
549}
551func TestValidateAPIPath(t *testing.T) {
552 t.Parallel()
554 tests := []struct {
555 path string
556 ok bool
557 }{
558 {"gitlab-org/cli", true},
559 {"group/sub/project", true},
560 {"my.project", true},
561 {"", false},
562 {"/abs", false},
563 {"..", false},
564 {".", false},
565 {"../../x", false},
566 {"a/../b", false},
567 {"a//b", false},
568 {"a/", false},
569 {`a\b`, false},
570 {"C:/x", false},
571 {"-flag", false},
572 }
573 for _, tt := range tests {
574 t.Run(tt.path, func(t *testing.T) {
575 t.Parallel()
576 err := validateAPIPath(tt.path)
577 if tt.ok {
578 require.NoError(t, err)
579 } else {
580 require.ErrorContains(t, err, "invalid project path")
581 }
582 })
583 }
584}
586func TestIsExcludedSubgroup(t *testing.T) {
587 t.Parallel()
589 testCases := []struct {
590 name string
591 path string
592 excludes []string
593 want bool
594 }{
595 {name: "project in excluded subgroup", path: "mygroup/domain/backend/api", excludes: []string{"domain/backend"}, want: true},
596 {name: "project in nested excluded subgroup", path: "mygroup/domain/backend/svc/api", excludes: []string{"domain/backend"}, want: true},
597 {name: "project path equals exclude", path: "mygroup/domain/backend", excludes: []string{"domain/backend"}, want: true},
598 {name: "exclude prefixed with group", path: "mygroup/domain/backend/api", excludes: []string{"mygroup/domain/backend"}, want: true},
599 {name: "sibling sharing string prefix", path: "mygroup/domain/backend-utils/api", excludes: []string{"domain/backend"}, want: false},
600 {name: "parent subgroup project", path: "mygroup/domain/api", excludes: []string{"domain/backend"}, want: false},
601 {name: "different case", path: "mygroup/domain/backend/api", excludes: []string{"Domain/Backend"}, want: true},
602 {name: "surrounding slashes", path: "mygroup/domain/backend/api", excludes: []string{"/domain/backend/"}, want: true},
603 {name: "one of several excludes", path: "mygroup/docs/site", excludes: []string{"domain/backend", "docs"}, want: true},
604 {name: "no excludes", path: "mygroup/domain/backend/api", excludes: nil, want: false},
605 {name: "empty exclude", path: "mygroup/api", excludes: []string{"", "/"}, want: false},
606 }
607 for _, tt := range testCases {
608 t.Run(tt.name, func(t *testing.T) {
609 t.Parallel()
611 assert.Equal(t, tt.want, isExcludedSubgroup(tt.path, "mygroup", tt.excludes))
612 })
613 }
614}
616func TestGroupCloneExcludeSubgroup(t *testing.T) {
617 projects := []*gitlab.Project{
618 {Path: "api", PathWithNamespace: "mygroup/api", SSHURLToRepo: "git@gitlab.com:mygroup/api.git"},
619 {Path: "svc", PathWithNamespace: "mygroup/domain/backend/svc", SSHURLToRepo: "git@gitlab.com:mygroup/domain/backend/svc.git"},
620 {Path: "lib", PathWithNamespace: "mygroup/domain/backend-utils/lib", SSHURLToRepo: "git@gitlab.com:mygroup/domain/backend-utils/lib.git"},
621 {Path: "shared", PathWithNamespace: "othergroup/domain/backend/shared", SSHURLToRepo: "git@gitlab.com:othergroup/domain/backend/shared.git"},
622 }
623 clonesWithoutBackend := []string{
624 "git clone -- git@gitlab.com:mygroup/api.git",
625 "git clone -- git@gitlab.com:mygroup/domain/backend-utils/lib.git",
626 "git clone -- git@gitlab.com:othergroup/domain/backend/shared.git",
627 }
629 testCases := []struct {
630 name string
631 cli string
632 group string
633 projects []*gitlab.Project
634 numericID bool
635 getGroupErr error
636 wantClones []string
637 wantErr string
638 wantStderr string
639 }{
640 {
641 name: "skips projects in excluded subgroup",
642 cli: "-g mygroup --exclude-subgroup domain/backend",
643 group: "mygroup",
644 wantClones: clonesWithoutBackend,
645 },
646 {
647 name: "accepts exclude prefixed with group path",
648 cli: "-g mygroup --exclude-subgroup mygroup/domain/backend",
649 group: "mygroup",
650 wantClones: clonesWithoutBackend,
651 },
652 {
653 name: "resolves numeric group ID to its full path",
654 cli: "-g 123 --exclude-subgroup domain/backend",
655 group: "123",
656 numericID: true,
657 wantClones: clonesWithoutBackend,
658 },
659 {
660 name: "no projects left after exclusion",
661 cli: "-g mygroup --exclude-subgroup domain",
662 group: "mygroup",
663 projects: projects[1:3],
664 wantErr: cmdutils.SilentError.Error(),
665 wantStderr: `No projects left to clone in group "mygroup" after applying --exclude-subgroup.`,
666 },
667 {
668 name: "group lookup fails",
669 cli: "-g 123 --exclude-subgroup domain/backend",
670 group: "123",
671 getGroupErr: errors.New("404 Group Not Found"),
672 wantErr: "404 Group Not Found",
673 },
674 }
675 for _, tc := range testCases {
676 t.Run(tc.name, func(t *testing.T) {
677 testClient := gitlabtesting.NewTestClient(t)
678 testClient.MockUsers.EXPECT().CurrentUser().Return(&gitlab.User{Username: "OWNER"}, nil, nil)
679 if tc.projects == nil {
680 tc.projects = projects
681 }
682 if tc.getGroupErr != nil {
683 testClient.MockGroups.EXPECT().GetGroup(tc.group, gomock.Any()).Return(nil, nil, tc.getGroupErr)
684 } else {
685 if tc.numericID {
686 testClient.MockGroups.EXPECT().GetGroup(tc.group, gomock.Any()).Return(&gitlab.Group{FullPath: "mygroup"}, nil, nil)
687 }
688 testClient.MockGroups.EXPECT().ListGroupProjects(tc.group, gomock.Any()).Return(slices.Clone(tc.projects), nil, nil)
689 }
691 cs, restore := test.InitCmdStubber()
692 defer restore()
693 for range tc.wantClones {
694 cs.Stub("")
695 }
697 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
698 return NewCmdClone(f, nil)
699 }, false,
700 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
701 cmdtest.WithConfig(config.NewFromString("git_protocol: ssh\n")),
702 )
704 out, err := exec(tc.cli)
706 if tc.wantErr != "" {
707 require.EqualError(t, err, tc.wantErr)
708 } else {
709 require.NoError(t, err)
710 }
711 if tc.wantStderr != "" {
712 assert.Contains(t, out.Stderr(), tc.wantStderr)
713 }
714 require.Len(t, cs.Calls, len(tc.wantClones))
715 for i, want := range tc.wantClones {
716 assert.Equal(t, want, strings.Join(cs.Calls[i].Args, " "))
717 }
718 })
719 }
720}
Diagnostics8 git calls · 220 ms
CallStatusSizeTime0220 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 · 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=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:02001.8 KiB · hit1 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=1857 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 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=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200614 B · 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=614 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=3a89d0cfc8e81880c982180388e42e561a0b7eee 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=614 git.pkt.payload.max=587 git.sideband.band1.bytes=587 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=587 git.pack.trailer=eb8250f8e8340ff2fafc22c5aba90db34fd906a9Open pack map
fetchtree:0200179 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=211 git.response.bytes=179 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=157370d354379c59ce19f23f16d03a4e5a5fab91 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=179 git.pkt.payload.max=152 git.sideband.band1.bytes=152 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=152 git.pack.trailer=9d92c3ecac1aa3febd9d2b53a59a0ae7785a4a68Open pack map
fetch2004.2 KiB211 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4267 git.response.cache=MISS git.response.duration_ms=211 git.fetch.wants=44942be39185282aa62639303466122920511aec 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=4267 git.pkt.payload.max=4240 git.sideband.band1.bytes=4240 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
- a5ea56836fec7089
- Request
- 7bb9c482…
- 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