GitLabgitlab-org
cli
main
1package installed
3import (
4 "errors"
5 "fmt"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "sort"
10 "testing"
12 "gitlab.com/gitlab-org/cli/internal/commands/skills/bundled"
13 "gitlab.com/gitlab-org/cli/internal/commands/skills/remote"
14 "gitlab.com/gitlab-org/cli/internal/commands/skills/skill"
15 "gitlab.com/gitlab-org/cli/internal/git"
16)
18// Duplicated from install.skillsRelDir rather than imported to keep this
19// leaf data package free of a back-reference into the install command.
20var skillsRelDir = filepath.Join(".agents", "skills")
22type Scope string
24const (
25 ScopeProject Scope = "project"
26 ScopeGlobal Scope = "global"
27)
29type Skill struct {
30 Name string
31 Dir string
32 Scope Scope
33 Source skill.Source
34 Files map[string][]byte
35 Hash string
36}
38// Discover walks both well-known skill locations and returns every
39// subdirectory whose name matches a known bundled or remote skill.
40// Missing locations (no git repo, no ~/.agents/skills/) contribute
41// zero entries and are not errors.
42func Discover() ([]Skill, error) {
43 known, err := buildKnownNames()
44 if err != nil {
45 return nil, err
46 }
48 var out []Skill
49 for _, loc := range candidateLocations() {
50 entries, err := readSkillDirs(loc.dir)
51 if err != nil {
52 return nil, err
53 }
54 for _, dirName := range entries {
55 src, ok := known[dirName]
56 if !ok {
57 continue
58 }
59 s, err := readInstalled(loc.dir, dirName, loc.scope, src)
60 if err != nil {
61 return nil, err
62 }
63 out = append(out, s)
64 }
65 }
66 sort.Slice(out, func(i, j int) bool {
67 if out[i].Name != out[j].Name {
68 return out[i].Name < out[j].Name
69 }
70 return out[i].Scope < out[j].Scope
71 })
72 return out, nil
73}
75type location struct {
76 dir string
77 scope Scope
78}
80// Overridable so tests can point at scratch dirs instead of ~/ and the repo root.
81var candidateLocationsFn = defaultCandidateLocations
83func candidateLocations() []location { return candidateLocationsFn() }
85// StubCandidateLocations points Discover at dir for the duration of the test.
86// Exposed for cross-package tests (e.g. the update command).
87func StubCandidateLocations(tb testing.TB, dir string) {
88 tb.Helper()
89 old := candidateLocationsFn
90 candidateLocationsFn = func() []location {
91 return []location{{dir: dir, scope: ScopeProject}}
92 }
93 tb.Cleanup(func() { candidateLocationsFn = old })
94}
96func defaultCandidateLocations() []location {
97 var out []location
98 if root, err := git.ToplevelDir(); err == nil {
99 out = append(out, location{filepath.Join(root, skillsRelDir), ScopeProject})
100 }
101 if home, err := os.UserHomeDir(); err == nil {
102 out = append(out, location{filepath.Join(home, skillsRelDir), ScopeGlobal})
103 }
104 return out
105}
107func readSkillDirs(dir string) ([]string, error) {
108 entries, err := os.ReadDir(dir)
109 if err != nil {
110 if errors.Is(err, fs.ErrNotExist) {
111 return nil, nil
112 }
113 return nil, fmt.Errorf("reading skills directory %s: %w", dir, err)
114 }
115 var names []string
116 for _, e := range entries {
117 if !e.IsDir() {
118 continue
119 }
120 // Reserved namespace for any future glab-managed bookkeeping —
121 // don't classify it as a skill even if it appears.
122 if e.Name() == ".glab" {
123 continue
124 }
125 names = append(names, e.Name())
126 }
127 return names, nil
128}
130func readInstalled(parent, dirName string, scope Scope, source skill.Source) (Skill, error) {
131 skillDir := filepath.Join(parent, dirName)
132 files := map[string][]byte{}
133 err := filepath.WalkDir(skillDir, func(path string, d fs.DirEntry, err error) error {
134 if err != nil {
135 return err
136 }
137 if d.IsDir() {
138 return nil
139 }
140 rel, err := filepath.Rel(skillDir, path)
141 if err != nil {
142 return err
143 }
144 content, err := os.ReadFile(path)
145 if err != nil {
146 return err
147 }
148 // Canonicalize to forward slashes so the on-disk hash matches
149 // what bundled.Get / remote.Get produce (both use embed/path).
150 files[filepath.ToSlash(rel)] = content
151 return nil
152 })
153 if err != nil {
154 return Skill{}, fmt.Errorf("reading installed skill %s: %w", skillDir, err)
155 }
156 return Skill{
157 Name: dirName,
158 Dir: skillDir,
159 Scope: scope,
160 Source: source,
161 Files: files,
162 Hash: skill.ContentHash(files),
163 }, nil
164}
166// buildKnownNames returns name → source for every skill glab knows
167// how to install. Used to skip user-authored directories that happen
168// to live in .agents/skills/ but don't correspond to anything we ship.
169func buildKnownNames() (map[string]skill.Source, error) {
170 out := map[string]skill.Source{}
171 bs, err := bundled.All()
172 if err != nil {
173 return nil, fmt.Errorf("loading bundled skills: %w", err)
174 }
175 for _, s := range bs {
176 out[s.Name] = skill.SourceBundled
177 }
178 rs, err := remote.All()
179 if err != nil {
180 return nil, fmt.Errorf("loading remote skills: %w", err)
181 }
182 for _, s := range rs {
183 // Bundled wins if a name somehow appears in both — bundled is
184 // the cheaper source to verify against.
185 if _, exists := out[s.Name]; !exists {
186 out[s.Name] = skill.SourceRemote
187 }
188 }
189 return out, nil
190}
Diagnostics8 git calls · 317 ms
CallStatusSizeTime0317 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 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
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=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
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=3 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:0200370 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=370 git.response.cache=HIT git.response.duration_ms=3 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=0795a26d2b25022c69da2ef2e68cfb92a77ef4bbOpen pack map
fetchtree:0200142 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=304 git.response.bytes=142 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=33a5560ee59c30509045038242d8fca78f7aaadf 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=142 git.pkt.payload.max=115 git.sideband.band1.bytes=115 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=115 git.pack.trailer=d610055c1027ea34a793c846e9b4688be22ac34aOpen pack map
fetch2001.9 KiB304 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1932 git.response.cache=MISS git.response.duration_ms=304 git.fetch.wants=f32705d2765bd64be5b4ff953bdcd10f2ccbba47 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=1932 git.pkt.payload.max=1905 git.sideband.band1.bytes=1905 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
- a5e2cc17a90b7929
- Request
- 60c021cf…
- 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