Add projectile-switch-worktree - #2147
Merged
Merged
Conversation
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
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.
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.
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 Wnarrows 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 listonly 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.