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() || abs_path == "/" {
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 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}