worktree_store.rs

  1use std::{
  2    io::{BufRead, BufReader},
  3    path::{Path, PathBuf},
  4    pin::pin,
  5    sync::{Arc, atomic::AtomicUsize},
  6};
  7
  8use anyhow::{Context as _, Result, anyhow};
  9use collections::{HashMap, HashSet};
 10use fs::Fs;
 11use futures::{
 12    FutureExt, SinkExt,
 13    future::{BoxFuture, Shared},
 14};
 15use gpui::{
 16    App, AppContext as _, AsyncApp, Context, Entity, EntityId, EventEmitter, Task, WeakEntity,
 17};
 18use postage::oneshot;
 19use rpc::{
 20    AnyProtoClient, ErrorExt, TypedEnvelope,
 21    proto::{self, FromProto, SSH_PROJECT_ID, ToProto},
 22};
 23use smol::{
 24    channel::{Receiver, Sender},
 25    stream::StreamExt,
 26};
 27use text::ReplicaId;
 28use util::{ResultExt, paths::SanitizedPath};
 29use worktree::{
 30    Entry, ProjectEntryId, UpdatedEntriesSet, UpdatedGitRepositoriesSet, Worktree, WorktreeId,
 31    WorktreeSettings,
 32};
 33
 34use crate::{ProjectPath, search::SearchQuery};
 35
 36struct MatchingEntry {
 37    worktree_path: Arc<Path>,
 38    path: ProjectPath,
 39    respond: oneshot::Sender<ProjectPath>,
 40}
 41
 42enum WorktreeStoreState {
 43    Local {
 44        fs: Arc<dyn Fs>,
 45    },
 46    Remote {
 47        upstream_client: AnyProtoClient,
 48        upstream_project_id: u64,
 49    },
 50}
 51
 52pub struct WorktreeStore {
 53    next_entry_id: Arc<AtomicUsize>,
 54    downstream_client: Option<(AnyProtoClient, u64)>,
 55    retain_worktrees: bool,
 56    worktrees: Vec<WorktreeHandle>,
 57    worktrees_reordered: bool,
 58    #[allow(clippy::type_complexity)]
 59    loading_worktrees:
 60        HashMap<SanitizedPath, Shared<Task<Result<Entity<Worktree>, Arc<anyhow::Error>>>>>,
 61    state: WorktreeStoreState,
 62}
 63
 64#[derive(Debug)]
 65pub enum WorktreeStoreEvent {
 66    WorktreeAdded(Entity<Worktree>),
 67    WorktreeRemoved(EntityId, WorktreeId),
 68    WorktreeReleased(EntityId, WorktreeId),
 69    WorktreeOrderChanged,
 70    WorktreeUpdateSent(Entity<Worktree>),
 71    WorktreeUpdatedEntries(WorktreeId, UpdatedEntriesSet),
 72    WorktreeUpdatedGitRepositories(WorktreeId, UpdatedGitRepositoriesSet),
 73    WorktreeDeletedEntry(WorktreeId, ProjectEntryId),
 74}
 75
 76impl EventEmitter<WorktreeStoreEvent> for WorktreeStore {}
 77
 78impl WorktreeStore {
 79    pub fn init(client: &AnyProtoClient) {
 80        client.add_entity_request_handler(Self::handle_create_project_entry);
 81        client.add_entity_request_handler(Self::handle_copy_project_entry);
 82        client.add_entity_request_handler(Self::handle_delete_project_entry);
 83        client.add_entity_request_handler(Self::handle_expand_project_entry);
 84        client.add_entity_request_handler(Self::handle_expand_all_for_project_entry);
 85    }
 86
 87    pub fn local(retain_worktrees: bool, fs: Arc<dyn Fs>) -> Self {
 88        Self {
 89            next_entry_id: Default::default(),
 90            loading_worktrees: Default::default(),
 91            downstream_client: None,
 92            worktrees: Vec::new(),
 93            worktrees_reordered: false,
 94            retain_worktrees,
 95            state: WorktreeStoreState::Local { fs },
 96        }
 97    }
 98
 99    pub fn remote(
100        retain_worktrees: bool,
101        upstream_client: AnyProtoClient,
102        upstream_project_id: u64,
103    ) -> Self {
104        Self {
105            next_entry_id: Default::default(),
106            loading_worktrees: Default::default(),
107            downstream_client: None,
108            worktrees: Vec::new(),
109            worktrees_reordered: false,
110            retain_worktrees,
111            state: WorktreeStoreState::Remote {
112                upstream_client,
113                upstream_project_id,
114            },
115        }
116    }
117
118    /// Iterates through all worktrees, including ones that don't appear in the project panel
119    pub fn worktrees(&self) -> impl '_ + DoubleEndedIterator<Item = Entity<Worktree>> {
120        self.worktrees
121            .iter()
122            .filter_map(move |worktree| worktree.upgrade())
123    }
124
125    /// Iterates through all user-visible worktrees, the ones that appear in the project panel.
126    pub fn visible_worktrees<'a>(
127        &'a self,
128        cx: &'a App,
129    ) -> impl 'a + DoubleEndedIterator<Item = Entity<Worktree>> {
130        self.worktrees()
131            .filter(|worktree| worktree.read(cx).is_visible())
132    }
133
134    pub fn worktree_for_id(&self, id: WorktreeId, cx: &App) -> Option<Entity<Worktree>> {
135        self.worktrees()
136            .find(|worktree| worktree.read(cx).id() == id)
137    }
138
139    pub fn worktree_for_entry(
140        &self,
141        entry_id: ProjectEntryId,
142        cx: &App,
143    ) -> Option<Entity<Worktree>> {
144        self.worktrees()
145            .find(|worktree| worktree.read(cx).contains_entry(entry_id))
146    }
147
148    pub fn find_worktree(
149        &self,
150        abs_path: impl Into<SanitizedPath>,
151        cx: &App,
152    ) -> Option<(Entity<Worktree>, PathBuf)> {
153        let abs_path: SanitizedPath = abs_path.into();
154        for tree in self.worktrees() {
155            if let Ok(relative_path) = abs_path.as_path().strip_prefix(tree.read(cx).abs_path()) {
156                return Some((tree.clone(), relative_path.into()));
157            }
158        }
159        None
160    }
161
162    pub fn absolutize(&self, project_path: &ProjectPath, cx: &App) -> Option<PathBuf> {
163        let worktree = self.worktree_for_id(project_path.worktree_id, cx)?;
164        worktree.read(cx).absolutize(&project_path.path).ok()
165    }
166
167    pub fn find_or_create_worktree(
168        &mut self,
169        abs_path: impl AsRef<Path>,
170        visible: bool,
171        cx: &mut Context<Self>,
172    ) -> Task<Result<(Entity<Worktree>, PathBuf)>> {
173        let abs_path = abs_path.as_ref();
174        if let Some((tree, relative_path)) = self.find_worktree(abs_path, cx) {
175            Task::ready(Ok((tree, relative_path)))
176        } else {
177            let worktree = self.create_worktree(abs_path, visible, cx);
178            cx.background_spawn(async move { Ok((worktree.await?, PathBuf::new())) })
179        }
180    }
181
182    pub fn entry_for_id<'a>(&'a self, entry_id: ProjectEntryId, cx: &'a App) -> Option<&'a Entry> {
183        self.worktrees()
184            .find_map(|worktree| worktree.read(cx).entry_for_id(entry_id))
185    }
186
187    pub fn worktree_and_entry_for_id<'a>(
188        &'a self,
189        entry_id: ProjectEntryId,
190        cx: &'a App,
191    ) -> Option<(Entity<Worktree>, &'a Entry)> {
192        self.worktrees().find_map(|worktree| {
193            worktree
194                .read(cx)
195                .entry_for_id(entry_id)
196                .map(|e| (worktree.clone(), e))
197        })
198    }
199
200    pub fn entry_for_path(&self, path: &ProjectPath, cx: &App) -> Option<Entry> {
201        self.worktree_for_id(path.worktree_id, cx)?
202            .read(cx)
203            .entry_for_path(&path.path)
204            .cloned()
205    }
206
207    pub fn create_worktree(
208        &mut self,
209        abs_path: impl Into<SanitizedPath>,
210        visible: bool,
211        cx: &mut Context<Self>,
212    ) -> Task<Result<Entity<Worktree>>> {
213        let abs_path: SanitizedPath = abs_path.into();
214        if !self.loading_worktrees.contains_key(&abs_path) {
215            let task = match &self.state {
216                WorktreeStoreState::Remote {
217                    upstream_client, ..
218                } => {
219                    if upstream_client.is_via_collab() {
220                        Task::ready(Err(Arc::new(anyhow!("cannot create worktrees via collab"))))
221                    } else {
222                        self.create_ssh_worktree(
223                            upstream_client.clone(),
224                            abs_path.clone(),
225                            visible,
226                            cx,
227                        )
228                    }
229                }
230                WorktreeStoreState::Local { fs } => {
231                    self.create_local_worktree(fs.clone(), abs_path.clone(), visible, cx)
232                }
233            };
234
235            self.loading_worktrees
236                .insert(abs_path.clone(), task.shared());
237        }
238        let task = self.loading_worktrees.get(&abs_path).unwrap().clone();
239        cx.spawn(async move |this, cx| {
240            let result = task.await;
241            this.update(cx, |this, _| this.loading_worktrees.remove(&abs_path))
242                .ok();
243            match result {
244                Ok(worktree) => Ok(worktree),
245                Err(err) => Err((*err).cloned()),
246            }
247        })
248    }
249
250    fn create_ssh_worktree(
251        &mut self,
252        client: AnyProtoClient,
253        abs_path: impl Into<SanitizedPath>,
254        visible: bool,
255        cx: &mut Context<Self>,
256    ) -> Task<Result<Entity<Worktree>, Arc<anyhow::Error>>> {
257        let mut abs_path = Into::<SanitizedPath>::into(abs_path).to_string();
258        // If we start with `/~` that means the ssh path was something like `ssh://user@host/~/home-dir-folder/`
259        // in which case want to strip the leading the `/`.
260        // On the host-side, the `~` will get expanded.
261        // That's what git does too: https://github.com/libgit2/libgit2/issues/3345#issuecomment-127050850
262        if abs_path.starts_with("/~") {
263            abs_path = abs_path[1..].to_string();
264        }
265        if abs_path.is_empty() {
266            abs_path = "~/".to_string();
267        }
268        cx.spawn(async move |this, cx| {
269            let this = this.upgrade().context("Dropped worktree store")?;
270
271            let path = Path::new(abs_path.as_str());
272            let response = client
273                .request(proto::AddWorktree {
274                    project_id: SSH_PROJECT_ID,
275                    path: path.to_proto(),
276                    visible,
277                })
278                .await?;
279
280            if let Some(existing_worktree) = this.read_with(cx, |this, cx| {
281                this.worktree_for_id(WorktreeId::from_proto(response.worktree_id), cx)
282            })? {
283                return Ok(existing_worktree);
284            }
285
286            let root_path_buf = PathBuf::from_proto(response.canonicalized_path.clone());
287            let root_name = root_path_buf
288                .file_name()
289                .map(|n| n.to_string_lossy().to_string())
290                .unwrap_or(root_path_buf.to_string_lossy().to_string());
291
292            let worktree = cx.update(|cx| {
293                Worktree::remote(
294                    SSH_PROJECT_ID,
295                    0,
296                    proto::WorktreeMetadata {
297                        id: response.worktree_id,
298                        root_name,
299                        visible,
300                        abs_path: response.canonicalized_path,
301                    },
302                    client,
303                    cx,
304                )
305            })?;
306
307            this.update(cx, |this, cx| {
308                this.add(&worktree, cx);
309            })?;
310            Ok(worktree)
311        })
312    }
313
314    fn create_local_worktree(
315        &mut self,
316        fs: Arc<dyn Fs>,
317        abs_path: impl Into<SanitizedPath>,
318        visible: bool,
319        cx: &mut Context<Self>,
320    ) -> Task<Result<Entity<Worktree>, Arc<anyhow::Error>>> {
321        let next_entry_id = self.next_entry_id.clone();
322        let path: SanitizedPath = abs_path.into();
323
324        cx.spawn(async move |this, cx| {
325            let worktree = Worktree::local(path.clone(), visible, fs, next_entry_id, cx).await;
326
327            let worktree = worktree?;
328
329            this.update(cx, |this, cx| this.add(&worktree, cx))?;
330
331            if visible {
332                cx.update(|cx| {
333                    cx.add_recent_document(path.as_path());
334                })
335                .log_err();
336            }
337
338            Ok(worktree)
339        })
340    }
341
342    pub fn add(&mut self, worktree: &Entity<Worktree>, cx: &mut Context<Self>) {
343        let worktree_id = worktree.read(cx).id();
344        debug_assert!(self.worktrees().all(|w| w.read(cx).id() != worktree_id));
345
346        let push_strong_handle = self.retain_worktrees || worktree.read(cx).is_visible();
347        let handle = if push_strong_handle {
348            WorktreeHandle::Strong(worktree.clone())
349        } else {
350            WorktreeHandle::Weak(worktree.downgrade())
351        };
352        if self.worktrees_reordered {
353            self.worktrees.push(handle);
354        } else {
355            let i = match self
356                .worktrees
357                .binary_search_by_key(&Some(worktree.read(cx).abs_path()), |other| {
358                    other.upgrade().map(|worktree| worktree.read(cx).abs_path())
359                }) {
360                Ok(i) | Err(i) => i,
361            };
362            self.worktrees.insert(i, handle);
363        }
364
365        cx.emit(WorktreeStoreEvent::WorktreeAdded(worktree.clone()));
366        self.send_project_updates(cx);
367
368        let handle_id = worktree.entity_id();
369        cx.subscribe(worktree, |_, worktree, event, cx| {
370            let worktree_id = worktree.read(cx).id();
371            match event {
372                worktree::Event::UpdatedEntries(changes) => {
373                    cx.emit(WorktreeStoreEvent::WorktreeUpdatedEntries(
374                        worktree_id,
375                        changes.clone(),
376                    ));
377                }
378                worktree::Event::UpdatedGitRepositories(set) => {
379                    cx.emit(WorktreeStoreEvent::WorktreeUpdatedGitRepositories(
380                        worktree_id,
381                        set.clone(),
382                    ));
383                }
384                worktree::Event::DeletedEntry(id) => {
385                    cx.emit(WorktreeStoreEvent::WorktreeDeletedEntry(worktree_id, *id))
386                }
387            }
388        })
389        .detach();
390        cx.observe_release(worktree, move |this, worktree, cx| {
391            cx.emit(WorktreeStoreEvent::WorktreeReleased(
392                handle_id,
393                worktree.id(),
394            ));
395            cx.emit(WorktreeStoreEvent::WorktreeRemoved(
396                handle_id,
397                worktree.id(),
398            ));
399            this.send_project_updates(cx);
400        })
401        .detach();
402    }
403
404    pub fn remove_worktree(&mut self, id_to_remove: WorktreeId, cx: &mut Context<Self>) {
405        self.worktrees.retain(|worktree| {
406            if let Some(worktree) = worktree.upgrade() {
407                if worktree.read(cx).id() == id_to_remove {
408                    cx.emit(WorktreeStoreEvent::WorktreeRemoved(
409                        worktree.entity_id(),
410                        id_to_remove,
411                    ));
412                    false
413                } else {
414                    true
415                }
416            } else {
417                false
418            }
419        });
420        self.send_project_updates(cx);
421    }
422
423    pub fn set_worktrees_reordered(&mut self, worktrees_reordered: bool) {
424        self.worktrees_reordered = worktrees_reordered;
425    }
426
427    fn upstream_client(&self) -> Option<(AnyProtoClient, u64)> {
428        match &self.state {
429            WorktreeStoreState::Remote {
430                upstream_client,
431                upstream_project_id,
432                ..
433            } => Some((upstream_client.clone(), *upstream_project_id)),
434            WorktreeStoreState::Local { .. } => None,
435        }
436    }
437
438    pub fn set_worktrees_from_proto(
439        &mut self,
440        worktrees: Vec<proto::WorktreeMetadata>,
441        replica_id: ReplicaId,
442        cx: &mut Context<Self>,
443    ) -> Result<()> {
444        let mut old_worktrees_by_id = self
445            .worktrees
446            .drain(..)
447            .filter_map(|worktree| {
448                let worktree = worktree.upgrade()?;
449                Some((worktree.read(cx).id(), worktree))
450            })
451            .collect::<HashMap<_, _>>();
452
453        let (client, project_id) = self.upstream_client().clone().context("invalid project")?;
454
455        for worktree in worktrees {
456            if let Some(old_worktree) =
457                old_worktrees_by_id.remove(&WorktreeId::from_proto(worktree.id))
458            {
459                let push_strong_handle =
460                    self.retain_worktrees || old_worktree.read(cx).is_visible();
461                let handle = if push_strong_handle {
462                    WorktreeHandle::Strong(old_worktree.clone())
463                } else {
464                    WorktreeHandle::Weak(old_worktree.downgrade())
465                };
466                self.worktrees.push(handle);
467            } else {
468                self.add(
469                    &Worktree::remote(project_id, replica_id, worktree, client.clone(), cx),
470                    cx,
471                );
472            }
473        }
474        self.send_project_updates(cx);
475
476        Ok(())
477    }
478
479    pub fn move_worktree(
480        &mut self,
481        source: WorktreeId,
482        destination: WorktreeId,
483        cx: &mut Context<Self>,
484    ) -> Result<()> {
485        if source == destination {
486            return Ok(());
487        }
488
489        let mut source_index = None;
490        let mut destination_index = None;
491        for (i, worktree) in self.worktrees.iter().enumerate() {
492            if let Some(worktree) = worktree.upgrade() {
493                let worktree_id = worktree.read(cx).id();
494                if worktree_id == source {
495                    source_index = Some(i);
496                    if destination_index.is_some() {
497                        break;
498                    }
499                } else if worktree_id == destination {
500                    destination_index = Some(i);
501                    if source_index.is_some() {
502                        break;
503                    }
504                }
505            }
506        }
507
508        let source_index =
509            source_index.with_context(|| format!("Missing worktree for id {source}"))?;
510        let destination_index =
511            destination_index.with_context(|| format!("Missing worktree for id {destination}"))?;
512
513        if source_index == destination_index {
514            return Ok(());
515        }
516
517        let worktree_to_move = self.worktrees.remove(source_index);
518        self.worktrees.insert(destination_index, worktree_to_move);
519        self.worktrees_reordered = true;
520        cx.emit(WorktreeStoreEvent::WorktreeOrderChanged);
521        cx.notify();
522        Ok(())
523    }
524
525    pub fn disconnected_from_host(&mut self, cx: &mut App) {
526        for worktree in &self.worktrees {
527            if let Some(worktree) = worktree.upgrade() {
528                worktree.update(cx, |worktree, _| {
529                    if let Some(worktree) = worktree.as_remote_mut() {
530                        worktree.disconnected_from_host();
531                    }
532                });
533            }
534        }
535    }
536
537    pub fn send_project_updates(&mut self, cx: &mut Context<Self>) {
538        let Some((downstream_client, project_id)) = self.downstream_client.clone() else {
539            return;
540        };
541
542        let update = proto::UpdateProject {
543            project_id,
544            worktrees: self.worktree_metadata_protos(cx),
545        };
546
547        // collab has bad concurrency guarantees, so we send requests in serial.
548        let update_project = if downstream_client.is_via_collab() {
549            Some(downstream_client.request(update))
550        } else {
551            downstream_client.send(update).log_err();
552            None
553        };
554        cx.spawn(async move |this, cx| {
555            if let Some(update_project) = update_project {
556                update_project.await?;
557            }
558
559            this.update(cx, |this, cx| {
560                let worktrees = this.worktrees().collect::<Vec<_>>();
561
562                for worktree in worktrees {
563                    worktree.update(cx, |worktree, cx| {
564                        let client = downstream_client.clone();
565                        worktree.observe_updates(project_id, cx, {
566                            move |update| {
567                                let client = client.clone();
568                                async move {
569                                    if client.is_via_collab() {
570                                        client
571                                            .request(update)
572                                            .map(|result| result.log_err().is_some())
573                                            .await
574                                    } else {
575                                        client.send(update).log_err().is_some()
576                                    }
577                                }
578                            }
579                        });
580                    });
581
582                    cx.emit(WorktreeStoreEvent::WorktreeUpdateSent(worktree.clone()))
583                }
584
585                anyhow::Ok(())
586            })
587        })
588        .detach_and_log_err(cx);
589    }
590
591    pub fn worktree_metadata_protos(&self, cx: &App) -> Vec<proto::WorktreeMetadata> {
592        self.worktrees()
593            .map(|worktree| {
594                let worktree = worktree.read(cx);
595                proto::WorktreeMetadata {
596                    id: worktree.id().to_proto(),
597                    root_name: worktree.root_name().into(),
598                    visible: worktree.is_visible(),
599                    abs_path: worktree.abs_path().to_proto(),
600                }
601            })
602            .collect()
603    }
604
605    pub fn shared(
606        &mut self,
607        remote_id: u64,
608        downstream_client: AnyProtoClient,
609        cx: &mut Context<Self>,
610    ) {
611        self.retain_worktrees = true;
612        self.downstream_client = Some((downstream_client, remote_id));
613
614        // When shared, retain all worktrees
615        for worktree_handle in self.worktrees.iter_mut() {
616            match worktree_handle {
617                WorktreeHandle::Strong(_) => {}
618                WorktreeHandle::Weak(worktree) => {
619                    if let Some(worktree) = worktree.upgrade() {
620                        *worktree_handle = WorktreeHandle::Strong(worktree);
621                    }
622                }
623            }
624        }
625        self.send_project_updates(cx);
626    }
627
628    pub fn unshared(&mut self, cx: &mut Context<Self>) {
629        self.retain_worktrees = false;
630        self.downstream_client.take();
631
632        // When not shared, only retain the visible worktrees
633        for worktree_handle in self.worktrees.iter_mut() {
634            if let WorktreeHandle::Strong(worktree) = worktree_handle {
635                let is_visible = worktree.update(cx, |worktree, _| {
636                    worktree.stop_observing_updates();
637                    worktree.is_visible()
638                });
639                if !is_visible {
640                    *worktree_handle = WorktreeHandle::Weak(worktree.downgrade());
641                }
642            }
643        }
644    }
645
646    /// search over all worktrees and return buffers that *might* match the search.
647    pub fn find_search_candidates(
648        &self,
649        query: SearchQuery,
650        limit: usize,
651        open_entries: HashSet<ProjectEntryId>,
652        fs: Arc<dyn Fs>,
653        cx: &Context<Self>,
654    ) -> Receiver<ProjectPath> {
655        let snapshots = self
656            .visible_worktrees(cx)
657            .filter_map(|tree| {
658                let tree = tree.read(cx);
659                Some((tree.snapshot(), tree.as_local()?.settings()))
660            })
661            .collect::<Vec<_>>();
662
663        let executor = cx.background_executor().clone();
664
665        // We want to return entries in the order they are in the worktrees, so we have one
666        // thread that iterates over the worktrees (and ignored directories) as necessary,
667        // and pushes a oneshot::Receiver to the output channel and a oneshot::Sender to the filter
668        // channel.
669        // We spawn a number of workers that take items from the filter channel and check the query
670        // against the version of the file on disk.
671        let (filter_tx, filter_rx) = smol::channel::bounded(64);
672        let (output_tx, output_rx) = smol::channel::bounded(64);
673        let (matching_paths_tx, matching_paths_rx) = smol::channel::unbounded();
674
675        let input = cx.background_spawn({
676            let fs = fs.clone();
677            let query = query.clone();
678            async move {
679                Self::find_candidate_paths(
680                    fs,
681                    snapshots,
682                    open_entries,
683                    query,
684                    filter_tx,
685                    output_tx,
686                )
687                .await
688                .log_err();
689            }
690        });
691        const MAX_CONCURRENT_FILE_SCANS: usize = 64;
692        let filters = cx.background_spawn(async move {
693            let fs = &fs;
694            let query = &query;
695            executor
696                .scoped(move |scope| {
697                    for _ in 0..MAX_CONCURRENT_FILE_SCANS {
698                        let filter_rx = filter_rx.clone();
699                        scope.spawn(async move {
700                            Self::filter_paths(fs, filter_rx, query)
701                                .await
702                                .log_with_level(log::Level::Debug);
703                        })
704                    }
705                })
706                .await;
707        });
708        cx.background_spawn(async move {
709            let mut matched = 0;
710            while let Ok(mut receiver) = output_rx.recv().await {
711                let Some(path) = receiver.next().await else {
712                    continue;
713                };
714                let Ok(_) = matching_paths_tx.send(path).await else {
715                    break;
716                };
717                matched += 1;
718                if matched == limit {
719                    break;
720                }
721            }
722            drop(input);
723            drop(filters);
724        })
725        .detach();
726        matching_paths_rx
727    }
728
729    fn scan_ignored_dir<'a>(
730        fs: &'a Arc<dyn Fs>,
731        snapshot: &'a worktree::Snapshot,
732        path: &'a Path,
733        query: &'a SearchQuery,
734        filter_tx: &'a Sender<MatchingEntry>,
735        output_tx: &'a Sender<oneshot::Receiver<ProjectPath>>,
736    ) -> BoxFuture<'a, Result<()>> {
737        async move {
738            let abs_path = snapshot.abs_path().join(path);
739            let Some(mut files) = fs
740                .read_dir(&abs_path)
741                .await
742                .with_context(|| format!("listing ignored path {abs_path:?}"))
743                .log_err()
744            else {
745                return Ok(());
746            };
747
748            let mut results = Vec::new();
749
750            while let Some(Ok(file)) = files.next().await {
751                let Some(metadata) = fs
752                    .metadata(&file)
753                    .await
754                    .with_context(|| format!("fetching fs metadata for {abs_path:?}"))
755                    .log_err()
756                    .flatten()
757                else {
758                    continue;
759                };
760                if metadata.is_symlink || metadata.is_fifo {
761                    continue;
762                }
763                results.push((
764                    file.strip_prefix(snapshot.abs_path())?.to_path_buf(),
765                    !metadata.is_dir,
766                ))
767            }
768            results.sort_by(|(a_path, _), (b_path, _)| a_path.cmp(b_path));
769            for (path, is_file) in results {
770                if is_file {
771                    if query.filters_path() {
772                        let matched_path = if query.match_full_paths() {
773                            let mut full_path = PathBuf::from(snapshot.root_name());
774                            full_path.push(&path);
775                            query.match_path(&full_path)
776                        } else {
777                            query.match_path(&path)
778                        };
779                        if !matched_path {
780                            continue;
781                        }
782                    }
783                    let (tx, rx) = oneshot::channel();
784                    output_tx.send(rx).await?;
785                    filter_tx
786                        .send(MatchingEntry {
787                            respond: tx,
788                            worktree_path: snapshot.abs_path().clone(),
789                            path: ProjectPath {
790                                worktree_id: snapshot.id(),
791                                path: Arc::from(path),
792                            },
793                        })
794                        .await?;
795                } else {
796                    Self::scan_ignored_dir(fs, snapshot, &path, query, filter_tx, output_tx)
797                        .await?;
798                }
799            }
800            Ok(())
801        }
802        .boxed()
803    }
804
805    async fn find_candidate_paths(
806        fs: Arc<dyn Fs>,
807        snapshots: Vec<(worktree::Snapshot, WorktreeSettings)>,
808        open_entries: HashSet<ProjectEntryId>,
809        query: SearchQuery,
810        filter_tx: Sender<MatchingEntry>,
811        output_tx: Sender<oneshot::Receiver<ProjectPath>>,
812    ) -> Result<()> {
813        for (snapshot, settings) in snapshots {
814            for entry in snapshot.entries(query.include_ignored(), 0) {
815                if entry.is_dir() && entry.is_ignored {
816                    if !settings.is_path_excluded(&entry.path) {
817                        Self::scan_ignored_dir(
818                            &fs,
819                            &snapshot,
820                            &entry.path,
821                            &query,
822                            &filter_tx,
823                            &output_tx,
824                        )
825                        .await?;
826                    }
827                    continue;
828                }
829
830                if entry.is_fifo || !entry.is_file() {
831                    continue;
832                }
833
834                if query.filters_path() {
835                    let matched_path = if query.match_full_paths() {
836                        let mut full_path = PathBuf::from(snapshot.root_name());
837                        full_path.push(&entry.path);
838                        query.match_path(&full_path)
839                    } else {
840                        query.match_path(&entry.path)
841                    };
842                    if !matched_path {
843                        continue;
844                    }
845                }
846
847                let (mut tx, rx) = oneshot::channel();
848
849                if open_entries.contains(&entry.id) {
850                    tx.send(ProjectPath {
851                        worktree_id: snapshot.id(),
852                        path: entry.path.clone(),
853                    })
854                    .await?;
855                } else {
856                    filter_tx
857                        .send(MatchingEntry {
858                            respond: tx,
859                            worktree_path: snapshot.abs_path().clone(),
860                            path: ProjectPath {
861                                worktree_id: snapshot.id(),
862                                path: entry.path.clone(),
863                            },
864                        })
865                        .await?;
866                }
867
868                output_tx.send(rx).await?;
869            }
870        }
871        Ok(())
872    }
873
874    async fn filter_paths(
875        fs: &Arc<dyn Fs>,
876        input: Receiver<MatchingEntry>,
877        query: &SearchQuery,
878    ) -> Result<()> {
879        let mut input = pin!(input);
880        while let Some(mut entry) = input.next().await {
881            let abs_path = entry.worktree_path.join(&entry.path.path);
882            let Some(file) = fs.open_sync(&abs_path).await.log_err() else {
883                continue;
884            };
885
886            let mut file = BufReader::new(file);
887            let file_start = file.fill_buf()?;
888
889            if let Err(Some(starting_position)) =
890                std::str::from_utf8(file_start).map_err(|e| e.error_len())
891            {
892                // Before attempting to match the file content, throw away files that have invalid UTF-8 sequences early on;
893                // That way we can still match files in a streaming fashion without having look at "obviously binary" files.
894                log::debug!(
895                    "Invalid UTF-8 sequence in file {abs_path:?} at byte position {starting_position}"
896                );
897                continue;
898            }
899
900            if query.detect(file).unwrap_or(false) {
901                entry.respond.send(entry.path).await?
902            }
903        }
904
905        Ok(())
906    }
907
908    pub async fn handle_create_project_entry(
909        this: Entity<Self>,
910        envelope: TypedEnvelope<proto::CreateProjectEntry>,
911        mut cx: AsyncApp,
912    ) -> Result<proto::ProjectEntryResponse> {
913        let worktree = this.update(&mut cx, |this, cx| {
914            let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
915            this.worktree_for_id(worktree_id, cx)
916                .context("worktree not found")
917        })??;
918        Worktree::handle_create_entry(worktree, envelope.payload, cx).await
919    }
920
921    pub async fn handle_copy_project_entry(
922        this: Entity<Self>,
923        envelope: TypedEnvelope<proto::CopyProjectEntry>,
924        mut cx: AsyncApp,
925    ) -> Result<proto::ProjectEntryResponse> {
926        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
927        let worktree = this.update(&mut cx, |this, cx| {
928            this.worktree_for_entry(entry_id, cx)
929                .context("worktree not found")
930        })??;
931        Worktree::handle_copy_entry(worktree, envelope.payload, cx).await
932    }
933
934    pub async fn handle_delete_project_entry(
935        this: Entity<Self>,
936        envelope: TypedEnvelope<proto::DeleteProjectEntry>,
937        mut cx: AsyncApp,
938    ) -> Result<proto::ProjectEntryResponse> {
939        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
940        let worktree = this.update(&mut cx, |this, cx| {
941            this.worktree_for_entry(entry_id, cx)
942                .context("worktree not found")
943        })??;
944        Worktree::handle_delete_entry(worktree, envelope.payload, cx).await
945    }
946
947    pub async fn handle_expand_project_entry(
948        this: Entity<Self>,
949        envelope: TypedEnvelope<proto::ExpandProjectEntry>,
950        mut cx: AsyncApp,
951    ) -> Result<proto::ExpandProjectEntryResponse> {
952        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
953        let worktree = this
954            .update(&mut cx, |this, cx| this.worktree_for_entry(entry_id, cx))?
955            .context("invalid request")?;
956        Worktree::handle_expand_entry(worktree, envelope.payload, cx).await
957    }
958
959    pub async fn handle_expand_all_for_project_entry(
960        this: Entity<Self>,
961        envelope: TypedEnvelope<proto::ExpandAllForProjectEntry>,
962        mut cx: AsyncApp,
963    ) -> Result<proto::ExpandAllForProjectEntryResponse> {
964        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
965        let worktree = this
966            .update(&mut cx, |this, cx| this.worktree_for_entry(entry_id, cx))?
967            .context("invalid request")?;
968        Worktree::handle_expand_all_for_entry(worktree, envelope.payload, cx).await
969    }
970
971    pub fn fs(&self) -> Option<Arc<dyn Fs>> {
972        match &self.state {
973            WorktreeStoreState::Local { fs } => Some(fs.clone()),
974            WorktreeStoreState::Remote { .. } => None,
975        }
976    }
977}
978
979#[derive(Clone, Debug)]
980enum WorktreeHandle {
981    Strong(Entity<Worktree>),
982    Weak(WeakEntity<Worktree>),
983}
984
985impl WorktreeHandle {
986    fn upgrade(&self) -> Option<Entity<Worktree>> {
987        match self {
988            WorktreeHandle::Strong(handle) => Some(handle.clone()),
989            WorktreeHandle::Weak(handle) => handle.upgrade(),
990        }
991    }
992}