スライム on Discord (#help, 10-22): taste-skills took some of its skills
out, Update all reported each of them as a failure, and the only way
out was to delete them one by one.
An update or a check that reads the source whole and finds the skill's
folder (or its SKILL.md) gone now calls it removed upstream, not a
failure. GitHub tarballs and other git repositories both work this way.
- UpdateSkill returns *GoneError.
- UpdateSkills and UpdateSomeSkills list it in Result.Gone, not
Unupdated.
- A check gives status "gone".
- Each marks Skill.Gone. The mark is cleared when a check or an update
finds the skill there again.
- A fetch that failed, a skill left out of a fetch as too big, and a
folder that can't be read stay unknown, never gone (goneFrom).
Nothing is deleted by itself. RemoveGoneSkills takes the named skills
out, or every marked one when none are named, through RemoveSkills, so
the folders go to the backups. A skill found in its source again is
kept and said so.
- GUI: a "Removed upstream" tag replaces Update available and the
update icon. Update all says gone skills apart from failures. "Clean
up N" sits in the group heading, or in the bar when there is one
group, and opens a sheet naming each skill. One changed here carries
"Changed here". Strings are in en, zh, zh-TW, ja and de.
- CLI: `magpie library skill rm --gone [--yes]` asks at a terminal and
refuses without --yes otherwise. It names the ones changed here.
update and status print the hint.
- TUI: the row says "removed from <repo> · d takes it out".
- API: POST /api/library/skills/remove-gone {names}.
- Docs: the Skill updates row and Verification in
docs/subsystems/library.md.
Record for this push: 13 commits on origin/main ca4a1cde0, head tree
539c84e92. Go 1.26.8, sandbox HOME, GOMODCACHE/GOCACHE pinned. The full Go
run and vet were at tree d6393dfc8, the same Go code as this head: only
app.js comments (cut under the core script budget) and one ja string
changed after it.
- gofmt -l: nothing. go build ./...; go vet ./... (darwin), GOOS=linux
go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -timeout 45m ./..., through tee, one suite on the machine:
68 packages ok. The one red was internal/gui's
TestCoreScriptBudget: app.js 1,335,314 > 1,335,000, since main grew
app.js under the stack. The autoscroll commit's own comments were cut
(to 1,334,629) rather than the budget raised, and go test -count=1
./internal/gui is ok at this head (113s). Packages the run took from
Go's test cache had the same inputs.
- GUI: the 18 files this stack touches or names, both engines,
202 of 204 pass (tree 068acf128, this push less the last commit). The
two reds: gui-ja, red on main since c2636b7b5 and fixed by this push's
last commit (gui-ja and i18n-keys 6 of 6 there), and usage-fold's
chromium "several accounts" (#2163, red on main the same way).
- skill-gone passes, chromium and webkit, en and zh, 860 and 400px.
- internal/library, internal/cli and internal/tui ok, the Gone tests and
TestLibrarySkillRm among them.
- The rebase onto main met c2636b7b5's Verification line in library.md;
both sides' tests are kept in it.
The whole GUI suite ran at 1c1fc7183 (this stack's first 12 commits on
0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run
alone at the stack and at origin/main 8278fd9b0. Eight are red the same
way on main: plugin-card-update (#3079), quota-count-wrap and
quota-name-translated (#2228), s3-sync (#2448), usage-agents-hide
(#2276), usage-checkin (#3091), usage-fold's chromium "several
accounts" (#2163), usage-ledger (#3104). The other 13 pass alone on both
and were red only under that run's load (50 to 335): currency #3101,
plugin-github #3218, provider-fetch-again #3292, rest-try-now #2564,
routing-auto-reset #3293, routing-keys-fold #1612, routing-steady #1804,
routing-wb-restricted #1905, sessions-folder-delete #2218,
sessions-manage #3294, switch-only #3295, text-size #1866, usage-pack
#3296.
Broken by hand, each red, then restored:
- prepare not returning GoneError
- apply not clearing Gone
- leftOutAt ignored
- every Stat error read as absent
- the git check not saying gone
- RemoveGoneSkills taking a skill that isn't marked
Not tried:
- the real taste-skills repo over the network
- the Windows box
- a skill CC Switch installed (Origin, adopt path), which still reports
as a failure
- a GitHub check with a known commit, which keeps an existing gone
mark but only marks gone after an update
465 lines
17 KiB
Go
465 lines
17 KiB
Go
package library
|
|
|
|
// Skills from any git repository (sunset on Discord): GitLab, a company's
|
|
// own GitLab on its intranet, Gitea, a bare repository on this machine.
|
|
// magpie can't ask such a host for a tarball or its commits the way it
|
|
// asks GitHub, so it runs the user's own git, which signs in to the host
|
|
// the way it does for the user (a credential helper, a token, an SSH key
|
|
// in the agent). The address is data: it reaches git as an argument after
|
|
// "--", never through a shell, and what git can't be told apart from an
|
|
// option, or a transport that runs a command, is refused before git runs.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
"unicode"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// gitPath is where git is, "" when it isn't here; a variable for tests.
|
|
var gitPath = func() string { return proc.FindTool("git") }
|
|
|
|
// gitTimeout is how long one git command may take before it is stopped:
|
|
// a host that never answers, or a sign-in no one can see, ends there.
|
|
var gitTimeout = 3 * time.Minute
|
|
|
|
var (
|
|
// remoteHelper is git's <transport>::<address>, which hands the
|
|
// address to a helper program (ext:: runs a command)
|
|
remoteHelper = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9+.-]*::`)
|
|
// scpLike is git's user@host:path for SSH
|
|
scpLike = regexp.MustCompile(`^(?:([\w.-]+)@)?([\w.-]+):(.+)$`)
|
|
gitRef = regexp.MustCompile(`^[\w./+@-]+$`)
|
|
hexRef = regexp.MustCompile(`^[0-9a-fA-F]{7,40}$`)
|
|
)
|
|
|
|
// gitSchemes are the URL schemes a git source may have. file:// is a
|
|
// repository on this machine or a mounted share, which the user named.
|
|
var gitSchemes = map[string]bool{"https": true, "http": true, "ssh": true, "git": true, "git+ssh": true, "ssh+git": true, "file": true}
|
|
|
|
// gitSource reads a git repository out of what the user typed: a URL
|
|
// (https://host/group/sub/repo(.git), ssh://, git://, file://), SSH's
|
|
// git@host:group/repo.git, or host/group/repo. A GitLab page's
|
|
// /-/tree/<ref>/<path> (or /-/blob/…/SKILL.md), and Gitea's
|
|
// /src/branch/<ref>/<path>, name a branch and a folder in it; the ref is
|
|
// the first part after them, so a branch with a / in its name is read
|
|
// as a ref and a folder. ok is false for what isn't meant as one; an
|
|
// error is one that is, refused.
|
|
func gitSource(s string) (src Source, ok bool, err error) {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return Source{}, false, nil
|
|
}
|
|
if strings.HasPrefix(s, "-") {
|
|
return Source{}, true, fmt.Errorf("a repository's address can't start with -")
|
|
}
|
|
if remoteHelper.MatchString(s) {
|
|
return Source{}, true, fmt.Errorf("magpie doesn't install from a git address with a transport (%s): give the repository's https or SSH address", s[:strings.Index(s, "::")+2])
|
|
}
|
|
if strings.IndexFunc(s, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }) >= 0 {
|
|
return Source{}, false, nil
|
|
}
|
|
repo, rest := s, ""
|
|
for _, sep := range []string{"/-/tree/", "/-/blob/", "/src/branch/", "/src/tag/", "/src/commit/"} {
|
|
if i := strings.Index(s, sep); i > 0 {
|
|
repo, rest = s[:i], s[i+len(sep):]
|
|
break
|
|
}
|
|
}
|
|
repo = strings.TrimSuffix(repo, "/")
|
|
if strings.HasSuffix(repo, "/-") { // https://host/group/repo/-/ alone
|
|
repo = strings.TrimSuffix(repo, "/-")
|
|
}
|
|
if !strings.Contains(repo, "://") {
|
|
// user@host:path, or host.name:path; a Windows drive (C:) is a
|
|
// folder, never a host
|
|
if m := scpLike.FindStringSubmatch(repo); m != nil && len(m[2]) > 1 && (m[1] != "" || strings.Contains(m[2], ".")) {
|
|
if strings.HasPrefix(m[3], "-") || strings.HasPrefix(m[2], "-") {
|
|
return Source{}, true, fmt.Errorf("a repository's address can't start with -")
|
|
}
|
|
} else if first, _, _ := strings.Cut(repo, "/"); strings.Contains(first, ".") && strings.Count(repo, "/") >= 2 {
|
|
repo = "https://" + repo // gitlab.example.com/group/repo
|
|
} else {
|
|
return Source{}, false, nil
|
|
}
|
|
}
|
|
if strings.Contains(repo, "://") {
|
|
u, err := url.Parse(repo)
|
|
if err != nil || u.Scheme == "" {
|
|
return Source{}, false, nil
|
|
}
|
|
if !gitSchemes[strings.ToLower(u.Scheme)] {
|
|
return Source{}, true, fmt.Errorf("magpie doesn't install from a %s:// address: give the repository's https or SSH address", u.Scheme)
|
|
}
|
|
if _, set := u.User.Password(); set {
|
|
// it would be kept in library.json, shown on the page and
|
|
// carried to a backup
|
|
return Source{}, true, fmt.Errorf("leave the password or token out of the address: magpie runs your own git, which signs in with its credential helper or your SSH key")
|
|
}
|
|
if strings.ToLower(u.Scheme) != "file" && (u.Host == "" || strings.HasPrefix(u.Host, "-")) {
|
|
return Source{}, true, fmt.Errorf("%s names no host", repo)
|
|
}
|
|
if strings.Trim(u.Path, "/") == "" {
|
|
return Source{}, true, fmt.Errorf("%s names no repository", repo)
|
|
}
|
|
}
|
|
src = Source{Kind: "git", Repo: repo}
|
|
if rest != "" {
|
|
ref, path, _ := strings.Cut(strings.Trim(rest, "/"), "/")
|
|
if ref != "HEAD" {
|
|
src.Ref = ref
|
|
}
|
|
if q := strings.IndexAny(path, "?#"); q >= 0 {
|
|
path = path[:q]
|
|
}
|
|
src.Path = strings.Trim(strings.TrimSuffix(strings.TrimSuffix(path, "SKILL.md"), "/"), "/")
|
|
if src.Ref != "" && (!gitRef.MatchString(src.Ref) || strings.HasPrefix(src.Ref, "-") || strings.Contains(src.Ref, "..")) {
|
|
return Source{}, true, fmt.Errorf("%q isn't a branch or tag git can fetch", src.Ref)
|
|
}
|
|
if strings.Contains("/"+src.Path+"/", "/../") {
|
|
return Source{}, true, fmt.Errorf("%q isn't a folder in the repository", src.Path)
|
|
}
|
|
}
|
|
return src, true, nil
|
|
}
|
|
|
|
// gitURL is a git source as the user can type it again: the repository,
|
|
// and its ref and folder as a GitLab page names them. An https address
|
|
// loses its .git there, so it opens the repository's page; GitLab and
|
|
// Gitea fetch it without one too.
|
|
func gitURL(s *Source) string {
|
|
if s.Ref == "" && s.Path == "" {
|
|
return s.Repo
|
|
}
|
|
ref := s.Ref
|
|
if ref == "" {
|
|
ref = "HEAD"
|
|
}
|
|
u := s.Repo
|
|
if strings.HasPrefix(u, "https://") || strings.HasPrefix(u, "http://") {
|
|
u = strings.TrimSuffix(u, ".git") // the page, not the clone
|
|
}
|
|
u += "/-/tree/" + ref
|
|
if s.Path != "" {
|
|
u += "/" + s.Path
|
|
}
|
|
return u
|
|
}
|
|
|
|
// sameGitRepo is whether two addresses are one repository: by host and
|
|
// path, however it is reached (https, SSH, with .git or without).
|
|
func sameGitRepo(a, b string) bool {
|
|
if strings.EqualFold(strings.TrimSuffix(a, ".git"), strings.TrimSuffix(b, ".git")) {
|
|
return true
|
|
}
|
|
ra, rb := remoteRepo(a), remoteRepo(b)
|
|
return ra != "" && strings.EqualFold(ra, rb)
|
|
}
|
|
|
|
// gitHeads is the commit each git download is at, by its folder.
|
|
var gitHeads = struct {
|
|
sync.Mutex
|
|
m map[string]string
|
|
}{m: map[string]string{}}
|
|
|
|
func gitHeadOf(dir string) string {
|
|
gitHeads.Lock()
|
|
defer gitHeads.Unlock()
|
|
return gitHeads.m[dir]
|
|
}
|
|
|
|
func noteGitHead(dir, sha string) {
|
|
gitHeads.Lock()
|
|
defer gitHeads.Unlock()
|
|
for d := range gitHeads.m {
|
|
if _, err := os.Stat(d); err != nil {
|
|
delete(gitHeads.m, d) // a download removed since
|
|
}
|
|
}
|
|
gitHeads.m[dir] = sha
|
|
}
|
|
|
|
// errNoGit is git missing from this computer.
|
|
func errNoGit() error {
|
|
return errors.New("installing from a git repository runs git, and git isn't installed here (or isn't on PATH): install git and try again")
|
|
}
|
|
|
|
// gitEnv is the environment git runs in: the user's, so their credential
|
|
// helper, SSH agent and config work, with every question asked on a
|
|
// terminal turned off (no one sees it, and it would wait for ever), and
|
|
// git's messages in English, as magpie reads them.
|
|
func gitEnv(git string) []string {
|
|
env := append(os.Environ(), "GIT_TERMINAL_PROMPT=0", "LC_ALL=C", "LANGUAGE=C")
|
|
if os.Getenv("GIT_SSH_COMMAND") == "" && os.Getenv("GIT_SSH") == "" {
|
|
// ssh asks for a passphrase or a new host's key on the terminal:
|
|
// BatchMode refuses instead. A core.sshCommand of the user's own
|
|
// (a key for the intranet) is kept, which GIT_SSH_COMMAND would
|
|
// override.
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
cmd := proc.CommandContext(ctx, git, "config", "--global", "--get", "core.sshCommand")
|
|
cmd.Env = env
|
|
if out, _ := cmd.Output(); strings.TrimSpace(string(out)) == "" {
|
|
env = append(env, "GIT_SSH_COMMAND=ssh -o BatchMode=yes")
|
|
}
|
|
}
|
|
return env
|
|
}
|
|
|
|
// runGit runs git with args in dir, and gives back its output, or an
|
|
// error saying plainly what went wrong for src.
|
|
func runGit(ctx context.Context, git string, env []string, dir string, src Source, args ...string) ([]byte, error) {
|
|
cmd := proc.CommandContext(ctx, git, append([]string{"-c", "protocol.ext.allow=never", "-c", "core.symlinks=false"}, args...)...)
|
|
cmd.Dir, cmd.Env = dir, env
|
|
var stderr strings.Builder
|
|
cmd.Stderr = &stderr
|
|
out, err := cmd.Output()
|
|
if err == nil {
|
|
return out, nil
|
|
}
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return nil, fmt.Errorf("git took over %s to fetch %s and was stopped", gitTimeout, src.Repo)
|
|
}
|
|
var ee *exec.ExitError
|
|
if !errors.As(err, &ee) {
|
|
return nil, fmt.Errorf("couldn't run git: %w", err)
|
|
}
|
|
return nil, gitError(src, stderr.String())
|
|
}
|
|
|
|
// gitError says what git's message means for the user.
|
|
func gitError(src Source, msg string) error {
|
|
low := strings.ToLower(msg)
|
|
said := gitSaid(msg)
|
|
host := gitHost(src.Repo)
|
|
switch {
|
|
case strings.Contains(low, "xcrun: error") || strings.Contains(low, "invalid active developer path") || strings.Contains(low, "no developer tools were found"):
|
|
return errors.New("installing from a git repository runs git, and git isn't installed here: install Apple's command line tools (xcode-select --install) and try again")
|
|
case strings.Contains(low, "host key verification failed"):
|
|
return fmt.Errorf("SSH doesn't know %s's host key yet: connect to it once with ssh or git in a terminal to accept it, then try again (git said: %s)", host, said)
|
|
case strings.Contains(low, "could not read username") || strings.Contains(low, "could not read password") ||
|
|
strings.Contains(low, "terminal prompts disabled") || strings.Contains(low, "authentication failed") ||
|
|
strings.Contains(low, "permission denied") || strings.Contains(low, "access denied") ||
|
|
strings.Contains(low, "the requested url returned error: 401") || strings.Contains(low, "the requested url returned error: 403"):
|
|
return fmt.Errorf("git couldn't sign in to %s. magpie runs your own git, so sign in the way git does there (a credential helper or an SSH key that can read the repository), check `git clone %s` works in a terminal, and try again (git said: %s)", host, src.Repo, said)
|
|
case strings.Contains(low, "couldn't find remote ref") || strings.Contains(low, "unknown revision"):
|
|
return fmt.Errorf("%s has no branch or tag %s", src.Repo, src.Ref)
|
|
case strings.Contains(low, "repository not found") || strings.Contains(low, "does not appear to be a git repository") ||
|
|
strings.Contains(low, "not found") && strings.Contains(low, "repository") || strings.Contains(low, "returned error: 404"):
|
|
return fmt.Errorf("there's no git repository at %s, or it can't be read without signing in (git said: %s)", src.Repo, said)
|
|
case strings.Contains(low, "could not resolve host") || strings.Contains(low, "connection refused") ||
|
|
strings.Contains(low, "connection timed out") || strings.Contains(low, "network is unreachable") || strings.Contains(low, "couldn't connect"):
|
|
return fmt.Errorf("git couldn't reach %s (git said: %s)", host, said)
|
|
}
|
|
return fmt.Errorf("git couldn't fetch %s: %s", src.Repo, said)
|
|
}
|
|
|
|
// gitSaid is git's message, its last few lines, for an error.
|
|
func gitSaid(s string) string {
|
|
var keep []string
|
|
for _, l := range strings.Split(strings.TrimSpace(s), "\n") {
|
|
if l = strings.TrimSpace(l); l != "" && !strings.HasPrefix(l, "hint:") {
|
|
keep = append(keep, l)
|
|
}
|
|
}
|
|
if len(keep) > 3 {
|
|
keep = keep[len(keep)-3:]
|
|
}
|
|
out := strings.Join(keep, " / ")
|
|
if r := []rune(out); len(r) > 400 {
|
|
out = string(r[:399]) + "…"
|
|
}
|
|
if out == "" {
|
|
out = "nothing"
|
|
}
|
|
return out
|
|
}
|
|
|
|
// gitHost is the host of a repository's address, for a message.
|
|
func gitHost(repo string) string {
|
|
if u, err := url.Parse(repo); err == nil && u.Scheme != "" {
|
|
if u.Host == "" {
|
|
return repo
|
|
}
|
|
return u.Hostname()
|
|
}
|
|
if m := scpLike.FindStringSubmatch(repo); m != nil {
|
|
return m[2]
|
|
}
|
|
return repo
|
|
}
|
|
|
|
// fetchGit fetches the repository's ref (its default branch when "")
|
|
// with the user's git, one commit deep, into a new folder, and gives it
|
|
// back without its .git: what GitHub's tarball would have. Links are
|
|
// checked out as small files holding where they point, as a tarball's
|
|
// are left out: never followed out of the folder.
|
|
func fetchGit(src Source) (string, error) {
|
|
git := gitPath()
|
|
if git == "" {
|
|
return "", errNoGit()
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), gitTimeout)
|
|
defer cancel()
|
|
env := gitEnv(git)
|
|
tmp, err := os.MkdirTemp("", skillTemp)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
fail := func(err error) (string, error) {
|
|
os.RemoveAll(tmp)
|
|
return "", err
|
|
}
|
|
if _, err := runGit(ctx, git, env, tmp, src, "init", "-q"); err != nil {
|
|
return fail(err)
|
|
}
|
|
ref := src.Ref
|
|
if ref == "" {
|
|
ref = "HEAD"
|
|
}
|
|
if _, err := runGit(ctx, git, env, tmp, src, "fetch", "-q", "--depth", "1", "--no-tags", "--", src.Repo, ref); err != nil {
|
|
// a host that serves git over plain ("dumb") HTTP can't fetch one
|
|
// commit deep: the whole history then
|
|
if !strings.Contains(strings.ToLower(err.Error()), "shallow") {
|
|
return fail(err)
|
|
}
|
|
if _, err := runGit(ctx, git, env, tmp, src, "fetch", "-q", "--no-tags", "--", src.Repo, ref); err != nil {
|
|
return fail(err)
|
|
}
|
|
}
|
|
if _, err := runGit(ctx, git, env, tmp, src, "-c", "advice.detachedHead=false", "checkout", "-q", "FETCH_HEAD"); err != nil {
|
|
return fail(err)
|
|
}
|
|
out, err := runGit(ctx, git, env, tmp, src, "rev-parse", "FETCH_HEAD")
|
|
if err != nil {
|
|
return fail(err)
|
|
}
|
|
if err := os.RemoveAll(filepath.Join(tmp, ".git")); err != nil {
|
|
return fail(err)
|
|
}
|
|
if size := dirSize(tmp); size > maxFetch {
|
|
return fail(fmt.Errorf("%s is %s, over the %s magpie fetches", src.Repo, mb(size), mb(maxFetch)))
|
|
}
|
|
noteGitHead(tmp, strings.TrimSpace(string(out)))
|
|
return tmp, nil
|
|
}
|
|
|
|
func dirSize(dir string) int64 {
|
|
var n int64
|
|
filepath.Walk(dir, func(_ string, fi os.FileInfo, err error) error {
|
|
if err == nil && fi.Mode().IsRegular() {
|
|
n += fi.Size()
|
|
}
|
|
return nil
|
|
})
|
|
return n
|
|
}
|
|
|
|
// gitRemoteHead asks the repository, with git ls-remote, which commit its
|
|
// ref (its default branch when "") is at now. A ref that is a commit is
|
|
// that commit.
|
|
func gitRemoteHead(src Source) (string, error) {
|
|
if hexRef.MatchString(src.Ref) {
|
|
return strings.ToLower(src.Ref), nil
|
|
}
|
|
git := gitPath()
|
|
if git == "" {
|
|
return "", errNoGit()
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), gitTimeout)
|
|
defer cancel()
|
|
args := []string{"ls-remote", "--", src.Repo}
|
|
if src.Ref == "" {
|
|
args = append(args, "HEAD")
|
|
} else {
|
|
args = append(args, "refs/heads/"+src.Ref, "refs/tags/"+src.Ref, "refs/tags/"+src.Ref+"^{}")
|
|
}
|
|
out, err := runGit(ctx, git, gitEnv(git), os.TempDir(), src, args...)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
refs := map[string]string{}
|
|
for _, l := range strings.Split(string(out), "\n") {
|
|
if sha, name, ok := strings.Cut(strings.TrimSpace(l), "\t"); ok {
|
|
refs[name] = sha
|
|
}
|
|
}
|
|
for _, k := range []string{"HEAD", "refs/tags/" + src.Ref + "^{}", "refs/heads/" + src.Ref, "refs/tags/" + src.Ref} {
|
|
if sha := refs[k]; hexRef.MatchString(sha) {
|
|
return sha, nil
|
|
}
|
|
}
|
|
if src.Ref == "" {
|
|
return "", fmt.Errorf("%s names no default branch", src.Repo)
|
|
}
|
|
return "", fmt.Errorf("%s has no branch or tag %s any more", src.Repo, src.Ref)
|
|
}
|
|
|
|
// checkGitSkill checks a skill from a git repository: git ls-remote says
|
|
// which commit its ref is at; one the skill was fetched at is current,
|
|
// and for another the repository is fetched and the skill's folder
|
|
// compared with the library's, since a commit elsewhere in the repository
|
|
// doesn't change it. When its files are the repository's, it gives back
|
|
// the commit they're at and their hash, for the library to keep.
|
|
func checkGitSkill(s *Skill, f *fetcher, asked map[string]string) (SkillCheck, string, string) {
|
|
src := *s.Source
|
|
c := SkillCheck{Name: s.Name, Status: "unknown"}
|
|
head, ok := asked[src.Ref]
|
|
if !ok {
|
|
var err error
|
|
if head, err = gitRemoteHead(src); err != nil {
|
|
c.Error = err.Error()
|
|
return c, "", ""
|
|
}
|
|
asked[src.Ref] = head
|
|
}
|
|
c.Commit = head
|
|
if s.Commit != "" && strings.EqualFold(s.Commit, head) {
|
|
c.Status = "current"
|
|
return c, "", ""
|
|
}
|
|
tmp, err := f.fetch(src)
|
|
if err != nil {
|
|
c.Error = err.Error()
|
|
return c, "", ""
|
|
}
|
|
from := filepath.Join(tmp, filepath.FromSlash(src.Path))
|
|
if _, ok := readMeta(from); !ok {
|
|
if goneFrom(tmp, src.Path) {
|
|
return goneCheck(&GoneError{Name: s.Name, Source: src.Repo}), "", ""
|
|
}
|
|
c.Error = fmt.Sprintf("%s has no SKILL.md at %s any more", src.Repo, src.Path)
|
|
return c, "", ""
|
|
}
|
|
have := s.Hash
|
|
if have == "" {
|
|
have = hashDir(skillDir(s.Name))
|
|
}
|
|
if theirs := hashDir(from); theirs == "" || theirs != have {
|
|
c.Status = "update"
|
|
return c, "", ""
|
|
}
|
|
c.Status = "current"
|
|
if h := gitHeadOf(tmp); h != "" {
|
|
c.Commit = h
|
|
}
|
|
return c, c.Commit, have
|
|
}
|
|
|
|
// isGit is whether what the user typed is a git repository's address
|
|
// magpie fetches from.
|
|
func isGit(s string) bool {
|
|
_, ok, err := gitSource(s)
|
|
return ok && err == nil
|
|
}
|