Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand Down
13 changes: 5 additions & 8 deletions cmd/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand All @@ -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")
Expand Down
Loading