TestPluginToldWhereTheAgentWorks was red on any machine with an empty .git above its temp folder. GitOnDisk took the first .git it met going up as a repository, and on this box /tmp/.git is an empty folder (owned by nobody), so every folder under /tmp read "yes". git passes such a folder over and goes on up: git rev-parse under it says "not a git repository". GitOnDisk now reads each .git on the way as git's own search does (repo_discovery_find_dir in git 2.56's setup.c), through GitAt: - A folder counts when git takes it for a repository (is_git_directory): a HEAD git takes (a link into refs/, "ref: refs/...", or a commit's id), and objects and refs folders, in the folder its commondir names for a linked worktree. Any other folder, an empty one say, is passed over and the walk goes on up. - A file counts when it is "gitdir: <folder>" naming such a folder, relative to the .git's own folder (a linked worktree's, a submodule's). Any other file, one over git's 1 MB, or one that is neither file nor folder is "no": git stops there with "not a git repository" (a worktree whose repository has gone) or "invalid gitfile format". (git 2.34 passed a .git that is neither file nor folder over; 2.55 and 2.56, CI's, stop at it.) - A .git it can't read is unknown, and the header is left off. git passes such a folder over and stops at such a file, but either way it can't tell an empty .git from a repository it isn't let into (another user's, through a shared magpie), and a failed read is unknown here. A bare repository kept as a folder's .git (git init --bare .git) is still "yes": git finds one there, though no work tree. Not mirrored: the filesystem boundary, GIT_CEILING_DIRECTORIES and safe.directory's ownership check. Cost: the same one stat per folder on the way up; at a .git, a read of HEAD (255 bytes at most) and stats of commondir, objects and refs. What a user sees: a request to a plugin provider from an agent that doesn't say whether its folder is a repository (Codex, Pi) was told "yes" for a project under a stray empty .git, under a worktree whose repository was deleted, or under a .git file that names nothing. The community ZCode plugin then tells the model "- <git label>: yes" and Command Code's sends isGitRepo: true, so the model may run git commands that fail with "not a git repository". They are now told "no", and with a .git on the way magpie can't read, nothing (ZCode's plugin leaves the line out, Command Code's sends false). Claude Code's and OpenCode's requests say it themselves and are passed on as they say it, so nothing changes for them; Claude Code 2.1.310's own check takes any .git, an empty one too. The only caller of GitOnDisk is tellWorkPlace (workdir.go), which needs no change. The library's skill groups had the same first-.git walk: gitRepoOf took the first .git within four folders of a linked skill for its checkout, so an empty one between a skill and its clone left the skill in no group, and gave one imported from there no From. gitRepoOf now asks provider.GitAt, passes such a .git over to the clone's, and reads the origin from the folder GitAt names (a linked worktree's common one), which replaces gitdirConfig. gitdirConfig also took "gitdir:" with no space after it, or with spaces around the line, which git doesn't take; such a .git now traces to nothing, as git finds nothing there. Checkouts git made are traced as before; the test's fake ones had only a config and now have a HEAD, objects and refs. Every other .git folder in the Go code is one a walk skips when it copies, hashes or checks a skill (carry.go, skillcheck.go, skills.go, skill_edits.go), or the one magpie removes from its own fetch of a skill (skillgit.go), not a decision about a repository. The Claude bridge's claudeWorkDir takes the temp folder for one in no repository; Claude Code decides that for itself, and its git status is off for every run (CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS), so it stays. TestPluginToldWhereTheAgentWorks gets a real repository for "in a repository" (HEAD, objects, refs; an empty .git was all it had) and a case under an empty .git in its own temp folder, so it fails without this on any machine. Its "not in one" cases, and the new provider tests' "no" ones, still need no repository above the temp folder, as the test did before. TestGitOnDiskIsWhatGitFinds lays out each kind of .git by hand; TestGitOnDiskAgreesWithGit asks git rev-parse --git-common-dir in each of those folders and in ones git made (init, worktree add, a worktree whose record was removed, --separate-git-dir, init --bare .git), for yes or no and where; TestGitOnDiskUnreadIsUnknown locks a .git folder and a worktree's .git file. provider-plugins.md and library.md say the same. What was run (tree 9a4504d9, on origin/main56602426, go1.26.8, linux/amd64, Linux 5.15), outside graftcp, umask 077, git 2.34.1, with the box's empty /tmp/.git above the tests' temp folders: - TestPluginToldWhereTheAgentWorks -count=5, Bun 1.4.2: 5 of 5 ok. TestGitOnDisk* and TestSkillRepo*, -count=3: 15 of 15 ok. The same and TestPluginToldWhereTheAgentWorks, -race -count=2: 12 of 12, no race. internal/library and internal/provider whole: ok. - 56602426's workplace.go and skill_from.go put back under the new tests: TestPluginToldWhereTheAgentWorks red 5 of 5, at plugin_workdir_test.go:125 both "not in one" and "not in one, under an empty .git": the plugin was told git "yes", want "no"; TestSkillRepoTraced red, "write-a-prd: repo "", want "mattpocock/skills"". - gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go vet for windows, go build -tags nogui for linux and darwin and go build for windows: exit 0 each. go vet and go build without the tag on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here). - go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2, log kept; 8m37s, load 32 at its start): 73 packages ok, and 14 tests red in 4 packages, none of them this commit's tests or what they cover. internal/gateway ran to its end (504s), so TestPluginToldWhereTheAgentWorks passed in it; internal/library and internal/provider ok. The reds: agent TestCodexDriftReached and TestCodexBehindReverseProxy (#3398); TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141); TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout (#3641: go build's own VCS stamping meets the same /tmp/.git, which this doesn't change); edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077, #3640); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649). This commit is 595b361c rebased: the same internal/ patch-id, and the same row in docs/subsystems/library.md, put in by git's 3-way merge, as main's33ccc618(#2794) rewrote the Skills row, a line of its context three rows above. The rest is what was run for 595b361c, at tree d56bed31 on origin/main098ac969, the same code (not this tree): - main's GitOnDisk and gitRepoOf under the new tests (098ac969's workplace.go and skill_from.go put back): TestPluginToldWhereTheAgentWorks -count=5, Bun 1.4.2: red 5 of 5, "not in one" and "not in one, under an empty .git" told git "yes". With TMPDIR=/var/tmp/3675-clean, no .git above it: red 5 of 5 on the new case alone. TestSkillRepoTraced: red, write-a-prd: repo "", want "mattpocock/skills". - Broken by hand, one at a time in workplace.go, each red in the GitOnDisk tests on what it breaks, and green again restored: any .git folder taken, main's rule for folders: "yes" under an empty .git, a zeroed HEAD and a HEAD alone, and for a locked .git a gitfile naming no repository, or not a gitfile, walked past: "yes" from the repository above it HEAD's content not read: "yes" for the zeroed HEAD; objects and refs not looked for: "yes" for a HEAD alone a HEAD at a commit not taken, or commondir not read: "no" for both linked worktrees; a worktree's own folder named, not its common one: the repository at .../worktrees/wt where git finds .../.git a HEAD linked into refs/ refused: "no"; a relative gitdir read from magpie's folder: "no" for the submodule; no 1 MB cap: "yes" ENOTDIR taken for a failed read: unknown for a gitfile naming a file; a .git it can't read walked past: "no" for both locked ones Not tried: CI's git 2.55.0 (ubuntu) and 2.56.0 (macOS, Windows) under TestGitOnDiskAgreesWithGit, which met git 2.34.1 alone; macOS and Windows at all (Windows CI only compiles the tests); a real ZCode or Command Code plugin end to end; a shared magpie told another user's folder; the Library page itself. Landed by yetone in a batch of Coffemon's patches from the issues. What was run, go1.26.8, on origin/maincc81ad4ea, then again at70a0c387c(gofmt, the three vets, the changed tests, umask 077): - mote (macOS 27): gofmt -l clean; go vet, GOOS=linux go vet -tags nogui and GOOS=windows go vet ./...: ok. edit and testenv at umask 077; agent, provider, library and gateway with an empty .git above TMPDIR; davsync, imagemcp, plugin, library, edit, testenv whole; the changed tests in agent, provider, gateway and gui; the timing tests -count=10 (gateway, davsync, imagemcp) and -count=5 (agent): all ok. TestCoreScriptBudget ok: core 1,536,751 of 1,537,034, app.js 1,312,975 of 1,315,316, no budget raised. - Red on the old code: on origin/main, TestWriteAtomicReadOnlyHardLink at umask 077 and two Reasonix tests under an empty .git; on Linux (wsl-run) TestProgram at umask 077 with the old program.go. With each fix's code taken back to main's, the Gemini imageConfig tests, TestPluginToldWhereTheAgentWorks, TestSkillRepoTraced, TestTaggedHooksAlone, TestMainURL/TestMainView and update-auto, plugin-github-hooks, plugins-more-discover, plugin-namesake were red. Broken by hand: claudeKeyed always true (both OwnEndpointNeverAsked tests red), claudeAsk a no-op (TestClaudeFirstWriteAsksTheProxy red), GitAt taking any .git folder (the three TestGitOnDisk tests red). - GUI: 123 files (plugins*, update-*, panel/tray/start/context, i18n, gui-zh-tw/ja/de and every file that evals app.js parts) in WebKit on Linux (wsl-run). 11 files had reds (click-scroll, panel-fold, plugin-card-update, plugin-github, plugin-move*, segs-glide, tray-totals, update-*), and run again side by side they fail the same on origin/maind80d75f30(79 pass, 26 fail here; 78 and 27 there). They are click-scroll guards ("the click must not scroll the page"), varying by language from run to run (#4385). copilot-ghe's route-after-close red is #1949, seen on main244d80e23too. In Chromium on mote, Chromium segfaulted partway through files (#4125): context-map, context-scroll-end and context-tune fail the same on origin/main0911a65e7, so the batch's own files (plugin-namesake, update-auto, plugins-more-discover, plugin-github-hooks) ran in Chromium on Linux: 62 of 62. - Full suite (go test -tags nogui -count=1 -timeout 45m ./..., mote) at 472a0608f (on70a0c387c): every package ok but internal/gateway, whose five reds were #4362's (Antigravity turned-away and TestCodexSpentGrokFailingIsTold), fixed on main ind80d75f30. After the rebase on244d80e23the five pass, and gofmt, the three vets and the targeted runs above were run again: ok. In Linux the sign-in, copilot, plugin-github*, plugins-more-discover, plugin-namesake, plugin-gateway, update-auto, quota-refresh-order and i18n files ran in both engines: 475 pass, 4 fail (copilot-ghe #1949, plugin-github's click scroll, both red on main too). - Rebased again onc7171a256, over its budget fixe94bf7a08: gofmt, vet of gui, gateway, provider and plugin, TestCoreScriptBudget (core 1,536,265 of 1,536,586 with the batch's last commit, statusTErr and settingsErr), the Gemini, GitOnDisk and plugin tests, and in Linux both engines i18n-keys, update-mirror, plugin-namesake, plugins-more-discover, plugin-github-hooks and update-auto (138 of 138): ok. Not tried: the whole GUI suite (mote's WebKit wouldn't launch and its Chromium segfaulted, #4125); macOS WebKit; the real app's window.
367 lines
12 KiB
Go
367 lines
12 KiB
Go
package library
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Where a skill came from, for the page to group it with the others from
|
|
// the same repository (White Immortal on Discord: the skills of
|
|
// github.com/mattpocock/skills together, however each came in). One
|
|
// installed from GitHub says so itself; for the others it is traced:
|
|
// CC Switch's record, the skills CLI's lock (npx skills add, in
|
|
// ~/.agents/.skill-lock.json or $XDG_STATE_HOME/skills), the git checkout a linked folder is in,
|
|
// or the repository the user installed from since that has the very same
|
|
// skill (From). It only groups: such a skill is not updated from there,
|
|
// unless the library's own copy is the repository's to the file, which
|
|
// then is the repository's skill (noteFrom).
|
|
|
|
// repoOf is the repository a library skill came from: owner/repo on
|
|
// GitHub, host/path elsewhere, "" when it can't be told.
|
|
func (tr *tracer) repoOf(s *Skill) string {
|
|
if s.Source != nil && s.Source.Kind == "github" {
|
|
return s.Source.Repo
|
|
}
|
|
if s.Source != nil && s.Source.Kind == "git" {
|
|
return remoteRepo(s.Source.Repo) // host/path, as a checkout of it is
|
|
}
|
|
if o, ok := ccSwitchOrigin(s); ok {
|
|
return o.Repo
|
|
}
|
|
if s.From != "" {
|
|
return fromKey(s.From)
|
|
}
|
|
dir := skillDir(s.Name)
|
|
if !linked(dir) {
|
|
// the library's own copy: the skills CLI's, moved in, by its name
|
|
if e, ok := tr.lock()[s.Name]; ok && (s.Source == nil || within(realDir(s.Source.Dir), realDir(sharedSkillsDir()))) {
|
|
return e
|
|
}
|
|
return ""
|
|
}
|
|
return tr.folder(realDir(dir))
|
|
}
|
|
|
|
// folder is the repository a skill's folder elsewhere came from: the
|
|
// skills CLI's lock for one in ~/.agents/skills, else its git checkout.
|
|
func (tr *tracer) folder(dir string) string {
|
|
if within(dir, realDir(sharedSkillsDir())) {
|
|
if e, ok := tr.lock()[filepath.Base(dir)]; ok {
|
|
return e
|
|
}
|
|
}
|
|
return gitRepoOf(dir)
|
|
}
|
|
|
|
// tracer reads what tracing needs once for a whole page.
|
|
type tracer struct {
|
|
locked map[string]string
|
|
read bool
|
|
}
|
|
|
|
// lock is the skills CLI's lock: each skill's repository by its name.
|
|
// The CLI keeps it in ~/.agents, or under $XDG_STATE_HOME when that is set
|
|
// (skills 1.7: $XDG_STATE_HOME/skills/.skill-lock.json, the skills still
|
|
// in ~/.agents/skills). Both are read, the XDG one's entries winning: one
|
|
// written before the variable was set names skills the newer doesn't.
|
|
func (tr *tracer) lock() map[string]string {
|
|
if !tr.read {
|
|
tr.read, tr.locked = true, readSkillLock(filepath.Join(home(), ".agents", ".skill-lock.json"))
|
|
if x := appdir.Getenv("XDG_STATE_HOME"); x != "" {
|
|
for name, r := range readSkillLock(filepath.Join(x, "skills", ".skill-lock.json")) {
|
|
if tr.locked == nil {
|
|
tr.locked = map[string]string{}
|
|
}
|
|
tr.locked[name] = r
|
|
}
|
|
}
|
|
}
|
|
return tr.locked
|
|
}
|
|
|
|
func readSkillLock(path string) map[string]string {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var f struct {
|
|
Skills map[string]struct {
|
|
Source string `json:"source"`
|
|
SourceType string `json:"sourceType"`
|
|
SourceURL string `json:"sourceUrl"`
|
|
SourceBaseURL string `json:"sourceBaseUrl"`
|
|
} `json:"skills"`
|
|
}
|
|
if json.Unmarshal(b, &f) != nil {
|
|
return nil
|
|
}
|
|
out := map[string]string{}
|
|
for name, e := range f.Skills {
|
|
r := ""
|
|
if e.SourceType == "github" && repoRe.MatchString(e.Source) {
|
|
r = strings.TrimSuffix(e.Source, ".git")
|
|
} else if e.SourceType == "well-known" {
|
|
r = wellKnownBase(e.SourceBaseURL, e.SourceURL)
|
|
} else if e.SourceURL != "" {
|
|
r = remoteRepo(e.SourceURL)
|
|
}
|
|
if r != "" {
|
|
out[name] = r
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// wellKnownBase is where a kit of skills a site serves at its
|
|
// /.well-known/agent-skills/ came from (#1862, WardLu): the lock's
|
|
// sourceBaseUrl, as host/path, so Feishu's lark-base, lark-doc and
|
|
// lark-sheets are one group and Fish Audio's two another, while two kits
|
|
// under one host with different base URLs stay apart. Each skill's
|
|
// sourceUrl is its own file there, so it isn't the group. A lock written
|
|
// before sourceBaseUrl has the base in the sourceUrl, before its
|
|
// /.well-known/; one with neither is grouped by its sourceUrl as before.
|
|
func wellKnownBase(base, src string) string {
|
|
if b := siteOf(base); b != "" {
|
|
return b
|
|
}
|
|
if i := strings.Index(src, "/.well-known/"); i > 0 {
|
|
if b := siteOf(src[:i]); b != "" {
|
|
return b
|
|
}
|
|
}
|
|
return remoteRepo(src)
|
|
}
|
|
|
|
// fromKey is the group a skill's From names. A skill brought in from
|
|
// ~/.agents/skills keeps the lock's key as it was then (ImportSkills), and
|
|
// before well-known kits were grouped by their base that was each skill's
|
|
// own sourceUrl, "docs.fish.audio/.well-known/agent-skills/fish-audio-api/
|
|
// skill.md": read as the base before /.well-known/, so a kit brought in
|
|
// then is one group as the lock now says (#1862, WardLu on v0.1.1223).
|
|
func fromKey(from string) string {
|
|
if before, _, ok := strings.Cut(from, "/.well-known/"); ok && before != "" {
|
|
return before
|
|
}
|
|
return from
|
|
}
|
|
|
|
// siteOf reads a web address as host/path, or the host alone, with no
|
|
// user, query or trailing slash: "" when it isn't an http(s) address.
|
|
func siteOf(s string) string {
|
|
u, err := url.Parse(strings.TrimSpace(s))
|
|
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Hostname() == "" {
|
|
return ""
|
|
}
|
|
host := strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.")
|
|
p := strings.Trim(u.Path, "/")
|
|
if before, _, ok := strings.Cut("/"+p+"/", "/.well-known/"); ok {
|
|
p = strings.Trim(before, "/")
|
|
}
|
|
if strings.Contains(p, "..") {
|
|
return ""
|
|
}
|
|
if p == "" {
|
|
return host
|
|
}
|
|
return host + "/" + p
|
|
}
|
|
|
|
// gitRepoOf is the repository of the git checkout a skill's folder is, or
|
|
// is in a few folders down: its remote origin. The checkout is the one git
|
|
// finds (provider.GitAt): a .git git passes over, an empty one say, is
|
|
// passed over here too (#3675), and its config is in the repository's
|
|
// folder, the common one of a linked worktree. A checkout that is the home
|
|
// folder, or a folder of an app's own in it (~/.claude kept in git), is
|
|
// the user's dotfiles rather than where the skill came from.
|
|
func gitRepoOf(dir string) string {
|
|
h := realDir(home())
|
|
for i := 0; i < 4 && dir != "" && dir != h; i++ {
|
|
if p := filepath.Dir(dir); p == h && strings.HasPrefix(filepath.Base(dir), ".") {
|
|
return ""
|
|
}
|
|
switch git, common, err := provider.GitAt(filepath.Join(dir, ".git")); {
|
|
case err != nil || git == "no":
|
|
return ""
|
|
case git == "yes":
|
|
return originURL(filepath.Join(common, "config"))
|
|
}
|
|
next := filepath.Dir(dir)
|
|
if next == dir {
|
|
break
|
|
}
|
|
dir = next
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var originSection = regexp.MustCompile(`^\[\s*remote\s+"origin"\s*\]$`)
|
|
|
|
// originURL is the repository remote "origin" of a git config fetches.
|
|
func originURL(cfg string) string {
|
|
b, err := os.ReadFile(cfg)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
in := false
|
|
for _, line := range bytes.Split(b, []byte("\n")) {
|
|
l := strings.TrimSpace(string(line))
|
|
if strings.HasPrefix(l, "[") {
|
|
in = originSection.MatchString(l)
|
|
continue
|
|
}
|
|
if k, v, ok := strings.Cut(l, "="); in && ok && strings.TrimSpace(k) == "url" {
|
|
return remoteRepo(strings.TrimSpace(v))
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var scpRemote = regexp.MustCompile(`^(?:[\w.-]+@)?([\w.-]+\.[\w.-]+):(.+)$`)
|
|
|
|
// remoteRepo reads a git remote's address as a repository: owner/repo on
|
|
// GitHub, host/path elsewhere. A user or a token in it is never kept.
|
|
func remoteRepo(s string) string {
|
|
s = strings.TrimSpace(s)
|
|
host, p := "", ""
|
|
if u, err := url.Parse(s); err == nil && u.Scheme != "" && u.Host != "" {
|
|
host, p = u.Hostname(), u.Path
|
|
} else if m := scpRemote.FindStringSubmatch(s); m != nil {
|
|
host, p = m[1], m[2]
|
|
} else if repoRe.MatchString(s) {
|
|
host, p = "github.com", s
|
|
} else {
|
|
return ""
|
|
}
|
|
host = strings.TrimPrefix(strings.ToLower(host), "www.")
|
|
p = strings.TrimSuffix(strings.Trim(p, "/"), ".git")
|
|
if strings.Count(p, "/") < 1 || strings.Contains(p, "..") {
|
|
return ""
|
|
}
|
|
if host == "github.com" {
|
|
parts := strings.Split(p, "/")
|
|
return parts[0] + "/" + parts[1]
|
|
}
|
|
return host + "/" + p
|
|
}
|
|
|
|
// noteFrom writes down, for the skills the library has already that a
|
|
// repository being installed from has too, that they came from there (and
|
|
// binds a copy that is the repository's whole to it, to update from):
|
|
// one whose SKILL.md is that repository's very own, and one traced to
|
|
// nowhere by the same name when the library has others from there. A
|
|
// pack added a while ago is mostly older copies (White Immortal on
|
|
// Discord: 20 of mattpocock/skills' 38 grouped, the rest edited there
|
|
// since), and a name the repository has beside others the user took
|
|
// from it is that skill. root is where the repository was fetched to. It
|
|
// gives how many it noted.
|
|
func noteFrom(src Source, root string, cands []Candidate) int {
|
|
if src.Kind != "github" {
|
|
return 0
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
l, err := load()
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
tr := &tracer{}
|
|
have := slices.ContainsFunc(l.Skills, func(s *Skill) bool { return strings.EqualFold(tr.repoOf(s), src.Repo) })
|
|
noted, bound := 0, 0
|
|
var same []*Skill
|
|
for _, c := range cands {
|
|
s := l.skill(c.Name)
|
|
if s == nil || (s.Source != nil && s.Source.Kind == "github") {
|
|
continue
|
|
}
|
|
grouped := strings.EqualFold(s.From, src.Repo)
|
|
mine, err1 := os.ReadFile(filepath.Join(skillDir(s.Name), "SKILL.md"))
|
|
theirs, err2 := os.ReadFile(filepath.Join(root, filepath.FromSlash(c.Path), "SKILL.md"))
|
|
if err1 != nil || err2 != nil {
|
|
continue
|
|
}
|
|
if bytes.Equal(bytes.TrimSpace(mine), bytes.TrimSpace(theirs)) {
|
|
if !grouped {
|
|
s.From, have = src.Repo, true
|
|
noted++
|
|
}
|
|
// the library's own copy, every file of it the repository's:
|
|
// it is that skill, and updates from there as one installed
|
|
// from it does (RockyShowlle on X). One linked to a folder
|
|
// elsewhere is kept by whatever put it there (npx skills, a
|
|
// git checkout), and an edited one stays only grouped.
|
|
if dir := skillDir(s.Name); s.Source == nil && !linked(dir) && sameTree(dir, filepath.Join(root, filepath.FromSlash(c.Path))) {
|
|
s.Source = &Source{Kind: "github", Repo: src.Repo, Ref: src.Ref, Path: c.Path}
|
|
s.Hash, s.Own, s.Commit, s.From = hashDir(dir), ownHash(dir), "", ""
|
|
l.seenAt(src.Repo, src.Ref, c.Path)
|
|
forgetCheck(s.Name)
|
|
bound++
|
|
}
|
|
} else if !grouped && tr.repoOf(s) == "" {
|
|
same = append(same, s)
|
|
}
|
|
}
|
|
if have {
|
|
for _, s := range same {
|
|
s.From = src.Repo
|
|
noted++
|
|
}
|
|
}
|
|
if noted > 0 || bound > 0 {
|
|
l.save()
|
|
}
|
|
return noted
|
|
}
|
|
|
|
// RegroupSkills traces the library's skills again (the Library page's
|
|
// Group again): each GitHub repository a skill is traced to is fetched,
|
|
// and the skills it has that aren't grouped with it yet are noted as from
|
|
// there, as installing from it again would. It gives how many were
|
|
// grouped; an error only when no repository could be fetched.
|
|
func RegroupSkills() (int, error) {
|
|
l, err := load()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
tr := &tracer{}
|
|
var repos []string
|
|
seen := map[string]bool{}
|
|
for _, s := range l.Skills {
|
|
r := tr.repoOf(s)
|
|
// a site's kit (docs.example.com/kit) reads like owner/repo, but
|
|
// a GitHub owner has no dot
|
|
if !repoRe.MatchString(r) || strings.Contains(strings.Split(r, "/")[0], ".") || seen[strings.ToLower(r)] {
|
|
continue
|
|
}
|
|
seen[strings.ToLower(r)] = true
|
|
repos = append(repos, r)
|
|
}
|
|
noted, fetched := 0, 0
|
|
var first error
|
|
for _, r := range repos {
|
|
src := Source{Kind: "github", Repo: r}
|
|
root, err := fetch(src)
|
|
if err != nil {
|
|
if first == nil {
|
|
first = err
|
|
}
|
|
continue
|
|
}
|
|
fetched++
|
|
noted += noteFrom(src, root, candidates(root, "", srcFolder(src)))
|
|
os.RemoveAll(root)
|
|
}
|
|
if fetched == 0 && first != nil {
|
|
return 0, first
|
|
}
|
|
return noted, nil
|
|
}
|