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