worktree_store.rs

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