mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* feat(install): pin GitHub tree/blob URLs to their ref Hub entries only carry a source URL, so a team hub had no way to pin a third-party skill to a reviewed tag or commit. The parser already read tree/<ref>/ and blob/<ref>/ URLs but discarded the ref, installing from the default branch instead of the version the URL names. Keep that ref as Source.Branch. An explicit --branch (CLI) or branch (API body) still overrides it; the server handlers now only override when a branch is given. Batch installs group by repo and ref so two pinned versions of one repo get separate clones. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * docs(install): describe pinning hub sources to a tag or commit Explain that a GitHub tree/<ref>/ source URL pins the install, in both the hub index guide and the install reference. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(install): pin GitLab/Bitbucket URLs and resolve refs containing "/" Pinning should not depend on the host, so GitLab (-/tree/<ref>/) and Bitbucket (src/<ref>/) web URLs now keep their ref too. Taking the first path segment as the ref broke branch names containing "/": tree/feature/x/skills/foo cloned branch "feature" and failed. When the first segment is not a branch or tag on the remote, the longest matching one is used instead. A ref the remote does not have fails with a hint to use --branch, rather than silently installing the default branch. A saved branch that covers more of the path (from --branch or config) moves the subdir without asking the remote. The grouped-install round-trip test pinned -/tree/main/ against a repo on master; the ref used to be ignored, so its branch is now renamed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * docs(install): cover GitLab, Bitbucket and "/" branches in URL pinning Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(install): keep web URL refs correct on reinstall, --branch and grouping Review of the slash-branch resolution found four gaps: - A saved branch equal to the whole URL tail (tree/feature/x) did not move the subdir back, so every reinstall or update looked for "x". - An unrelated --branch with a slash-branch URL kept the parse-time subdir (x/skills/foo) instead of the URL's real one. - Grouped installs copy the source to a repo root; resolution then put the subdir back and discovery looked under a doubled path. Grouping also keyed on the unresolved first segment, so tree/feature/x and tree/feature/y could share one clone. Web URLs with a ref now install on their own, and resolution leaves a repo-root copy at its root. - A skill named after the parse-time subdir kept that name once the path turned out to be all ref (tree/feature/x named the skill "x"). The unknown-ref error now reads as a missing ref first, since a stale ref (tree/master after a rename) is more common than a slash branch. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(update): keep legacy installs on the default branch Skills installed before URL refs were honoured got the default branch and recorded no branch. Single-skill update re-parses the recorded source, so it moved them to the URL's ref (or failed on a stale one, with a hint to pass --branch, which update does not accept), while update --all rebuilds from RepoURL and kept the default branch. ApplyRecordedBranch treats an empty recorded branch as the remote default and drops the URL's ref. It replaces the branch handling at every site that reinstalls from recorded metadata: CLI and server update for skills and agents, and tracked-repo rehydration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * perf(search): share one clone across pinned hub skills from a repo Any web URL with a path after its ref installed on its own, so a hub listing many tree/<tag>/ skills from one repo cloned it once per skill. groupByRepo now resolves each URL's ref before grouping, with a RemoteRefs cache that lists each remote's refs once, and groups by repo and resolved ref. Only refs containing "/" (whose subdir the discovery match cannot re-derive) and refs that fail to resolve still install alone. A resolved source is marked settled so the grouped clone does not resolve it again, and ApplyRecordedBranch settles the subdir from a recorded branch without asking the remote. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Willie <egg358358@gmail.com>
714 lines
23 KiB
Go
714 lines
23 KiB
Go
package install
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// buildDiscoverySkillSource constructs metadata Source string for a skill
|
|
// selected from a discovery result.
|
|
func buildDiscoverySkillSource(source *Source, skillPath string) string {
|
|
if skillPath == "." {
|
|
return source.Raw
|
|
}
|
|
if source.HasSubdir() {
|
|
return source.Raw + "/" + skillPath
|
|
}
|
|
// Whole-repo SSH sources encode subdir using //path.
|
|
if source.Type == SourceTypeGitSSH {
|
|
return source.Raw + "//" + skillPath
|
|
}
|
|
return source.Raw + "/" + skillPath
|
|
}
|
|
|
|
func discoverySourceRoot(discovery *DiscoveryResult) string {
|
|
if discovery.Source != nil && discovery.Source.Type == SourceTypeLocalPath {
|
|
return discovery.RepoPath
|
|
}
|
|
return filepath.Join(discovery.RepoPath, "repo")
|
|
}
|
|
|
|
// descendantSkillPaths returns the set of slash-normalized paths (relative to
|
|
// the source root of `skill`) of every other discovered skill that lives
|
|
// strictly inside `skill`. The returned map is suitable as the excludes
|
|
// argument to copyDirExcluding.
|
|
//
|
|
// For the root skill (Path="."), every other discovered skill counts as a
|
|
// descendant. For a non-root skill, only nested sub-skills count. Returns nil
|
|
// when there is nothing to exclude so callers hit the fast path in
|
|
// copyDirExcluding.
|
|
func descendantSkillPaths(discovery *DiscoveryResult, skill SkillInfo) map[string]bool {
|
|
if discovery == nil || len(discovery.Skills) <= 1 {
|
|
return nil
|
|
}
|
|
current := filepath.ToSlash(skill.Path)
|
|
var prefix string
|
|
if current != "." {
|
|
prefix = current + "/"
|
|
}
|
|
excludes := make(map[string]bool)
|
|
for _, other := range discovery.Skills {
|
|
otherPath := filepath.ToSlash(other.Path)
|
|
if otherPath == current {
|
|
continue
|
|
}
|
|
if current == "." {
|
|
// Root skill: every other skill path is a strict descendant.
|
|
if otherPath != "." {
|
|
excludes[otherPath] = true
|
|
}
|
|
continue
|
|
}
|
|
// Non-root: only paths strictly under current count.
|
|
if rel, ok := strings.CutPrefix(otherPath, prefix); ok {
|
|
// rel is the path relative to `skill` (which is how
|
|
// copyDirExcluding walks `srcPath`).
|
|
excludes[rel] = true
|
|
}
|
|
}
|
|
if len(excludes) == 0 {
|
|
return nil
|
|
}
|
|
return excludes
|
|
}
|
|
|
|
func installAgentRelativePath(agent AgentInfo) string {
|
|
return strings.TrimPrefix(filepath.ToSlash(agent.Path), "agents/")
|
|
}
|
|
|
|
func resolveDiscoveredAgentSourcePath(discovery *DiscoveryResult, agent AgentInfo) string {
|
|
sourceRoot := discoverySourceRoot(discovery)
|
|
if discovery.Source.HasSubdir() {
|
|
return filepath.Join(sourceRoot, discovery.Source.Subdir, agent.Path)
|
|
}
|
|
return filepath.Join(sourceRoot, agent.Path)
|
|
}
|
|
|
|
func buildDiscoveredAgentSource(discovery *DiscoveryResult, agent AgentInfo) *Source {
|
|
return &Source{
|
|
Type: discovery.Source.Type,
|
|
Raw: buildDiscoverySkillSource(discovery.Source, agent.Path),
|
|
CloneURL: discovery.Source.CloneURL,
|
|
Subdir: agent.Path,
|
|
Name: agent.Name,
|
|
Branch: discovery.Source.Branch,
|
|
Commit: discovery.Source.Commit,
|
|
}
|
|
}
|
|
|
|
func writeDiscoveredAgentMetadata(discovery *DiscoveryResult, agent AgentInfo, destFile, sourceDir string) error {
|
|
source := buildDiscoveredAgentSource(discovery, agent)
|
|
meta := NewMetaFromSource(source)
|
|
meta.Kind = "agent"
|
|
if discovery.CommitHash != "" {
|
|
meta.Version = discovery.CommitHash
|
|
}
|
|
if hash, hashErr := computeSingleFileHash(destFile); hashErr == nil {
|
|
meta.FileHashes = map[string]string{filepath.Base(destFile): hash}
|
|
}
|
|
return WriteMetaToStore(sourceDir, destFile, meta)
|
|
}
|
|
|
|
func installAgentFromDiscoveryInternal(discovery *DiscoveryResult, agent AgentInfo, destFile string, opts InstallOptions, writeMeta bool) (*InstallResult, error) {
|
|
result := &InstallResult{
|
|
SkillName: agent.Name,
|
|
Source: buildDiscoverySkillSource(discovery.Source, agent.Path),
|
|
SkillPath: destFile,
|
|
}
|
|
|
|
if opts.DryRun {
|
|
result.Action = "would install"
|
|
return result, nil
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(destFile), 0755); err != nil {
|
|
return nil, fmt.Errorf("failed to create agents directory: %w", err)
|
|
}
|
|
|
|
if _, err := os.Stat(destFile); err == nil && !opts.Force {
|
|
result.Action = "skipped"
|
|
result.Warnings = append(result.Warnings, "agent already exists (use --force to overwrite)")
|
|
return result, nil
|
|
}
|
|
|
|
data, err := os.ReadFile(resolveDiscoveredAgentSourcePath(discovery, agent))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read agent %s: %w", agent.FileName, err)
|
|
}
|
|
|
|
if err := os.WriteFile(destFile, data, 0644); err != nil {
|
|
return nil, fmt.Errorf("failed to write agent %s: %w", agent.FileName, err)
|
|
}
|
|
|
|
if err := auditInstalledAgent(destFile, result, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if writeMeta {
|
|
if err := writeDiscoveredAgentMetadata(discovery, agent, destFile, opts.SourceDir); err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err))
|
|
}
|
|
}
|
|
|
|
result.Action = "installed"
|
|
return result, nil
|
|
}
|
|
|
|
// skillsRootFor returns opts.SourceDir, or derives it when the caller left it
|
|
// empty: destPath = sourceDir[/into]/skillName, so strip Into + skillName.
|
|
func skillsRootFor(destPath string, opts InstallOptions) string {
|
|
if opts.SourceDir != "" {
|
|
return opts.SourceDir
|
|
}
|
|
dir := filepath.Dir(destPath)
|
|
if opts.Into != "" {
|
|
dir = filepath.Dir(dir)
|
|
for i := strings.Count(opts.Into, "/"); i > 0; i-- {
|
|
dir = filepath.Dir(dir)
|
|
}
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func installImpl(source *Source, destPath string, opts InstallOptions) (*InstallResult, error) {
|
|
opts.SourceDir = skillsRootFor(destPath, opts)
|
|
|
|
result := &InstallResult{
|
|
SkillName: source.Name,
|
|
Source: source.Raw,
|
|
}
|
|
|
|
// Check if destination exists
|
|
destInfo, destErr := os.Stat(destPath)
|
|
destExists := destErr == nil
|
|
|
|
if destExists {
|
|
if opts.Update {
|
|
return handleUpdate(source, destPath, result, opts)
|
|
}
|
|
if !opts.Force {
|
|
hint := buildForceHint(source.Raw, opts.Into)
|
|
if err := checkExistingConflict(destPath, source.CloneURL, hint); err != nil {
|
|
return nil, err
|
|
}
|
|
// nil means empty/invalid dir — safe to overwrite, fall through.
|
|
}
|
|
// Force mode (or empty dir): remove existing
|
|
if !opts.DryRun {
|
|
if err := os.RemoveAll(destPath); err != nil {
|
|
return nil, fmt.Errorf("failed to remove existing skill: %w", err)
|
|
}
|
|
}
|
|
} else if destInfo != nil && !destInfo.IsDir() {
|
|
return nil, fmt.Errorf("destination exists but is not a directory")
|
|
}
|
|
|
|
result.SkillPath = destPath
|
|
|
|
// Execute installation based on source type
|
|
switch source.Type {
|
|
case SourceTypeLocalPath:
|
|
return installFromLocal(source, destPath, result, opts)
|
|
case SourceTypeGitHub, SourceTypeGitHTTPS, SourceTypeGitSSH:
|
|
return installFromGit(source, destPath, result, opts)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported source type: %s", source.Type)
|
|
}
|
|
}
|
|
|
|
func installFromLocal(source *Source, destPath string, result *InstallResult, opts InstallOptions) (*InstallResult, error) {
|
|
// Verify source exists
|
|
srcInfo, err := os.Stat(source.Path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("source path does not exist: %s", source.Path)
|
|
}
|
|
return nil, fmt.Errorf("cannot access source path: %w", err)
|
|
}
|
|
if !srcInfo.IsDir() {
|
|
return nil, fmt.Errorf("source path is not a directory: %s", source.Path)
|
|
}
|
|
|
|
if opts.DryRun {
|
|
result.Action = "would copy"
|
|
return result, nil
|
|
}
|
|
|
|
// Copy directory
|
|
if err := copyDir(source.Path, destPath); err != nil {
|
|
return nil, fmt.Errorf("failed to copy skill: %w", err)
|
|
}
|
|
|
|
// Security audit
|
|
if err := auditInstalledSkill(destPath, result, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Write metadata with file hashes
|
|
meta := NewMetaFromSource(source)
|
|
if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil {
|
|
meta.FileHashes = hashes
|
|
}
|
|
if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err))
|
|
}
|
|
|
|
// Check for SKILL.md
|
|
checkSkillFile(destPath, result)
|
|
|
|
result.Action = "copied"
|
|
return result, nil
|
|
}
|
|
|
|
func installFromGit(source *Source, destPath string, result *InstallResult, opts InstallOptions) (*InstallResult, error) {
|
|
// Check if git is available
|
|
if !isGitInstalled() {
|
|
return nil, fmt.Errorf("git is not installed or not in PATH")
|
|
}
|
|
if err := resolveWebRef(source); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// If subdir is specified, install directly
|
|
if source.HasSubdir() {
|
|
return installFromGitSubdir(source, destPath, result, opts)
|
|
}
|
|
|
|
// No subdir specified - this should be handled by DiscoverFromGit first
|
|
// If we get here, treat it as "install entire repo as one skill"
|
|
if opts.DryRun {
|
|
result.Action = "would clone"
|
|
return result, nil
|
|
}
|
|
|
|
// Clone the repository
|
|
if err := cloneRepoForSource(source, destPath, source.Branch, true, opts.OnProgress); err != nil {
|
|
return nil, fmt.Errorf("failed to clone repository: %w", err)
|
|
}
|
|
|
|
// Write metadata with file hashes
|
|
meta := NewMetaFromSource(source)
|
|
if hash, err := getGitCommit(destPath); err == nil {
|
|
meta.Version = hash
|
|
}
|
|
if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil {
|
|
meta.FileHashes = hashes
|
|
}
|
|
if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err))
|
|
}
|
|
|
|
// Check for SKILL.md
|
|
checkSkillFile(destPath, result)
|
|
|
|
// Security audit
|
|
if err := auditInstalledSkill(destPath, result, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result.Action = "cloned"
|
|
return result, nil
|
|
}
|
|
|
|
// DiscoverFromGit clones a repo and discovers available skills
|
|
|
|
func installFromDiscoveryImpl(discovery *DiscoveryResult, skill SkillInfo, destPath string, opts InstallOptions) (*InstallResult, error) {
|
|
return installFromDiscoveryInternal(discovery, skill, destPath, opts, true)
|
|
}
|
|
|
|
func discoveredSkillSourceParts(discovery *DiscoveryResult, skill SkillInfo) (string, string) {
|
|
// For subdir discovery, skill.Path is relative to the subdir.
|
|
// For whole-repo discovery, skill.Path is relative to repo root.
|
|
if skill.Path == "." {
|
|
// Root skill of a subdir discovery.
|
|
return buildDiscoverySkillSource(discovery.Source, skill.Path), discovery.Source.Subdir
|
|
}
|
|
if discovery.Source.HasSubdir() {
|
|
// Nested skill within subdir discovery.
|
|
return buildDiscoverySkillSource(discovery.Source, skill.Path), discovery.Source.Subdir + "/" + skill.Path
|
|
}
|
|
// Whole-repo discovery.
|
|
return buildDiscoverySkillSource(discovery.Source, skill.Path), skill.Path
|
|
}
|
|
|
|
func installFromDiscoveryInternal(discovery *DiscoveryResult, skill SkillInfo, destPath string, opts InstallOptions, writeMeta bool) (*InstallResult, error) {
|
|
// Same derivation as Install: without it the metadata of an --into install
|
|
// lands in the group folder and the skill never reaches the config.
|
|
opts.SourceDir = skillsRootFor(destPath, opts)
|
|
fullSource, fullSubdir := discoveredSkillSourceParts(discovery, skill)
|
|
|
|
result := &InstallResult{
|
|
SkillName: skill.Name,
|
|
Source: fullSource,
|
|
}
|
|
|
|
// Check if destination exists
|
|
stage := false
|
|
if _, err := os.Stat(destPath); err == nil {
|
|
if !opts.Force {
|
|
// Use the original repo URL for force hints, not the per-skill
|
|
// fullSource URL (which isn't a valid install target).
|
|
hint := buildForceHint(discovery.Source.Raw, opts.Into)
|
|
if err := checkExistingConflict(destPath, discovery.Source.CloneURL, hint); err != nil {
|
|
return nil, err
|
|
}
|
|
// nil means empty/invalid dir — safe to overwrite, fall through.
|
|
}
|
|
// Update: stage the new content in a temp dir and swap only after the
|
|
// audit passes, so a blocked update keeps the previously installed
|
|
// version on disk (mirrors handleUpdate). Fresh --force installs still
|
|
// overwrite in place.
|
|
stage = opts.Update
|
|
if !opts.DryRun && !stage {
|
|
if err := os.RemoveAll(destPath); err != nil {
|
|
return nil, fmt.Errorf("failed to remove existing skill: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
result.SkillPath = destPath
|
|
|
|
if opts.DryRun {
|
|
result.Action = "would install"
|
|
return result, nil
|
|
}
|
|
|
|
workDest := destPath
|
|
if stage {
|
|
tempDir, err := os.MkdirTemp("", "skillshare-update-*")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create temp directory: %w", err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
workDest = filepath.Join(tempDir, "skill")
|
|
}
|
|
|
|
// Determine source path in temp repo
|
|
sourceRoot := discoverySourceRoot(discovery)
|
|
var srcPath string
|
|
if discovery.Source.HasSubdir() {
|
|
// Subdir discovery: paths are relative to the subdir
|
|
if skill.Path == "." {
|
|
srcPath = filepath.Join(sourceRoot, discovery.Source.Subdir)
|
|
} else {
|
|
srcPath = filepath.Join(sourceRoot, discovery.Source.Subdir, skill.Path)
|
|
}
|
|
} else {
|
|
// Whole-repo discovery: paths are relative to repo root
|
|
srcPath = filepath.Join(sourceRoot, skill.Path)
|
|
}
|
|
|
|
// In a repo-root orchestrator (root SKILL.md + children), the root skill
|
|
// has no directory boundary — the entire repo root is its "directory",
|
|
// which includes project infrastructure (src/, assets/, CI, etc.) that
|
|
// is not skill content. Copy only the SKILL.md file itself.
|
|
//
|
|
// Do not apply this to scoped subdir discovery: there the selected subdir
|
|
// is the boundary, so sibling files in that subdir are legitimate skill
|
|
// content and must still be copied.
|
|
excludes := descendantSkillPaths(discovery, skill)
|
|
rootIsRepoRoot := skill.Path == "." && discovery.Source != nil && !discovery.Source.HasSubdir()
|
|
|
|
if rootIsRepoRoot && excludes != nil {
|
|
// Repo-root orchestrator: copy only SKILL.md (no directory boundary).
|
|
if err := os.MkdirAll(workDest, 0755); err != nil {
|
|
return nil, fmt.Errorf("failed to create destination: %w", err)
|
|
}
|
|
if err := copyFile(filepath.Join(srcPath, "SKILL.md"), filepath.Join(workDest, "SKILL.md")); err != nil {
|
|
return nil, fmt.Errorf("failed to copy SKILL.md: %w", err)
|
|
}
|
|
} else if err := copyDirExcluding(srcPath, workDest, excludes); err != nil {
|
|
return nil, fmt.Errorf("failed to copy skill: %w", err)
|
|
}
|
|
|
|
// Security audit — on block, cleanup only touches workDest, so a staged
|
|
// update leaves the existing install untouched.
|
|
auditOpts := opts
|
|
if auditOpts.AuditAcceptPath == "" {
|
|
auditOpts.AuditAcceptRoot, auditOpts.AuditAcceptPath = opts.SourceDir, destPath
|
|
}
|
|
if err := auditInstalledSkill(workDest, result, auditOpts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if stage {
|
|
if err := os.RemoveAll(destPath); err != nil {
|
|
return nil, fmt.Errorf("failed to remove existing skill: %w", err)
|
|
}
|
|
if err := os.Rename(workDest, destPath); err != nil {
|
|
// Rename failed (possibly cross-device), try copy instead
|
|
if err := copyDir(workDest, destPath); err != nil {
|
|
return nil, fmt.Errorf("failed to move updated skill: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
if writeMeta {
|
|
if err := writeDiscoveredSkillMetadata(discovery, skill, destPath, opts.SourceDir, fullSource, fullSubdir); err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err))
|
|
}
|
|
}
|
|
|
|
result.Action = "installed"
|
|
return result, nil
|
|
}
|
|
|
|
func writeDiscoveredSkillMetadata(discovery *DiscoveryResult, skill SkillInfo, destPath, sourceDir, fullSource, fullSubdir string) error {
|
|
source := &Source{
|
|
Type: discovery.Source.Type,
|
|
Raw: fullSource,
|
|
CloneURL: discovery.Source.CloneURL,
|
|
Subdir: fullSubdir,
|
|
Name: skill.Name,
|
|
Branch: discovery.Source.Branch,
|
|
Commit: discovery.Source.Commit,
|
|
}
|
|
meta := NewMetaFromSource(source)
|
|
sourceRoot := discoverySourceRoot(discovery)
|
|
if discovery.CommitHash != "" {
|
|
meta.Version = discovery.CommitHash
|
|
} else if hash, err := getGitCommit(sourceRoot); err == nil {
|
|
meta.Version = hash
|
|
}
|
|
if fullSubdir != "" {
|
|
meta.TreeHash = getSubdirTreeHash(sourceRoot, fullSubdir)
|
|
}
|
|
if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil {
|
|
meta.FileHashes = hashes
|
|
}
|
|
return WriteMetaToStore(sourceDir, destPath, meta)
|
|
}
|
|
|
|
func installFromGitSubdir(source *Source, destPath string, result *InstallResult, opts InstallOptions) (*InstallResult, error) {
|
|
if opts.DryRun {
|
|
result.Action = "would clone and extract"
|
|
return result, nil
|
|
}
|
|
|
|
// Clone to temp directory
|
|
tempDir, err := os.MkdirTemp("", "skillshare-install-*")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create temp directory: %w", err)
|
|
}
|
|
defer cleanupTempRepo(tempDir)
|
|
|
|
tempRepoPath := filepath.Join(tempDir, "repo")
|
|
var subdirPath string
|
|
var resolved string
|
|
var commitHash string
|
|
|
|
// The Contents API paths below download the default branch and ignore any
|
|
// branch, tag or commit, so they only run when none was asked for.
|
|
hasRef := source.ref() != ""
|
|
|
|
// Fast path 1: sparse checkout (preferred for speed if git is modern)
|
|
// Works for GitHub and non-GitHub hosts.
|
|
if gitSupportsSparseCheckout() {
|
|
resolved = source.Subdir
|
|
if err := sparseCloneSubdir(source.CloneURL, resolved, tempRepoPath, source.ref(), source.authEnv(), opts.OnProgress); err == nil {
|
|
subdirPath = filepath.Join(tempRepoPath, resolved)
|
|
if info, statErr := os.Stat(subdirPath); statErr != nil || !info.IsDir() {
|
|
subdirPath = ""
|
|
result.Warnings = append(result.Warnings, "sparse checkout install fallback: subdirectory missing after checkout")
|
|
cleanupTempRepo(tempRepoPath)
|
|
} else {
|
|
source.recordCommit(tempRepoPath)
|
|
if hash, hashErr := getGitCommit(tempRepoPath); hashErr == nil {
|
|
commitHash = hash
|
|
}
|
|
}
|
|
} else {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("sparse checkout install fallback: %v", err))
|
|
cleanupTempRepo(tempRepoPath)
|
|
subdirPath = ""
|
|
}
|
|
}
|
|
|
|
// Fast path 2: GitHub/GHE Contents API
|
|
// Fallback for when sparse checkout is unavailable or fails.
|
|
if subdirPath == "" && !hasRef && isGitHubAPISource(source) {
|
|
owner, repo := source.GitHubOwner(), source.GitHubRepo()
|
|
resolved = source.Subdir
|
|
subdirPath = filepath.Join(tempRepoPath, resolved)
|
|
hash, dlErr := downloadGitHubDir(owner, repo, source.Subdir, subdirPath, source, opts.OnProgress)
|
|
if dlErr == nil {
|
|
commitHash = hash
|
|
} else {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("GitHub API install fallback: %v", dlErr))
|
|
subdirPath = ""
|
|
cleanupTempRepo(tempRepoPath)
|
|
}
|
|
}
|
|
|
|
// Fast path 2b: CNB Contents API
|
|
if subdirPath == "" && !hasRef && isCNBAPISource(source) {
|
|
repo := cnbRepoPath(source.CloneURL)
|
|
resolved = source.Subdir
|
|
subdirPath = filepath.Join(tempRepoPath, resolved)
|
|
hash, dlErr := downloadCNBDir(repo, source.Subdir, subdirPath, source, opts.OnProgress)
|
|
if dlErr == nil {
|
|
commitHash = hash
|
|
} else {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("CNB API install fallback: %v", dlErr))
|
|
subdirPath = ""
|
|
_ = os.RemoveAll(tempRepoPath)
|
|
}
|
|
}
|
|
|
|
// Fast path 2b: Gitea Contents API
|
|
if subdirPath == "" && !hasRef && isGiteaAPISource(source) {
|
|
owner, repo := giteaOwnerRepo(source.CloneURL)
|
|
resolved = source.Subdir
|
|
subdirPath = filepath.Join(tempRepoPath, resolved)
|
|
hash, dlErr := downloadGiteaDir(owner, repo, source.Subdir, subdirPath, source, opts.OnProgress)
|
|
if dlErr == nil {
|
|
commitHash = hash
|
|
} else {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("Gitea API install fallback: %v", dlErr))
|
|
subdirPath = ""
|
|
_ = os.RemoveAll(tempRepoPath)
|
|
}
|
|
}
|
|
|
|
// Fallback: full clone + fuzzy subdir resolution
|
|
if subdirPath == "" {
|
|
cleanupTempRepo(tempRepoPath)
|
|
if opts.OnProgress != nil {
|
|
opts.OnProgress("Cloning repository...")
|
|
}
|
|
if err := cloneRepoForSource(source, tempRepoPath, source.Branch, true, opts.OnProgress); err != nil {
|
|
return nil, fmt.Errorf("failed to clone repository: %w", err)
|
|
}
|
|
|
|
var err error
|
|
resolved, err = resolveSubdir(tempRepoPath, source.Subdir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resolved != source.Subdir {
|
|
source.Subdir = resolved
|
|
source.Name = filepath.Base(resolved)
|
|
result.SkillName = source.Name
|
|
}
|
|
subdirPath = filepath.Join(tempRepoPath, resolved)
|
|
if hash, hashErr := getGitCommit(tempRepoPath); hashErr == nil {
|
|
commitHash = hash
|
|
}
|
|
}
|
|
|
|
// Copy subdirectory to destination
|
|
if err := copyDir(subdirPath, destPath); err != nil {
|
|
return nil, fmt.Errorf("failed to copy skill: %w", err)
|
|
}
|
|
|
|
// Security audit
|
|
if err := auditInstalledSkill(destPath, result, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Write metadata with file hashes
|
|
meta := NewMetaFromSource(source)
|
|
if commitHash != "" {
|
|
meta.Version = commitHash
|
|
}
|
|
if resolved != "" {
|
|
meta.TreeHash = getSubdirTreeHash(tempRepoPath, resolved)
|
|
}
|
|
if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil {
|
|
meta.FileHashes = hashes
|
|
}
|
|
if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err))
|
|
}
|
|
|
|
// Check for SKILL.md
|
|
checkSkillFile(destPath, result)
|
|
|
|
result.Action = "cloned and extracted"
|
|
return result, nil
|
|
}
|
|
|
|
func checkSkillFile(skillPath string, result *InstallResult) {
|
|
skillFile := filepath.Join(skillPath, "SKILL.md")
|
|
if _, err := os.Stat(skillFile); os.IsNotExist(err) {
|
|
result.Warnings = append(result.Warnings, "no SKILL.md found in skill directory")
|
|
}
|
|
}
|
|
|
|
// InstallAgentFromDiscovery installs a single agent (.md file) from a discovery result.
|
|
// Unlike skill install (directory copy), agent install copies a single file.
|
|
func InstallAgentFromDiscovery(discovery *DiscoveryResult, agent AgentInfo, destDir string, opts InstallOptions) (*InstallResult, error) {
|
|
destFile := filepath.Join(destDir, filepath.FromSlash(installAgentRelativePath(agent)))
|
|
return installAgentFromDiscoveryInternal(discovery, agent, destFile, opts, true)
|
|
}
|
|
|
|
func UpdateAgentFromDiscovery(discovery *DiscoveryResult, agent AgentInfo, destDir string, opts InstallOptions) (*InstallResult, error) {
|
|
relPath := installAgentRelativePath(agent)
|
|
destFile := filepath.Join(destDir, filepath.FromSlash(relPath))
|
|
result := &InstallResult{
|
|
SkillName: agent.Name,
|
|
Source: buildDiscoverySkillSource(discovery.Source, agent.Path),
|
|
SkillPath: destFile,
|
|
}
|
|
|
|
if opts.DryRun {
|
|
result.Action = "would reinstall from source"
|
|
return result, nil
|
|
}
|
|
|
|
tempDir, err := os.MkdirTemp("", "skillshare-agent-update-*")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create temp directory: %w", err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
tempFile := filepath.Join(tempDir, filepath.FromSlash(relPath))
|
|
innerOpts := opts
|
|
innerOpts.DryRun = false
|
|
innerOpts.Update = false
|
|
innerOpts.SourceDir = tempDir
|
|
innerOpts.AuditAcceptRoot = opts.SourceDir
|
|
innerOpts.AuditAcceptPath = destFile
|
|
|
|
innerResult, err := installAgentFromDiscoveryInternal(discovery, agent, tempFile, innerOpts, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(destFile), 0755); err != nil {
|
|
return nil, fmt.Errorf("failed to create agents directory: %w", err)
|
|
}
|
|
// os.Rename is atomic on POSIX and overwrites the destination. If it
|
|
// fails (e.g. cross-device EXDEV on exotic setups), fall back to copy.
|
|
if err := os.Rename(tempFile, destFile); err != nil {
|
|
data, readErr := os.ReadFile(tempFile)
|
|
if readErr != nil {
|
|
return nil, fmt.Errorf("failed to read staged agent: %w", readErr)
|
|
}
|
|
if writeErr := os.WriteFile(destFile, data, 0644); writeErr != nil {
|
|
return nil, fmt.Errorf("failed to move updated agent: %w", writeErr)
|
|
}
|
|
}
|
|
|
|
if err := writeDiscoveredAgentMetadata(discovery, agent, destFile, opts.SourceDir); err != nil {
|
|
innerResult.Warnings = append(innerResult.Warnings, fmt.Sprintf("failed to write metadata: %v", err))
|
|
}
|
|
|
|
innerResult.SkillPath = destFile
|
|
innerResult.Action = "updated"
|
|
return innerResult, nil
|
|
}
|
|
|
|
// computeSingleFileHash computes the sha256 hash for a single file.
|
|
func computeSingleFileHash(filePath string) (string, error) {
|
|
return utils.FileHashFormatted(filePath)
|
|
}
|
|
|
|
// auditInstalledSkill scans the installed skill for security threats.
|
|
// It blocks installation when findings are at or above configured threshold
|
|
// unless force is enabled.
|