fix(collect): skip .git directories when pulling nested skills to source

copyDirectoryWithState did not skip .git/ dirs, causing collect to fail
on git-cloned repos (e.g. obra/superpowers) — filepath.Walk would abort
on .git pack files, leaving only empty directories in source.

Add copyDirectorySkipGit() variant and use it in PullSkill. Existing
copyDirectory (used by copy-mode sync) is unchanged.
This commit is contained in:
Willie
2026-03-13 13:37:12 +08:00
parent 9d837e943f
commit 767ad31ec3
4 changed files with 319 additions and 5 deletions
@@ -0,0 +1,190 @@
# Collect: Nested Git Repo Skill
## Scope
Verifies that `skillshare collect` correctly handles skills with nested directory
structures (e.g., a git-cloned multi-skill repo like `obra/superpowers` placed
directly in a target). Key behaviors:
- Nested files and directories are fully copied to source (not just empty dirs)
- `.git/` directory is skipped during collect (not copied to source)
- Flat file at root level is also collected
This targets a reported bug where `collect` produced only empty directories when
the skill contained nested subdirectories (and optionally a `.git/` dir).
## Environment
- Devcontainer with `ss` binary
- ssenv-isolated HOME
- No network required (uses local fixture data)
## Steps
### Step 1: Setup — create environment and target with a simulated git-cloned repo
```bash
# Clean any stale state
ss extras remove rules --force -g 2>/dev/null || true
rm -rf ~/.claude/rules 2>/dev/null || true
# Init skillshare with claude target only
ss init --no-copy --all-targets --no-git --no-skill --force 2>/dev/null || true
# Get source and target paths
SOURCE=$(ss status --json | jq -r '.source.path')
TARGET=$(ss status --json | jq -r '.targets[] | select(.name == "claude") | .path')
echo "SOURCE=$SOURCE"
echo "TARGET=$TARGET"
# Ensure target dir exists
mkdir -p "$TARGET"
# Create a simulated git-cloned multi-skill repo in the target
REPO="$TARGET/superpowers"
mkdir -p "$REPO/.git/objects/pack"
mkdir -p "$REPO/.git/refs/heads"
mkdir -p "$REPO/skills/brainstorming"
mkdir -p "$REPO/skills/debugging/prompts"
mkdir -p "$REPO/skills/tdd/templates/go"
mkdir -p "$REPO/commands/commit"
mkdir -p "$REPO/docs"
# .git metadata files
echo "ref: refs/heads/main" > "$REPO/.git/HEAD"
echo "[core]\n\trepositoryformatversion = 0" > "$REPO/.git/config"
echo "pack-data" > "$REPO/.git/objects/pack/pack-abc.pack"
# Skill files (nested 2-3 levels deep)
echo "# Brainstorming Skill" > "$REPO/skills/brainstorming/SKILL.md"
echo "---\nname: brainstorming\n---" > "$REPO/skills/brainstorming/SKILL.md"
echo "# Debugging Skill" > "$REPO/skills/debugging/SKILL.md"
echo "debug prompt" > "$REPO/skills/debugging/prompts/default.md"
echo "# TDD Skill" > "$REPO/skills/tdd/SKILL.md"
echo "go test template" > "$REPO/skills/tdd/templates/go/test.md"
echo "// commit command" > "$REPO/commands/commit/index.js"
echo "# Superpowers Docs" > "$REPO/docs/README.md"
# Root-level file
echo "// gemini extension" > "$REPO/gemini-extension.js"
# Verify structure
find "$REPO" -not -path '*/.git/*' -not -path '*/.git' | sort
```
Expected:
- exit_code: 0
- skills/brainstorming/SKILL.md
- skills/debugging/prompts/default.md
- skills/tdd/templates/go/test.md
- commands/commit/index.js
- docs/README.md
- gemini-extension.js
### Step 2: Collect — run collect and verify JSON output
```bash
SOURCE=$(ss status --json | jq -r '.source.path')
TARGET=$(ss status --json | jq -r '.targets[] | select(.name == "claude") | .path')
# Run collect targeting claude, with --json (implies --force)
ss collect claude --json
```
Expected:
- exit_code: 0
- jq: .pulled | length == 1
- jq: .pulled[0] == "superpowers"
- jq: .failed | length == 0
### Step 3: Verify — nested files exist in source
```bash
SOURCE=$(ss status --json | jq -r '.source.path')
# Check all nested files were copied
echo "=== Checking nested files ==="
test -f "$SOURCE/superpowers/skills/brainstorming/SKILL.md" && echo "FOUND brainstorming/SKILL.md" || echo "MISSING brainstorming/SKILL.md"
test -f "$SOURCE/superpowers/skills/debugging/SKILL.md" && echo "FOUND debugging/SKILL.md" || echo "MISSING debugging/SKILL.md"
test -f "$SOURCE/superpowers/skills/debugging/prompts/default.md" && echo "FOUND debugging/prompts/default.md" || echo "MISSING debugging/prompts/default.md"
test -f "$SOURCE/superpowers/skills/tdd/SKILL.md" && echo "FOUND tdd/SKILL.md" || echo "MISSING tdd/SKILL.md"
test -f "$SOURCE/superpowers/skills/tdd/templates/go/test.md" && echo "FOUND tdd/templates/go/test.md" || echo "MISSING tdd/templates/go/test.md"
test -f "$SOURCE/superpowers/commands/commit/index.js" && echo "FOUND commands/commit/index.js" || echo "MISSING commands/commit/index.js"
test -f "$SOURCE/superpowers/docs/README.md" && echo "FOUND docs/README.md" || echo "MISSING docs/README.md"
test -f "$SOURCE/superpowers/gemini-extension.js" && echo "FOUND gemini-extension.js" || echo "MISSING gemini-extension.js"
# Count total files (excluding .git)
TOTAL=$(find "$SOURCE/superpowers" -type f | wc -l | tr -d ' ')
echo "TOTAL_FILES=$TOTAL"
```
Expected:
- exit_code: 0
- FOUND brainstorming/SKILL.md
- FOUND debugging/SKILL.md
- FOUND debugging/prompts/default.md
- FOUND tdd/SKILL.md
- FOUND tdd/templates/go/test.md
- FOUND commands/commit/index.js
- FOUND docs/README.md
- FOUND gemini-extension.js
- Not MISSING
### Step 4: Verify — .git directory was NOT copied to source
```bash
SOURCE=$(ss status --json | jq -r '.source.path')
if [ -d "$SOURCE/superpowers/.git" ]; then
echo "FAIL: .git directory was copied to source"
ls -la "$SOURCE/superpowers/.git/"
exit 1
else
echo "OK: .git directory correctly skipped"
fi
# Double check no .git files leaked
GIT_FILES=$(find "$SOURCE/superpowers" -path '*/.git/*' 2>/dev/null | wc -l | tr -d ' ')
echo "GIT_FILES_IN_SOURCE=$GIT_FILES"
```
Expected:
- exit_code: 0
- OK: .git directory correctly skipped
- GIT_FILES_IN_SOURCE=0
- Not FAIL
### Step 5: Verify — file content integrity
```bash
SOURCE=$(ss status --json | jq -r '.source.path')
# Verify actual file content was preserved (not truncated or empty)
CONTENT=$(cat "$SOURCE/superpowers/skills/tdd/templates/go/test.md")
echo "DEEPEST_FILE_CONTENT=$CONTENT"
SIZE=$(wc -c < "$SOURCE/superpowers/skills/tdd/templates/go/test.md" | tr -d ' ')
echo "DEEPEST_FILE_SIZE=$SIZE"
# Verify root-level file
ROOT_CONTENT=$(cat "$SOURCE/superpowers/gemini-extension.js")
echo "ROOT_FILE_CONTENT=$ROOT_CONTENT"
```
Expected:
- exit_code: 0
- DEEPEST_FILE_CONTENT=go test template
- ROOT_FILE_CONTENT=// gemini extension
## Pass Criteria
- Steps 1-5 all pass with exit_code: 0
- All 8 nested files are present in source after collect (Step 3)
- `.git/` directory is NOT present in source (Step 4)
- File content is preserved, not empty (Step 5)
+3 -2
View File
@@ -127,8 +127,9 @@ func PullSkill(skill LocalSkillInfo, sourcePath string, force bool) error {
}
}
// Copy skill to source
return copyDirectory(skill.Path, destPath)
// Copy skill to source, skipping .git directories (collect brings
// user content, not repository metadata).
return copyDirectorySkipGit(skill.Path, destPath)
}
// PullSkills pulls multiple skills to source
+107
View File
@@ -131,6 +131,113 @@ func TestPullSkill_NewSkill(t *testing.T) {
}
}
func TestPullSkill_NestedDirectories(t *testing.T) {
tmp := t.TempDir()
src := filepath.Join(tmp, "source")
tgt := filepath.Join(tmp, "target")
os.MkdirAll(src, 0755)
// Create a skill with nested subdirectories in the target
localSkill := filepath.Join(tgt, "my-skill")
os.MkdirAll(filepath.Join(localSkill, "prompts"), 0755)
os.MkdirAll(filepath.Join(localSkill, "templates", "react"), 0755)
os.WriteFile(filepath.Join(localSkill, "SKILL.md"), []byte("# My Skill"), 0644)
os.WriteFile(filepath.Join(localSkill, "prompts", "default.md"), []byte("prompt content"), 0644)
os.WriteFile(filepath.Join(localSkill, "templates", "base.md"), []byte("base template"), 0644)
os.WriteFile(filepath.Join(localSkill, "templates", "react", "component.md"), []byte("react template"), 0644)
skill := LocalSkillInfo{
Name: "my-skill",
Path: localSkill,
}
if err := PullSkill(skill, src, false); err != nil {
t.Fatal(err)
}
// Verify ALL files were copied, including nested ones
checks := []struct {
path string
want string
}{
{"my-skill/SKILL.md", "# My Skill"},
{"my-skill/prompts/default.md", "prompt content"},
{"my-skill/templates/base.md", "base template"},
{"my-skill/templates/react/component.md", "react template"},
}
for _, c := range checks {
data, err := os.ReadFile(filepath.Join(src, c.path))
if err != nil {
t.Errorf("expected %s to exist in source after pull: %v", c.path, err)
continue
}
if string(data) != c.want {
t.Errorf("%s: got %q, want %q", c.path, string(data), c.want)
}
}
}
func TestPullSkill_GitRepoSkipsGit(t *testing.T) {
tmp := t.TempDir()
src := filepath.Join(tmp, "source")
tgt := filepath.Join(tmp, "target")
os.MkdirAll(src, 0755)
// Simulate a git-cloned repo in the target (like superpowers)
localSkill := filepath.Join(tgt, "superpowers")
os.MkdirAll(filepath.Join(localSkill, ".git", "objects"), 0755)
os.MkdirAll(filepath.Join(localSkill, "agents", "brainstorming"), 0755)
os.MkdirAll(filepath.Join(localSkill, "commands", "commit"), 0755)
os.MkdirAll(filepath.Join(localSkill, "docs"), 0755)
// .git files
os.WriteFile(filepath.Join(localSkill, ".git", "HEAD"), []byte("ref: refs/heads/main"), 0644)
os.WriteFile(filepath.Join(localSkill, ".git", "config"), []byte("[core]"), 0644)
// Skill files
os.WriteFile(filepath.Join(localSkill, "agents", "brainstorming", "SKILL.md"), []byte("# Brainstorming"), 0644)
os.WriteFile(filepath.Join(localSkill, "commands", "commit", "SKILL.md"), []byte("# Commit"), 0644)
os.WriteFile(filepath.Join(localSkill, "docs", "README.md"), []byte("# Docs"), 0644)
os.WriteFile(filepath.Join(localSkill, "gemini-extension.js"), []byte("// ext"), 0644)
skill := LocalSkillInfo{
Name: "superpowers",
Path: localSkill,
}
if err := PullSkill(skill, src, false); err != nil {
t.Fatal(err)
}
// Verify skill files were copied
checks := []struct {
path string
want string
}{
{"superpowers/agents/brainstorming/SKILL.md", "# Brainstorming"},
{"superpowers/commands/commit/SKILL.md", "# Commit"},
{"superpowers/docs/README.md", "# Docs"},
{"superpowers/gemini-extension.js", "// ext"},
}
for _, c := range checks {
data, err := os.ReadFile(filepath.Join(src, c.path))
if err != nil {
t.Errorf("expected %s to exist in source after pull: %v", c.path, err)
continue
}
if string(data) != c.want {
t.Errorf("%s: got %q, want %q", c.path, string(data), c.want)
}
}
// Verify .git was NOT copied
if _, err := os.Stat(filepath.Join(src, "superpowers", ".git")); !os.IsNotExist(err) {
t.Error(".git directory should NOT be copied to source")
}
}
func TestPullSkill_AlreadyExists(t *testing.T) {
tmp := t.TempDir()
src := filepath.Join(tmp, "source")
+19 -3
View File
@@ -364,12 +364,23 @@ func mergeDirectories(src, dst string) error {
// copyDirectory copies a directory recursively
func copyDirectory(src, dst string) error {
return copyDirectoryWithState(src, dst, map[string]bool{})
return copyDirectoryWithState(src, dst, map[string]bool{}, nil)
}
// copyDirectoryOpts controls behavior of copyDirectoryWithState.
type copyDirectoryOpts struct {
SkipGit bool // skip .git directories
}
// copyDirectorySkipGit copies a directory recursively, skipping .git directories.
// Use this for collect/pull operations where .git is not wanted in the destination.
func copyDirectorySkipGit(src, dst string) error {
return copyDirectoryWithState(src, dst, map[string]bool{}, &copyDirectoryOpts{SkipGit: true})
}
// copyDirectoryWithState copies recursively and dereferences directory symlinks.
// active tracks real paths in the current recursion stack to prevent cycles.
func copyDirectoryWithState(src, dst string, active map[string]bool) error {
func copyDirectoryWithState(src, dst string, active map[string]bool, opts *copyDirectoryOpts) error {
resolvedSrc, err := filepath.EvalSymlinks(src)
if err != nil {
return fmt.Errorf("failed to resolve source directory %s: %w", src, err)
@@ -380,6 +391,8 @@ func copyDirectoryWithState(src, dst string, active map[string]bool) error {
active[resolvedSrc] = true
defer delete(active, resolvedSrc)
skipGit := opts != nil && opts.SkipGit
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -389,6 +402,9 @@ func copyDirectoryWithState(src, dst string, active map[string]bool) error {
dstPath := filepath.Join(dst, relPath)
if info.IsDir() {
if skipGit && info.Name() == ".git" {
return filepath.SkipDir
}
return os.MkdirAll(dstPath, info.Mode())
}
@@ -404,7 +420,7 @@ func copyDirectoryWithState(src, dst string, active map[string]bool) error {
if resolveErr != nil {
return fmt.Errorf("failed to resolve symlink directory %s: %w", path, resolveErr)
}
return copyDirectoryWithState(resolvedDir, dstPath, active)
return copyDirectoryWithState(resolvedDir, dstPath, active, opts)
}
}