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