Fix active repository selection after removing remote worktrees (#66299)

Removing a project folder containing the active Git repository from a
remote workspace could leave the Git panel showing “No Git Repositories”
and an “Initialize Repository” button, even when other Git repositories
remained open.

Select a remaining repository when the active repository is removed.
Preserve the current selection when removing another repository, and
clear it when the last repository is removed.

Testing:
- Added a regression test covering removal of an inactive repository,
the active repository, and the last repository.
- Confirmed the regression test fails before the fix and passes
afterward on Windows.
- Passed the existing remote worktree add/remove/re-add test.

Release Notes:

- Fixed the Git panel showing “No Git Repositories” after removing the
active repository's project folder from a remote workspace while other
repositories remain open.

---------

Co-authored-by: dino <dinojoaocosta@gmail.com>
This commit is contained in:
buvidk1234
2026-11-02 14:55:47 +00:00
committed by GitHub
parent 7a349ce42d
commit e38d4ec980
2 changed files with 75 additions and 9 deletions
+9 -9
View File
@@ -2460,18 +2460,18 @@ impl GitStore {
}
if is_active_repo_removed {
if let Some((&repo_id, _)) = self.repositories.iter().next() {
self.active_repo_id = Some(repo_id);
cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(repo_id)));
} else {
self.active_repo_id = None;
cx.emit(GitStoreEvent::ActiveRepositoryChanged(None));
}
self.select_fallback_active_repository(cx);
}
}
_ => {}
}
}
fn select_fallback_active_repository(&mut self, cx: &mut Context<Self>) {
self.active_repo_id = self.repositories.keys().next().copied();
cx.emit(GitStoreEvent::ActiveRepositoryChanged(self.active_repo_id));
}
fn on_repository_event(
&mut self,
repo: Entity<Repository>,
@@ -3396,8 +3396,8 @@ impl GitStore {
client.send(update).log_err();
}
if this.active_repo_id == Some(id) {
this.active_repo_id = None;
cx.emit(GitStoreEvent::ActiveRepositoryChanged(None));
this.active_repo_id = this.repositories.keys().next().copied();
cx.emit(GitStoreEvent::ActiveRepositoryChanged(this.active_repo_id));
}
cx.emit(GitStoreEvent::RepositoryRemoved(id));
});
@@ -3533,6 +3533,72 @@ async fn test_canceling_buffer_opening(cx: &mut TestAppContext, server_cx: &mut
});
}
#[gpui::test]
async fn test_removing_remote_worktree_keeps_remaining_repository_active(
cx: &mut TestAppContext,
server_cx: &mut TestAppContext,
) {
let fs = FakeFs::new(server_cx.executor());
fs.insert_tree(
path!("/code"),
json!({
"remaining": { ".git": {}, "file.txt": "remaining" },
"active": { ".git": {}, "file.txt": "active" },
"other": { ".git": {}, "file.txt": "other" },
}),
)
.await;
let (project, _headless) = init_test(&fs, cx, server_cx).await;
let mut worktree_ids = HashMap::default();
for name in ["remaining", "active", "other"] {
let root = Path::new(path!("/code")).join(name);
let (worktree, _) = project
.update(cx, |project, cx| {
project.find_or_create_worktree(root, true, cx)
})
.await
.expect("open remote worktree");
worktree_ids.insert(name, worktree.read_with(cx, |worktree, _| worktree.id()));
}
cx.run_until_parked();
let git_store = project.read_with(cx, |project, _| project.git_store().clone());
let repository_for_name = |name: &str, cx: &mut TestAppContext| {
let root = Path::new(path!("/code")).join(name);
git_store.read_with(cx, |git_store, cx| {
git_store
.repositories()
.values()
.find(|repository| repository.read(cx).work_directory_abs_path.as_ref() == root)
.cloned()
.expect("remote repository is available")
})
};
let active_repository = repository_for_name("active", cx);
let remaining_repository = repository_for_name("remaining", cx);
active_repository.update(cx, |repository, cx| repository.set_as_active_repository(cx));
for (name, expected_repository, expected_count) in [
("other", Some(active_repository), 2),
("active", Some(remaining_repository), 1),
("remaining", None, 0),
] {
let worktree_id = *worktree_ids.get(name).expect("remote worktree exists");
project.update(cx, |project, cx| project.remove_worktree(worktree_id, cx));
cx.run_until_parked();
git_store.read_with(cx, |git_store, _| {
assert_eq!(git_store.repositories().len(), expected_count);
assert_eq!(
git_store.active_repository(),
expected_repository,
"removing {name} should preserve a remaining active repository"
);
});
}
}
#[gpui::test]
async fn test_adding_then_removing_then_adding_worktrees(
cx: &mut TestAppContext,