GitLabgitlab-org
cli
main
1package archive
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "slices"
10 "strings"
12 "github.com/MakeNowJust/heredoc/v2"
13 "github.com/dustin/go-humanize"
14 "github.com/spf13/cobra"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 "gitlab.com/gitlab-org/cli/internal/cmdutils"
19 "gitlab.com/gitlab-org/cli/internal/glrepo"
20 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
21)
23func NewCmdArchive(f cmdutils.Factory) *cobra.Command {
24 repoArchiveCmd := &cobra.Command{
25 Use: "archive [<repo>] [<dir>] [flags]",
26 Short: `Get an archive of the repository.`,
27 Example: heredoc.Doc(`
28 glab repo archive profclems/glab
30 # Downloads zip file of current repository
31 glab repo archive
33 # Downloads repo zip file into mydirectory
34 glab repo archive profclems/glab mydirectory
36 # Finds repo for current user and download in ZIP format
37 glab repo archive profclems/glab --format=zip`),
38 Long: heredoc.Doc(`
39 Without a repository argument, archives the current repository.
40 The repository accepts these shorthand references:
42 - repo
43 - namespace/repo
44 - namespace/group/repo
46 The second argument sets the directory to download the archive into.
47 `),
48 Args: cobra.MaximumNArgs(2),
49 Annotations: map[string]string{
50 mcpannotations.Destructive: "true",
51 },
52 RunE: func(cmd *cobra.Command, args []string) error {
53 var name string
54 var err error
56 var client *gitlab.Client
57 var repo glrepo.Interface
58 if len(args) != 0 {
59 repo, err = glrepo.FromFullName(args[0], f.DefaultHostname(), f.Config())
60 if err != nil {
61 return err
62 }
64 // repository is coming from command args, not -R
65 apiClient, err := f.ApiClient(repo.RepoHost())
66 if err != nil {
67 return err
68 }
70 if len(args) > 1 {
71 name = args[1]
72 }
74 client = apiClient.Lab()
75 } else {
76 client, err = f.GitLabClient()
77 if err != nil {
78 return err
79 }
80 repo, err = f.BaseRepo()
81 if err != nil {
82 return err
83 }
84 }
86 format, _ := cmd.Flags().GetString("format")
88 extensions := []string{"tar.gz", "tar.bz2", "tbz", "tbz2", "tb2", "bz2", "tar", "zip"}
89 if b := contains(extensions, format); !b {
90 return errors.New("format must be one of " + strings.Join(extensions, ","))
91 }
93 l := &gitlab.ArchiveOptions{}
94 l.Format = new(format)
95 if sha, _ := cmd.Flags().GetString("sha"); sha != "" {
96 l.SHA = new(sha)
97 }
98 ext := *l.Format
99 archiveName := strings.ReplaceAll(repo.FullName(), "/", "-") + "." + ext
100 if strings.TrimSpace(name) != "" {
101 archiveName = name + "." + ext
102 }
104 bt, _, err := client.Repositories.Archive(repo.FullName(), l)
105 if err != nil {
106 return fmt.Errorf("failed to get archive: %w", err)
107 }
109 r := bytes.NewReader(bt)
110 out, err := os.Create(archiveName + ".tmp")
111 if err != nil {
112 return fmt.Errorf("failed to create repository archive: %w", err)
113 }
115 counter := &CloneWriter{}
116 if _, err = io.Copy(out, io.TeeReader(r, counter)); err != nil {
117 _ = out.Close()
118 return fmt.Errorf("failed to write repositories: %w", err)
119 }
121 f.IO().LogInfo()
122 _ = out.Close()
123 if err = os.Rename(archiveName+".tmp", archiveName); err != nil {
124 return fmt.Errorf("failed to rename tmp repos: %w", err)
125 }
126 f.IO().LogInfo("Complete...", archiveName)
127 return nil
128 },
129 }
131 repoArchiveCmd.Flags().StringP("format", "f", "zip", "Optional. Specify format if you want a downloaded archive: tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, zip.")
132 repoArchiveCmd.Flags().StringP("sha", "s", "", "The commit SHA to download. A tag, branch reference, or SHA can be used. Defaults to the tip of the default branch if not specified.")
134 return repoArchiveCmd
135}
137// CloneWriter w
138type CloneWriter struct {
139 Total uint64
140}
142func (wc *CloneWriter) Write(p []byte) (int, error) {
143 n := len(p)
144 wc.Total += uint64(n)
145 wc.progress()
146 return n, nil
147}
149func (wc CloneWriter) progress() {
150 fmt.Printf("\r%s", strings.Repeat(" ", 35))
151 fmt.Printf("\rCloning... %s complete", humanize.Bytes(wc.Total))
152}
154func contains(arr []string, str string) bool {
155 return slices.Contains(arr, str)
156}
Diagnostics8 git calls · 212 ms
CallStatusSizeTime0212 ms
discover200185 B3 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 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=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 · 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=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=3 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 · 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=614 git.response.cache=HIT git.response.duration_ms=3 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:0200156 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=200 git.response.bytes=156 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=fc5027baa602a812421c16b97e69723dbcfb48dd 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=156 git.pkt.payload.max=129 git.sideband.band1.bytes=129 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=129 git.pack.trailer=fa119b65b340aa7833f59c5be9710a353590ec19Open pack map
fetch2001.7 KiB200 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1724 git.response.cache=MISS git.response.duration_ms=200 git.fetch.wants=9361489733ba751768aa1d9974f7e49f8c16f5ff 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=1724 git.pkt.payload.max=1697 git.sideband.band1.bytes=1697 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
- a5ea56c23e3f7089
- Request
- d1d850f6…
- 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