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