fix(branch): include branches checked out in other worktrees - #2013
Merged
CKolkey merged 1 commit intoSep 28, 2026
Merged
Conversation
`git branch` prefixes branches checked out in a linked worktree with "+ ", which parse_branches did not match, so those branches were missing from pickers such as the diff range picker. Also strip the "*"/"+" markers in list_related_branches. Fixes NeogitOrg#2003
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.
Bug
Branches that are checked out in a linked worktree never show up in the pickers built from
git.branch(for example the diff popup's range picker, or "checkout recent branch").Root cause
git branchmarks each line with a prefix:*for the current branch,+for a branch checked out in another worktree, and two spaces for everything else.parse_branchesinlua/neogit/lib/git/branch.luaonly matched"^[* ] (.+)"/"^ (.+)", so any line starting with+was dropped, whateverinclude_currentwas set to.list_related_branches(used by cherry-pick "harvest") parses the same output but only trimmed whitespace. Branches checked out in another worktree came back as+ name, and the current branch as* name.Change
parse_branchesnow also matches the+marker:"^[*+ ] (.+)"when the current branch is included,"^[+ ] (.+)"when it isn't. The current branch is still left out unless it's requested.list_related_branchesstrips a leading*or+before trimming, so it returns plain branch names.These are the only two places that parse
git branchoutput. The other pickers (checkout, push, log, etc.) usegit.refs, which readsfor-each-refand already listed these branches.Behavior note: a branch checked out in another worktree is now selectable, the same way the
git.refs-based checkout picker already offers it. If you try to check it out, git refuses with its usual "already used by worktree" error. Neogit doesn't add any extra filtering, so git's own rules still decide what's allowed.Tests
Added a
lib.git.branch with linked worktreesblock totests/specs/neogit/lib/git/branch_spec.lua. It sits outside the existingpending(...)block, so it actually runs. The spec creates a real temporary repo withgit worktree addand checks that:get_local_branches(false/true)andget_all_branches(false)include the branch checked out in the other worktreeinclude_currentis setlist_related_branches("--contains", ...)returns names without*/+markersBefore the fix, 3 of the 4 new tests fail. After the fix they all pass.
make test: all 17 spec files pass, 0 failuresbundle exec rspec(run locally on macOS with Neovim 0.12): upstreammasteralready fails 67 of 519 examples here with the same seed, mostly screen-layout and default-branch mismatches in this environment. With this change, 68 fail. The difference is only in flaky examples (reflog, stash, bisect, push), and none of them depend on the parsing changed here. CI should give the real result.selene --config selene/config.toml lua,typosandstylua --check lua/ tests/are all cleanFixes #2003
Thanks to @f1code for the detailed report, which pinned down the root cause and proposed this pattern fix.
Generated with Claude Code on behalf of @ethanstoner.