Files
skillshare/internal/install/install.go
T
Willie 2ce46834e2 fix(collect,install): collect only real skills; let --track take a local git path (#508)
* fix(collect,install): collect only real skills; let --track take a local git path

collect treated every directory in a target as a local skill, so a scratch
folder was listed and `collect --force` copied it into the source. A folder
now counts only when it holds SKILL.md; the dashboard scan shares
FindLocalSkills and the dashboard collect request rejects such a folder.

`install /path/to/repo --track` failed with "requires a git repository
source" while file:///path worked. A local path that is a git repository
root is now normalized to the same file:// clone URL for --track (also past
the --branch guard); other local folders get an error that names file://.
Help text, docs (English and i18n) and the built-in skill reference follow.

* fix(install): keep the drive letter in file:// clone URLs for local --track paths

A Windows path such as C:/repo produced file://C:/repo, which makes C: the
URI authority instead of part of the path, so a tracked install from a local
Windows repository could clone the wrong location. Prefix the missing slash
so the URL is file:///C:/repo; POSIX paths are unchanged. Also cover the
dashboard install endpoint with a local-path --track test.

* fix(collect,install): keep UNC file URLs intact; require SKILL.md to be a file

The drive-letter fix lived in the helper shared with explicit file:// URLs,
which rewrote file://server/share/repo into a local path. Keep the slash
logic in the local-path --track case only and leave explicit URLs as parsed.

A subdirectory named SKILL.md passed the existence check, so a scratch folder
holding one was still collected. Both collect entry points (scan and the
dashboard request) now share sync.HasSkillFile, which rejects directories.

* fix(install): escape the local path in the file:// URL used by --track

Git decodes a file:// URL, so a repository path containing %20, %2F, a space
or # was cloned from a different path than the one IsGitRepo accepted. Build
the URL with net/url so those characters are escaped; drive-letter and POSIX
paths keep their form. The integration test now installs from a directory
whose name contains a space and a literal %20.

* fix(install): accept a local bare repository path for --track

A bare repository has HEAD and objects at its root but no .git entry, so the
local-path check rejected it while file:///same/path cloned fine. Add
IsLocalGitRepo (working tree or bare) and use it for both the --track
normalization and the --branch guard.

* test(install): build the bare repo path from a Windows file URL correctly

makeRemote returns file:///C:/dir on Windows; trimming only file:// left
/C:/dir, which filepath.Abs turned into D:\C:\dir, so the bare-repo test
failed on the Windows runner. Drop the slash before the drive letter.

* fix(install): refuse a --track local source that is the install destination

--force removes the tracked destination before cloning. When the local path
given to --track is that same directory, the removal deleted the only clone
source (and any unpushed work) before the clone failed. Compare the two with
os.SameFile, which also covers symlinked paths, and stop before touching the
destination.

* fix(install,collect): keep a tracked source inside its destination; reject special SKILL.md files

--track --force removed the destination before cloning, so a local source
nested below it (or given as an explicit file:// URL) was deleted along with
any unpushed work, and the clone then failed. Resolve the source and refuse
when it is the destination or lies under it, for dry runs too.

HasSkillFile used a not-a-directory test, so a FIFO named SKILL.md counted as
a skill and the collect copy blocked on it. Require a regular file; a
symlinked SKILL.md still resolves through os.Stat.

* fix(install): treat file://localhost URLs as local clone sources

Git clones file://localhost/path from the local filesystem, but the
containment guard only recognised an empty authority, so --track --force
could still delete a source given that way. Accept localhost (any case) as
local.

* test(install): build the localhost file URL from a slash path

On Windows the temp path starts with a drive letter, so prefixing it onto
file://localhost produced file://localhostC:\... instead of a valid URL and
the case failed on the Windows runner. Convert to slashes and add the
separator explicitly.

* fix(install): clone --track --force into a staging dir and swap it in last

--force removed the existing tracked repo before cloning, so any source that
lived under it (plain path, file:///, file://localhost, UNC, symlink or 8.3
alias) was destroyed before git could read it, and a failed clone also lost
the old repo. Clone beside the destination, run the checks and the audit on
the staging copy, then move the old repo aside, rename the new one in and
drop the backup; on any failure the staging copy is removed and the old repo
stays. The pre-removal containment refusal now covers plain local paths only
(the url.Parse special-casing is gone).

* fix(install): refuse a file:// --track source inside the forced destination

The staged swap in 7375400 still lost data for a file:///, file://localhost
or UNC source nested under the destination: the clone succeeded, the old repo
(with the source inside it) was renamed to the backup, and removing the backup
deleted the source's uncommitted work, which a clone cannot carry.

Restore the file URL to local path mapping (now including UNC hosts) so the
containment refusal covers every local spelling, not only plain paths. The
test now keeps an uncommitted file in the source and requires it to survive.

* fix(install): apply the source-inside-destination refusal only to --force

The refusal ran before the --update branch, so `install <tracked repo path>
--track --update` was rejected even though an update pulls the existing
checkout in place and removes nothing. Only the forced replacement can
destroy a source that lives under the destination, so check there.

* fix(install): report a forced clone's root skill under its tracked name

With --force the repo is discovered in the .skillshare-clone-<stamp> staging
directory, and discoverSkills names a root SKILL.md after the directory, so
the CLI summary, dashboard response and oplog listed a random staging name
instead of _foo. Use the tracked name for the root entry.

* fix(install): stage fresh tracked clones so cleanup never hits another installer

A fresh tracked install cloned straight into the destination, and the
deferred cleanup removed it on any failure. If another process created the
same tracked repo between the existence check and the clone, the failing
install deleted that process's checkout. Clone every install into its own
staging directory and rename it in last; a claimed destination makes the
rename fail and only the staging copy is removed.
2026-10-10 01:06:23 +08:00

359 lines
13 KiB
Go

package install
import (
"errors"
"fmt"
"log/slog"
"os"
"strings"
"skillshare/internal/sourcewalk"
)
// InstallOptions configures the install behavior
type InstallOptions struct {
Name string // Override skill name
Kind string // "skill", "agent", or "" (auto-detect)
Force bool // Overwrite existing
DryRun bool // Preview only
Update bool // Update existing installation
Track bool // Install as tracked repository (preserves .git)
OnProgress ProgressCallback
Skills []string // Select specific skills from multi-skill repo (comma-separated)
AgentNames []string // Select specific agents from repo (comma-separated)
Exclude []string // Skills to exclude from installation (comma-separated)
All bool // Install all discovered skills without prompting
Yes bool // Auto-accept all prompts (equivalent to --all for multi-skill repos)
Into string // Install into subdirectory (e.g. "frontend" or "frontend/react")
SkipAudit bool // Skip security audit entirely
AuditVerbose bool // Print full audit findings in CLI output (default is compact summary)
AuditThreshold string // Block threshold: CRITICAL/HIGH/MEDIUM/LOW/INFO
AuditProjectRoot string // Project root for project-mode audit rule resolution
Quiet bool // Suppress per-skill output in InstallFromConfig
Branch string // Git branch to clone from (empty = remote default)
SourceDir string // Skills root dir for centralized metadata (set by caller)
Lock *Lock // Pinned commits for InstallFromConfig (project mode); nil = follow the branch
// AuditOverride lets an install proceed despite audit findings at/above the
// block threshold. Deliberately separate from Force, which only means
// "overwrite what is already there" and is set unconditionally by update and
// --json paths — routing the audit gate through Force would disable it there.
AuditOverride bool
// AuditAcceptRoot/AuditAcceptPath override where accepted audit findings
// are looked up and recorded. Staged installs audit a temp directory, so
// they point these at the final destination to keep metadata keys stable.
AuditAcceptRoot string
AuditAcceptPath string
// skillFileOnly makes a local install copy only SKILL.md. Updates set it
// to keep the shape of a collection root installed that way.
skillFileOnly bool
// SourceFollow is the operation's follow policy for SourceDir, so writes
// pass through followed first-level links; nil follows none.
SourceFollow *sourcewalk.Follow
}
// auditAcceptTarget returns the (sourceDir, path) pair used to key accepted
// audit findings for an install landing at path.
func (o InstallOptions) auditAcceptTarget(path string) (string, string) {
if o.AuditAcceptPath != "" {
return o.AuditAcceptRoot, o.AuditAcceptPath
}
return o.SourceDir, path
}
// IsAgentMode returns true if explicitly installing agents.
func (o InstallOptions) IsAgentMode() bool { return o.Kind == "agent" }
// HasAgentFilter returns true if specific agents were requested via -a flag.
func (o InstallOptions) HasAgentFilter() bool { return len(o.AgentNames) > 0 }
// ShouldInstallAll returns true if all discovered skills should be installed without prompting.
func (o InstallOptions) ShouldInstallAll() bool { return o.All || o.Yes }
// HasSkillFilter returns true if specific skills were requested via --skill flag.
func (o InstallOptions) HasSkillFilter() bool { return len(o.Skills) > 0 }
// InstallResult reports the outcome of an installation
type InstallResult struct {
SkillName string
SkillPath string
Source string
Action string // "cloned", "copied", "updated", "skipped"
Warnings []string
AuditThreshold string
AuditRiskScore int
AuditRiskLabel string
AuditSkipped bool
}
// SkillInfo represents a discovered skill in a repository
type SkillInfo struct {
Name string // Skill name (directory name)
Path string // Relative path from repo root
License string // License from SKILL.md frontmatter (if any)
Description string // Description from SKILL.md frontmatter (if any)
}
// AgentInfo represents a discovered agent (.md file) in a repository
type AgentInfo struct {
Name string // Agent name (filename without .md)
Path string // Relative path from repo root (e.g. "agents/tutor.md")
FileName string // Filename (e.g. "tutor.md")
}
// DiscoveryResult contains discovered skills and agents from a repository
type DiscoveryResult struct {
RepoPath string // Temp directory where repo was cloned
Skills []SkillInfo // Discovered skills
Agents []AgentInfo // Discovered agents
Source *Source // Original source
CommitHash string // Source commit hash when available
Warnings []string // Non-fatal warnings during discovery
}
// HasAgents reports whether the discovery found any agents.
func (d *DiscoveryResult) HasAgents() bool {
return len(d.Agents) > 0
}
// HasSkills reports whether the discovery found any skills.
func (d *DiscoveryResult) HasSkills() bool {
return len(d.Skills) > 0
}
// IsMixed reports whether the discovery found both skills and agents.
func (d *DiscoveryResult) IsMixed() bool {
return d.HasSkills() && d.HasAgents()
}
// TrackedRepoResult reports the outcome of a tracked repo installation
type TrackedRepoResult struct {
RepoName string // Name of the tracked repo (e.g., "_team-skills")
RepoPath string // Full path to the repo
SkillCount int // Number of skills discovered
Skills []string // Names of discovered skills
AgentCount int // Number of agents discovered
Agents []string // Names of discovered agents
Action string // "cloned", "updated", "skipped"
Warnings []string
AuditThreshold string
AuditRiskScore int
AuditRiskLabel string
AuditSkipped bool
}
// ErrSkipSameRepo is returned when the destination skill already exists and
// was installed from the same repository. Callers should treat this as a
// friendly skip, not a hard failure.
var ErrSkipSameRepo = errors.New("already installed from same repo")
// checkExistingConflict reads the meta of an existing skill and compares
// its repo_url with the incoming source. Returns:
// - nil when the directory is empty/invalid (caller should overwrite)
// - ErrSkipSameRepo when the repos match (caller should skip gracefully)
// - a descriptive error when they differ or meta is absent (real conflict)
func checkExistingConflict(destPath string, incomingCloneURL string, forceHint string) error {
meta, err := ReadMeta(destPath)
if err != nil || meta == nil {
// No meta — check if directory is truly empty.
if dirIsEmpty(destPath) {
return nil
}
// Non-empty directory without meta — unknown origin.
return fmt.Errorf("already exists. To overwrite: %s", forceHint)
}
if repoURLsMatch(meta.RepoURL, incomingCloneURL) {
return fmt.Errorf("%w: use 'skillshare update' or --force to overwrite", ErrSkipSameRepo)
}
// Different repo — real conflict with provenance info.
return fmt.Errorf("already exists (installed from %s). To overwrite: %s", meta.RepoURL, forceHint)
}
// dirIsEmpty returns true if the directory has no entries (or only OS junk
// like .DS_Store). Returns false for non-directories or read errors.
func dirIsEmpty(path string) bool {
entries, err := os.ReadDir(path)
if err != nil {
return false
}
for _, e := range entries {
if e.Name() == ".DS_Store" {
continue
}
return false
}
return true
}
// repoURLsMatch compares two clone URLs, normalizing trailing ".git" and
// protocol differences (https vs git@). Returns false when either URL is empty.
func repoURLsMatch(a, b string) bool {
na, nb := normalizeCloneURL(a), normalizeCloneURL(b)
return na != "" && nb != "" && na == nb
}
func normalizeCloneURL(u string) string {
u = strings.TrimSpace(u)
u = strings.TrimSuffix(u, ".git")
u = strings.TrimSuffix(u, "/")
// user@github.com:owner/repo → github.com/owner/repo
if at := strings.Index(u, "@"); at > 0 && !strings.Contains(u, "://") {
u = u[at+1:]
u = strings.Replace(u, ":", "/", 1)
}
// https://github.com/owner/repo → github.com/owner/repo
u = strings.TrimPrefix(u, "https://")
u = strings.TrimPrefix(u, "http://")
return strings.ToLower(u)
}
// buildForceHint constructs a user-facing hint like
// "skillshare install <source> --into <dir> --force".
func buildForceHint(rawSource, into string) string {
cmd := "skillshare install " + rawSource
if into != "" {
cmd += " --into " + into
}
cmd += " --force"
return cmd
}
// removeAll is a test hook used by audit/install paths.
var removeAll = os.RemoveAll
var removeTempRepoPath = os.RemoveAll
func cleanupTempRepo(path string) {
if path == "" {
return
}
if err := removeTempRepoPath(path); err != nil {
slog.Warn("failed to clean up temp repo", "path", path, "error", err)
}
}
// Install executes the installation from source to destination.
// This file is intentionally a thin facade; implementation lives in split files.
func Install(source *Source, destPath string, opts InstallOptions) (*InstallResult, error) {
return installImpl(source, destPath, opts)
}
// DiscoverFromGit clones a repository and discovers skills inside it.
func DiscoverFromGit(source *Source) (*DiscoveryResult, error) {
return discoverFromGitImpl(source)
}
// DiscoverFromGitWithProgress clones a repository and discovers skills inside it
// while optionally streaming git progress output.
func DiscoverFromGitWithProgress(source *Source, onProgress ProgressCallback) (*DiscoveryResult, error) {
return discoverFromGitWithProgressImpl(source, onProgress)
}
// DiscoverFromGitSubdir clones a repository and discovers skills in the source subdir.
func DiscoverFromGitSubdir(source *Source) (*DiscoveryResult, error) {
return discoverFromGitSubdirImpl(source)
}
// DiscoverFromGitSubdirWithProgress clones a repository and discovers skills in
// the source subdir while optionally streaming git progress output.
func DiscoverFromGitSubdirWithProgress(source *Source, onProgress ProgressCallback) (*DiscoveryResult, error) {
return discoverFromGitSubdirWithProgressImpl(source, onProgress)
}
// TrackKindAmbiguousError is returned by InferTrackedKind when a tracked
// install targets a repository that contains both skills and agents and the
// caller did not pass an explicit kind. It carries the discovered counts so
// the UI can present a kind picker without re-cloning.
type TrackKindAmbiguousError struct {
Skills int
Agents int
}
func (e *TrackKindAmbiguousError) Error() string {
return "tracked install is ambiguous for mixed repositories; pass --kind skill or --kind agent"
}
// InferTrackedKind determines which resource kind a tracked install should use.
// Pure-agent repositories resolve to "agent". Mixed repositories must specify
// the kind explicitly to avoid ambiguous install roots; in that case the
// returned error is a *TrackKindAmbiguousError carrying the discovered counts.
func InferTrackedKind(source *Source, explicitKind string) (string, error) {
if err := normalizeTrackSource(source); err != nil {
return "", err
}
if explicitKind == "skill" || explicitKind == "agent" {
return explicitKind, nil
}
var (
discovery *DiscoveryResult
err error
)
if source.HasSubdir() {
discovery, err = DiscoverFromGitSubdir(source)
} else {
discovery, err = DiscoverFromGit(source)
}
if err != nil {
return "", err
}
defer CleanupDiscovery(discovery)
switch {
case discovery.HasSkills() && discovery.HasAgents():
return "", &TrackKindAmbiguousError{
Skills: len(discovery.Skills),
Agents: len(discovery.Agents),
}
case discovery.HasAgents() && !discovery.HasSkills():
return "agent", nil
default:
return "skill", nil
}
}
// DiscoverLocal inspects a local directory and discovers skills and agents.
func DiscoverLocal(source *Source) (*DiscoveryResult, error) {
return discoverLocalImpl(source)
}
// CleanupDiscovery removes temporary resources created by discovery.
func CleanupDiscovery(result *DiscoveryResult) {
cleanupDiscoveryImpl(result)
}
// InstallFromDiscovery installs one selected skill from a discovery result.
func InstallFromDiscovery(discovery *DiscoveryResult, skill SkillInfo, destPath string, opts InstallOptions) (*InstallResult, error) {
return installFromDiscoveryImpl(discovery, skill, destPath, opts)
}
// InstallTrackedRepo clones a git repository as a tracked repo.
func InstallTrackedRepo(source *Source, sourceDir string, opts InstallOptions) (*TrackedRepoResult, error) {
return installTrackedRepoImpl(source, sourceDir, opts)
}
// GetUpdatableSkills returns skill names that have metadata with a remote source.
func GetUpdatableSkills(sourceDir string) ([]string, error) {
return getUpdatableSkillsImpl(sourceDir)
}
// GetTrackedRepos returns tracked repositories in the source directory as
// slash paths relative to it (org/_team), on every OS. An optional walk policy
// follows first-level source links; omitted, none are.
func GetTrackedRepos(sourceDir string, walk ...sourcewalk.Options) ([]string, error) {
var opts sourcewalk.Options
if len(walk) > 0 {
opts = walk[0]
}
return getTrackedReposImpl(sourceDir, opts)
}
// GetMissingTrackedRepos returns tracked metadata entries whose repo clone is absent.
// An optional walk policy recognizes existing followed source checkouts.
func GetMissingTrackedRepos(sourceDir string, walks ...sourcewalk.Options) ([]TrackedRepoMeta, error) {
return getMissingTrackedReposImpl(sourceDir, walks...)
}