// Package plugin clones the repositories that hold Claude Code // plugins, for a Mandate's turns and for a Console's sessions. Each // repository holds one plugin at its root: .claude-plugin/plugin.json // and skills/. package plugin import ( "context" "fmt" "os" "os/exec" "path/filepath" "strings" "tangled.org/guid.foo/simmer/dataplane" ) // paths are the only paths of a plugin's repository that a clone // checks out: Claude Code's manifest and the skills. Non-cone patterns // anchor each one at the root, where cone mode would also check out // every file at the root. var paths = []string{"/.claude-plugin/", "/skills/"} // Dirs answers the directory under root that each plugin's repository // is cloned into, named for the repository. Claude Code names a plugin // for its manifest, so two repositories with one name would load two // plugins under one name, and Dirs refuses them. func Dirs(root string, repositories []string) ([]string, error) { var dirs []string seen := map[string]bool{} for _, repository := range repositories { name, err := dataplane.RepositoryName(repository) if err != nil { return nil, err } if seen[name] { return nil, fmt.Errorf("two plugins are both named %s", name) } seen[name] = true dirs = append(dirs, filepath.Join(root, name)) } return dirs, nil } // Clone clones a plugin's repository into dir. The rest of a plugin's // repository may be private, so the clone fetches no file contents up // front and then checks out only the manifest and the skills: the // other files' contents never reach a session. A shallow clone keeps // the history out too. func Clone(ctx context.Context, repository, dir string) error { if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil { return err } if err := git(ctx, "clone", "--quiet", "--depth", "1", "--filter=blob:none", "--no-checkout", repository, dir); err != nil { return err } if err := git(ctx, append([]string{"-C", dir, "sparse-checkout", "set", "--no-cone"}, paths...)...); err != nil { return err } return git(ctx, "-C", dir, "checkout", "--quiet") } // git runs git with the process's environment, so the GIT_SSH_COMMAND // that a pod's template sets carries the forge's key. func git(ctx context.Context, args ...string) error { output, err := exec.CommandContext(ctx, "git", args...).CombinedOutput() if err != nil { return fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(string(output))) } return nil }