Fix worktree trust not applied on window reuse (#46623)

Kirill Bulatov created

New workspace/window creates a different `WeakEntity<WorktreeStore>` for
the same path, which was not considered before.

Closes https://github.com/zed-industries/zed/issues/46318

Release Notes:

- Fixed worktree trust not applied in window reuse

Change summary

crates/project/src/trusted_worktrees.rs | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)

Detailed changes

crates/project/src/trusted_worktrees.rs 🔗

@@ -306,6 +306,9 @@ impl TrustedWorktreesStore {
                         self.restricted.get_mut(&weak_worktree_store)
                     {
                         restricted_worktrees.remove(worktree_id);
+                        if restricted_worktrees.is_empty() {
+                            self.restricted.remove(&weak_worktree_store);
+                        }
                     };
 
                     if let Some(worktree) =
@@ -614,7 +617,8 @@ impl TrustedWorktreesStore {
         &mut self,
         cx: &mut Context<Self>,
     ) -> HashMap<Option<RemoteHostLocation>, HashSet<PathBuf>> {
-        self.trusted_paths
+        let new_trusted_paths = self
+            .trusted_paths
             .iter()
             .filter_map(|(worktree_store, paths)| {
                 let host = self.worktree_stores.get(&worktree_store)?.clone();
@@ -632,13 +636,16 @@ impl TrustedWorktreesStore {
                     .collect::<HashSet<_>>();
                 Some((host, abs_paths))
             })
-            .chain(self.db_trusted_paths.clone())
+            .chain(self.db_trusted_paths.drain())
             .fold(HashMap::default(), |mut acc, (host, paths)| {
                 acc.entry(host)
                     .or_insert_with(HashSet::default)
                     .extend(paths);
                 acc
-            })
+            });
+
+        self.db_trusted_paths = new_trusted_paths.clone();
+        new_trusted_paths
     }
 
     fn add_worktree_store(
@@ -647,6 +654,8 @@ impl TrustedWorktreesStore {
         remote_host: Option<RemoteHostLocation>,
         cx: &mut Context<Self>,
     ) {
+        self.worktree_stores
+            .retain(|worktree_store, _| worktree_store.is_upgradable());
         let weak_worktree_store = worktree_store.downgrade();
         self.worktree_stores
             .insert(weak_worktree_store.clone(), remote_host.clone());