Skip to content

Add projectile-switch-worktree - #2147

Merged
bbatsov merged 2 commits into
masterfrom
feature/worktree-switching
Aug 7, 2026
Merged

Add projectile-switch-worktree#2147
bbatsov merged 2 commits into
masterfrom
feature/worktree-switching

Conversation

@bbatsov

@bbatsov bbatsov commented Aug 7, 2026

Copy link
Copy Markdown
Owner

Working on two branches at once means two directories, and switching between them today means going through the whole known-projects list and remembering which directory holds which branch.

s-p W narrows that down to the other checkouts of the current project's repository, showing the branch each one has checked out. Git worktrees come from git itself, so ones you've never opened in Emacs turn up too. Separate clones of the same upstream count as checkouts as well - it's the same workflow without the plumbing, and plenty of people do it that way.

Underneath sits a small repository-identity layer that can say when two project roots are really the same repository. It reads git's own files rather than shelling out, since it runs over every known project - on my machine that's 105 projects in 0.07s instead of a couple of hundred subprocesses.

Mercurial shares work through the known projects too. jj workspaces don't: jj workspace list only learned to report workspace paths in 0.38, and I'd rather add that once I can test it against a real jj than ship it blind.

bbatsov added 2 commits August 7, 2026 14:46
Projectile treats every checkout as a project of its own, which is the
right call - a git worktree and the checkout it was linked from have
their own roots, file listings and branches. It does mean there's no way
to ask whether two project roots are really the same repository seen
twice, and that question is about to come up in a few places.

`projectile-repo-identity' answers it with two keys: the directory the
checkouts share (git's common dir, the store an `hg share' points at),
and a canonical spelling of the upstream they were cloned from, so that
`git@host:owner/repo.git' and `https://host/owner/repo' compare equal.

It reads git's own files rather than shelling out. The identity of every
known project gets computed when looking for the checkouts of one, and a
subprocess apiece is seconds of latency once you have a hundred of them.
Working on two branches at once means two directories, and switching
between them today means going through the full known-projects list and
remembering which directory holds which branch.

`s-p W' offers just the other checkouts of the current project's
repository, annotated with the branch each has checked out. Git worktrees
are found by asking git, so ones you've never visited turn up too;
anything else - a Mercurial working directory sharing another's store,
or simply a second clone of the same upstream - is found among the known
projects. The second case is worth supporting properly: cloning a repo
twice is the same workflow without the plumbing, and plenty of people do
it that way.

Worktree candidates get their own completion category rather than
reusing `projectile-project'. Marginalia registers a file annotator for
that one, and a registered annotator wins over the metadata's own, which
would have hidden the branch names.
@bbatsov
bbatsov merged commit 8a4cd59 into master Aug 7, 2026
10 checks passed
@bbatsov
bbatsov deleted the feature/worktree-switching branch August 7, 2026 11:48
bbatsov added a commit that referenced this pull request Aug 7, 2026
A project marked out by its own .projectile inside a larger repository
was being offered that repository's worktrees. `projectile-project-vcs'
answers `git' for such a project - it walks up to find the repository, so
that file listing still works - and the worktree lookup took that as
license to ask git, which happily listed worktrees that are copies of the
repository, not of the project. Switching to one landed you somewhere
unrelated.

The repository identity layer already made this distinction; the git
lookup just wasn't using it. It now requires the project to hold a .git
of its own, which is exactly what being a checkout means.

While here, make the two commands explain themselves when they come up
empty. "No other checkout found" reads as "I looked" - unhelpful when the
project is under Subversion and Projectile can't look at all, so that
case now says so and points at the sibling command, which needs no
repository. And a sibling lookup finding nothing is an ordinary outcome
of the share cap rather than a fault, so it points at
`projectile-project-groups'.

No CHANGELOG entry: all of this is refining code from #2147 and #2148,
which haven't been in a release yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant