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