git: Keep other git roots when switching to an existing worktree - #62620
Open
somtri wants to merge 1 commit into
Open
git: Keep other git roots when switching to an existing worktree#62620somtri wants to merge 1 commit into
somtri wants to merge 1 commit into
Conversation
do_switch_worktree remapped every git work directory onto the destination worktree and built the new workspace's path list from that destination alone, so switching in a multi-root project dropped every other git root. Resolve which repository owns the destination worktree, replace only that one, and carry the remaining git roots into the new workspace unchanged.
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.
Objective
Fixes #62331
In a project with more than one git repository, switching to an existing worktree from the picker drops every other git root. Open
appanddeployas one project, switch toapp's existingapp-featureworktree, and the project is left holding onlyapp-feature.deployis gone, with no warning.Creating a worktree already handles multi-root projects. Switching has no equivalent.
Solution
do_switch_worktreetreated every git work directory in the project as the source of the switch:all_pathsthen starts from the destination alone, so sibling git roots never reach the new workspace. That is the reported bug.A switch targets one worktree, and that worktree belongs to one repository.
handle_switch_worktreenow passes the repository entities through, anddo_switch_worktreelooks for the one whoseworktrees()lists the destination path.do_create_worktreealready makes that same call. That repository is the one replaced by the destination. Every other repository keeps its own work directory, and its root goes intoall_pathsunchanged.The same ownership result builds
path_remapping, so each repository is attributed to its own path instead of all of them to the destination. That part changes no behaviour today:path_remappingis only read whentransfer_stateis set, and a switch never sets it. I kept it consistent rather than leave the switch path carrying attribution it would have to unlearn later.If no repository lists the destination path, no root is replaced.
This adds one
worktrees()call per repository on the switch path. Creating a worktree already pays the same cost.Testing
test_switch_worktree_keeps_sibling_git_reposbuilds a project with two separate repositories and a pre-existing linked worktree, switches to that worktree, then checks that the sibling root survives, the destination opens, and the source root is replaced.It fails on
mainbefore the change:cargo test -p git_ui_core worktree_service::tests: 6 passcargo test -p git_ui_core: 26 passTested on Windows, headless. I have not run it on macOS or Linux, and I have not tested the remote path. The change is platform independent: no
cfg, and no path assumptions beyond what the surrounding code already makes.To reproduce: open two git repositories as one project, run
git worktree addin one of them, then switch to that worktree from the picker.Self-Review Checklist:
Release Notes: