diff --git a/cmd/switch.go b/cmd/switch.go index b92776f..b65138a 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -98,17 +98,6 @@ func runSwitch(gitClient git.GitClient, branchName string) error { } func resolveWorktreePath(gitClient git.GitClient, branchName string) (string, error) { - // Check if it's the base branch - baseBranch := stack.GetBaseBranch(gitClient) - if branchName == baseBranch { - repoRoot, err := gitClient.GetRepoRoot() - if err != nil { - return "", fmt.Errorf("failed to get repo root: %w", err) - } - return repoRoot, nil - } - - // Look up in worktree branches worktreeBranches, err := gitClient.GetWorktreeBranches() if err != nil { return "", fmt.Errorf("failed to get worktree branches: %w", err) @@ -150,18 +139,16 @@ func runSwitchInteractive(gitClient git.GitClient) error { } var options []option - // Add main repo + // Add main repo (base branch from worktree list) baseBranch := stack.GetBaseBranch(gitClient) - repoRoot, err := gitClient.GetRepoRoot() - if err != nil { - return fmt.Errorf("failed to get repo root: %w", err) + if path, ok := worktreeBranches[baseBranch]; ok { + options = append(options, option{branch: baseBranch, path: path}) } - options = append(options, option{branch: baseBranch, path: repoRoot}) // Add worktrees filtered to this repo's worktrees dir var sortedBranches []string for branch, path := range worktreeBranches { - if pathWithinDir(path, worktreesDir) { + if branch != baseBranch && pathWithinDir(path, worktreesDir) { sortedBranches = append(sortedBranches, branch) } } diff --git a/cmd/switch_test.go b/cmd/switch_test.go index 1abc0b6..7f03232 100644 --- a/cmd/switch_test.go +++ b/cmd/switch_test.go @@ -17,8 +17,6 @@ func TestRunSwitch(t *testing.T) { t.Run("resolves worktree branch to path", func(t *testing.T) { mockGit := new(testutil.MockGitClient) - mockGit.On("GetConfig", "stack.baseBranch").Return("") - mockGit.On("GetDefaultBranch").Return("main") mockGit.On("GetWorktreeBranches").Return(map[string]string{ "feature-a": "/home/user/.stack/worktrees/repo/feature-a", "feature-b": "/home/user/.stack/worktrees/repo/feature-b", @@ -41,12 +39,13 @@ func TestRunSwitch(t *testing.T) { mockGit.AssertExpectations(t) }) - t.Run("resolves base branch to repo root", func(t *testing.T) { + t.Run("resolves base branch to main repo via worktree list", func(t *testing.T) { mockGit := new(testutil.MockGitClient) - mockGit.On("GetConfig", "stack.baseBranch").Return("") - mockGit.On("GetDefaultBranch").Return("main") - mockGit.On("GetRepoRoot").Return("/home/user/code/repo", nil) + mockGit.On("GetWorktreeBranches").Return(map[string]string{ + "main": "/home/user/code/repo", + "feature-a": "/home/user/.stack/worktrees/repo/feature-a", + }, nil) old := os.Stdout r, w, _ := os.Pipe() @@ -67,8 +66,6 @@ func TestRunSwitch(t *testing.T) { t.Run("errors for unknown branch", func(t *testing.T) { mockGit := new(testutil.MockGitClient) - mockGit.On("GetConfig", "stack.baseBranch").Return("") - mockGit.On("GetDefaultBranch").Return("main") mockGit.On("GetWorktreeBranches").Return(map[string]string{}, nil) err := runSwitch(mockGit, "nonexistent")