-
Notifications
You must be signed in to change notification settings - Fork 542
fix: Hide worktree branches from branch switcher and show worktree tabs #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,37 @@ interface BranchInfo { | |||||||||||||||||||
| name: string; | ||||||||||||||||||||
| isCurrent: boolean; | ||||||||||||||||||||
| isRemote: boolean; | ||||||||||||||||||||
| /** Path to worktree if this branch is checked out in a worktree (null if not) */ | ||||||||||||||||||||
| checkedOutInWorktree: string | null; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Get a map of branch names to their worktree paths. | ||||||||||||||||||||
| * Git doesn't allow checking out a branch that's already checked out in a worktree. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| async function getWorktreeBranches(cwd: string): Promise<Map<string, string>> { | ||||||||||||||||||||
| const branchToWorktree = new Map<string, string>(); | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| const { stdout } = await execAsync('git worktree list --porcelain', { cwd }); | ||||||||||||||||||||
| const lines = stdout.split('\n'); | ||||||||||||||||||||
| let currentWorktreePath: string | null = null; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for (const line of lines) { | ||||||||||||||||||||
| if (line.startsWith('worktree ')) { | ||||||||||||||||||||
| currentWorktreePath = line.slice(9); | ||||||||||||||||||||
| } else if (line.startsWith('branch ')) { | ||||||||||||||||||||
| const branchName = line.slice(7).replace('refs/heads/', ''); | ||||||||||||||||||||
| if (currentWorktreePath) { | ||||||||||||||||||||
| branchToWorktree.set(branchName, currentWorktreePath); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } else if (line === '') { | ||||||||||||||||||||
| currentWorktreePath = null; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } catch { | ||||||||||||||||||||
| // If we can't get worktree info, return empty map | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return branchToWorktree; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export function createListBranchesHandler() { | ||||||||||||||||||||
|
|
@@ -40,6 +71,10 @@ export function createListBranchesHandler() { | |||||||||||||||||||
| }); | ||||||||||||||||||||
| const currentBranch = currentBranchOutput.trim(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Get branches that are already checked out in worktrees | ||||||||||||||||||||
| // These cannot be switched to from other locations | ||||||||||||||||||||
| const worktreeBranches = await getWorktreeBranches(worktreePath); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // List all local branches | ||||||||||||||||||||
| // Use double quotes around the format string for cross-platform compatibility | ||||||||||||||||||||
| // Single quotes are preserved literally on Windows; double quotes work on both | ||||||||||||||||||||
|
|
@@ -54,10 +89,13 @@ export function createListBranchesHandler() { | |||||||||||||||||||
| .map((name) => { | ||||||||||||||||||||
| // Remove any surrounding quotes (Windows git may preserve them) | ||||||||||||||||||||
| const cleanName = name.trim().replace(/^['"]|['"]$/g, ''); | ||||||||||||||||||||
| const worktreePath = worktreeBranches.get(cleanName); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable shadowing: The variable Suggested rename- const worktreePath = worktreeBranches.get(cleanName);
+ const branchWorktreePath = worktreeBranches.get(cleanName);
return {
name: cleanName,
isCurrent: cleanName === currentBranch,
isRemote: false,
// Mark if this branch is checked out in a worktree (and it's not the current worktree)
- checkedOutInWorktree: worktreePath && cleanName !== currentBranch ? worktreePath : null,
+ checkedOutInWorktree: branchWorktreePath && cleanName !== currentBranch ? branchWorktreePath : null,
};📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| return { | ||||||||||||||||||||
| name: cleanName, | ||||||||||||||||||||
| isCurrent: cleanName === currentBranch, | ||||||||||||||||||||
| isRemote: false, | ||||||||||||||||||||
| // Mark if this branch is checked out in a worktree (and it's not the current worktree) | ||||||||||||||||||||
| checkedOutInWorktree: worktreePath && cleanName !== currentBranch ? worktreePath : null, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -102,6 +140,7 @@ export function createListBranchesHandler() { | |||||||||||||||||||
| name: cleanName, // Keep full name like "origin/main" | ||||||||||||||||||||
| isCurrent: false, | ||||||||||||||||||||
| isRemote: true, | ||||||||||||||||||||
| checkedOutInWorktree: null, // Remote branches are never checked out in worktrees | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While returning an empty map on failure is a reasonable fallback, swallowing the error completely can make debugging difficult if
git worktree listfails for an unexpected reason. It's better to log the error using the existinglogWorktreeErrorutility to aid in troubleshooting.