beads/cmd/bd/hooks_export_worktree_test.go
Rongjun GENG 343157e1dc
fix(hooks): export from linked worktree (#6717)
* fix(hooks): export from linked worktree

* fix(hooks): scope the worktree export retarget and share it with the importer

Maintainer review fixups for #6717 (fixes #6680).

Blocker — the pre-commit export retarget was unconditional.
preCommitExportPath rewrote the destination whenever hookWorkTreeRoot() was
non-empty, but that is true for *every* hook invocation (it returns
filepath.Dir(GIT_DIR) for a plain repo too, pinned by export_auto_test.go's
"plain repo" case). Any topology whose discovered beads dir is not
<repoRoot>/.beads — a .beads/redirect to a shared out-of-repo store, a
BEADS_DIR override, a beads dir above the repo root — silently stopped
refreshing its real JSONL and instead got a brand-new in-repo dump that,
being inside the worktree, passed gitAddFile's GH#3311 guard and was staged
into the user's commit.

worktreeJSONLDir now retargets only when the hook belongs to a *linked*
worktree and the discovered beads dir lives inside the primary checkout —
exactly the cross-worktree write #6680 reported. Everything else keeps the
discovered directory and pre-#6680 behavior. The destination mirrors the
beads dir's path *relative to the primary checkout*: a store renamed at the
top level or nested under one (a relative .beads/redirect to tools/store)
lands at the path the committing worktree actually tracks, rather than at a
collapsed leaf no commit has ever contained. A lexical result that would
escape the worktree, and a store that *is* the checkout root, both decline.

Major — only the writer had moved. syncImportJSONLPath still imported
<primary>/.beads/issues.jsonl while the hook wrote <worktree>/..., so a
git pull in a linked worktree could import a JSONL the hook never updates.
Both hook lanes now go through one resolver, hookJSONLDir. The non-hook
auto-export lane (maybeAutoExport and its export-state.json key) is
deliberately left alone and documented: it has no GIT_DIR to resolve a
worktree from, and changing it means resolving the worktree from cwd for
every bd command — a product decision, not a hook fix.

Major — moving that reader made the importer's own subprocess harmful.
`bd import` ran with BD_GIT_HOOK cleared, so its PersistentPostRun
auto-export re-resolved the destination from beads.FindBeadsDir() and wrote
<primary>/.beads/issues.jsonl. That was content-neutral while both halves
of the sync path read the primary's copy; with the source moved to the
worktree, a post-merge in a linked worktree writes one branch's issue state
over another checkout's tracked file — #6680's second symptom, out of the
fix for its first. No import path consults BD_GIT_HOOK (its only consumers
are auto-export, auto-backup, the first-run metrics notice and terminal
color), so the comment justifying the clear was false in exactly the way
this commit already proved the export-side comment was. Both subprocesses
now share hookSubprocessEnv.

Also:
- create the destination directory before exporting; atomicfile does not
  create parents, so a retargeted or nested export.path warned on every
  commit and wrote nothing.
- run isExportFileStagedForDeletion from the nearest existing ancestor so a
  missing directory can no longer make exec fail and silently drop the
  GH#3838 `git rm` guard.
- hookSubprocessEnv (was preCommitExportEnv) forces BD_GIT_HOOK=1 rather
  than being an identity copy that assumes the caller set it, and now
  serves both hook subprocesses.
- record that keeping BD_GIT_HOOK also suppresses the subprocess
  auto-backup, and correct the comments that claimed the suppressed export
  could stage the primary's JSONL (gitAddFile's guard stops it) and that
  the redirect case was unreachable.
- scope the hookLinkedWorktreePrimaryRoot comment to <root>/.git layouts.
  It is deliberately narrower than internal/beads' probe, which also serves
  bare-repo-backed worktrees: mirroring a tracked path needs a primary
  checkout and a bare repository has none, so #6680 stays unfixed for that
  layout and the comment now says so instead of claiming parity.
- log the resolver's inputs and verdict, so a "keep beadsDir" outcome is
  observable under BD_DEBUG rather than indistinguishable from the hook not
  running at all.

- document the sync lane's JSONL ownership decision, which this commit
  makes but did not state: the source file follows the committing worktree
  while the store it feeds stays the primary checkout's shared one, so a
  linked worktree on a feature branch can introduce that branch's rows
  there. Forward-only — the import never deletes and rewrites a row only
  when updated_at is strictly newer, a guard auto-import paths cannot
  switch off.
- name, in worktreeJSONLDir, which lane actually reaches the mirrored
  non-.beads topologies. preCommitHasStagedBeadsFiles still gates the
  export lane on a hardcoded ".beads" pathspec (byte-identical to base), so
  a renamed or nested store normally stages nothing it matches and the
  mirror is reached by the sync lane; the comment is a correctness bound,
  not "nested stores now auto-export on every commit".
- name the one case the gating does not catch: a <primary>/.beads that is
  itself a symlink out of the repository. pathInsideDir resolves symlinks
  on the parent only, so it still compares as inside the primary and gets
  retargeted. The supported .beads/redirect mechanism is unaffected;
  closing the hand-rolled `ln -s` case means resolving beadsDir before both
  the gating and the lexical Rel, which also moves the mixed-symlink-form
  case they currently decline.

- keep this file's fixture helper out of the //go:build cgo namespace.
  writeFile lives in explicit_db_nodb_test.go, which the pure-Go
  (CGO_ENABLED=0) and native-Windows lanes exclude, so referencing it from
  this unconstrained file failed to compile the whole cmd/bd test binary
  there. Use a local writeFileForWorktreeTest, matching the suffixed naming
  the rest of this file already uses for the same collision reason.

Tests: a gating matrix over the redirect / BEADS_DIR / already-inside /
renamed / nested / checkout-root topologies; a real `git worktree add`
fixture driving hookJSONLDir; and a wiring test that stands a stub `bd` on
PATH and drives both call sites — asserting the pre-commit export writes
and stages the worktree's JSONL while the primary's copy stays
byte-identical, and that the post-merge import reads the worktree's copy
with BD_GIT_HOOK=1. Reverting either call site to the discovered beads dir
— the whole user-visible fix, which the earlier helper-only tests let pass
— fails there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-26 15:43:45 -07:00

420 lines
16 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"github.com/steveyegge/beads/internal/config"
)
// mkdirs creates every directory so pathInsideDir can resolve symlinks on both
// sides (macOS /tmp is a symlink, and an unresolvable side compares as outside).
func mkdirs(t *testing.T, dirs ...string) {
t.Helper()
for _, dir := range dirs {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
}
}
// runGitForWorktreeTest runs git in dir, failing the test on error. The env is
// scrubbed because the test process may itself be running under a git hook.
func runGitForWorktreeTest(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = scrubGitHookEnv(os.Environ())
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
// TestWorktreeJSONLDir covers the gating matrix for the GH#6680 retarget.
// The rewrite must fire for a linked worktree sharing the primary checkout's
// store and for nothing else: hookWorkTreeRoot() is non-empty for every hook
// run, so an unconditional rewrite would hijack .beads/redirect, BEADS_DIR and
// above-the-repo topologies that resolve outside the committing worktree.
func TestWorktreeJSONLDir(t *testing.T) {
root := t.TempDir()
primary := filepath.Join(root, "main")
worktree := filepath.Join(root, "wt")
outside := filepath.Join(root, "shared-store", ".beads")
mkdirs(t,
filepath.Join(primary, ".beads"),
filepath.Join(primary, "beads-store"),
filepath.Join(primary, "nested", "beads-store"),
filepath.Join(worktree, ".beads"),
outside,
)
tests := []struct {
name string
beadsDir string
hookRoot string
primaryRoot string
want string
}{
{
name: "linked worktree sharing the primary store retargets",
beadsDir: filepath.Join(primary, ".beads"),
hookRoot: worktree,
primaryRoot: primary,
want: filepath.Join(worktree, ".beads"),
},
{
name: "a renamed top-level store mirrors to the same name",
beadsDir: filepath.Join(primary, "beads-store"),
hookRoot: worktree,
primaryRoot: primary,
want: filepath.Join(worktree, "beads-store"),
},
{
// The path the worktree tracks is nested/beads-store; mirroring
// only the leaf would dump the store at a path no commit has ever
// contained, and leave the tracked copy stale.
name: "a nested store keeps its path relative to the primary checkout",
beadsDir: filepath.Join(primary, "nested", "beads-store"),
hookRoot: worktree,
primaryRoot: primary,
want: filepath.Join(worktree, "nested", "beads-store"),
},
{
// beadsDir == primaryRoot: the store *is* the checkout root, so
// there is no relative path to mirror. Decline rather than hand
// back the worktree root itself.
name: "a store at the checkout root is never retargeted",
beadsDir: primary,
hookRoot: worktree,
primaryRoot: primary,
want: primary,
},
{
name: "worktree owning its beads dir keeps it",
beadsDir: filepath.Join(worktree, ".beads"),
hookRoot: worktree,
primaryRoot: primary,
want: filepath.Join(worktree, ".beads"),
},
{
name: "plain repo keeps its discovered beads dir",
beadsDir: filepath.Join(primary, ".beads"),
hookRoot: primary,
primaryRoot: "",
want: filepath.Join(primary, ".beads"),
},
{
name: "plain repo redirecting out of the repo is never retargeted",
beadsDir: outside,
hookRoot: primary,
primaryRoot: "",
want: outside,
},
{
name: "linked worktree redirecting out of the repo is never retargeted",
beadsDir: outside,
hookRoot: worktree,
primaryRoot: primary,
want: outside,
},
{
name: "no hook context keeps the discovered beads dir",
beadsDir: outside,
hookRoot: "",
primaryRoot: "",
want: outside,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := worktreeJSONLDir(tt.beadsDir, tt.hookRoot, tt.primaryRoot)
if got != tt.want {
t.Fatalf("worktreeJSONLDir(%q, %q, %q) = %q, want %q",
tt.beadsDir, tt.hookRoot, tt.primaryRoot, got, tt.want)
}
})
}
}
// TestHookJSONLDirWithRealWorktrees pins the wiring, not just the helpers:
// it builds a real `git worktree add` fixture, enters it the way git enters a
// hook (GIT_DIR exported), and drives hookJSONLDir end to end. A regression
// that stops detecting the linked worktree — or that starts retargeting the
// primary checkout — fails here even though worktreeJSONLDir is untouched.
func TestHookJSONLDirWithRealWorktrees(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
root := t.TempDir()
root, err := filepath.EvalSymlinks(root)
if err != nil {
t.Fatal(err)
}
primary := filepath.Join(root, "main")
mkdirs(t, filepath.Join(primary, ".beads"))
runGitForWorktreeTest(t, primary, "init", "-q", "-b", "main")
runGitForWorktreeTest(t, primary, "config", "user.email", "test@example.com")
runGitForWorktreeTest(t, primary, "config", "user.name", "test")
runGitForWorktreeTest(t, primary, "commit", "-q", "--allow-empty", "-m", "init")
worktree := filepath.Join(root, "wt")
runGitForWorktreeTest(t, primary, "worktree", "add", "-q", worktree, "-b", "wt")
primaryBeads := filepath.Join(primary, ".beads")
// A pre-commit hook in the linked worktree: GIT_DIR points at the
// per-worktree admin dir, and FindBeadsDir has fallen back to the
// primary checkout's shared .beads.
t.Setenv("GIT_DIR", filepath.Join(primary, ".git", "worktrees", "wt"))
if got, want := hookJSONLDir(primaryBeads), filepath.Join(worktree, ".beads"); got != want {
t.Errorf("linked worktree: hookJSONLDir(%q) = %q, want %q", primaryBeads, got, want)
}
// The same hook in the primary checkout must be left alone.
t.Setenv("GIT_DIR", filepath.Join(primary, ".git"))
if got := hookJSONLDir(primaryBeads); got != primaryBeads {
t.Errorf("primary checkout: hookJSONLDir(%q) = %q, want it unchanged", primaryBeads, got)
}
// A redirect target outside the repository stays put even from the
// linked worktree — retargeting it would strand the real store and add
// an untracked dump to the commit.
outside := filepath.Join(root, "shared-store", ".beads")
mkdirs(t, outside)
t.Setenv("GIT_DIR", filepath.Join(primary, ".git", "worktrees", "wt"))
if got := hookJSONLDir(outside); got != outside {
t.Errorf("out-of-repo store: hookJSONLDir(%q) = %q, want it unchanged", outside, got)
}
}
// TestHookLinkedWorktreePrimaryRootNonRepo verifies the probe declines rather
// than guessing when it cannot describe the directory.
func TestHookLinkedWorktreePrimaryRootNonRepo(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
if got := hookLinkedWorktreePrimaryRoot(""); got != "" {
t.Errorf("empty hookRoot: got %q, want \"\"", got)
}
// GIT_CEILING_DIRECTORIES stops discovery from walking out of the temp
// dir into a real repository that may enclose it.
dir := t.TempDir()
t.Setenv("GIT_CEILING_DIRECTORIES", filepath.Dir(dir))
if got := hookLinkedWorktreePrimaryRoot(dir); got != "" {
t.Errorf("non-repository hookRoot: got %q, want \"\"", got)
}
}
// TestHookSubprocessEnvForcesHookSuppression pins the invariant the helper is
// named for: the `bd` subprocesses a hook shells out to must always see
// BD_GIT_HOOK=1 so their PersistentPostRun auto-export (and auto-backup) stay
// suppressed.
func TestHookSubprocessEnvForcesHookSuppression(t *testing.T) {
t.Run("preserves an already-set marker exactly once", func(t *testing.T) {
got := hookSubprocessEnv([]string{"PATH=/bin", "BD_GIT_HOOK=1", "GIT_DIR=/repo/.git"})
if !slices.Contains(got, "BD_GIT_HOOK=1") {
t.Fatalf("hookSubprocessEnv() = %#v, want BD_GIT_HOOK=1", got)
}
if !slices.Contains(got, "GIT_DIR=/repo/.git") {
t.Fatalf("hookSubprocessEnv() = %#v, want git routing preserved", got)
}
count := 0
for _, e := range got {
if e == "BD_GIT_HOOK=1" {
count++
}
}
if count != 1 {
t.Fatalf("hookSubprocessEnv() = %#v, want exactly one BD_GIT_HOOK entry", got)
}
})
t.Run("sets the marker when the caller did not", func(t *testing.T) {
got := hookSubprocessEnv([]string{"PATH=/bin"})
if !slices.Contains(got, "BD_GIT_HOOK=1") {
t.Fatalf("hookSubprocessEnv() = %#v, want BD_GIT_HOOK=1 forced", got)
}
})
t.Run("overrides a conflicting value", func(t *testing.T) {
got := hookSubprocessEnv([]string{"BD_GIT_HOOK=0"})
if slices.Contains(got, "BD_GIT_HOOK=0") {
t.Fatalf("hookSubprocessEnv() = %#v, want BD_GIT_HOOK=0 replaced", got)
}
if !slices.Contains(got, "BD_GIT_HOOK=1") {
t.Fatalf("hookSubprocessEnv() = %#v, want BD_GIT_HOOK=1", got)
}
})
}
// TestHookLanesUseTheWorktreeJSONL pins the two call sites hookJSONLDir was
// added for, which the resolver's own tests cannot reach: reverting either of
// them to the discovered beadsDir — the whole user-visible fix — leaves every
// other test in this file green.
//
// A stub `bd` on PATH stands in for the real binary so the hook's subprocess
// is observable without a store: it records its argv and the BD_GIT_HOOK it
// inherited, and emulates `bd export -o <path>` by writing that file.
func TestHookLanesUseTheWorktreeJSONL(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell-script stub binary is POSIX-only")
}
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
primary := filepath.Join(root, "main")
primaryBeads := filepath.Join(primary, ".beads")
primaryJSONL := filepath.Join(primaryBeads, "issues.jsonl")
// BEADS_DIR is only honored for a directory that looks like a project.
writeFileForWorktreeTest(t, filepath.Join(primaryBeads, "metadata.json"), []byte("{}\n"))
writeFileForWorktreeTest(t, primaryJSONL, []byte(`{"id":"bd-primary"}`+"\n"))
runGitForWorktreeTest(t, primary, "init", "-q", "-b", "main")
runGitForWorktreeTest(t, primary, "config", "user.email", "test@example.com")
runGitForWorktreeTest(t, primary, "config", "user.name", "test")
runGitForWorktreeTest(t, primary, "commit", "-q", "--allow-empty", "-m", "init")
worktree := filepath.Join(root, "wt")
runGitForWorktreeTest(t, primary, "worktree", "add", "-q", worktree, "-b", "wt")
worktreeJSONL := filepath.Join(worktree, ".beads", "issues.jsonl")
writeFileForWorktreeTest(t, worktreeJSONL, []byte(`{"id":"bd-worktree"}`+"\n"))
// The pre-commit export only runs when the commit already touches .beads.
// Stage a sibling for that gate so the JSONL's own staging stays a real
// assertion below rather than something the fixture did.
writeFileForWorktreeTest(t, filepath.Join(worktree, ".beads", "metadata.json"), []byte("{}\n"))
runGitForWorktreeTest(t, worktree, "add", ".beads/metadata.json")
binDir := t.TempDir()
sentinel := filepath.Join(binDir, "invocations")
quotedSentinel := "'" + strings.ReplaceAll(sentinel, "'", `'\''`) + "'"
stub := "#!/bin/sh\n" +
`printf '%s BD_GIT_HOOK=%s\n' "$*" "${BD_GIT_HOOK-unset}" >> ` + quotedSentinel + "\n" +
`if [ "$1" = export ] && [ "$2" = -o ]; then printf '%s\n' '{"id":"bd-exported"}' > "$3"; fi` + "\n" +
"exit 0\n"
if err := os.WriteFile(filepath.Join(binDir, "bd"), []byte(stub), 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
// Prove the stub is reachable and records, else nothing below can fail.
if err := exec.Command("bd", "probe").Run(); err != nil {
t.Fatalf("stub bd must be runnable from PATH: %v", err)
}
if _, err := os.Stat(sentinel); err != nil {
t.Fatalf("stub bd must record its invocations: %v", err)
}
initConfigForTest(t)
config.Set("export.auto", true)
config.Set("export.git-add", true)
config.Set("export.path", "issues.jsonl")
config.Set("no-git-ops", false)
config.Set("import.auto", true)
config.Set("sync.remote", "")
config.Set("sync.git-remote", "")
// Enter the linked worktree the way git enters a hook: GIT_DIR names the
// per-worktree admin dir, and store discovery has fallen back to the
// primary checkout's shared .beads.
t.Setenv("GIT_DIR", filepath.Join(primary, ".git", "worktrees", "wt"))
t.Setenv("BEADS_DIR", primaryBeads)
t.Run("pre-commit exports the worktree's copy and leaves the primary's alone", func(t *testing.T) {
if err := os.RemoveAll(sentinel); err != nil {
t.Fatal(err)
}
_ = captureHookStderr(t, exportJSONLForCommit)
if got, want := readFileForWorktreeTest(t, sentinel), "export -o "+worktreeJSONL+" BD_GIT_HOOK=1\n"; got != want {
t.Fatalf("export subprocess = %q, want %q", got, want)
}
if got, want := readFileForWorktreeTest(t, worktreeJSONL), `{"id":"bd-exported"}`+"\n"; got != want {
t.Errorf("worktree JSONL = %q, want the exported %q", got, want)
}
if got, want := readFileForWorktreeTest(t, primaryJSONL), `{"id":"bd-primary"}`+"\n"; got != want {
t.Errorf("primary JSONL = %q, want it untouched %q", got, want)
}
if staged := stagedPathsForWorktreeTest(t, worktree); !slices.Contains(staged, ".beads/issues.jsonl") {
t.Errorf("worktree staged paths = %v, want .beads/issues.jsonl among them", staged)
}
if staged := stagedPathsForWorktreeTest(t, primary); len(staged) != 0 {
t.Errorf("primary staged paths = %v, want none", staged)
}
})
t.Run("post-merge imports the worktree's copy with the hook marker kept", func(t *testing.T) {
if err := os.RemoveAll(sentinel); err != nil {
t.Fatal(err)
}
_ = captureHookStderr(t, func() { importJSONLForSync("post-merge") })
// BD_GIT_HOOK=1 is half the assertion: clearing it lets the import
// subprocess's own PersistentPostRun auto-export write the primary's
// JSONL from the worktree's branch state (GH#6680, second symptom).
if got, want := readFileForWorktreeTest(t, sentinel), "import --quiet "+worktreeJSONL+" BD_GIT_HOOK=1\n"; got != want {
t.Fatalf("import subprocess = %q, want %q", got, want)
}
})
}
// writeFileForWorktreeTest creates path's parents and writes content. This
// duplicates the package's writeFile helper on purpose: that one lives in a
// //go:build cgo test file, and these tests must still compile in the pure-Go
// (CGO_ENABLED=0) and Windows lanes, which exclude it.
func writeFileForWorktreeTest(t *testing.T, path string, content []byte) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir parent for %s: %v", path, err)
}
if err := os.WriteFile(path, content, 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func readFileForWorktreeTest(t *testing.T, path string) string {
t.Helper()
body, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return string(body)
}
// stagedPathsForWorktreeTest lists the paths staged in dir's index, so a test
// can tell "the hook staged this" from "the fixture did".
func stagedPathsForWorktreeTest(t *testing.T, dir string) []string {
t.Helper()
cmd := exec.Command("git", "diff", "--cached", "--name-only")
cmd.Dir = dir
cmd.Env = scrubGitHookEnv(os.Environ())
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git diff --cached in %s: %v\n%s", dir, err, out)
}
return strings.Fields(strings.TrimSpace(string(out)))
}
// TestNearestExistingDir pins the fallback that keeps the GH#3838 staged-
// deletion guard alive when the export directory does not exist yet.
func TestNearestExistingDir(t *testing.T) {
root := t.TempDir()
if got := nearestExistingDir(root); got != root {
t.Errorf("existing dir: got %q, want %q", got, root)
}
missing := filepath.Join(root, "a", "b", "c")
if got := nearestExistingDir(missing); got != root {
t.Errorf("missing dir: got %q, want %q", got, root)
}
}