{"record":{"id":"bab584b14262980e","repo":"gastownhall/beads","slug":"not-in-a-git-worktree-w","errorCode":null,"errorMessage":"not in a git worktree: %w","messagePattern":"not in a git worktree: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/worktree_cmd.go","lineNumber":1226,"sourceCode":"}\n\nfunc prepareWorktreeRemoval(\n\tctx context.Context,\n\tname string,\n\toptions *worktreeRemoveOptions,\n\tafterTargetResolution func() error,\n) (*worktreeRemovalPlan, error) {\n\tgitRunner, err := newWorktreeRemovalGit()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tcurrentDirectory, err := os.Getwd()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to resolve current directory: %w\", err)\n\t}\n\tcurrentRootOutput, err := gitRunner.output(ctx, currentDirectory, \"rev-parse\", \"--show-toplevel\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"not in a git worktree: %w\", err)\n\t}\n\tcurrentRoot := filepath.Clean(strings.TrimSpace(string(currentRootOutput)))\n\n\tworktrees, err := listRegisteredWorktrees(ctx, gitRunner, currentRoot)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tmainWorktree := worktrees[0]\n\tif mainWorktree.bare {\n\t\treturn nil, fmt.Errorf(\"cannot remove a worktree when the primary worktree is bare\")\n\t}\n\ttargetEntry, err := resolveRegisteredWorktree(name, currentRoot, mainWorktree.path, worktrees)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tplan := &worktreeRemovalPlan{\n\t\tforce: options.force.value,\n\t\tprepareFacts: worktreeremove.PrepareFacts{","sourceCodeStart":1208,"sourceCodeEnd":1244,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/worktree_cmd.go#L1208-L1244","documentation":"This error wraps a failure of `git rev-parse --show-toplevel` run in the current directory while building a worktree removal plan. It means bd could not determine the top-level of the current git worktree, so it cannot confirm the caller is inside a managed git worktree. bd throws it as a precondition for any worktree remove operation, since removal logic needs the current worktree root to resolve relative targets.","triggerScenarios":"Calling `bd worktree remove` (via buildWorktreeRemovalPlan at cmd/bd/worktree_cmd.go:1226) from a directory where `git rev-parse --show-toplevel` fails: outside any repository, inside a corrupt .git, or with git unavailable/misbehaving in the given directory.","commonSituations":"Running bd in a non-git directory; a broken or deleted .git directory; GIT_DIR/GIT_WORK_TREE env vars pointing to invalid locations; running from a path with permission problems so git cannot traverse it; a too-old or missing git binary.","solutions":["cd into a directory inside a valid git worktree (or the repository root) and re-run the command","Verify `git rev-parse --show-toplevel` succeeds manually in the current directory; fix the repo (e.g. restore .git) if it fails","Check for and unset stale GIT_DIR / GIT_WORK_TREE / GIT_COMMON_DIR environment variables","Confirm git is installed and on PATH, and that the directory is readable/executable"],"exampleFix":"// before: running from ~/notes (not a git repo)\n$ bd worktree remove feature-x\n// error: not in a git worktree: fatal: not a git repository...\n// after\n$ cd ~/src/myrepo && bd worktree remove feature-x","handlingStrategy":"validation","validationCode":"if !test -d .git && ! git rev-parse --show-toplevel >/dev/null 2>&1; then echo 'not in a git worktree'; exit 1; fi","typeGuard":"func inGitWorktree(dir string) bool {\n\tout, err := exec.Command(\"git\", \"-C\", dir, \"rev-parse\", \"--show-toplevel\").Output()\n\treturn err == nil && len(bytes.TrimSpace(out)) > 0\n}","tryCatchPattern":"root, err := findWorktreeRoot(ctx)\nif err != nil {\n\tvar wrapped *fmt.wrapError\n\tif errors.As(err, &wrapped) && strings.Contains(wrapped.Unwrap().Error(), \"not a git repository\") {\n\t\treturn fmt.Errorf(\"run bd from inside a git worktree\")\n\t}\n\treturn err\n}","preventionTips":["Always run bd worktree commands from inside a valid git worktree","Sanity-check with `git rev-parse --show-toplevel` in scripts before invoking bd","Unset GIT_DIR/GIT_WORK_TREE overrides in CI environments","Keep git installed and updated on machines running bd"],"tags":["git","worktree","cli"],"backgroundTag":"not-a-git-repository","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}