GitLabgitlab-org

cli

main
ply8 git calls377 ms99 KiB5 cachedprotocol v2
178 lines5.1 KiBRaw
1package update
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/commands/skills/installed"
15 "gitlab.com/gitlab-org/cli/internal/commands/skills/registry"
16 "gitlab.com/gitlab-org/cli/internal/commands/skills/skill"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19 "gitlab.com/gitlab-org/cli/internal/text"
22type options struct {
23 io *iostreams.IOStreams
24 requested string
25 all bool
28func NewCmd(f cmdutils.Factory) *cobra.Command {
29 opts := &options{io: f.IO()}
31 cmd := &cobra.Command{
32 Use: "update [name]",
33 Short: "Update installed agent skills to the current shipped version. (EXPERIMENTAL)",
34 Long: heredoc.Doc(`
35 Re-fetch installed agent skills from their source (bundled in this
36 glab binary, or the curated remote registry) and overwrite the on-disk
37 copy if it differs.
39 By default, updates only the named skill in every well-known location
40 it is installed (the current project's '.agents/skills/' and the
41 user-scope '~/.agents/skills/'). Use --all to update every installed
42 skill in those locations.
44 Skills whose on-disk content already matches the source are left alone.
45 Skills installed via 'glab skills install --path' are not considered —
46 update only knows about the two well-known locations.
47 `) + text.ExperimentalString,
48 Example: heredoc.Doc(`
49 # Update the bundled glab skill in every location it is installed
50 glab skills update glab
52 # Update every installed skill
53 glab skills update --all
54 `),
55 Args: cobra.MaximumNArgs(1),
56 Annotations: map[string]string{
57 mcpannotations.Destructive: "true",
58 },
59 RunE: func(cmd *cobra.Command, args []string) error {
60 opts.complete(cmd, args)
61 if err := opts.validate(); err != nil {
62 return err
63 }
64 return opts.run(cmd.Context())
65 },
66 }
68 cmd.Flags().BoolVar(&opts.all, "all", false, "Update every installed skill.")
70 return cmd
73func (o *options) complete(_ *cobra.Command, args []string) {
74 if len(args) == 1 {
75 o.requested = args[0]
76 }
79func (o *options) validate() error {
80 if o.requested == "" && !o.all {
81 return cmdutils.WrapError(errors.New("missing argument"), "pass a skill name or use --all.")
82 }
83 if o.requested != "" && o.all {
84 return cmdutils.WrapError(errors.New("flag conflict"), "cannot combine a skill name with --all.")
85 }
86 return nil
89func (o *options) run(ctx context.Context) error {
90 installedSkills, err := installed.Discover()
91 if err != nil {
92 return err
93 }
95 targets := installedSkills
96 if !o.all {
97 targets = filterByName(installedSkills, o.requested)
98 if len(targets) == 0 {
99 return cmdutils.WrapError(
100 fmt.Errorf("not installed"),
101 fmt.Sprintf("skill %q is not installed in any known location. Run 'glab skills install %s' first.", o.requested, o.requested),
102 )
103 }
104 }
106 if len(targets) == 0 {
107 o.io.LogError("No installed skills found.")
108 return nil
109 }
111 c := o.io.Color()
112 var updateErrs []error
113 for _, ins := range targets {
114 if err := ctx.Err(); err != nil {
115 return errors.Join(append(updateErrs, err)...)
116 }
117 source, err := registry.Get(ins.Name)
118 if err != nil {
119 updateErrs = append(updateErrs, fmt.Errorf("%s: %w", ins.Name, err))
120 continue
121 }
122 if skill.ContentHash(source.Files) == ins.Hash {
123 o.io.LogInfof("%s %s (%s) already up to date.\n", c.GreenCheck(), ins.Name, ins.Scope)
124 continue
125 }
126 if err := writeSkill(ins.Dir, source.Files); err != nil {
127 updateErrs = append(updateErrs, fmt.Errorf("%s: %w", ins.Name, err))
128 continue
129 }
130 o.io.LogInfof("%s Updated %s (%s).\n", c.GreenCheck(), ins.Name, ins.Scope)
131 }
132 return errors.Join(updateErrs...)
135func filterByName(in []installed.Skill, name string) []installed.Skill {
136 out := in[:0:0]
137 for _, s := range in {
138 if s.Name == name {
139 out = append(out, s)
140 }
141 }
142 return out
145// writeSkill replaces skillDir's contents with files. Writes to a sibling
146// temp dir first then renames so a failed write can't leave a partial
147// update, and so files removed upstream don't linger on disk and make
148// the on-disk hash perpetually differ from the source.
149func writeSkill(skillDir string, files map[string][]byte) error {
150 parent := filepath.Dir(skillDir)
151 tmp, err := os.MkdirTemp(parent, ".skill-update-*")
152 if err != nil {
153 return fmt.Errorf("creating temp directory: %w", err)
154 }
155 success := false
156 defer func() {
157 if !success {
158 _ = os.RemoveAll(tmp)
159 }
160 }()
161 for rel, content := range files {
162 destPath := filepath.Join(tmp, filepath.FromSlash(rel))
163 if err := os.MkdirAll(filepath.Dir(destPath), 0o755); err != nil {
164 return fmt.Errorf("creating directory for %s: %w", destPath, err)
165 }
166 if err := os.WriteFile(destPath, content, 0o644); err != nil {
167 return fmt.Errorf("writing %s: %w", destPath, err)
168 }
169 }
170 if err := os.RemoveAll(skillDir); err != nil {
171 return fmt.Errorf("removing old skill directory %s: %w", skillDir, err)
172 }
173 if err := os.Rename(tmp, skillDir); err != nil {
174 return fmt.Errorf("installing updated skill: %w", err)
175 }
176 success = true
177 return nil
Diagnostics8 git calls · 377 ms
CallStatusSizeTime0377 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
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=4
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=44
Open 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=3
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 · hit3 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=1857
git.response.cache=HIT
git.response.duration_ms=3
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:0200370 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=370
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=290ae7a62f76e23cbbd60697570f19b90ddcece6
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=370
git.pkt.payload.max=343
git.sideband.band1.bytes=343
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=343
git.pack.trailer=0795a26d2b25022c69da2ef2e68cfb92a77ef4bb
Open pack map
fetchtree:0200139 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=360
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=978ceb6b3de5a5372a77abc2675f9842fed5bf02
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=d33112e7112d8990c2505d67aa9eb537f8f4c75e
Open pack map
fetch2002.0 KiB360 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2040
git.response.cache=MISS
git.response.duration_ms=360
git.fetch.wants=96e05d9adbabaee334e5a44b16885ed4d88bbecc
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=2040
git.pkt.payload.max=2013
git.sideband.band1.bytes=2013
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
a5e2e07b48b32308
Request
15d19cc0…
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