diff --git a/internal/install/install_tracked.go b/internal/install/install_tracked.go index 4d41e6e0b..1e08f3d72 100644 --- a/internal/install/install_tracked.go +++ b/internal/install/install_tracked.go @@ -5,7 +5,9 @@ import ( "net/url" "os" "path/filepath" + "strconv" "strings" + "time" "skillshare/internal/sourcefs" ) @@ -21,24 +23,6 @@ func localFileURL(path string) string { return (&url.URL{Scheme: "file", Path: p}).String() } -// localCloneSource returns the filesystem path a local source clones from, or -// "" for a remote one. -func localCloneSource(source *Source) string { - if source.Path != "" { - return source.Path - } - u, err := url.Parse(source.CloneURL) - // file://localhost/path is the same local path as file:///path. - if err != nil || u.Scheme != "file" || (u.Host != "" && !strings.EqualFold(u.Host, "localhost")) { - return "" - } - // file:///C:/repo carries the drive after a leading slash. - if p := u.Path; len(p) > 2 && p[0] == '/' && p[2] == ':' { - return filepath.FromSlash(p[1:]) - } - return filepath.FromSlash(u.Path) -} - // pathWithin reports whether path is dir itself or lies below it, after // resolving links. func pathWithin(dir, path string) bool { @@ -126,9 +110,10 @@ func installTrackedRepoImpl(source *Source, sourceDir string, opts InstallOption } // Check if already exists + replacing := false if _, err := os.Stat(destPath); err == nil { - // --force removes the destination before cloning; never do that to the clone source. - if p := localCloneSource(source); p != "" && pathWithin(destPath, p) { + // A plain local path that is, or lies inside, the destination would be replaced under itself. + if p := source.Path; p != "" && pathWithin(destPath, p) { return nil, fmt.Errorf("source %s is inside the install destination %s; nothing to install", p, destPath) } if opts.Update { @@ -152,11 +137,7 @@ func installTrackedRepoImpl(source *Source, sourceDir string, opts InstallOption if err := src.CheckNoLink(destRel); err != nil { return nil, err } - if !opts.DryRun { - if err := src.RemoveAll(destRel); err != nil { - return nil, fmt.Errorf("failed to remove existing repo: %w", err) - } - } + replacing = true } if opts.DryRun { @@ -171,28 +152,40 @@ func installTrackedRepoImpl(source *Source, sourceDir string, opts InstallOption if cloneBranch == "" { cloneBranch = source.Branch } + // --force clones beside the destination and swaps it in only after every + // check passed, so a failure (or a source that lives under the destination, + // however its URL is spelled) never costs the existing repo. + stamp := strconv.FormatInt(time.Now().UnixNano(), 36) + cloneRel, clonePath := destRel, destPath + if replacing { + cloneRel = filepath.Join(filepath.Dir(destRel), ".skillshare-clone-"+stamp) + clonePath = filepath.Join(sourceDir, cloneRel) + } + installed := false + defer func() { + if !installed { + _ = src.RemoveAll(cloneRel) + } + }() // A tag or commit SHA has no branch for `git pull` to follow. A SHA fails // at clone (--branch rejects it); a tag clones but leaves HEAD detached. - if err := cloneTrackedRepoForSource(source, destPath, cloneBranch, opts.OnProgress); err != nil { + if err := cloneTrackedRepoForSource(source, clonePath, cloneBranch, opts.OnProgress); err != nil { if IsCommitSHA(cloneBranch) { return nil, errTrackedNeedsBranch(cloneBranch) } return nil, fmt.Errorf("failed to clone repository: %w", err) } - if isDetachedHead(destPath) { - _ = src.RemoveAll(destRel) + if isDetachedHead(clonePath) { return nil, errTrackedNeedsBranch(cloneBranch) } if source.Commit != "" { - if err := resetTrackedToCommit(destPath, source.Commit, source.authEnv()); err != nil { - _ = src.RemoveAll(destRel) + if err := resetTrackedToCommit(clonePath, source.Commit, source.authEnv()); err != nil { return nil, err } } if source.HasSubdir() { - if err := submoduleError(destPath, source.Subdir, source.authEnv()); err != nil { - _ = src.RemoveAll(destRel) + if err := submoduleError(clonePath, source.Subdir, source.authEnv()); err != nil { return nil, err } } @@ -200,15 +193,15 @@ func installTrackedRepoImpl(source *Source, sourceDir string, opts InstallOption // Discover skills in the cloned repo. Include root SKILL.md so the count // matches what `skillshare sync` will see: every SKILL.md inside a tracked // repo (root and nested alike) becomes an independent skill on sync. - skills := discoverSkills(destPath, true) + skills := discoverSkills(clonePath, true) result.SkillCount = len(skills) for _, skill := range skills { result.Skills = append(result.Skills, skill.Name) } - result.Warnings = append(result.Warnings, SubmoduleWarnings(destPath, source.authEnv())...) + result.Warnings = append(result.Warnings, SubmoduleWarnings(clonePath, source.authEnv())...) // Also discover agents in the tracked repo - agents := discoverAgents(destPath, len(skills) > 0) + agents := discoverAgents(clonePath, len(skills) > 0) result.AgentCount = len(agents) if len(agents) > 0 { for _, agent := range agents { @@ -223,11 +216,33 @@ func installTrackedRepoImpl(source *Source, sourceDir string, opts InstallOption // Only agents found — not a warning, just informational } - // Security audit on the entire tracked repo - if err := auditTrackedRepo(destPath, result, opts); err != nil { + // Security audit on the entire tracked repo. Accepted findings are keyed by + // the final path, not the staging one. + if replacing { + opts.AuditAcceptRoot, opts.AuditAcceptPath = opts.auditAcceptTarget(destPath) + } + if err := auditTrackedRepo(clonePath, result, opts); err != nil { return nil, err } + if replacing { + // Keep the old repo until the new one is in place, as swapStagedIntoSource does. + backup := filepath.Join(filepath.Dir(destRel), ".skillshare-"+filepath.Base(destRel)+".old."+stamp) + if err := src.Rename(destRel, backup); err != nil { + return nil, fmt.Errorf("failed to move the existing repo aside: %w", err) + } + if err := src.Rename(cloneRel, destRel); err != nil { + if restoreErr := src.Rename(backup, destRel); restoreErr != nil { + return nil, fmt.Errorf("failed to move the new clone into place: %w; the previous repo is left at %s: %v", err, backup, restoreErr) + } + return nil, fmt.Errorf("failed to move the new clone into place: %w", err) + } + if err := src.RemoveAll(backup); err != nil { + result.Warnings = append(result.Warnings, fmt.Sprintf("failed to remove the previous repo %s: %v", backup, err)) + } + } + installed = true + // Auto-add to .gitignore to prevent committing tracked repo contents gitignoreEntry := trackedName if opts.Into != "" { diff --git a/internal/install/install_tracked_test.go b/internal/install/install_tracked_test.go index 18eff9328..e5d391946 100644 --- a/internal/install/install_tracked_test.go +++ b/internal/install/install_tracked_test.go @@ -315,18 +315,20 @@ func TestInferTrackedKind_LocalBareRepoPath(t *testing.T) { } } -func TestInstallTrackedRepo_RefusesLocalSourceInsideTheDestination(t *testing.T) { +func TestInstallTrackedRepo_ForceNeverDestroysItsLocalSource(t *testing.T) { cases := []struct { name string repo string // relative to the destination source func(repo string) string dryRun bool + refuse bool // a plain local path inside the destination is refused outright }{ - {"same path", "", func(p string) string { return p }, false}, - {"nested path", "nested", func(p string) string { return p }, false}, - {"explicit file URL", "nested", func(p string) string { return fileURL(p) }, false}, - {"localhost file URL", "nested", func(p string) string { return "file://localhost/" + strings.TrimPrefix(filepath.ToSlash(p), "/") }, false}, - {"dry run", "nested", func(p string) string { return p }, true}, + {"same path", "", func(p string) string { return p }, false, true}, + {"nested path", "nested", func(p string) string { return p }, false, true}, + {"dry run", "nested", func(p string) string { return p }, true, true}, + // URL forms are not matched by path; the clone must finish before anything is removed. + {"explicit file URL", "nested", func(p string) string { return fileURL(p) }, false, false}, + {"localhost file URL", "nested", func(p string) string { return "file://localhost/" + strings.TrimPrefix(filepath.ToSlash(p), "/") }, false, false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -338,18 +340,80 @@ func TestInstallTrackedRepo_RefusesLocalSourceInsideTheDestination(t *testing.T) if err := os.WriteFile(marker, []byte("# unpushed"), 0644); err != nil { t.Fatal(err) } + mustRunGit(t, repo, "add", ".") + mustRunGit(t, repo, "-c", "user.email=a@b", "-c", "user.name=n", "commit", "-m", "i") source, err := ParseSource(tc.source(repo)) if err != nil { t.Fatal(err) } _, err = InstallTrackedRepo(source, sourceDir, InstallOptions{Name: "foo", Force: true, DryRun: tc.dryRun}) - if err == nil { + switch { + case tc.refuse && err == nil: t.Fatal("expected an error when the source is inside the install destination") - } - if _, err := os.Stat(marker); err != nil { - t.Fatalf("source must be left intact: %v", err) + case err != nil: + if _, statErr := os.Stat(marker); statErr != nil { + t.Fatalf("source must be left intact after a failure: %v", statErr) + } + default: + if _, statErr := os.Stat(filepath.Join(dest, "SKILL.md")); statErr != nil { + t.Fatalf("a successful install must leave the cloned repo in place: %v", statErr) + } } }) } } + +func TestInstallTrackedRepo_ForceKeepsExistingRepoWhenCloneFails(t *testing.T) { + remoteURL := makeRemote(t, "") + sourceDir := t.TempDir() + source := &Source{Type: SourceTypeGitHTTPS, Raw: remoteURL, CloneURL: remoteURL} + if _, err := InstallTrackedRepo(source, sourceDir, InstallOptions{Name: "foo"}); err != nil { + t.Fatalf("first install: %v", err) + } + + _, err := InstallTrackedRepo(source, sourceDir, InstallOptions{Name: "foo", Force: true, Branch: "no-such-branch"}) + if err == nil { + t.Fatal("expected the clone of a missing branch to fail") + } + if _, statErr := os.Stat(filepath.Join(sourceDir, "_foo", "SKILL.md")); statErr != nil { + t.Fatalf("the existing repo must survive a failed forced reinstall: %v", statErr) + } + entries, _ := os.ReadDir(sourceDir) + for _, e := range entries { + if strings.HasPrefix(e.Name(), ".skillshare-clone-") { + t.Errorf("staging dir %s was left behind", e.Name()) + } + } +} + +func TestInstallTrackedRepo_ForceReplacesExistingRepoInto(t *testing.T) { + remoteURL := makeRemote(t, "") + sourceDir := t.TempDir() + source := &Source{Type: SourceTypeGitHTTPS, Raw: remoteURL, CloneURL: remoteURL} + opts := InstallOptions{Name: "foo", Into: "team"} + if _, err := InstallTrackedRepo(source, sourceDir, opts); err != nil { + t.Fatalf("first install: %v", err) + } + stale := filepath.Join(sourceDir, "team", "_foo", "stale.txt") + if err := os.WriteFile(stale, []byte("x"), 0644); err != nil { + t.Fatal(err) + } + + opts.Force = true + if _, err := InstallTrackedRepo(source, sourceDir, opts); err != nil { + t.Fatalf("forced reinstall: %v", err) + } + if _, err := os.Stat(stale); !os.IsNotExist(err) { + t.Errorf("the previous checkout should be replaced, stat err = %v", err) + } + if _, err := os.Stat(filepath.Join(sourceDir, "team", "_foo", "SKILL.md")); err != nil { + t.Errorf("the new clone should be in place: %v", err) + } + entries, _ := os.ReadDir(filepath.Join(sourceDir, "team")) + for _, e := range entries { + if strings.HasPrefix(e.Name(), ".skillshare-") { + t.Errorf("leftover %s after a forced reinstall", e.Name()) + } + } +}