1use crate::{copy_recursive, git_repository::GitRepository, ProjectEntryId, RemoveOptions};
2
3use super::{
4 fs::{self, Fs},
5 ignore::IgnoreStack,
6 DiagnosticSummary,
7};
8use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
9use anyhow::{anyhow, Context, Result};
10use client::{proto, Client};
11use clock::ReplicaId;
12use collections::{HashMap, VecDeque};
13use futures::{
14 channel::{
15 mpsc::{self, UnboundedSender},
16 oneshot,
17 },
18 Stream, StreamExt,
19};
20use fuzzy::CharBag;
21use gpui::{
22 executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
23 Task,
24};
25use language::{
26 proto::{deserialize_version, serialize_line_ending, serialize_version},
27 Buffer, DiagnosticEntry, LineEnding, PointUtf16, Rope,
28};
29use parking_lot::Mutex;
30use postage::{
31 prelude::{Sink as _, Stream as _},
32 watch,
33};
34use settings::Settings;
35use smol::channel::{self, Sender};
36use std::{
37 any::Any,
38 cmp::{self, Ordering},
39 convert::TryFrom,
40 ffi::{OsStr, OsString},
41 fmt,
42 future::Future,
43 mem,
44 ops::{Deref, DerefMut},
45 os::unix::prelude::{OsStrExt, OsStringExt},
46 path::{Path, PathBuf},
47 sync::{atomic::AtomicUsize, Arc},
48 task::Poll,
49 time::{Duration, SystemTime},
50};
51use sum_tree::{Bias, Edit, SeekTarget, SumTree, TreeMap, TreeSet};
52use util::{ResultExt, TryFutureExt, DOT_GIT, GITIGNORE};
53
54#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
55pub struct WorktreeId(usize);
56
57#[allow(clippy::large_enum_variant)]
58pub enum Worktree {
59 Local(LocalWorktree),
60 Remote(RemoteWorktree),
61}
62
63pub struct LocalWorktree {
64 snapshot: LocalSnapshot,
65 background_snapshot: Arc<Mutex<LocalSnapshot>>,
66 last_scan_state_rx: watch::Receiver<ScanState>,
67 _background_scanner_task: Option<Task<()>>,
68 poll_task: Option<Task<()>>,
69 share: Option<ShareState>,
70 diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
71 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
72 client: Arc<Client>,
73 fs: Arc<dyn Fs>,
74 visible: bool,
75}
76
77pub struct RemoteWorktree {
78 pub snapshot: Snapshot,
79 pub(crate) background_snapshot: Arc<Mutex<Snapshot>>,
80 project_id: u64,
81 client: Arc<Client>,
82 updates_tx: Option<UnboundedSender<proto::UpdateWorktree>>,
83 snapshot_subscriptions: VecDeque<(usize, oneshot::Sender<()>)>,
84 replica_id: ReplicaId,
85 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
86 visible: bool,
87}
88
89#[derive(Clone)]
90pub struct Snapshot {
91 id: WorktreeId,
92 root_name: String,
93 root_char_bag: CharBag,
94 entries_by_path: SumTree<Entry>,
95 entries_by_id: SumTree<PathEntry>,
96 scan_id: usize,
97 is_complete: bool,
98}
99
100pub struct LocalSnapshot {
101 abs_path: Arc<Path>,
102 ignores_by_parent_abs_path: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
103 git_repositories: Vec<Box<dyn GitRepository>>,
104 removed_entry_ids: HashMap<u64, ProjectEntryId>,
105 next_entry_id: Arc<AtomicUsize>,
106 snapshot: Snapshot,
107 extension_counts: HashMap<OsString, usize>,
108}
109
110impl Clone for LocalSnapshot {
111 fn clone(&self) -> Self {
112 Self {
113 abs_path: self.abs_path.clone(),
114 ignores_by_parent_abs_path: self.ignores_by_parent_abs_path.clone(),
115 git_repositories: self
116 .git_repositories
117 .iter()
118 .map(|repo| repo.boxed_clone())
119 .collect(),
120 removed_entry_ids: self.removed_entry_ids.clone(),
121 next_entry_id: self.next_entry_id.clone(),
122 snapshot: self.snapshot.clone(),
123 extension_counts: self.extension_counts.clone(),
124 }
125 }
126}
127
128impl Deref for LocalSnapshot {
129 type Target = Snapshot;
130
131 fn deref(&self) -> &Self::Target {
132 &self.snapshot
133 }
134}
135
136impl DerefMut for LocalSnapshot {
137 fn deref_mut(&mut self) -> &mut Self::Target {
138 &mut self.snapshot
139 }
140}
141
142#[derive(Clone, Debug)]
143enum ScanState {
144 Idle,
145 /// The worktree is performing its initial scan of the filesystem.
146 Initializing,
147 /// The worktree is updating in response to filesystem events.
148 Updating,
149 Err(Arc<anyhow::Error>),
150}
151
152struct ShareState {
153 project_id: u64,
154 snapshots_tx: watch::Sender<LocalSnapshot>,
155 _maintain_remote_snapshot: Option<Task<Option<()>>>,
156}
157
158pub enum Event {
159 UpdatedEntries,
160 UpdatedGitRepositories(Vec<Box<dyn GitRepository>>),
161}
162
163impl Entity for Worktree {
164 type Event = Event;
165}
166
167impl Worktree {
168 pub async fn local(
169 client: Arc<Client>,
170 path: impl Into<Arc<Path>>,
171 visible: bool,
172 fs: Arc<dyn Fs>,
173 next_entry_id: Arc<AtomicUsize>,
174 cx: &mut AsyncAppContext,
175 ) -> Result<ModelHandle<Self>> {
176 let (tree, scan_states_tx) =
177 LocalWorktree::create(client, path, visible, fs.clone(), next_entry_id, cx).await?;
178 tree.update(cx, |tree, cx| {
179 let tree = tree.as_local_mut().unwrap();
180 let abs_path = tree.abs_path().clone();
181 let background_snapshot = tree.background_snapshot.clone();
182 let background = cx.background().clone();
183 tree._background_scanner_task = Some(cx.background().spawn(async move {
184 let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
185 let scanner =
186 BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
187 scanner.run(events).await;
188 }));
189 });
190 Ok(tree)
191 }
192
193 pub fn remote(
194 project_remote_id: u64,
195 replica_id: ReplicaId,
196 worktree: proto::WorktreeMetadata,
197 client: Arc<Client>,
198 cx: &mut MutableAppContext,
199 ) -> ModelHandle<Self> {
200 let remote_id = worktree.id;
201 let root_char_bag: CharBag = worktree
202 .root_name
203 .chars()
204 .map(|c| c.to_ascii_lowercase())
205 .collect();
206 let root_name = worktree.root_name.clone();
207 let visible = worktree.visible;
208 let snapshot = Snapshot {
209 id: WorktreeId(remote_id as usize),
210 root_name,
211 root_char_bag,
212 entries_by_path: Default::default(),
213 entries_by_id: Default::default(),
214 scan_id: 0,
215 is_complete: false,
216 };
217
218 let (updates_tx, mut updates_rx) = mpsc::unbounded();
219 let background_snapshot = Arc::new(Mutex::new(snapshot.clone()));
220 let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
221 let worktree_handle = cx.add_model(|_: &mut ModelContext<Worktree>| {
222 Worktree::Remote(RemoteWorktree {
223 project_id: project_remote_id,
224 replica_id,
225 snapshot: snapshot.clone(),
226 background_snapshot: background_snapshot.clone(),
227 updates_tx: Some(updates_tx),
228 snapshot_subscriptions: Default::default(),
229 client: client.clone(),
230 diagnostic_summaries: Default::default(),
231 visible,
232 })
233 });
234
235 cx.background()
236 .spawn(async move {
237 while let Some(update) = updates_rx.next().await {
238 if let Err(error) = background_snapshot.lock().apply_remote_update(update) {
239 log::error!("error applying worktree update: {}", error);
240 }
241 snapshot_updated_tx.send(()).await.ok();
242 }
243 })
244 .detach();
245
246 cx.spawn(|mut cx| {
247 let this = worktree_handle.downgrade();
248 async move {
249 while (snapshot_updated_rx.recv().await).is_some() {
250 if let Some(this) = this.upgrade(&cx) {
251 this.update(&mut cx, |this, cx| {
252 this.poll_snapshot(cx);
253 let this = this.as_remote_mut().unwrap();
254 while let Some((scan_id, _)) = this.snapshot_subscriptions.front() {
255 if this.observed_snapshot(*scan_id) {
256 let (_, tx) = this.snapshot_subscriptions.pop_front().unwrap();
257 let _ = tx.send(());
258 } else {
259 break;
260 }
261 }
262 });
263 } else {
264 break;
265 }
266 }
267 }
268 })
269 .detach();
270
271 worktree_handle
272 }
273
274 pub fn as_local(&self) -> Option<&LocalWorktree> {
275 if let Worktree::Local(worktree) = self {
276 Some(worktree)
277 } else {
278 None
279 }
280 }
281
282 pub fn as_remote(&self) -> Option<&RemoteWorktree> {
283 if let Worktree::Remote(worktree) = self {
284 Some(worktree)
285 } else {
286 None
287 }
288 }
289
290 pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
291 if let Worktree::Local(worktree) = self {
292 Some(worktree)
293 } else {
294 None
295 }
296 }
297
298 pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
299 if let Worktree::Remote(worktree) = self {
300 Some(worktree)
301 } else {
302 None
303 }
304 }
305
306 pub fn is_local(&self) -> bool {
307 matches!(self, Worktree::Local(_))
308 }
309
310 pub fn is_remote(&self) -> bool {
311 !self.is_local()
312 }
313
314 pub fn snapshot(&self) -> Snapshot {
315 match self {
316 Worktree::Local(worktree) => worktree.snapshot().snapshot,
317 Worktree::Remote(worktree) => worktree.snapshot(),
318 }
319 }
320
321 pub fn scan_id(&self) -> usize {
322 match self {
323 Worktree::Local(worktree) => worktree.snapshot.scan_id,
324 Worktree::Remote(worktree) => worktree.snapshot.scan_id,
325 }
326 }
327
328 pub fn is_visible(&self) -> bool {
329 match self {
330 Worktree::Local(worktree) => worktree.visible,
331 Worktree::Remote(worktree) => worktree.visible,
332 }
333 }
334
335 pub fn replica_id(&self) -> ReplicaId {
336 match self {
337 Worktree::Local(_) => 0,
338 Worktree::Remote(worktree) => worktree.replica_id,
339 }
340 }
341
342 pub fn diagnostic_summaries(
343 &self,
344 ) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + '_ {
345 match self {
346 Worktree::Local(worktree) => &worktree.diagnostic_summaries,
347 Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
348 }
349 .iter()
350 .map(|(path, summary)| (path.0.clone(), *summary))
351 }
352
353 fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
354 match self {
355 Self::Local(worktree) => worktree.poll_snapshot(false, cx),
356 Self::Remote(worktree) => worktree.poll_snapshot(cx),
357 };
358 }
359}
360
361impl LocalWorktree {
362 async fn create(
363 client: Arc<Client>,
364 path: impl Into<Arc<Path>>,
365 visible: bool,
366 fs: Arc<dyn Fs>,
367 next_entry_id: Arc<AtomicUsize>,
368 cx: &mut AsyncAppContext,
369 ) -> Result<(ModelHandle<Worktree>, UnboundedSender<ScanState>)> {
370 let abs_path = path.into();
371 let path: Arc<Path> = Arc::from(Path::new(""));
372
373 // After determining whether the root entry is a file or a directory, populate the
374 // snapshot's "root name", which will be used for the purpose of fuzzy matching.
375 let root_name = abs_path
376 .file_name()
377 .map_or(String::new(), |f| f.to_string_lossy().to_string());
378 let root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
379 let metadata = fs
380 .metadata(&abs_path)
381 .await
382 .context("failed to stat worktree path")?;
383
384 let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
385 let (mut last_scan_state_tx, last_scan_state_rx) =
386 watch::channel_with(ScanState::Initializing);
387 let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
388 let mut snapshot = LocalSnapshot {
389 abs_path,
390 ignores_by_parent_abs_path: Default::default(),
391 git_repositories: Default::default(),
392 removed_entry_ids: Default::default(),
393 next_entry_id,
394 snapshot: Snapshot {
395 id: WorktreeId::from_usize(cx.model_id()),
396 root_name: root_name.clone(),
397 root_char_bag,
398 entries_by_path: Default::default(),
399 entries_by_id: Default::default(),
400 scan_id: 0,
401 is_complete: true,
402 },
403 extension_counts: Default::default(),
404 };
405 if let Some(metadata) = metadata {
406 let entry = Entry::new(
407 path,
408 &metadata,
409 &snapshot.next_entry_id,
410 snapshot.root_char_bag,
411 );
412 snapshot.insert_entry(entry, fs.as_ref());
413 }
414
415 let tree = Self {
416 snapshot: snapshot.clone(),
417 background_snapshot: Arc::new(Mutex::new(snapshot)),
418 last_scan_state_rx,
419 _background_scanner_task: None,
420 share: None,
421 poll_task: None,
422 diagnostics: Default::default(),
423 diagnostic_summaries: Default::default(),
424 client,
425 fs,
426 visible,
427 };
428
429 cx.spawn_weak(|this, mut cx| async move {
430 while let Some(scan_state) = scan_states_rx.next().await {
431 if let Some(this) = this.upgrade(&cx) {
432 last_scan_state_tx.blocking_send(scan_state).ok();
433 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
434 } else {
435 break;
436 }
437 }
438 })
439 .detach();
440
441 Worktree::Local(tree)
442 });
443
444 Ok((tree, scan_states_tx))
445 }
446
447 pub fn contains_abs_path(&self, path: &Path) -> bool {
448 path.starts_with(&self.abs_path)
449 }
450
451 fn absolutize(&self, path: &Path) -> PathBuf {
452 if path.file_name().is_some() {
453 self.abs_path.join(path)
454 } else {
455 self.abs_path.to_path_buf()
456 }
457 }
458
459 pub(crate) fn load_buffer(
460 &mut self,
461 path: &Path,
462 cx: &mut ModelContext<Worktree>,
463 ) -> Task<Result<ModelHandle<Buffer>>> {
464 let path = Arc::from(path);
465 cx.spawn(move |this, mut cx| async move {
466 let (file, contents, head_text) = this
467 .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
468 .await?;
469 Ok(cx.add_model(|cx| {
470 let mut buffer = Buffer::from_file(0, contents, head_text, Arc::new(file), cx);
471 buffer.update_git(cx);
472 buffer
473 }))
474 })
475 }
476
477 pub fn diagnostics_for_path(&self, path: &Path) -> Option<Vec<DiagnosticEntry<PointUtf16>>> {
478 self.diagnostics.get(path).cloned()
479 }
480
481 pub fn update_diagnostics(
482 &mut self,
483 language_server_id: usize,
484 worktree_path: Arc<Path>,
485 diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
486 _: &mut ModelContext<Worktree>,
487 ) -> Result<bool> {
488 self.diagnostics.remove(&worktree_path);
489 let old_summary = self
490 .diagnostic_summaries
491 .remove(&PathKey(worktree_path.clone()))
492 .unwrap_or_default();
493 let new_summary = DiagnosticSummary::new(language_server_id, &diagnostics);
494 if !new_summary.is_empty() {
495 self.diagnostic_summaries
496 .insert(PathKey(worktree_path.clone()), new_summary);
497 self.diagnostics.insert(worktree_path.clone(), diagnostics);
498 }
499
500 let updated = !old_summary.is_empty() || !new_summary.is_empty();
501 if updated {
502 if let Some(share) = self.share.as_ref() {
503 self.client
504 .send(proto::UpdateDiagnosticSummary {
505 project_id: share.project_id,
506 worktree_id: self.id().to_proto(),
507 summary: Some(proto::DiagnosticSummary {
508 path: worktree_path.to_string_lossy().to_string(),
509 language_server_id: language_server_id as u64,
510 error_count: new_summary.error_count as u32,
511 warning_count: new_summary.warning_count as u32,
512 }),
513 })
514 .log_err();
515 }
516 }
517
518 Ok(updated)
519 }
520
521 fn poll_snapshot(&mut self, force: bool, cx: &mut ModelContext<Worktree>) {
522 self.poll_task.take();
523
524 match self.scan_state() {
525 ScanState::Idle => {
526 self.snapshot = self.background_snapshot.lock().clone();
527 if let Some(share) = self.share.as_mut() {
528 *share.snapshots_tx.borrow_mut() = self.snapshot.clone();
529 }
530 cx.emit(Event::UpdatedEntries);
531 }
532
533 ScanState::Initializing => {
534 let is_fake_fs = self.fs.is_fake();
535 self.snapshot = self.background_snapshot.lock().clone();
536 self.poll_task = Some(cx.spawn_weak(|this, mut cx| async move {
537 if is_fake_fs {
538 #[cfg(any(test, feature = "test-support"))]
539 cx.background().simulate_random_delay().await;
540 } else {
541 smol::Timer::after(Duration::from_millis(100)).await;
542 }
543 if let Some(this) = this.upgrade(&cx) {
544 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
545 }
546 }));
547 cx.emit(Event::UpdatedEntries);
548 }
549
550 _ => {
551 if force {
552 self.snapshot = self.background_snapshot.lock().clone();
553 }
554 }
555 }
556
557 cx.notify();
558 }
559
560 pub fn scan_complete(&self) -> impl Future<Output = ()> {
561 let mut scan_state_rx = self.last_scan_state_rx.clone();
562 async move {
563 let mut scan_state = Some(scan_state_rx.borrow().clone());
564 while let Some(ScanState::Initializing | ScanState::Updating) = scan_state {
565 scan_state = scan_state_rx.recv().await;
566 }
567 }
568 }
569
570 fn scan_state(&self) -> ScanState {
571 self.last_scan_state_rx.borrow().clone()
572 }
573
574 pub fn snapshot(&self) -> LocalSnapshot {
575 self.snapshot.clone()
576 }
577
578 pub fn metadata_proto(&self) -> proto::WorktreeMetadata {
579 proto::WorktreeMetadata {
580 id: self.id().to_proto(),
581 root_name: self.root_name().to_string(),
582 visible: self.visible,
583 }
584 }
585
586 fn load(
587 &self,
588 path: &Path,
589 cx: &mut ModelContext<Worktree>,
590 ) -> Task<Result<(File, String, Option<String>)>> {
591 let handle = cx.handle();
592 let path = Arc::from(path);
593 let abs_path = self.absolutize(&path);
594 let fs = self.fs.clone();
595
596 let files_included = cx
597 .global::<Settings>()
598 .editor_overrides
599 .git_gutter
600 .unwrap_or_default()
601 .files_included;
602
603 cx.spawn(|this, mut cx| async move {
604 let text = fs.load(&abs_path).await?;
605
606 let head_text = if matches!(
607 files_included,
608 settings::GitFilesIncluded::All | settings::GitFilesIncluded::OnlyTracked
609 ) {
610 let fs = fs.clone();
611 let abs_path = abs_path.clone();
612 let task = async move { fs.load_head_text(&abs_path).await };
613 let results = cx.background().spawn(task).await;
614
615 if files_included == settings::GitFilesIncluded::All {
616 results.or_else(|| Some(text.clone()))
617 } else {
618 results
619 }
620 } else {
621 None
622 };
623
624 // Eagerly populate the snapshot with an updated entry for the loaded file
625 let entry = this
626 .update(&mut cx, |this, cx| {
627 this.as_local()
628 .unwrap()
629 .refresh_entry(path, abs_path, None, cx)
630 })
631 .await?;
632
633 Ok((
634 File {
635 entry_id: Some(entry.id),
636 worktree: handle,
637 path: entry.path,
638 mtime: entry.mtime,
639 is_local: true,
640 },
641 text,
642 head_text,
643 ))
644 })
645 }
646
647 pub fn save_buffer_as(
648 &self,
649 buffer_handle: ModelHandle<Buffer>,
650 path: impl Into<Arc<Path>>,
651 cx: &mut ModelContext<Worktree>,
652 ) -> Task<Result<()>> {
653 let buffer = buffer_handle.read(cx);
654 let text = buffer.as_rope().clone();
655 let fingerprint = text.fingerprint();
656 let version = buffer.version();
657 let save = self.write_file(path, text, buffer.line_ending(), cx);
658 let handle = cx.handle();
659 cx.as_mut().spawn(|mut cx| async move {
660 let entry = save.await?;
661 let file = File {
662 entry_id: Some(entry.id),
663 worktree: handle,
664 path: entry.path,
665 mtime: entry.mtime,
666 is_local: true,
667 };
668
669 buffer_handle.update(&mut cx, |buffer, cx| {
670 buffer.did_save(version, fingerprint, file.mtime, Some(Arc::new(file)), cx);
671 });
672
673 Ok(())
674 })
675 }
676
677 pub fn create_entry(
678 &self,
679 path: impl Into<Arc<Path>>,
680 is_dir: bool,
681 cx: &mut ModelContext<Worktree>,
682 ) -> Task<Result<Entry>> {
683 self.write_entry_internal(
684 path,
685 if is_dir {
686 None
687 } else {
688 Some(Default::default())
689 },
690 cx,
691 )
692 }
693
694 pub fn write_file(
695 &self,
696 path: impl Into<Arc<Path>>,
697 text: Rope,
698 line_ending: LineEnding,
699 cx: &mut ModelContext<Worktree>,
700 ) -> Task<Result<Entry>> {
701 self.write_entry_internal(path, Some((text, line_ending)), cx)
702 }
703
704 pub fn delete_entry(
705 &self,
706 entry_id: ProjectEntryId,
707 cx: &mut ModelContext<Worktree>,
708 ) -> Option<Task<Result<()>>> {
709 let entry = self.entry_for_id(entry_id)?.clone();
710 let abs_path = self.absolutize(&entry.path);
711 let delete = cx.background().spawn({
712 let fs = self.fs.clone();
713 let abs_path = abs_path;
714 async move {
715 if entry.is_file() {
716 fs.remove_file(&abs_path, Default::default()).await
717 } else {
718 fs.remove_dir(
719 &abs_path,
720 RemoveOptions {
721 recursive: true,
722 ignore_if_not_exists: false,
723 },
724 )
725 .await
726 }
727 }
728 });
729
730 Some(cx.spawn(|this, mut cx| async move {
731 delete.await?;
732 this.update(&mut cx, |this, cx| {
733 let this = this.as_local_mut().unwrap();
734 {
735 let mut snapshot = this.background_snapshot.lock();
736 snapshot.delete_entry(entry_id);
737 }
738 this.poll_snapshot(true, cx);
739 });
740 Ok(())
741 }))
742 }
743
744 pub fn rename_entry(
745 &self,
746 entry_id: ProjectEntryId,
747 new_path: impl Into<Arc<Path>>,
748 cx: &mut ModelContext<Worktree>,
749 ) -> Option<Task<Result<Entry>>> {
750 let old_path = self.entry_for_id(entry_id)?.path.clone();
751 let new_path = new_path.into();
752 let abs_old_path = self.absolutize(&old_path);
753 let abs_new_path = self.absolutize(&new_path);
754 let rename = cx.background().spawn({
755 let fs = self.fs.clone();
756 let abs_new_path = abs_new_path.clone();
757 async move {
758 fs.rename(&abs_old_path, &abs_new_path, Default::default())
759 .await
760 }
761 });
762
763 Some(cx.spawn(|this, mut cx| async move {
764 rename.await?;
765 let entry = this
766 .update(&mut cx, |this, cx| {
767 this.as_local_mut().unwrap().refresh_entry(
768 new_path.clone(),
769 abs_new_path,
770 Some(old_path),
771 cx,
772 )
773 })
774 .await?;
775 Ok(entry)
776 }))
777 }
778
779 pub fn copy_entry(
780 &self,
781 entry_id: ProjectEntryId,
782 new_path: impl Into<Arc<Path>>,
783 cx: &mut ModelContext<Worktree>,
784 ) -> Option<Task<Result<Entry>>> {
785 let old_path = self.entry_for_id(entry_id)?.path.clone();
786 let new_path = new_path.into();
787 let abs_old_path = self.absolutize(&old_path);
788 let abs_new_path = self.absolutize(&new_path);
789 let copy = cx.background().spawn({
790 let fs = self.fs.clone();
791 let abs_new_path = abs_new_path.clone();
792 async move {
793 copy_recursive(
794 fs.as_ref(),
795 &abs_old_path,
796 &abs_new_path,
797 Default::default(),
798 )
799 .await
800 }
801 });
802
803 Some(cx.spawn(|this, mut cx| async move {
804 copy.await?;
805 let entry = this
806 .update(&mut cx, |this, cx| {
807 this.as_local_mut().unwrap().refresh_entry(
808 new_path.clone(),
809 abs_new_path,
810 None,
811 cx,
812 )
813 })
814 .await?;
815 Ok(entry)
816 }))
817 }
818
819 fn write_entry_internal(
820 &self,
821 path: impl Into<Arc<Path>>,
822 text_if_file: Option<(Rope, LineEnding)>,
823 cx: &mut ModelContext<Worktree>,
824 ) -> Task<Result<Entry>> {
825 let path = path.into();
826 let abs_path = self.absolutize(&path);
827 let write = cx.background().spawn({
828 let fs = self.fs.clone();
829 let abs_path = abs_path.clone();
830 async move {
831 if let Some((text, line_ending)) = text_if_file {
832 fs.save(&abs_path, &text, line_ending).await
833 } else {
834 fs.create_dir(&abs_path).await
835 }
836 }
837 });
838
839 cx.spawn(|this, mut cx| async move {
840 write.await?;
841 let entry = this
842 .update(&mut cx, |this, cx| {
843 this.as_local_mut()
844 .unwrap()
845 .refresh_entry(path, abs_path, None, cx)
846 })
847 .await?;
848 Ok(entry)
849 })
850 }
851
852 fn refresh_entry(
853 &self,
854 path: Arc<Path>,
855 abs_path: PathBuf,
856 old_path: Option<Arc<Path>>,
857 cx: &mut ModelContext<Worktree>,
858 ) -> Task<Result<Entry>> {
859 let fs = self.fs.clone();
860 let root_char_bag;
861 let next_entry_id;
862 {
863 let snapshot = self.background_snapshot.lock();
864 root_char_bag = snapshot.root_char_bag;
865 next_entry_id = snapshot.next_entry_id.clone();
866 }
867 cx.spawn_weak(|this, mut cx| async move {
868 let metadata = fs
869 .metadata(&abs_path)
870 .await?
871 .ok_or_else(|| anyhow!("could not read saved file metadata"))?;
872 let this = this
873 .upgrade(&cx)
874 .ok_or_else(|| anyhow!("worktree was dropped"))?;
875 this.update(&mut cx, |this, cx| {
876 let this = this.as_local_mut().unwrap();
877 let inserted_entry;
878 {
879 let mut snapshot = this.background_snapshot.lock();
880 let mut entry = Entry::new(path, &metadata, &next_entry_id, root_char_bag);
881 entry.is_ignored = snapshot
882 .ignore_stack_for_abs_path(&abs_path, entry.is_dir())
883 .is_abs_path_ignored(&abs_path, entry.is_dir());
884 if let Some(old_path) = old_path {
885 snapshot.remove_path(&old_path);
886 }
887 inserted_entry = snapshot.insert_entry(entry, fs.as_ref());
888 snapshot.scan_id += 1;
889 }
890 this.poll_snapshot(true, cx);
891 Ok(inserted_entry)
892 })
893 })
894 }
895
896 pub fn share(&mut self, project_id: u64, cx: &mut ModelContext<Worktree>) -> Task<Result<()>> {
897 let (share_tx, share_rx) = oneshot::channel();
898
899 if self.share.is_some() {
900 let _ = share_tx.send(Ok(()));
901 } else {
902 let (snapshots_tx, mut snapshots_rx) = watch::channel_with(self.snapshot());
903 let rpc = self.client.clone();
904 let worktree_id = cx.model_id() as u64;
905 let maintain_remote_snapshot = cx.background().spawn({
906 let rpc = rpc;
907 let diagnostic_summaries = self.diagnostic_summaries.clone();
908 async move {
909 let mut prev_snapshot = match snapshots_rx.recv().await {
910 Some(snapshot) => {
911 let update = proto::UpdateWorktree {
912 project_id,
913 worktree_id,
914 root_name: snapshot.root_name().to_string(),
915 updated_entries: snapshot
916 .entries_by_path
917 .iter()
918 .map(Into::into)
919 .collect(),
920 removed_entries: Default::default(),
921 scan_id: snapshot.scan_id as u64,
922 is_last_update: true,
923 };
924 if let Err(error) = send_worktree_update(&rpc, update).await {
925 let _ = share_tx.send(Err(error));
926 return Err(anyhow!("failed to send initial update worktree"));
927 } else {
928 let _ = share_tx.send(Ok(()));
929 snapshot
930 }
931 }
932 None => {
933 share_tx
934 .send(Err(anyhow!("worktree dropped before share completed")))
935 .ok();
936 return Err(anyhow!("failed to send initial update worktree"));
937 }
938 };
939
940 for (path, summary) in diagnostic_summaries.iter() {
941 rpc.send(proto::UpdateDiagnosticSummary {
942 project_id,
943 worktree_id,
944 summary: Some(summary.to_proto(&path.0)),
945 })?;
946 }
947
948 while let Some(snapshot) = snapshots_rx.recv().await {
949 send_worktree_update(
950 &rpc,
951 snapshot.build_update(&prev_snapshot, project_id, worktree_id, true),
952 )
953 .await?;
954 prev_snapshot = snapshot;
955 }
956
957 Ok::<_, anyhow::Error>(())
958 }
959 .log_err()
960 });
961 self.share = Some(ShareState {
962 project_id,
963 snapshots_tx,
964 _maintain_remote_snapshot: Some(maintain_remote_snapshot),
965 });
966 }
967
968 cx.foreground().spawn(async move {
969 share_rx
970 .await
971 .unwrap_or_else(|_| Err(anyhow!("share ended")))
972 })
973 }
974
975 pub fn unshare(&mut self) {
976 self.share.take();
977 }
978
979 pub fn is_shared(&self) -> bool {
980 self.share.is_some()
981 }
982
983 pub fn send_extension_counts(&self, project_id: u64) {
984 let mut extensions = Vec::new();
985 let mut counts = Vec::new();
986
987 for (extension, count) in self.extension_counts() {
988 extensions.push(extension.to_string_lossy().to_string());
989 counts.push(*count as u32);
990 }
991
992 self.client
993 .send(proto::UpdateWorktreeExtensions {
994 project_id,
995 worktree_id: self.id().to_proto(),
996 extensions,
997 counts,
998 })
999 .log_err();
1000 }
1001}
1002
1003impl RemoteWorktree {
1004 fn snapshot(&self) -> Snapshot {
1005 self.snapshot.clone()
1006 }
1007
1008 fn poll_snapshot(&mut self, cx: &mut ModelContext<Worktree>) {
1009 self.snapshot = self.background_snapshot.lock().clone();
1010 cx.emit(Event::UpdatedEntries);
1011 cx.notify();
1012 }
1013
1014 pub fn disconnected_from_host(&mut self) {
1015 self.updates_tx.take();
1016 self.snapshot_subscriptions.clear();
1017 }
1018
1019 pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) {
1020 if let Some(updates_tx) = &self.updates_tx {
1021 updates_tx
1022 .unbounded_send(update)
1023 .expect("consumer runs to completion");
1024 }
1025 }
1026
1027 fn observed_snapshot(&self, scan_id: usize) -> bool {
1028 self.scan_id > scan_id || (self.scan_id == scan_id && self.is_complete)
1029 }
1030
1031 fn wait_for_snapshot(&mut self, scan_id: usize) -> impl Future<Output = ()> {
1032 let (tx, rx) = oneshot::channel();
1033 if self.observed_snapshot(scan_id) {
1034 let _ = tx.send(());
1035 } else {
1036 match self
1037 .snapshot_subscriptions
1038 .binary_search_by_key(&scan_id, |probe| probe.0)
1039 {
1040 Ok(ix) | Err(ix) => self.snapshot_subscriptions.insert(ix, (scan_id, tx)),
1041 }
1042 }
1043
1044 async move {
1045 let _ = rx.await;
1046 }
1047 }
1048
1049 pub fn update_diagnostic_summary(
1050 &mut self,
1051 path: Arc<Path>,
1052 summary: &proto::DiagnosticSummary,
1053 ) {
1054 let summary = DiagnosticSummary {
1055 language_server_id: summary.language_server_id as usize,
1056 error_count: summary.error_count as usize,
1057 warning_count: summary.warning_count as usize,
1058 };
1059 if summary.is_empty() {
1060 self.diagnostic_summaries.remove(&PathKey(path));
1061 } else {
1062 self.diagnostic_summaries.insert(PathKey(path), summary);
1063 }
1064 }
1065
1066 pub fn insert_entry(
1067 &mut self,
1068 entry: proto::Entry,
1069 scan_id: usize,
1070 cx: &mut ModelContext<Worktree>,
1071 ) -> Task<Result<Entry>> {
1072 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1073 cx.spawn(|this, mut cx| async move {
1074 wait_for_snapshot.await;
1075 this.update(&mut cx, |worktree, _| {
1076 let worktree = worktree.as_remote_mut().unwrap();
1077 let mut snapshot = worktree.background_snapshot.lock();
1078 let entry = snapshot.insert_entry(entry);
1079 worktree.snapshot = snapshot.clone();
1080 entry
1081 })
1082 })
1083 }
1084
1085 pub(crate) fn delete_entry(
1086 &mut self,
1087 id: ProjectEntryId,
1088 scan_id: usize,
1089 cx: &mut ModelContext<Worktree>,
1090 ) -> Task<Result<()>> {
1091 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1092 cx.spawn(|this, mut cx| async move {
1093 wait_for_snapshot.await;
1094 this.update(&mut cx, |worktree, _| {
1095 let worktree = worktree.as_remote_mut().unwrap();
1096 let mut snapshot = worktree.background_snapshot.lock();
1097 snapshot.delete_entry(id);
1098 worktree.snapshot = snapshot.clone();
1099 });
1100 Ok(())
1101 })
1102 }
1103}
1104
1105impl Snapshot {
1106 pub fn id(&self) -> WorktreeId {
1107 self.id
1108 }
1109
1110 pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
1111 self.entries_by_id.get(&entry_id, &()).is_some()
1112 }
1113
1114 pub(crate) fn insert_entry(&mut self, entry: proto::Entry) -> Result<Entry> {
1115 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1116 let old_entry = self.entries_by_id.insert_or_replace(
1117 PathEntry {
1118 id: entry.id,
1119 path: entry.path.clone(),
1120 is_ignored: entry.is_ignored,
1121 scan_id: 0,
1122 },
1123 &(),
1124 );
1125 if let Some(old_entry) = old_entry {
1126 self.entries_by_path.remove(&PathKey(old_entry.path), &());
1127 }
1128 self.entries_by_path.insert_or_replace(entry.clone(), &());
1129 Ok(entry)
1130 }
1131
1132 fn delete_entry(&mut self, entry_id: ProjectEntryId) -> bool {
1133 if let Some(removed_entry) = self.entries_by_id.remove(&entry_id, &()) {
1134 self.entries_by_path = {
1135 let mut cursor = self.entries_by_path.cursor();
1136 let mut new_entries_by_path =
1137 cursor.slice(&TraversalTarget::Path(&removed_entry.path), Bias::Left, &());
1138 while let Some(entry) = cursor.item() {
1139 if entry.path.starts_with(&removed_entry.path) {
1140 self.entries_by_id.remove(&entry.id, &());
1141 cursor.next(&());
1142 } else {
1143 break;
1144 }
1145 }
1146 new_entries_by_path.push_tree(cursor.suffix(&()), &());
1147 new_entries_by_path
1148 };
1149
1150 true
1151 } else {
1152 false
1153 }
1154 }
1155
1156 pub(crate) fn apply_remote_update(&mut self, update: proto::UpdateWorktree) -> Result<()> {
1157 let mut entries_by_path_edits = Vec::new();
1158 let mut entries_by_id_edits = Vec::new();
1159 for entry_id in update.removed_entries {
1160 let entry = self
1161 .entry_for_id(ProjectEntryId::from_proto(entry_id))
1162 .ok_or_else(|| anyhow!("unknown entry {}", entry_id))?;
1163 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
1164 entries_by_id_edits.push(Edit::Remove(entry.id));
1165 }
1166
1167 for entry in update.updated_entries {
1168 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1169 if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
1170 entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
1171 }
1172 entries_by_id_edits.push(Edit::Insert(PathEntry {
1173 id: entry.id,
1174 path: entry.path.clone(),
1175 is_ignored: entry.is_ignored,
1176 scan_id: 0,
1177 }));
1178 entries_by_path_edits.push(Edit::Insert(entry));
1179 }
1180
1181 self.entries_by_path.edit(entries_by_path_edits, &());
1182 self.entries_by_id.edit(entries_by_id_edits, &());
1183 self.scan_id = update.scan_id as usize;
1184 self.is_complete = update.is_last_update;
1185
1186 Ok(())
1187 }
1188
1189 pub fn file_count(&self) -> usize {
1190 self.entries_by_path.summary().file_count
1191 }
1192
1193 pub fn visible_file_count(&self) -> usize {
1194 self.entries_by_path.summary().visible_file_count
1195 }
1196
1197 fn traverse_from_offset(
1198 &self,
1199 include_dirs: bool,
1200 include_ignored: bool,
1201 start_offset: usize,
1202 ) -> Traversal {
1203 let mut cursor = self.entries_by_path.cursor();
1204 cursor.seek(
1205 &TraversalTarget::Count {
1206 count: start_offset,
1207 include_dirs,
1208 include_ignored,
1209 },
1210 Bias::Right,
1211 &(),
1212 );
1213 Traversal {
1214 cursor,
1215 include_dirs,
1216 include_ignored,
1217 }
1218 }
1219
1220 fn traverse_from_path(
1221 &self,
1222 include_dirs: bool,
1223 include_ignored: bool,
1224 path: &Path,
1225 ) -> Traversal {
1226 let mut cursor = self.entries_by_path.cursor();
1227 cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1228 Traversal {
1229 cursor,
1230 include_dirs,
1231 include_ignored,
1232 }
1233 }
1234
1235 pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1236 self.traverse_from_offset(false, include_ignored, start)
1237 }
1238
1239 pub fn entries(&self, include_ignored: bool) -> Traversal {
1240 self.traverse_from_offset(true, include_ignored, 0)
1241 }
1242
1243 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1244 let empty_path = Path::new("");
1245 self.entries_by_path
1246 .cursor::<()>()
1247 .filter(move |entry| entry.path.as_ref() != empty_path)
1248 .map(|entry| &entry.path)
1249 }
1250
1251 fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
1252 let mut cursor = self.entries_by_path.cursor();
1253 cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
1254 let traversal = Traversal {
1255 cursor,
1256 include_dirs: true,
1257 include_ignored: true,
1258 };
1259 ChildEntriesIter {
1260 traversal,
1261 parent_path,
1262 }
1263 }
1264
1265 pub fn root_entry(&self) -> Option<&Entry> {
1266 self.entry_for_path("")
1267 }
1268
1269 pub fn root_name(&self) -> &str {
1270 &self.root_name
1271 }
1272
1273 pub fn scan_id(&self) -> usize {
1274 self.scan_id
1275 }
1276
1277 pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
1278 let path = path.as_ref();
1279 self.traverse_from_path(true, true, path)
1280 .entry()
1281 .and_then(|entry| {
1282 if entry.path.as_ref() == path {
1283 Some(entry)
1284 } else {
1285 None
1286 }
1287 })
1288 }
1289
1290 pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
1291 let entry = self.entries_by_id.get(&id, &())?;
1292 self.entry_for_path(&entry.path)
1293 }
1294
1295 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
1296 self.entry_for_path(path.as_ref()).map(|e| e.inode)
1297 }
1298}
1299
1300impl LocalSnapshot {
1301 pub fn abs_path(&self) -> &Arc<Path> {
1302 &self.abs_path
1303 }
1304
1305 pub fn extension_counts(&self) -> &HashMap<OsString, usize> {
1306 &self.extension_counts
1307 }
1308
1309 // Gives the most specific git repository for a given path
1310 pub(crate) fn git_repository_for_file_path(
1311 &self,
1312 path: &Path,
1313 ) -> Option<Box<dyn GitRepository>> {
1314 let repos = self
1315 .git_repositories
1316 .iter()
1317 .map(|repo| repo.content_path().to_str().unwrap().to_string())
1318 .collect::<Vec<String>>();
1319 dbg!(repos);
1320
1321 self.git_repositories
1322 .iter()
1323 .rev() //git_repository is ordered lexicographically
1324 .find(|repo| repo.is_path_managed_by(path))
1325 .map(|repo| repo.boxed_clone())
1326 }
1327
1328 // ~/zed:
1329 // - src
1330 // - crates
1331 // - .git -> /usr/.git
1332 pub(crate) fn git_repository_for_git_data(
1333 &self,
1334 path: &Path,
1335 ) -> Option<Box<dyn GitRepository>> {
1336 self.git_repositories
1337 .iter()
1338 .find(|repo| repo.is_path_in_git_folder(path))
1339 .map(|repo| repo.boxed_clone())
1340 }
1341
1342 pub(crate) fn does_git_repository_track_file_path(
1343 &self,
1344 repo: &Box<dyn GitRepository>,
1345 file_path: &Path,
1346 ) -> bool {
1347 // /zed
1348 // - .git
1349 // - a.txt
1350 // - /dep
1351 // - b.txt
1352 // - .git
1353
1354 // Depends on git_repository_for_file_path returning the most specific git repository for a given path
1355 self.git_repository_for_file_path(file_path)
1356 .map_or(false, |r| &r == repo)
1357 }
1358
1359 #[cfg(test)]
1360 pub(crate) fn build_initial_update(&self, project_id: u64) -> proto::UpdateWorktree {
1361 let root_name = self.root_name.clone();
1362 proto::UpdateWorktree {
1363 project_id,
1364 worktree_id: self.id().to_proto(),
1365 root_name,
1366 updated_entries: self.entries_by_path.iter().map(Into::into).collect(),
1367 removed_entries: Default::default(),
1368 scan_id: self.scan_id as u64,
1369 is_last_update: true,
1370 }
1371 }
1372
1373 pub(crate) fn build_update(
1374 &self,
1375 other: &Self,
1376 project_id: u64,
1377 worktree_id: u64,
1378 include_ignored: bool,
1379 ) -> proto::UpdateWorktree {
1380 let mut updated_entries = Vec::new();
1381 let mut removed_entries = Vec::new();
1382 let mut self_entries = self
1383 .entries_by_id
1384 .cursor::<()>()
1385 .filter(|e| include_ignored || !e.is_ignored)
1386 .peekable();
1387 let mut other_entries = other
1388 .entries_by_id
1389 .cursor::<()>()
1390 .filter(|e| include_ignored || !e.is_ignored)
1391 .peekable();
1392 loop {
1393 match (self_entries.peek(), other_entries.peek()) {
1394 (Some(self_entry), Some(other_entry)) => {
1395 match Ord::cmp(&self_entry.id, &other_entry.id) {
1396 Ordering::Less => {
1397 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1398 updated_entries.push(entry);
1399 self_entries.next();
1400 }
1401 Ordering::Equal => {
1402 if self_entry.scan_id != other_entry.scan_id {
1403 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1404 updated_entries.push(entry);
1405 }
1406
1407 self_entries.next();
1408 other_entries.next();
1409 }
1410 Ordering::Greater => {
1411 removed_entries.push(other_entry.id.to_proto());
1412 other_entries.next();
1413 }
1414 }
1415 }
1416 (Some(self_entry), None) => {
1417 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1418 updated_entries.push(entry);
1419 self_entries.next();
1420 }
1421 (None, Some(other_entry)) => {
1422 removed_entries.push(other_entry.id.to_proto());
1423 other_entries.next();
1424 }
1425 (None, None) => break,
1426 }
1427 }
1428
1429 proto::UpdateWorktree {
1430 project_id,
1431 worktree_id,
1432 root_name: self.root_name().to_string(),
1433 updated_entries,
1434 removed_entries,
1435 scan_id: self.scan_id as u64,
1436 is_last_update: true,
1437 }
1438 }
1439
1440 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
1441 dbg!(&entry.path);
1442 if entry.is_file() && entry.path.file_name() == Some(&GITIGNORE) {
1443 let abs_path = self.abs_path.join(&entry.path);
1444 match smol::block_on(build_gitignore(&abs_path, fs)) {
1445 Ok(ignore) => {
1446 self.ignores_by_parent_abs_path.insert(
1447 abs_path.parent().unwrap().into(),
1448 (Arc::new(ignore), self.scan_id),
1449 );
1450 }
1451 Err(error) => {
1452 log::error!(
1453 "error loading .gitignore file {:?} - {:?}",
1454 &entry.path,
1455 error
1456 );
1457 }
1458 }
1459 } else if entry.path.file_name() == Some(&DOT_GIT) {
1460 dbg!(&entry.path);
1461
1462 let abs_path = self.abs_path.join(&entry.path);
1463 let content_path: Arc<Path> = entry.path.parent().unwrap().into();
1464 if let Err(ix) = self
1465 .git_repositories
1466 .binary_search_by_key(&content_path.as_ref(), |repo| repo.content_path())
1467 {
1468 if let Some(repository) = fs.open_git_repository(&abs_path, &content_path) {
1469 self.git_repositories.insert(ix, repository);
1470 }
1471 }
1472 }
1473
1474 self.reuse_entry_id(&mut entry);
1475
1476 if entry.kind == EntryKind::PendingDir {
1477 if let Some(existing_entry) =
1478 self.entries_by_path.get(&PathKey(entry.path.clone()), &())
1479 {
1480 entry.kind = existing_entry.kind;
1481 }
1482 }
1483
1484 self.entries_by_path.insert_or_replace(entry.clone(), &());
1485 let scan_id = self.scan_id;
1486 let removed_entry = self.entries_by_id.insert_or_replace(
1487 PathEntry {
1488 id: entry.id,
1489 path: entry.path.clone(),
1490 is_ignored: entry.is_ignored,
1491 scan_id,
1492 },
1493 &(),
1494 );
1495
1496 if let Some(removed_entry) = removed_entry {
1497 self.dec_extension_count(&removed_entry.path, removed_entry.is_ignored);
1498 }
1499 self.inc_extension_count(&entry.path, entry.is_ignored);
1500
1501 entry
1502 }
1503
1504 fn populate_dir(
1505 &mut self,
1506 parent_path: Arc<Path>,
1507 entries: impl IntoIterator<Item = Entry>,
1508 ignore: Option<Arc<Gitignore>>,
1509 ) {
1510 let mut parent_entry = if let Some(parent_entry) =
1511 self.entries_by_path.get(&PathKey(parent_path.clone()), &())
1512 {
1513 parent_entry.clone()
1514 } else {
1515 log::warn!(
1516 "populating a directory {:?} that has been removed",
1517 parent_path
1518 );
1519 return;
1520 };
1521
1522 if let Some(ignore) = ignore {
1523 self.ignores_by_parent_abs_path.insert(
1524 self.abs_path.join(&parent_path).into(),
1525 (ignore, self.scan_id),
1526 );
1527 }
1528 if matches!(parent_entry.kind, EntryKind::PendingDir) {
1529 parent_entry.kind = EntryKind::Dir;
1530 } else {
1531 unreachable!();
1532 }
1533
1534 let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
1535 let mut entries_by_id_edits = Vec::new();
1536
1537 for mut entry in entries {
1538 self.reuse_entry_id(&mut entry);
1539 self.inc_extension_count(&entry.path, entry.is_ignored);
1540 entries_by_id_edits.push(Edit::Insert(PathEntry {
1541 id: entry.id,
1542 path: entry.path.clone(),
1543 is_ignored: entry.is_ignored,
1544 scan_id: self.scan_id,
1545 }));
1546 entries_by_path_edits.push(Edit::Insert(entry));
1547 }
1548
1549 self.entries_by_path.edit(entries_by_path_edits, &());
1550 let removed_entries = self.entries_by_id.edit(entries_by_id_edits, &());
1551
1552 for removed_entry in removed_entries {
1553 self.dec_extension_count(&removed_entry.path, removed_entry.is_ignored);
1554 }
1555 }
1556
1557 fn inc_extension_count(&mut self, path: &Path, ignored: bool) {
1558 if !ignored {
1559 if let Some(extension) = path.extension() {
1560 if let Some(count) = self.extension_counts.get_mut(extension) {
1561 *count += 1;
1562 } else {
1563 self.extension_counts.insert(extension.into(), 1);
1564 }
1565 }
1566 }
1567 }
1568
1569 fn dec_extension_count(&mut self, path: &Path, ignored: bool) {
1570 if !ignored {
1571 if let Some(extension) = path.extension() {
1572 if let Some(count) = self.extension_counts.get_mut(extension) {
1573 *count -= 1;
1574 }
1575 }
1576 }
1577 }
1578
1579 fn reuse_entry_id(&mut self, entry: &mut Entry) {
1580 if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
1581 entry.id = removed_entry_id;
1582 } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
1583 entry.id = existing_entry.id;
1584 }
1585 }
1586
1587 fn remove_path(&mut self, path: &Path) {
1588 let mut new_entries;
1589 let removed_entries;
1590 {
1591 let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
1592 new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
1593 removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
1594 new_entries.push_tree(cursor.suffix(&()), &());
1595 }
1596 self.entries_by_path = new_entries;
1597
1598 let mut entries_by_id_edits = Vec::new();
1599 for entry in removed_entries.cursor::<()>() {
1600 let removed_entry_id = self
1601 .removed_entry_ids
1602 .entry(entry.inode)
1603 .or_insert(entry.id);
1604 *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
1605 entries_by_id_edits.push(Edit::Remove(entry.id));
1606 self.dec_extension_count(&entry.path, entry.is_ignored);
1607 }
1608 self.entries_by_id.edit(entries_by_id_edits, &());
1609
1610 if path.file_name() == Some(&GITIGNORE) {
1611 let abs_parent_path = self.abs_path.join(path.parent().unwrap());
1612 if let Some((_, scan_id)) = self
1613 .ignores_by_parent_abs_path
1614 .get_mut(abs_parent_path.as_path())
1615 {
1616 *scan_id = self.snapshot.scan_id;
1617 }
1618 } else if path.file_name() == Some(&DOT_GIT) {
1619 let parent_path = path.parent().unwrap();
1620 if let Ok(ix) = self
1621 .git_repositories
1622 .binary_search_by_key(&parent_path, |repo| repo.content_path().as_ref())
1623 {
1624 self.git_repositories[ix].set_scan_id(self.snapshot.scan_id);
1625 }
1626 }
1627 }
1628
1629 fn ancestor_inodes_for_path(&self, path: &Path) -> TreeSet<u64> {
1630 let mut inodes = TreeSet::default();
1631 for ancestor in path.ancestors().skip(1) {
1632 if let Some(entry) = self.entry_for_path(ancestor) {
1633 inodes.insert(entry.inode);
1634 }
1635 }
1636 inodes
1637 }
1638
1639 fn ignore_stack_for_abs_path(&self, abs_path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
1640 let mut new_ignores = Vec::new();
1641 for ancestor in abs_path.ancestors().skip(1) {
1642 if let Some((ignore, _)) = self.ignores_by_parent_abs_path.get(ancestor) {
1643 new_ignores.push((ancestor, Some(ignore.clone())));
1644 } else {
1645 new_ignores.push((ancestor, None));
1646 }
1647 }
1648
1649 let mut ignore_stack = IgnoreStack::none();
1650 for (parent_abs_path, ignore) in new_ignores.into_iter().rev() {
1651 if ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
1652 ignore_stack = IgnoreStack::all();
1653 break;
1654 } else if let Some(ignore) = ignore {
1655 ignore_stack = ignore_stack.append(parent_abs_path.into(), ignore);
1656 }
1657 }
1658
1659 if ignore_stack.is_abs_path_ignored(abs_path, is_dir) {
1660 ignore_stack = IgnoreStack::all();
1661 }
1662
1663 ignore_stack
1664 }
1665}
1666
1667async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
1668 let contents = fs.load(abs_path).await?;
1669 let parent = abs_path.parent().unwrap_or_else(|| Path::new("/"));
1670 let mut builder = GitignoreBuilder::new(parent);
1671 for line in contents.lines() {
1672 builder.add_line(Some(abs_path.into()), line)?;
1673 }
1674 Ok(builder.build()?)
1675}
1676
1677impl WorktreeId {
1678 pub fn from_usize(handle_id: usize) -> Self {
1679 Self(handle_id)
1680 }
1681
1682 pub(crate) fn from_proto(id: u64) -> Self {
1683 Self(id as usize)
1684 }
1685
1686 pub fn to_proto(&self) -> u64 {
1687 self.0 as u64
1688 }
1689
1690 pub fn to_usize(&self) -> usize {
1691 self.0
1692 }
1693}
1694
1695impl fmt::Display for WorktreeId {
1696 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1697 self.0.fmt(f)
1698 }
1699}
1700
1701impl Deref for Worktree {
1702 type Target = Snapshot;
1703
1704 fn deref(&self) -> &Self::Target {
1705 match self {
1706 Worktree::Local(worktree) => &worktree.snapshot,
1707 Worktree::Remote(worktree) => &worktree.snapshot,
1708 }
1709 }
1710}
1711
1712impl Deref for LocalWorktree {
1713 type Target = LocalSnapshot;
1714
1715 fn deref(&self) -> &Self::Target {
1716 &self.snapshot
1717 }
1718}
1719
1720impl Deref for RemoteWorktree {
1721 type Target = Snapshot;
1722
1723 fn deref(&self) -> &Self::Target {
1724 &self.snapshot
1725 }
1726}
1727
1728impl fmt::Debug for LocalWorktree {
1729 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1730 self.snapshot.fmt(f)
1731 }
1732}
1733
1734impl fmt::Debug for Snapshot {
1735 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1736 struct EntriesById<'a>(&'a SumTree<PathEntry>);
1737 struct EntriesByPath<'a>(&'a SumTree<Entry>);
1738
1739 impl<'a> fmt::Debug for EntriesByPath<'a> {
1740 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1741 f.debug_map()
1742 .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
1743 .finish()
1744 }
1745 }
1746
1747 impl<'a> fmt::Debug for EntriesById<'a> {
1748 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1749 f.debug_list().entries(self.0.iter()).finish()
1750 }
1751 }
1752
1753 f.debug_struct("Snapshot")
1754 .field("id", &self.id)
1755 .field("root_name", &self.root_name)
1756 .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
1757 .field("entries_by_id", &EntriesById(&self.entries_by_id))
1758 .finish()
1759 }
1760}
1761
1762#[derive(Clone, PartialEq)]
1763pub struct File {
1764 pub worktree: ModelHandle<Worktree>,
1765 pub path: Arc<Path>,
1766 pub mtime: SystemTime,
1767 pub(crate) entry_id: Option<ProjectEntryId>,
1768 pub(crate) is_local: bool,
1769}
1770
1771impl language::File for File {
1772 fn as_local(&self) -> Option<&dyn language::LocalFile> {
1773 if self.is_local {
1774 Some(self)
1775 } else {
1776 None
1777 }
1778 }
1779
1780 fn mtime(&self) -> SystemTime {
1781 self.mtime
1782 }
1783
1784 fn path(&self) -> &Arc<Path> {
1785 &self.path
1786 }
1787
1788 fn full_path(&self, cx: &AppContext) -> PathBuf {
1789 let mut full_path = PathBuf::new();
1790 full_path.push(self.worktree.read(cx).root_name());
1791 if self.path.components().next().is_some() {
1792 full_path.push(&self.path);
1793 }
1794 full_path
1795 }
1796
1797 /// Returns the last component of this handle's absolute path. If this handle refers to the root
1798 /// of its worktree, then this method will return the name of the worktree itself.
1799 fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr {
1800 self.path
1801 .file_name()
1802 .unwrap_or_else(|| OsStr::new(&self.worktree.read(cx).root_name))
1803 }
1804
1805 fn is_deleted(&self) -> bool {
1806 self.entry_id.is_none()
1807 }
1808
1809 fn save(
1810 &self,
1811 buffer_id: u64,
1812 text: Rope,
1813 version: clock::Global,
1814 line_ending: LineEnding,
1815 cx: &mut MutableAppContext,
1816 ) -> Task<Result<(clock::Global, String, SystemTime)>> {
1817 self.worktree.update(cx, |worktree, cx| match worktree {
1818 Worktree::Local(worktree) => {
1819 let rpc = worktree.client.clone();
1820 let project_id = worktree.share.as_ref().map(|share| share.project_id);
1821 let fingerprint = text.fingerprint();
1822 let save = worktree.write_file(self.path.clone(), text, line_ending, cx);
1823 cx.background().spawn(async move {
1824 let entry = save.await?;
1825 if let Some(project_id) = project_id {
1826 rpc.send(proto::BufferSaved {
1827 project_id,
1828 buffer_id,
1829 version: serialize_version(&version),
1830 mtime: Some(entry.mtime.into()),
1831 fingerprint: fingerprint.clone(),
1832 })?;
1833 }
1834 Ok((version, fingerprint, entry.mtime))
1835 })
1836 }
1837 Worktree::Remote(worktree) => {
1838 let rpc = worktree.client.clone();
1839 let project_id = worktree.project_id;
1840 cx.foreground().spawn(async move {
1841 let response = rpc
1842 .request(proto::SaveBuffer {
1843 project_id,
1844 buffer_id,
1845 version: serialize_version(&version),
1846 })
1847 .await?;
1848 let version = deserialize_version(response.version);
1849 let mtime = response
1850 .mtime
1851 .ok_or_else(|| anyhow!("missing mtime"))?
1852 .into();
1853 Ok((version, response.fingerprint, mtime))
1854 })
1855 }
1856 })
1857 }
1858
1859 fn as_any(&self) -> &dyn Any {
1860 self
1861 }
1862
1863 fn to_proto(&self) -> rpc::proto::File {
1864 rpc::proto::File {
1865 worktree_id: self.worktree.id() as u64,
1866 entry_id: self.entry_id.map(|entry_id| entry_id.to_proto()),
1867 path: self.path.to_string_lossy().into(),
1868 mtime: Some(self.mtime.into()),
1869 }
1870 }
1871}
1872
1873impl language::LocalFile for File {
1874 fn abs_path(&self, cx: &AppContext) -> PathBuf {
1875 self.worktree
1876 .read(cx)
1877 .as_local()
1878 .unwrap()
1879 .abs_path
1880 .join(&self.path)
1881 }
1882
1883 fn load(&self, cx: &AppContext) -> Task<Result<String>> {
1884 let worktree = self.worktree.read(cx).as_local().unwrap();
1885 let abs_path = worktree.absolutize(&self.path);
1886 let fs = worktree.fs.clone();
1887 cx.background()
1888 .spawn(async move { fs.load(&abs_path).await })
1889 }
1890
1891 fn buffer_reloaded(
1892 &self,
1893 buffer_id: u64,
1894 version: &clock::Global,
1895 fingerprint: String,
1896 line_ending: LineEnding,
1897 mtime: SystemTime,
1898 cx: &mut MutableAppContext,
1899 ) {
1900 let worktree = self.worktree.read(cx).as_local().unwrap();
1901 if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) {
1902 worktree
1903 .client
1904 .send(proto::BufferReloaded {
1905 project_id,
1906 buffer_id,
1907 version: serialize_version(version),
1908 mtime: Some(mtime.into()),
1909 fingerprint,
1910 line_ending: serialize_line_ending(line_ending) as i32,
1911 })
1912 .log_err();
1913 }
1914 }
1915}
1916
1917impl File {
1918 pub fn from_proto(
1919 proto: rpc::proto::File,
1920 worktree: ModelHandle<Worktree>,
1921 cx: &AppContext,
1922 ) -> Result<Self> {
1923 let worktree_id = worktree
1924 .read(cx)
1925 .as_remote()
1926 .ok_or_else(|| anyhow!("not remote"))?
1927 .id();
1928
1929 if worktree_id.to_proto() != proto.worktree_id {
1930 return Err(anyhow!("worktree id does not match file"));
1931 }
1932
1933 Ok(Self {
1934 worktree,
1935 path: Path::new(&proto.path).into(),
1936 mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(),
1937 entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
1938 is_local: false,
1939 })
1940 }
1941
1942 pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
1943 file.and_then(|f| f.as_any().downcast_ref())
1944 }
1945
1946 pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
1947 self.worktree.read(cx).id()
1948 }
1949
1950 pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
1951 self.entry_id
1952 }
1953}
1954
1955#[derive(Clone, Debug, PartialEq, Eq)]
1956pub struct Entry {
1957 pub id: ProjectEntryId,
1958 pub kind: EntryKind,
1959 pub path: Arc<Path>,
1960 pub inode: u64,
1961 pub mtime: SystemTime,
1962 pub is_symlink: bool,
1963 pub is_ignored: bool,
1964}
1965
1966#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1967pub enum EntryKind {
1968 PendingDir,
1969 Dir,
1970 File(CharBag),
1971}
1972
1973impl Entry {
1974 fn new(
1975 path: Arc<Path>,
1976 metadata: &fs::Metadata,
1977 next_entry_id: &AtomicUsize,
1978 root_char_bag: CharBag,
1979 ) -> Self {
1980 Self {
1981 id: ProjectEntryId::new(next_entry_id),
1982 kind: if metadata.is_dir {
1983 EntryKind::PendingDir
1984 } else {
1985 EntryKind::File(char_bag_for_path(root_char_bag, &path))
1986 },
1987 path,
1988 inode: metadata.inode,
1989 mtime: metadata.mtime,
1990 is_symlink: metadata.is_symlink,
1991 is_ignored: false,
1992 }
1993 }
1994
1995 pub fn is_dir(&self) -> bool {
1996 matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
1997 }
1998
1999 pub fn is_file(&self) -> bool {
2000 matches!(self.kind, EntryKind::File(_))
2001 }
2002}
2003
2004impl sum_tree::Item for Entry {
2005 type Summary = EntrySummary;
2006
2007 fn summary(&self) -> Self::Summary {
2008 let visible_count = if self.is_ignored { 0 } else { 1 };
2009 let file_count;
2010 let visible_file_count;
2011 if self.is_file() {
2012 file_count = 1;
2013 visible_file_count = visible_count;
2014 } else {
2015 file_count = 0;
2016 visible_file_count = 0;
2017 }
2018
2019 EntrySummary {
2020 max_path: self.path.clone(),
2021 count: 1,
2022 visible_count,
2023 file_count,
2024 visible_file_count,
2025 }
2026 }
2027}
2028
2029impl sum_tree::KeyedItem for Entry {
2030 type Key = PathKey;
2031
2032 fn key(&self) -> Self::Key {
2033 PathKey(self.path.clone())
2034 }
2035}
2036
2037#[derive(Clone, Debug)]
2038pub struct EntrySummary {
2039 max_path: Arc<Path>,
2040 count: usize,
2041 visible_count: usize,
2042 file_count: usize,
2043 visible_file_count: usize,
2044}
2045
2046impl Default for EntrySummary {
2047 fn default() -> Self {
2048 Self {
2049 max_path: Arc::from(Path::new("")),
2050 count: 0,
2051 visible_count: 0,
2052 file_count: 0,
2053 visible_file_count: 0,
2054 }
2055 }
2056}
2057
2058impl sum_tree::Summary for EntrySummary {
2059 type Context = ();
2060
2061 fn add_summary(&mut self, rhs: &Self, _: &()) {
2062 self.max_path = rhs.max_path.clone();
2063 self.count += rhs.count;
2064 self.visible_count += rhs.visible_count;
2065 self.file_count += rhs.file_count;
2066 self.visible_file_count += rhs.visible_file_count;
2067 }
2068}
2069
2070#[derive(Clone, Debug)]
2071struct PathEntry {
2072 id: ProjectEntryId,
2073 path: Arc<Path>,
2074 is_ignored: bool,
2075 scan_id: usize,
2076}
2077
2078impl sum_tree::Item for PathEntry {
2079 type Summary = PathEntrySummary;
2080
2081 fn summary(&self) -> Self::Summary {
2082 PathEntrySummary { max_id: self.id }
2083 }
2084}
2085
2086impl sum_tree::KeyedItem for PathEntry {
2087 type Key = ProjectEntryId;
2088
2089 fn key(&self) -> Self::Key {
2090 self.id
2091 }
2092}
2093
2094#[derive(Clone, Debug, Default)]
2095struct PathEntrySummary {
2096 max_id: ProjectEntryId,
2097}
2098
2099impl sum_tree::Summary for PathEntrySummary {
2100 type Context = ();
2101
2102 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
2103 self.max_id = summary.max_id;
2104 }
2105}
2106
2107impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
2108 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
2109 *self = summary.max_id;
2110 }
2111}
2112
2113#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2114pub struct PathKey(Arc<Path>);
2115
2116impl Default for PathKey {
2117 fn default() -> Self {
2118 Self(Path::new("").into())
2119 }
2120}
2121
2122impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
2123 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2124 self.0 = summary.max_path.clone();
2125 }
2126}
2127
2128struct BackgroundScanner {
2129 fs: Arc<dyn Fs>,
2130 snapshot: Arc<Mutex<LocalSnapshot>>,
2131 notify: UnboundedSender<ScanState>,
2132 executor: Arc<executor::Background>,
2133}
2134
2135impl BackgroundScanner {
2136 fn new(
2137 snapshot: Arc<Mutex<LocalSnapshot>>,
2138 notify: UnboundedSender<ScanState>,
2139 fs: Arc<dyn Fs>,
2140 executor: Arc<executor::Background>,
2141 ) -> Self {
2142 Self {
2143 fs,
2144 snapshot,
2145 notify,
2146 executor,
2147 }
2148 }
2149
2150 fn abs_path(&self) -> Arc<Path> {
2151 self.snapshot.lock().abs_path.clone()
2152 }
2153
2154 fn snapshot(&self) -> LocalSnapshot {
2155 self.snapshot.lock().clone()
2156 }
2157
2158 async fn run(mut self, events_rx: impl Stream<Item = Vec<fsevent::Event>>) {
2159 if self.notify.unbounded_send(ScanState::Initializing).is_err() {
2160 return;
2161 }
2162
2163 if let Err(err) = self.scan_dirs().await {
2164 if self
2165 .notify
2166 .unbounded_send(ScanState::Err(Arc::new(err)))
2167 .is_err()
2168 {
2169 return;
2170 }
2171 }
2172
2173 if self.notify.unbounded_send(ScanState::Idle).is_err() {
2174 return;
2175 }
2176
2177 futures::pin_mut!(events_rx);
2178
2179 while let Some(mut events) = events_rx.next().await {
2180 while let Poll::Ready(Some(additional_events)) = futures::poll!(events_rx.next()) {
2181 events.extend(additional_events);
2182 }
2183
2184 if self.notify.unbounded_send(ScanState::Updating).is_err() {
2185 break;
2186 }
2187
2188 if !self.process_events(events).await {
2189 break;
2190 }
2191
2192 if self.notify.unbounded_send(ScanState::Idle).is_err() {
2193 break;
2194 }
2195 }
2196 }
2197
2198 async fn scan_dirs(&mut self) -> Result<()> {
2199 let root_char_bag;
2200 let root_abs_path;
2201 let root_inode;
2202 let is_dir;
2203 let next_entry_id;
2204 {
2205 let snapshot = self.snapshot.lock();
2206 root_char_bag = snapshot.root_char_bag;
2207 root_abs_path = snapshot.abs_path.clone();
2208 root_inode = snapshot.root_entry().map(|e| e.inode);
2209 is_dir = snapshot.root_entry().map_or(false, |e| e.is_dir());
2210 next_entry_id = snapshot.next_entry_id.clone();
2211 };
2212
2213 // Populate ignores above the root.
2214 for ancestor in root_abs_path.ancestors().skip(1) {
2215 if let Ok(ignore) = build_gitignore(&ancestor.join(&*GITIGNORE), self.fs.as_ref()).await
2216 {
2217 self.snapshot
2218 .lock()
2219 .ignores_by_parent_abs_path
2220 .insert(ancestor.into(), (ignore.into(), 0));
2221 }
2222 }
2223
2224 let ignore_stack = {
2225 let mut snapshot = self.snapshot.lock();
2226 let ignore_stack = snapshot.ignore_stack_for_abs_path(&root_abs_path, true);
2227 if ignore_stack.is_all() {
2228 if let Some(mut root_entry) = snapshot.root_entry().cloned() {
2229 root_entry.is_ignored = true;
2230 dbg!("scan dirs entry");
2231 snapshot.insert_entry(root_entry, self.fs.as_ref());
2232 }
2233 }
2234 ignore_stack
2235 };
2236
2237 if is_dir {
2238 let path: Arc<Path> = Arc::from(Path::new(""));
2239 let mut ancestor_inodes = TreeSet::default();
2240 if let Some(root_inode) = root_inode {
2241 ancestor_inodes.insert(root_inode);
2242 }
2243
2244 let (tx, rx) = channel::unbounded();
2245 self.executor
2246 .block(tx.send(ScanJob {
2247 abs_path: root_abs_path.to_path_buf(),
2248 path,
2249 ignore_stack,
2250 ancestor_inodes,
2251 scan_queue: tx.clone(),
2252 }))
2253 .unwrap();
2254 drop(tx);
2255
2256 self.executor
2257 .scoped(|scope| {
2258 for _ in 0..self.executor.num_cpus() {
2259 scope.spawn(async {
2260 while let Ok(job) = rx.recv().await {
2261 if let Err(err) = self
2262 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2263 .await
2264 {
2265 log::error!("error scanning {:?}: {}", job.abs_path, err);
2266 }
2267 }
2268 });
2269 }
2270 })
2271 .await;
2272 }
2273
2274 Ok(())
2275 }
2276
2277 async fn scan_dir(
2278 &self,
2279 root_char_bag: CharBag,
2280 next_entry_id: Arc<AtomicUsize>,
2281 job: &ScanJob,
2282 ) -> Result<()> {
2283 let mut new_entries: Vec<Entry> = Vec::new();
2284 let mut new_jobs: Vec<ScanJob> = Vec::new();
2285 let mut ignore_stack = job.ignore_stack.clone();
2286 let mut new_ignore = None;
2287
2288 let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2289 while let Some(child_abs_path) = child_paths.next().await {
2290 let child_abs_path = match child_abs_path {
2291 Ok(child_abs_path) => child_abs_path,
2292 Err(error) => {
2293 log::error!("error processing entry {:?}", error);
2294 continue;
2295 }
2296 };
2297 let child_name = child_abs_path.file_name().unwrap();
2298 let child_path: Arc<Path> = job.path.join(child_name).into();
2299 let child_metadata = match self.fs.metadata(&child_abs_path).await {
2300 Ok(Some(metadata)) => metadata,
2301 Ok(None) => continue,
2302 Err(err) => {
2303 log::error!("error processing {:?}: {:?}", child_abs_path, err);
2304 continue;
2305 }
2306 };
2307
2308 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2309 if child_name == *GITIGNORE {
2310 match build_gitignore(&child_abs_path, self.fs.as_ref()).await {
2311 Ok(ignore) => {
2312 let ignore = Arc::new(ignore);
2313 ignore_stack =
2314 ignore_stack.append(job.abs_path.as_path().into(), ignore.clone());
2315 new_ignore = Some(ignore);
2316 }
2317 Err(error) => {
2318 log::error!(
2319 "error loading .gitignore file {:?} - {:?}",
2320 child_name,
2321 error
2322 );
2323 }
2324 }
2325
2326 // Update ignore status of any child entries we've already processed to reflect the
2327 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2328 // there should rarely be too numerous. Update the ignore stack associated with any
2329 // new jobs as well.
2330 let mut new_jobs = new_jobs.iter_mut();
2331 for entry in &mut new_entries {
2332 let entry_abs_path = self.abs_path().join(&entry.path);
2333 entry.is_ignored =
2334 ignore_stack.is_abs_path_ignored(&entry_abs_path, entry.is_dir());
2335 if entry.is_dir() {
2336 new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
2337 IgnoreStack::all()
2338 } else {
2339 ignore_stack.clone()
2340 };
2341 }
2342 }
2343 }
2344
2345 let mut child_entry = Entry::new(
2346 child_path.clone(),
2347 &child_metadata,
2348 &next_entry_id,
2349 root_char_bag,
2350 );
2351
2352 if child_entry.is_dir() {
2353 let is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true);
2354 child_entry.is_ignored = is_ignored;
2355
2356 if !job.ancestor_inodes.contains(&child_entry.inode) {
2357 let mut ancestor_inodes = job.ancestor_inodes.clone();
2358 ancestor_inodes.insert(child_entry.inode);
2359 new_jobs.push(ScanJob {
2360 abs_path: child_abs_path,
2361 path: child_path,
2362 ignore_stack: if is_ignored {
2363 IgnoreStack::all()
2364 } else {
2365 ignore_stack.clone()
2366 },
2367 ancestor_inodes,
2368 scan_queue: job.scan_queue.clone(),
2369 });
2370 }
2371 } else {
2372 child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false);
2373 }
2374
2375 new_entries.push(child_entry);
2376 }
2377
2378 self.snapshot
2379 .lock()
2380 .populate_dir(job.path.clone(), new_entries, new_ignore);
2381 for new_job in new_jobs {
2382 job.scan_queue.send(new_job).await.unwrap();
2383 }
2384
2385 Ok(())
2386 }
2387
2388 async fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
2389 events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2390 events.dedup_by(|a, b| a.path.starts_with(&b.path));
2391
2392 let root_char_bag;
2393 let root_abs_path;
2394 let next_entry_id;
2395 {
2396 let snapshot = self.snapshot.lock();
2397 root_char_bag = snapshot.root_char_bag;
2398 root_abs_path = snapshot.abs_path.clone();
2399 next_entry_id = snapshot.next_entry_id.clone();
2400 }
2401
2402 let root_canonical_path = if let Ok(path) = self.fs.canonicalize(&root_abs_path).await {
2403 path
2404 } else {
2405 return false;
2406 };
2407 let metadata = futures::future::join_all(
2408 events
2409 .iter()
2410 .map(|event| self.fs.metadata(&event.path))
2411 .collect::<Vec<_>>(),
2412 )
2413 .await;
2414
2415 // Hold the snapshot lock while clearing and re-inserting the root entries
2416 // for each event. This way, the snapshot is not observable to the foreground
2417 // thread while this operation is in-progress.
2418 let (scan_queue_tx, scan_queue_rx) = channel::unbounded();
2419 {
2420 let mut snapshot = self.snapshot.lock();
2421 snapshot.scan_id += 1;
2422 for event in &events {
2423 if let Ok(path) = event.path.strip_prefix(&root_canonical_path) {
2424 snapshot.remove_path(path);
2425 }
2426 }
2427
2428 for (event, metadata) in events.into_iter().zip(metadata.into_iter()) {
2429 let path: Arc<Path> = match event.path.strip_prefix(&root_canonical_path) {
2430 Ok(path) => Arc::from(path.to_path_buf()),
2431 Err(_) => {
2432 log::error!(
2433 "unexpected event {:?} for root path {:?}",
2434 event.path,
2435 root_canonical_path
2436 );
2437 continue;
2438 }
2439 };
2440 let abs_path = root_abs_path.join(&path);
2441
2442 match metadata {
2443 Ok(Some(metadata)) => {
2444 let ignore_stack =
2445 snapshot.ignore_stack_for_abs_path(&abs_path, metadata.is_dir);
2446 let mut fs_entry = Entry::new(
2447 path.clone(),
2448 &metadata,
2449 snapshot.next_entry_id.as_ref(),
2450 snapshot.root_char_bag,
2451 );
2452 fs_entry.is_ignored = ignore_stack.is_all();
2453 dbg!("process_events entry");
2454 snapshot.insert_entry(fs_entry, self.fs.as_ref());
2455
2456 let mut ancestor_inodes = snapshot.ancestor_inodes_for_path(&path);
2457 if metadata.is_dir && !ancestor_inodes.contains(&metadata.inode) {
2458 ancestor_inodes.insert(metadata.inode);
2459 self.executor
2460 .block(scan_queue_tx.send(ScanJob {
2461 abs_path,
2462 path,
2463 ignore_stack,
2464 ancestor_inodes,
2465 scan_queue: scan_queue_tx.clone(),
2466 }))
2467 .unwrap();
2468 }
2469 }
2470 Ok(None) => {}
2471 Err(err) => {
2472 // TODO - create a special 'error' entry in the entries tree to mark this
2473 log::error!("error reading file on event {:?}", err);
2474 }
2475 }
2476 }
2477 drop(scan_queue_tx);
2478 }
2479
2480 // Scan any directories that were created as part of this event batch.
2481 self.executor
2482 .scoped(|scope| {
2483 for _ in 0..self.executor.num_cpus() {
2484 scope.spawn(async {
2485 while let Ok(job) = scan_queue_rx.recv().await {
2486 if let Err(err) = self
2487 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2488 .await
2489 {
2490 log::error!("error scanning {:?}: {}", job.abs_path, err);
2491 }
2492 }
2493 });
2494 }
2495 })
2496 .await;
2497
2498 // Attempt to detect renames only over a single batch of file-system events.
2499 self.snapshot.lock().removed_entry_ids.clear();
2500
2501 self.update_ignore_statuses().await;
2502 self.update_git_repositories().await;
2503 true
2504 }
2505
2506 async fn update_ignore_statuses(&self) {
2507 let mut snapshot = self.snapshot();
2508
2509 let mut ignores_to_update = Vec::new();
2510 let mut ignores_to_delete = Vec::new();
2511 for (parent_abs_path, (_, scan_id)) in &snapshot.ignores_by_parent_abs_path {
2512 if let Ok(parent_path) = parent_abs_path.strip_prefix(&snapshot.abs_path) {
2513 if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
2514 ignores_to_update.push(parent_abs_path.clone());
2515 }
2516
2517 let ignore_path = parent_path.join(&*GITIGNORE);
2518 if snapshot.entry_for_path(ignore_path).is_none() {
2519 ignores_to_delete.push(parent_abs_path.clone());
2520 }
2521 }
2522 }
2523
2524 for parent_abs_path in ignores_to_delete {
2525 snapshot.ignores_by_parent_abs_path.remove(&parent_abs_path);
2526 self.snapshot
2527 .lock()
2528 .ignores_by_parent_abs_path
2529 .remove(&parent_abs_path);
2530 }
2531
2532 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2533 ignores_to_update.sort_unstable();
2534 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2535 while let Some(parent_abs_path) = ignores_to_update.next() {
2536 while ignores_to_update
2537 .peek()
2538 .map_or(false, |p| p.starts_with(&parent_abs_path))
2539 {
2540 ignores_to_update.next().unwrap();
2541 }
2542
2543 let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true);
2544 ignore_queue_tx
2545 .send(UpdateIgnoreStatusJob {
2546 abs_path: parent_abs_path,
2547 ignore_stack,
2548 ignore_queue: ignore_queue_tx.clone(),
2549 })
2550 .await
2551 .unwrap();
2552 }
2553 drop(ignore_queue_tx);
2554
2555 self.executor
2556 .scoped(|scope| {
2557 for _ in 0..self.executor.num_cpus() {
2558 scope.spawn(async {
2559 while let Ok(job) = ignore_queue_rx.recv().await {
2560 self.update_ignore_status(job, &snapshot).await;
2561 }
2562 });
2563 }
2564 })
2565 .await;
2566 }
2567
2568 async fn update_git_repositories(&self) {
2569 let mut snapshot = self.snapshot();
2570 let mut git_repositories = mem::take(&mut snapshot.git_repositories);
2571 git_repositories.retain(|git_repository| {
2572 let dot_git_path = git_repository.content_path().join(&*DOT_GIT);
2573 snapshot.entry_for_path(dot_git_path).is_some()
2574 });
2575 snapshot.git_repositories = git_repositories;
2576 }
2577
2578 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
2579 let mut ignore_stack = job.ignore_stack;
2580 if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) {
2581 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
2582 }
2583
2584 let mut entries_by_id_edits = Vec::new();
2585 let mut entries_by_path_edits = Vec::new();
2586 let path = job.abs_path.strip_prefix(&snapshot.abs_path).unwrap();
2587 for mut entry in snapshot.child_entries(path).cloned() {
2588 let was_ignored = entry.is_ignored;
2589 let abs_path = self.abs_path().join(&entry.path);
2590 entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir());
2591 if entry.is_dir() {
2592 let child_ignore_stack = if entry.is_ignored {
2593 IgnoreStack::all()
2594 } else {
2595 ignore_stack.clone()
2596 };
2597 job.ignore_queue
2598 .send(UpdateIgnoreStatusJob {
2599 abs_path: abs_path.into(),
2600 ignore_stack: child_ignore_stack,
2601 ignore_queue: job.ignore_queue.clone(),
2602 })
2603 .await
2604 .unwrap();
2605 }
2606
2607 if entry.is_ignored != was_ignored {
2608 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
2609 path_entry.scan_id = snapshot.scan_id;
2610 path_entry.is_ignored = entry.is_ignored;
2611 entries_by_id_edits.push(Edit::Insert(path_entry));
2612 entries_by_path_edits.push(Edit::Insert(entry));
2613 }
2614 }
2615
2616 let mut snapshot = self.snapshot.lock();
2617 snapshot.entries_by_path.edit(entries_by_path_edits, &());
2618 snapshot.entries_by_id.edit(entries_by_id_edits, &());
2619 }
2620}
2621
2622fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
2623 let mut result = root_char_bag;
2624 result.extend(
2625 path.to_string_lossy()
2626 .chars()
2627 .map(|c| c.to_ascii_lowercase()),
2628 );
2629 result
2630}
2631
2632struct ScanJob {
2633 abs_path: PathBuf,
2634 path: Arc<Path>,
2635 ignore_stack: Arc<IgnoreStack>,
2636 scan_queue: Sender<ScanJob>,
2637 ancestor_inodes: TreeSet<u64>,
2638}
2639
2640struct UpdateIgnoreStatusJob {
2641 abs_path: Arc<Path>,
2642 ignore_stack: Arc<IgnoreStack>,
2643 ignore_queue: Sender<UpdateIgnoreStatusJob>,
2644}
2645
2646pub trait WorktreeHandle {
2647 #[cfg(any(test, feature = "test-support"))]
2648 fn flush_fs_events<'a>(
2649 &self,
2650 cx: &'a gpui::TestAppContext,
2651 ) -> futures::future::LocalBoxFuture<'a, ()>;
2652}
2653
2654impl WorktreeHandle for ModelHandle<Worktree> {
2655 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
2656 // occurred before the worktree was constructed. These events can cause the worktree to perfrom
2657 // extra directory scans, and emit extra scan-state notifications.
2658 //
2659 // This function mutates the worktree's directory and waits for those mutations to be picked up,
2660 // to ensure that all redundant FS events have already been processed.
2661 #[cfg(any(test, feature = "test-support"))]
2662 fn flush_fs_events<'a>(
2663 &self,
2664 cx: &'a gpui::TestAppContext,
2665 ) -> futures::future::LocalBoxFuture<'a, ()> {
2666 use smol::future::FutureExt;
2667
2668 let filename = "fs-event-sentinel";
2669 let tree = self.clone();
2670 let (fs, root_path) = self.read_with(cx, |tree, _| {
2671 let tree = tree.as_local().unwrap();
2672 (tree.fs.clone(), tree.abs_path().clone())
2673 });
2674
2675 async move {
2676 fs.create_file(&root_path.join(filename), Default::default())
2677 .await
2678 .unwrap();
2679 tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_some())
2680 .await;
2681
2682 fs.remove_file(&root_path.join(filename), Default::default())
2683 .await
2684 .unwrap();
2685 tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_none())
2686 .await;
2687
2688 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2689 .await;
2690 }
2691 .boxed_local()
2692 }
2693}
2694
2695#[derive(Clone, Debug)]
2696struct TraversalProgress<'a> {
2697 max_path: &'a Path,
2698 count: usize,
2699 visible_count: usize,
2700 file_count: usize,
2701 visible_file_count: usize,
2702}
2703
2704impl<'a> TraversalProgress<'a> {
2705 fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
2706 match (include_ignored, include_dirs) {
2707 (true, true) => self.count,
2708 (true, false) => self.file_count,
2709 (false, true) => self.visible_count,
2710 (false, false) => self.visible_file_count,
2711 }
2712 }
2713}
2714
2715impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
2716 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2717 self.max_path = summary.max_path.as_ref();
2718 self.count += summary.count;
2719 self.visible_count += summary.visible_count;
2720 self.file_count += summary.file_count;
2721 self.visible_file_count += summary.visible_file_count;
2722 }
2723}
2724
2725impl<'a> Default for TraversalProgress<'a> {
2726 fn default() -> Self {
2727 Self {
2728 max_path: Path::new(""),
2729 count: 0,
2730 visible_count: 0,
2731 file_count: 0,
2732 visible_file_count: 0,
2733 }
2734 }
2735}
2736
2737pub struct Traversal<'a> {
2738 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
2739 include_ignored: bool,
2740 include_dirs: bool,
2741}
2742
2743impl<'a> Traversal<'a> {
2744 pub fn advance(&mut self) -> bool {
2745 self.advance_to_offset(self.offset() + 1)
2746 }
2747
2748 pub fn advance_to_offset(&mut self, offset: usize) -> bool {
2749 self.cursor.seek_forward(
2750 &TraversalTarget::Count {
2751 count: offset,
2752 include_dirs: self.include_dirs,
2753 include_ignored: self.include_ignored,
2754 },
2755 Bias::Right,
2756 &(),
2757 )
2758 }
2759
2760 pub fn advance_to_sibling(&mut self) -> bool {
2761 while let Some(entry) = self.cursor.item() {
2762 self.cursor.seek_forward(
2763 &TraversalTarget::PathSuccessor(&entry.path),
2764 Bias::Left,
2765 &(),
2766 );
2767 if let Some(entry) = self.cursor.item() {
2768 if (self.include_dirs || !entry.is_dir())
2769 && (self.include_ignored || !entry.is_ignored)
2770 {
2771 return true;
2772 }
2773 }
2774 }
2775 false
2776 }
2777
2778 pub fn entry(&self) -> Option<&'a Entry> {
2779 self.cursor.item()
2780 }
2781
2782 pub fn offset(&self) -> usize {
2783 self.cursor
2784 .start()
2785 .count(self.include_dirs, self.include_ignored)
2786 }
2787}
2788
2789impl<'a> Iterator for Traversal<'a> {
2790 type Item = &'a Entry;
2791
2792 fn next(&mut self) -> Option<Self::Item> {
2793 if let Some(item) = self.entry() {
2794 self.advance();
2795 Some(item)
2796 } else {
2797 None
2798 }
2799 }
2800}
2801
2802#[derive(Debug)]
2803enum TraversalTarget<'a> {
2804 Path(&'a Path),
2805 PathSuccessor(&'a Path),
2806 Count {
2807 count: usize,
2808 include_ignored: bool,
2809 include_dirs: bool,
2810 },
2811}
2812
2813impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
2814 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
2815 match self {
2816 TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
2817 TraversalTarget::PathSuccessor(path) => {
2818 if !cursor_location.max_path.starts_with(path) {
2819 Ordering::Equal
2820 } else {
2821 Ordering::Greater
2822 }
2823 }
2824 TraversalTarget::Count {
2825 count,
2826 include_dirs,
2827 include_ignored,
2828 } => Ord::cmp(
2829 count,
2830 &cursor_location.count(*include_dirs, *include_ignored),
2831 ),
2832 }
2833 }
2834}
2835
2836struct ChildEntriesIter<'a> {
2837 parent_path: &'a Path,
2838 traversal: Traversal<'a>,
2839}
2840
2841impl<'a> Iterator for ChildEntriesIter<'a> {
2842 type Item = &'a Entry;
2843
2844 fn next(&mut self) -> Option<Self::Item> {
2845 if let Some(item) = self.traversal.entry() {
2846 if item.path.starts_with(&self.parent_path) {
2847 self.traversal.advance_to_sibling();
2848 return Some(item);
2849 }
2850 }
2851 None
2852 }
2853}
2854
2855impl<'a> From<&'a Entry> for proto::Entry {
2856 fn from(entry: &'a Entry) -> Self {
2857 Self {
2858 id: entry.id.to_proto(),
2859 is_dir: entry.is_dir(),
2860 path: entry.path.as_os_str().as_bytes().to_vec(),
2861 inode: entry.inode,
2862 mtime: Some(entry.mtime.into()),
2863 is_symlink: entry.is_symlink,
2864 is_ignored: entry.is_ignored,
2865 }
2866 }
2867}
2868
2869impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
2870 type Error = anyhow::Error;
2871
2872 fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
2873 if let Some(mtime) = entry.mtime {
2874 let kind = if entry.is_dir {
2875 EntryKind::Dir
2876 } else {
2877 let mut char_bag = *root_char_bag;
2878 char_bag.extend(
2879 String::from_utf8_lossy(&entry.path)
2880 .chars()
2881 .map(|c| c.to_ascii_lowercase()),
2882 );
2883 EntryKind::File(char_bag)
2884 };
2885 let path: Arc<Path> = PathBuf::from(OsString::from_vec(entry.path)).into();
2886 Ok(Entry {
2887 id: ProjectEntryId::from_proto(entry.id),
2888 kind,
2889 path,
2890 inode: entry.inode,
2891 mtime: mtime.into(),
2892 is_symlink: entry.is_symlink,
2893 is_ignored: entry.is_ignored,
2894 })
2895 } else {
2896 Err(anyhow!(
2897 "missing mtime in remote worktree entry {:?}",
2898 entry.path
2899 ))
2900 }
2901 }
2902}
2903
2904async fn send_worktree_update(client: &Arc<Client>, update: proto::UpdateWorktree) -> Result<()> {
2905 #[cfg(any(test, feature = "test-support"))]
2906 const MAX_CHUNK_SIZE: usize = 2;
2907 #[cfg(not(any(test, feature = "test-support")))]
2908 const MAX_CHUNK_SIZE: usize = 256;
2909
2910 for update in proto::split_worktree_update(update, MAX_CHUNK_SIZE) {
2911 client.request(update).await?;
2912 }
2913
2914 Ok(())
2915}
2916
2917#[cfg(test)]
2918mod tests {
2919 use super::*;
2920 use crate::fs::FakeFs;
2921 use anyhow::Result;
2922 use client::test::FakeHttpClient;
2923 use fs::RealFs;
2924 use gpui::{executor::Deterministic, TestAppContext};
2925 use rand::prelude::*;
2926 use serde_json::json;
2927 use std::{
2928 env,
2929 fmt::Write,
2930 time::{SystemTime, UNIX_EPOCH},
2931 };
2932
2933 use util::test::temp_tree;
2934
2935 #[gpui::test]
2936 async fn test_traversal(cx: &mut TestAppContext) {
2937 let fs = FakeFs::new(cx.background());
2938 fs.insert_tree(
2939 "/root",
2940 json!({
2941 ".gitignore": "a/b\n",
2942 "a": {
2943 "b": "",
2944 "c": "",
2945 }
2946 }),
2947 )
2948 .await;
2949
2950 let http_client = FakeHttpClient::with_404_response();
2951 let client = cx.read(|cx| Client::new(http_client, cx));
2952
2953 let tree = Worktree::local(
2954 client,
2955 Arc::from(Path::new("/root")),
2956 true,
2957 fs,
2958 Default::default(),
2959 &mut cx.to_async(),
2960 )
2961 .await
2962 .unwrap();
2963 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2964 .await;
2965
2966 tree.read_with(cx, |tree, _| {
2967 assert_eq!(
2968 tree.entries(false)
2969 .map(|entry| entry.path.as_ref())
2970 .collect::<Vec<_>>(),
2971 vec![
2972 Path::new(""),
2973 Path::new(".gitignore"),
2974 Path::new("a"),
2975 Path::new("a/c"),
2976 ]
2977 );
2978 assert_eq!(
2979 tree.entries(true)
2980 .map(|entry| entry.path.as_ref())
2981 .collect::<Vec<_>>(),
2982 vec![
2983 Path::new(""),
2984 Path::new(".gitignore"),
2985 Path::new("a"),
2986 Path::new("a/b"),
2987 Path::new("a/c"),
2988 ]
2989 );
2990 })
2991 }
2992
2993 #[gpui::test(iterations = 10)]
2994 async fn test_circular_symlinks(executor: Arc<Deterministic>, cx: &mut TestAppContext) {
2995 let fs = FakeFs::new(cx.background());
2996 fs.insert_tree(
2997 "/root",
2998 json!({
2999 "lib": {
3000 "a": {
3001 "a.txt": ""
3002 },
3003 "b": {
3004 "b.txt": ""
3005 }
3006 }
3007 }),
3008 )
3009 .await;
3010 fs.insert_symlink("/root/lib/a/lib", "..".into()).await;
3011 fs.insert_symlink("/root/lib/b/lib", "..".into()).await;
3012
3013 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3014 let tree = Worktree::local(
3015 client,
3016 Arc::from(Path::new("/root")),
3017 true,
3018 fs.clone(),
3019 Default::default(),
3020 &mut cx.to_async(),
3021 )
3022 .await
3023 .unwrap();
3024
3025 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3026 .await;
3027
3028 tree.read_with(cx, |tree, _| {
3029 assert_eq!(
3030 tree.entries(false)
3031 .map(|entry| entry.path.as_ref())
3032 .collect::<Vec<_>>(),
3033 vec![
3034 Path::new(""),
3035 Path::new("lib"),
3036 Path::new("lib/a"),
3037 Path::new("lib/a/a.txt"),
3038 Path::new("lib/a/lib"),
3039 Path::new("lib/b"),
3040 Path::new("lib/b/b.txt"),
3041 Path::new("lib/b/lib"),
3042 ]
3043 );
3044 });
3045
3046 fs.rename(
3047 Path::new("/root/lib/a/lib"),
3048 Path::new("/root/lib/a/lib-2"),
3049 Default::default(),
3050 )
3051 .await
3052 .unwrap();
3053 executor.run_until_parked();
3054 tree.read_with(cx, |tree, _| {
3055 assert_eq!(
3056 tree.entries(false)
3057 .map(|entry| entry.path.as_ref())
3058 .collect::<Vec<_>>(),
3059 vec![
3060 Path::new(""),
3061 Path::new("lib"),
3062 Path::new("lib/a"),
3063 Path::new("lib/a/a.txt"),
3064 Path::new("lib/a/lib-2"),
3065 Path::new("lib/b"),
3066 Path::new("lib/b/b.txt"),
3067 Path::new("lib/b/lib"),
3068 ]
3069 );
3070 });
3071 }
3072
3073 #[gpui::test]
3074 async fn test_rescan_with_gitignore(cx: &mut TestAppContext) {
3075 let parent_dir = temp_tree(json!({
3076 ".gitignore": "ancestor-ignored-file1\nancestor-ignored-file2\n",
3077 "tree": {
3078 ".git": {},
3079 ".gitignore": "ignored-dir\n",
3080 "tracked-dir": {
3081 "tracked-file1": "",
3082 "ancestor-ignored-file1": "",
3083 },
3084 "ignored-dir": {
3085 "ignored-file1": ""
3086 }
3087 }
3088 }));
3089 let dir = parent_dir.path().join("tree");
3090
3091 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3092
3093 let tree = Worktree::local(
3094 client,
3095 dir.as_path(),
3096 true,
3097 Arc::new(RealFs),
3098 Default::default(),
3099 &mut cx.to_async(),
3100 )
3101 .await
3102 .unwrap();
3103 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3104 .await;
3105 tree.flush_fs_events(cx).await;
3106 cx.read(|cx| {
3107 let tree = tree.read(cx);
3108 assert!(
3109 !tree
3110 .entry_for_path("tracked-dir/tracked-file1")
3111 .unwrap()
3112 .is_ignored
3113 );
3114 assert!(
3115 tree.entry_for_path("tracked-dir/ancestor-ignored-file1")
3116 .unwrap()
3117 .is_ignored
3118 );
3119 assert!(
3120 tree.entry_for_path("ignored-dir/ignored-file1")
3121 .unwrap()
3122 .is_ignored
3123 );
3124 });
3125
3126 std::fs::write(dir.join("tracked-dir/tracked-file2"), "").unwrap();
3127 std::fs::write(dir.join("tracked-dir/ancestor-ignored-file2"), "").unwrap();
3128 std::fs::write(dir.join("ignored-dir/ignored-file2"), "").unwrap();
3129 tree.flush_fs_events(cx).await;
3130 cx.read(|cx| {
3131 let tree = tree.read(cx);
3132 assert!(
3133 !tree
3134 .entry_for_path("tracked-dir/tracked-file2")
3135 .unwrap()
3136 .is_ignored
3137 );
3138 assert!(
3139 tree.entry_for_path("tracked-dir/ancestor-ignored-file2")
3140 .unwrap()
3141 .is_ignored
3142 );
3143 assert!(
3144 tree.entry_for_path("ignored-dir/ignored-file2")
3145 .unwrap()
3146 .is_ignored
3147 );
3148 assert!(tree.entry_for_path(".git").unwrap().is_ignored);
3149 });
3150 }
3151
3152 #[gpui::test]
3153 async fn test_git_repository_for_path(cx: &mut TestAppContext) {
3154 let root = temp_tree(json!({
3155 "dir1": {
3156 ".git": {},
3157 "deps": {
3158 "dep1": {
3159 ".git": {},
3160 "src": {
3161 "a.txt": ""
3162 }
3163 }
3164 },
3165 "src": {
3166 "b.txt": ""
3167 }
3168 },
3169 "c.txt": ""
3170 }));
3171
3172 let http_client = FakeHttpClient::with_404_response();
3173 let client = Client::new(http_client);
3174 let tree = Worktree::local(
3175 client,
3176 root.path(),
3177 true,
3178 Arc::new(RealFs),
3179 Default::default(),
3180 &mut cx.to_async(),
3181 )
3182 .await
3183 .unwrap();
3184
3185 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3186 .await;
3187 tree.flush_fs_events(cx).await;
3188
3189 tree.read_with(cx, |tree, _cx| {
3190 let tree = tree.as_local().unwrap();
3191
3192 dbg!(tree);
3193
3194 assert!(tree
3195 .git_repository_for_file_path("c.txt".as_ref())
3196 .is_none());
3197
3198 let repo = tree
3199 .git_repository_for_file_path("dir1/src/b.txt".as_ref())
3200 .unwrap();
3201
3202 // Need to update the file system for anything involving git
3203 // Goal: Make this test pass
3204 // Up Next: Invalidating git repos!
3205 assert_eq!(repo.content_path(), Path::new("dir1"));
3206 assert_eq!(repo.git_dir_path(), Path::new("dir1/.git"));
3207
3208 let repo = tree
3209 .git_repository_for_file_path("dir1/deps/dep1/src/a.txt".as_ref())
3210 .unwrap();
3211
3212 assert_eq!(repo.content_path(), Path::new("dir1/deps/dep1"));
3213 assert_eq!(repo.git_dir_path(), Path::new("dir1/deps/dep1"));
3214
3215 let repo = tree
3216 .git_repository_for_git_data("dir1/.git/HEAD".as_ref())
3217 .unwrap();
3218
3219 assert_eq!(repo.content_path(), Path::new("dir1"));
3220 assert_eq!(repo.git_dir_path(), Path::new("dir1/.git"));
3221
3222 assert!(tree.does_git_repository_track_file_path(&repo, "dir1/src/b.txt".as_ref()));
3223 assert!(!tree
3224 .does_git_repository_track_file_path(&repo, "dir1/deps/dep1/src/a.txt".as_ref()));
3225 });
3226 }
3227
3228 #[gpui::test]
3229 async fn test_write_file(cx: &mut TestAppContext) {
3230 let dir = temp_tree(json!({
3231 ".git": {},
3232 ".gitignore": "ignored-dir\n",
3233 "tracked-dir": {},
3234 "ignored-dir": {}
3235 }));
3236
3237 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3238
3239 let tree = Worktree::local(
3240 client,
3241 dir.path(),
3242 true,
3243 Arc::new(RealFs),
3244 Default::default(),
3245 &mut cx.to_async(),
3246 )
3247 .await
3248 .unwrap();
3249 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3250 .await;
3251 tree.flush_fs_events(cx).await;
3252
3253 tree.update(cx, |tree, cx| {
3254 tree.as_local().unwrap().write_file(
3255 Path::new("tracked-dir/file.txt"),
3256 "hello".into(),
3257 Default::default(),
3258 cx,
3259 )
3260 })
3261 .await
3262 .unwrap();
3263 tree.update(cx, |tree, cx| {
3264 tree.as_local().unwrap().write_file(
3265 Path::new("ignored-dir/file.txt"),
3266 "world".into(),
3267 Default::default(),
3268 cx,
3269 )
3270 })
3271 .await
3272 .unwrap();
3273
3274 tree.read_with(cx, |tree, _| {
3275 let tracked = tree.entry_for_path("tracked-dir/file.txt").unwrap();
3276 let ignored = tree.entry_for_path("ignored-dir/file.txt").unwrap();
3277 assert!(!tracked.is_ignored);
3278 assert!(ignored.is_ignored);
3279 });
3280 }
3281
3282 #[gpui::test(iterations = 30)]
3283 async fn test_create_directory(cx: &mut TestAppContext) {
3284 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3285
3286 let fs = FakeFs::new(cx.background());
3287 fs.insert_tree(
3288 "/a",
3289 json!({
3290 "b": {},
3291 "c": {},
3292 "d": {},
3293 }),
3294 )
3295 .await;
3296
3297 let tree = Worktree::local(
3298 client,
3299 "/a".as_ref(),
3300 true,
3301 fs,
3302 Default::default(),
3303 &mut cx.to_async(),
3304 )
3305 .await
3306 .unwrap();
3307
3308 let entry = tree
3309 .update(cx, |tree, cx| {
3310 tree.as_local_mut()
3311 .unwrap()
3312 .create_entry("a/e".as_ref(), true, cx)
3313 })
3314 .await
3315 .unwrap();
3316 assert!(entry.is_dir());
3317
3318 cx.foreground().run_until_parked();
3319 tree.read_with(cx, |tree, _| {
3320 assert_eq!(tree.entry_for_path("a/e").unwrap().kind, EntryKind::Dir);
3321 });
3322 }
3323
3324 #[gpui::test(iterations = 100)]
3325 fn test_random(mut rng: StdRng) {
3326 let operations = env::var("OPERATIONS")
3327 .map(|o| o.parse().unwrap())
3328 .unwrap_or(40);
3329 let initial_entries = env::var("INITIAL_ENTRIES")
3330 .map(|o| o.parse().unwrap())
3331 .unwrap_or(20);
3332
3333 let root_dir = tempdir::TempDir::new("worktree-test").unwrap();
3334 for _ in 0..initial_entries {
3335 randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
3336 }
3337 log::info!("Generated initial tree");
3338
3339 let (notify_tx, _notify_rx) = mpsc::unbounded();
3340 let fs = Arc::new(RealFs);
3341 let next_entry_id = Arc::new(AtomicUsize::new(0));
3342 let mut initial_snapshot = LocalSnapshot {
3343 abs_path: root_dir.path().into(),
3344 removed_entry_ids: Default::default(),
3345 ignores_by_parent_abs_path: Default::default(),
3346 git_repositories: Default::default(),
3347 next_entry_id: next_entry_id.clone(),
3348 snapshot: Snapshot {
3349 id: WorktreeId::from_usize(0),
3350 entries_by_path: Default::default(),
3351 entries_by_id: Default::default(),
3352 root_name: Default::default(),
3353 root_char_bag: Default::default(),
3354 scan_id: 0,
3355 is_complete: true,
3356 },
3357 extension_counts: Default::default(),
3358 };
3359 initial_snapshot.insert_entry(
3360 Entry::new(
3361 Path::new("").into(),
3362 &smol::block_on(fs.metadata(root_dir.path()))
3363 .unwrap()
3364 .unwrap(),
3365 &next_entry_id,
3366 Default::default(),
3367 ),
3368 fs.as_ref(),
3369 );
3370 let mut scanner = BackgroundScanner::new(
3371 Arc::new(Mutex::new(initial_snapshot.clone())),
3372 notify_tx,
3373 fs.clone(),
3374 Arc::new(gpui::executor::Background::new()),
3375 );
3376 smol::block_on(scanner.scan_dirs()).unwrap();
3377 scanner.snapshot().check_invariants();
3378
3379 let mut events = Vec::new();
3380 let mut snapshots = Vec::new();
3381 let mut mutations_len = operations;
3382 while mutations_len > 1 {
3383 if !events.is_empty() && rng.gen_bool(0.4) {
3384 let len = rng.gen_range(0..=events.len());
3385 let to_deliver = events.drain(0..len).collect::<Vec<_>>();
3386 log::info!("Delivering events: {:#?}", to_deliver);
3387 smol::block_on(scanner.process_events(to_deliver));
3388 scanner.snapshot().check_invariants();
3389 } else {
3390 events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
3391 mutations_len -= 1;
3392 }
3393
3394 if rng.gen_bool(0.2) {
3395 snapshots.push(scanner.snapshot());
3396 }
3397 }
3398 log::info!("Quiescing: {:#?}", events);
3399 smol::block_on(scanner.process_events(events));
3400 scanner.snapshot().check_invariants();
3401
3402 let (notify_tx, _notify_rx) = mpsc::unbounded();
3403 let mut new_scanner = BackgroundScanner::new(
3404 Arc::new(Mutex::new(initial_snapshot)),
3405 notify_tx,
3406 scanner.fs.clone(),
3407 scanner.executor.clone(),
3408 );
3409 smol::block_on(new_scanner.scan_dirs()).unwrap();
3410 assert_eq!(
3411 scanner.snapshot().to_vec(true),
3412 new_scanner.snapshot().to_vec(true)
3413 );
3414
3415 for mut prev_snapshot in snapshots {
3416 let include_ignored = rng.gen::<bool>();
3417 if !include_ignored {
3418 let mut entries_by_path_edits = Vec::new();
3419 let mut entries_by_id_edits = Vec::new();
3420 for entry in prev_snapshot
3421 .entries_by_id
3422 .cursor::<()>()
3423 .filter(|e| e.is_ignored)
3424 {
3425 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
3426 entries_by_id_edits.push(Edit::Remove(entry.id));
3427 }
3428
3429 prev_snapshot
3430 .entries_by_path
3431 .edit(entries_by_path_edits, &());
3432 prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
3433 }
3434
3435 let update = scanner
3436 .snapshot()
3437 .build_update(&prev_snapshot, 0, 0, include_ignored);
3438 prev_snapshot.apply_remote_update(update).unwrap();
3439 assert_eq!(
3440 prev_snapshot.to_vec(true),
3441 scanner.snapshot().to_vec(include_ignored)
3442 );
3443 }
3444 }
3445
3446 fn randomly_mutate_tree(
3447 root_path: &Path,
3448 insertion_probability: f64,
3449 rng: &mut impl Rng,
3450 ) -> Result<Vec<fsevent::Event>> {
3451 let root_path = root_path.canonicalize().unwrap();
3452 let (dirs, files) = read_dir_recursive(root_path.clone());
3453
3454 let mut events = Vec::new();
3455 let mut record_event = |path: PathBuf| {
3456 events.push(fsevent::Event {
3457 event_id: SystemTime::now()
3458 .duration_since(UNIX_EPOCH)
3459 .unwrap()
3460 .as_secs(),
3461 flags: fsevent::StreamFlags::empty(),
3462 path,
3463 });
3464 };
3465
3466 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
3467 let path = dirs.choose(rng).unwrap();
3468 let new_path = path.join(gen_name(rng));
3469
3470 if rng.gen() {
3471 log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
3472 std::fs::create_dir(&new_path)?;
3473 } else {
3474 log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
3475 std::fs::write(&new_path, "")?;
3476 }
3477 record_event(new_path);
3478 } else if rng.gen_bool(0.05) {
3479 let ignore_dir_path = dirs.choose(rng).unwrap();
3480 let ignore_path = ignore_dir_path.join(&*GITIGNORE);
3481
3482 let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
3483 let files_to_ignore = {
3484 let len = rng.gen_range(0..=subfiles.len());
3485 subfiles.choose_multiple(rng, len)
3486 };
3487 let dirs_to_ignore = {
3488 let len = rng.gen_range(0..subdirs.len());
3489 subdirs.choose_multiple(rng, len)
3490 };
3491
3492 let mut ignore_contents = String::new();
3493 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
3494 writeln!(
3495 ignore_contents,
3496 "{}",
3497 path_to_ignore
3498 .strip_prefix(&ignore_dir_path)?
3499 .to_str()
3500 .unwrap()
3501 )
3502 .unwrap();
3503 }
3504 log::info!(
3505 "Creating {:?} with contents:\n{}",
3506 ignore_path.strip_prefix(&root_path)?,
3507 ignore_contents
3508 );
3509 std::fs::write(&ignore_path, ignore_contents).unwrap();
3510 record_event(ignore_path);
3511 } else {
3512 let old_path = {
3513 let file_path = files.choose(rng);
3514 let dir_path = dirs[1..].choose(rng);
3515 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
3516 };
3517
3518 let is_rename = rng.gen();
3519 if is_rename {
3520 let new_path_parent = dirs
3521 .iter()
3522 .filter(|d| !d.starts_with(old_path))
3523 .choose(rng)
3524 .unwrap();
3525
3526 let overwrite_existing_dir =
3527 !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
3528 let new_path = if overwrite_existing_dir {
3529 std::fs::remove_dir_all(&new_path_parent).ok();
3530 new_path_parent.to_path_buf()
3531 } else {
3532 new_path_parent.join(gen_name(rng))
3533 };
3534
3535 log::info!(
3536 "Renaming {:?} to {}{:?}",
3537 old_path.strip_prefix(&root_path)?,
3538 if overwrite_existing_dir {
3539 "overwrite "
3540 } else {
3541 ""
3542 },
3543 new_path.strip_prefix(&root_path)?
3544 );
3545 std::fs::rename(&old_path, &new_path)?;
3546 record_event(old_path.clone());
3547 record_event(new_path);
3548 } else if old_path.is_dir() {
3549 let (dirs, files) = read_dir_recursive(old_path.clone());
3550
3551 log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
3552 std::fs::remove_dir_all(&old_path).unwrap();
3553 for file in files {
3554 record_event(file);
3555 }
3556 for dir in dirs {
3557 record_event(dir);
3558 }
3559 } else {
3560 log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
3561 std::fs::remove_file(old_path).unwrap();
3562 record_event(old_path.clone());
3563 }
3564 }
3565
3566 Ok(events)
3567 }
3568
3569 fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
3570 let child_entries = std::fs::read_dir(&path).unwrap();
3571 let mut dirs = vec![path];
3572 let mut files = Vec::new();
3573 for child_entry in child_entries {
3574 let child_path = child_entry.unwrap().path();
3575 if child_path.is_dir() {
3576 let (child_dirs, child_files) = read_dir_recursive(child_path);
3577 dirs.extend(child_dirs);
3578 files.extend(child_files);
3579 } else {
3580 files.push(child_path);
3581 }
3582 }
3583 (dirs, files)
3584 }
3585
3586 fn gen_name(rng: &mut impl Rng) -> String {
3587 (0..6)
3588 .map(|_| rng.sample(rand::distributions::Alphanumeric))
3589 .map(char::from)
3590 .collect()
3591 }
3592
3593 impl LocalSnapshot {
3594 fn check_invariants(&self) {
3595 let mut files = self.files(true, 0);
3596 let mut visible_files = self.files(false, 0);
3597 for entry in self.entries_by_path.cursor::<()>() {
3598 if entry.is_file() {
3599 assert_eq!(files.next().unwrap().inode, entry.inode);
3600 if !entry.is_ignored {
3601 assert_eq!(visible_files.next().unwrap().inode, entry.inode);
3602 }
3603 }
3604 }
3605 assert!(files.next().is_none());
3606 assert!(visible_files.next().is_none());
3607
3608 let mut bfs_paths = Vec::new();
3609 let mut stack = vec![Path::new("")];
3610 while let Some(path) = stack.pop() {
3611 bfs_paths.push(path);
3612 let ix = stack.len();
3613 for child_entry in self.child_entries(path) {
3614 stack.insert(ix, &child_entry.path);
3615 }
3616 }
3617
3618 let dfs_paths_via_iter = self
3619 .entries_by_path
3620 .cursor::<()>()
3621 .map(|e| e.path.as_ref())
3622 .collect::<Vec<_>>();
3623 assert_eq!(bfs_paths, dfs_paths_via_iter);
3624
3625 let dfs_paths_via_traversal = self
3626 .entries(true)
3627 .map(|e| e.path.as_ref())
3628 .collect::<Vec<_>>();
3629 assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter);
3630
3631 for ignore_parent_abs_path in self.ignores_by_parent_abs_path.keys() {
3632 let ignore_parent_path =
3633 ignore_parent_abs_path.strip_prefix(&self.abs_path).unwrap();
3634 assert!(self.entry_for_path(&ignore_parent_path).is_some());
3635 assert!(self
3636 .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
3637 .is_some());
3638 }
3639
3640 // Ensure extension counts are correct.
3641 let mut expected_extension_counts = HashMap::default();
3642 for extension in self.entries(false).filter_map(|e| e.path.extension()) {
3643 *expected_extension_counts
3644 .entry(extension.into())
3645 .or_insert(0) += 1;
3646 }
3647 assert_eq!(self.extension_counts, expected_extension_counts);
3648 }
3649
3650 fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
3651 let mut paths = Vec::new();
3652 for entry in self.entries_by_path.cursor::<()>() {
3653 if include_ignored || !entry.is_ignored {
3654 paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
3655 }
3656 }
3657 paths.sort_by(|a, b| a.0.cmp(b.0));
3658 paths
3659 }
3660 }
3661}