Files
Willie 52744e2f34 feat(project): register every Git checkout in a workspace with --scan (#958)
Some tools, such as Codex, look for project skills only up to the Git root
of the folder you open. When one workspace folder holds several checkouts
or worktrees, skills synced to the workspace are not seen inside them, and
registering each checkout by hand repeats the same settings many times.

`project add <root> --scan DIR` registers the root and each Git checkout
found below the scan folders as ordinary project entries with the same
settings, in one write. No scan setting is saved; rerun it after a new
clone. It only adds: a checkout already registered with the same settings
is kept, one with different settings or its own .skillshare/ is skipped
with the reason, and a name clash derives the name from the path. A scan
folder that is missing or cannot be read is skipped with a warning so one
unreadable folder does not block the rest. Rows carry a root-relative path
and a skip code so the dashboard can explain them in its own words.

Dashboard:

The dashboard should offer what `project add --scan` does, so a workspace
with many checkouts can be set up without the terminal.

The Add project dialog gains "Also add each checkout inside this folder"
with the folders to look in. Review runs the scan as a dry run through the
new POST /api/projects/scan and lists each folder as Added, Kept or Skipped
with a translated reason and a link to a project whose settings differ;
nothing is written until it is confirmed. The write scans again and counts
what it actually added, so a checkout registered in the meantime is not
reported as new. MCP and Hooks, when picked, are set up for each added
project. Without an explicit storage choice the default follows
projects_storage, as the CLI does without a flag.

Closes #953

Release-As: 0.30.12
2026-11-19 01:35:56 +08:00

125 lines
4.2 KiB
Go

package projects
import (
"maps"
"os"
"path/filepath"
"runtime"
"slices"
"testing"
"skillshare/internal/config"
)
func TestScanCheckoutsSkipsLinks(t *testing.T) {
root := t.TempDir()
outside := t.TempDir()
for _, dir := range []string{filepath.Join(root, "app", ".git"), filepath.Join(outside, "other", ".git"), filepath.Join(root, "linked-git")} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
}
// A linked checkout folder and a linked .git could both point at another checkout.
if err := os.Symlink(filepath.Join(outside, "other"), filepath.Join(root, "linked")); err != nil {
t.Skip("symlinks unavailable:", err)
}
if err := os.Symlink(filepath.Join(outside, "other", ".git"), filepath.Join(root, "linked-git", ".git")); err != nil {
t.Fatal(err)
}
found, _, err := ScanCheckouts(root, []string{".", "app"})
if err != nil {
t.Fatal(err)
}
if !slices.Equal(found, []string{"app"}) {
t.Fatalf("found %v, want [app]", found)
}
if _, _, err := ScanCheckouts(root, []string{"linked"}); err == nil {
t.Fatal("a linked scan folder was accepted")
}
}
func TestScanCheckoutsThroughLinkedRoot(t *testing.T) {
real := filepath.Join(t.TempDir(), "suite")
if err := os.MkdirAll(filepath.Join(real, "app", ".git"), 0755); err != nil {
t.Fatal(err)
}
link := filepath.Join(t.TempDir(), "suite")
if err := os.Symlink(real, link); err != nil {
t.Skip("symlinks unavailable:", err)
}
found, _, err := ScanCheckouts(link, []string{"."})
if err != nil {
t.Fatal(err)
}
if !slices.Equal(found, []string{"app"}) {
t.Fatalf("found %v, want [app]", found)
}
}
func TestPlanScanNamesClashingCheckoutsFromTheirPath(t *testing.T) {
home := t.TempDir()
root := filepath.Join(home, "suite")
cfg := &config.Config{Projects: map[string]config.ManagedProject{
filepath.Join(home, "other", "worktrees-app-b"): {Targets: []string{"codex"}},
}}
entry := config.ManagedProject{Targets: []string{"codex"}}
// Sorted as a scan returns them: the worktrees come before their checkouts.
found := []string{".worktrees/app-a", ".worktrees/app-b", "app-a", "app-b"}
rows, adds, err := planScan(cfg, root, entry, found, "", home, home)
if err != nil {
t.Fatal(err)
}
names := map[string]string{}
for _, row := range rows {
rel, _ := filepath.Rel(root, row.Root)
names[filepath.ToSlash(rel)] = row.Result + ":" + row.Name
}
want := map[string]string{".": "added:suite", "app-a": "added:app-a", "app-b": "added:app-b", ".worktrees/app-a": "added:worktrees-app-a", ".worktrees/app-b": "skipped:worktrees-app-b"}
if !maps.Equal(names, want) {
t.Fatalf("rows %v, want %v", names, want)
}
if adds[filepath.Join(root, "app-a")].Name != "" || adds[filepath.Join(root, ".worktrees", "app-a")].Name != "worktrees-app-a" {
t.Fatalf("saved names: %+v", adds)
}
}
func TestPlanScanSkipsCheckoutWithItsOwnSkillshare(t *testing.T) {
root := t.TempDir()
// A clone of a shared skills repository: config.yaml is gitignored.
if err := os.MkdirAll(filepath.Join(root, "tools", ".skillshare", "skills"), 0755); err != nil {
t.Fatal(err)
}
rows, adds, err := planScan(&config.Config{}, root, config.ManagedProject{Targets: []string{"codex"}}, []string{"tools"}, "", root, root)
if err != nil {
t.Fatal(err)
}
if len(adds) != 1 || rows[1].Result != ScanSkipped {
t.Fatalf("rows %+v, adds %v", rows, adds)
}
}
func TestScanCheckoutsSkipsUnreadableFolders(t *testing.T) {
if os.Geteuid() == 0 || runtime.GOOS == "windows" {
t.Skip("test requires POSIX directory permissions and a non-root user")
}
root := t.TempDir()
for _, dir := range []string{filepath.Join(root, "app", ".git"), filepath.Join(root, "private", "secret", ".git")} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
}
private := filepath.Join(root, "private")
if err := os.Chmod(private, 0); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(private, 0755) })
// The scan folder itself sits below the unreadable one.
found, warnings, err := ScanCheckouts(root, []string{".", "private/secret"})
if err != nil {
t.Fatal(err)
}
if !slices.Equal(found, []string{"app"}) || len(warnings) != 2 || warnings[0].Folder != "private" || warnings[1].Folder != "private/secret" {
t.Fatalf("found %v, warnings %v", found, warnings)
}
}