Conversation
gw derived the worktree path from the branch name alone, so a worktree created outside gw was invisible to it: the derived path did not exist, gw fell through to `git worktree add`, and git refused because the branch was already checked out elsewhere. Ask git where the branch lives before falling back to the derived path. Setting wt_path once, before the mode dispatch, fixes -d and -D too. Claude-Session: https://claude.ai/code/session_01J467etVSU3EDkjv3CXXFGi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
This PR fixes
gw <branch>failing withfatal: '<branch>' is already used by worktree at ...whenever that worktree was created outsidegw.gwderives the directory from the branch name by replacing slashes with dashes, soowen/featuremaps to.worktrees/owen-feature. A worktree made by hand, or renamed since, sits at some other path.gwdoes not find its derived path, falls through togit worktree add, and git refuses because the branch is already checked out. Completion makes it worse: it lists any branch whose worktree is under.worktrees/, so the branch you tab-completed is exactly the one that then errors.What changed
_gw_path_for_branchasksgit worktree list --porcelainwhere a branch is actually checked out. It reads the path withsubstr($0, 10)rather than$2, so paths containing spaces survive.gwprefers that path over the derived one. It is assigned once, before the mode dispatch, so-dand-Dpick it up too. Deleting a non-conventional worktree previously failed the same way.Two behaviors improve as a side effect.
gw mainnow cds to the main worktree instead of erroring, andgw -bon an already checked-out branch cds there rather than failing, matching what it already did when the derived directory existed.