From 5941544a5ec0cb1e5dad292f0dc0c78232bca1a9 Mon Sep 17 00:00:00 2001 From: Bob Cotton Date: Sun, 11 Jan 2026 19:15:57 -0700 Subject: [PATCH] fix(init): prevent initialization from within git worktrees (#1026) Add check to prevent 'bd init' from running inside a git worktree. Worktrees should share the .beads database from the main repository, not create their own. The error message guides users to: 1. Run 'bd init' from the main repository 2. Use 'bd worktree create' to create worktrees with proper redirects This prevents the confusing behavior where init would create files in unexpected locations or fail with "pathspec '.beads/.gitignore' did not match any files" errors. According to docs/WORKTREES.md, the proper workflow is: - Initialize beads once in the main repository - Use 'bd worktree create' to create worktrees with redirect files - All worktrees share the single .beads/ database via redirects Co-Authored-By: Claude Sonnet 4.5 --- cmd/bd/init.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/bd/init.go b/cmd/bd/init.go index b0238465..a1891b5b 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -147,20 +147,31 @@ With --stealth: configures per-repository git settings for invisible beads usage if isGitRepo() { isWorktree = git.IsWorktree() } - var beadsDir string + + // Prevent initialization from within a worktree if isWorktree { - // For worktrees, .beads should be in the main repository root mainRepoRoot, err := git.GetMainRepoRoot() if err != nil { fmt.Fprintf(os.Stderr, "Error: failed to get main repository root: %v\n", err) os.Exit(1) } - beadsDir = filepath.Join(mainRepoRoot, ".beads") - } else { - // For regular repos, use current directory - beadsDir = filepath.Join(cwd, ".beads") + + fmt.Fprintf(os.Stderr, "Error: cannot run 'bd init' from within a git worktree\n\n") + fmt.Fprintf(os.Stderr, "Git worktrees share the .beads database from the main repository.\n") + fmt.Fprintf(os.Stderr, "To fix this:\n\n") + fmt.Fprintf(os.Stderr, " 1. Initialize beads in the main repository:\n") + fmt.Fprintf(os.Stderr, " cd %s\n", mainRepoRoot) + fmt.Fprintf(os.Stderr, " bd init\n\n") + fmt.Fprintf(os.Stderr, " 2. Then create worktrees with beads support:\n") + fmt.Fprintf(os.Stderr, " bd worktree create --branch \n\n") + fmt.Fprintf(os.Stderr, "For more information, see: https://github.com/steveyegge/beads/blob/main/docs/WORKTREES.md\n") + os.Exit(1) } + var beadsDir string + // For regular repos, use current directory + beadsDir = filepath.Join(cwd, ".beads") + // Prevent nested .beads directories // Check if current working directory is inside a .beads directory if strings.Contains(filepath.Clean(cwd), string(filepath.Separator)+".beads"+string(filepath.Separator)) ||