GitLabgitlab-org
cli
main
1package list
3import (
4 "errors"
5 "fmt"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/iostreams"
15 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
16 "gitlab.com/gitlab-org/cli/internal/tableprinter"
17)
19type options struct {
20 orderBy string
21 sort string
22 group string
23 includeSubgroups bool
24 perPage int
25 page int
26 outputFormat string
27 filterAll bool
28 filterOwner bool
29 filterMember bool
30 filterStarred bool
31 archived bool
32 archivedSet bool
33 user string
35 io *iostreams.IOStreams
36 apiClient func(repoHost string) (*api.Client, error)
37}
39func NewCmdList(f cmdutils.Factory) *cobra.Command {
40 opts := &options{
41 io: f.IO(),
42 apiClient: f.ApiClient,
43 }
44 repoListCmd := &cobra.Command{
45 Use: "list",
46 Short: `Get list of repositories.`,
47 Long: heredoc.Docf(`
48 By default, lists the projects you own. Use %[1]s--all%[1]s to list every
49 project on the instance, %[1]s--group%[1]s to scope to one group, or
50 %[1]s--user%[1]s to list another user's projects.
51 `, "`"),
52 Example: heredoc.Doc(`
53 glab repo list`),
54 Args: cobra.NoArgs,
55 ValidArgsFunction: cobra.NoFileCompletions,
56 Aliases: []string{"ls"},
57 Annotations: map[string]string{
58 mcpannotations.Safe: "true",
59 },
60 RunE: func(cmd *cobra.Command, args []string) error {
61 opts.complete(cmd)
63 return opts.run()
64 },
65 }
67 repoListCmd.Flags().StringVarP(&opts.orderBy, "order", "o", "last_activity_at", "Return repositories ordered by id, name, path, created_at, updated_at, similarity, star_count, last_activity_at.")
68 repoListCmd.Flags().StringVarP(&opts.sort, "sort", "s", "", "Sort direction for --order field: asc or desc.")
69 repoListCmd.Flags().StringVarP(&opts.group, "group", "g", "", "Return repositories in only the given group.")
70 repoListCmd.Flags().BoolVarP(&opts.includeSubgroups, "include-subgroups", "G", false, "Include projects in subgroups of this group. Default is false. Used with the '--group' flag.")
71 repoListCmd.Flags().IntVarP(&opts.page, "page", "p", 1, "Page number.")
72 repoListCmd.Flags().IntVarP(&opts.perPage, "per-page", "P", 30, "Number of items to list per page.")
73 cmdutils.EnableJSONOutput(repoListCmd, opts.io, &opts.outputFormat)
74 repoListCmd.Flags().BoolVarP(&opts.filterAll, "all", "a", false, "List all projects on the instance. Removes the ownership filter. Results are still paginated. Use --page to navigate.")
75 repoListCmd.Flags().BoolVarP(&opts.filterOwner, "mine", "m", false, "List only projects you own. Default if no filters are provided.")
76 repoListCmd.Flags().StringVarP(&opts.user, "user", "u", "", "List user projects.")
77 repoListCmd.Flags().BoolVar(&opts.filterMember, "member", false, "List only projects of which you are a member.")
78 repoListCmd.Flags().BoolVar(&opts.filterStarred, "starred", false, "List only starred projects.")
79 repoListCmd.Flags().BoolVar(&opts.archived, "archived", false, "Limit by archived status. Use 'false' to exclude archived repositories. Used with the '--group' flag.")
81 repoListCmd.MarkFlagsMutuallyExclusive("user", "group")
82 return repoListCmd
83}
85func (o *options) complete(cmd *cobra.Command) {
86 o.archivedSet = cmd.Flags().Changed("archived")
87}
89func (o *options) run() error {
90 var err error
91 c := o.io.Color()
93 apiClient, err := o.apiClient("")
94 if err != nil {
95 return err
96 }
97 gitlabClient := apiClient.Lab()
99 var projects []*gitlab.Project
100 var resp *gitlab.Response
101 switch {
102 case o.group != "":
103 projects, resp, err = listAllProjectsForGroup(gitlabClient, *o)
104 case o.user != "":
105 projects, resp, err = listAllProjectsForUser(gitlabClient, *o)
106 default:
107 projects, resp, err = listAllProjects(gitlabClient, *o)
108 }
110 if err != nil {
111 return err
112 }
114 if o.outputFormat == "json" {
115 if err := o.io.PrintJSON(projects); err != nil {
116 return err
117 }
118 } else {
119 // Title
120 title := fmt.Sprintf("Showing %d of %d projects (Page %d of %d).\n", len(projects), resp.TotalItems, resp.CurrentPage, resp.TotalPages)
122 // List
123 table := tableprinter.NewTablePrinter()
124 if len(projects) > 0 {
125 table.AddRow("Project path", "Git URL", "Description")
126 }
128 for _, prj := range projects {
129 table.AddCell(c.Blue(prj.PathWithNamespace))
130 table.AddCell(prj.SSHURLToRepo)
131 table.AddCell(prj.Description)
132 table.EndRow()
133 }
135 o.io.LogInfof("%s\n%s\n", title, table.String())
136 }
138 return err
139}
141func listAllProjects(apiClient *gitlab.Client, opts options) ([]*gitlab.Project, *gitlab.Response, error) {
142 l := &gitlab.ListProjectsOptions{
143 ListOptions: gitlab.ListOptions{
144 PerPage: int64(opts.perPage),
145 Page: int64(opts.page),
146 },
147 OrderBy: new(opts.orderBy),
148 }
150 // Other filters only valid if FilterAll not true
151 if !opts.filterAll {
152 if !opts.filterStarred && !opts.filterMember {
153 // if no other filters are passed, default to Owned filter
154 l.Owned = new(true)
155 }
157 if opts.filterOwner {
158 l.Owned = new(opts.filterOwner)
159 }
161 if opts.filterStarred {
162 l.Starred = new(opts.filterStarred)
163 }
165 if opts.filterMember {
166 l.Membership = new(opts.filterMember)
167 }
168 }
170 if opts.archivedSet {
171 l.Archived = new(opts.archived)
172 }
174 if opts.sort != "" {
175 l.Sort = new(opts.sort)
176 }
178 return apiClient.Projects.ListProjects(l)
179}
181func listAllProjectsForGroup(apiClient *gitlab.Client, opts options) ([]*gitlab.Project, *gitlab.Response, error) {
182 group, resp, err := apiClient.Groups.GetGroup(opts.group, &gitlab.GetGroupOptions{})
183 if err != nil {
184 if errors.Is(err, gitlab.ErrNotFound) {
185 return nil, nil, fmt.Errorf("no group matching path %s", opts.group)
186 }
187 return nil, resp, err
188 }
190 l := &gitlab.ListGroupProjectsOptions{
191 ListOptions: gitlab.ListOptions{
192 PerPage: int64(opts.perPage),
193 Page: int64(opts.page),
194 },
195 OrderBy: new(opts.orderBy),
196 }
198 // Other filters only valid if FilterAll not true
199 if !opts.filterAll {
200 if !opts.filterStarred && !opts.filterMember {
201 // if no other filters are passed, default to Owned filter
202 l.Owned = new(true)
203 }
205 if opts.filterOwner {
206 l.Owned = new(opts.filterOwner)
207 }
209 if opts.filterStarred {
210 l.Starred = new(opts.filterStarred)
211 }
212 }
214 if opts.includeSubgroups {
215 l.IncludeSubGroups = new(true)
216 }
218 if opts.archivedSet {
219 l.Archived = new(opts.archived)
220 }
222 if opts.sort != "" {
223 l.Sort = new(opts.sort)
224 }
226 return apiClient.Groups.ListGroupProjects(group.ID, l)
227}
229func listAllProjectsForUser(apiClient *gitlab.Client, opts options) ([]*gitlab.Project, *gitlab.Response, error) {
230 l := &gitlab.ListProjectsOptions{
231 OrderBy: new(opts.orderBy),
232 ListOptions: gitlab.ListOptions{
233 PerPage: int64(opts.perPage),
234 Page: int64(opts.page),
235 },
236 }
238 if opts.archivedSet {
239 l.Archived = new(opts.archived)
240 }
242 if opts.filterStarred {
243 l.Starred = new(opts.filterStarred)
244 }
246 if opts.sort != "" {
247 l.Sort = new(opts.sort)
248 }
250 return apiClient.Projects.ListUserProjects(opts.user, l)
251}
Diagnostics8 git calls · 202 ms
CallStatusSizeTime0202 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 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
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=3 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=2 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=1 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:0200134 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=134 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=169be413780d9e9d7c54514d002c8f196a648e9a 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=a92e6fbe4b21e2fbd70e08d9caa80951a91ffd51Open pack map
fetch2002.2 KiB191 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=191 git.response.bytes=2274 git.response.cache=MISS git.response.duration_ms=191 git.fetch.wants=f26e4b672d726f910d4ae206bc038b82de008a57 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=2274 git.pkt.payload.max=2247 git.sideband.band1.bytes=2247 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
- a5ea567fb9b77089
- Request
- 6d710102…
- 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