Skip to content

Commit 1a931f8

Browse files
timvwclaude
andcommitted
fix: filter out invalid branch names in interactive selection
Fix issue where "origin" and other remote names appeared as selectable branches in interactive checkout. The problem occurred because `git branch -a` can output remote reference names that aren't actual branches. Improvements: - Skip lines containing "->" (HEAD pointer references) - Explicitly filter out "origin" and "upstream" as branch names - Better deduplication of local vs remote branches - Show all branches (not just those without worktrees) since checkout handles existing worktrees by navigating to them Fixes error: "branch 'origin' does not exist" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 60f3391 commit 1a931f8

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

main.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,26 +156,47 @@ func printCDMarker(path string) {
156156
}
157157

158158
func getAvailableBranches() ([]string, error) {
159-
// Get all branches
159+
// Get local and remote branches
160160
cmd := exec.Command("git", "branch", "-a", "--format=%(refname:short)")
161161
output, err := cmd.Output()
162162
if err != nil {
163163
return nil, err
164164
}
165165

166-
branches := []string{}
166+
// Use a map to deduplicate
167+
branchMap := make(map[string]bool)
168+
167169
for _, line := range strings.Split(string(output), "\n") {
168170
branch := strings.TrimSpace(line)
169-
if branch == "" || strings.HasPrefix(branch, "origin/HEAD") {
171+
if branch == "" {
170172
continue
171173
}
172-
// Remove origin/ prefix for remote branches
173-
branch = strings.TrimPrefix(branch, "origin/")
174-
// Check if this branch doesn't already have a worktree
175-
if _, exists := worktreeExists(branch); !exists {
176-
branches = append(branches, branch)
174+
175+
// Skip remote HEAD pointers
176+
if strings.HasPrefix(branch, "origin/HEAD") || strings.Contains(branch, "->") {
177+
continue
178+
}
179+
180+
// For remote branches, strip the origin/ prefix
181+
if strings.HasPrefix(branch, "origin/") {
182+
branch = strings.TrimPrefix(branch, "origin/")
183+
}
184+
185+
// Skip if branch name is just "origin" or other remote names
186+
if branch == "origin" || branch == "upstream" {
187+
continue
177188
}
189+
190+
// Add to map (deduplicates automatically)
191+
branchMap[branch] = true
178192
}
193+
194+
// Convert map to slice
195+
branches := []string{}
196+
for branch := range branchMap {
197+
branches = append(branches, branch)
198+
}
199+
179200
return branches, nil
180201
}
181202

0 commit comments

Comments
 (0)