GitLabgitlab-org

cli

main
ply8 git calls253 ms100 KiB6 cachedprotocol v2
216 lines7.1 KiBRaw
1package mirror
3import (
4 "errors"
5 "strings"
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/config"
15 "gitlab.com/gitlab-org/cli/internal/glrepo"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20type options struct {
21 url string
22 direction string
23 enabled bool
24 protectedBranchesOnly bool
25 allowDivergence bool
26 projectID int64
28 io *iostreams.IOStreams
29 baseRepo glrepo.Interface
30 apiClient func(repoHost string) (*api.Client, error)
31 gitlabClient func() (*gitlab.Client, error)
32 client *gitlab.Client
33 baseRepoFactory func() (glrepo.Interface, error)
34 defaultHostname string
35 config config.Config
38func NewCmdMirror(f cmdutils.Factory) *cobra.Command {
39 opts := options{
40 io: f.IO(),
41 apiClient: f.ApiClient,
42 gitlabClient: f.GitLabClient,
43 baseRepoFactory: f.BaseRepo,
44 defaultHostname: f.DefaultHostname(),
45 config: f.Config(),
46 }
48 projectMirrorCmd := &cobra.Command{
49 Use: "mirror [<id | url | path>] [flags]",
50 Short: "Configure mirroring on an existing project to sync with a remote repository.",
51 Long: heredoc.Docf(`
52 Configure repository mirroring for an existing GitLab project.
54 The GitLab project must already exist. This command configures mirroring
55 on existing projects but does not create new projects.
57 Mirror types:
59 - Pull mirror: Syncs changes from an external repository to your GitLab project. The external repository is the source of truth. Use pull mirrors to sync from GitHub, Bitbucket, or other GitLab instances.
60 - Push mirror: Syncs changes from your GitLab project to an external repository. Your GitLab project is the source of truth. Use push mirrors to sync to GitHub, Bitbucket, or other GitLab instances.
62 Authentication:
64 - For pull mirrors from private repositories, embed credentials in the URL: %[1]shttps://username:token@gitlab.example.com/org/repo%[1]s
65 - For push mirrors to private repositories, configure credentials in the GitLab UI or use SSH URLs with deploy keys.
66 `, "`"),
67 Example: heredoc.Docf(`
68 # Create a project, then configure pull mirroring
69 glab repo create mygroup/myproject --public
70 glab repo mirror mygroup/myproject --direction=pull --url=%[1]shttps://gitlab.example.com/org/repo%[1]s
72 # Configure pull mirroring from a private repository
73 glab repo mirror mygroup/myproject --direction=pull --url=%[1]shttps://username:token@gitlab.example.com/org/private-repo%[1]s
75 # Configure pull mirroring for protected branches only
76 glab repo mirror mygroup/myproject --direction=pull --url=%[1]shttps://gitlab.example.com/org/repo%[1]s --protected-branches-only
78 # Configure push mirroring to another GitLab instance
79 glab repo mirror mygroup/myproject --direction=push --url=%[1]shttps://gitlab-backup.example.com/backup/myproject%[1]s
81 # Configure push mirroring and allow divergent refs
82 glab repo mirror mygroup/myproject --direction=push --url=%[1]shttps://gitlab-backup.example.com/backup/repo%[1]s --allow-divergence`, `"`),
83 Args: cobra.MaximumNArgs(1),
84 Annotations: map[string]string{
85 mcpannotations.Destructive: "true",
86 },
87 RunE: func(cmd *cobra.Command, args []string) error {
88 if err := opts.complete(args); err != nil {
89 return err
90 }
91 if err := opts.validate(); err != nil {
92 return err
93 }
95 return opts.run()
96 },
97 }
98 projectMirrorCmd.Flags().StringVar(&opts.url, "url", "", "The target URL to which the repository is mirrored.")
99 projectMirrorCmd.Flags().StringVar(&opts.direction, "direction", "pull", "Mirror direction. Options: pull, push.")
100 projectMirrorCmd.Flags().BoolVar(&opts.enabled, "enabled", true, "Determines if the mirror is enabled.")
101 projectMirrorCmd.Flags().BoolVar(&opts.protectedBranchesOnly, "protected-branches-only", false, "Determines if only protected branches are mirrored.")
102 projectMirrorCmd.Flags().BoolVar(&opts.allowDivergence, "allow-divergence", false, "Determines if divergent refs are skipped.")
104 _ = projectMirrorCmd.MarkFlagRequired("url")
105 _ = projectMirrorCmd.MarkFlagRequired("direction")
107 return projectMirrorCmd
110func (o *options) complete(args []string) error {
111 if len(args) > 0 {
112 baseRepo, err := glrepo.FromFullName(args[0], o.defaultHostname, o.config)
113 if err != nil {
114 return err
115 }
116 o.baseRepo = baseRepo
118 o.gitlabClient = func() (*gitlab.Client, error) {
119 if o.client != nil {
120 return o.client, nil
121 }
122 c, err := o.apiClient(o.baseRepo.RepoHost())
123 if err != nil {
124 return nil, err
125 }
126 o.client = c.Lab()
127 return o.client, nil
128 }
130 } else {
131 baseRepo, err := o.baseRepoFactory()
132 if err != nil {
133 return err
134 }
135 o.baseRepo = baseRepo
136 }
138 o.url = strings.TrimSpace(o.url)
140 client, err := o.gitlabClient()
141 if err != nil {
142 return err
143 }
144 o.client = client
146 project, err := o.baseRepo.Project(o.client)
147 if err != nil {
148 return cmdutils.WrapError(
149 err,
150 "Failed to find project. The project must exist before you can configure mirroring. Create it with 'glab repo create'.",
151 )
152 }
153 o.projectID = project.ID
155 return nil
158func (o *options) validate() error {
159 if o.direction != "pull" && o.direction != "push" {
160 return cmdutils.WrapError(
161 errors.New("invalid choice for --direction"),
162 "the argument direction value should be 'pull' or 'push'.",
163 )
164 }
166 if o.direction == "pull" && o.allowDivergence {
167 o.io.LogInfof(
168 "[Warning] the 'allow-divergence' flag has no effect for pull mirroring, and is ignored.\n",
169 )
170 }
172 return nil
175func (o *options) run() error {
176 if o.direction == "push" {
177 return o.createPushMirror()
178 } else {
179 return o.createPullMirror()
180 }
183func (o *options) createPushMirror() error {
184 pm, _, err := o.client.ProjectMirrors.AddProjectMirror(o.projectID, &gitlab.AddProjectMirrorOptions{
185 URL: new(o.url),
186 Enabled: new(o.enabled),
187 OnlyProtectedBranches: new(o.protectedBranchesOnly),
188 KeepDivergentRefs: new(o.allowDivergence),
189 })
190 if err != nil {
191 return cmdutils.WrapError(err, "Failed to create push mirror. Check if the project exists and ensure you have the necessary permissions.")
192 }
193 greenCheck := o.io.Color().Green("✓")
194 o.io.LogInfof(
195 "%s Created push mirror for %s (%d) on GitLab at %s (%d).\n",
196 greenCheck, pm.URL, pm.ID, o.baseRepo.FullName(), o.projectID,
197 )
198 return err
201func (o *options) createPullMirror() error {
202 _, _, err := o.client.Projects.EditProject(o.projectID, &gitlab.EditProjectOptions{
203 ImportURL: new(o.url),
204 Mirror: new(o.enabled),
205 OnlyMirrorProtectedBranches: new(o.protectedBranchesOnly),
206 })
207 if err != nil {
208 return cmdutils.WrapError(err, "Failed to create pull mirror. Check if the project exists and ensure you have the necessary permissions.")
209 }
210 greenCheck := o.io.Color().Green("✓")
211 o.io.LogInfof(
212 "%s Created pull mirror for %s on GitLab at %s (%d).\n",
213 greenCheck, o.url, o.baseRepo.FullName(), o.projectID,
214 )
215 return err
Diagnostics8 git calls · 253 ms
CallStatusSizeTime0253 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 · 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=4273
git.response.cache=HIT
git.response.duration_ms=1
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 · hit2 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=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
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=fb03183b7909876778f740d196d7516811003517
Open 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=eb8250f8e8340ff2fafc22c5aba90db34fd906a9
Open pack map
fetchtree:0200145 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=145
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=e395af8aa4461b7aeed97c5dcfaac0f700a81dd0
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=145
git.pkt.payload.max=118
git.sideband.band1.bytes=118
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=118
git.pack.trailer=9136b8fb22290652e906998a8c95b23b9125e63d
Open pack map
fetch2002.2 KiB242 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=242
git.response.bytes=2279
git.response.cache=MISS
git.response.duration_ms=242
git.fetch.wants=73012e5fce247e0506a78e3dcab7da0e164f7111
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=2279
git.pkt.payload.max=2252
git.sideband.band1.bytes=2252
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
a5ea569d4baa7089
Request
71df4855…
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