diff --git a/cmd/bd/hooks.go b/cmd/bd/hooks.go index 7b71c5c38..4954e29fe 100644 --- a/cmd/bd/hooks.go +++ b/cmd/bd/hooks.go @@ -17,6 +17,7 @@ import ( "github.com/steveyegge/beads/internal/git" "github.com/steveyegge/beads/internal/metrics" "github.com/steveyegge/beads/internal/ui" + "github.com/steveyegge/beads/internal/utils" ) // managedHookNames lists the git hooks managed by beads. @@ -1530,7 +1531,7 @@ func exportJSONLForCommit() { if exportPath == "" { exportPath = "issues.jsonl" } - fullPath := filepath.Join(beadsDir, exportPath) + fullPath := filepath.Join(hookJSONLDir(beadsDir), exportPath) // If the export file is staged for deletion (user ran `git rm`), do not // re-export or re-stage it. GIT_INDEX_FILE is set during an actual commit, @@ -1551,21 +1552,42 @@ func exportJSONLForCommit() { debug.Logf("pre-commit: exporting JSONL to %s\n", fullPath) warnJSONLWithoutDoltRemote("pre-commit auto-export") + // The destination directory is not guaranteed to exist: hookJSONLDir can + // retarget into a worktree that has no .beads yet, and a nested + // export.path ("exports/issues.jsonl") adds another level. atomicfile + // does not create parents, so without this every commit would warn and + // write nothing. + // 0o750 matches the permissions `bd init` gives a .beads directory. + if err := os.MkdirAll(filepath.Dir(fullPath), 0o750); err != nil { + fmt.Fprintf(os.Stderr, "beads: pre-commit export warning: %v\n", err) + return + } + // Shell out to `bd export` which initializes its own store. - // Clear BD_GIT_HOOK from the subprocess env so that its - // PersistentPostRun auto-export path does not also fire. + // Keep BD_GIT_HOOK=1 in the subprocess environment. The explicit export + // command must run, but its PersistentPostRun auto-export must stay + // suppressed: maybeAutoExport resolves its own destination straight from + // beads.FindBeadsDir(), so letting it fire writes the shared primary + // checkout's JSONL behind this hook's back (GH#6680). It cannot *stage* + // that file — gitAddFile's cross-worktree guard rejects a target outside + // the hook's worktree — but the stray write is exactly the unexplained + // `M .beads/issues.jsonl` users see in the primary checkout. // - // NOTE: we intentionally preserve GIT_DIR et al. in the subprocess - // env. The subprocess's PostRun eventually routes through the same - // gitAddFile as the parent, which relies on the inherited GIT_DIR to - // identify the hook's worktree and apply the cross-worktree staging - // guard (GH#3311 part 2). Scrubbing here would disable that guard. + // Keeping the variable set also leaves the subprocess's PersistentPostRun + // auto-backup suppressed (backup_auto.go), matching the parent hook + // process: the parent bd is already in hook context and skips it, so a + // pre-commit no longer takes a second backup of the same state. + // + // NOTE: we intentionally preserve GIT_DIR et al. in the subprocess env so + // it retains the hook's selected Git context. The parent stages fullPath + // after the subprocess returns, using that same context for the + // cross-worktree guard (GH#3311 part 2). // Run from the project root, not .beads/. Embedded Dolt discovery starts // from cwd, so cwd=.beads/ can make the export subprocess look for a // nested .beads/.beads workspace and warn on every commit (GH#3454). cmd := exec.Command("bd", "export", "-o", fullPath) cmd.Dir = exportSubprocessDir(beadsDir) - cmd.Env = filterEnv(os.Environ(), "BD_GIT_HOOK") + cmd.Env = hookSubprocessEnv(os.Environ()) cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { fmt.Fprintf(os.Stderr, "beads: pre-commit export warning: %v\n", err) @@ -1575,8 +1597,11 @@ func exportJSONLForCommit() { // Stage the exported file if configured. Skip when no-git-ops is set // (GH#3314). gitAddFile scrubs the inherited git hook env vars so git // rediscovers the repo from cwd, and silently skips when fullPath is - // outside the hook's worktree (the .beads/redirect case where fullPath - // points into the main repo, not this worktree). See GH#3311. + // outside the hook's worktree. That guard still matters here: hookJSONLDir + // only retargets the cross-worktree case it can fix, so a .beads/redirect + // to a shared out-of-repo store, a BEADS_DIR override, or a beads dir + // above the repository root all still resolve outside this worktree and + // must not be dropped into the index. See GH#3311. if config.GetBool("export.git-add") && !config.GetBool("no-git-ops") { if err := gitAddFile(fullPath); err != nil { debug.Logf("pre-commit: git add failed: %v\n", err) @@ -1584,6 +1609,176 @@ func exportJSONLForCommit() { } } +// hookJSONLDir returns the directory the running git hook should read and +// write the git-tracked JSONL snapshot in. It is the single destination +// resolver shared by the pre-commit export writer and the +// post-merge/post-checkout sync importer, so those two can never disagree +// about which copy of the file the hook owns. +// +// Database discovery and JSONL ownership are separate concerns (GH#6680): a +// linked worktree has no store of its own, so beads.FindBeadsDir deliberately +// falls back to the primary checkout's .beads directory, but the JSONL is a +// tracked file whose contents belong to the worktree being committed. +// +// Known asymmetry, deliberately out of scope for a hook fix: the non-hook +// auto-export lane (maybeAutoExport and its export-state.json throttle key) +// still resolves straight from beads.FindBeadsDir. It cannot share this +// resolver — outside a hook there is no GIT_DIR, hookWorkTreeRoot is empty, +// and this function is the identity. Giving ordinary `bd` commands in a linked +// worktree ownership of their own JSONL means resolving the worktree from cwd +// on every command, which changes JSONL ownership globally; that is a product +// decision rather than a hook fix, and is tracked separately. +func hookJSONLDir(beadsDir string) string { + hookRoot := hookWorkTreeRoot() + primaryRoot := hookLinkedWorktreePrimaryRoot(hookRoot) + jsonlDir := worktreeJSONLDir(beadsDir, hookRoot, primaryRoot) + // Log both outcomes. When the verdict goes the "keep beadsDir" way for a + // topology the user expected this fix to cover, the only other observable + // is the absence of a change — which is indistinguishable from the hook + // not running at all. + debug.Logf("hook: JSONL dir %s (retargeted=%t beadsDir=%s hookRoot=%s primaryRoot=%s)\n", + jsonlDir, jsonlDir != beadsDir, beadsDir, hookRoot, primaryRoot) + return jsonlDir +} + +// worktreeJSONLDir is the pure gating decision behind hookJSONLDir. +// +// The rewrite is deliberately narrow, because hookWorkTreeRoot is non-empty +// for *every* hook invocation — it returns filepath.Dir(GIT_DIR) for a plain, +// non-worktree repository too — so it cannot carry this decision alone. +// beadsDir is retargeted into the committing worktree only when all of: +// +// - the hook supplied a worktree root (hookRoot != ""); +// - that worktree is a linked one (primaryRoot != ""); +// - beadsDir is not already inside the committing worktree; +// - beadsDir lives inside the primary checkout — that is, exporting to it +// is precisely the cross-worktree write GH#6680 reported. +// +// Everything else keeps the discovered directory and so keeps pre-GH#6680 +// behavior: a .beads/redirect to a shared out-of-repo store, a BEADS_DIR +// override, and a beads dir above the repository root are not another working +// tree. Retargeting those would both strand the real JSONL and drop a +// brand-new in-repo copy into the user's commit, and gitAddFile's GH#3311 +// guard already keeps them out of the index. +// +// The destination mirrors beadsDir's path *relative to the primary checkout* +// rather than a hardcoded ".beads" or a bare leaf name, because the relative +// path is the one the committing worktree actually tracks: a store reached +// through a .beads/redirect to an in-repo "tools/store" must land at +// /tools/store, not at /store, which is a path no commit +// has ever contained. +// +// Which lane reaches that mirroring: the pre-commit export runs only when +// preCommitHasStagedBeadsFiles sees a staged path under its hardcoded ".beads" +// pathspec, unchanged from pre-GH#6680. A store that lives outside .beads — +// "beads-store", "nested/beads-store", an in-repo redirect target — normally +// stages nothing that gate matches, so on the export lane the mirror is a +// correctness bound rather than a new capability; the sync lane +// (importJSONLForSync) has no staged gate and does reach it. The gate blocked +// exactly the same commits at base, so this is a limit on the fix's reach and +// not a regression. Read the mirror as "the hook never writes a path the +// committing worktree does not track", not as "nested stores now auto-export +// on every commit". +// +// One case the gating deliberately does not catch: a /.beads that is +// itself a symlink pointing out of the repository. pathInsideDir resolves +// symlinks on the path's *parent* only (export_auto.go), so such a beadsDir +// still compares as inside primaryRoot and is retargeted, leaving the real +// store's JSONL stale — the outcome the .beads/redirect case above exists to +// prevent. The supported redirect mechanism is unaffected, because +// FollowRedirect hands back the resolved out-of-repo target whose parent is +// outside primaryRoot; only a hand-rolled `ln -s` is affected. Closing it +// means resolving beadsDir itself before both the gating and the lexical Rel +// below, which also moves the mixed-symlink-form case those two lines +// currently decline — a behavior change, so the case is named here instead. +func worktreeJSONLDir(beadsDir, hookRoot, primaryRoot string) string { + if hookRoot == "" || primaryRoot == "" { + return beadsDir + } + if pathInsideDir(beadsDir, hookRoot) || !pathInsideDir(beadsDir, primaryRoot) { + return beadsDir + } + // pathInsideDir resolves symlinks (on the parent); filepath.Rel is lexical, + // so the two can still disagree and hand back a path that escapes + // hookRoot. IsLocal + // rejects exactly those ("..", an absolute result), and rel == "." means + // the store *is* the checkout root — too degenerate to mirror. Both + // decline, which keeps pre-GH#6680 behavior rather than inventing a target. + rel, err := filepath.Rel(primaryRoot, beadsDir) + if err != nil || rel == "." || !filepath.IsLocal(rel) { + return beadsDir + } + return filepath.Join(hookRoot, rel) +} + +// hookLinkedWorktreePrimaryRoot returns the primary checkout's worktree root +// when hookRoot is a *linked* git worktree, and "" otherwise: the main +// checkout, a repository git cannot describe, or a layout whose common git dir +// is not a conventional "/.git". +// +// That last requirement is narrower than internal/beads' probe +// (worktreeFallbackBeadsDirForRepo), which classifies linked-ness as +// --git-dir != --git-common-dir and serves a bare-repo-backed worktree by +// sharing /.beads. The difference is deliberate: this function must +// name a primary *checkout* to mirror a tracked path out of, and a bare +// repository has none — /.beads is not inside any working tree, so +// there is no relative path to carry into the committing worktree. Worktrees +// of a `git clone --bare` therefore keep writing the shared copy and GH#6680 +// stays unfixed for that layout; where their JSONL should live is a product +// decision rather than something this hook fix can infer. +func hookLinkedWorktreePrimaryRoot(hookRoot string) string { + if hookRoot == "" { + return "" + } + cmd := exec.Command("git", "-C", hookRoot, "rev-parse", "--git-common-dir") + // Scrub the inherited GIT_* vars so git describes hookRoot itself rather + // than whatever repository GIT_DIR happens to name. + cmd.Env = scrubGitHookEnv(os.Environ()) + out, err := cmd.Output() + if err != nil { + debug.Logf("hook: failed to resolve git common dir for %s: %v\n", hookRoot, err) + return "" + } + commonDir := strings.TrimSpace(string(out)) + if commonDir == "" { + return "" + } + if !filepath.IsAbs(commonDir) { + commonDir = filepath.Join(hookRoot, commonDir) + } + if filepath.Base(commonDir) != ".git" { + return "" + } + primaryRoot, err := filepath.Abs(filepath.Dir(commonDir)) + if err != nil { + return "" + } + if utils.PathsEqual(primaryRoot, hookRoot) { + // The hook's worktree is the primary one; nothing to retarget. + return "" + } + return primaryRoot +} + +// hookSubprocessEnv builds the environment for the `bd` subprocesses a hook +// shells out to — `bd export` on the pre-commit lane, `bd import` on the sync +// lane. It forces BD_GIT_HOOK=1 rather than assuming the caller already +// exported it: the variable's whole job here is to keep the subprocess's +// PersistentPostRun auto-export (and auto-backup) suppressed, and a hook +// entrypoint that had not set it would leave nothing to suppress with. Git +// routing vars (GIT_DIR et al.) are deliberately preserved — see +// exportJSONLForCommit. +// +// Nothing on either the export or the import path reads BD_GIT_HOOK to decide +// whether to do its work: its only consumers are maybeAutoExport +// (export_auto.go), maybeAutoBackup (backup_auto.go), the first-run metrics +// notice (metrics.go) and terminal color (internal/ui). Setting it therefore +// suppresses only the post-run writes the hook is doing itself, never the +// explicit command. +func hookSubprocessEnv(env []string) []string { + return append(filterEnv(env, "BD_GIT_HOOK"), "BD_GIT_HOOK=1") +} + // isExportFileStagedForDeletion reports whether the beads export file at // fullPath is staged for deletion (the user ran `git rm` on it). When true, // exportJSONLForCommit must skip re-exporting and re-staging it: running @@ -1598,12 +1793,41 @@ func exportJSONLForCommit() { // here would make git fall back to the on-disk index and miss the // deletion. Reimplements gastownhall/beads#3838 (ckumar1). func isExportFileStagedForDeletion(fullPath string) bool { - checkCmd := exec.Command("git", "diff", "--cached", "--diff-filter=D", "--name-only", "--", filepath.Base(fullPath)) - checkCmd.Dir = filepath.Dir(fullPath) + // The export file's directory is not guaranteed to exist — hookJSONLDir + // can retarget into a worktree with no .beads yet, and a nested + // export.path adds another level. exec fails outright on a missing Dir, + // which would silently drop this guard, so run from the nearest existing + // ancestor and address the file relative to it. When the directory does + // exist this is identical to running in it with the bare file name. + dir := nearestExistingDir(filepath.Dir(fullPath)) + if dir == "" { + return false + } + rel, err := filepath.Rel(dir, fullPath) + if err != nil { + return false + } + checkCmd := exec.Command("git", "diff", "--cached", "--diff-filter=D", "--name-only", "--", rel) + checkCmd.Dir = dir out, _ := checkCmd.Output() return len(out) > 0 } +// nearestExistingDir walks up from dir to the first existing directory, +// returning "" if it reaches the filesystem root without finding one. +func nearestExistingDir(dir string) string { + for { + if info, err := os.Stat(dir); err == nil && info.IsDir() { + return dir + } + parent := filepath.Dir(dir) + if parent == dir { + return "" + } + dir = parent + } +} + func preCommitHasStagedBeadsFiles(beadsDir string) bool { cmdDir := exportSubprocessDir(beadsDir) if hookRoot := hookWorkTreeRoot(); hookRoot != "" { @@ -1628,14 +1852,19 @@ func exportSubprocessDir(beadsDir string) string { // import path. Existing projects may have customized export.path before // import.path existed, so keep importing from export.path unless import.path is // explicitly configured. -func syncImportJSONLPath(beadsDir string) string { +// +// jsonlDir is the hook-resolved JSONL directory (hookJSONLDir), which is not +// always the discovered beads dir: in a linked worktree the sync hook must +// import the copy `git pull` just refreshed in this worktree — the same copy +// the pre-commit hook exports — not the primary checkout's (GH#6680). +func syncImportJSONLPath(jsonlDir string) string { if config.GetValueSource("import.path") == config.SourceDefault { exportPath := config.GetString("export.path") if exportPath != "" { - return filepath.Join(beadsDir, exportPath) + return filepath.Join(jsonlDir, exportPath) } } - return configuredImportJSONLPath(beadsDir) + return configuredImportJSONLPath(jsonlDir) } // importJSONLForSync imports JSONL into Dolt after a git @@ -1647,6 +1876,21 @@ func syncImportJSONLPath(beadsDir string) string { // import is upsert; running it on an unchanged JSONL is a no-op (bd // import returns "Error 1105: nothing to commit", which we tolerate). // +// JSONL ownership on this lane, the decision GH#6680's fix makes here: the +// *source file* follows the committing worktree (hookJSONLDir below), while +// the *store* it feeds stays beads.FindBeadsDir()'s — the primary checkout's, +// shared by every linked worktree. The asymmetry is deliberate. git has just +// rewritten this worktree's copy of the tracked JSONL, so that is the only +// copy describing the pull/merge/checkout being imported; the primary's copy +// is some other branch's state. The consequence is that a linked worktree on +// a feature branch can introduce that branch's rows into the shared store. +// That is forward-only, not a rollback: the import never deletes, and it +// rewrites an existing row only when updated_at is strictly newer — a guard +// auto-import paths cannot switch off, since --allow-stale is settable only +// on an explicit `bd import` (import_shared.go). Narrower still, the +// resolveSyncRemote check above confines the whole lane to legacy projects +// with no Dolt remote. +// // See GH#3729. func importJSONLForSync(reason string) { if !config.GetBool("import.auto") { @@ -1662,7 +1906,7 @@ func importJSONLForSync(reason string) { return } - fullPath := syncImportJSONLPath(beadsDir) + fullPath := syncImportJSONLPath(hookJSONLDir(beadsDir)) if info, err := os.Stat(fullPath); err != nil || info.Size() == 0 { return @@ -1671,12 +1915,20 @@ func importJSONLForSync(reason string) { debug.Logf("%s: importing JSONL from %s\n", reason, fullPath) warnJSONLWithoutDoltRemote(reason + " JSONL import") - // Shell out to `bd import` — same pattern as exportJSONLForCommit. - // Clear BD_GIT_HOOK so the subprocess's own hook-detection logic - // doesn't suppress its work. + // Shell out to `bd import` — same pattern as exportJSONLForCommit, + // including BD_GIT_HOOK=1. No import path consults that variable, so + // clearing it suppressed nothing the import needs; all it did was let the + // subprocess's PersistentPostRun auto-export fire, and that export + // re-resolves its destination from beads.FindBeadsDir() — the *primary* + // checkout's JSONL. Harmless while this hook read the primary's copy too, + // but the source above is now the committing worktree's, so an unsuppressed + // post-run export would write one worktree's branch state over another + // checkout's tracked file: GH#6680's second symptom, from the fix for its + // first. Keeping the marker also suppresses the subprocess's auto-backup, + // matching the export lane (hookSubprocessEnv). cmd := exec.Command("bd", "import", "--quiet", fullPath) cmd.Dir = exportSubprocessDir(beadsDir) - cmd.Env = filterEnv(os.Environ(), "BD_GIT_HOOK") + cmd.Env = hookSubprocessEnv(os.Environ()) out, err := cmd.CombinedOutput() if err == nil { diff --git a/cmd/bd/hooks_export_worktree_test.go b/cmd/bd/hooks_export_worktree_test.go new file mode 100644 index 000000000..8f8bfeb4c --- /dev/null +++ b/cmd/bd/hooks_export_worktree_test.go @@ -0,0 +1,420 @@ +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 ` 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) + } +}