-
Notifications
You must be signed in to change notification settings - Fork 0
refuse to run when repo root is inside .worktrees #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,9 @@ func loadContext(opts runOptions) (string, string, config, []agentConfig, error) | |
| return "", "", config{}, nil, err | ||
| } | ||
| repoRoot := filepath.Dir(configPath) | ||
| if err := refuseWorktreeRoot(repoRoot); err != nil { | ||
| return "", "", config{}, nil, err | ||
| } | ||
|
yourconscience marked this conversation as resolved.
|
||
|
|
||
| cfg, err := loadConfig(repoRoot, home, configPath) | ||
| if err != nil { | ||
|
|
@@ -358,6 +361,34 @@ func absoluteExpandedPath(path string, home string) (string, error) { | |
| return abs, nil | ||
| } | ||
|
|
||
| // refuseWorktreeRoot rejects repo roots that are (or live under) a linked | ||
| // git worktree. sync materializes absolute links into the repo root; when | ||
| // the worktree is removed those links dangle and every managed skill | ||
| // breaks (ENOENT). The canonical checkout is the only valid root. | ||
| // | ||
| // Two detections: a linked worktree has a .git FILE pointing at its git | ||
| // dir (the canonical checkout has a .git directory or none), and the | ||
| // project convention places worktrees under .worktrees/, which catches | ||
| // roots whose .git check cannot run (missing dir, odd layouts). | ||
| func refuseWorktreeRoot(repoRoot string) error { | ||
| resolved := repoRoot | ||
| if real, err := filepath.EvalSymlinks(repoRoot); err == nil { | ||
| resolved = real | ||
| } | ||
| refuse := func() error { | ||
| return fmt.Errorf("refusing to run with repo root %s: it is (or lives inside) a linked git worktree; materialized links would dangle when the worktree is removed — run from the canonical checkout", repoRoot) | ||
| } | ||
| if fi, err := os.Stat(filepath.Join(resolved, ".git")); err == nil && fi.Mode().IsRegular() { | ||
| return refuse() | ||
|
Comment on lines
+381
to
+382
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an explicit config is in a subdirectory of an arbitrarily named linked worktree (for example, AGENTS.md reference: AGENTS.md:L5-L9 Useful? React with 👍 / 👎. |
||
| } | ||
| for _, seg := range strings.Split(filepath.ToSlash(resolved), "/") { | ||
| if seg == ".worktrees" { | ||
| return refuse() | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func expandPath(path string, home string) string { | ||
| path = strings.TrimSpace(path) | ||
| switch { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRefuseWorktreeRoot(t *testing.T) { | ||
| repo := t.TempDir() | ||
| if err := refuseWorktreeRoot(repo); err != nil { | ||
| t.Fatalf("canonical root must be accepted: %v", err) | ||
| } | ||
|
|
||
| worktree := filepath.Join(repo, ".worktrees", "sync-main") | ||
| if err := os.MkdirAll(worktree, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := refuseWorktreeRoot(worktree); err == nil { | ||
| t.Fatal("worktree root must be refused") | ||
| } else if !strings.Contains(err.Error(), "canonical checkout") { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| // A linked worktree outside .worktrees/ is caught by its .git file. | ||
| arbitrary := filepath.Join(t.TempDir(), "agents-test") | ||
| if err := os.MkdirAll(arbitrary, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(arbitrary, ".git"), []byte("gitdir: elsewhere\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := refuseWorktreeRoot(arbitrary); err == nil { | ||
| t.Fatal("linked worktree via .git file must be refused") | ||
| } | ||
|
|
||
| // A canonical checkout has a .git directory, which is allowed. | ||
| canonical := filepath.Join(t.TempDir(), "repo") | ||
| if err := os.MkdirAll(filepath.Join(canonical, ".git"), 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := refuseWorktreeRoot(canonical); err != nil { | ||
| t.Fatalf("canonical checkout with .git dir must be accepted: %v", err) | ||
| } | ||
|
|
||
| // A symlink aliasing a worktree root resolves to the same refusal. | ||
| alias := filepath.Join(t.TempDir(), "alias") | ||
| if err := os.Symlink(worktree, alias); err != nil { | ||
| t.Skipf("symlink unavailable: %v", err) | ||
| } | ||
| if err := refuseWorktreeRoot(alias); err == nil { | ||
| t.Fatal("symlinked worktree root must be refused") | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.