fix(export): stage basename under cmd.Dir to avoid double-root git add (GH#4351) (#5121)

* fix(export): stage basename under cmd.Dir to avoid double-root git add (GH#4351)

gitAddFile set cmd.Dir to the parent of path but passed the full path as the
git-add argument. For a relative path like .beads/issues.jsonl that became
`cd .beads && git add .beads/issues.jsonl`, which resolves under
.beads/.beads/ and exits 128 — leaving issues.jsonl dirty after every
auto-export.

Pass filepath.Base(path) (with --) so the pathspec is relative to cmd.Dir.
Keeps Dir=parent so GH#3311 hook worktree staging still lands under .beads/.

* docs(export): clarify double-root guard as defensive, not live behavior

Per contributor request on gastownhall/beads#5121: the gitAddFile
comment near the basename/cmd.Dir split read as if the double-root
staging bug is currently reachable. Review traced both callers and
found they always pass absolute paths, so the failure this code
guards against cannot fire today. Reword to describe the guard as
defensive against a future relative-path caller rather than a fix
for observed live behavior.

Agent-Signature: claude-code-sonnet-medium on behalf of matt wilkie

---------

Co-authored-by: matt wilkie <maphew@gmail.com>
This commit is contained in:
Vishnu J 2026-07-31 01:44:24 -07:00 • committed by GitHub
commit 91de2908d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 80 additions and 1 deletions

View file

@ -691,7 +691,15 @@ func gitAddFile(path string) error {
ctx, cancel := context.WithTimeout(context.Background(), gitAddTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "git", "add", path)
// Pass the basename only, defensively: cmd.Dir is the parent of path, so
// a full path argument would double-root (cd .beads && git add
// .beads/issues.jsonl → pathspec looks under .beads/.beads/) if a caller
// ever passed a relative path here. Both current callers pass absolute
// paths, so this guards against a regression rather than fixing a live
// failure. See GH#4351.
// Keep cmd.Dir = parent so GH#3311 hook worktree staging still resolves
// the index path under the repo root (not bare "issues.jsonl" at root).
cmd := exec.CommandContext(ctx, "git", "add", "--", filepath.Base(path))
cmd.Dir = filepath.Dir(path)
cmd.Env = env
// Capture combined output so the caller's warning surfaces git's stderr

View file

@ -642,6 +642,77 @@ func TestGitAddFile_NonHookContext_GuardDoesNotFire(t *testing.T) {
}
}
// TestGitAddFile_RelativePathDoesNotDoubleRoot is a regression test for
// GH#4351: gitAddFile sets cmd.Dir to filepath.Dir(path). When path is
// relative (e.g. ".beads/issues.jsonl"), passing that full path as the
// git-add argument becomes `cd .beads && git add .beads/issues.jsonl`,
// which looks for a non-existent nested path and exits 128. The fix is
// to pass filepath.Base(path) so the pathspec is relative to cmd.Dir.
func TestGitAddFile_RelativePathDoesNotDoubleRoot(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-gh4351-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
repo := filepath.Join(tmpDir, "repo")
if err := os.MkdirAll(repo, 0o755); err != nil {
t.Fatal(err)
}
runGit := func(args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = repo
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
runGit("init", "-q")
runGit("config", "user.email", "t@t")
runGit("config", "user.name", "t")
if err := os.WriteFile(filepath.Join(repo, "README.md"), []byte("x\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit("add", "README.md")
runGit("commit", "-qm", "init")
if err := os.MkdirAll(filepath.Join(repo, ".beads"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repo, ".beads", "issues.jsonl"), []byte(`{"id":"x"}`+"\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Unsetenv("GIT_DIR"); err != nil {
t.Fatal(err)
}
// Repo-relative path — the double-root trigger. Must stage successfully.
t.Chdir(repo)
relPath := filepath.Join(".beads", "issues.jsonl")
if err := gitAddFile(relPath); err != nil {
t.Fatalf("gitAddFile(%q): %v", relPath, err)
}
c := exec.Command("git", "diff", "--cached", "--name-only")
c.Dir = repo
data, err := c.CombinedOutput()
if err != nil {
t.Fatalf("git diff --cached: %v\n%s", err, data)
}
staged := strings.TrimSpace(string(data))
if !strings.Contains(staged, ".beads/issues.jsonl") && !strings.Contains(staged, filepath.ToSlash(relPath)) {
t.Errorf("expected .beads/issues.jsonl staged, got: %q", staged)
}
}
// TestGitAddFile_CapturesStderrOnFailure verifies that when `git add` fails,
// the returned error wraps git's stderr text instead of just the bare exit
// status. Regression guard for the silent "Warning: auto-export: git add