fix(worktree): resolve worktrees by name from git registry (#921)

This commit is contained in:
aaron-sangster
2026-01-07 02:45:27 +00:00
committed by GitHub
parent c43682f5b6
commit cca2016376
2 changed files with 121 additions and 0 deletions

View File

@@ -523,6 +523,24 @@ func resolveWorktreePath(repoRoot, name string) (string, error) {
return repoPath, nil
}
// Consult git's worktree registry - match by name (basename) or path
// This handles worktrees created in subdirectories (e.g., .worktrees/foo)
// where the name shown in "bd worktree list" doesn't match a simple path
// #nosec G204 - repoRoot comes from git.GetRepoRoot()
gitCmd := exec.Command("git", "worktree", "list", "--porcelain")
gitCmd.Dir = repoRoot
output, err := gitCmd.CombinedOutput()
if err == nil {
worktrees := parseWorktreeList(string(output))
for _, wt := range worktrees {
if wt.Name == name || wt.Path == name {
if _, err := os.Stat(wt.Path); err == nil {
return wt.Path, nil
}
}
}
}
return "", fmt.Errorf("worktree not found: %s", name)
}

View File

@@ -1,6 +1,9 @@
package main
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
@@ -63,3 +66,103 @@ func TestGitRevParse(t *testing.T) {
t.Logf("Not in git repo or error")
}
}
// TestResolveWorktreePathByName verifies that resolveWorktreePath can find
// worktrees by name (basename) when they're in subdirectories like .worktrees/
func TestResolveWorktreePathByName(t *testing.T) {
// Create a temp directory for the main repo
mainDir := t.TempDir()
// Initialize git repo
cmd := exec.Command("git", "init", "--initial-branch=main")
cmd.Dir = mainDir
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to init git repo: %v\n%s", err, output)
}
// Configure git user
cmd = exec.Command("git", "config", "user.email", "test@test.com")
cmd.Dir = mainDir
_ = cmd.Run()
cmd = exec.Command("git", "config", "user.name", "Test User")
cmd.Dir = mainDir
_ = cmd.Run()
// Create initial commit (required for worktrees)
if err := os.WriteFile(filepath.Join(mainDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
cmd = exec.Command("git", "add", ".")
cmd.Dir = mainDir
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "Initial commit")
cmd.Dir = mainDir
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to create initial commit: %v\n%s", err, output)
}
// Create .worktrees subdirectory
worktreesDir := filepath.Join(mainDir, ".worktrees")
if err := os.MkdirAll(worktreesDir, 0755); err != nil {
t.Fatalf("Failed to create .worktrees dir: %v", err)
}
// Create a worktree inside .worktrees/
worktreePath := filepath.Join(worktreesDir, "test-wt")
cmd = exec.Command("git", "worktree", "add", "-b", "test-wt", worktreePath)
cmd.Dir = mainDir
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to create worktree: %v\n%s", err, output)
}
defer func() {
// Cleanup worktree
cmd := exec.Command("git", "worktree", "remove", worktreePath, "--force")
cmd.Dir = mainDir
_ = cmd.Run()
}()
t.Run("resolves by name when worktree is in subdirectory", func(t *testing.T) {
// This should find the worktree by consulting git's registry
resolved, err := resolveWorktreePath(mainDir, "test-wt")
if err != nil {
t.Errorf("resolveWorktreePath(repoRoot, \"test-wt\") failed: %v", err)
return
}
// Compare resolved paths to handle symlinks (e.g., /var -> /private/var on macOS)
wantResolved, _ := filepath.EvalSymlinks(worktreePath)
gotResolved, _ := filepath.EvalSymlinks(resolved)
if gotResolved != wantResolved {
t.Errorf("resolveWorktreePath returned %q, want %q", resolved, worktreePath)
}
})
t.Run("resolves by relative path", func(t *testing.T) {
// This should work via the existing relative-to-repo-root logic
resolved, err := resolveWorktreePath(mainDir, ".worktrees/test-wt")
if err != nil {
t.Errorf("resolveWorktreePath(repoRoot, \".worktrees/test-wt\") failed: %v", err)
return
}
if resolved != worktreePath {
t.Errorf("resolveWorktreePath returned %q, want %q", resolved, worktreePath)
}
})
t.Run("resolves by absolute path", func(t *testing.T) {
resolved, err := resolveWorktreePath(mainDir, worktreePath)
if err != nil {
t.Errorf("resolveWorktreePath(repoRoot, absolutePath) failed: %v", err)
return
}
if resolved != worktreePath {
t.Errorf("resolveWorktreePath returned %q, want %q", resolved, worktreePath)
}
})
t.Run("returns error for non-existent worktree", func(t *testing.T) {
_, err := resolveWorktreePath(mainDir, "non-existent")
if err == nil {
t.Error("resolveWorktreePath should return error for non-existent worktree")
}
})
}