GitLabgitlab-org
cli
main
1package add
3import (
4 "errors"
5 "fmt"
6 "strings"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 "gitlab.com/gitlab-org/cli/internal/api"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/config"
14 "gitlab.com/gitlab-org/cli/internal/git"
15 "gitlab.com/gitlab-org/cli/internal/glrepo"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
18)
20type options struct {
21 projectID string
22 remoteName string
23 protocol string
24 repoHost string
25 defaultHostname string
27 io *iostreams.IOStreams
28 remotes func() (glrepo.Remotes, error)
29 apiClient func(repoHost string) (*api.Client, error)
30 config func() config.Config
31}
33func NewCmdRemoteAdd(f cmdutils.Factory) *cobra.Command {
34 opts := &options{
35 io: f.IO(),
36 remotes: f.Remotes,
37 apiClient: f.ApiClient,
38 config: f.Config,
39 defaultHostname: f.DefaultHostname(),
40 }
42 cmd := &cobra.Command{
43 Use: "add <namespace/project>",
44 Short: "Add a Git remote for a GitLab project.",
45 Long: heredoc.Doc(`
46 Add a Git remote for a GitLab project using a project reference.
48 The remote name defaults to the first path component (the namespace),
49 so the remote identifies where the repository lives.
50 `),
51 Example: heredoc.Doc(`
52 # Add a remote repository (remote named "alice")
53 glab repo remote add alice/my-project
55 # Add a remote repository with a custom name
56 glab repo remote add alice/my-project --name upstream
58 # Add a remote repository in a subgroup (remote named "group")
59 glab repo remote add group/subgroup/my-project`),
60 Args: cobra.ExactArgs(1),
61 Annotations: map[string]string{
62 mcpannotations.Destructive: "true",
63 },
64 RunE: func(cmd *cobra.Command, args []string) error {
65 if err := opts.complete(args); err != nil {
66 return err
67 }
68 if err := opts.validate(); err != nil {
69 return err
70 }
71 return opts.run()
72 },
73 }
75 cmd.Flags().StringVarP(&opts.remoteName, "name", "n", "", "Name for the remote (default: first path component)")
76 cmd.Flags().StringVarP(&opts.protocol, "protocol", "p", "", "Git protocol: ssh, https (default: git_protocol config)")
78 return cmd
79}
81func (o *options) complete(args []string) error {
82 o.projectID = args[0]
84 if o.remoteName == "" {
85 o.remoteName = defaultRemoteName(o.projectID)
86 }
88 repo, err := glrepo.FromFullName(o.projectID, o.defaultHostname, o.config())
89 if err != nil {
90 return fmt.Errorf("invalid project reference: %w", err)
91 }
92 o.repoHost = repo.RepoHost()
94 if o.protocol == "" {
95 o.protocol, _ = o.config().Get(o.repoHost, "git_protocol")
96 }
98 return nil
99}
101func (o *options) validate() error {
102 if o.protocol != "ssh" && o.protocol != "https" {
103 return cmdutils.WrapError(
104 errors.New("invalid protocol"),
105 fmt.Sprintf("protocol must be 'ssh' or 'https', got %q", o.protocol),
106 )
107 }
109 remotes, err := o.remotes()
110 if err != nil {
111 return fmt.Errorf("failed to list remotes: %w", err)
112 }
113 if existing, _ := remotes.FindByName(o.remoteName); existing != nil {
114 return fmt.Errorf("remote %q already exists", o.remoteName)
115 }
117 return nil
118}
120func (o *options) run() error {
121 apiClient, err := o.apiClient(o.repoHost)
122 if err != nil {
123 return err
124 }
126 project, err := api.GetProject(apiClient.Lab(), o.projectID)
127 if err != nil {
128 return fmt.Errorf("failed to find project %q: %w", o.projectID, err)
129 }
131 remoteURL, err := glrepo.RemoteURL(project, o.protocol)
132 if err != nil {
133 return err
134 }
136 if _, err = git.AddRemote(o.remoteName, remoteURL); err != nil {
137 return cmdutils.WrapError(err, "failed to add remote")
138 }
140 o.io.LogInfof("%s Remote %q added using %s protocol.\n",
141 o.io.Color().GreenCheck(), o.remoteName, o.protocol)
143 return nil
144}
146func defaultRemoteName(projectID string) string {
147 parts := strings.Split(projectID, "/")
148 return parts[0]
149}
Diagnostics9 git calls · 223 ms
CallStatusSizeTime0223 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 · hit2 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=HIT 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
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=2 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=4 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:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=2 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:0200139 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=139 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=86b4a9f4a0b09eded2143443ad74d7f917671c23 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=139 git.pkt.payload.max=112 git.sideband.band1.bytes=112 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=112 git.pack.trailer=6a9499ce505b6df1dd25fe321c1d78aa9bbaf947Open pack map
fetchtree:0200135 B · hit4 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=135 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=b692c18e7078bb6aa5483ed627c7ca0f9bba8494 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=135 git.pkt.payload.max=108 git.sideband.band1.bytes=108 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=108 git.pack.trailer=e8840bdb8fc726bffb54b68317e2352e4cfb344eOpen pack map
fetch2001.4 KiB206 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=206 git.response.bytes=1456 git.response.cache=MISS git.response.duration_ms=206 git.fetch.wants=fdec2836badc35544f77db34fdaf188ff994736d 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=1456 git.pkt.payload.max=1429 git.sideband.band1.bytes=1429 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
- a5ea62118befecd4
- Request
- 83407777…
- 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