1mod ignore;
2mod worktree_settings;
3#[cfg(test)]
4mod worktree_tests;
5
6use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
7use anyhow::{Context as _, Result, anyhow};
8use clock::ReplicaId;
9use collections::{HashMap, HashSet, VecDeque};
10use fs::{Fs, MTime, PathEvent, RemoveOptions, Watcher, copy_recursive, read_dir_items};
11use futures::{
12 FutureExt as _, Stream, StreamExt,
13 channel::{
14 mpsc::{self, UnboundedSender},
15 oneshot,
16 },
17 select_biased,
18 task::Poll,
19};
20use fuzzy::CharBag;
21use git::{
22 COMMIT_MESSAGE, DOT_GIT, FSMONITOR_DAEMON, GITIGNORE, INDEX_LOCK, LFS_DIR,
23 repository::RepoPath, status::GitSummary,
24};
25use gpui::{
26 App, AppContext as _, AsyncApp, BackgroundExecutor, Context, Entity, EventEmitter, Task,
27};
28use ignore::IgnoreStack;
29use language::DiskState;
30
31use parking_lot::Mutex;
32use paths::{local_settings_folder_relative_path, local_vscode_folder_relative_path};
33use postage::{
34 barrier,
35 prelude::{Sink as _, Stream as _},
36 watch,
37};
38use rpc::{
39 AnyProtoClient,
40 proto::{self, FromProto, ToProto, split_worktree_update},
41};
42pub use settings::WorktreeId;
43use settings::{Settings, SettingsLocation, SettingsStore};
44use smallvec::{SmallVec, smallvec};
45use smol::channel::{self, Sender};
46use std::{
47 any::Any,
48 borrow::Borrow as _,
49 cmp::Ordering,
50 collections::hash_map,
51 convert::TryFrom,
52 ffi::OsStr,
53 fmt,
54 future::Future,
55 mem::{self},
56 ops::{Deref, DerefMut},
57 path::{Component, Path, PathBuf},
58 pin::Pin,
59 sync::{
60 Arc,
61 atomic::{AtomicUsize, Ordering::SeqCst},
62 },
63 time::{Duration, Instant},
64};
65use sum_tree::{Bias, Edit, KeyedItem, SeekTarget, SumTree, Summary, TreeMap, TreeSet, Unit};
66use text::{LineEnding, Rope};
67use util::{
68 ResultExt,
69 paths::{PathMatcher, SanitizedPath, home_dir},
70};
71pub use worktree_settings::WorktreeSettings;
72
73pub const FS_WATCH_LATENCY: Duration = Duration::from_millis(100);
74
75/// A set of local or remote files that are being opened as part of a project.
76/// Responsible for tracking related FS (for local)/collab (for remote) events and corresponding updates.
77/// Stores git repositories data and the diagnostics for the file(s).
78///
79/// Has an absolute path, and may be set to be visible in Zed UI or not.
80/// May correspond to a directory or a single file.
81/// Possible examples:
82/// * a drag and dropped file — may be added as an invisible, "ephemeral" entry to the current worktree
83/// * a directory opened in Zed — may be added as a visible entry to the current worktree
84///
85/// Uses [`Entry`] to track the state of each file/directory, can look up absolute paths for entries.
86pub enum Worktree {
87 Local(LocalWorktree),
88 Remote(RemoteWorktree),
89}
90
91/// An entry, created in the worktree.
92#[derive(Debug)]
93pub enum CreatedEntry {
94 /// Got created and indexed by the worktree, receiving a corresponding entry.
95 Included(Entry),
96 /// Got created, but not indexed due to falling under exclusion filters.
97 Excluded { abs_path: PathBuf },
98}
99
100pub struct LoadedFile {
101 pub file: Arc<File>,
102 pub text: String,
103}
104
105pub struct LoadedBinaryFile {
106 pub file: Arc<File>,
107 pub content: Vec<u8>,
108}
109
110impl fmt::Debug for LoadedBinaryFile {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 f.debug_struct("LoadedBinaryFile")
113 .field("file", &self.file)
114 .field("content_bytes", &self.content.len())
115 .finish()
116 }
117}
118
119pub struct LocalWorktree {
120 snapshot: LocalSnapshot,
121 scan_requests_tx: channel::Sender<ScanRequest>,
122 path_prefixes_to_scan_tx: channel::Sender<PathPrefixScanRequest>,
123 is_scanning: (watch::Sender<bool>, watch::Receiver<bool>),
124 _background_scanner_tasks: Vec<Task<()>>,
125 update_observer: Option<UpdateObservationState>,
126 fs: Arc<dyn Fs>,
127 fs_case_sensitive: bool,
128 visible: bool,
129 next_entry_id: Arc<AtomicUsize>,
130 settings: WorktreeSettings,
131 share_private_files: bool,
132}
133
134pub struct PathPrefixScanRequest {
135 path: Arc<Path>,
136 done: SmallVec<[barrier::Sender; 1]>,
137}
138
139struct ScanRequest {
140 relative_paths: Vec<Arc<Path>>,
141 done: SmallVec<[barrier::Sender; 1]>,
142}
143
144pub struct RemoteWorktree {
145 snapshot: Snapshot,
146 background_snapshot: Arc<Mutex<(Snapshot, Vec<proto::UpdateWorktree>)>>,
147 project_id: u64,
148 client: AnyProtoClient,
149 file_scan_inclusions: PathMatcher,
150 updates_tx: Option<UnboundedSender<proto::UpdateWorktree>>,
151 update_observer: Option<mpsc::UnboundedSender<proto::UpdateWorktree>>,
152 snapshot_subscriptions: VecDeque<(usize, oneshot::Sender<()>)>,
153 replica_id: ReplicaId,
154 visible: bool,
155 disconnected: bool,
156}
157
158#[derive(Clone)]
159pub struct Snapshot {
160 id: WorktreeId,
161 abs_path: SanitizedPath,
162 root_name: String,
163 root_char_bag: CharBag,
164 entries_by_path: SumTree<Entry>,
165 entries_by_id: SumTree<PathEntry>,
166 always_included_entries: Vec<Arc<Path>>,
167
168 /// A number that increases every time the worktree begins scanning
169 /// a set of paths from the filesystem. This scanning could be caused
170 /// by some operation performed on the worktree, such as reading or
171 /// writing a file, or by an event reported by the filesystem.
172 scan_id: usize,
173
174 /// The latest scan id that has completed, and whose preceding scans
175 /// have all completed. The current `scan_id` could be more than one
176 /// greater than the `completed_scan_id` if operations are performed
177 /// on the worktree while it is processing a file-system event.
178 completed_scan_id: usize,
179}
180
181/// This path corresponds to the 'content path' of a repository in relation
182/// to Zed's project root.
183/// In the majority of the cases, this is the folder that contains the .git folder.
184/// But if a sub-folder of a git repository is opened, this corresponds to the
185/// project root and the .git folder is located in a parent directory.
186#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
187pub enum WorkDirectory {
188 InProject {
189 relative_path: Arc<Path>,
190 },
191 AboveProject {
192 absolute_path: Arc<Path>,
193 location_in_repo: Arc<Path>,
194 },
195}
196
197impl WorkDirectory {
198 #[cfg(test)]
199 fn in_project(path: &str) -> Self {
200 let path = Path::new(path);
201 Self::InProject {
202 relative_path: path.into(),
203 }
204 }
205
206 //#[cfg(test)]
207 //fn canonicalize(&self) -> Self {
208 // match self {
209 // WorkDirectory::InProject { relative_path } => WorkDirectory::InProject {
210 // relative_path: relative_path.clone(),
211 // },
212 // WorkDirectory::AboveProject {
213 // absolute_path,
214 // location_in_repo,
215 // } => WorkDirectory::AboveProject {
216 // absolute_path: absolute_path.canonicalize().unwrap().into(),
217 // location_in_repo: location_in_repo.clone(),
218 // },
219 // }
220 //}
221
222 fn path_key(&self) -> PathKey {
223 match self {
224 WorkDirectory::InProject { relative_path } => PathKey(relative_path.clone()),
225 WorkDirectory::AboveProject { .. } => PathKey(Path::new("").into()),
226 }
227 }
228
229 /// Returns true if the given path is a child of the work directory.
230 ///
231 /// Note that the path may not be a member of this repository, if there
232 /// is a repository in a directory between these two paths
233 /// external .git folder in a parent folder of the project root.
234 #[track_caller]
235 pub fn directory_contains(&self, path: impl AsRef<Path>) -> bool {
236 let path = path.as_ref();
237 debug_assert!(path.is_relative());
238 match self {
239 WorkDirectory::InProject { relative_path } => path.starts_with(relative_path),
240 WorkDirectory::AboveProject { .. } => true,
241 }
242 }
243
244 /// relativize returns the given project path relative to the root folder of the
245 /// repository.
246 /// If the root of the repository (and its .git folder) are located in a parent folder
247 /// of the project root folder, then the returned RepoPath is relative to the root
248 /// of the repository and not a valid path inside the project.
249 pub fn relativize(&self, path: &Path) -> Result<RepoPath> {
250 // path is assumed to be relative to worktree root.
251 debug_assert!(path.is_relative());
252 match self {
253 WorkDirectory::InProject { relative_path } => Ok(path
254 .strip_prefix(relative_path)
255 .map_err(|_| {
256 anyhow!(
257 "could not relativize {:?} against {:?}",
258 path,
259 relative_path
260 )
261 })?
262 .into()),
263 WorkDirectory::AboveProject {
264 location_in_repo, ..
265 } => {
266 // Avoid joining a `/` to location_in_repo in the case of a single-file worktree.
267 if path == Path::new("") {
268 Ok(RepoPath(location_in_repo.clone()))
269 } else {
270 Ok(location_in_repo.join(path).into())
271 }
272 }
273 }
274 }
275
276 /// This is the opposite operation to `relativize` above
277 pub fn try_unrelativize(&self, path: &RepoPath) -> Option<Arc<Path>> {
278 match self {
279 WorkDirectory::InProject { relative_path } => Some(relative_path.join(path).into()),
280 WorkDirectory::AboveProject {
281 location_in_repo, ..
282 } => {
283 // If we fail to strip the prefix, that means this status entry is
284 // external to this worktree, and we definitely won't have an entry_id
285 path.strip_prefix(location_in_repo).ok().map(Into::into)
286 }
287 }
288 }
289
290 pub fn unrelativize(&self, path: &RepoPath) -> Arc<Path> {
291 match self {
292 WorkDirectory::InProject { relative_path } => relative_path.join(path).into(),
293 WorkDirectory::AboveProject {
294 location_in_repo, ..
295 } => {
296 if &path.0 == location_in_repo {
297 // Single-file worktree
298 return location_in_repo
299 .file_name()
300 .map(Path::new)
301 .unwrap_or(Path::new(""))
302 .into();
303 }
304 let mut location_in_repo = &**location_in_repo;
305 let mut parents = PathBuf::new();
306 loop {
307 if let Ok(segment) = path.strip_prefix(location_in_repo) {
308 return parents.join(segment).into();
309 }
310 location_in_repo = location_in_repo.parent().unwrap_or(Path::new(""));
311 parents.push(Component::ParentDir);
312 }
313 }
314 }
315 }
316
317 pub fn display_name(&self) -> String {
318 match self {
319 WorkDirectory::InProject { relative_path } => relative_path.display().to_string(),
320 WorkDirectory::AboveProject {
321 absolute_path,
322 location_in_repo,
323 } => {
324 let num_of_dots = location_in_repo.components().count();
325
326 "../".repeat(num_of_dots)
327 + &absolute_path
328 .file_name()
329 .map(|s| s.to_string_lossy())
330 .unwrap_or_default()
331 + "/"
332 }
333 }
334 }
335}
336
337impl Default for WorkDirectory {
338 fn default() -> Self {
339 Self::InProject {
340 relative_path: Arc::from(Path::new("")),
341 }
342 }
343}
344
345#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
346pub struct WorkDirectoryEntry(ProjectEntryId);
347
348impl Deref for WorkDirectoryEntry {
349 type Target = ProjectEntryId;
350
351 fn deref(&self) -> &Self::Target {
352 &self.0
353 }
354}
355
356impl From<ProjectEntryId> for WorkDirectoryEntry {
357 fn from(value: ProjectEntryId) -> Self {
358 WorkDirectoryEntry(value)
359 }
360}
361
362#[derive(Debug, Clone)]
363pub struct LocalSnapshot {
364 snapshot: Snapshot,
365 /// All of the gitignore files in the worktree, indexed by their relative path.
366 /// The boolean indicates whether the gitignore needs to be updated.
367 ignores_by_parent_abs_path: HashMap<Arc<Path>, (Arc<Gitignore>, bool)>,
368 /// All of the git repositories in the worktree, indexed by the project entry
369 /// id of their parent directory.
370 git_repositories: TreeMap<ProjectEntryId, LocalRepositoryEntry>,
371 /// The file handle of the root dir
372 /// (so we can find it after it's been moved)
373 root_file_handle: Option<Arc<dyn fs::FileHandle>>,
374}
375
376struct BackgroundScannerState {
377 snapshot: LocalSnapshot,
378 scanned_dirs: HashSet<ProjectEntryId>,
379 path_prefixes_to_scan: HashSet<Arc<Path>>,
380 paths_to_scan: HashSet<Arc<Path>>,
381 /// The ids of all of the entries that were removed from the snapshot
382 /// as part of the current update. These entry ids may be re-used
383 /// if the same inode is discovered at a new path, or if the given
384 /// path is re-created after being deleted.
385 removed_entries: HashMap<u64, Entry>,
386 changed_paths: Vec<Arc<Path>>,
387 prev_snapshot: Snapshot,
388}
389
390#[derive(Debug, Clone)]
391struct LocalRepositoryEntry {
392 work_directory_id: ProjectEntryId,
393 work_directory: WorkDirectory,
394 work_directory_abs_path: Arc<Path>,
395 git_dir_scan_id: usize,
396 /// Absolute path to the original .git entry that caused us to create this repository.
397 ///
398 /// This is normally a directory, but may be a "gitfile" that points to a directory elsewhere
399 /// (whose path we then store in `repository_dir_abs_path`).
400 dot_git_abs_path: Arc<Path>,
401 /// Absolute path to the "commondir" for this repository.
402 ///
403 /// This is always a directory. For a normal repository, this is the same as dot_git_abs_path,
404 /// but in the case of a submodule or a worktree it is the path to the "parent" .git directory
405 /// from which the submodule/worktree was derived.
406 common_dir_abs_path: Arc<Path>,
407 /// Absolute path to the directory holding the repository's state.
408 ///
409 /// For a normal repository, this is a directory and coincides with `dot_git_abs_path` and
410 /// `common_dir_abs_path`. For a submodule or worktree, this is some subdirectory of the
411 /// commondir like `/project/.git/modules/foo`.
412 repository_dir_abs_path: Arc<Path>,
413}
414
415impl sum_tree::Item for LocalRepositoryEntry {
416 type Summary = PathSummary<Unit>;
417
418 fn summary(&self, _: &<Self::Summary as Summary>::Context) -> Self::Summary {
419 PathSummary {
420 max_path: self.work_directory.path_key().0,
421 item_summary: Unit,
422 }
423 }
424}
425
426impl KeyedItem for LocalRepositoryEntry {
427 type Key = PathKey;
428
429 fn key(&self) -> Self::Key {
430 self.work_directory.path_key()
431 }
432}
433
434//impl LocalRepositoryEntry {
435// pub fn repo(&self) -> &Arc<dyn GitRepository> {
436// &self.repo_ptr
437// }
438//}
439
440impl Deref for LocalRepositoryEntry {
441 type Target = WorkDirectory;
442
443 fn deref(&self) -> &Self::Target {
444 &self.work_directory
445 }
446}
447
448impl Deref for LocalSnapshot {
449 type Target = Snapshot;
450
451 fn deref(&self) -> &Self::Target {
452 &self.snapshot
453 }
454}
455
456impl DerefMut for LocalSnapshot {
457 fn deref_mut(&mut self) -> &mut Self::Target {
458 &mut self.snapshot
459 }
460}
461
462#[derive(Debug)]
463enum ScanState {
464 Started,
465 Updated {
466 snapshot: LocalSnapshot,
467 changes: UpdatedEntriesSet,
468 barrier: SmallVec<[barrier::Sender; 1]>,
469 scanning: bool,
470 },
471 RootUpdated {
472 new_path: Option<SanitizedPath>,
473 },
474}
475
476struct UpdateObservationState {
477 snapshots_tx: mpsc::UnboundedSender<(LocalSnapshot, UpdatedEntriesSet)>,
478 resume_updates: watch::Sender<()>,
479 _maintain_remote_snapshot: Task<Option<()>>,
480}
481
482#[derive(Clone)]
483pub enum Event {
484 UpdatedEntries(UpdatedEntriesSet),
485 UpdatedGitRepositories(UpdatedGitRepositoriesSet),
486 DeletedEntry(ProjectEntryId),
487}
488
489const EMPTY_PATH: &str = "";
490
491impl EventEmitter<Event> for Worktree {}
492
493impl Worktree {
494 pub async fn local(
495 path: impl Into<Arc<Path>>,
496 visible: bool,
497 fs: Arc<dyn Fs>,
498 next_entry_id: Arc<AtomicUsize>,
499 cx: &mut AsyncApp,
500 ) -> Result<Entity<Self>> {
501 let abs_path = path.into();
502 let metadata = fs
503 .metadata(&abs_path)
504 .await
505 .context("failed to stat worktree path")?;
506
507 let fs_case_sensitive = fs.is_case_sensitive().await.unwrap_or_else(|e| {
508 log::error!(
509 "Failed to determine whether filesystem is case sensitive (falling back to true) due to error: {e:#}"
510 );
511 true
512 });
513
514 let root_file_handle = fs.open_handle(&abs_path).await.log_err();
515
516 cx.new(move |cx: &mut Context<Worktree>| {
517 let mut snapshot = LocalSnapshot {
518 ignores_by_parent_abs_path: Default::default(),
519 git_repositories: Default::default(),
520 snapshot: Snapshot::new(
521 cx.entity_id().as_u64(),
522 abs_path
523 .file_name()
524 .map_or(String::new(), |f| f.to_string_lossy().to_string()),
525 abs_path.clone(),
526 ),
527 root_file_handle,
528 };
529
530 let worktree_id = snapshot.id();
531 let settings_location = Some(SettingsLocation {
532 worktree_id,
533 path: Path::new(EMPTY_PATH),
534 });
535
536 let settings = WorktreeSettings::get(settings_location, cx).clone();
537 cx.observe_global::<SettingsStore>(move |this, cx| {
538 if let Self::Local(this) = this {
539 let settings = WorktreeSettings::get(settings_location, cx).clone();
540 if this.settings != settings {
541 this.settings = settings;
542 this.restart_background_scanners(cx);
543 }
544 }
545 })
546 .detach();
547
548 let share_private_files = false;
549 if let Some(metadata) = metadata {
550 let mut entry = Entry::new(
551 Arc::from(Path::new("")),
552 &metadata,
553 &next_entry_id,
554 snapshot.root_char_bag,
555 None,
556 );
557 if !metadata.is_dir {
558 entry.is_private = !share_private_files
559 && settings.is_path_private(abs_path.file_name().unwrap().as_ref());
560 }
561 snapshot.insert_entry(entry, fs.as_ref());
562 }
563
564 let (scan_requests_tx, scan_requests_rx) = channel::unbounded();
565 let (path_prefixes_to_scan_tx, path_prefixes_to_scan_rx) = channel::unbounded();
566 let mut worktree = LocalWorktree {
567 share_private_files,
568 next_entry_id,
569 snapshot,
570 is_scanning: watch::channel_with(true),
571 update_observer: None,
572 scan_requests_tx,
573 path_prefixes_to_scan_tx,
574 _background_scanner_tasks: Vec::new(),
575 fs,
576 fs_case_sensitive,
577 visible,
578 settings,
579 };
580 worktree.start_background_scanner(scan_requests_rx, path_prefixes_to_scan_rx, cx);
581 Worktree::Local(worktree)
582 })
583 }
584
585 pub fn remote(
586 project_id: u64,
587 replica_id: ReplicaId,
588 worktree: proto::WorktreeMetadata,
589 client: AnyProtoClient,
590 cx: &mut App,
591 ) -> Entity<Self> {
592 cx.new(|cx: &mut Context<Self>| {
593 let snapshot = Snapshot::new(
594 worktree.id,
595 worktree.root_name,
596 Arc::<Path>::from_proto(worktree.abs_path),
597 );
598
599 let background_snapshot = Arc::new(Mutex::new((
600 snapshot.clone(),
601 Vec::<proto::UpdateWorktree>::new(),
602 )));
603 let (background_updates_tx, mut background_updates_rx) =
604 mpsc::unbounded::<proto::UpdateWorktree>();
605 let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
606
607 let worktree_id = snapshot.id();
608 let settings_location = Some(SettingsLocation {
609 worktree_id,
610 path: Path::new(EMPTY_PATH),
611 });
612
613 let settings = WorktreeSettings::get(settings_location, cx).clone();
614 let worktree = RemoteWorktree {
615 client,
616 project_id,
617 replica_id,
618 snapshot,
619 file_scan_inclusions: settings.file_scan_inclusions.clone(),
620 background_snapshot: background_snapshot.clone(),
621 updates_tx: Some(background_updates_tx),
622 update_observer: None,
623 snapshot_subscriptions: Default::default(),
624 visible: worktree.visible,
625 disconnected: false,
626 };
627
628 // Apply updates to a separate snapshot in a background task, then
629 // send them to a foreground task which updates the model.
630 cx.background_spawn(async move {
631 while let Some(update) = background_updates_rx.next().await {
632 {
633 let mut lock = background_snapshot.lock();
634 lock.0
635 .apply_remote_update(update.clone(), &settings.file_scan_inclusions)
636 .log_err();
637 lock.1.push(update);
638 }
639 snapshot_updated_tx.send(()).await.ok();
640 }
641 })
642 .detach();
643
644 // On the foreground task, update to the latest snapshot and notify
645 // any update observer of all updates that led to that snapshot.
646 cx.spawn(async move |this, cx| {
647 while (snapshot_updated_rx.recv().await).is_some() {
648 this.update(cx, |this, cx| {
649 let mut entries_changed = false;
650 let this = this.as_remote_mut().unwrap();
651 {
652 let mut lock = this.background_snapshot.lock();
653 this.snapshot = lock.0.clone();
654 for update in lock.1.drain(..) {
655 entries_changed |= !update.updated_entries.is_empty()
656 || !update.removed_entries.is_empty();
657 if let Some(tx) = &this.update_observer {
658 tx.unbounded_send(update).ok();
659 }
660 }
661 };
662
663 if entries_changed {
664 cx.emit(Event::UpdatedEntries(Arc::default()));
665 }
666 cx.notify();
667 while let Some((scan_id, _)) = this.snapshot_subscriptions.front() {
668 if this.observed_snapshot(*scan_id) {
669 let (_, tx) = this.snapshot_subscriptions.pop_front().unwrap();
670 let _ = tx.send(());
671 } else {
672 break;
673 }
674 }
675 })?;
676 }
677 anyhow::Ok(())
678 })
679 .detach();
680
681 Worktree::Remote(worktree)
682 })
683 }
684
685 pub fn as_local(&self) -> Option<&LocalWorktree> {
686 if let Worktree::Local(worktree) = self {
687 Some(worktree)
688 } else {
689 None
690 }
691 }
692
693 pub fn as_remote(&self) -> Option<&RemoteWorktree> {
694 if let Worktree::Remote(worktree) = self {
695 Some(worktree)
696 } else {
697 None
698 }
699 }
700
701 pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
702 if let Worktree::Local(worktree) = self {
703 Some(worktree)
704 } else {
705 None
706 }
707 }
708
709 pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
710 if let Worktree::Remote(worktree) = self {
711 Some(worktree)
712 } else {
713 None
714 }
715 }
716
717 pub fn is_local(&self) -> bool {
718 matches!(self, Worktree::Local(_))
719 }
720
721 pub fn is_remote(&self) -> bool {
722 !self.is_local()
723 }
724
725 pub fn settings_location(&self, _: &Context<Self>) -> SettingsLocation<'static> {
726 SettingsLocation {
727 worktree_id: self.id(),
728 path: Path::new(EMPTY_PATH),
729 }
730 }
731
732 pub fn snapshot(&self) -> Snapshot {
733 match self {
734 Worktree::Local(worktree) => worktree.snapshot.snapshot.clone(),
735 Worktree::Remote(worktree) => worktree.snapshot.clone(),
736 }
737 }
738
739 pub fn scan_id(&self) -> usize {
740 match self {
741 Worktree::Local(worktree) => worktree.snapshot.scan_id,
742 Worktree::Remote(worktree) => worktree.snapshot.scan_id,
743 }
744 }
745
746 pub fn metadata_proto(&self) -> proto::WorktreeMetadata {
747 proto::WorktreeMetadata {
748 id: self.id().to_proto(),
749 root_name: self.root_name().to_string(),
750 visible: self.is_visible(),
751 abs_path: self.abs_path().to_proto(),
752 }
753 }
754
755 pub fn completed_scan_id(&self) -> usize {
756 match self {
757 Worktree::Local(worktree) => worktree.snapshot.completed_scan_id,
758 Worktree::Remote(worktree) => worktree.snapshot.completed_scan_id,
759 }
760 }
761
762 pub fn is_visible(&self) -> bool {
763 match self {
764 Worktree::Local(worktree) => worktree.visible,
765 Worktree::Remote(worktree) => worktree.visible,
766 }
767 }
768
769 pub fn replica_id(&self) -> ReplicaId {
770 match self {
771 Worktree::Local(_) => 0,
772 Worktree::Remote(worktree) => worktree.replica_id,
773 }
774 }
775
776 pub fn abs_path(&self) -> Arc<Path> {
777 match self {
778 Worktree::Local(worktree) => worktree.abs_path.clone().into(),
779 Worktree::Remote(worktree) => worktree.abs_path.clone().into(),
780 }
781 }
782
783 pub fn root_file(&self, cx: &Context<Self>) -> Option<Arc<File>> {
784 let entry = self.root_entry()?;
785 Some(File::for_entry(entry.clone(), cx.entity()))
786 }
787
788 pub fn observe_updates<F, Fut>(&mut self, project_id: u64, cx: &Context<Worktree>, callback: F)
789 where
790 F: 'static + Send + Fn(proto::UpdateWorktree) -> Fut,
791 Fut: 'static + Send + Future<Output = bool>,
792 {
793 match self {
794 Worktree::Local(this) => this.observe_updates(project_id, cx, callback),
795 Worktree::Remote(this) => this.observe_updates(project_id, cx, callback),
796 }
797 }
798
799 pub fn stop_observing_updates(&mut self) {
800 match self {
801 Worktree::Local(this) => {
802 this.update_observer.take();
803 }
804 Worktree::Remote(this) => {
805 this.update_observer.take();
806 }
807 }
808 }
809
810 #[cfg(any(test, feature = "test-support"))]
811 pub fn has_update_observer(&self) -> bool {
812 match self {
813 Worktree::Local(this) => this.update_observer.is_some(),
814 Worktree::Remote(this) => this.update_observer.is_some(),
815 }
816 }
817
818 pub fn load_file(&self, path: &Path, cx: &Context<Worktree>) -> Task<Result<LoadedFile>> {
819 match self {
820 Worktree::Local(this) => this.load_file(path, cx),
821 Worktree::Remote(_) => {
822 Task::ready(Err(anyhow!("remote worktrees can't yet load files")))
823 }
824 }
825 }
826
827 pub fn load_binary_file(
828 &self,
829 path: &Path,
830 cx: &Context<Worktree>,
831 ) -> Task<Result<LoadedBinaryFile>> {
832 match self {
833 Worktree::Local(this) => this.load_binary_file(path, cx),
834 Worktree::Remote(_) => {
835 Task::ready(Err(anyhow!("remote worktrees can't yet load binary files")))
836 }
837 }
838 }
839
840 pub fn write_file(
841 &self,
842 path: &Path,
843 text: Rope,
844 line_ending: LineEnding,
845 cx: &Context<Worktree>,
846 ) -> Task<Result<Arc<File>>> {
847 match self {
848 Worktree::Local(this) => this.write_file(path, text, line_ending, cx),
849 Worktree::Remote(_) => {
850 Task::ready(Err(anyhow!("remote worktree can't yet write files")))
851 }
852 }
853 }
854
855 pub fn create_entry(
856 &mut self,
857 path: impl Into<Arc<Path>>,
858 is_directory: bool,
859 content: Option<Vec<u8>>,
860 cx: &Context<Worktree>,
861 ) -> Task<Result<CreatedEntry>> {
862 let path: Arc<Path> = path.into();
863 let worktree_id = self.id();
864 match self {
865 Worktree::Local(this) => this.create_entry(path, is_directory, content, cx),
866 Worktree::Remote(this) => {
867 let project_id = this.project_id;
868 let request = this.client.request(proto::CreateProjectEntry {
869 worktree_id: worktree_id.to_proto(),
870 project_id,
871 path: path.as_ref().to_proto(),
872 content,
873 is_directory,
874 });
875 cx.spawn(async move |this, cx| {
876 let response = request.await?;
877 match response.entry {
878 Some(entry) => this
879 .update(cx, |worktree, cx| {
880 worktree.as_remote_mut().unwrap().insert_entry(
881 entry,
882 response.worktree_scan_id as usize,
883 cx,
884 )
885 })?
886 .await
887 .map(CreatedEntry::Included),
888 None => {
889 let abs_path = this.update(cx, |worktree, _| {
890 worktree
891 .absolutize(&path)
892 .with_context(|| format!("absolutizing {path:?}"))
893 })??;
894 Ok(CreatedEntry::Excluded { abs_path })
895 }
896 }
897 })
898 }
899 }
900 }
901
902 pub fn delete_entry(
903 &mut self,
904 entry_id: ProjectEntryId,
905 trash: bool,
906 cx: &mut Context<Worktree>,
907 ) -> Option<Task<Result<()>>> {
908 let task = match self {
909 Worktree::Local(this) => this.delete_entry(entry_id, trash, cx),
910 Worktree::Remote(this) => this.delete_entry(entry_id, trash, cx),
911 }?;
912
913 let entry = match &*self {
914 Worktree::Local(this) => this.entry_for_id(entry_id),
915 Worktree::Remote(this) => this.entry_for_id(entry_id),
916 }?;
917
918 let mut ids = vec![entry_id];
919 let path = &*entry.path;
920
921 self.get_children_ids_recursive(path, &mut ids);
922
923 for id in ids {
924 cx.emit(Event::DeletedEntry(id));
925 }
926 Some(task)
927 }
928
929 fn get_children_ids_recursive(&self, path: &Path, ids: &mut Vec<ProjectEntryId>) {
930 let children_iter = self.child_entries(path);
931 for child in children_iter {
932 ids.push(child.id);
933 self.get_children_ids_recursive(&child.path, ids);
934 }
935 }
936
937 pub fn rename_entry(
938 &mut self,
939 entry_id: ProjectEntryId,
940 new_path: impl Into<Arc<Path>>,
941 cx: &Context<Self>,
942 ) -> Task<Result<CreatedEntry>> {
943 let new_path = new_path.into();
944 match self {
945 Worktree::Local(this) => this.rename_entry(entry_id, new_path, cx),
946 Worktree::Remote(this) => this.rename_entry(entry_id, new_path, cx),
947 }
948 }
949
950 pub fn copy_entry(
951 &mut self,
952 entry_id: ProjectEntryId,
953 relative_worktree_source_path: Option<PathBuf>,
954 new_path: impl Into<Arc<Path>>,
955 cx: &Context<Self>,
956 ) -> Task<Result<Option<Entry>>> {
957 let new_path: Arc<Path> = new_path.into();
958 match self {
959 Worktree::Local(this) => {
960 this.copy_entry(entry_id, relative_worktree_source_path, new_path, cx)
961 }
962 Worktree::Remote(this) => {
963 let relative_worktree_source_path = relative_worktree_source_path
964 .map(|relative_worktree_source_path| relative_worktree_source_path.to_proto());
965 let response = this.client.request(proto::CopyProjectEntry {
966 project_id: this.project_id,
967 entry_id: entry_id.to_proto(),
968 relative_worktree_source_path,
969 new_path: new_path.to_proto(),
970 });
971 cx.spawn(async move |this, cx| {
972 let response = response.await?;
973 match response.entry {
974 Some(entry) => this
975 .update(cx, |worktree, cx| {
976 worktree.as_remote_mut().unwrap().insert_entry(
977 entry,
978 response.worktree_scan_id as usize,
979 cx,
980 )
981 })?
982 .await
983 .map(Some),
984 None => Ok(None),
985 }
986 })
987 }
988 }
989 }
990
991 pub fn copy_external_entries(
992 &mut self,
993 target_directory: Arc<Path>,
994 paths: Vec<Arc<Path>>,
995 fs: Arc<dyn Fs>,
996 cx: &Context<Worktree>,
997 ) -> Task<Result<Vec<ProjectEntryId>>> {
998 match self {
999 Worktree::Local(this) => this.copy_external_entries(target_directory, paths, cx),
1000 Worktree::Remote(this) => this.copy_external_entries(target_directory, paths, fs, cx),
1001 }
1002 }
1003
1004 pub fn expand_entry(
1005 &mut self,
1006 entry_id: ProjectEntryId,
1007 cx: &Context<Worktree>,
1008 ) -> Option<Task<Result<()>>> {
1009 match self {
1010 Worktree::Local(this) => this.expand_entry(entry_id, cx),
1011 Worktree::Remote(this) => {
1012 let response = this.client.request(proto::ExpandProjectEntry {
1013 project_id: this.project_id,
1014 entry_id: entry_id.to_proto(),
1015 });
1016 Some(cx.spawn(async move |this, cx| {
1017 let response = response.await?;
1018 this.update(cx, |this, _| {
1019 this.as_remote_mut()
1020 .unwrap()
1021 .wait_for_snapshot(response.worktree_scan_id as usize)
1022 })?
1023 .await?;
1024 Ok(())
1025 }))
1026 }
1027 }
1028 }
1029
1030 pub fn expand_all_for_entry(
1031 &mut self,
1032 entry_id: ProjectEntryId,
1033 cx: &Context<Worktree>,
1034 ) -> Option<Task<Result<()>>> {
1035 match self {
1036 Worktree::Local(this) => this.expand_all_for_entry(entry_id, cx),
1037 Worktree::Remote(this) => {
1038 let response = this.client.request(proto::ExpandAllForProjectEntry {
1039 project_id: this.project_id,
1040 entry_id: entry_id.to_proto(),
1041 });
1042 Some(cx.spawn(async move |this, cx| {
1043 let response = response.await?;
1044 this.update(cx, |this, _| {
1045 this.as_remote_mut()
1046 .unwrap()
1047 .wait_for_snapshot(response.worktree_scan_id as usize)
1048 })?
1049 .await?;
1050 Ok(())
1051 }))
1052 }
1053 }
1054 }
1055
1056 pub async fn handle_create_entry(
1057 this: Entity<Self>,
1058 request: proto::CreateProjectEntry,
1059 mut cx: AsyncApp,
1060 ) -> Result<proto::ProjectEntryResponse> {
1061 let (scan_id, entry) = this.update(&mut cx, |this, cx| {
1062 (
1063 this.scan_id(),
1064 this.create_entry(
1065 Arc::<Path>::from_proto(request.path),
1066 request.is_directory,
1067 request.content,
1068 cx,
1069 ),
1070 )
1071 })?;
1072 Ok(proto::ProjectEntryResponse {
1073 entry: match &entry.await? {
1074 CreatedEntry::Included(entry) => Some(entry.into()),
1075 CreatedEntry::Excluded { .. } => None,
1076 },
1077 worktree_scan_id: scan_id as u64,
1078 })
1079 }
1080
1081 pub async fn handle_delete_entry(
1082 this: Entity<Self>,
1083 request: proto::DeleteProjectEntry,
1084 mut cx: AsyncApp,
1085 ) -> Result<proto::ProjectEntryResponse> {
1086 let (scan_id, task) = this.update(&mut cx, |this, cx| {
1087 (
1088 this.scan_id(),
1089 this.delete_entry(
1090 ProjectEntryId::from_proto(request.entry_id),
1091 request.use_trash,
1092 cx,
1093 ),
1094 )
1095 })?;
1096 task.ok_or_else(|| anyhow!("invalid entry"))?.await?;
1097 Ok(proto::ProjectEntryResponse {
1098 entry: None,
1099 worktree_scan_id: scan_id as u64,
1100 })
1101 }
1102
1103 pub async fn handle_expand_entry(
1104 this: Entity<Self>,
1105 request: proto::ExpandProjectEntry,
1106 mut cx: AsyncApp,
1107 ) -> Result<proto::ExpandProjectEntryResponse> {
1108 let task = this.update(&mut cx, |this, cx| {
1109 this.expand_entry(ProjectEntryId::from_proto(request.entry_id), cx)
1110 })?;
1111 task.ok_or_else(|| anyhow!("no such entry"))?.await?;
1112 let scan_id = this.read_with(&cx, |this, _| this.scan_id())?;
1113 Ok(proto::ExpandProjectEntryResponse {
1114 worktree_scan_id: scan_id as u64,
1115 })
1116 }
1117
1118 pub async fn handle_expand_all_for_entry(
1119 this: Entity<Self>,
1120 request: proto::ExpandAllForProjectEntry,
1121 mut cx: AsyncApp,
1122 ) -> Result<proto::ExpandAllForProjectEntryResponse> {
1123 let task = this.update(&mut cx, |this, cx| {
1124 this.expand_all_for_entry(ProjectEntryId::from_proto(request.entry_id), cx)
1125 })?;
1126 task.ok_or_else(|| anyhow!("no such entry"))?.await?;
1127 let scan_id = this.read_with(&cx, |this, _| this.scan_id())?;
1128 Ok(proto::ExpandAllForProjectEntryResponse {
1129 worktree_scan_id: scan_id as u64,
1130 })
1131 }
1132
1133 pub async fn handle_rename_entry(
1134 this: Entity<Self>,
1135 request: proto::RenameProjectEntry,
1136 mut cx: AsyncApp,
1137 ) -> Result<proto::ProjectEntryResponse> {
1138 let (scan_id, task) = this.update(&mut cx, |this, cx| {
1139 (
1140 this.scan_id(),
1141 this.rename_entry(
1142 ProjectEntryId::from_proto(request.entry_id),
1143 Arc::<Path>::from_proto(request.new_path),
1144 cx,
1145 ),
1146 )
1147 })?;
1148 Ok(proto::ProjectEntryResponse {
1149 entry: match &task.await? {
1150 CreatedEntry::Included(entry) => Some(entry.into()),
1151 CreatedEntry::Excluded { .. } => None,
1152 },
1153 worktree_scan_id: scan_id as u64,
1154 })
1155 }
1156
1157 pub async fn handle_copy_entry(
1158 this: Entity<Self>,
1159 request: proto::CopyProjectEntry,
1160 mut cx: AsyncApp,
1161 ) -> Result<proto::ProjectEntryResponse> {
1162 let (scan_id, task) = this.update(&mut cx, |this, cx| {
1163 let relative_worktree_source_path = request
1164 .relative_worktree_source_path
1165 .map(PathBuf::from_proto);
1166 (
1167 this.scan_id(),
1168 this.copy_entry(
1169 ProjectEntryId::from_proto(request.entry_id),
1170 relative_worktree_source_path,
1171 PathBuf::from_proto(request.new_path),
1172 cx,
1173 ),
1174 )
1175 })?;
1176 Ok(proto::ProjectEntryResponse {
1177 entry: task.await?.as_ref().map(|e| e.into()),
1178 worktree_scan_id: scan_id as u64,
1179 })
1180 }
1181
1182 pub fn dot_git_abs_path(&self, work_directory: &WorkDirectory) -> PathBuf {
1183 let mut path = match work_directory {
1184 WorkDirectory::InProject { relative_path } => self.abs_path().join(relative_path),
1185 WorkDirectory::AboveProject { absolute_path, .. } => absolute_path.as_ref().to_owned(),
1186 };
1187 path.push(".git");
1188 path
1189 }
1190
1191 pub fn is_single_file(&self) -> bool {
1192 self.root_dir().is_none()
1193 }
1194
1195 /// For visible worktrees, returns the path with the worktree name as the first component.
1196 /// Otherwise, returns an absolute path.
1197 pub fn full_path(&self, worktree_relative_path: &Path) -> PathBuf {
1198 let mut full_path = PathBuf::new();
1199
1200 if self.is_visible() {
1201 full_path.push(self.root_name());
1202 } else {
1203 let path = self.abs_path();
1204
1205 if self.is_local() && path.starts_with(home_dir().as_path()) {
1206 full_path.push("~");
1207 full_path.push(path.strip_prefix(home_dir().as_path()).unwrap());
1208 } else {
1209 full_path.push(path)
1210 }
1211 }
1212
1213 if worktree_relative_path.components().next().is_some() {
1214 full_path.push(&worktree_relative_path);
1215 }
1216
1217 full_path
1218 }
1219}
1220
1221impl LocalWorktree {
1222 pub fn fs(&self) -> &Arc<dyn Fs> {
1223 &self.fs
1224 }
1225
1226 pub fn is_path_private(&self, path: &Path) -> bool {
1227 !self.share_private_files && self.settings.is_path_private(path)
1228 }
1229
1230 fn restart_background_scanners(&mut self, cx: &Context<Worktree>) {
1231 let (scan_requests_tx, scan_requests_rx) = channel::unbounded();
1232 let (path_prefixes_to_scan_tx, path_prefixes_to_scan_rx) = channel::unbounded();
1233 self.scan_requests_tx = scan_requests_tx;
1234 self.path_prefixes_to_scan_tx = path_prefixes_to_scan_tx;
1235
1236 self.start_background_scanner(scan_requests_rx, path_prefixes_to_scan_rx, cx);
1237 let always_included_entries = mem::take(&mut self.snapshot.always_included_entries);
1238 log::debug!(
1239 "refreshing entries for the following always included paths: {:?}",
1240 always_included_entries
1241 );
1242
1243 // Cleans up old always included entries to ensure they get updated properly. Otherwise,
1244 // nested always included entries may not get updated and will result in out-of-date info.
1245 self.refresh_entries_for_paths(always_included_entries);
1246 }
1247
1248 fn start_background_scanner(
1249 &mut self,
1250 scan_requests_rx: channel::Receiver<ScanRequest>,
1251 path_prefixes_to_scan_rx: channel::Receiver<PathPrefixScanRequest>,
1252 cx: &Context<Worktree>,
1253 ) {
1254 let snapshot = self.snapshot();
1255 let share_private_files = self.share_private_files;
1256 let next_entry_id = self.next_entry_id.clone();
1257 let fs = self.fs.clone();
1258 let settings = self.settings.clone();
1259 let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
1260 let background_scanner = cx.background_spawn({
1261 let abs_path = snapshot.abs_path.as_path().to_path_buf();
1262 let background = cx.background_executor().clone();
1263 async move {
1264 let (events, watcher) = fs.watch(&abs_path, FS_WATCH_LATENCY).await;
1265 let fs_case_sensitive = fs.is_case_sensitive().await.unwrap_or_else(|e| {
1266 log::error!("Failed to determine whether filesystem is case sensitive: {e:#}");
1267 true
1268 });
1269
1270 let mut scanner = BackgroundScanner {
1271 fs,
1272 fs_case_sensitive,
1273 status_updates_tx: scan_states_tx,
1274 executor: background,
1275 scan_requests_rx,
1276 path_prefixes_to_scan_rx,
1277 next_entry_id,
1278 state: Mutex::new(BackgroundScannerState {
1279 prev_snapshot: snapshot.snapshot.clone(),
1280 snapshot,
1281 scanned_dirs: Default::default(),
1282 path_prefixes_to_scan: Default::default(),
1283 paths_to_scan: Default::default(),
1284 removed_entries: Default::default(),
1285 changed_paths: Default::default(),
1286 }),
1287 phase: BackgroundScannerPhase::InitialScan,
1288 share_private_files,
1289 settings,
1290 watcher,
1291 };
1292
1293 scanner
1294 .run(Box::pin(events.map(|events| events.into_iter().collect())))
1295 .await;
1296 }
1297 });
1298 let scan_state_updater = cx.spawn(async move |this, cx| {
1299 while let Some((state, this)) = scan_states_rx.next().await.zip(this.upgrade()) {
1300 this.update(cx, |this, cx| {
1301 let this = this.as_local_mut().unwrap();
1302 match state {
1303 ScanState::Started => {
1304 *this.is_scanning.0.borrow_mut() = true;
1305 }
1306 ScanState::Updated {
1307 snapshot,
1308 changes,
1309 barrier,
1310 scanning,
1311 } => {
1312 *this.is_scanning.0.borrow_mut() = scanning;
1313 this.set_snapshot(snapshot, changes, cx);
1314 drop(barrier);
1315 }
1316 ScanState::RootUpdated { new_path } => {
1317 this.update_abs_path_and_refresh(new_path, cx);
1318 }
1319 }
1320 })
1321 .ok();
1322 }
1323 });
1324 self._background_scanner_tasks = vec![background_scanner, scan_state_updater];
1325 self.is_scanning = watch::channel_with(true);
1326 }
1327
1328 fn set_snapshot(
1329 &mut self,
1330 mut new_snapshot: LocalSnapshot,
1331 entry_changes: UpdatedEntriesSet,
1332 cx: &mut Context<Worktree>,
1333 ) {
1334 let repo_changes = self.changed_repos(&self.snapshot, &mut new_snapshot);
1335 self.snapshot = new_snapshot;
1336
1337 if let Some(share) = self.update_observer.as_mut() {
1338 share
1339 .snapshots_tx
1340 .unbounded_send((self.snapshot.clone(), entry_changes.clone()))
1341 .ok();
1342 }
1343
1344 if !entry_changes.is_empty() {
1345 cx.emit(Event::UpdatedEntries(entry_changes));
1346 }
1347 if !repo_changes.is_empty() {
1348 cx.emit(Event::UpdatedGitRepositories(repo_changes));
1349 }
1350 }
1351
1352 fn changed_repos(
1353 &self,
1354 old_snapshot: &LocalSnapshot,
1355 new_snapshot: &mut LocalSnapshot,
1356 ) -> UpdatedGitRepositoriesSet {
1357 let mut changes = Vec::new();
1358 let mut old_repos = old_snapshot.git_repositories.iter().peekable();
1359 let new_repos = new_snapshot.git_repositories.clone();
1360 let mut new_repos = new_repos.iter().peekable();
1361
1362 loop {
1363 match (new_repos.peek().map(clone), old_repos.peek().map(clone)) {
1364 (Some((new_entry_id, new_repo)), Some((old_entry_id, old_repo))) => {
1365 match Ord::cmp(&new_entry_id, &old_entry_id) {
1366 Ordering::Less => {
1367 changes.push(UpdatedGitRepository {
1368 work_directory_id: new_entry_id,
1369 old_work_directory_abs_path: None,
1370 new_work_directory_abs_path: Some(
1371 new_repo.work_directory_abs_path.clone(),
1372 ),
1373 dot_git_abs_path: Some(new_repo.dot_git_abs_path.clone()),
1374 repository_dir_abs_path: Some(
1375 new_repo.repository_dir_abs_path.clone(),
1376 ),
1377 common_dir_abs_path: Some(new_repo.common_dir_abs_path.clone()),
1378 });
1379 new_repos.next();
1380 }
1381 Ordering::Equal => {
1382 if new_repo.git_dir_scan_id != old_repo.git_dir_scan_id
1383 || new_repo.work_directory_abs_path
1384 != old_repo.work_directory_abs_path
1385 {
1386 changes.push(UpdatedGitRepository {
1387 work_directory_id: new_entry_id,
1388 old_work_directory_abs_path: Some(
1389 old_repo.work_directory_abs_path.clone(),
1390 ),
1391 new_work_directory_abs_path: Some(
1392 new_repo.work_directory_abs_path.clone(),
1393 ),
1394 dot_git_abs_path: Some(new_repo.dot_git_abs_path.clone()),
1395 repository_dir_abs_path: Some(
1396 new_repo.repository_dir_abs_path.clone(),
1397 ),
1398 common_dir_abs_path: Some(new_repo.common_dir_abs_path.clone()),
1399 });
1400 }
1401 new_repos.next();
1402 old_repos.next();
1403 }
1404 Ordering::Greater => {
1405 changes.push(UpdatedGitRepository {
1406 work_directory_id: old_entry_id,
1407 old_work_directory_abs_path: Some(
1408 old_repo.work_directory_abs_path.clone(),
1409 ),
1410 new_work_directory_abs_path: None,
1411 dot_git_abs_path: None,
1412 repository_dir_abs_path: None,
1413 common_dir_abs_path: None,
1414 });
1415 old_repos.next();
1416 }
1417 }
1418 }
1419 (Some((entry_id, repo)), None) => {
1420 changes.push(UpdatedGitRepository {
1421 work_directory_id: entry_id,
1422 old_work_directory_abs_path: None,
1423 new_work_directory_abs_path: Some(repo.work_directory_abs_path.clone()),
1424 dot_git_abs_path: Some(repo.dot_git_abs_path.clone()),
1425 repository_dir_abs_path: Some(repo.repository_dir_abs_path.clone()),
1426 common_dir_abs_path: Some(repo.common_dir_abs_path.clone()),
1427 });
1428 new_repos.next();
1429 }
1430 (None, Some((entry_id, repo))) => {
1431 changes.push(UpdatedGitRepository {
1432 work_directory_id: entry_id,
1433 old_work_directory_abs_path: Some(repo.work_directory_abs_path.clone()),
1434 new_work_directory_abs_path: None,
1435 dot_git_abs_path: Some(repo.dot_git_abs_path.clone()),
1436 repository_dir_abs_path: Some(repo.repository_dir_abs_path.clone()),
1437 common_dir_abs_path: Some(repo.common_dir_abs_path.clone()),
1438 });
1439 old_repos.next();
1440 }
1441 (None, None) => break,
1442 }
1443 }
1444
1445 fn clone<T: Clone, U: Clone>(value: &(&T, &U)) -> (T, U) {
1446 (value.0.clone(), value.1.clone())
1447 }
1448
1449 changes.into()
1450 }
1451
1452 pub fn scan_complete(&self) -> impl Future<Output = ()> + use<> {
1453 let mut is_scanning_rx = self.is_scanning.1.clone();
1454 async move {
1455 let mut is_scanning = *is_scanning_rx.borrow();
1456 while is_scanning {
1457 if let Some(value) = is_scanning_rx.recv().await {
1458 is_scanning = value;
1459 } else {
1460 break;
1461 }
1462 }
1463 }
1464 }
1465
1466 pub fn snapshot(&self) -> LocalSnapshot {
1467 self.snapshot.clone()
1468 }
1469
1470 pub fn settings(&self) -> WorktreeSettings {
1471 self.settings.clone()
1472 }
1473
1474 fn load_binary_file(
1475 &self,
1476 path: &Path,
1477 cx: &Context<Worktree>,
1478 ) -> Task<Result<LoadedBinaryFile>> {
1479 let path = Arc::from(path);
1480 let abs_path = self.absolutize(&path);
1481 let fs = self.fs.clone();
1482 let entry = self.refresh_entry(path.clone(), None, cx);
1483 let is_private = self.is_path_private(path.as_ref());
1484
1485 let worktree = cx.weak_entity();
1486 cx.background_spawn(async move {
1487 let abs_path = abs_path?;
1488 let content = fs.load_bytes(&abs_path).await?;
1489
1490 let worktree = worktree
1491 .upgrade()
1492 .ok_or_else(|| anyhow!("worktree was dropped"))?;
1493 let file = match entry.await? {
1494 Some(entry) => File::for_entry(entry, worktree),
1495 None => {
1496 let metadata = fs
1497 .metadata(&abs_path)
1498 .await
1499 .with_context(|| {
1500 format!("Loading metadata for excluded file {abs_path:?}")
1501 })?
1502 .with_context(|| {
1503 format!("Excluded file {abs_path:?} got removed during loading")
1504 })?;
1505 Arc::new(File {
1506 entry_id: None,
1507 worktree,
1508 path,
1509 disk_state: DiskState::Present {
1510 mtime: metadata.mtime,
1511 },
1512 is_local: true,
1513 is_private,
1514 })
1515 }
1516 };
1517
1518 Ok(LoadedBinaryFile { file, content })
1519 })
1520 }
1521
1522 fn load_file(&self, path: &Path, cx: &Context<Worktree>) -> Task<Result<LoadedFile>> {
1523 let path = Arc::from(path);
1524 let abs_path = self.absolutize(&path);
1525 let fs = self.fs.clone();
1526 let entry = self.refresh_entry(path.clone(), None, cx);
1527 let is_private = self.is_path_private(path.as_ref());
1528
1529 cx.spawn(async move |this, _cx| {
1530 let abs_path = abs_path?;
1531 // WARN: Temporary workaround for #27283.
1532 // We are not efficient with our memory usage per file, and use in excess of 64GB for a 10GB file
1533 // Therefore, as a temporary workaround to prevent system freezes, we just bail before opening a file
1534 // if it is too large
1535 // 5GB seems to be more reasonable, peaking at ~16GB, while 6GB jumps up to >24GB which seems like a
1536 // reasonable limit
1537 {
1538 const FILE_SIZE_MAX: u64 = 6 * 1024 * 1024 * 1024; // 6GB
1539 if let Ok(Some(metadata)) = fs.metadata(&abs_path).await {
1540 if metadata.len >= FILE_SIZE_MAX {
1541 anyhow::bail!("File is too large to load");
1542 }
1543 }
1544 }
1545 let text = fs.load(&abs_path).await?;
1546
1547 let worktree = this
1548 .upgrade()
1549 .ok_or_else(|| anyhow!("worktree was dropped"))?;
1550 let file = match entry.await? {
1551 Some(entry) => File::for_entry(entry, worktree),
1552 None => {
1553 let metadata = fs
1554 .metadata(&abs_path)
1555 .await
1556 .with_context(|| {
1557 format!("Loading metadata for excluded file {abs_path:?}")
1558 })?
1559 .with_context(|| {
1560 format!("Excluded file {abs_path:?} got removed during loading")
1561 })?;
1562 Arc::new(File {
1563 entry_id: None,
1564 worktree,
1565 path,
1566 disk_state: DiskState::Present {
1567 mtime: metadata.mtime,
1568 },
1569 is_local: true,
1570 is_private,
1571 })
1572 }
1573 };
1574
1575 Ok(LoadedFile { file, text })
1576 })
1577 }
1578
1579 /// Find the lowest path in the worktree's datastructures that is an ancestor
1580 fn lowest_ancestor(&self, path: &Path) -> PathBuf {
1581 let mut lowest_ancestor = None;
1582 for path in path.ancestors() {
1583 if self.entry_for_path(path).is_some() {
1584 lowest_ancestor = Some(path.to_path_buf());
1585 break;
1586 }
1587 }
1588
1589 lowest_ancestor.unwrap_or_else(|| PathBuf::from(""))
1590 }
1591
1592 fn create_entry(
1593 &self,
1594 path: impl Into<Arc<Path>>,
1595 is_dir: bool,
1596 content: Option<Vec<u8>>,
1597 cx: &Context<Worktree>,
1598 ) -> Task<Result<CreatedEntry>> {
1599 let path = path.into();
1600 let abs_path = match self.absolutize(&path) {
1601 Ok(path) => path,
1602 Err(e) => return Task::ready(Err(e.context(format!("absolutizing path {path:?}")))),
1603 };
1604 let path_excluded = self.settings.is_path_excluded(&abs_path);
1605 let fs = self.fs.clone();
1606 let task_abs_path = abs_path.clone();
1607 let write = cx.background_spawn(async move {
1608 if is_dir {
1609 fs.create_dir(&task_abs_path)
1610 .await
1611 .with_context(|| format!("creating directory {task_abs_path:?}"))
1612 } else {
1613 fs.write(&task_abs_path, content.as_deref().unwrap_or(&[]))
1614 .await
1615 .with_context(|| format!("creating file {task_abs_path:?}"))
1616 }
1617 });
1618
1619 let lowest_ancestor = self.lowest_ancestor(&path);
1620 cx.spawn(async move |this, cx| {
1621 write.await?;
1622 if path_excluded {
1623 return Ok(CreatedEntry::Excluded { abs_path });
1624 }
1625
1626 let (result, refreshes) = this.update(cx, |this, cx| {
1627 let mut refreshes = Vec::new();
1628 let refresh_paths = path.strip_prefix(&lowest_ancestor).unwrap();
1629 for refresh_path in refresh_paths.ancestors() {
1630 if refresh_path == Path::new("") {
1631 continue;
1632 }
1633 let refresh_full_path = lowest_ancestor.join(refresh_path);
1634
1635 refreshes.push(this.as_local_mut().unwrap().refresh_entry(
1636 refresh_full_path.into(),
1637 None,
1638 cx,
1639 ));
1640 }
1641 (
1642 this.as_local_mut().unwrap().refresh_entry(path, None, cx),
1643 refreshes,
1644 )
1645 })?;
1646 for refresh in refreshes {
1647 refresh.await.log_err();
1648 }
1649
1650 Ok(result
1651 .await?
1652 .map(CreatedEntry::Included)
1653 .unwrap_or_else(|| CreatedEntry::Excluded { abs_path }))
1654 })
1655 }
1656
1657 fn write_file(
1658 &self,
1659 path: impl Into<Arc<Path>>,
1660 text: Rope,
1661 line_ending: LineEnding,
1662 cx: &Context<Worktree>,
1663 ) -> Task<Result<Arc<File>>> {
1664 let path = path.into();
1665 let fs = self.fs.clone();
1666 let is_private = self.is_path_private(&path);
1667 let Ok(abs_path) = self.absolutize(&path) else {
1668 return Task::ready(Err(anyhow!("invalid path {path:?}")));
1669 };
1670
1671 let write = cx.background_spawn({
1672 let fs = fs.clone();
1673 let abs_path = abs_path.clone();
1674 async move { fs.save(&abs_path, &text, line_ending).await }
1675 });
1676
1677 cx.spawn(async move |this, cx| {
1678 write.await?;
1679 let entry = this
1680 .update(cx, |this, cx| {
1681 this.as_local_mut()
1682 .unwrap()
1683 .refresh_entry(path.clone(), None, cx)
1684 })?
1685 .await?;
1686 let worktree = this.upgrade().ok_or_else(|| anyhow!("worktree dropped"))?;
1687 if let Some(entry) = entry {
1688 Ok(File::for_entry(entry, worktree))
1689 } else {
1690 let metadata = fs
1691 .metadata(&abs_path)
1692 .await
1693 .with_context(|| {
1694 format!("Fetching metadata after saving the excluded buffer {abs_path:?}")
1695 })?
1696 .with_context(|| {
1697 format!("Excluded buffer {path:?} got removed during saving")
1698 })?;
1699 Ok(Arc::new(File {
1700 worktree,
1701 path,
1702 disk_state: DiskState::Present {
1703 mtime: metadata.mtime,
1704 },
1705 entry_id: None,
1706 is_local: true,
1707 is_private,
1708 }))
1709 }
1710 })
1711 }
1712
1713 fn delete_entry(
1714 &self,
1715 entry_id: ProjectEntryId,
1716 trash: bool,
1717 cx: &Context<Worktree>,
1718 ) -> Option<Task<Result<()>>> {
1719 let entry = self.entry_for_id(entry_id)?.clone();
1720 let abs_path = self.absolutize(&entry.path);
1721 let fs = self.fs.clone();
1722
1723 let delete = cx.background_spawn(async move {
1724 if entry.is_file() {
1725 if trash {
1726 fs.trash_file(&abs_path?, Default::default()).await?;
1727 } else {
1728 fs.remove_file(&abs_path?, Default::default()).await?;
1729 }
1730 } else if trash {
1731 fs.trash_dir(
1732 &abs_path?,
1733 RemoveOptions {
1734 recursive: true,
1735 ignore_if_not_exists: false,
1736 },
1737 )
1738 .await?;
1739 } else {
1740 fs.remove_dir(
1741 &abs_path?,
1742 RemoveOptions {
1743 recursive: true,
1744 ignore_if_not_exists: false,
1745 },
1746 )
1747 .await?;
1748 }
1749 anyhow::Ok(entry.path)
1750 });
1751
1752 Some(cx.spawn(async move |this, cx| {
1753 let path = delete.await?;
1754 this.update(cx, |this, _| {
1755 this.as_local_mut()
1756 .unwrap()
1757 .refresh_entries_for_paths(vec![path])
1758 })?
1759 .recv()
1760 .await;
1761 Ok(())
1762 }))
1763 }
1764
1765 /// Rename an entry.
1766 ///
1767 /// `new_path` is the new relative path to the worktree root.
1768 /// If the root entry is renamed then `new_path` is the new root name instead.
1769 fn rename_entry(
1770 &self,
1771 entry_id: ProjectEntryId,
1772 new_path: impl Into<Arc<Path>>,
1773 cx: &Context<Worktree>,
1774 ) -> Task<Result<CreatedEntry>> {
1775 let old_path = match self.entry_for_id(entry_id) {
1776 Some(entry) => entry.path.clone(),
1777 None => return Task::ready(Err(anyhow!("no entry to rename for id {entry_id:?}"))),
1778 };
1779 let new_path = new_path.into();
1780 let abs_old_path = self.absolutize(&old_path);
1781
1782 let is_root_entry = self.root_entry().is_some_and(|e| e.id == entry_id);
1783 let abs_new_path = if is_root_entry {
1784 let Some(root_parent_path) = self.abs_path().parent() else {
1785 return Task::ready(Err(anyhow!("no parent for path {:?}", self.abs_path)));
1786 };
1787 root_parent_path.join(&new_path)
1788 } else {
1789 let Ok(absolutize_path) = self.absolutize(&new_path) else {
1790 return Task::ready(Err(anyhow!("absolutizing path {new_path:?}")));
1791 };
1792 absolutize_path
1793 };
1794 let abs_path = abs_new_path.clone();
1795 let fs = self.fs.clone();
1796 let case_sensitive = self.fs_case_sensitive;
1797 let rename = cx.background_spawn(async move {
1798 let abs_old_path = abs_old_path?;
1799 let abs_new_path = abs_new_path;
1800
1801 let abs_old_path_lower = abs_old_path.to_str().map(|p| p.to_lowercase());
1802 let abs_new_path_lower = abs_new_path.to_str().map(|p| p.to_lowercase());
1803
1804 // If we're on a case-insensitive FS and we're doing a case-only rename (i.e. `foobar` to `FOOBAR`)
1805 // we want to overwrite, because otherwise we run into a file-already-exists error.
1806 let overwrite = !case_sensitive
1807 && abs_old_path != abs_new_path
1808 && abs_old_path_lower == abs_new_path_lower;
1809
1810 fs.rename(
1811 &abs_old_path,
1812 &abs_new_path,
1813 fs::RenameOptions {
1814 overwrite,
1815 ..Default::default()
1816 },
1817 )
1818 .await
1819 .with_context(|| format!("Renaming {abs_old_path:?} into {abs_new_path:?}"))
1820 });
1821
1822 cx.spawn(async move |this, cx| {
1823 rename.await?;
1824 Ok(this
1825 .update(cx, |this, cx| {
1826 let local = this.as_local_mut().unwrap();
1827 if is_root_entry {
1828 // We eagerly update `abs_path` and refresh this worktree.
1829 // Otherwise, the FS watcher would do it on the `RootUpdated` event,
1830 // but with a noticeable delay, so we handle it proactively.
1831 local.update_abs_path_and_refresh(
1832 Some(SanitizedPath::from(abs_path.clone())),
1833 cx,
1834 );
1835 Task::ready(Ok(this.root_entry().cloned()))
1836 } else {
1837 local.refresh_entry(new_path.clone(), Some(old_path), cx)
1838 }
1839 })?
1840 .await?
1841 .map(CreatedEntry::Included)
1842 .unwrap_or_else(|| CreatedEntry::Excluded { abs_path }))
1843 })
1844 }
1845
1846 fn copy_entry(
1847 &self,
1848 entry_id: ProjectEntryId,
1849 relative_worktree_source_path: Option<PathBuf>,
1850 new_path: impl Into<Arc<Path>>,
1851 cx: &Context<Worktree>,
1852 ) -> Task<Result<Option<Entry>>> {
1853 let old_path = match self.entry_for_id(entry_id) {
1854 Some(entry) => entry.path.clone(),
1855 None => return Task::ready(Ok(None)),
1856 };
1857 let new_path = new_path.into();
1858 let abs_old_path =
1859 if let Some(relative_worktree_source_path) = relative_worktree_source_path {
1860 Ok(self.abs_path().join(relative_worktree_source_path))
1861 } else {
1862 self.absolutize(&old_path)
1863 };
1864 let abs_new_path = self.absolutize(&new_path);
1865 let fs = self.fs.clone();
1866 let copy = cx.background_spawn(async move {
1867 copy_recursive(
1868 fs.as_ref(),
1869 &abs_old_path?,
1870 &abs_new_path?,
1871 Default::default(),
1872 )
1873 .await
1874 });
1875
1876 cx.spawn(async move |this, cx| {
1877 copy.await?;
1878 this.update(cx, |this, cx| {
1879 this.as_local_mut()
1880 .unwrap()
1881 .refresh_entry(new_path.clone(), None, cx)
1882 })?
1883 .await
1884 })
1885 }
1886
1887 pub fn copy_external_entries(
1888 &self,
1889 target_directory: Arc<Path>,
1890 paths: Vec<Arc<Path>>,
1891 cx: &Context<Worktree>,
1892 ) -> Task<Result<Vec<ProjectEntryId>>> {
1893 let Ok(target_directory) = self.absolutize(&target_directory) else {
1894 return Task::ready(Err(anyhow!("invalid target path")));
1895 };
1896 let worktree_path = self.abs_path().clone();
1897 let fs = self.fs.clone();
1898 let paths = paths
1899 .into_iter()
1900 .filter_map(|source| {
1901 let file_name = source.file_name()?;
1902 let mut target = target_directory.clone();
1903 target.push(file_name);
1904
1905 // Do not allow copying the same file to itself.
1906 if source.as_ref() != target.as_path() {
1907 Some((source, target))
1908 } else {
1909 None
1910 }
1911 })
1912 .collect::<Vec<_>>();
1913
1914 let paths_to_refresh = paths
1915 .iter()
1916 .filter_map(|(_, target)| Some(target.strip_prefix(&worktree_path).ok()?.into()))
1917 .collect::<Vec<_>>();
1918
1919 cx.spawn(async move |this, cx| {
1920 cx.background_spawn(async move {
1921 for (source, target) in paths {
1922 copy_recursive(
1923 fs.as_ref(),
1924 &source,
1925 &target,
1926 fs::CopyOptions {
1927 overwrite: true,
1928 ..Default::default()
1929 },
1930 )
1931 .await
1932 .with_context(|| {
1933 anyhow!("Failed to copy file from {source:?} to {target:?}")
1934 })?;
1935 }
1936 Ok::<(), anyhow::Error>(())
1937 })
1938 .await
1939 .log_err();
1940 let mut refresh = cx.read_entity(
1941 &this.upgrade().with_context(|| "Dropped worktree")?,
1942 |this, _| {
1943 Ok::<postage::barrier::Receiver, anyhow::Error>(
1944 this.as_local()
1945 .with_context(|| "Worktree is not local")?
1946 .refresh_entries_for_paths(paths_to_refresh.clone()),
1947 )
1948 },
1949 )??;
1950
1951 cx.background_spawn(async move {
1952 refresh.next().await;
1953 Ok::<(), anyhow::Error>(())
1954 })
1955 .await
1956 .log_err();
1957
1958 let this = this.upgrade().with_context(|| "Dropped worktree")?;
1959 cx.read_entity(&this, |this, _| {
1960 paths_to_refresh
1961 .iter()
1962 .filter_map(|path| Some(this.entry_for_path(path)?.id))
1963 .collect()
1964 })
1965 })
1966 }
1967
1968 fn expand_entry(
1969 &self,
1970 entry_id: ProjectEntryId,
1971 cx: &Context<Worktree>,
1972 ) -> Option<Task<Result<()>>> {
1973 let path = self.entry_for_id(entry_id)?.path.clone();
1974 let mut refresh = self.refresh_entries_for_paths(vec![path]);
1975 Some(cx.background_spawn(async move {
1976 refresh.next().await;
1977 Ok(())
1978 }))
1979 }
1980
1981 fn expand_all_for_entry(
1982 &self,
1983 entry_id: ProjectEntryId,
1984 cx: &Context<Worktree>,
1985 ) -> Option<Task<Result<()>>> {
1986 let path = self.entry_for_id(entry_id).unwrap().path.clone();
1987 let mut rx = self.add_path_prefix_to_scan(path.clone());
1988 Some(cx.background_spawn(async move {
1989 rx.next().await;
1990 Ok(())
1991 }))
1992 }
1993
1994 fn refresh_entries_for_paths(&self, paths: Vec<Arc<Path>>) -> barrier::Receiver {
1995 let (tx, rx) = barrier::channel();
1996 self.scan_requests_tx
1997 .try_send(ScanRequest {
1998 relative_paths: paths,
1999 done: smallvec![tx],
2000 })
2001 .ok();
2002 rx
2003 }
2004
2005 #[cfg(feature = "test-support")]
2006 pub fn manually_refresh_entries_for_paths(&self, paths: Vec<Arc<Path>>) -> barrier::Receiver {
2007 self.refresh_entries_for_paths(paths)
2008 }
2009
2010 pub fn add_path_prefix_to_scan(&self, path_prefix: Arc<Path>) -> barrier::Receiver {
2011 let (tx, rx) = barrier::channel();
2012 self.path_prefixes_to_scan_tx
2013 .try_send(PathPrefixScanRequest {
2014 path: path_prefix,
2015 done: smallvec![tx],
2016 })
2017 .ok();
2018 rx
2019 }
2020
2021 fn refresh_entry(
2022 &self,
2023 path: Arc<Path>,
2024 old_path: Option<Arc<Path>>,
2025 cx: &Context<Worktree>,
2026 ) -> Task<Result<Option<Entry>>> {
2027 if self.settings.is_path_excluded(&path) {
2028 return Task::ready(Ok(None));
2029 }
2030 let paths = if let Some(old_path) = old_path.as_ref() {
2031 vec![old_path.clone(), path.clone()]
2032 } else {
2033 vec![path.clone()]
2034 };
2035 let t0 = Instant::now();
2036 let mut refresh = self.refresh_entries_for_paths(paths);
2037 cx.spawn(async move |this, cx| {
2038 refresh.recv().await;
2039 log::trace!("refreshed entry {path:?} in {:?}", t0.elapsed());
2040 let new_entry = this.update(cx, |this, _| {
2041 this.entry_for_path(path)
2042 .cloned()
2043 .ok_or_else(|| anyhow!("failed to read path after update"))
2044 })??;
2045 Ok(Some(new_entry))
2046 })
2047 }
2048
2049 fn observe_updates<F, Fut>(&mut self, project_id: u64, cx: &Context<Worktree>, callback: F)
2050 where
2051 F: 'static + Send + Fn(proto::UpdateWorktree) -> Fut,
2052 Fut: 'static + Send + Future<Output = bool>,
2053 {
2054 if let Some(observer) = self.update_observer.as_mut() {
2055 *observer.resume_updates.borrow_mut() = ();
2056 return;
2057 }
2058
2059 let (resume_updates_tx, mut resume_updates_rx) = watch::channel::<()>();
2060 let (snapshots_tx, mut snapshots_rx) =
2061 mpsc::unbounded::<(LocalSnapshot, UpdatedEntriesSet)>();
2062 snapshots_tx
2063 .unbounded_send((self.snapshot(), Arc::default()))
2064 .ok();
2065
2066 let worktree_id = cx.entity_id().as_u64();
2067 let _maintain_remote_snapshot = cx.background_spawn(async move {
2068 let mut is_first = true;
2069 while let Some((snapshot, entry_changes)) = snapshots_rx.next().await {
2070 let update = if is_first {
2071 is_first = false;
2072 snapshot.build_initial_update(project_id, worktree_id)
2073 } else {
2074 snapshot.build_update(project_id, worktree_id, entry_changes)
2075 };
2076
2077 for update in proto::split_worktree_update(update) {
2078 let _ = resume_updates_rx.try_recv();
2079 loop {
2080 let result = callback(update.clone());
2081 if result.await {
2082 break;
2083 } else {
2084 log::info!("waiting to resume updates");
2085 if resume_updates_rx.next().await.is_none() {
2086 return Some(());
2087 }
2088 }
2089 }
2090 }
2091 }
2092 Some(())
2093 });
2094
2095 self.update_observer = Some(UpdateObservationState {
2096 snapshots_tx,
2097 resume_updates: resume_updates_tx,
2098 _maintain_remote_snapshot,
2099 });
2100 }
2101
2102 pub fn share_private_files(&mut self, cx: &Context<Worktree>) {
2103 self.share_private_files = true;
2104 self.restart_background_scanners(cx);
2105 }
2106
2107 fn update_abs_path_and_refresh(
2108 &mut self,
2109 new_path: Option<SanitizedPath>,
2110 cx: &Context<Worktree>,
2111 ) {
2112 if let Some(new_path) = new_path {
2113 self.snapshot.git_repositories = Default::default();
2114 self.snapshot.ignores_by_parent_abs_path = Default::default();
2115 let root_name = new_path
2116 .as_path()
2117 .file_name()
2118 .map_or(String::new(), |f| f.to_string_lossy().to_string());
2119 self.snapshot.update_abs_path(new_path, root_name);
2120 }
2121 self.restart_background_scanners(cx);
2122 }
2123}
2124
2125impl RemoteWorktree {
2126 pub fn project_id(&self) -> u64 {
2127 self.project_id
2128 }
2129
2130 pub fn client(&self) -> AnyProtoClient {
2131 self.client.clone()
2132 }
2133
2134 pub fn disconnected_from_host(&mut self) {
2135 self.updates_tx.take();
2136 self.snapshot_subscriptions.clear();
2137 self.disconnected = true;
2138 }
2139
2140 pub fn update_from_remote(&self, update: proto::UpdateWorktree) {
2141 if let Some(updates_tx) = &self.updates_tx {
2142 updates_tx
2143 .unbounded_send(update)
2144 .expect("consumer runs to completion");
2145 }
2146 }
2147
2148 fn observe_updates<F, Fut>(&mut self, project_id: u64, cx: &Context<Worktree>, callback: F)
2149 where
2150 F: 'static + Send + Fn(proto::UpdateWorktree) -> Fut,
2151 Fut: 'static + Send + Future<Output = bool>,
2152 {
2153 let (tx, mut rx) = mpsc::unbounded();
2154 let initial_update = self
2155 .snapshot
2156 .build_initial_update(project_id, self.id().to_proto());
2157 self.update_observer = Some(tx);
2158 cx.spawn(async move |this, cx| {
2159 let mut update = initial_update;
2160 'outer: loop {
2161 // SSH projects use a special project ID of 0, and we need to
2162 // remap it to the correct one here.
2163 update.project_id = project_id;
2164
2165 for chunk in split_worktree_update(update) {
2166 if !callback(chunk).await {
2167 break 'outer;
2168 }
2169 }
2170
2171 if let Some(next_update) = rx.next().await {
2172 update = next_update;
2173 } else {
2174 break;
2175 }
2176 }
2177 this.update(cx, |this, _| {
2178 let this = this.as_remote_mut().unwrap();
2179 this.update_observer.take();
2180 })
2181 })
2182 .detach();
2183 }
2184
2185 fn observed_snapshot(&self, scan_id: usize) -> bool {
2186 self.completed_scan_id >= scan_id
2187 }
2188
2189 pub fn wait_for_snapshot(
2190 &mut self,
2191 scan_id: usize,
2192 ) -> impl Future<Output = Result<()>> + use<> {
2193 let (tx, rx) = oneshot::channel();
2194 if self.observed_snapshot(scan_id) {
2195 let _ = tx.send(());
2196 } else if self.disconnected {
2197 drop(tx);
2198 } else {
2199 match self
2200 .snapshot_subscriptions
2201 .binary_search_by_key(&scan_id, |probe| probe.0)
2202 {
2203 Ok(ix) | Err(ix) => self.snapshot_subscriptions.insert(ix, (scan_id, tx)),
2204 }
2205 }
2206
2207 async move {
2208 rx.await?;
2209 Ok(())
2210 }
2211 }
2212
2213 fn insert_entry(
2214 &mut self,
2215 entry: proto::Entry,
2216 scan_id: usize,
2217 cx: &Context<Worktree>,
2218 ) -> Task<Result<Entry>> {
2219 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
2220 cx.spawn(async move |this, cx| {
2221 wait_for_snapshot.await?;
2222 this.update(cx, |worktree, _| {
2223 let worktree = worktree.as_remote_mut().unwrap();
2224 let snapshot = &mut worktree.background_snapshot.lock().0;
2225 let entry = snapshot.insert_entry(entry, &worktree.file_scan_inclusions);
2226 worktree.snapshot = snapshot.clone();
2227 entry
2228 })?
2229 })
2230 }
2231
2232 fn delete_entry(
2233 &self,
2234 entry_id: ProjectEntryId,
2235 trash: bool,
2236 cx: &Context<Worktree>,
2237 ) -> Option<Task<Result<()>>> {
2238 let response = self.client.request(proto::DeleteProjectEntry {
2239 project_id: self.project_id,
2240 entry_id: entry_id.to_proto(),
2241 use_trash: trash,
2242 });
2243 Some(cx.spawn(async move |this, cx| {
2244 let response = response.await?;
2245 let scan_id = response.worktree_scan_id as usize;
2246
2247 this.update(cx, move |this, _| {
2248 this.as_remote_mut().unwrap().wait_for_snapshot(scan_id)
2249 })?
2250 .await?;
2251
2252 this.update(cx, |this, _| {
2253 let this = this.as_remote_mut().unwrap();
2254 let snapshot = &mut this.background_snapshot.lock().0;
2255 snapshot.delete_entry(entry_id);
2256 this.snapshot = snapshot.clone();
2257 })
2258 }))
2259 }
2260
2261 fn rename_entry(
2262 &self,
2263 entry_id: ProjectEntryId,
2264 new_path: impl Into<Arc<Path>>,
2265 cx: &Context<Worktree>,
2266 ) -> Task<Result<CreatedEntry>> {
2267 let new_path: Arc<Path> = new_path.into();
2268 let response = self.client.request(proto::RenameProjectEntry {
2269 project_id: self.project_id,
2270 entry_id: entry_id.to_proto(),
2271 new_path: new_path.as_ref().to_proto(),
2272 });
2273 cx.spawn(async move |this, cx| {
2274 let response = response.await?;
2275 match response.entry {
2276 Some(entry) => this
2277 .update(cx, |this, cx| {
2278 this.as_remote_mut().unwrap().insert_entry(
2279 entry,
2280 response.worktree_scan_id as usize,
2281 cx,
2282 )
2283 })?
2284 .await
2285 .map(CreatedEntry::Included),
2286 None => {
2287 let abs_path = this.update(cx, |worktree, _| {
2288 worktree
2289 .absolutize(&new_path)
2290 .with_context(|| format!("absolutizing {new_path:?}"))
2291 })??;
2292 Ok(CreatedEntry::Excluded { abs_path })
2293 }
2294 }
2295 })
2296 }
2297
2298 fn copy_external_entries(
2299 &self,
2300 target_directory: Arc<Path>,
2301 paths_to_copy: Vec<Arc<Path>>,
2302 local_fs: Arc<dyn Fs>,
2303 cx: &Context<Worktree>,
2304 ) -> Task<Result<Vec<ProjectEntryId>, anyhow::Error>> {
2305 let client = self.client.clone();
2306 let worktree_id = self.id().to_proto();
2307 let project_id = self.project_id;
2308
2309 cx.background_spawn(async move {
2310 let mut requests = Vec::new();
2311 for root_path_to_copy in paths_to_copy {
2312 let Some(filename) = root_path_to_copy.file_name() else {
2313 continue;
2314 };
2315 for (abs_path, is_directory) in
2316 read_dir_items(local_fs.as_ref(), &root_path_to_copy).await?
2317 {
2318 let Ok(relative_path) = abs_path.strip_prefix(&root_path_to_copy) else {
2319 continue;
2320 };
2321 let content = if is_directory {
2322 None
2323 } else {
2324 Some(local_fs.load_bytes(&abs_path).await?)
2325 };
2326
2327 let mut target_path = target_directory.join(filename);
2328 if relative_path.file_name().is_some() {
2329 target_path.push(relative_path)
2330 }
2331
2332 requests.push(proto::CreateProjectEntry {
2333 project_id,
2334 worktree_id,
2335 path: target_path.to_string_lossy().to_string(),
2336 is_directory,
2337 content,
2338 });
2339 }
2340 }
2341 requests.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2342 requests.dedup();
2343
2344 let mut copied_entry_ids = Vec::new();
2345 for request in requests {
2346 let response = client.request(request).await?;
2347 copied_entry_ids.extend(response.entry.map(|e| ProjectEntryId::from_proto(e.id)));
2348 }
2349
2350 Ok(copied_entry_ids)
2351 })
2352 }
2353}
2354
2355impl Snapshot {
2356 pub fn new(id: u64, root_name: String, abs_path: Arc<Path>) -> Self {
2357 Snapshot {
2358 id: WorktreeId::from_usize(id as usize),
2359 abs_path: abs_path.into(),
2360 root_char_bag: root_name.chars().map(|c| c.to_ascii_lowercase()).collect(),
2361 root_name,
2362 always_included_entries: Default::default(),
2363 entries_by_path: Default::default(),
2364 entries_by_id: Default::default(),
2365 scan_id: 1,
2366 completed_scan_id: 0,
2367 }
2368 }
2369
2370 pub fn id(&self) -> WorktreeId {
2371 self.id
2372 }
2373
2374 // TODO:
2375 // Consider the following:
2376 //
2377 // ```rust
2378 // let abs_path: Arc<Path> = snapshot.abs_path(); // e.g. "C:\Users\user\Desktop\project"
2379 // let some_non_trimmed_path = Path::new("\\\\?\\C:\\Users\\user\\Desktop\\project\\main.rs");
2380 // // The caller perform some actions here:
2381 // some_non_trimmed_path.strip_prefix(abs_path); // This fails
2382 // some_non_trimmed_path.starts_with(abs_path); // This fails too
2383 // ```
2384 //
2385 // This is definitely a bug, but it's not clear if we should handle it here or not.
2386 pub fn abs_path(&self) -> &Arc<Path> {
2387 self.abs_path.as_path()
2388 }
2389
2390 fn build_initial_update(&self, project_id: u64, worktree_id: u64) -> proto::UpdateWorktree {
2391 let mut updated_entries = self
2392 .entries_by_path
2393 .iter()
2394 .map(proto::Entry::from)
2395 .collect::<Vec<_>>();
2396 updated_entries.sort_unstable_by_key(|e| e.id);
2397
2398 proto::UpdateWorktree {
2399 project_id,
2400 worktree_id,
2401 abs_path: self.abs_path().to_proto(),
2402 root_name: self.root_name().to_string(),
2403 updated_entries,
2404 removed_entries: Vec::new(),
2405 scan_id: self.scan_id as u64,
2406 is_last_update: self.completed_scan_id == self.scan_id,
2407 // Sent in separate messages.
2408 updated_repositories: Vec::new(),
2409 removed_repositories: Vec::new(),
2410 }
2411 }
2412
2413 pub fn work_directory_abs_path(&self, work_directory: &WorkDirectory) -> Result<PathBuf> {
2414 match work_directory {
2415 WorkDirectory::InProject { relative_path } => self.absolutize(relative_path),
2416 WorkDirectory::AboveProject { absolute_path, .. } => {
2417 Ok(absolute_path.as_ref().to_owned())
2418 }
2419 }
2420 }
2421
2422 pub fn absolutize(&self, path: &Path) -> Result<PathBuf> {
2423 if path
2424 .components()
2425 .any(|component| !matches!(component, std::path::Component::Normal(_)))
2426 {
2427 return Err(anyhow!("invalid path"));
2428 }
2429 if path.file_name().is_some() {
2430 Ok(self.abs_path.as_path().join(path))
2431 } else {
2432 Ok(self.abs_path.as_path().to_path_buf())
2433 }
2434 }
2435
2436 pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
2437 self.entries_by_id.get(&entry_id, &()).is_some()
2438 }
2439
2440 fn insert_entry(
2441 &mut self,
2442 entry: proto::Entry,
2443 always_included_paths: &PathMatcher,
2444 ) -> Result<Entry> {
2445 let entry = Entry::try_from((&self.root_char_bag, always_included_paths, entry))?;
2446 let old_entry = self.entries_by_id.insert_or_replace(
2447 PathEntry {
2448 id: entry.id,
2449 path: entry.path.clone(),
2450 is_ignored: entry.is_ignored,
2451 scan_id: 0,
2452 },
2453 &(),
2454 );
2455 if let Some(old_entry) = old_entry {
2456 self.entries_by_path.remove(&PathKey(old_entry.path), &());
2457 }
2458 self.entries_by_path.insert_or_replace(entry.clone(), &());
2459 Ok(entry)
2460 }
2461
2462 fn delete_entry(&mut self, entry_id: ProjectEntryId) -> Option<Arc<Path>> {
2463 let removed_entry = self.entries_by_id.remove(&entry_id, &())?;
2464 self.entries_by_path = {
2465 let mut cursor = self.entries_by_path.cursor::<TraversalProgress>(&());
2466 let mut new_entries_by_path =
2467 cursor.slice(&TraversalTarget::path(&removed_entry.path), Bias::Left, &());
2468 while let Some(entry) = cursor.item() {
2469 if entry.path.starts_with(&removed_entry.path) {
2470 self.entries_by_id.remove(&entry.id, &());
2471 cursor.next(&());
2472 } else {
2473 break;
2474 }
2475 }
2476 new_entries_by_path.append(cursor.suffix(&()), &());
2477 new_entries_by_path
2478 };
2479
2480 Some(removed_entry.path)
2481 }
2482
2483 fn update_abs_path(&mut self, abs_path: SanitizedPath, root_name: String) {
2484 self.abs_path = abs_path;
2485 if root_name != self.root_name {
2486 self.root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
2487 self.root_name = root_name;
2488 }
2489 }
2490
2491 fn apply_remote_update(
2492 &mut self,
2493 update: proto::UpdateWorktree,
2494 always_included_paths: &PathMatcher,
2495 ) -> Result<()> {
2496 log::debug!(
2497 "applying remote worktree update. {} entries updated, {} removed",
2498 update.updated_entries.len(),
2499 update.removed_entries.len()
2500 );
2501 self.update_abs_path(
2502 SanitizedPath::from(PathBuf::from_proto(update.abs_path)),
2503 update.root_name,
2504 );
2505
2506 let mut entries_by_path_edits = Vec::new();
2507 let mut entries_by_id_edits = Vec::new();
2508
2509 for entry_id in update.removed_entries {
2510 let entry_id = ProjectEntryId::from_proto(entry_id);
2511 entries_by_id_edits.push(Edit::Remove(entry_id));
2512 if let Some(entry) = self.entry_for_id(entry_id) {
2513 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
2514 }
2515 }
2516
2517 for entry in update.updated_entries {
2518 let entry = Entry::try_from((&self.root_char_bag, always_included_paths, entry))?;
2519 if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
2520 entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
2521 }
2522 if let Some(old_entry) = self.entries_by_path.get(&PathKey(entry.path.clone()), &()) {
2523 if old_entry.id != entry.id {
2524 entries_by_id_edits.push(Edit::Remove(old_entry.id));
2525 }
2526 }
2527 entries_by_id_edits.push(Edit::Insert(PathEntry {
2528 id: entry.id,
2529 path: entry.path.clone(),
2530 is_ignored: entry.is_ignored,
2531 scan_id: 0,
2532 }));
2533 entries_by_path_edits.push(Edit::Insert(entry));
2534 }
2535
2536 self.entries_by_path.edit(entries_by_path_edits, &());
2537 self.entries_by_id.edit(entries_by_id_edits, &());
2538
2539 self.scan_id = update.scan_id as usize;
2540 if update.is_last_update {
2541 self.completed_scan_id = update.scan_id as usize;
2542 }
2543
2544 Ok(())
2545 }
2546
2547 pub fn entry_count(&self) -> usize {
2548 self.entries_by_path.summary().count
2549 }
2550
2551 pub fn visible_entry_count(&self) -> usize {
2552 self.entries_by_path.summary().non_ignored_count
2553 }
2554
2555 pub fn dir_count(&self) -> usize {
2556 let summary = self.entries_by_path.summary();
2557 summary.count - summary.file_count
2558 }
2559
2560 pub fn visible_dir_count(&self) -> usize {
2561 let summary = self.entries_by_path.summary();
2562 summary.non_ignored_count - summary.non_ignored_file_count
2563 }
2564
2565 pub fn file_count(&self) -> usize {
2566 self.entries_by_path.summary().file_count
2567 }
2568
2569 pub fn visible_file_count(&self) -> usize {
2570 self.entries_by_path.summary().non_ignored_file_count
2571 }
2572
2573 fn traverse_from_offset(
2574 &self,
2575 include_files: bool,
2576 include_dirs: bool,
2577 include_ignored: bool,
2578 start_offset: usize,
2579 ) -> Traversal {
2580 let mut cursor = self.entries_by_path.cursor(&());
2581 cursor.seek(
2582 &TraversalTarget::Count {
2583 count: start_offset,
2584 include_files,
2585 include_dirs,
2586 include_ignored,
2587 },
2588 Bias::Right,
2589 &(),
2590 );
2591 Traversal {
2592 snapshot: self,
2593 cursor,
2594 include_files,
2595 include_dirs,
2596 include_ignored,
2597 }
2598 }
2599
2600 pub fn traverse_from_path(
2601 &self,
2602 include_files: bool,
2603 include_dirs: bool,
2604 include_ignored: bool,
2605 path: &Path,
2606 ) -> Traversal {
2607 Traversal::new(self, include_files, include_dirs, include_ignored, path)
2608 }
2609
2610 pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
2611 self.traverse_from_offset(true, false, include_ignored, start)
2612 }
2613
2614 pub fn directories(&self, include_ignored: bool, start: usize) -> Traversal {
2615 self.traverse_from_offset(false, true, include_ignored, start)
2616 }
2617
2618 pub fn entries(&self, include_ignored: bool, start: usize) -> Traversal {
2619 self.traverse_from_offset(true, true, include_ignored, start)
2620 }
2621
2622 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
2623 let empty_path = Path::new("");
2624 self.entries_by_path
2625 .cursor::<()>(&())
2626 .filter(move |entry| entry.path.as_ref() != empty_path)
2627 .map(|entry| &entry.path)
2628 }
2629
2630 pub fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
2631 let options = ChildEntriesOptions {
2632 include_files: true,
2633 include_dirs: true,
2634 include_ignored: true,
2635 };
2636 self.child_entries_with_options(parent_path, options)
2637 }
2638
2639 pub fn child_entries_with_options<'a>(
2640 &'a self,
2641 parent_path: &'a Path,
2642 options: ChildEntriesOptions,
2643 ) -> ChildEntriesIter<'a> {
2644 let mut cursor = self.entries_by_path.cursor(&());
2645 cursor.seek(&TraversalTarget::path(parent_path), Bias::Right, &());
2646 let traversal = Traversal {
2647 snapshot: self,
2648 cursor,
2649 include_files: options.include_files,
2650 include_dirs: options.include_dirs,
2651 include_ignored: options.include_ignored,
2652 };
2653 ChildEntriesIter {
2654 traversal,
2655 parent_path,
2656 }
2657 }
2658
2659 pub fn root_entry(&self) -> Option<&Entry> {
2660 self.entries_by_path.first()
2661 }
2662
2663 /// TODO: what's the difference between `root_dir` and `abs_path`?
2664 /// is there any? if so, document it.
2665 pub fn root_dir(&self) -> Option<Arc<Path>> {
2666 self.root_entry()
2667 .filter(|entry| entry.is_dir())
2668 .map(|_| self.abs_path().clone())
2669 }
2670
2671 pub fn root_name(&self) -> &str {
2672 &self.root_name
2673 }
2674
2675 pub fn scan_id(&self) -> usize {
2676 self.scan_id
2677 }
2678
2679 pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
2680 let path = path.as_ref();
2681 debug_assert!(path.is_relative());
2682 self.traverse_from_path(true, true, true, path)
2683 .entry()
2684 .and_then(|entry| {
2685 if entry.path.as_ref() == path {
2686 Some(entry)
2687 } else {
2688 None
2689 }
2690 })
2691 }
2692
2693 pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
2694 let entry = self.entries_by_id.get(&id, &())?;
2695 self.entry_for_path(&entry.path)
2696 }
2697
2698 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
2699 self.entry_for_path(path.as_ref()).map(|e| e.inode)
2700 }
2701}
2702
2703impl LocalSnapshot {
2704 fn local_repo_for_work_directory_path(&self, path: &Path) -> Option<&LocalRepositoryEntry> {
2705 self.git_repositories
2706 .iter()
2707 .map(|(_, entry)| entry)
2708 .find(|entry| entry.work_directory.path_key() == PathKey(path.into()))
2709 }
2710
2711 fn build_update(
2712 &self,
2713 project_id: u64,
2714 worktree_id: u64,
2715 entry_changes: UpdatedEntriesSet,
2716 ) -> proto::UpdateWorktree {
2717 let mut updated_entries = Vec::new();
2718 let mut removed_entries = Vec::new();
2719
2720 for (_, entry_id, path_change) in entry_changes.iter() {
2721 if let PathChange::Removed = path_change {
2722 removed_entries.push(entry_id.0 as u64);
2723 } else if let Some(entry) = self.entry_for_id(*entry_id) {
2724 updated_entries.push(proto::Entry::from(entry));
2725 }
2726 }
2727
2728 removed_entries.sort_unstable();
2729 updated_entries.sort_unstable_by_key(|e| e.id);
2730
2731 // TODO - optimize, knowing that removed_entries are sorted.
2732 removed_entries.retain(|id| updated_entries.binary_search_by_key(id, |e| e.id).is_err());
2733
2734 proto::UpdateWorktree {
2735 project_id,
2736 worktree_id,
2737 abs_path: self.abs_path().to_proto(),
2738 root_name: self.root_name().to_string(),
2739 updated_entries,
2740 removed_entries,
2741 scan_id: self.scan_id as u64,
2742 is_last_update: self.completed_scan_id == self.scan_id,
2743 // Sent in separate messages.
2744 updated_repositories: Vec::new(),
2745 removed_repositories: Vec::new(),
2746 }
2747 }
2748
2749 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
2750 if entry.is_file() && entry.path.file_name() == Some(&GITIGNORE) {
2751 let abs_path = self.abs_path.as_path().join(&entry.path);
2752 match smol::block_on(build_gitignore(&abs_path, fs)) {
2753 Ok(ignore) => {
2754 self.ignores_by_parent_abs_path
2755 .insert(abs_path.parent().unwrap().into(), (Arc::new(ignore), true));
2756 }
2757 Err(error) => {
2758 log::error!(
2759 "error loading .gitignore file {:?} - {:?}",
2760 &entry.path,
2761 error
2762 );
2763 }
2764 }
2765 }
2766
2767 if entry.kind == EntryKind::PendingDir {
2768 if let Some(existing_entry) =
2769 self.entries_by_path.get(&PathKey(entry.path.clone()), &())
2770 {
2771 entry.kind = existing_entry.kind;
2772 }
2773 }
2774
2775 let scan_id = self.scan_id;
2776 let removed = self.entries_by_path.insert_or_replace(entry.clone(), &());
2777 if let Some(removed) = removed {
2778 if removed.id != entry.id {
2779 self.entries_by_id.remove(&removed.id, &());
2780 }
2781 }
2782 self.entries_by_id.insert_or_replace(
2783 PathEntry {
2784 id: entry.id,
2785 path: entry.path.clone(),
2786 is_ignored: entry.is_ignored,
2787 scan_id,
2788 },
2789 &(),
2790 );
2791
2792 entry
2793 }
2794
2795 fn ancestor_inodes_for_path(&self, path: &Path) -> TreeSet<u64> {
2796 let mut inodes = TreeSet::default();
2797 for ancestor in path.ancestors().skip(1) {
2798 if let Some(entry) = self.entry_for_path(ancestor) {
2799 inodes.insert(entry.inode);
2800 }
2801 }
2802 inodes
2803 }
2804
2805 fn ignore_stack_for_abs_path(&self, abs_path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
2806 let mut new_ignores = Vec::new();
2807 for (index, ancestor) in abs_path.ancestors().enumerate() {
2808 if index > 0 {
2809 if let Some((ignore, _)) = self.ignores_by_parent_abs_path.get(ancestor) {
2810 new_ignores.push((ancestor, Some(ignore.clone())));
2811 } else {
2812 new_ignores.push((ancestor, None));
2813 }
2814 }
2815 if ancestor.join(*DOT_GIT).exists() {
2816 break;
2817 }
2818 }
2819
2820 let mut ignore_stack = IgnoreStack::none();
2821 for (parent_abs_path, ignore) in new_ignores.into_iter().rev() {
2822 if ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
2823 ignore_stack = IgnoreStack::all();
2824 break;
2825 } else if let Some(ignore) = ignore {
2826 ignore_stack = ignore_stack.append(parent_abs_path.into(), ignore);
2827 }
2828 }
2829
2830 if ignore_stack.is_abs_path_ignored(abs_path, is_dir) {
2831 ignore_stack = IgnoreStack::all();
2832 }
2833
2834 ignore_stack
2835 }
2836
2837 #[cfg(test)]
2838 fn expanded_entries(&self) -> impl Iterator<Item = &Entry> {
2839 self.entries_by_path
2840 .cursor::<()>(&())
2841 .filter(|entry| entry.kind == EntryKind::Dir && (entry.is_external || entry.is_ignored))
2842 }
2843
2844 #[cfg(test)]
2845 pub fn check_invariants(&self, git_state: bool) {
2846 use pretty_assertions::assert_eq;
2847
2848 assert_eq!(
2849 self.entries_by_path
2850 .cursor::<()>(&())
2851 .map(|e| (&e.path, e.id))
2852 .collect::<Vec<_>>(),
2853 self.entries_by_id
2854 .cursor::<()>(&())
2855 .map(|e| (&e.path, e.id))
2856 .collect::<collections::BTreeSet<_>>()
2857 .into_iter()
2858 .collect::<Vec<_>>(),
2859 "entries_by_path and entries_by_id are inconsistent"
2860 );
2861
2862 let mut files = self.files(true, 0);
2863 let mut visible_files = self.files(false, 0);
2864 for entry in self.entries_by_path.cursor::<()>(&()) {
2865 if entry.is_file() {
2866 assert_eq!(files.next().unwrap().inode, entry.inode);
2867 if (!entry.is_ignored && !entry.is_external) || entry.is_always_included {
2868 assert_eq!(visible_files.next().unwrap().inode, entry.inode);
2869 }
2870 }
2871 }
2872
2873 assert!(files.next().is_none());
2874 assert!(visible_files.next().is_none());
2875
2876 let mut bfs_paths = Vec::new();
2877 let mut stack = self
2878 .root_entry()
2879 .map(|e| e.path.as_ref())
2880 .into_iter()
2881 .collect::<Vec<_>>();
2882 while let Some(path) = stack.pop() {
2883 bfs_paths.push(path);
2884 let ix = stack.len();
2885 for child_entry in self.child_entries(path) {
2886 stack.insert(ix, &child_entry.path);
2887 }
2888 }
2889
2890 let dfs_paths_via_iter = self
2891 .entries_by_path
2892 .cursor::<()>(&())
2893 .map(|e| e.path.as_ref())
2894 .collect::<Vec<_>>();
2895 assert_eq!(bfs_paths, dfs_paths_via_iter);
2896
2897 let dfs_paths_via_traversal = self
2898 .entries(true, 0)
2899 .map(|e| e.path.as_ref())
2900 .collect::<Vec<_>>();
2901 assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter);
2902
2903 if git_state {
2904 for ignore_parent_abs_path in self.ignores_by_parent_abs_path.keys() {
2905 let ignore_parent_path = ignore_parent_abs_path
2906 .strip_prefix(self.abs_path.as_path())
2907 .unwrap();
2908 assert!(self.entry_for_path(ignore_parent_path).is_some());
2909 assert!(
2910 self.entry_for_path(ignore_parent_path.join(*GITIGNORE))
2911 .is_some()
2912 );
2913 }
2914 }
2915 }
2916
2917 #[cfg(test)]
2918 pub fn entries_without_ids(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
2919 let mut paths = Vec::new();
2920 for entry in self.entries_by_path.cursor::<()>(&()) {
2921 if include_ignored || !entry.is_ignored {
2922 paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
2923 }
2924 }
2925 paths.sort_by(|a, b| a.0.cmp(b.0));
2926 paths
2927 }
2928}
2929
2930impl BackgroundScannerState {
2931 fn should_scan_directory(&self, entry: &Entry) -> bool {
2932 (!entry.is_external && (!entry.is_ignored || entry.is_always_included))
2933 || entry.path.file_name() == Some(*DOT_GIT)
2934 || entry.path.file_name() == Some(local_settings_folder_relative_path().as_os_str())
2935 || entry.path.file_name() == Some(local_vscode_folder_relative_path().as_os_str())
2936 || self.scanned_dirs.contains(&entry.id) // If we've ever scanned it, keep scanning
2937 || self
2938 .paths_to_scan
2939 .iter()
2940 .any(|p| p.starts_with(&entry.path))
2941 || self
2942 .path_prefixes_to_scan
2943 .iter()
2944 .any(|p| entry.path.starts_with(p))
2945 }
2946
2947 fn enqueue_scan_dir(&self, abs_path: Arc<Path>, entry: &Entry, scan_job_tx: &Sender<ScanJob>) {
2948 let path = entry.path.clone();
2949 let ignore_stack = self.snapshot.ignore_stack_for_abs_path(&abs_path, true);
2950 let mut ancestor_inodes = self.snapshot.ancestor_inodes_for_path(&path);
2951
2952 if !ancestor_inodes.contains(&entry.inode) {
2953 ancestor_inodes.insert(entry.inode);
2954 scan_job_tx
2955 .try_send(ScanJob {
2956 abs_path,
2957 path,
2958 ignore_stack,
2959 scan_queue: scan_job_tx.clone(),
2960 ancestor_inodes,
2961 is_external: entry.is_external,
2962 })
2963 .unwrap();
2964 }
2965 }
2966
2967 fn reuse_entry_id(&mut self, entry: &mut Entry) {
2968 if let Some(mtime) = entry.mtime {
2969 // If an entry with the same inode was removed from the worktree during this scan,
2970 // then it *might* represent the same file or directory. But the OS might also have
2971 // re-used the inode for a completely different file or directory.
2972 //
2973 // Conditionally reuse the old entry's id:
2974 // * if the mtime is the same, the file was probably been renamed.
2975 // * if the path is the same, the file may just have been updated
2976 if let Some(removed_entry) = self.removed_entries.remove(&entry.inode) {
2977 if removed_entry.mtime == Some(mtime) || removed_entry.path == entry.path {
2978 entry.id = removed_entry.id;
2979 }
2980 } else if let Some(existing_entry) = self.snapshot.entry_for_path(&entry.path) {
2981 entry.id = existing_entry.id;
2982 }
2983 }
2984 }
2985
2986 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs, watcher: &dyn Watcher) -> Entry {
2987 self.reuse_entry_id(&mut entry);
2988 let entry = self.snapshot.insert_entry(entry, fs);
2989 if entry.path.file_name() == Some(&DOT_GIT) {
2990 self.insert_git_repository(entry.path.clone(), fs, watcher);
2991 }
2992
2993 #[cfg(test)]
2994 self.snapshot.check_invariants(false);
2995
2996 entry
2997 }
2998
2999 fn populate_dir(
3000 &mut self,
3001 parent_path: &Arc<Path>,
3002 entries: impl IntoIterator<Item = Entry>,
3003 ignore: Option<Arc<Gitignore>>,
3004 ) {
3005 let mut parent_entry = if let Some(parent_entry) = self
3006 .snapshot
3007 .entries_by_path
3008 .get(&PathKey(parent_path.clone()), &())
3009 {
3010 parent_entry.clone()
3011 } else {
3012 log::warn!(
3013 "populating a directory {:?} that has been removed",
3014 parent_path
3015 );
3016 return;
3017 };
3018
3019 match parent_entry.kind {
3020 EntryKind::PendingDir | EntryKind::UnloadedDir => parent_entry.kind = EntryKind::Dir,
3021 EntryKind::Dir => {}
3022 _ => return,
3023 }
3024
3025 if let Some(ignore) = ignore {
3026 let abs_parent_path = self.snapshot.abs_path.as_path().join(parent_path).into();
3027 self.snapshot
3028 .ignores_by_parent_abs_path
3029 .insert(abs_parent_path, (ignore, false));
3030 }
3031
3032 let parent_entry_id = parent_entry.id;
3033 self.scanned_dirs.insert(parent_entry_id);
3034 let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
3035 let mut entries_by_id_edits = Vec::new();
3036
3037 for entry in entries {
3038 entries_by_id_edits.push(Edit::Insert(PathEntry {
3039 id: entry.id,
3040 path: entry.path.clone(),
3041 is_ignored: entry.is_ignored,
3042 scan_id: self.snapshot.scan_id,
3043 }));
3044 entries_by_path_edits.push(Edit::Insert(entry));
3045 }
3046
3047 self.snapshot
3048 .entries_by_path
3049 .edit(entries_by_path_edits, &());
3050 self.snapshot.entries_by_id.edit(entries_by_id_edits, &());
3051
3052 if let Err(ix) = self.changed_paths.binary_search(parent_path) {
3053 self.changed_paths.insert(ix, parent_path.clone());
3054 }
3055
3056 #[cfg(test)]
3057 self.snapshot.check_invariants(false);
3058 }
3059
3060 fn remove_path(&mut self, path: &Path) {
3061 log::trace!("background scanner removing path {path:?}");
3062 let mut new_entries;
3063 let removed_entries;
3064 {
3065 let mut cursor = self
3066 .snapshot
3067 .entries_by_path
3068 .cursor::<TraversalProgress>(&());
3069 new_entries = cursor.slice(&TraversalTarget::path(path), Bias::Left, &());
3070 removed_entries = cursor.slice(&TraversalTarget::successor(path), Bias::Left, &());
3071 new_entries.append(cursor.suffix(&()), &());
3072 }
3073 self.snapshot.entries_by_path = new_entries;
3074
3075 let mut removed_ids = Vec::with_capacity(removed_entries.summary().count);
3076 for entry in removed_entries.cursor::<()>(&()) {
3077 match self.removed_entries.entry(entry.inode) {
3078 hash_map::Entry::Occupied(mut e) => {
3079 let prev_removed_entry = e.get_mut();
3080 if entry.id > prev_removed_entry.id {
3081 *prev_removed_entry = entry.clone();
3082 }
3083 }
3084 hash_map::Entry::Vacant(e) => {
3085 e.insert(entry.clone());
3086 }
3087 }
3088
3089 if entry.path.file_name() == Some(&GITIGNORE) {
3090 let abs_parent_path = self
3091 .snapshot
3092 .abs_path
3093 .as_path()
3094 .join(entry.path.parent().unwrap());
3095 if let Some((_, needs_update)) = self
3096 .snapshot
3097 .ignores_by_parent_abs_path
3098 .get_mut(abs_parent_path.as_path())
3099 {
3100 *needs_update = true;
3101 }
3102 }
3103
3104 if let Err(ix) = removed_ids.binary_search(&entry.id) {
3105 removed_ids.insert(ix, entry.id);
3106 }
3107 }
3108
3109 self.snapshot.entries_by_id.edit(
3110 removed_ids.iter().map(|&id| Edit::Remove(id)).collect(),
3111 &(),
3112 );
3113 self.snapshot
3114 .git_repositories
3115 .retain(|id, _| removed_ids.binary_search(id).is_err());
3116
3117 #[cfg(test)]
3118 self.snapshot.check_invariants(false);
3119 }
3120
3121 fn insert_git_repository(
3122 &mut self,
3123 dot_git_path: Arc<Path>,
3124 fs: &dyn Fs,
3125 watcher: &dyn Watcher,
3126 ) {
3127 let work_dir_path: Arc<Path> = match dot_git_path.parent() {
3128 Some(parent_dir) => {
3129 // Guard against repositories inside the repository metadata
3130 if parent_dir.iter().any(|component| component == *DOT_GIT) {
3131 log::debug!(
3132 "not building git repository for nested `.git` directory, `.git` path in the worktree: {dot_git_path:?}"
3133 );
3134 return;
3135 };
3136
3137 parent_dir.into()
3138 }
3139 None => {
3140 // `dot_git_path.parent().is_none()` means `.git` directory is the opened worktree itself,
3141 // no files inside that directory are tracked by git, so no need to build the repo around it
3142 log::debug!(
3143 "not building git repository for the worktree itself, `.git` path in the worktree: {dot_git_path:?}"
3144 );
3145 return;
3146 }
3147 };
3148
3149 self.insert_git_repository_for_path(
3150 WorkDirectory::InProject {
3151 relative_path: work_dir_path,
3152 },
3153 dot_git_path,
3154 fs,
3155 watcher,
3156 );
3157 }
3158
3159 fn insert_git_repository_for_path(
3160 &mut self,
3161 work_directory: WorkDirectory,
3162 dot_git_path: Arc<Path>,
3163 fs: &dyn Fs,
3164 watcher: &dyn Watcher,
3165 ) -> Option<LocalRepositoryEntry> {
3166 let work_dir_entry = self.snapshot.entry_for_path(work_directory.path_key().0)?;
3167 let work_directory_abs_path = self
3168 .snapshot
3169 .work_directory_abs_path(&work_directory)
3170 .log_err()?;
3171
3172 if self
3173 .snapshot
3174 .git_repositories
3175 .get(&work_dir_entry.id)
3176 .is_some()
3177 {
3178 log::trace!("existing git repository for {work_directory:?}");
3179 return None;
3180 }
3181
3182 let dot_git_abs_path: Arc<Path> = self
3183 .snapshot
3184 .abs_path
3185 .as_path()
3186 .join(&dot_git_path)
3187 .as_path()
3188 .into();
3189
3190 let (repository_dir_abs_path, common_dir_abs_path) =
3191 discover_git_paths(&dot_git_abs_path, fs);
3192 watcher.add(&common_dir_abs_path).log_err();
3193 if !repository_dir_abs_path.starts_with(&common_dir_abs_path) {
3194 watcher.add(&repository_dir_abs_path).log_err();
3195 }
3196
3197 let work_directory_id = work_dir_entry.id;
3198
3199 let local_repository = LocalRepositoryEntry {
3200 work_directory_id,
3201 work_directory,
3202 work_directory_abs_path: work_directory_abs_path.as_path().into(),
3203 git_dir_scan_id: 0,
3204 dot_git_abs_path,
3205 common_dir_abs_path,
3206 repository_dir_abs_path,
3207 };
3208
3209 self.snapshot
3210 .git_repositories
3211 .insert(work_directory_id, local_repository.clone());
3212
3213 log::trace!("inserting new local git repository");
3214 Some(local_repository)
3215 }
3216}
3217
3218async fn is_git_dir(path: &Path, fs: &dyn Fs) -> bool {
3219 if path.file_name() == Some(&*DOT_GIT) {
3220 return true;
3221 }
3222
3223 // If we're in a bare repository, we are not inside a `.git` folder. In a
3224 // bare repository, the root folder contains what would normally be in the
3225 // `.git` folder.
3226 let head_metadata = fs.metadata(&path.join("HEAD")).await;
3227 if !matches!(head_metadata, Ok(Some(_))) {
3228 return false;
3229 }
3230 let config_metadata = fs.metadata(&path.join("config")).await;
3231 matches!(config_metadata, Ok(Some(_)))
3232}
3233
3234async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
3235 let contents = fs.load(abs_path).await?;
3236 let parent = abs_path.parent().unwrap_or_else(|| Path::new("/"));
3237 let mut builder = GitignoreBuilder::new(parent);
3238 for line in contents.lines() {
3239 builder.add_line(Some(abs_path.into()), line)?;
3240 }
3241 Ok(builder.build()?)
3242}
3243
3244impl Deref for Worktree {
3245 type Target = Snapshot;
3246
3247 fn deref(&self) -> &Self::Target {
3248 match self {
3249 Worktree::Local(worktree) => &worktree.snapshot,
3250 Worktree::Remote(worktree) => &worktree.snapshot,
3251 }
3252 }
3253}
3254
3255impl Deref for LocalWorktree {
3256 type Target = LocalSnapshot;
3257
3258 fn deref(&self) -> &Self::Target {
3259 &self.snapshot
3260 }
3261}
3262
3263impl Deref for RemoteWorktree {
3264 type Target = Snapshot;
3265
3266 fn deref(&self) -> &Self::Target {
3267 &self.snapshot
3268 }
3269}
3270
3271impl fmt::Debug for LocalWorktree {
3272 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3273 self.snapshot.fmt(f)
3274 }
3275}
3276
3277impl fmt::Debug for Snapshot {
3278 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3279 struct EntriesById<'a>(&'a SumTree<PathEntry>);
3280 struct EntriesByPath<'a>(&'a SumTree<Entry>);
3281
3282 impl fmt::Debug for EntriesByPath<'_> {
3283 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3284 f.debug_map()
3285 .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
3286 .finish()
3287 }
3288 }
3289
3290 impl fmt::Debug for EntriesById<'_> {
3291 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3292 f.debug_list().entries(self.0.iter()).finish()
3293 }
3294 }
3295
3296 f.debug_struct("Snapshot")
3297 .field("id", &self.id)
3298 .field("root_name", &self.root_name)
3299 .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
3300 .field("entries_by_id", &EntriesById(&self.entries_by_id))
3301 .finish()
3302 }
3303}
3304
3305#[derive(Debug, Clone, PartialEq)]
3306pub struct File {
3307 pub worktree: Entity<Worktree>,
3308 pub path: Arc<Path>,
3309 pub disk_state: DiskState,
3310 pub entry_id: Option<ProjectEntryId>,
3311 pub is_local: bool,
3312 pub is_private: bool,
3313}
3314
3315impl language::File for File {
3316 fn as_local(&self) -> Option<&dyn language::LocalFile> {
3317 if self.is_local { Some(self) } else { None }
3318 }
3319
3320 fn disk_state(&self) -> DiskState {
3321 self.disk_state
3322 }
3323
3324 fn path(&self) -> &Arc<Path> {
3325 &self.path
3326 }
3327
3328 fn full_path(&self, cx: &App) -> PathBuf {
3329 self.worktree.read(cx).full_path(&self.path)
3330 }
3331
3332 /// Returns the last component of this handle's absolute path. If this handle refers to the root
3333 /// of its worktree, then this method will return the name of the worktree itself.
3334 fn file_name<'a>(&'a self, cx: &'a App) -> &'a OsStr {
3335 self.path
3336 .file_name()
3337 .unwrap_or_else(|| OsStr::new(&self.worktree.read(cx).root_name))
3338 }
3339
3340 fn worktree_id(&self, cx: &App) -> WorktreeId {
3341 self.worktree.read(cx).id()
3342 }
3343
3344 fn to_proto(&self, cx: &App) -> rpc::proto::File {
3345 rpc::proto::File {
3346 worktree_id: self.worktree.read(cx).id().to_proto(),
3347 entry_id: self.entry_id.map(|id| id.to_proto()),
3348 path: self.path.as_ref().to_proto(),
3349 mtime: self.disk_state.mtime().map(|time| time.into()),
3350 is_deleted: self.disk_state == DiskState::Deleted,
3351 }
3352 }
3353
3354 fn is_private(&self) -> bool {
3355 self.is_private
3356 }
3357}
3358
3359impl language::LocalFile for File {
3360 fn abs_path(&self, cx: &App) -> PathBuf {
3361 let worktree_path = &self.worktree.read(cx).abs_path();
3362 if self.path.as_ref() == Path::new("") {
3363 worktree_path.to_path_buf()
3364 } else {
3365 worktree_path.join(&self.path)
3366 }
3367 }
3368
3369 fn load(&self, cx: &App) -> Task<Result<String>> {
3370 let worktree = self.worktree.read(cx).as_local().unwrap();
3371 let abs_path = worktree.absolutize(&self.path);
3372 let fs = worktree.fs.clone();
3373 cx.background_spawn(async move { fs.load(&abs_path?).await })
3374 }
3375
3376 fn load_bytes(&self, cx: &App) -> Task<Result<Vec<u8>>> {
3377 let worktree = self.worktree.read(cx).as_local().unwrap();
3378 let abs_path = worktree.absolutize(&self.path);
3379 let fs = worktree.fs.clone();
3380 cx.background_spawn(async move { fs.load_bytes(&abs_path?).await })
3381 }
3382}
3383
3384impl File {
3385 pub fn for_entry(entry: Entry, worktree: Entity<Worktree>) -> Arc<Self> {
3386 Arc::new(Self {
3387 worktree,
3388 path: entry.path.clone(),
3389 disk_state: if let Some(mtime) = entry.mtime {
3390 DiskState::Present { mtime }
3391 } else {
3392 DiskState::New
3393 },
3394 entry_id: Some(entry.id),
3395 is_local: true,
3396 is_private: entry.is_private,
3397 })
3398 }
3399
3400 pub fn from_proto(
3401 proto: rpc::proto::File,
3402 worktree: Entity<Worktree>,
3403 cx: &App,
3404 ) -> Result<Self> {
3405 let worktree_id = worktree
3406 .read(cx)
3407 .as_remote()
3408 .ok_or_else(|| anyhow!("not remote"))?
3409 .id();
3410
3411 if worktree_id.to_proto() != proto.worktree_id {
3412 return Err(anyhow!("worktree id does not match file"));
3413 }
3414
3415 let disk_state = if proto.is_deleted {
3416 DiskState::Deleted
3417 } else {
3418 if let Some(mtime) = proto.mtime.map(&Into::into) {
3419 DiskState::Present { mtime }
3420 } else {
3421 DiskState::New
3422 }
3423 };
3424
3425 Ok(Self {
3426 worktree,
3427 path: Arc::<Path>::from_proto(proto.path),
3428 disk_state,
3429 entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
3430 is_local: false,
3431 is_private: false,
3432 })
3433 }
3434
3435 pub fn from_dyn(file: Option<&Arc<dyn language::File>>) -> Option<&Self> {
3436 file.and_then(|f| {
3437 let f: &dyn language::File = f.borrow();
3438 let f: &dyn Any = f;
3439 f.downcast_ref()
3440 })
3441 }
3442
3443 pub fn worktree_id(&self, cx: &App) -> WorktreeId {
3444 self.worktree.read(cx).id()
3445 }
3446
3447 pub fn project_entry_id(&self, _: &App) -> Option<ProjectEntryId> {
3448 match self.disk_state {
3449 DiskState::Deleted => None,
3450 _ => self.entry_id,
3451 }
3452 }
3453}
3454
3455#[derive(Clone, Debug, PartialEq, Eq)]
3456pub struct Entry {
3457 pub id: ProjectEntryId,
3458 pub kind: EntryKind,
3459 pub path: Arc<Path>,
3460 pub inode: u64,
3461 pub mtime: Option<MTime>,
3462
3463 pub canonical_path: Option<Arc<Path>>,
3464 /// Whether this entry is ignored by Git.
3465 ///
3466 /// We only scan ignored entries once the directory is expanded and
3467 /// exclude them from searches.
3468 pub is_ignored: bool,
3469
3470 /// Whether this entry is always included in searches.
3471 ///
3472 /// This is used for entries that are always included in searches, even
3473 /// if they are ignored by git. Overridden by file_scan_exclusions.
3474 pub is_always_included: bool,
3475
3476 /// Whether this entry's canonical path is outside of the worktree.
3477 /// This means the entry is only accessible from the worktree root via a
3478 /// symlink.
3479 ///
3480 /// We only scan entries outside of the worktree once the symlinked
3481 /// directory is expanded. External entries are treated like gitignored
3482 /// entries in that they are not included in searches.
3483 pub is_external: bool,
3484
3485 /// Whether this entry is considered to be a `.env` file.
3486 pub is_private: bool,
3487 /// The entry's size on disk, in bytes.
3488 pub size: u64,
3489 pub char_bag: CharBag,
3490 pub is_fifo: bool,
3491}
3492
3493#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3494pub enum EntryKind {
3495 UnloadedDir,
3496 PendingDir,
3497 Dir,
3498 File,
3499}
3500
3501#[derive(Clone, Copy, Debug, PartialEq)]
3502pub enum PathChange {
3503 /// A filesystem entry was was created.
3504 Added,
3505 /// A filesystem entry was removed.
3506 Removed,
3507 /// A filesystem entry was updated.
3508 Updated,
3509 /// A filesystem entry was either updated or added. We don't know
3510 /// whether or not it already existed, because the path had not
3511 /// been loaded before the event.
3512 AddedOrUpdated,
3513 /// A filesystem entry was found during the initial scan of the worktree.
3514 Loaded,
3515}
3516
3517#[derive(Clone, Debug, PartialEq, Eq)]
3518pub struct UpdatedGitRepository {
3519 /// ID of the repository's working directory.
3520 ///
3521 /// For a repo that's above the worktree root, this is the ID of the worktree root, and hence not unique.
3522 /// It's included here to aid the GitStore in detecting when a repository's working directory is renamed.
3523 pub work_directory_id: ProjectEntryId,
3524 pub old_work_directory_abs_path: Option<Arc<Path>>,
3525 pub new_work_directory_abs_path: Option<Arc<Path>>,
3526 /// For a normal git repository checkout, the absolute path to the .git directory.
3527 /// For a worktree, the absolute path to the worktree's subdirectory inside the .git directory.
3528 pub dot_git_abs_path: Option<Arc<Path>>,
3529 pub repository_dir_abs_path: Option<Arc<Path>>,
3530 pub common_dir_abs_path: Option<Arc<Path>>,
3531}
3532
3533pub type UpdatedEntriesSet = Arc<[(Arc<Path>, ProjectEntryId, PathChange)]>;
3534pub type UpdatedGitRepositoriesSet = Arc<[UpdatedGitRepository]>;
3535
3536#[derive(Clone, Debug)]
3537pub struct PathProgress<'a> {
3538 pub max_path: &'a Path,
3539}
3540
3541#[derive(Clone, Debug)]
3542pub struct PathSummary<S> {
3543 pub max_path: Arc<Path>,
3544 pub item_summary: S,
3545}
3546
3547impl<S: Summary> Summary for PathSummary<S> {
3548 type Context = S::Context;
3549
3550 fn zero(cx: &Self::Context) -> Self {
3551 Self {
3552 max_path: Path::new("").into(),
3553 item_summary: S::zero(cx),
3554 }
3555 }
3556
3557 fn add_summary(&mut self, rhs: &Self, cx: &Self::Context) {
3558 self.max_path = rhs.max_path.clone();
3559 self.item_summary.add_summary(&rhs.item_summary, cx);
3560 }
3561}
3562
3563impl<'a, S: Summary> sum_tree::Dimension<'a, PathSummary<S>> for PathProgress<'a> {
3564 fn zero(_: &<PathSummary<S> as Summary>::Context) -> Self {
3565 Self {
3566 max_path: Path::new(""),
3567 }
3568 }
3569
3570 fn add_summary(
3571 &mut self,
3572 summary: &'a PathSummary<S>,
3573 _: &<PathSummary<S> as Summary>::Context,
3574 ) {
3575 self.max_path = summary.max_path.as_ref()
3576 }
3577}
3578
3579impl<'a> sum_tree::Dimension<'a, PathSummary<GitSummary>> for GitSummary {
3580 fn zero(_cx: &()) -> Self {
3581 Default::default()
3582 }
3583
3584 fn add_summary(&mut self, summary: &'a PathSummary<GitSummary>, _: &()) {
3585 *self += summary.item_summary
3586 }
3587}
3588
3589impl<'a> sum_tree::SeekTarget<'a, PathSummary<GitSummary>, (TraversalProgress<'a>, GitSummary)>
3590 for PathTarget<'_>
3591{
3592 fn cmp(&self, cursor_location: &(TraversalProgress<'a>, GitSummary), _: &()) -> Ordering {
3593 self.cmp_path(&cursor_location.0.max_path)
3594 }
3595}
3596
3597impl<'a, S: Summary> sum_tree::Dimension<'a, PathSummary<S>> for PathKey {
3598 fn zero(_: &S::Context) -> Self {
3599 Default::default()
3600 }
3601
3602 fn add_summary(&mut self, summary: &'a PathSummary<S>, _: &S::Context) {
3603 self.0 = summary.max_path.clone();
3604 }
3605}
3606
3607impl<'a, S: Summary> sum_tree::Dimension<'a, PathSummary<S>> for TraversalProgress<'a> {
3608 fn zero(_cx: &S::Context) -> Self {
3609 Default::default()
3610 }
3611
3612 fn add_summary(&mut self, summary: &'a PathSummary<S>, _: &S::Context) {
3613 self.max_path = summary.max_path.as_ref();
3614 }
3615}
3616
3617impl Entry {
3618 fn new(
3619 path: Arc<Path>,
3620 metadata: &fs::Metadata,
3621 next_entry_id: &AtomicUsize,
3622 root_char_bag: CharBag,
3623 canonical_path: Option<Arc<Path>>,
3624 ) -> Self {
3625 let char_bag = char_bag_for_path(root_char_bag, &path);
3626 Self {
3627 id: ProjectEntryId::new(next_entry_id),
3628 kind: if metadata.is_dir {
3629 EntryKind::PendingDir
3630 } else {
3631 EntryKind::File
3632 },
3633 path,
3634 inode: metadata.inode,
3635 mtime: Some(metadata.mtime),
3636 size: metadata.len,
3637 canonical_path,
3638 is_ignored: false,
3639 is_always_included: false,
3640 is_external: false,
3641 is_private: false,
3642 char_bag,
3643 is_fifo: metadata.is_fifo,
3644 }
3645 }
3646
3647 pub fn is_created(&self) -> bool {
3648 self.mtime.is_some()
3649 }
3650
3651 pub fn is_dir(&self) -> bool {
3652 self.kind.is_dir()
3653 }
3654
3655 pub fn is_file(&self) -> bool {
3656 self.kind.is_file()
3657 }
3658}
3659
3660impl EntryKind {
3661 pub fn is_dir(&self) -> bool {
3662 matches!(
3663 self,
3664 EntryKind::Dir | EntryKind::PendingDir | EntryKind::UnloadedDir
3665 )
3666 }
3667
3668 pub fn is_unloaded(&self) -> bool {
3669 matches!(self, EntryKind::UnloadedDir)
3670 }
3671
3672 pub fn is_file(&self) -> bool {
3673 matches!(self, EntryKind::File)
3674 }
3675}
3676
3677impl sum_tree::Item for Entry {
3678 type Summary = EntrySummary;
3679
3680 fn summary(&self, _cx: &()) -> Self::Summary {
3681 let non_ignored_count = if (self.is_ignored || self.is_external) && !self.is_always_included
3682 {
3683 0
3684 } else {
3685 1
3686 };
3687 let file_count;
3688 let non_ignored_file_count;
3689 if self.is_file() {
3690 file_count = 1;
3691 non_ignored_file_count = non_ignored_count;
3692 } else {
3693 file_count = 0;
3694 non_ignored_file_count = 0;
3695 }
3696
3697 EntrySummary {
3698 max_path: self.path.clone(),
3699 count: 1,
3700 non_ignored_count,
3701 file_count,
3702 non_ignored_file_count,
3703 }
3704 }
3705}
3706
3707impl sum_tree::KeyedItem for Entry {
3708 type Key = PathKey;
3709
3710 fn key(&self) -> Self::Key {
3711 PathKey(self.path.clone())
3712 }
3713}
3714
3715#[derive(Clone, Debug)]
3716pub struct EntrySummary {
3717 max_path: Arc<Path>,
3718 count: usize,
3719 non_ignored_count: usize,
3720 file_count: usize,
3721 non_ignored_file_count: usize,
3722}
3723
3724impl Default for EntrySummary {
3725 fn default() -> Self {
3726 Self {
3727 max_path: Arc::from(Path::new("")),
3728 count: 0,
3729 non_ignored_count: 0,
3730 file_count: 0,
3731 non_ignored_file_count: 0,
3732 }
3733 }
3734}
3735
3736impl sum_tree::Summary for EntrySummary {
3737 type Context = ();
3738
3739 fn zero(_cx: &()) -> Self {
3740 Default::default()
3741 }
3742
3743 fn add_summary(&mut self, rhs: &Self, _: &()) {
3744 self.max_path = rhs.max_path.clone();
3745 self.count += rhs.count;
3746 self.non_ignored_count += rhs.non_ignored_count;
3747 self.file_count += rhs.file_count;
3748 self.non_ignored_file_count += rhs.non_ignored_file_count;
3749 }
3750}
3751
3752#[derive(Clone, Debug)]
3753struct PathEntry {
3754 id: ProjectEntryId,
3755 path: Arc<Path>,
3756 is_ignored: bool,
3757 scan_id: usize,
3758}
3759
3760impl sum_tree::Item for PathEntry {
3761 type Summary = PathEntrySummary;
3762
3763 fn summary(&self, _cx: &()) -> Self::Summary {
3764 PathEntrySummary { max_id: self.id }
3765 }
3766}
3767
3768impl sum_tree::KeyedItem for PathEntry {
3769 type Key = ProjectEntryId;
3770
3771 fn key(&self) -> Self::Key {
3772 self.id
3773 }
3774}
3775
3776#[derive(Clone, Debug, Default)]
3777struct PathEntrySummary {
3778 max_id: ProjectEntryId,
3779}
3780
3781impl sum_tree::Summary for PathEntrySummary {
3782 type Context = ();
3783
3784 fn zero(_cx: &Self::Context) -> Self {
3785 Default::default()
3786 }
3787
3788 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
3789 self.max_id = summary.max_id;
3790 }
3791}
3792
3793impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
3794 fn zero(_cx: &()) -> Self {
3795 Default::default()
3796 }
3797
3798 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
3799 *self = summary.max_id;
3800 }
3801}
3802
3803#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
3804pub struct PathKey(pub Arc<Path>);
3805
3806impl Default for PathKey {
3807 fn default() -> Self {
3808 Self(Path::new("").into())
3809 }
3810}
3811
3812impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
3813 fn zero(_cx: &()) -> Self {
3814 Default::default()
3815 }
3816
3817 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
3818 self.0 = summary.max_path.clone();
3819 }
3820}
3821
3822struct BackgroundScanner {
3823 state: Mutex<BackgroundScannerState>,
3824 fs: Arc<dyn Fs>,
3825 fs_case_sensitive: bool,
3826 status_updates_tx: UnboundedSender<ScanState>,
3827 executor: BackgroundExecutor,
3828 scan_requests_rx: channel::Receiver<ScanRequest>,
3829 path_prefixes_to_scan_rx: channel::Receiver<PathPrefixScanRequest>,
3830 next_entry_id: Arc<AtomicUsize>,
3831 phase: BackgroundScannerPhase,
3832 watcher: Arc<dyn Watcher>,
3833 settings: WorktreeSettings,
3834 share_private_files: bool,
3835}
3836
3837#[derive(Copy, Clone, PartialEq)]
3838enum BackgroundScannerPhase {
3839 InitialScan,
3840 EventsReceivedDuringInitialScan,
3841 Events,
3842}
3843
3844impl BackgroundScanner {
3845 async fn run(&mut self, mut fs_events_rx: Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>) {
3846 // If the worktree root does not contain a git repository, then find
3847 // the git repository in an ancestor directory. Find any gitignore files
3848 // in ancestor directories.
3849 let root_abs_path = self.state.lock().snapshot.abs_path.clone();
3850 let (ignores, repo) = discover_ancestor_git_repo(self.fs.clone(), &root_abs_path).await;
3851 self.state
3852 .lock()
3853 .snapshot
3854 .ignores_by_parent_abs_path
3855 .extend(ignores);
3856 let containing_git_repository = repo.and_then(|(ancestor_dot_git, work_directory)| {
3857 self.state.lock().insert_git_repository_for_path(
3858 work_directory,
3859 ancestor_dot_git.as_path().into(),
3860 self.fs.as_ref(),
3861 self.watcher.as_ref(),
3862 )?;
3863 Some(ancestor_dot_git)
3864 });
3865
3866 log::trace!("containing git repository: {containing_git_repository:?}");
3867
3868 let (scan_job_tx, scan_job_rx) = channel::unbounded();
3869 {
3870 let mut state = self.state.lock();
3871 state.snapshot.scan_id += 1;
3872 if let Some(mut root_entry) = state.snapshot.root_entry().cloned() {
3873 let ignore_stack = state
3874 .snapshot
3875 .ignore_stack_for_abs_path(root_abs_path.as_path(), true);
3876 if ignore_stack.is_abs_path_ignored(root_abs_path.as_path(), true) {
3877 root_entry.is_ignored = true;
3878 state.insert_entry(root_entry.clone(), self.fs.as_ref(), self.watcher.as_ref());
3879 }
3880 state.enqueue_scan_dir(root_abs_path.into(), &root_entry, &scan_job_tx);
3881 }
3882 };
3883
3884 // Perform an initial scan of the directory.
3885 drop(scan_job_tx);
3886 self.scan_dirs(true, scan_job_rx).await;
3887 {
3888 let mut state = self.state.lock();
3889 state.snapshot.completed_scan_id = state.snapshot.scan_id;
3890 }
3891
3892 self.send_status_update(false, SmallVec::new());
3893
3894 // Process any any FS events that occurred while performing the initial scan.
3895 // For these events, update events cannot be as precise, because we didn't
3896 // have the previous state loaded yet.
3897 self.phase = BackgroundScannerPhase::EventsReceivedDuringInitialScan;
3898 if let Poll::Ready(Some(mut paths)) = futures::poll!(fs_events_rx.next()) {
3899 while let Poll::Ready(Some(more_paths)) = futures::poll!(fs_events_rx.next()) {
3900 paths.extend(more_paths);
3901 }
3902 self.process_events(paths.into_iter().map(Into::into).collect())
3903 .await;
3904 }
3905 if let Some(abs_path) = containing_git_repository {
3906 self.process_events(vec![abs_path]).await;
3907 }
3908
3909 // Continue processing events until the worktree is dropped.
3910 self.phase = BackgroundScannerPhase::Events;
3911
3912 loop {
3913 select_biased! {
3914 // Process any path refresh requests from the worktree. Prioritize
3915 // these before handling changes reported by the filesystem.
3916 request = self.next_scan_request().fuse() => {
3917 let Ok(request) = request else { break };
3918 if !self.process_scan_request(request, false).await {
3919 return;
3920 }
3921 }
3922
3923 path_prefix_request = self.path_prefixes_to_scan_rx.recv().fuse() => {
3924 let Ok(request) = path_prefix_request else { break };
3925 log::trace!("adding path prefix {:?}", request.path);
3926
3927 let did_scan = self.forcibly_load_paths(&[request.path.clone()]).await;
3928 if did_scan {
3929 let abs_path =
3930 {
3931 let mut state = self.state.lock();
3932 state.path_prefixes_to_scan.insert(request.path.clone());
3933 state.snapshot.abs_path.as_path().join(&request.path)
3934 };
3935
3936 if let Some(abs_path) = self.fs.canonicalize(&abs_path).await.log_err() {
3937 self.process_events(vec![abs_path]).await;
3938 }
3939 }
3940 self.send_status_update(false, request.done);
3941 }
3942
3943 paths = fs_events_rx.next().fuse() => {
3944 let Some(mut paths) = paths else { break };
3945 while let Poll::Ready(Some(more_paths)) = futures::poll!(fs_events_rx.next()) {
3946 paths.extend(more_paths);
3947 }
3948 self.process_events(paths.into_iter().map(Into::into).collect()).await;
3949 }
3950 }
3951 }
3952 }
3953
3954 async fn process_scan_request(&self, mut request: ScanRequest, scanning: bool) -> bool {
3955 log::debug!("rescanning paths {:?}", request.relative_paths);
3956
3957 request.relative_paths.sort_unstable();
3958 self.forcibly_load_paths(&request.relative_paths).await;
3959
3960 let root_path = self.state.lock().snapshot.abs_path.clone();
3961 let root_canonical_path = match self.fs.canonicalize(root_path.as_path()).await {
3962 Ok(path) => SanitizedPath::from(path),
3963 Err(err) => {
3964 log::error!("failed to canonicalize root path: {}", err);
3965 return true;
3966 }
3967 };
3968 let abs_paths = request
3969 .relative_paths
3970 .iter()
3971 .map(|path| {
3972 if path.file_name().is_some() {
3973 root_canonical_path.as_path().join(path).to_path_buf()
3974 } else {
3975 root_canonical_path.as_path().to_path_buf()
3976 }
3977 })
3978 .collect::<Vec<_>>();
3979
3980 {
3981 let mut state = self.state.lock();
3982 let is_idle = state.snapshot.completed_scan_id == state.snapshot.scan_id;
3983 state.snapshot.scan_id += 1;
3984 if is_idle {
3985 state.snapshot.completed_scan_id = state.snapshot.scan_id;
3986 }
3987 }
3988
3989 self.reload_entries_for_paths(
3990 root_path,
3991 root_canonical_path,
3992 &request.relative_paths,
3993 abs_paths,
3994 None,
3995 )
3996 .await;
3997
3998 self.send_status_update(scanning, request.done)
3999 }
4000
4001 async fn process_events(&self, mut abs_paths: Vec<PathBuf>) {
4002 let root_path = self.state.lock().snapshot.abs_path.clone();
4003 let root_canonical_path = match self.fs.canonicalize(root_path.as_path()).await {
4004 Ok(path) => SanitizedPath::from(path),
4005 Err(err) => {
4006 let new_path = self
4007 .state
4008 .lock()
4009 .snapshot
4010 .root_file_handle
4011 .clone()
4012 .and_then(|handle| handle.current_path(&self.fs).log_err())
4013 .map(SanitizedPath::from)
4014 .filter(|new_path| *new_path != root_path);
4015
4016 if let Some(new_path) = new_path.as_ref() {
4017 log::info!(
4018 "root renamed from {} to {}",
4019 root_path.as_path().display(),
4020 new_path.as_path().display()
4021 )
4022 } else {
4023 log::warn!("root path could not be canonicalized: {}", err);
4024 }
4025 self.status_updates_tx
4026 .unbounded_send(ScanState::RootUpdated { new_path })
4027 .ok();
4028 return;
4029 }
4030 };
4031
4032 // Certain directories may have FS changes, but do not lead to git data changes that Zed cares about.
4033 // Ignore these, to avoid Zed unnecessarily rescanning git metadata.
4034 let skipped_files_in_dot_git = HashSet::from_iter([*COMMIT_MESSAGE, *INDEX_LOCK]);
4035 let skipped_dirs_in_dot_git = [*FSMONITOR_DAEMON, *LFS_DIR];
4036
4037 let mut relative_paths = Vec::with_capacity(abs_paths.len());
4038 let mut dot_git_abs_paths = Vec::new();
4039 abs_paths.sort_unstable();
4040 abs_paths.dedup_by(|a, b| a.starts_with(b));
4041 abs_paths.retain(|abs_path| {
4042 let abs_path = SanitizedPath::from(abs_path);
4043
4044 let snapshot = &self.state.lock().snapshot;
4045 {
4046 let mut is_git_related = false;
4047
4048 let dot_git_paths = abs_path.as_path().ancestors().find_map(|ancestor| {
4049 if smol::block_on(is_git_dir(ancestor, self.fs.as_ref())) {
4050 let path_in_git_dir = abs_path.as_path().strip_prefix(ancestor).expect("stripping off the ancestor");
4051 Some((ancestor.to_owned(), path_in_git_dir.to_owned()))
4052 } else {
4053 None
4054 }
4055 });
4056
4057 if let Some((dot_git_abs_path, path_in_git_dir)) = dot_git_paths {
4058 if skipped_files_in_dot_git.contains(path_in_git_dir.as_os_str()) || skipped_dirs_in_dot_git.iter().any(|skipped_git_subdir| path_in_git_dir.starts_with(skipped_git_subdir)) {
4059 log::debug!("ignoring event {abs_path:?} as it's in the .git directory among skipped files or directories");
4060 return false;
4061 }
4062
4063 is_git_related = true;
4064 if !dot_git_abs_paths.contains(&dot_git_abs_path) {
4065 dot_git_abs_paths.push(dot_git_abs_path);
4066 }
4067 }
4068
4069 let relative_path: Arc<Path> =
4070 if let Ok(path) = abs_path.strip_prefix(&root_canonical_path) {
4071 path.into()
4072 } else {
4073 if is_git_related {
4074 log::debug!(
4075 "ignoring event {abs_path:?}, since it's in git dir outside of root path {root_canonical_path:?}",
4076 );
4077 } else {
4078 log::error!(
4079 "ignoring event {abs_path:?} outside of root path {root_canonical_path:?}",
4080 );
4081 }
4082 return false;
4083 };
4084
4085 if abs_path.0.file_name() == Some(*GITIGNORE) {
4086 for (_, repo) in snapshot.git_repositories.iter().filter(|(_, repo)| repo.directory_contains(&relative_path)) {
4087 if !dot_git_abs_paths.iter().any(|dot_git_abs_path| dot_git_abs_path == repo.common_dir_abs_path.as_ref()) {
4088 dot_git_abs_paths.push(repo.common_dir_abs_path.to_path_buf());
4089 }
4090 }
4091 }
4092
4093 let parent_dir_is_loaded = relative_path.parent().map_or(true, |parent| {
4094 snapshot
4095 .entry_for_path(parent)
4096 .map_or(false, |entry| entry.kind == EntryKind::Dir)
4097 });
4098 if !parent_dir_is_loaded {
4099 log::debug!("ignoring event {relative_path:?} within unloaded directory");
4100 return false;
4101 }
4102
4103 if self.settings.is_path_excluded(&relative_path) {
4104 if !is_git_related {
4105 log::debug!("ignoring FS event for excluded path {relative_path:?}");
4106 }
4107 return false;
4108 }
4109
4110 relative_paths.push(relative_path);
4111 true
4112 }
4113 });
4114
4115 if relative_paths.is_empty() && dot_git_abs_paths.is_empty() {
4116 return;
4117 }
4118
4119 self.state.lock().snapshot.scan_id += 1;
4120
4121 let (scan_job_tx, scan_job_rx) = channel::unbounded();
4122 log::debug!("received fs events {:?}", relative_paths);
4123 self.reload_entries_for_paths(
4124 root_path,
4125 root_canonical_path,
4126 &relative_paths,
4127 abs_paths,
4128 Some(scan_job_tx.clone()),
4129 )
4130 .await;
4131
4132 self.update_ignore_statuses(scan_job_tx).await;
4133 self.scan_dirs(false, scan_job_rx).await;
4134
4135 if !dot_git_abs_paths.is_empty() {
4136 self.update_git_repositories(dot_git_abs_paths);
4137 }
4138
4139 {
4140 let mut state = self.state.lock();
4141 state.snapshot.completed_scan_id = state.snapshot.scan_id;
4142 for (_, entry) in mem::take(&mut state.removed_entries) {
4143 state.scanned_dirs.remove(&entry.id);
4144 }
4145 }
4146 self.send_status_update(false, SmallVec::new());
4147 }
4148
4149 async fn forcibly_load_paths(&self, paths: &[Arc<Path>]) -> bool {
4150 let (scan_job_tx, scan_job_rx) = channel::unbounded();
4151 {
4152 let mut state = self.state.lock();
4153 let root_path = state.snapshot.abs_path.clone();
4154 for path in paths {
4155 for ancestor in path.ancestors() {
4156 if let Some(entry) = state.snapshot.entry_for_path(ancestor) {
4157 if entry.kind == EntryKind::UnloadedDir {
4158 let abs_path = root_path.as_path().join(ancestor);
4159 state.enqueue_scan_dir(abs_path.into(), entry, &scan_job_tx);
4160 state.paths_to_scan.insert(path.clone());
4161 break;
4162 }
4163 }
4164 }
4165 }
4166 drop(scan_job_tx);
4167 }
4168 while let Ok(job) = scan_job_rx.recv().await {
4169 self.scan_dir(&job).await.log_err();
4170 }
4171
4172 !mem::take(&mut self.state.lock().paths_to_scan).is_empty()
4173 }
4174
4175 async fn scan_dirs(
4176 &self,
4177 enable_progress_updates: bool,
4178 scan_jobs_rx: channel::Receiver<ScanJob>,
4179 ) {
4180 if self
4181 .status_updates_tx
4182 .unbounded_send(ScanState::Started)
4183 .is_err()
4184 {
4185 return;
4186 }
4187
4188 let progress_update_count = AtomicUsize::new(0);
4189 self.executor
4190 .scoped(|scope| {
4191 for _ in 0..self.executor.num_cpus() {
4192 scope.spawn(async {
4193 let mut last_progress_update_count = 0;
4194 let progress_update_timer = self.progress_timer(enable_progress_updates).fuse();
4195 futures::pin_mut!(progress_update_timer);
4196
4197 loop {
4198 select_biased! {
4199 // Process any path refresh requests before moving on to process
4200 // the scan queue, so that user operations are prioritized.
4201 request = self.next_scan_request().fuse() => {
4202 let Ok(request) = request else { break };
4203 if !self.process_scan_request(request, true).await {
4204 return;
4205 }
4206 }
4207
4208 // Send periodic progress updates to the worktree. Use an atomic counter
4209 // to ensure that only one of the workers sends a progress update after
4210 // the update interval elapses.
4211 _ = progress_update_timer => {
4212 match progress_update_count.compare_exchange(
4213 last_progress_update_count,
4214 last_progress_update_count + 1,
4215 SeqCst,
4216 SeqCst
4217 ) {
4218 Ok(_) => {
4219 last_progress_update_count += 1;
4220 self.send_status_update(true, SmallVec::new());
4221 }
4222 Err(count) => {
4223 last_progress_update_count = count;
4224 }
4225 }
4226 progress_update_timer.set(self.progress_timer(enable_progress_updates).fuse());
4227 }
4228
4229 // Recursively load directories from the file system.
4230 job = scan_jobs_rx.recv().fuse() => {
4231 let Ok(job) = job else { break };
4232 if let Err(err) = self.scan_dir(&job).await {
4233 if job.path.as_ref() != Path::new("") {
4234 log::error!("error scanning directory {:?}: {}", job.abs_path, err);
4235 }
4236 }
4237 }
4238 }
4239 }
4240 });
4241 }
4242 })
4243 .await;
4244 }
4245
4246 fn send_status_update(&self, scanning: bool, barrier: SmallVec<[barrier::Sender; 1]>) -> bool {
4247 let mut state = self.state.lock();
4248 if state.changed_paths.is_empty() && scanning {
4249 return true;
4250 }
4251
4252 let new_snapshot = state.snapshot.clone();
4253 let old_snapshot = mem::replace(&mut state.prev_snapshot, new_snapshot.snapshot.clone());
4254 let changes = build_diff(
4255 self.phase,
4256 &old_snapshot,
4257 &new_snapshot,
4258 &state.changed_paths,
4259 );
4260 state.changed_paths.clear();
4261
4262 self.status_updates_tx
4263 .unbounded_send(ScanState::Updated {
4264 snapshot: new_snapshot,
4265 changes,
4266 scanning,
4267 barrier,
4268 })
4269 .is_ok()
4270 }
4271
4272 async fn scan_dir(&self, job: &ScanJob) -> Result<()> {
4273 let root_abs_path;
4274 let root_char_bag;
4275 {
4276 let snapshot = &self.state.lock().snapshot;
4277 if self.settings.is_path_excluded(&job.path) {
4278 log::error!("skipping excluded directory {:?}", job.path);
4279 return Ok(());
4280 }
4281 log::trace!("scanning directory {:?}", job.path);
4282 root_abs_path = snapshot.abs_path().clone();
4283 root_char_bag = snapshot.root_char_bag;
4284 }
4285
4286 let next_entry_id = self.next_entry_id.clone();
4287 let mut ignore_stack = job.ignore_stack.clone();
4288 let mut new_ignore = None;
4289 let mut root_canonical_path = None;
4290 let mut new_entries: Vec<Entry> = Vec::new();
4291 let mut new_jobs: Vec<Option<ScanJob>> = Vec::new();
4292 let mut child_paths = self
4293 .fs
4294 .read_dir(&job.abs_path)
4295 .await?
4296 .filter_map(|entry| async {
4297 match entry {
4298 Ok(entry) => Some(entry),
4299 Err(error) => {
4300 log::error!("error processing entry {:?}", error);
4301 None
4302 }
4303 }
4304 })
4305 .collect::<Vec<_>>()
4306 .await;
4307
4308 // Ensure that .git and .gitignore are processed first.
4309 swap_to_front(&mut child_paths, *GITIGNORE);
4310 swap_to_front(&mut child_paths, *DOT_GIT);
4311
4312 for child_abs_path in child_paths {
4313 let child_abs_path: Arc<Path> = child_abs_path.into();
4314 let child_name = child_abs_path.file_name().unwrap();
4315 let child_path: Arc<Path> = job.path.join(child_name).into();
4316
4317 if child_name == *DOT_GIT {
4318 let mut state = self.state.lock();
4319 state.insert_git_repository(
4320 child_path.clone(),
4321 self.fs.as_ref(),
4322 self.watcher.as_ref(),
4323 );
4324 } else if child_name == *GITIGNORE {
4325 match build_gitignore(&child_abs_path, self.fs.as_ref()).await {
4326 Ok(ignore) => {
4327 let ignore = Arc::new(ignore);
4328 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
4329 new_ignore = Some(ignore);
4330 }
4331 Err(error) => {
4332 log::error!(
4333 "error loading .gitignore file {:?} - {:?}",
4334 child_name,
4335 error
4336 );
4337 }
4338 }
4339 }
4340
4341 if self.settings.is_path_excluded(&child_path) {
4342 log::debug!("skipping excluded child entry {child_path:?}");
4343 self.state.lock().remove_path(&child_path);
4344 continue;
4345 }
4346
4347 let child_metadata = match self.fs.metadata(&child_abs_path).await {
4348 Ok(Some(metadata)) => metadata,
4349 Ok(None) => continue,
4350 Err(err) => {
4351 log::error!("error processing {child_abs_path:?}: {err:?}");
4352 continue;
4353 }
4354 };
4355
4356 let mut child_entry = Entry::new(
4357 child_path.clone(),
4358 &child_metadata,
4359 &next_entry_id,
4360 root_char_bag,
4361 None,
4362 );
4363
4364 if job.is_external {
4365 child_entry.is_external = true;
4366 } else if child_metadata.is_symlink {
4367 let canonical_path = match self.fs.canonicalize(&child_abs_path).await {
4368 Ok(path) => path,
4369 Err(err) => {
4370 log::error!("error reading target of symlink {child_abs_path:?}: {err:#}",);
4371 continue;
4372 }
4373 };
4374
4375 // lazily canonicalize the root path in order to determine if
4376 // symlinks point outside of the worktree.
4377 let root_canonical_path = match &root_canonical_path {
4378 Some(path) => path,
4379 None => match self.fs.canonicalize(&root_abs_path).await {
4380 Ok(path) => root_canonical_path.insert(path),
4381 Err(err) => {
4382 log::error!("error canonicalizing root {:?}: {:?}", root_abs_path, err);
4383 continue;
4384 }
4385 },
4386 };
4387
4388 if !canonical_path.starts_with(root_canonical_path) {
4389 child_entry.is_external = true;
4390 }
4391
4392 child_entry.canonical_path = Some(canonical_path.into());
4393 }
4394
4395 if child_entry.is_dir() {
4396 child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true);
4397 child_entry.is_always_included = self.settings.is_path_always_included(&child_path);
4398
4399 // Avoid recursing until crash in the case of a recursive symlink
4400 if job.ancestor_inodes.contains(&child_entry.inode) {
4401 new_jobs.push(None);
4402 } else {
4403 let mut ancestor_inodes = job.ancestor_inodes.clone();
4404 ancestor_inodes.insert(child_entry.inode);
4405
4406 new_jobs.push(Some(ScanJob {
4407 abs_path: child_abs_path.clone(),
4408 path: child_path,
4409 is_external: child_entry.is_external,
4410 ignore_stack: if child_entry.is_ignored {
4411 IgnoreStack::all()
4412 } else {
4413 ignore_stack.clone()
4414 },
4415 ancestor_inodes,
4416 scan_queue: job.scan_queue.clone(),
4417 }));
4418 }
4419 } else {
4420 child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false);
4421 child_entry.is_always_included = self.settings.is_path_always_included(&child_path);
4422 }
4423
4424 {
4425 let relative_path = job.path.join(child_name);
4426 if self.is_path_private(&relative_path) {
4427 log::debug!("detected private file: {relative_path:?}");
4428 child_entry.is_private = true;
4429 }
4430 }
4431
4432 new_entries.push(child_entry);
4433 }
4434
4435 let mut state = self.state.lock();
4436
4437 // Identify any subdirectories that should not be scanned.
4438 let mut job_ix = 0;
4439 for entry in &mut new_entries {
4440 state.reuse_entry_id(entry);
4441 if entry.is_dir() {
4442 if state.should_scan_directory(entry) {
4443 job_ix += 1;
4444 } else {
4445 log::debug!("defer scanning directory {:?}", entry.path);
4446 entry.kind = EntryKind::UnloadedDir;
4447 new_jobs.remove(job_ix);
4448 }
4449 }
4450 if entry.is_always_included {
4451 state
4452 .snapshot
4453 .always_included_entries
4454 .push(entry.path.clone());
4455 }
4456 }
4457
4458 state.populate_dir(&job.path, new_entries, new_ignore);
4459 self.watcher.add(job.abs_path.as_ref()).log_err();
4460
4461 for new_job in new_jobs.into_iter().flatten() {
4462 job.scan_queue
4463 .try_send(new_job)
4464 .expect("channel is unbounded");
4465 }
4466
4467 Ok(())
4468 }
4469
4470 /// All list arguments should be sorted before calling this function
4471 async fn reload_entries_for_paths(
4472 &self,
4473 root_abs_path: SanitizedPath,
4474 root_canonical_path: SanitizedPath,
4475 relative_paths: &[Arc<Path>],
4476 abs_paths: Vec<PathBuf>,
4477 scan_queue_tx: Option<Sender<ScanJob>>,
4478 ) {
4479 // grab metadata for all requested paths
4480 let metadata = futures::future::join_all(
4481 abs_paths
4482 .iter()
4483 .map(|abs_path| async move {
4484 let metadata = self.fs.metadata(abs_path).await?;
4485 if let Some(metadata) = metadata {
4486 let canonical_path = self.fs.canonicalize(abs_path).await?;
4487
4488 // If we're on a case-insensitive filesystem (default on macOS), we want
4489 // to only ignore metadata for non-symlink files if their absolute-path matches
4490 // the canonical-path.
4491 // Because if not, this might be a case-only-renaming (`mv test.txt TEST.TXT`)
4492 // and we want to ignore the metadata for the old path (`test.txt`) so it's
4493 // treated as removed.
4494 if !self.fs_case_sensitive && !metadata.is_symlink {
4495 let canonical_file_name = canonical_path.file_name();
4496 let file_name = abs_path.file_name();
4497 if canonical_file_name != file_name {
4498 return Ok(None);
4499 }
4500 }
4501
4502 anyhow::Ok(Some((metadata, SanitizedPath::from(canonical_path))))
4503 } else {
4504 Ok(None)
4505 }
4506 })
4507 .collect::<Vec<_>>(),
4508 )
4509 .await;
4510
4511 let mut new_ancestor_repo = if relative_paths
4512 .iter()
4513 .any(|path| path.as_ref() == Path::new(""))
4514 {
4515 Some(discover_ancestor_git_repo(self.fs.clone(), &root_abs_path).await)
4516 } else {
4517 None
4518 };
4519
4520 let mut state = self.state.lock();
4521 let doing_recursive_update = scan_queue_tx.is_some();
4522
4523 // Remove any entries for paths that no longer exist or are being recursively
4524 // refreshed. Do this before adding any new entries, so that renames can be
4525 // detected regardless of the order of the paths.
4526 for (path, metadata) in relative_paths.iter().zip(metadata.iter()) {
4527 if matches!(metadata, Ok(None)) || doing_recursive_update {
4528 log::trace!("remove path {:?}", path);
4529 state.remove_path(path);
4530 }
4531 }
4532
4533 for (path, metadata) in relative_paths.iter().zip(metadata.into_iter()) {
4534 let abs_path: Arc<Path> = root_abs_path.as_path().join(path).into();
4535 match metadata {
4536 Ok(Some((metadata, canonical_path))) => {
4537 let ignore_stack = state
4538 .snapshot
4539 .ignore_stack_for_abs_path(&abs_path, metadata.is_dir);
4540 let is_external = !canonical_path.starts_with(&root_canonical_path);
4541 let mut fs_entry = Entry::new(
4542 path.clone(),
4543 &metadata,
4544 self.next_entry_id.as_ref(),
4545 state.snapshot.root_char_bag,
4546 if metadata.is_symlink {
4547 Some(canonical_path.as_path().to_path_buf().into())
4548 } else {
4549 None
4550 },
4551 );
4552
4553 let is_dir = fs_entry.is_dir();
4554 fs_entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, is_dir);
4555 fs_entry.is_external = is_external;
4556 fs_entry.is_private = self.is_path_private(path);
4557 fs_entry.is_always_included = self.settings.is_path_always_included(path);
4558
4559 if let (Some(scan_queue_tx), true) = (&scan_queue_tx, is_dir) {
4560 if state.should_scan_directory(&fs_entry)
4561 || (fs_entry.path.as_os_str().is_empty()
4562 && abs_path.file_name() == Some(*DOT_GIT))
4563 {
4564 state.enqueue_scan_dir(abs_path, &fs_entry, scan_queue_tx);
4565 } else {
4566 fs_entry.kind = EntryKind::UnloadedDir;
4567 }
4568 }
4569
4570 state.insert_entry(fs_entry.clone(), self.fs.as_ref(), self.watcher.as_ref());
4571
4572 if path.as_ref() == Path::new("") {
4573 if let Some((ignores, repo)) = new_ancestor_repo.take() {
4574 log::trace!("updating ancestor git repository");
4575 state.snapshot.ignores_by_parent_abs_path.extend(ignores);
4576 if let Some((ancestor_dot_git, work_directory)) = repo {
4577 state.insert_git_repository_for_path(
4578 work_directory,
4579 ancestor_dot_git.as_path().into(),
4580 self.fs.as_ref(),
4581 self.watcher.as_ref(),
4582 );
4583 }
4584 }
4585 }
4586 }
4587 Ok(None) => {
4588 self.remove_repo_path(path, &mut state.snapshot);
4589 }
4590 Err(err) => {
4591 log::error!("error reading file {abs_path:?} on event: {err:#}");
4592 }
4593 }
4594 }
4595
4596 util::extend_sorted(
4597 &mut state.changed_paths,
4598 relative_paths.iter().cloned(),
4599 usize::MAX,
4600 Ord::cmp,
4601 );
4602 }
4603
4604 fn remove_repo_path(&self, path: &Arc<Path>, snapshot: &mut LocalSnapshot) -> Option<()> {
4605 if !path
4606 .components()
4607 .any(|component| component.as_os_str() == *DOT_GIT)
4608 {
4609 if let Some(local_repo) = snapshot.local_repo_for_work_directory_path(path) {
4610 let id = local_repo.work_directory_id;
4611 log::debug!("remove repo path: {:?}", path);
4612 snapshot.git_repositories.remove(&id);
4613 return Some(());
4614 }
4615 }
4616
4617 Some(())
4618 }
4619
4620 async fn update_ignore_statuses(&self, scan_job_tx: Sender<ScanJob>) {
4621 let mut ignores_to_update = Vec::new();
4622 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
4623 let prev_snapshot;
4624 {
4625 let snapshot = &mut self.state.lock().snapshot;
4626 let abs_path = snapshot.abs_path.clone();
4627 snapshot
4628 .ignores_by_parent_abs_path
4629 .retain(|parent_abs_path, (_, needs_update)| {
4630 if let Ok(parent_path) = parent_abs_path.strip_prefix(abs_path.as_path()) {
4631 if *needs_update {
4632 *needs_update = false;
4633 if snapshot.snapshot.entry_for_path(parent_path).is_some() {
4634 ignores_to_update.push(parent_abs_path.clone());
4635 }
4636 }
4637
4638 let ignore_path = parent_path.join(*GITIGNORE);
4639 if snapshot.snapshot.entry_for_path(ignore_path).is_none() {
4640 return false;
4641 }
4642 }
4643 true
4644 });
4645
4646 ignores_to_update.sort_unstable();
4647 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
4648 while let Some(parent_abs_path) = ignores_to_update.next() {
4649 while ignores_to_update
4650 .peek()
4651 .map_or(false, |p| p.starts_with(&parent_abs_path))
4652 {
4653 ignores_to_update.next().unwrap();
4654 }
4655
4656 let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true);
4657 ignore_queue_tx
4658 .send_blocking(UpdateIgnoreStatusJob {
4659 abs_path: parent_abs_path,
4660 ignore_stack,
4661 ignore_queue: ignore_queue_tx.clone(),
4662 scan_queue: scan_job_tx.clone(),
4663 })
4664 .unwrap();
4665 }
4666
4667 prev_snapshot = snapshot.clone();
4668 }
4669 drop(ignore_queue_tx);
4670
4671 self.executor
4672 .scoped(|scope| {
4673 for _ in 0..self.executor.num_cpus() {
4674 scope.spawn(async {
4675 loop {
4676 select_biased! {
4677 // Process any path refresh requests before moving on to process
4678 // the queue of ignore statuses.
4679 request = self.next_scan_request().fuse() => {
4680 let Ok(request) = request else { break };
4681 if !self.process_scan_request(request, true).await {
4682 return;
4683 }
4684 }
4685
4686 // Recursively process directories whose ignores have changed.
4687 job = ignore_queue_rx.recv().fuse() => {
4688 let Ok(job) = job else { break };
4689 self.update_ignore_status(job, &prev_snapshot).await;
4690 }
4691 }
4692 }
4693 });
4694 }
4695 })
4696 .await;
4697 }
4698
4699 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
4700 log::trace!("update ignore status {:?}", job.abs_path);
4701
4702 let mut ignore_stack = job.ignore_stack;
4703 if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) {
4704 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
4705 }
4706
4707 let mut entries_by_id_edits = Vec::new();
4708 let mut entries_by_path_edits = Vec::new();
4709 let path = job
4710 .abs_path
4711 .strip_prefix(snapshot.abs_path.as_path())
4712 .unwrap();
4713
4714 for mut entry in snapshot.child_entries(path).cloned() {
4715 let was_ignored = entry.is_ignored;
4716 let abs_path: Arc<Path> = snapshot.abs_path().join(&entry.path).into();
4717 entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir());
4718
4719 if entry.is_dir() {
4720 let child_ignore_stack = if entry.is_ignored {
4721 IgnoreStack::all()
4722 } else {
4723 ignore_stack.clone()
4724 };
4725
4726 // Scan any directories that were previously ignored and weren't previously scanned.
4727 if was_ignored && !entry.is_ignored && entry.kind.is_unloaded() {
4728 let state = self.state.lock();
4729 if state.should_scan_directory(&entry) {
4730 state.enqueue_scan_dir(abs_path.clone(), &entry, &job.scan_queue);
4731 }
4732 }
4733
4734 job.ignore_queue
4735 .send(UpdateIgnoreStatusJob {
4736 abs_path: abs_path.clone(),
4737 ignore_stack: child_ignore_stack,
4738 ignore_queue: job.ignore_queue.clone(),
4739 scan_queue: job.scan_queue.clone(),
4740 })
4741 .await
4742 .unwrap();
4743 }
4744
4745 if entry.is_ignored != was_ignored {
4746 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
4747 path_entry.scan_id = snapshot.scan_id;
4748 path_entry.is_ignored = entry.is_ignored;
4749 entries_by_id_edits.push(Edit::Insert(path_entry));
4750 entries_by_path_edits.push(Edit::Insert(entry));
4751 }
4752 }
4753
4754 let state = &mut self.state.lock();
4755 for edit in &entries_by_path_edits {
4756 if let Edit::Insert(entry) = edit {
4757 if let Err(ix) = state.changed_paths.binary_search(&entry.path) {
4758 state.changed_paths.insert(ix, entry.path.clone());
4759 }
4760 }
4761 }
4762
4763 state
4764 .snapshot
4765 .entries_by_path
4766 .edit(entries_by_path_edits, &());
4767 state.snapshot.entries_by_id.edit(entries_by_id_edits, &());
4768 }
4769
4770 fn update_git_repositories(&self, dot_git_paths: Vec<PathBuf>) {
4771 log::trace!("reloading repositories: {dot_git_paths:?}");
4772 let mut state = self.state.lock();
4773 let scan_id = state.snapshot.scan_id;
4774 for dot_git_dir in dot_git_paths {
4775 let existing_repository_entry =
4776 state
4777 .snapshot
4778 .git_repositories
4779 .iter()
4780 .find_map(|(_, repo)| {
4781 if repo.common_dir_abs_path.as_ref() == &dot_git_dir
4782 || repo.repository_dir_abs_path.as_ref() == &dot_git_dir
4783 {
4784 Some(repo.clone())
4785 } else {
4786 None
4787 }
4788 });
4789
4790 match existing_repository_entry {
4791 None => {
4792 let Ok(relative) = dot_git_dir.strip_prefix(state.snapshot.abs_path()) else {
4793 return;
4794 };
4795 state.insert_git_repository(
4796 relative.into(),
4797 self.fs.as_ref(),
4798 self.watcher.as_ref(),
4799 );
4800 }
4801 Some(local_repository) => {
4802 state.snapshot.git_repositories.update(
4803 &local_repository.work_directory_id,
4804 |entry| {
4805 entry.git_dir_scan_id = scan_id;
4806 },
4807 );
4808 }
4809 };
4810 }
4811
4812 // Remove any git repositories whose .git entry no longer exists.
4813 let snapshot = &mut state.snapshot;
4814 let mut ids_to_preserve = HashSet::default();
4815 for (&work_directory_id, entry) in snapshot.git_repositories.iter() {
4816 let exists_in_snapshot = snapshot
4817 .entry_for_id(work_directory_id)
4818 .map_or(false, |entry| {
4819 snapshot.entry_for_path(entry.path.join(*DOT_GIT)).is_some()
4820 });
4821
4822 if exists_in_snapshot
4823 || matches!(
4824 smol::block_on(self.fs.metadata(&entry.common_dir_abs_path)),
4825 Ok(Some(_))
4826 )
4827 {
4828 ids_to_preserve.insert(work_directory_id);
4829 }
4830 }
4831
4832 snapshot
4833 .git_repositories
4834 .retain(|work_directory_id, _| ids_to_preserve.contains(work_directory_id));
4835 }
4836
4837 async fn progress_timer(&self, running: bool) {
4838 if !running {
4839 return futures::future::pending().await;
4840 }
4841
4842 #[cfg(any(test, feature = "test-support"))]
4843 if self.fs.is_fake() {
4844 return self.executor.simulate_random_delay().await;
4845 }
4846
4847 smol::Timer::after(FS_WATCH_LATENCY).await;
4848 }
4849
4850 fn is_path_private(&self, path: &Path) -> bool {
4851 !self.share_private_files && self.settings.is_path_private(path)
4852 }
4853
4854 async fn next_scan_request(&self) -> Result<ScanRequest> {
4855 let mut request = self.scan_requests_rx.recv().await?;
4856 while let Ok(next_request) = self.scan_requests_rx.try_recv() {
4857 request.relative_paths.extend(next_request.relative_paths);
4858 request.done.extend(next_request.done);
4859 }
4860 Ok(request)
4861 }
4862}
4863
4864async fn discover_ancestor_git_repo(
4865 fs: Arc<dyn Fs>,
4866 root_abs_path: &SanitizedPath,
4867) -> (
4868 HashMap<Arc<Path>, (Arc<Gitignore>, bool)>,
4869 Option<(PathBuf, WorkDirectory)>,
4870) {
4871 let mut ignores = HashMap::default();
4872 for (index, ancestor) in root_abs_path.as_path().ancestors().enumerate() {
4873 if index != 0 {
4874 if Some(ancestor) == fs.home_dir().as_deref() {
4875 // Unless $HOME is itself the worktree root, don't consider it as a
4876 // containing git repository---expensive and likely unwanted.
4877 break;
4878 } else if let Ok(ignore) =
4879 build_gitignore(&ancestor.join(*GITIGNORE), fs.as_ref()).await
4880 {
4881 ignores.insert(ancestor.into(), (ignore.into(), false));
4882 }
4883 }
4884
4885 let ancestor_dot_git = ancestor.join(*DOT_GIT);
4886 log::trace!("considering ancestor: {ancestor_dot_git:?}");
4887 // Check whether the directory or file called `.git` exists (in the
4888 // case of worktrees it's a file.)
4889 if fs
4890 .metadata(&ancestor_dot_git)
4891 .await
4892 .is_ok_and(|metadata| metadata.is_some())
4893 {
4894 if index != 0 {
4895 // We canonicalize, since the FS events use the canonicalized path.
4896 if let Some(ancestor_dot_git) = fs.canonicalize(&ancestor_dot_git).await.log_err() {
4897 let location_in_repo = root_abs_path
4898 .as_path()
4899 .strip_prefix(ancestor)
4900 .unwrap()
4901 .into();
4902 log::info!("inserting parent git repo for this worktree: {location_in_repo:?}");
4903 // We associate the external git repo with our root folder and
4904 // also mark where in the git repo the root folder is located.
4905 return (
4906 ignores,
4907 Some((
4908 ancestor_dot_git,
4909 WorkDirectory::AboveProject {
4910 absolute_path: ancestor.into(),
4911 location_in_repo,
4912 },
4913 )),
4914 );
4915 };
4916 }
4917
4918 // Reached root of git repository.
4919 break;
4920 }
4921 }
4922
4923 (ignores, None)
4924}
4925
4926fn build_diff(
4927 phase: BackgroundScannerPhase,
4928 old_snapshot: &Snapshot,
4929 new_snapshot: &Snapshot,
4930 event_paths: &[Arc<Path>],
4931) -> UpdatedEntriesSet {
4932 use BackgroundScannerPhase::*;
4933 use PathChange::{Added, AddedOrUpdated, Loaded, Removed, Updated};
4934
4935 // Identify which paths have changed. Use the known set of changed
4936 // parent paths to optimize the search.
4937 let mut changes = Vec::new();
4938 let mut old_paths = old_snapshot.entries_by_path.cursor::<PathKey>(&());
4939 let mut new_paths = new_snapshot.entries_by_path.cursor::<PathKey>(&());
4940 let mut last_newly_loaded_dir_path = None;
4941 old_paths.next(&());
4942 new_paths.next(&());
4943 for path in event_paths {
4944 let path = PathKey(path.clone());
4945 if old_paths.item().map_or(false, |e| e.path < path.0) {
4946 old_paths.seek_forward(&path, Bias::Left, &());
4947 }
4948 if new_paths.item().map_or(false, |e| e.path < path.0) {
4949 new_paths.seek_forward(&path, Bias::Left, &());
4950 }
4951 loop {
4952 match (old_paths.item(), new_paths.item()) {
4953 (Some(old_entry), Some(new_entry)) => {
4954 if old_entry.path > path.0
4955 && new_entry.path > path.0
4956 && !old_entry.path.starts_with(&path.0)
4957 && !new_entry.path.starts_with(&path.0)
4958 {
4959 break;
4960 }
4961
4962 match Ord::cmp(&old_entry.path, &new_entry.path) {
4963 Ordering::Less => {
4964 changes.push((old_entry.path.clone(), old_entry.id, Removed));
4965 old_paths.next(&());
4966 }
4967 Ordering::Equal => {
4968 if phase == EventsReceivedDuringInitialScan {
4969 if old_entry.id != new_entry.id {
4970 changes.push((old_entry.path.clone(), old_entry.id, Removed));
4971 }
4972 // If the worktree was not fully initialized when this event was generated,
4973 // we can't know whether this entry was added during the scan or whether
4974 // it was merely updated.
4975 changes.push((
4976 new_entry.path.clone(),
4977 new_entry.id,
4978 AddedOrUpdated,
4979 ));
4980 } else if old_entry.id != new_entry.id {
4981 changes.push((old_entry.path.clone(), old_entry.id, Removed));
4982 changes.push((new_entry.path.clone(), new_entry.id, Added));
4983 } else if old_entry != new_entry {
4984 if old_entry.kind.is_unloaded() {
4985 last_newly_loaded_dir_path = Some(&new_entry.path);
4986 changes.push((new_entry.path.clone(), new_entry.id, Loaded));
4987 } else {
4988 changes.push((new_entry.path.clone(), new_entry.id, Updated));
4989 }
4990 }
4991 old_paths.next(&());
4992 new_paths.next(&());
4993 }
4994 Ordering::Greater => {
4995 let is_newly_loaded = phase == InitialScan
4996 || last_newly_loaded_dir_path
4997 .as_ref()
4998 .map_or(false, |dir| new_entry.path.starts_with(dir));
4999 changes.push((
5000 new_entry.path.clone(),
5001 new_entry.id,
5002 if is_newly_loaded { Loaded } else { Added },
5003 ));
5004 new_paths.next(&());
5005 }
5006 }
5007 }
5008 (Some(old_entry), None) => {
5009 changes.push((old_entry.path.clone(), old_entry.id, Removed));
5010 old_paths.next(&());
5011 }
5012 (None, Some(new_entry)) => {
5013 let is_newly_loaded = phase == InitialScan
5014 || last_newly_loaded_dir_path
5015 .as_ref()
5016 .map_or(false, |dir| new_entry.path.starts_with(dir));
5017 changes.push((
5018 new_entry.path.clone(),
5019 new_entry.id,
5020 if is_newly_loaded { Loaded } else { Added },
5021 ));
5022 new_paths.next(&());
5023 }
5024 (None, None) => break,
5025 }
5026 }
5027 }
5028
5029 changes.into()
5030}
5031
5032fn swap_to_front(child_paths: &mut Vec<PathBuf>, file: &OsStr) {
5033 let position = child_paths
5034 .iter()
5035 .position(|path| path.file_name().unwrap() == file);
5036 if let Some(position) = position {
5037 let temp = child_paths.remove(position);
5038 child_paths.insert(0, temp);
5039 }
5040}
5041
5042fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
5043 let mut result = root_char_bag;
5044 result.extend(
5045 path.to_string_lossy()
5046 .chars()
5047 .map(|c| c.to_ascii_lowercase()),
5048 );
5049 result
5050}
5051
5052#[derive(Debug)]
5053struct ScanJob {
5054 abs_path: Arc<Path>,
5055 path: Arc<Path>,
5056 ignore_stack: Arc<IgnoreStack>,
5057 scan_queue: Sender<ScanJob>,
5058 ancestor_inodes: TreeSet<u64>,
5059 is_external: bool,
5060}
5061
5062struct UpdateIgnoreStatusJob {
5063 abs_path: Arc<Path>,
5064 ignore_stack: Arc<IgnoreStack>,
5065 ignore_queue: Sender<UpdateIgnoreStatusJob>,
5066 scan_queue: Sender<ScanJob>,
5067}
5068
5069pub trait WorktreeModelHandle {
5070 #[cfg(any(test, feature = "test-support"))]
5071 fn flush_fs_events<'a>(
5072 &self,
5073 cx: &'a mut gpui::TestAppContext,
5074 ) -> futures::future::LocalBoxFuture<'a, ()>;
5075
5076 #[cfg(any(test, feature = "test-support"))]
5077 fn flush_fs_events_in_root_git_repository<'a>(
5078 &self,
5079 cx: &'a mut gpui::TestAppContext,
5080 ) -> futures::future::LocalBoxFuture<'a, ()>;
5081}
5082
5083impl WorktreeModelHandle for Entity<Worktree> {
5084 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
5085 // occurred before the worktree was constructed. These events can cause the worktree to perform
5086 // extra directory scans, and emit extra scan-state notifications.
5087 //
5088 // This function mutates the worktree's directory and waits for those mutations to be picked up,
5089 // to ensure that all redundant FS events have already been processed.
5090 #[cfg(any(test, feature = "test-support"))]
5091 fn flush_fs_events<'a>(
5092 &self,
5093 cx: &'a mut gpui::TestAppContext,
5094 ) -> futures::future::LocalBoxFuture<'a, ()> {
5095 let file_name = "fs-event-sentinel";
5096
5097 let tree = self.clone();
5098 let (fs, root_path) = self.update(cx, |tree, _| {
5099 let tree = tree.as_local().unwrap();
5100 (tree.fs.clone(), tree.abs_path().clone())
5101 });
5102
5103 async move {
5104 fs.create_file(&root_path.join(file_name), Default::default())
5105 .await
5106 .unwrap();
5107
5108 let mut events = cx.events(&tree);
5109 while events.next().await.is_some() {
5110 if tree.update(cx, |tree, _| tree.entry_for_path(file_name).is_some()) {
5111 break;
5112 }
5113 }
5114
5115 fs.remove_file(&root_path.join(file_name), Default::default())
5116 .await
5117 .unwrap();
5118 while events.next().await.is_some() {
5119 if tree.update(cx, |tree, _| tree.entry_for_path(file_name).is_none()) {
5120 break;
5121 }
5122 }
5123
5124 cx.update(|cx| tree.read(cx).as_local().unwrap().scan_complete())
5125 .await;
5126 }
5127 .boxed_local()
5128 }
5129
5130 // This function is similar to flush_fs_events, except that it waits for events to be flushed in
5131 // the .git folder of the root repository.
5132 // The reason for its existence is that a repository's .git folder might live *outside* of the
5133 // worktree and thus its FS events might go through a different path.
5134 // In order to flush those, we need to create artificial events in the .git folder and wait
5135 // for the repository to be reloaded.
5136 #[cfg(any(test, feature = "test-support"))]
5137 fn flush_fs_events_in_root_git_repository<'a>(
5138 &self,
5139 cx: &'a mut gpui::TestAppContext,
5140 ) -> futures::future::LocalBoxFuture<'a, ()> {
5141 let file_name = "fs-event-sentinel";
5142
5143 let tree = self.clone();
5144 let (fs, root_path, mut git_dir_scan_id) = self.update(cx, |tree, _| {
5145 let tree = tree.as_local().unwrap();
5146 let local_repo_entry = tree
5147 .git_repositories
5148 .values()
5149 .min_by_key(|local_repo_entry| local_repo_entry.work_directory.clone())
5150 .unwrap();
5151 (
5152 tree.fs.clone(),
5153 local_repo_entry.common_dir_abs_path.clone(),
5154 local_repo_entry.git_dir_scan_id,
5155 )
5156 });
5157
5158 let scan_id_increased = |tree: &mut Worktree, git_dir_scan_id: &mut usize| {
5159 let tree = tree.as_local().unwrap();
5160 // let repository = tree.repositories.first().unwrap();
5161 let local_repo_entry = tree
5162 .git_repositories
5163 .values()
5164 .min_by_key(|local_repo_entry| local_repo_entry.work_directory.clone())
5165 .unwrap();
5166
5167 if local_repo_entry.git_dir_scan_id > *git_dir_scan_id {
5168 *git_dir_scan_id = local_repo_entry.git_dir_scan_id;
5169 true
5170 } else {
5171 false
5172 }
5173 };
5174
5175 async move {
5176 fs.create_file(&root_path.join(file_name), Default::default())
5177 .await
5178 .unwrap();
5179
5180 let mut events = cx.events(&tree);
5181 while events.next().await.is_some() {
5182 if tree.update(cx, |tree, _| scan_id_increased(tree, &mut git_dir_scan_id)) {
5183 break;
5184 }
5185 }
5186
5187 fs.remove_file(&root_path.join(file_name), Default::default())
5188 .await
5189 .unwrap();
5190
5191 while events.next().await.is_some() {
5192 if tree.update(cx, |tree, _| scan_id_increased(tree, &mut git_dir_scan_id)) {
5193 break;
5194 }
5195 }
5196
5197 cx.update(|cx| tree.read(cx).as_local().unwrap().scan_complete())
5198 .await;
5199 }
5200 .boxed_local()
5201 }
5202}
5203
5204#[derive(Clone, Debug)]
5205struct TraversalProgress<'a> {
5206 max_path: &'a Path,
5207 count: usize,
5208 non_ignored_count: usize,
5209 file_count: usize,
5210 non_ignored_file_count: usize,
5211}
5212
5213impl TraversalProgress<'_> {
5214 fn count(&self, include_files: bool, include_dirs: bool, include_ignored: bool) -> usize {
5215 match (include_files, include_dirs, include_ignored) {
5216 (true, true, true) => self.count,
5217 (true, true, false) => self.non_ignored_count,
5218 (true, false, true) => self.file_count,
5219 (true, false, false) => self.non_ignored_file_count,
5220 (false, true, true) => self.count - self.file_count,
5221 (false, true, false) => self.non_ignored_count - self.non_ignored_file_count,
5222 (false, false, _) => 0,
5223 }
5224 }
5225}
5226
5227impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
5228 fn zero(_cx: &()) -> Self {
5229 Default::default()
5230 }
5231
5232 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
5233 self.max_path = summary.max_path.as_ref();
5234 self.count += summary.count;
5235 self.non_ignored_count += summary.non_ignored_count;
5236 self.file_count += summary.file_count;
5237 self.non_ignored_file_count += summary.non_ignored_file_count;
5238 }
5239}
5240
5241impl Default for TraversalProgress<'_> {
5242 fn default() -> Self {
5243 Self {
5244 max_path: Path::new(""),
5245 count: 0,
5246 non_ignored_count: 0,
5247 file_count: 0,
5248 non_ignored_file_count: 0,
5249 }
5250 }
5251}
5252
5253#[derive(Debug)]
5254pub struct Traversal<'a> {
5255 snapshot: &'a Snapshot,
5256 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
5257 include_ignored: bool,
5258 include_files: bool,
5259 include_dirs: bool,
5260}
5261
5262impl<'a> Traversal<'a> {
5263 fn new(
5264 snapshot: &'a Snapshot,
5265 include_files: bool,
5266 include_dirs: bool,
5267 include_ignored: bool,
5268 start_path: &Path,
5269 ) -> Self {
5270 let mut cursor = snapshot.entries_by_path.cursor(&());
5271 cursor.seek(&TraversalTarget::path(start_path), Bias::Left, &());
5272 let mut traversal = Self {
5273 snapshot,
5274 cursor,
5275 include_files,
5276 include_dirs,
5277 include_ignored,
5278 };
5279 if traversal.end_offset() == traversal.start_offset() {
5280 traversal.next();
5281 }
5282 traversal
5283 }
5284
5285 pub fn advance(&mut self) -> bool {
5286 self.advance_by(1)
5287 }
5288
5289 pub fn advance_by(&mut self, count: usize) -> bool {
5290 self.cursor.seek_forward(
5291 &TraversalTarget::Count {
5292 count: self.end_offset() + count,
5293 include_dirs: self.include_dirs,
5294 include_files: self.include_files,
5295 include_ignored: self.include_ignored,
5296 },
5297 Bias::Left,
5298 &(),
5299 )
5300 }
5301
5302 pub fn advance_to_sibling(&mut self) -> bool {
5303 while let Some(entry) = self.cursor.item() {
5304 self.cursor
5305 .seek_forward(&TraversalTarget::successor(&entry.path), Bias::Left, &());
5306 if let Some(entry) = self.cursor.item() {
5307 if (self.include_files || !entry.is_file())
5308 && (self.include_dirs || !entry.is_dir())
5309 && (self.include_ignored || !entry.is_ignored || entry.is_always_included)
5310 {
5311 return true;
5312 }
5313 }
5314 }
5315 false
5316 }
5317
5318 pub fn back_to_parent(&mut self) -> bool {
5319 let Some(parent_path) = self.cursor.item().and_then(|entry| entry.path.parent()) else {
5320 return false;
5321 };
5322 self.cursor
5323 .seek(&TraversalTarget::path(parent_path), Bias::Left, &())
5324 }
5325
5326 pub fn entry(&self) -> Option<&'a Entry> {
5327 self.cursor.item()
5328 }
5329
5330 pub fn snapshot(&self) -> &'a Snapshot {
5331 self.snapshot
5332 }
5333
5334 pub fn start_offset(&self) -> usize {
5335 self.cursor
5336 .start()
5337 .count(self.include_files, self.include_dirs, self.include_ignored)
5338 }
5339
5340 pub fn end_offset(&self) -> usize {
5341 self.cursor
5342 .end(&())
5343 .count(self.include_files, self.include_dirs, self.include_ignored)
5344 }
5345}
5346
5347impl<'a> Iterator for Traversal<'a> {
5348 type Item = &'a Entry;
5349
5350 fn next(&mut self) -> Option<Self::Item> {
5351 if let Some(item) = self.entry() {
5352 self.advance();
5353 Some(item)
5354 } else {
5355 None
5356 }
5357 }
5358}
5359
5360#[derive(Debug, Clone, Copy)]
5361pub enum PathTarget<'a> {
5362 Path(&'a Path),
5363 Successor(&'a Path),
5364}
5365
5366impl PathTarget<'_> {
5367 fn cmp_path(&self, other: &Path) -> Ordering {
5368 match self {
5369 PathTarget::Path(path) => path.cmp(&other),
5370 PathTarget::Successor(path) => {
5371 if other.starts_with(path) {
5372 Ordering::Greater
5373 } else {
5374 Ordering::Equal
5375 }
5376 }
5377 }
5378 }
5379}
5380
5381impl<'a, S: Summary> SeekTarget<'a, PathSummary<S>, PathProgress<'a>> for PathTarget<'_> {
5382 fn cmp(&self, cursor_location: &PathProgress<'a>, _: &S::Context) -> Ordering {
5383 self.cmp_path(&cursor_location.max_path)
5384 }
5385}
5386
5387impl<'a, S: Summary> SeekTarget<'a, PathSummary<S>, TraversalProgress<'a>> for PathTarget<'_> {
5388 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &S::Context) -> Ordering {
5389 self.cmp_path(&cursor_location.max_path)
5390 }
5391}
5392
5393#[derive(Debug)]
5394enum TraversalTarget<'a> {
5395 Path(PathTarget<'a>),
5396 Count {
5397 count: usize,
5398 include_files: bool,
5399 include_ignored: bool,
5400 include_dirs: bool,
5401 },
5402}
5403
5404impl<'a> TraversalTarget<'a> {
5405 fn path(path: &'a Path) -> Self {
5406 Self::Path(PathTarget::Path(path))
5407 }
5408
5409 fn successor(path: &'a Path) -> Self {
5410 Self::Path(PathTarget::Successor(path))
5411 }
5412
5413 fn cmp_progress(&self, progress: &TraversalProgress) -> Ordering {
5414 match self {
5415 TraversalTarget::Path(path) => path.cmp_path(&progress.max_path),
5416 TraversalTarget::Count {
5417 count,
5418 include_files,
5419 include_dirs,
5420 include_ignored,
5421 } => Ord::cmp(
5422 count,
5423 &progress.count(*include_files, *include_dirs, *include_ignored),
5424 ),
5425 }
5426 }
5427}
5428
5429impl<'a> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'_> {
5430 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
5431 self.cmp_progress(cursor_location)
5432 }
5433}
5434
5435impl<'a> SeekTarget<'a, PathSummary<Unit>, TraversalProgress<'a>> for TraversalTarget<'_> {
5436 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
5437 self.cmp_progress(cursor_location)
5438 }
5439}
5440
5441pub struct ChildEntriesOptions {
5442 pub include_files: bool,
5443 pub include_dirs: bool,
5444 pub include_ignored: bool,
5445}
5446
5447pub struct ChildEntriesIter<'a> {
5448 parent_path: &'a Path,
5449 traversal: Traversal<'a>,
5450}
5451
5452impl<'a> Iterator for ChildEntriesIter<'a> {
5453 type Item = &'a Entry;
5454
5455 fn next(&mut self) -> Option<Self::Item> {
5456 if let Some(item) = self.traversal.entry() {
5457 if item.path.starts_with(self.parent_path) {
5458 self.traversal.advance_to_sibling();
5459 return Some(item);
5460 }
5461 }
5462 None
5463 }
5464}
5465
5466impl<'a> From<&'a Entry> for proto::Entry {
5467 fn from(entry: &'a Entry) -> Self {
5468 Self {
5469 id: entry.id.to_proto(),
5470 is_dir: entry.is_dir(),
5471 path: entry.path.as_ref().to_proto(),
5472 inode: entry.inode,
5473 mtime: entry.mtime.map(|time| time.into()),
5474 is_ignored: entry.is_ignored,
5475 is_external: entry.is_external,
5476 is_fifo: entry.is_fifo,
5477 size: Some(entry.size),
5478 canonical_path: entry
5479 .canonical_path
5480 .as_ref()
5481 .map(|path| path.as_ref().to_proto()),
5482 }
5483 }
5484}
5485
5486impl<'a> TryFrom<(&'a CharBag, &PathMatcher, proto::Entry)> for Entry {
5487 type Error = anyhow::Error;
5488
5489 fn try_from(
5490 (root_char_bag, always_included, entry): (&'a CharBag, &PathMatcher, proto::Entry),
5491 ) -> Result<Self> {
5492 let kind = if entry.is_dir {
5493 EntryKind::Dir
5494 } else {
5495 EntryKind::File
5496 };
5497
5498 let path = Arc::<Path>::from_proto(entry.path);
5499 let char_bag = char_bag_for_path(*root_char_bag, &path);
5500 let is_always_included = always_included.is_match(path.as_ref());
5501 Ok(Entry {
5502 id: ProjectEntryId::from_proto(entry.id),
5503 kind,
5504 path,
5505 inode: entry.inode,
5506 mtime: entry.mtime.map(|time| time.into()),
5507 size: entry.size.unwrap_or(0),
5508 canonical_path: entry
5509 .canonical_path
5510 .map(|path_string| Arc::from(PathBuf::from_proto(path_string))),
5511 is_ignored: entry.is_ignored,
5512 is_always_included,
5513 is_external: entry.is_external,
5514 is_private: false,
5515 char_bag,
5516 is_fifo: entry.is_fifo,
5517 })
5518 }
5519}
5520
5521#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
5522pub struct ProjectEntryId(usize);
5523
5524impl ProjectEntryId {
5525 pub const MAX: Self = Self(usize::MAX);
5526 pub const MIN: Self = Self(usize::MIN);
5527
5528 pub fn new(counter: &AtomicUsize) -> Self {
5529 Self(counter.fetch_add(1, SeqCst))
5530 }
5531
5532 pub fn from_proto(id: u64) -> Self {
5533 Self(id as usize)
5534 }
5535
5536 pub fn to_proto(&self) -> u64 {
5537 self.0 as u64
5538 }
5539
5540 pub fn from_usize(id: usize) -> Self {
5541 ProjectEntryId(id)
5542 }
5543
5544 pub fn to_usize(&self) -> usize {
5545 self.0
5546 }
5547}
5548
5549#[cfg(any(test, feature = "test-support"))]
5550impl CreatedEntry {
5551 pub fn to_included(self) -> Option<Entry> {
5552 match self {
5553 CreatedEntry::Included(entry) => Some(entry),
5554 CreatedEntry::Excluded { .. } => None,
5555 }
5556 }
5557}
5558
5559fn parse_gitfile(content: &str) -> anyhow::Result<&Path> {
5560 let path = content
5561 .strip_prefix("gitdir:")
5562 .ok_or_else(|| anyhow!("failed to parse gitfile content {content:?}"))?;
5563 Ok(Path::new(path.trim()))
5564}
5565
5566fn discover_git_paths(dot_git_abs_path: &Arc<Path>, fs: &dyn Fs) -> (Arc<Path>, Arc<Path>) {
5567 let mut repository_dir_abs_path = dot_git_abs_path.clone();
5568 let mut common_dir_abs_path = dot_git_abs_path.clone();
5569
5570 if let Some(path) = smol::block_on(fs.load(&dot_git_abs_path))
5571 .ok()
5572 .as_ref()
5573 .and_then(|contents| parse_gitfile(contents).log_err())
5574 {
5575 let path = dot_git_abs_path
5576 .parent()
5577 .unwrap_or(Path::new(""))
5578 .join(path);
5579 if let Some(path) = smol::block_on(fs.canonicalize(&path)).log_err() {
5580 repository_dir_abs_path = Path::new(&path).into();
5581 common_dir_abs_path = repository_dir_abs_path.clone();
5582 if let Some(commondir_contents) = smol::block_on(fs.load(&path.join("commondir"))).ok()
5583 {
5584 if let Some(commondir_path) =
5585 smol::block_on(fs.canonicalize(&path.join(commondir_contents.trim()))).log_err()
5586 {
5587 common_dir_abs_path = commondir_path.as_path().into();
5588 }
5589 }
5590 }
5591 };
5592
5593 (repository_dir_abs_path, common_dir_abs_path)
5594}