diff --git a/cmd/promptctl/main.go b/cmd/promptctl/main.go index 3b8fcda..bf77129 100644 --- a/cmd/promptctl/main.go +++ b/cmd/promptctl/main.go @@ -48,6 +48,7 @@ func main() { if len(os.Args) < 2 { usage() } + chdirRepoRoot() switch os.Args[1] { case "commit": commit(os.Args[2:]) @@ -190,7 +191,7 @@ func logCmd(args []string) { if len(args) != 1 { log.Fatal("usage: promptctl log ") } - path := filepath.ToSlash(args[0]) + path := repoPath(args[0]) r := openRepo() iter, err := r.Log(&git.LogOptions{FileName: &path, Order: git.LogOrderCommitterTime}) if err != nil { @@ -213,7 +214,7 @@ func promote(args []string) { if len(args) != 3 { log.Fatal("usage: promptctl promote ") } - path, from, to := filepath.ToSlash(args[0]), args[1], args[2] + path, from, to := repoPath(args[0]), args[1], args[2] r := openRepo() content := blobContent(treeAt(r, from), path) @@ -349,9 +350,17 @@ func publishCmd(args []string) { if *server == "" { log.Fatal("publish needs -server host:port") } - paths := fs.Args() + var paths []string + for _, a := range fs.Args() { + paths = append(paths, repoPath(a)) + } if len(paths) == 0 { - paths = diskPromptFiles() + // No arguments: the prompts under the directory promptctl was run in. + for _, p := range diskPromptFiles() { + if cwdInRepo == "" || strings.HasPrefix(p, filepath.ToSlash(cwdInRepo)+"/") { + paths = append(paths, p) + } + } } publishToServer(*server, *useTLS, *caCert, paths) } @@ -506,6 +515,50 @@ func promptUnion(tree *object.Tree) []string { // --- go-git helpers -------------------------------------------------------- +// cwdInRepo is the directory promptctl was started in, relative to the repo +// root ("" at the root or outside a repo). main chdirs to the root so prompt +// discovery, URIs and file writes are repo-relative wherever it runs; a prompt's +// URI comes from its repo path, so resolving it from a subdirectory would +// publish it under the wrong org. +var cwdInRepo string + +func chdirRepoRoot() { + r, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true}) + if err != nil { + return // not a repo: publish still works on plain files + } + wt, err := r.Worktree() + if err != nil { + return + } + root := wt.Filesystem.Root() + cwd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + rel, err := filepath.Rel(root, cwd) + if err != nil || rel == "." { + return + } + if err := os.Chdir(root); err != nil { + log.Fatal(err) + } + cwdInRepo = rel +} + +// repoPath turns a path argument, given relative to where promptctl was +// started (or absolute), into a repo-relative slash path. +func repoPath(arg string) string { + if filepath.IsAbs(arg) { + if root, err := os.Getwd(); err == nil { + if rel, err := filepath.Rel(root, arg); err == nil { + return filepath.ToSlash(rel) + } + } + } + return filepath.ToSlash(filepath.Join(cwdInRepo, arg)) +} + func openRepo() *git.Repository { r, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true}) if err != nil { diff --git a/cmd/promptctl/subdir_test.go b/cmd/promptctl/subdir_test.go new file mode 100644 index 0000000..57ee8db --- /dev/null +++ b/cmd/promptctl/subdir_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" +) + +// Running from a subdirectory must resolve prompts against the repo root: +// URIs come from the repo path, so acme/support/agent.prompt seen as +// support/agent.prompt would publish under org "support". +func TestSubdirectoryPathsResolveAgainstRepoRoot(t *testing.T) { + dir := t.TempDir() + t.Chdir(dir) + r, err := git.PlainInit(dir, false) + if err != nil { + t.Fatal(err) + } + wt, _ := r.Worktree() + if err := os.MkdirAll(filepath.Join("acme", "support"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join("acme", "support", "agent.prompt"), []byte("Hi {name}\n"), 0o644); err != nil { + t.Fatal(err) + } + wt.Add("acme") + if _, err := wt.Commit("init", &git.CommitOptions{Author: &object.Signature{Name: "t", Email: "t@t"}}); err != nil { + t.Fatal(err) + } + + t.Chdir(filepath.Join(dir, "acme", "support")) + t.Cleanup(func() { cwdInRepo = "" }) + chdirRepoRoot() + + if got := filepath.ToSlash(cwdInRepo); got != "acme/support" { + t.Fatalf("cwdInRepo = %q, want acme/support", got) + } + for _, arg := range []string{"agent.prompt", "./agent.prompt", filepath.Join(dir, "acme", "support", "agent.prompt")} { + p := repoPath(arg) + if p != "acme/support/agent.prompt" { + t.Errorf("repoPath(%q) = %q, want acme/support/agent.prompt", arg, p) + } + if uri := pathToURI(p); uri != "priompt://acme/support/agent" { + t.Errorf("pathToURI(%q) = %q, want priompt://acme/support/agent", p, uri) + } + } + var out bytes.Buffer + if v := runDiff(r, "HEAD", &out); len(v) != 0 || !bytes.Contains(out.Bytes(), []byte("no prompt changes")) { + t.Errorf("diff from a subdirectory on a clean tree: verdicts %v, output %q", v, out.String()) + } +}