Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix active repository selection after removing remote worktrees (#66299)
buvidk1234e38d4ec98099
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rsindex 7bcddc83080..798c913951b 100644--- a/crates/project/src/git_store.rs+++ b/crates/project/src/git_store.rs@@ -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));});diff --git a/crates/remote_server/src/remote_editing_tests.rs b/crates/remote_server/src/remote_editing_tests.rsindex a9d324bed4e..4d849363bcc 100644--- a/crates/remote_server/src/remote_editing_tests.rs+++ b/crates/remote_server/src/remote_editing_tests.rs@@ -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,