fix(worktree): Cache git detection results to eliminate slowness (bd-7di)

In git worktrees, any bd command was slow with a 2-3s pause because:
- git.IsWorktree() was called 4+ times per command
- Each call spawned 2 git processes (git-dir and git-common-dir)
- git.GetRepoRoot() and git.GetMainRepoRoot() also called multiple times

Fix: Cache results using sync.Once since these values do not change during
a single command execution:
- IsWorktree() - caches worktree detection
- GetRepoRoot() - caches repo root path
- GetMainRepoRoot() - caches main repo root for worktrees

Added ResetCaches() for test cleanup between subtests that change directories.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-25 22:04:40 -08:00
parent df5d4b384c
commit 32c803dd16
3 changed files with 50 additions and 0 deletions

View File

@@ -5,6 +5,8 @@ import (
"os/exec"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/git"
)
func TestFindDatabasePathEnvVar(t *testing.T) {
@@ -821,6 +823,7 @@ func TestFindGitRoot_RegularRepo(t *testing.T) {
}
t.Chdir(subDir)
git.ResetCaches() // Reset after chdir for caching tests
// findGitRoot should return the repo root
result := findGitRoot()
@@ -897,6 +900,7 @@ func TestFindGitRoot_Worktree(t *testing.T) {
// Change to the worktree directory
t.Chdir(worktreeDir)
git.ResetCaches() // Reset after chdir for caching tests
// findGitRoot should return the WORKTREE root, not the main repo root
result := findGitRoot()
@@ -927,6 +931,7 @@ func TestFindGitRoot_NotGitRepo(t *testing.T) {
defer os.RemoveAll(tmpDir)
t.Chdir(tmpDir)
git.ResetCaches() // Reset after chdir for caching tests
// findGitRoot should return empty string
result := findGitRoot()
@@ -1024,6 +1029,7 @@ func TestFindBeadsDir_Worktree(t *testing.T) {
// Change to worktree
t.Chdir(worktreeDir)
git.ResetCaches() // Reset after chdir for caching tests
// FindBeadsDir should prioritize the main repo's .beads for worktrees (bd-de6)
result := FindBeadsDir()
@@ -1134,6 +1140,7 @@ func TestFindDatabasePath_Worktree(t *testing.T) {
t.Fatal(err)
}
t.Chdir(worktreeSubDir)
git.ResetCaches() // Reset after chdir for caching tests
// FindDatabasePath should find the main repo's shared database
result := FindDatabasePath()
@@ -1241,6 +1248,7 @@ func TestFindDatabasePath_WorktreeNoLocalDB(t *testing.T) {
// Change to worktree
t.Chdir(worktreeDir)
git.ResetCaches() // Reset after chdir for caching tests
// FindDatabasePath should find the main repo's shared database
result := FindDatabasePath()