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