Skip to content

Commit 9d68dff

Browse files
javoireclaude
andauthored
fix(switch): resolve main worktree path from worktree list instead of repo root (#79)
GetRepoRoot() uses `git rev-parse --show-toplevel` which returns the current worktree's root, not the main repo root. This caused `ss main` to resolve to the wrong path when run from a worktree. Use `git worktree list` for all branches uniformly, which always returns the correct path. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent eeecb86 commit 9d68dff

2 files changed

Lines changed: 9 additions & 25 deletions

File tree

cmd/switch.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,6 @@ func runSwitch(gitClient git.GitClient, branchName string) error {
9898
}
9999

100100
func resolveWorktreePath(gitClient git.GitClient, branchName string) (string, error) {
101-
// Check if it's the base branch
102-
baseBranch := stack.GetBaseBranch(gitClient)
103-
if branchName == baseBranch {
104-
repoRoot, err := gitClient.GetRepoRoot()
105-
if err != nil {
106-
return "", fmt.Errorf("failed to get repo root: %w", err)
107-
}
108-
return repoRoot, nil
109-
}
110-
111-
// Look up in worktree branches
112101
worktreeBranches, err := gitClient.GetWorktreeBranches()
113102
if err != nil {
114103
return "", fmt.Errorf("failed to get worktree branches: %w", err)
@@ -150,18 +139,16 @@ func runSwitchInteractive(gitClient git.GitClient) error {
150139
}
151140
var options []option
152141

153-
// Add main repo
142+
// Add main repo (base branch from worktree list)
154143
baseBranch := stack.GetBaseBranch(gitClient)
155-
repoRoot, err := gitClient.GetRepoRoot()
156-
if err != nil {
157-
return fmt.Errorf("failed to get repo root: %w", err)
144+
if path, ok := worktreeBranches[baseBranch]; ok {
145+
options = append(options, option{branch: baseBranch, path: path})
158146
}
159-
options = append(options, option{branch: baseBranch, path: repoRoot})
160147

161148
// Add worktrees filtered to this repo's worktrees dir
162149
var sortedBranches []string
163150
for branch, path := range worktreeBranches {
164-
if pathWithinDir(path, worktreesDir) {
151+
if branch != baseBranch && pathWithinDir(path, worktreesDir) {
165152
sortedBranches = append(sortedBranches, branch)
166153
}
167154
}

cmd/switch_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ func TestRunSwitch(t *testing.T) {
1717
t.Run("resolves worktree branch to path", func(t *testing.T) {
1818
mockGit := new(testutil.MockGitClient)
1919

20-
mockGit.On("GetConfig", "stack.baseBranch").Return("")
21-
mockGit.On("GetDefaultBranch").Return("main")
2220
mockGit.On("GetWorktreeBranches").Return(map[string]string{
2321
"feature-a": "/home/user/.stack/worktrees/repo/feature-a",
2422
"feature-b": "/home/user/.stack/worktrees/repo/feature-b",
@@ -41,12 +39,13 @@ func TestRunSwitch(t *testing.T) {
4139
mockGit.AssertExpectations(t)
4240
})
4341

44-
t.Run("resolves base branch to repo root", func(t *testing.T) {
42+
t.Run("resolves base branch to main repo via worktree list", func(t *testing.T) {
4543
mockGit := new(testutil.MockGitClient)
4644

47-
mockGit.On("GetConfig", "stack.baseBranch").Return("")
48-
mockGit.On("GetDefaultBranch").Return("main")
49-
mockGit.On("GetRepoRoot").Return("/home/user/code/repo", nil)
45+
mockGit.On("GetWorktreeBranches").Return(map[string]string{
46+
"main": "/home/user/code/repo",
47+
"feature-a": "/home/user/.stack/worktrees/repo/feature-a",
48+
}, nil)
5049

5150
old := os.Stdout
5251
r, w, _ := os.Pipe()
@@ -67,8 +66,6 @@ func TestRunSwitch(t *testing.T) {
6766
t.Run("errors for unknown branch", func(t *testing.T) {
6867
mockGit := new(testutil.MockGitClient)
6968

70-
mockGit.On("GetConfig", "stack.baseBranch").Return("")
71-
mockGit.On("GetDefaultBranch").Return("main")
7269
mockGit.On("GetWorktreeBranches").Return(map[string]string{}, nil)
7370

7471
err := runSwitch(mockGit, "nonexistent")

0 commit comments

Comments
 (0)