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.update(cx, |worktree, _| worktree.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
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        filter_tx: &'a Sender<MatchingEntry>,
738        output_tx: &'a Sender<oneshot::Receiver<ProjectPath>>,
739    ) -> BoxFuture<'a, Result<()>> {
740        async move {
741            let abs_path = snapshot.abs_path().join(path);
742            let Some(mut files) = fs
743                .read_dir(&abs_path)
744                .await
745                .with_context(|| format!("listing ignored path {abs_path:?}"))
746                .log_err()
747            else {
748                return Ok(());
749            };
750
751            let mut results = Vec::new();
752
753            while let Some(Ok(file)) = files.next().await {
754                let Some(metadata) = fs
755                    .metadata(&file)
756                    .await
757                    .with_context(|| format!("fetching fs metadata for {abs_path:?}"))
758                    .log_err()
759                    .flatten()
760                else {
761                    continue;
762                };
763                if metadata.is_symlink || metadata.is_fifo {
764                    continue;
765                }
766                results.push((
767                    file.strip_prefix(snapshot.abs_path())?.to_path_buf(),
768                    !metadata.is_dir,
769                ))
770            }
771            results.sort_by(|(a_path, _), (b_path, _)| a_path.cmp(b_path));
772            for (path, is_file) in results {
773                if is_file {
774                    if query.filters_path() {
775                        let matched_path = if query.match_full_paths() {
776                            let mut full_path = PathBuf::from(snapshot.root_name());
777                            full_path.push(&path);
778                            query.match_path(&full_path)
779                        } else {
780                            query.match_path(&path)
781                        };
782                        if !matched_path {
783                            continue;
784                        }
785                    }
786                    let (tx, rx) = oneshot::channel();
787                    output_tx.send(rx).await?;
788                    filter_tx
789                        .send(MatchingEntry {
790                            respond: tx,
791                            worktree_path: snapshot.abs_path().clone(),
792                            path: ProjectPath {
793                                worktree_id: snapshot.id(),
794                                path: Arc::from(path),
795                            },
796                        })
797                        .await?;
798                } else {
799                    Self::scan_ignored_dir(fs, snapshot, &path, query, filter_tx, output_tx)
800                        .await?;
801                }
802            }
803            Ok(())
804        }
805        .boxed()
806    }
807
808    async fn find_candidate_paths(
809        fs: Arc<dyn Fs>,
810        snapshots: Vec<(worktree::Snapshot, WorktreeSettings)>,
811        open_entries: HashSet<ProjectEntryId>,
812        query: SearchQuery,
813        filter_tx: Sender<MatchingEntry>,
814        output_tx: Sender<oneshot::Receiver<ProjectPath>>,
815    ) -> Result<()> {
816        for (snapshot, settings) in snapshots {
817            for entry in snapshot.entries(query.include_ignored(), 0) {
818                if entry.is_dir() && entry.is_ignored {
819                    if !settings.is_path_excluded(&entry.path) {
820                        Self::scan_ignored_dir(
821                            &fs,
822                            &snapshot,
823                            &entry.path,
824                            &query,
825                            &filter_tx,
826                            &output_tx,
827                        )
828                        .await?;
829                    }
830                    continue;
831                }
832
833                if entry.is_fifo || !entry.is_file() {
834                    continue;
835                }
836
837                if query.filters_path() {
838                    let matched_path = if query.match_full_paths() {
839                        let mut full_path = PathBuf::from(snapshot.root_name());
840                        full_path.push(&entry.path);
841                        query.match_path(&full_path)
842                    } else {
843                        query.match_path(&entry.path)
844                    };
845                    if !matched_path {
846                        continue;
847                    }
848                }
849
850                let (mut tx, rx) = oneshot::channel();
851
852                if open_entries.contains(&entry.id) {
853                    tx.send(ProjectPath {
854                        worktree_id: snapshot.id(),
855                        path: entry.path.clone(),
856                    })
857                    .await?;
858                } else {
859                    filter_tx
860                        .send(MatchingEntry {
861                            respond: tx,
862                            worktree_path: snapshot.abs_path().clone(),
863                            path: ProjectPath {
864                                worktree_id: snapshot.id(),
865                                path: entry.path.clone(),
866                            },
867                        })
868                        .await?;
869                }
870
871                output_tx.send(rx).await?;
872            }
873        }
874        Ok(())
875    }
876
877    async fn filter_paths(
878        fs: &Arc<dyn Fs>,
879        input: Receiver<MatchingEntry>,
880        query: &SearchQuery,
881    ) -> Result<()> {
882        let mut input = pin!(input);
883        while let Some(mut entry) = input.next().await {
884            let abs_path = entry.worktree_path.join(&entry.path.path);
885            let Some(file) = fs.open_sync(&abs_path).await.log_err() else {
886                continue;
887            };
888
889            let mut file = BufReader::new(file);
890            let file_start = file.fill_buf()?;
891
892            if let Err(Some(starting_position)) =
893                std::str::from_utf8(file_start).map_err(|e| e.error_len())
894            {
895                // Before attempting to match the file content, throw away files that have invalid UTF-8 sequences early on;
896                // That way we can still match files in a streaming fashion without having look at "obviously binary" files.
897                log::debug!(
898                    "Invalid UTF-8 sequence in file {abs_path:?} at byte position {starting_position}"
899                );
900                continue;
901            }
902
903            if query.detect(file).unwrap_or(false) {
904                entry.respond.send(entry.path).await?
905            }
906        }
907
908        Ok(())
909    }
910
911    pub async fn handle_create_project_entry(
912        this: Entity<Self>,
913        envelope: TypedEnvelope<proto::CreateProjectEntry>,
914        mut cx: AsyncApp,
915    ) -> Result<proto::ProjectEntryResponse> {
916        let worktree = this.update(&mut cx, |this, cx| {
917            let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
918            this.worktree_for_id(worktree_id, cx)
919                .ok_or_else(|| anyhow!("worktree not found"))
920        })??;
921        Worktree::handle_create_entry(worktree, envelope.payload, cx).await
922    }
923
924    pub async fn handle_copy_project_entry(
925        this: Entity<Self>,
926        envelope: TypedEnvelope<proto::CopyProjectEntry>,
927        mut cx: AsyncApp,
928    ) -> Result<proto::ProjectEntryResponse> {
929        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
930        let worktree = this.update(&mut cx, |this, cx| {
931            this.worktree_for_entry(entry_id, cx)
932                .ok_or_else(|| anyhow!("worktree not found"))
933        })??;
934        Worktree::handle_copy_entry(worktree, envelope.payload, cx).await
935    }
936
937    pub async fn handle_delete_project_entry(
938        this: Entity<Self>,
939        envelope: TypedEnvelope<proto::DeleteProjectEntry>,
940        mut cx: AsyncApp,
941    ) -> Result<proto::ProjectEntryResponse> {
942        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
943        let worktree = this.update(&mut cx, |this, cx| {
944            this.worktree_for_entry(entry_id, cx)
945                .ok_or_else(|| anyhow!("worktree not found"))
946        })??;
947        Worktree::handle_delete_entry(worktree, envelope.payload, cx).await
948    }
949
950    pub async fn handle_expand_project_entry(
951        this: Entity<Self>,
952        envelope: TypedEnvelope<proto::ExpandProjectEntry>,
953        mut cx: AsyncApp,
954    ) -> Result<proto::ExpandProjectEntryResponse> {
955        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
956        let worktree = this
957            .update(&mut cx, |this, cx| this.worktree_for_entry(entry_id, cx))?
958            .ok_or_else(|| anyhow!("invalid request"))?;
959        Worktree::handle_expand_entry(worktree, envelope.payload, cx).await
960    }
961
962    pub async fn handle_expand_all_for_project_entry(
963        this: Entity<Self>,
964        envelope: TypedEnvelope<proto::ExpandAllForProjectEntry>,
965        mut cx: AsyncApp,
966    ) -> Result<proto::ExpandAllForProjectEntryResponse> {
967        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
968        let worktree = this
969            .update(&mut cx, |this, cx| this.worktree_for_entry(entry_id, cx))?
970            .ok_or_else(|| anyhow!("invalid request"))?;
971        Worktree::handle_expand_all_for_entry(worktree, envelope.payload, cx).await
972    }
973}
974
975#[derive(Clone, Debug)]
976enum WorktreeHandle {
977    Strong(Entity<Worktree>),
978    Weak(WeakEntity<Worktree>),
979}
980
981impl WorktreeHandle {
982    fn upgrade(&self) -> Option<Entity<Worktree>> {
983        match self {
984            WorktreeHandle::Strong(handle) => Some(handle.clone()),
985            WorktreeHandle::Weak(handle) => handle.upgrade(),
986        }
987    }
988}