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 let work_dir_path: Arc<Path> = match dot_git_path.parent() {
2700 Some(parent_dir) => {
2701 // Guard against repositories inside the repository metadata
2702 if parent_dir.iter().any(|component| component == *DOT_GIT) {
2703 log::info!(
2704 "not building git repository for nested `.git` directory, `.git` path in the worktree: {dot_git_path:?}"
2705 );
2706 return None;
2707 };
2708 log::info!(
2709 "building git repository, `.git` path in the worktree: {dot_git_path:?}"
2710 );
2711 parent_dir.into()
2712 }
2713 None => {
2714 // `dot_git_path.parent().is_none()` means `.git` directory is the opened worktree itself,
2715 // no files inside that directory are tracked by git, so no need to build the repo around it
2716 log::info!(
2717 "not building git repository for the worktree itself, `.git` path in the worktree: {dot_git_path:?}"
2718 );
2719 return None;
2720 }
2721 };
2722
2723 let work_dir_id = self
2724 .snapshot
2725 .entry_for_path(work_dir_path.clone())
2726 .map(|entry| entry.id)?;
2727
2728 if self.snapshot.git_repositories.get(&work_dir_id).is_some() {
2729 return None;
2730 }
2731
2732 let abs_path = self.snapshot.abs_path.join(&dot_git_path);
2733 let repository = fs.open_repo(abs_path.as_path())?;
2734 let work_directory = RepositoryWorkDirectory(work_dir_path.clone());
2735
2736 let repo_lock = repository.lock();
2737 self.snapshot.repository_entries.insert(
2738 work_directory.clone(),
2739 RepositoryEntry {
2740 work_directory: work_dir_id.into(),
2741 branch: repo_lock.branch_name().map(Into::into),
2742 },
2743 );
2744
2745 let staged_statuses = self.update_git_statuses(&work_directory, &*repo_lock);
2746 drop(repo_lock);
2747
2748 self.snapshot.git_repositories.insert(
2749 work_dir_id,
2750 LocalRepositoryEntry {
2751 git_dir_scan_id: 0,
2752 repo_ptr: repository.clone(),
2753 git_dir_path: dot_git_path.clone(),
2754 },
2755 );
2756
2757 Some((work_directory, repository, staged_statuses))
2758 }
2759
2760 fn update_git_statuses(
2761 &mut self,
2762 work_directory: &RepositoryWorkDirectory,
2763 repo: &dyn GitRepository,
2764 ) -> TreeMap<RepoPath, GitFileStatus> {
2765 let staged_statuses = repo.staged_statuses(Path::new(""));
2766
2767 let mut changes = vec![];
2768 let mut edits = vec![];
2769
2770 for mut entry in self
2771 .snapshot
2772 .descendent_entries(false, false, &work_directory.0)
2773 .cloned()
2774 {
2775 let Ok(repo_path) = entry.path.strip_prefix(&work_directory.0) else {
2776 continue;
2777 };
2778 let Some(mtime) = entry.mtime else {
2779 continue;
2780 };
2781 let repo_path = RepoPath(repo_path.to_path_buf());
2782 let git_file_status = combine_git_statuses(
2783 staged_statuses.get(&repo_path).copied(),
2784 repo.unstaged_status(&repo_path, mtime),
2785 );
2786 if entry.git_status != git_file_status {
2787 entry.git_status = git_file_status;
2788 changes.push(entry.path.clone());
2789 edits.push(Edit::Insert(entry));
2790 }
2791 }
2792
2793 self.snapshot.entries_by_path.edit(edits, &());
2794 util::extend_sorted(&mut self.changed_paths, changes, usize::MAX, Ord::cmp);
2795 staged_statuses
2796 }
2797}
2798
2799async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
2800 let contents = fs.load(abs_path).await?;
2801 let parent = abs_path.parent().unwrap_or_else(|| Path::new("/"));
2802 let mut builder = GitignoreBuilder::new(parent);
2803 for line in contents.lines() {
2804 builder.add_line(Some(abs_path.into()), line)?;
2805 }
2806 Ok(builder.build()?)
2807}
2808
2809impl WorktreeId {
2810 pub fn from_usize(handle_id: usize) -> Self {
2811 Self(handle_id)
2812 }
2813
2814 pub fn from_proto(id: u64) -> Self {
2815 Self(id as usize)
2816 }
2817
2818 pub fn to_proto(&self) -> u64 {
2819 self.0 as u64
2820 }
2821
2822 pub fn to_usize(&self) -> usize {
2823 self.0
2824 }
2825}
2826
2827impl fmt::Display for WorktreeId {
2828 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2829 self.0.fmt(f)
2830 }
2831}
2832
2833impl Deref for Worktree {
2834 type Target = Snapshot;
2835
2836 fn deref(&self) -> &Self::Target {
2837 match self {
2838 Worktree::Local(worktree) => &worktree.snapshot,
2839 Worktree::Remote(worktree) => &worktree.snapshot,
2840 }
2841 }
2842}
2843
2844impl Deref for LocalWorktree {
2845 type Target = LocalSnapshot;
2846
2847 fn deref(&self) -> &Self::Target {
2848 &self.snapshot
2849 }
2850}
2851
2852impl Deref for RemoteWorktree {
2853 type Target = Snapshot;
2854
2855 fn deref(&self) -> &Self::Target {
2856 &self.snapshot
2857 }
2858}
2859
2860impl fmt::Debug for LocalWorktree {
2861 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2862 self.snapshot.fmt(f)
2863 }
2864}
2865
2866impl fmt::Debug for Snapshot {
2867 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2868 struct EntriesById<'a>(&'a SumTree<PathEntry>);
2869 struct EntriesByPath<'a>(&'a SumTree<Entry>);
2870
2871 impl<'a> fmt::Debug for EntriesByPath<'a> {
2872 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2873 f.debug_map()
2874 .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
2875 .finish()
2876 }
2877 }
2878
2879 impl<'a> fmt::Debug for EntriesById<'a> {
2880 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2881 f.debug_list().entries(self.0.iter()).finish()
2882 }
2883 }
2884
2885 f.debug_struct("Snapshot")
2886 .field("id", &self.id)
2887 .field("root_name", &self.root_name)
2888 .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
2889 .field("entries_by_id", &EntriesById(&self.entries_by_id))
2890 .finish()
2891 }
2892}
2893
2894#[derive(Clone, PartialEq)]
2895pub struct File {
2896 pub worktree: Model<Worktree>,
2897 pub path: Arc<Path>,
2898 pub mtime: Option<SystemTime>,
2899 pub entry_id: Option<ProjectEntryId>,
2900 pub is_local: bool,
2901 pub is_deleted: bool,
2902 pub is_private: bool,
2903}
2904
2905impl language::File for File {
2906 fn as_local(&self) -> Option<&dyn language::LocalFile> {
2907 if self.is_local {
2908 Some(self)
2909 } else {
2910 None
2911 }
2912 }
2913
2914 fn mtime(&self) -> Option<SystemTime> {
2915 self.mtime
2916 }
2917
2918 fn path(&self) -> &Arc<Path> {
2919 &self.path
2920 }
2921
2922 fn full_path(&self, cx: &AppContext) -> PathBuf {
2923 let mut full_path = PathBuf::new();
2924 let worktree = self.worktree.read(cx);
2925
2926 if worktree.is_visible() {
2927 full_path.push(worktree.root_name());
2928 } else {
2929 let path = worktree.abs_path();
2930
2931 if worktree.is_local() && path.starts_with(HOME.as_path()) {
2932 full_path.push("~");
2933 full_path.push(path.strip_prefix(HOME.as_path()).unwrap());
2934 } else {
2935 full_path.push(path)
2936 }
2937 }
2938
2939 if self.path.components().next().is_some() {
2940 full_path.push(&self.path);
2941 }
2942
2943 full_path
2944 }
2945
2946 /// Returns the last component of this handle's absolute path. If this handle refers to the root
2947 /// of its worktree, then this method will return the name of the worktree itself.
2948 fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr {
2949 self.path
2950 .file_name()
2951 .unwrap_or_else(|| OsStr::new(&self.worktree.read(cx).root_name))
2952 }
2953
2954 fn worktree_id(&self) -> usize {
2955 self.worktree.entity_id().as_u64() as usize
2956 }
2957
2958 fn is_deleted(&self) -> bool {
2959 self.is_deleted
2960 }
2961
2962 fn as_any(&self) -> &dyn Any {
2963 self
2964 }
2965
2966 fn to_proto(&self) -> rpc::proto::File {
2967 rpc::proto::File {
2968 worktree_id: self.worktree.entity_id().as_u64(),
2969 entry_id: self.entry_id.map(|id| id.to_proto()),
2970 path: self.path.to_string_lossy().into(),
2971 mtime: self.mtime.map(|time| time.into()),
2972 is_deleted: self.is_deleted,
2973 }
2974 }
2975
2976 fn is_private(&self) -> bool {
2977 self.is_private
2978 }
2979}
2980
2981impl language::LocalFile for File {
2982 fn abs_path(&self, cx: &AppContext) -> PathBuf {
2983 let worktree_path = &self.worktree.read(cx).as_local().unwrap().abs_path;
2984 if self.path.as_ref() == Path::new("") {
2985 worktree_path.to_path_buf()
2986 } else {
2987 worktree_path.join(&self.path)
2988 }
2989 }
2990
2991 fn load(&self, cx: &AppContext) -> Task<Result<String>> {
2992 let worktree = self.worktree.read(cx).as_local().unwrap();
2993 let abs_path = worktree.absolutize(&self.path);
2994 let fs = worktree.fs.clone();
2995 cx.background_executor()
2996 .spawn(async move { fs.load(&abs_path?).await })
2997 }
2998
2999 fn buffer_reloaded(
3000 &self,
3001 buffer_id: BufferId,
3002 version: &clock::Global,
3003 fingerprint: RopeFingerprint,
3004 line_ending: LineEnding,
3005 mtime: Option<SystemTime>,
3006 cx: &mut AppContext,
3007 ) {
3008 let worktree = self.worktree.read(cx).as_local().unwrap();
3009 if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) {
3010 worktree
3011 .client
3012 .send(proto::BufferReloaded {
3013 project_id,
3014 buffer_id: buffer_id.into(),
3015 version: serialize_version(version),
3016 mtime: mtime.map(|time| time.into()),
3017 fingerprint: serialize_fingerprint(fingerprint),
3018 line_ending: serialize_line_ending(line_ending) as i32,
3019 })
3020 .log_err();
3021 }
3022 }
3023}
3024
3025impl File {
3026 pub fn for_entry(entry: Entry, worktree: Model<Worktree>) -> Arc<Self> {
3027 Arc::new(Self {
3028 worktree,
3029 path: entry.path.clone(),
3030 mtime: entry.mtime,
3031 entry_id: Some(entry.id),
3032 is_local: true,
3033 is_deleted: false,
3034 is_private: entry.is_private,
3035 })
3036 }
3037
3038 pub fn from_proto(
3039 proto: rpc::proto::File,
3040 worktree: Model<Worktree>,
3041 cx: &AppContext,
3042 ) -> Result<Self> {
3043 let worktree_id = worktree
3044 .read(cx)
3045 .as_remote()
3046 .ok_or_else(|| anyhow!("not remote"))?
3047 .id();
3048
3049 if worktree_id.to_proto() != proto.worktree_id {
3050 return Err(anyhow!("worktree id does not match file"));
3051 }
3052
3053 Ok(Self {
3054 worktree,
3055 path: Path::new(&proto.path).into(),
3056 mtime: proto.mtime.map(|time| time.into()),
3057 entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
3058 is_local: false,
3059 is_deleted: proto.is_deleted,
3060 is_private: false,
3061 })
3062 }
3063
3064 pub fn from_dyn(file: Option<&Arc<dyn language::File>>) -> Option<&Self> {
3065 file.and_then(|f| f.as_any().downcast_ref())
3066 }
3067
3068 pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
3069 self.worktree.read(cx).id()
3070 }
3071
3072 pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
3073 if self.is_deleted {
3074 None
3075 } else {
3076 self.entry_id
3077 }
3078 }
3079}
3080
3081#[derive(Clone, Debug, PartialEq, Eq)]
3082pub struct Entry {
3083 pub id: ProjectEntryId,
3084 pub kind: EntryKind,
3085 pub path: Arc<Path>,
3086 pub inode: u64,
3087 pub mtime: Option<SystemTime>,
3088 pub is_symlink: bool,
3089
3090 /// Whether this entry is ignored by Git.
3091 ///
3092 /// We only scan ignored entries once the directory is expanded and
3093 /// exclude them from searches.
3094 pub is_ignored: bool,
3095
3096 /// Whether this entry's canonical path is outside of the worktree.
3097 /// This means the entry is only accessible from the worktree root via a
3098 /// symlink.
3099 ///
3100 /// We only scan entries outside of the worktree once the symlinked
3101 /// directory is expanded. External entries are treated like gitignored
3102 /// entries in that they are not included in searches.
3103 pub is_external: bool,
3104 pub git_status: Option<GitFileStatus>,
3105 /// Whether this entry is considered to be a `.env` file.
3106 pub is_private: bool,
3107}
3108
3109#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3110pub enum EntryKind {
3111 UnloadedDir,
3112 PendingDir,
3113 Dir,
3114 File(CharBag),
3115}
3116
3117#[derive(Clone, Copy, Debug, PartialEq)]
3118pub enum PathChange {
3119 /// A filesystem entry was was created.
3120 Added,
3121 /// A filesystem entry was removed.
3122 Removed,
3123 /// A filesystem entry was updated.
3124 Updated,
3125 /// A filesystem entry was either updated or added. We don't know
3126 /// whether or not it already existed, because the path had not
3127 /// been loaded before the event.
3128 AddedOrUpdated,
3129 /// A filesystem entry was found during the initial scan of the worktree.
3130 Loaded,
3131}
3132
3133pub struct GitRepositoryChange {
3134 /// The previous state of the repository, if it already existed.
3135 pub old_repository: Option<RepositoryEntry>,
3136}
3137
3138pub type UpdatedEntriesSet = Arc<[(Arc<Path>, ProjectEntryId, PathChange)]>;
3139pub type UpdatedGitRepositoriesSet = Arc<[(Arc<Path>, GitRepositoryChange)]>;
3140
3141impl Entry {
3142 fn new(
3143 path: Arc<Path>,
3144 metadata: &fs::Metadata,
3145 next_entry_id: &AtomicUsize,
3146 root_char_bag: CharBag,
3147 ) -> Self {
3148 Self {
3149 id: ProjectEntryId::new(next_entry_id),
3150 kind: if metadata.is_dir {
3151 EntryKind::PendingDir
3152 } else {
3153 EntryKind::File(char_bag_for_path(root_char_bag, &path))
3154 },
3155 path,
3156 inode: metadata.inode,
3157 mtime: Some(metadata.mtime),
3158 is_symlink: metadata.is_symlink,
3159 is_ignored: false,
3160 is_external: false,
3161 is_private: false,
3162 git_status: None,
3163 }
3164 }
3165
3166 pub fn is_created(&self) -> bool {
3167 self.mtime.is_some()
3168 }
3169
3170 pub fn is_dir(&self) -> bool {
3171 self.kind.is_dir()
3172 }
3173
3174 pub fn is_file(&self) -> bool {
3175 self.kind.is_file()
3176 }
3177
3178 pub fn git_status(&self) -> Option<GitFileStatus> {
3179 self.git_status
3180 }
3181}
3182
3183impl EntryKind {
3184 pub fn is_dir(&self) -> bool {
3185 matches!(
3186 self,
3187 EntryKind::Dir | EntryKind::PendingDir | EntryKind::UnloadedDir
3188 )
3189 }
3190
3191 pub fn is_unloaded(&self) -> bool {
3192 matches!(self, EntryKind::UnloadedDir)
3193 }
3194
3195 pub fn is_file(&self) -> bool {
3196 matches!(self, EntryKind::File(_))
3197 }
3198}
3199
3200impl sum_tree::Item for Entry {
3201 type Summary = EntrySummary;
3202
3203 fn summary(&self) -> Self::Summary {
3204 let non_ignored_count = if self.is_ignored || self.is_external {
3205 0
3206 } else {
3207 1
3208 };
3209 let file_count;
3210 let non_ignored_file_count;
3211 if self.is_file() {
3212 file_count = 1;
3213 non_ignored_file_count = non_ignored_count;
3214 } else {
3215 file_count = 0;
3216 non_ignored_file_count = 0;
3217 }
3218
3219 let mut statuses = GitStatuses::default();
3220 match self.git_status {
3221 Some(status) => match status {
3222 GitFileStatus::Added => statuses.added = 1,
3223 GitFileStatus::Modified => statuses.modified = 1,
3224 GitFileStatus::Conflict => statuses.conflict = 1,
3225 },
3226 None => {}
3227 }
3228
3229 EntrySummary {
3230 max_path: self.path.clone(),
3231 count: 1,
3232 non_ignored_count,
3233 file_count,
3234 non_ignored_file_count,
3235 statuses,
3236 }
3237 }
3238}
3239
3240impl sum_tree::KeyedItem for Entry {
3241 type Key = PathKey;
3242
3243 fn key(&self) -> Self::Key {
3244 PathKey(self.path.clone())
3245 }
3246}
3247
3248#[derive(Clone, Debug)]
3249pub struct EntrySummary {
3250 max_path: Arc<Path>,
3251 count: usize,
3252 non_ignored_count: usize,
3253 file_count: usize,
3254 non_ignored_file_count: usize,
3255 statuses: GitStatuses,
3256}
3257
3258impl Default for EntrySummary {
3259 fn default() -> Self {
3260 Self {
3261 max_path: Arc::from(Path::new("")),
3262 count: 0,
3263 non_ignored_count: 0,
3264 file_count: 0,
3265 non_ignored_file_count: 0,
3266 statuses: Default::default(),
3267 }
3268 }
3269}
3270
3271impl sum_tree::Summary for EntrySummary {
3272 type Context = ();
3273
3274 fn add_summary(&mut self, rhs: &Self, _: &()) {
3275 self.max_path = rhs.max_path.clone();
3276 self.count += rhs.count;
3277 self.non_ignored_count += rhs.non_ignored_count;
3278 self.file_count += rhs.file_count;
3279 self.non_ignored_file_count += rhs.non_ignored_file_count;
3280 self.statuses += rhs.statuses;
3281 }
3282}
3283
3284#[derive(Clone, Debug)]
3285struct PathEntry {
3286 id: ProjectEntryId,
3287 path: Arc<Path>,
3288 is_ignored: bool,
3289 scan_id: usize,
3290}
3291
3292impl sum_tree::Item for PathEntry {
3293 type Summary = PathEntrySummary;
3294
3295 fn summary(&self) -> Self::Summary {
3296 PathEntrySummary { max_id: self.id }
3297 }
3298}
3299
3300impl sum_tree::KeyedItem for PathEntry {
3301 type Key = ProjectEntryId;
3302
3303 fn key(&self) -> Self::Key {
3304 self.id
3305 }
3306}
3307
3308#[derive(Clone, Debug, Default)]
3309struct PathEntrySummary {
3310 max_id: ProjectEntryId,
3311}
3312
3313impl sum_tree::Summary for PathEntrySummary {
3314 type Context = ();
3315
3316 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
3317 self.max_id = summary.max_id;
3318 }
3319}
3320
3321impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
3322 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
3323 *self = summary.max_id;
3324 }
3325}
3326
3327#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
3328pub struct PathKey(Arc<Path>);
3329
3330impl Default for PathKey {
3331 fn default() -> Self {
3332 Self(Path::new("").into())
3333 }
3334}
3335
3336impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
3337 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
3338 self.0 = summary.max_path.clone();
3339 }
3340}
3341
3342struct BackgroundScanner {
3343 state: Mutex<BackgroundScannerState>,
3344 fs: Arc<dyn Fs>,
3345 fs_case_sensitive: bool,
3346 status_updates_tx: UnboundedSender<ScanState>,
3347 executor: BackgroundExecutor,
3348 scan_requests_rx: channel::Receiver<ScanRequest>,
3349 path_prefixes_to_scan_rx: channel::Receiver<Arc<Path>>,
3350 next_entry_id: Arc<AtomicUsize>,
3351 phase: BackgroundScannerPhase,
3352}
3353
3354#[derive(PartialEq)]
3355enum BackgroundScannerPhase {
3356 InitialScan,
3357 EventsReceivedDuringInitialScan,
3358 Events,
3359}
3360
3361impl BackgroundScanner {
3362 #[allow(clippy::too_many_arguments)]
3363 fn new(
3364 snapshot: LocalSnapshot,
3365 next_entry_id: Arc<AtomicUsize>,
3366 fs: Arc<dyn Fs>,
3367 fs_case_sensitive: bool,
3368 status_updates_tx: UnboundedSender<ScanState>,
3369 executor: BackgroundExecutor,
3370 scan_requests_rx: channel::Receiver<ScanRequest>,
3371 path_prefixes_to_scan_rx: channel::Receiver<Arc<Path>>,
3372 ) -> Self {
3373 Self {
3374 fs,
3375 fs_case_sensitive,
3376 status_updates_tx,
3377 executor,
3378 scan_requests_rx,
3379 path_prefixes_to_scan_rx,
3380 next_entry_id,
3381 state: Mutex::new(BackgroundScannerState {
3382 prev_snapshot: snapshot.snapshot.clone(),
3383 snapshot,
3384 scanned_dirs: Default::default(),
3385 path_prefixes_to_scan: Default::default(),
3386 paths_to_scan: Default::default(),
3387 removed_entry_ids: Default::default(),
3388 changed_paths: Default::default(),
3389 }),
3390 phase: BackgroundScannerPhase::InitialScan,
3391 }
3392 }
3393
3394 async fn run(&mut self, mut fs_events_rx: Pin<Box<dyn Send + Stream<Item = Vec<PathBuf>>>>) {
3395 use futures::FutureExt as _;
3396
3397 // Populate ignores above the root.
3398 let root_abs_path = self.state.lock().snapshot.abs_path.clone();
3399 for (index, ancestor) in root_abs_path.ancestors().enumerate() {
3400 if index != 0 {
3401 if let Ok(ignore) =
3402 build_gitignore(&ancestor.join(&*GITIGNORE), self.fs.as_ref()).await
3403 {
3404 self.state
3405 .lock()
3406 .snapshot
3407 .ignores_by_parent_abs_path
3408 .insert(ancestor.into(), (ignore.into(), false));
3409 }
3410 }
3411 if ancestor.join(&*DOT_GIT).is_dir() {
3412 // Reached root of git repository.
3413 break;
3414 }
3415 }
3416
3417 let (scan_job_tx, scan_job_rx) = channel::unbounded();
3418 {
3419 let mut state = self.state.lock();
3420 state.snapshot.scan_id += 1;
3421 if let Some(mut root_entry) = state.snapshot.root_entry().cloned() {
3422 let ignore_stack = state
3423 .snapshot
3424 .ignore_stack_for_abs_path(&root_abs_path, true);
3425 if ignore_stack.is_abs_path_ignored(&root_abs_path, true) {
3426 root_entry.is_ignored = true;
3427 state.insert_entry(root_entry.clone(), self.fs.as_ref());
3428 }
3429 state.enqueue_scan_dir(root_abs_path, &root_entry, &scan_job_tx);
3430 }
3431 };
3432
3433 // Perform an initial scan of the directory.
3434 drop(scan_job_tx);
3435 self.scan_dirs(true, scan_job_rx).await;
3436 {
3437 let mut state = self.state.lock();
3438 state.snapshot.completed_scan_id = state.snapshot.scan_id;
3439 }
3440
3441 self.send_status_update(false, None);
3442
3443 // Process any any FS events that occurred while performing the initial scan.
3444 // For these events, update events cannot be as precise, because we didn't
3445 // have the previous state loaded yet.
3446 self.phase = BackgroundScannerPhase::EventsReceivedDuringInitialScan;
3447 if let Poll::Ready(Some(mut paths)) = futures::poll!(fs_events_rx.next()) {
3448 while let Poll::Ready(Some(more_paths)) = futures::poll!(fs_events_rx.next()) {
3449 paths.extend(more_paths);
3450 }
3451 self.process_events(paths).await;
3452 }
3453
3454 // Continue processing events until the worktree is dropped.
3455 self.phase = BackgroundScannerPhase::Events;
3456 loop {
3457 select_biased! {
3458 // Process any path refresh requests from the worktree. Prioritize
3459 // these before handling changes reported by the filesystem.
3460 request = self.scan_requests_rx.recv().fuse() => {
3461 let Ok(request) = request else { break };
3462 if !self.process_scan_request(request, false).await {
3463 return;
3464 }
3465 }
3466
3467 path_prefix = self.path_prefixes_to_scan_rx.recv().fuse() => {
3468 let Ok(path_prefix) = path_prefix else { break };
3469 log::trace!("adding path prefix {:?}", path_prefix);
3470
3471 let did_scan = self.forcibly_load_paths(&[path_prefix.clone()]).await;
3472 if did_scan {
3473 let abs_path =
3474 {
3475 let mut state = self.state.lock();
3476 state.path_prefixes_to_scan.insert(path_prefix.clone());
3477 state.snapshot.abs_path.join(&path_prefix)
3478 };
3479
3480 if let Some(abs_path) = self.fs.canonicalize(&abs_path).await.log_err() {
3481 self.process_events(vec![abs_path]).await;
3482 }
3483 }
3484 }
3485
3486 paths = fs_events_rx.next().fuse() => {
3487 let Some(mut paths) = paths else { break };
3488 while let Poll::Ready(Some(more_paths)) = futures::poll!(fs_events_rx.next()) {
3489 paths.extend(more_paths);
3490 }
3491 self.process_events(paths.clone()).await;
3492 }
3493 }
3494 }
3495 }
3496
3497 async fn process_scan_request(&self, mut request: ScanRequest, scanning: bool) -> bool {
3498 log::debug!("rescanning paths {:?}", request.relative_paths);
3499
3500 request.relative_paths.sort_unstable();
3501 self.forcibly_load_paths(&request.relative_paths).await;
3502
3503 let root_path = self.state.lock().snapshot.abs_path.clone();
3504 let root_canonical_path = match self.fs.canonicalize(&root_path).await {
3505 Ok(path) => path,
3506 Err(err) => {
3507 log::error!("failed to canonicalize root path: {}", err);
3508 return true;
3509 }
3510 };
3511 let abs_paths = request
3512 .relative_paths
3513 .iter()
3514 .map(|path| {
3515 if path.file_name().is_some() {
3516 root_canonical_path.join(path)
3517 } else {
3518 root_canonical_path.clone()
3519 }
3520 })
3521 .collect::<Vec<_>>();
3522
3523 self.reload_entries_for_paths(
3524 root_path,
3525 root_canonical_path,
3526 &request.relative_paths,
3527 abs_paths,
3528 None,
3529 )
3530 .await;
3531 self.send_status_update(scanning, Some(request.done))
3532 }
3533
3534 async fn process_events(&mut self, mut abs_paths: Vec<PathBuf>) {
3535 let root_path = self.state.lock().snapshot.abs_path.clone();
3536 let root_canonical_path = match self.fs.canonicalize(&root_path).await {
3537 Ok(path) => path,
3538 Err(err) => {
3539 log::error!("failed to canonicalize root path: {}", err);
3540 return;
3541 }
3542 };
3543
3544 let mut relative_paths = Vec::with_capacity(abs_paths.len());
3545 let mut dot_git_paths_to_reload = HashSet::default();
3546 abs_paths.sort_unstable();
3547 abs_paths.dedup_by(|a, b| a.starts_with(&b));
3548 abs_paths.retain(|abs_path| {
3549 let snapshot = &self.state.lock().snapshot;
3550 {
3551 let mut is_git_related = false;
3552 if let Some(dot_git_dir) = abs_path
3553 .ancestors()
3554 .find(|ancestor| ancestor.file_name() == Some(*DOT_GIT))
3555 {
3556 let dot_git_path = dot_git_dir
3557 .strip_prefix(&root_canonical_path)
3558 .ok()
3559 .map(|path| path.to_path_buf())
3560 .unwrap_or_else(|| dot_git_dir.to_path_buf());
3561 dot_git_paths_to_reload.insert(dot_git_path.to_path_buf());
3562 is_git_related = true;
3563 }
3564
3565 let relative_path: Arc<Path> =
3566 if let Ok(path) = abs_path.strip_prefix(&root_canonical_path) {
3567 path.into()
3568 } else {
3569 log::error!(
3570 "ignoring event {abs_path:?} outside of root path {root_canonical_path:?}",
3571 );
3572 return false;
3573 };
3574
3575 let parent_dir_is_loaded = relative_path.parent().map_or(true, |parent| {
3576 snapshot
3577 .entry_for_path(parent)
3578 .map_or(false, |entry| entry.kind == EntryKind::Dir)
3579 });
3580 if !parent_dir_is_loaded {
3581 log::debug!("ignoring event {relative_path:?} within unloaded directory");
3582 return false;
3583 }
3584
3585 if snapshot.is_path_excluded(relative_path.to_path_buf()) {
3586 if !is_git_related {
3587 log::debug!("ignoring FS event for excluded path {relative_path:?}");
3588 }
3589 return false;
3590 }
3591
3592 relative_paths.push(relative_path);
3593 true
3594 }
3595 });
3596
3597 if dot_git_paths_to_reload.is_empty() && relative_paths.is_empty() {
3598 return;
3599 }
3600
3601 if !relative_paths.is_empty() {
3602 log::debug!("received fs events {:?}", relative_paths);
3603
3604 let (scan_job_tx, scan_job_rx) = channel::unbounded();
3605 self.reload_entries_for_paths(
3606 root_path,
3607 root_canonical_path,
3608 &relative_paths,
3609 abs_paths,
3610 Some(scan_job_tx.clone()),
3611 )
3612 .await;
3613 drop(scan_job_tx);
3614 self.scan_dirs(false, scan_job_rx).await;
3615
3616 let (scan_job_tx, scan_job_rx) = channel::unbounded();
3617 self.update_ignore_statuses(scan_job_tx).await;
3618 self.scan_dirs(false, scan_job_rx).await;
3619 }
3620
3621 {
3622 let mut state = self.state.lock();
3623 if !dot_git_paths_to_reload.is_empty() {
3624 if relative_paths.is_empty() {
3625 state.snapshot.scan_id += 1;
3626 }
3627 log::debug!("reloading repositories: {dot_git_paths_to_reload:?}");
3628 state.reload_repositories(&dot_git_paths_to_reload, self.fs.as_ref());
3629 }
3630 state.snapshot.completed_scan_id = state.snapshot.scan_id;
3631 for (_, entry_id) in mem::take(&mut state.removed_entry_ids) {
3632 state.scanned_dirs.remove(&entry_id);
3633 }
3634 }
3635
3636 self.send_status_update(false, None);
3637 }
3638
3639 async fn forcibly_load_paths(&self, paths: &[Arc<Path>]) -> bool {
3640 let (scan_job_tx, mut scan_job_rx) = channel::unbounded();
3641 {
3642 let mut state = self.state.lock();
3643 let root_path = state.snapshot.abs_path.clone();
3644 for path in paths {
3645 for ancestor in path.ancestors() {
3646 if let Some(entry) = state.snapshot.entry_for_path(ancestor) {
3647 if entry.kind == EntryKind::UnloadedDir {
3648 let abs_path = root_path.join(ancestor);
3649 state.enqueue_scan_dir(abs_path.into(), entry, &scan_job_tx);
3650 state.paths_to_scan.insert(path.clone());
3651 break;
3652 }
3653 }
3654 }
3655 }
3656 drop(scan_job_tx);
3657 }
3658 while let Some(job) = scan_job_rx.next().await {
3659 self.scan_dir(&job).await.log_err();
3660 }
3661
3662 mem::take(&mut self.state.lock().paths_to_scan).len() > 0
3663 }
3664
3665 async fn scan_dirs(
3666 &self,
3667 enable_progress_updates: bool,
3668 scan_jobs_rx: channel::Receiver<ScanJob>,
3669 ) {
3670 use futures::FutureExt as _;
3671
3672 if self
3673 .status_updates_tx
3674 .unbounded_send(ScanState::Started)
3675 .is_err()
3676 {
3677 return;
3678 }
3679
3680 let progress_update_count = AtomicUsize::new(0);
3681 self.executor
3682 .scoped(|scope| {
3683 for _ in 0..self.executor.num_cpus() {
3684 scope.spawn(async {
3685 let mut last_progress_update_count = 0;
3686 let progress_update_timer = self.progress_timer(enable_progress_updates).fuse();
3687 futures::pin_mut!(progress_update_timer);
3688
3689 loop {
3690 select_biased! {
3691 // Process any path refresh requests before moving on to process
3692 // the scan queue, so that user operations are prioritized.
3693 request = self.scan_requests_rx.recv().fuse() => {
3694 let Ok(request) = request else { break };
3695 if !self.process_scan_request(request, true).await {
3696 return;
3697 }
3698 }
3699
3700 // Send periodic progress updates to the worktree. Use an atomic counter
3701 // to ensure that only one of the workers sends a progress update after
3702 // the update interval elapses.
3703 _ = progress_update_timer => {
3704 match progress_update_count.compare_exchange(
3705 last_progress_update_count,
3706 last_progress_update_count + 1,
3707 SeqCst,
3708 SeqCst
3709 ) {
3710 Ok(_) => {
3711 last_progress_update_count += 1;
3712 self.send_status_update(true, None);
3713 }
3714 Err(count) => {
3715 last_progress_update_count = count;
3716 }
3717 }
3718 progress_update_timer.set(self.progress_timer(enable_progress_updates).fuse());
3719 }
3720
3721 // Recursively load directories from the file system.
3722 job = scan_jobs_rx.recv().fuse() => {
3723 let Ok(job) = job else { break };
3724 if let Err(err) = self.scan_dir(&job).await {
3725 if job.path.as_ref() != Path::new("") {
3726 log::error!("error scanning directory {:?}: {}", job.abs_path, err);
3727 }
3728 }
3729 }
3730 }
3731 }
3732 })
3733 }
3734 })
3735 .await;
3736 }
3737
3738 fn send_status_update(&self, scanning: bool, barrier: Option<barrier::Sender>) -> bool {
3739 let mut state = self.state.lock();
3740 if state.changed_paths.is_empty() && scanning {
3741 return true;
3742 }
3743
3744 let new_snapshot = state.snapshot.clone();
3745 let old_snapshot = mem::replace(&mut state.prev_snapshot, new_snapshot.snapshot.clone());
3746 let changes = self.build_change_set(&old_snapshot, &new_snapshot, &state.changed_paths);
3747 state.changed_paths.clear();
3748
3749 self.status_updates_tx
3750 .unbounded_send(ScanState::Updated {
3751 snapshot: new_snapshot,
3752 changes,
3753 scanning,
3754 barrier,
3755 })
3756 .is_ok()
3757 }
3758
3759 async fn scan_dir(&self, job: &ScanJob) -> Result<()> {
3760 let root_abs_path;
3761 let mut ignore_stack;
3762 let mut new_ignore;
3763 let root_char_bag;
3764 let next_entry_id;
3765 {
3766 let state = self.state.lock();
3767 let snapshot = &state.snapshot;
3768 root_abs_path = snapshot.abs_path().clone();
3769 if snapshot.is_path_excluded(job.path.to_path_buf()) {
3770 log::error!("skipping excluded directory {:?}", job.path);
3771 return Ok(());
3772 }
3773 log::debug!("scanning directory {:?}", job.path);
3774 ignore_stack = job.ignore_stack.clone();
3775 new_ignore = None;
3776 root_char_bag = snapshot.root_char_bag;
3777 next_entry_id = self.next_entry_id.clone();
3778 drop(state);
3779 }
3780
3781 let mut dotgit_path = None;
3782 let mut root_canonical_path = None;
3783 let mut new_entries: Vec<Entry> = Vec::new();
3784 let mut new_jobs: Vec<Option<ScanJob>> = Vec::new();
3785 let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
3786 while let Some(child_abs_path) = child_paths.next().await {
3787 let child_abs_path: Arc<Path> = match child_abs_path {
3788 Ok(child_abs_path) => child_abs_path.into(),
3789 Err(error) => {
3790 log::error!("error processing entry {:?}", error);
3791 continue;
3792 }
3793 };
3794 let child_name = child_abs_path.file_name().unwrap();
3795 let child_path: Arc<Path> = job.path.join(child_name).into();
3796 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
3797 if child_name == *GITIGNORE {
3798 match build_gitignore(&child_abs_path, self.fs.as_ref()).await {
3799 Ok(ignore) => {
3800 let ignore = Arc::new(ignore);
3801 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
3802 new_ignore = Some(ignore);
3803 }
3804 Err(error) => {
3805 log::error!(
3806 "error loading .gitignore file {:?} - {:?}",
3807 child_name,
3808 error
3809 );
3810 }
3811 }
3812
3813 // Update ignore status of any child entries we've already processed to reflect the
3814 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
3815 // there should rarely be too numerous. Update the ignore stack associated with any
3816 // new jobs as well.
3817 let mut new_jobs = new_jobs.iter_mut();
3818 for entry in &mut new_entries {
3819 let entry_abs_path = root_abs_path.join(&entry.path);
3820 entry.is_ignored =
3821 ignore_stack.is_abs_path_ignored(&entry_abs_path, entry.is_dir());
3822
3823 if entry.is_dir() {
3824 if let Some(job) = new_jobs.next().expect("missing scan job for entry") {
3825 job.ignore_stack = if entry.is_ignored {
3826 IgnoreStack::all()
3827 } else {
3828 ignore_stack.clone()
3829 };
3830 }
3831 }
3832 }
3833 }
3834 // If we find a .git, we'll need to load the repository.
3835 else if child_name == *DOT_GIT {
3836 dotgit_path = Some(child_path.clone());
3837 }
3838
3839 {
3840 let relative_path = job.path.join(child_name);
3841 let mut state = self.state.lock();
3842 if state.snapshot.is_path_excluded(relative_path.clone()) {
3843 log::debug!("skipping excluded child entry {relative_path:?}");
3844 state.remove_path(&relative_path);
3845 continue;
3846 }
3847 drop(state);
3848 }
3849
3850 let child_metadata = match self.fs.metadata(&child_abs_path).await {
3851 Ok(Some(metadata)) => metadata,
3852 Ok(None) => continue,
3853 Err(err) => {
3854 log::error!("error processing {child_abs_path:?}: {err:?}");
3855 continue;
3856 }
3857 };
3858
3859 let mut child_entry = Entry::new(
3860 child_path.clone(),
3861 &child_metadata,
3862 &next_entry_id,
3863 root_char_bag,
3864 );
3865
3866 if job.is_external {
3867 child_entry.is_external = true;
3868 } else if child_metadata.is_symlink {
3869 let canonical_path = match self.fs.canonicalize(&child_abs_path).await {
3870 Ok(path) => path,
3871 Err(err) => {
3872 log::error!(
3873 "error reading target of symlink {:?}: {:?}",
3874 child_abs_path,
3875 err
3876 );
3877 continue;
3878 }
3879 };
3880
3881 // lazily canonicalize the root path in order to determine if
3882 // symlinks point outside of the worktree.
3883 let root_canonical_path = match &root_canonical_path {
3884 Some(path) => path,
3885 None => match self.fs.canonicalize(&root_abs_path).await {
3886 Ok(path) => root_canonical_path.insert(path),
3887 Err(err) => {
3888 log::error!("error canonicalizing root {:?}: {:?}", root_abs_path, err);
3889 continue;
3890 }
3891 },
3892 };
3893
3894 if !canonical_path.starts_with(root_canonical_path) {
3895 child_entry.is_external = true;
3896 }
3897 }
3898
3899 if child_entry.is_dir() {
3900 child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true);
3901
3902 // Avoid recursing until crash in the case of a recursive symlink
3903 if !job.ancestor_inodes.contains(&child_entry.inode) {
3904 let mut ancestor_inodes = job.ancestor_inodes.clone();
3905 ancestor_inodes.insert(child_entry.inode);
3906
3907 new_jobs.push(Some(ScanJob {
3908 abs_path: child_abs_path.clone(),
3909 path: child_path,
3910 is_external: child_entry.is_external,
3911 ignore_stack: if child_entry.is_ignored {
3912 IgnoreStack::all()
3913 } else {
3914 ignore_stack.clone()
3915 },
3916 ancestor_inodes,
3917 scan_queue: job.scan_queue.clone(),
3918 containing_repository: job.containing_repository.clone(),
3919 }));
3920 } else {
3921 new_jobs.push(None);
3922 }
3923 } else {
3924 child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false);
3925 if !child_entry.is_ignored {
3926 if let Some((repository_dir, repository, staged_statuses)) =
3927 &job.containing_repository
3928 {
3929 if let Ok(repo_path) = child_entry.path.strip_prefix(&repository_dir.0) {
3930 if let Some(mtime) = child_entry.mtime {
3931 let repo_path = RepoPath(repo_path.into());
3932 child_entry.git_status = combine_git_statuses(
3933 staged_statuses.get(&repo_path).copied(),
3934 repository.lock().unstaged_status(&repo_path, mtime),
3935 );
3936 }
3937 }
3938 }
3939 }
3940 }
3941
3942 {
3943 let relative_path = job.path.join(child_name);
3944 let state = self.state.lock();
3945 if state.snapshot.is_path_private(&relative_path) {
3946 log::debug!("detected private file: {relative_path:?}");
3947 child_entry.is_private = true;
3948 }
3949 drop(state)
3950 }
3951
3952 new_entries.push(child_entry);
3953 }
3954
3955 let mut state = self.state.lock();
3956
3957 // Identify any subdirectories that should not be scanned.
3958 let mut job_ix = 0;
3959 for entry in &mut new_entries {
3960 state.reuse_entry_id(entry);
3961 if entry.is_dir() {
3962 if state.should_scan_directory(entry) {
3963 job_ix += 1;
3964 } else {
3965 log::debug!("defer scanning directory {:?}", entry.path);
3966 entry.kind = EntryKind::UnloadedDir;
3967 new_jobs.remove(job_ix);
3968 }
3969 }
3970 }
3971
3972 state.populate_dir(&job.path, new_entries, new_ignore);
3973
3974 let repository =
3975 dotgit_path.and_then(|path| state.build_git_repository(path, self.fs.as_ref()));
3976
3977 for mut new_job in new_jobs.into_iter().flatten() {
3978 if let Some(containing_repository) = &repository {
3979 new_job.containing_repository = Some(containing_repository.clone());
3980 }
3981
3982 job.scan_queue
3983 .try_send(new_job)
3984 .expect("channel is unbounded");
3985 }
3986
3987 Ok(())
3988 }
3989
3990 async fn reload_entries_for_paths(
3991 &self,
3992 root_abs_path: Arc<Path>,
3993 root_canonical_path: PathBuf,
3994 relative_paths: &[Arc<Path>],
3995 abs_paths: Vec<PathBuf>,
3996 scan_queue_tx: Option<Sender<ScanJob>>,
3997 ) {
3998 let metadata = futures::future::join_all(
3999 abs_paths
4000 .iter()
4001 .map(|abs_path| async move {
4002 let metadata = self.fs.metadata(abs_path).await?;
4003 if let Some(metadata) = metadata {
4004 let canonical_path = self.fs.canonicalize(abs_path).await?;
4005
4006 // If we're on a case-insensitive filesystem (default on macOS), we want
4007 // to only ignore metadata for non-symlink files if their absolute-path matches
4008 // the canonical-path.
4009 // Because if not, this might be a case-only-renaming (`mv test.txt TEST.TXT`)
4010 // and we want to ignore the metadata for the old path (`test.txt`) so it's
4011 // treated as removed.
4012 if !self.fs_case_sensitive && !metadata.is_symlink {
4013 let canonical_file_name = canonical_path.file_name();
4014 let file_name = abs_path.file_name();
4015 if canonical_file_name != file_name {
4016 return Ok(None);
4017 }
4018 }
4019
4020 anyhow::Ok(Some((metadata, canonical_path)))
4021 } else {
4022 Ok(None)
4023 }
4024 })
4025 .collect::<Vec<_>>(),
4026 )
4027 .await;
4028
4029 let mut state = self.state.lock();
4030 let snapshot = &mut state.snapshot;
4031 let is_idle = snapshot.completed_scan_id == snapshot.scan_id;
4032 let doing_recursive_update = scan_queue_tx.is_some();
4033 snapshot.scan_id += 1;
4034 if is_idle && !doing_recursive_update {
4035 snapshot.completed_scan_id = snapshot.scan_id;
4036 }
4037
4038 // Remove any entries for paths that no longer exist or are being recursively
4039 // refreshed. Do this before adding any new entries, so that renames can be
4040 // detected regardless of the order of the paths.
4041 for (path, metadata) in relative_paths.iter().zip(metadata.iter()) {
4042 if matches!(metadata, Ok(None)) || doing_recursive_update {
4043 log::trace!("remove path {:?}", path);
4044 state.remove_path(path);
4045 }
4046 }
4047
4048 for (path, metadata) in relative_paths.iter().zip(metadata.iter()) {
4049 let abs_path: Arc<Path> = root_abs_path.join(&path).into();
4050 match metadata {
4051 Ok(Some((metadata, canonical_path))) => {
4052 let ignore_stack = state
4053 .snapshot
4054 .ignore_stack_for_abs_path(&abs_path, metadata.is_dir);
4055
4056 let mut fs_entry = Entry::new(
4057 path.clone(),
4058 metadata,
4059 self.next_entry_id.as_ref(),
4060 state.snapshot.root_char_bag,
4061 );
4062 let is_dir = fs_entry.is_dir();
4063 fs_entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, is_dir);
4064 fs_entry.is_external = !canonical_path.starts_with(&root_canonical_path);
4065 fs_entry.is_private = state.snapshot.is_path_private(path);
4066
4067 if !is_dir && !fs_entry.is_ignored && !fs_entry.is_external {
4068 if let Some((work_dir, repo)) = state.snapshot.local_repo_for_path(path) {
4069 if let Ok(repo_path) = path.strip_prefix(work_dir.0) {
4070 if let Some(mtime) = fs_entry.mtime {
4071 let repo_path = RepoPath(repo_path.into());
4072 let repo = repo.repo_ptr.lock();
4073 fs_entry.git_status = repo.status(&repo_path, mtime);
4074 }
4075 }
4076 }
4077 }
4078
4079 if let (Some(scan_queue_tx), true) = (&scan_queue_tx, fs_entry.is_dir()) {
4080 if state.should_scan_directory(&fs_entry) {
4081 state.enqueue_scan_dir(abs_path, &fs_entry, scan_queue_tx);
4082 } else {
4083 fs_entry.kind = EntryKind::UnloadedDir;
4084 }
4085 }
4086
4087 state.insert_entry(fs_entry, self.fs.as_ref());
4088 }
4089 Ok(None) => {
4090 self.remove_repo_path(path, &mut state.snapshot);
4091 }
4092 Err(err) => {
4093 // TODO - create a special 'error' entry in the entries tree to mark this
4094 log::error!("error reading file {abs_path:?} on event: {err:#}");
4095 }
4096 }
4097 }
4098
4099 util::extend_sorted(
4100 &mut state.changed_paths,
4101 relative_paths.iter().cloned(),
4102 usize::MAX,
4103 Ord::cmp,
4104 );
4105 }
4106
4107 fn remove_repo_path(&self, path: &Path, snapshot: &mut LocalSnapshot) -> Option<()> {
4108 if !path
4109 .components()
4110 .any(|component| component.as_os_str() == *DOT_GIT)
4111 {
4112 if let Some(repository) = snapshot.repository_for_work_directory(path) {
4113 let entry = repository.work_directory.0;
4114 snapshot.git_repositories.remove(&entry);
4115 snapshot
4116 .snapshot
4117 .repository_entries
4118 .remove(&RepositoryWorkDirectory(path.into()));
4119 return Some(());
4120 }
4121 }
4122
4123 // TODO statuses
4124 // Track when a .git is removed and iterate over the file system there
4125
4126 Some(())
4127 }
4128
4129 async fn update_ignore_statuses(&self, scan_job_tx: Sender<ScanJob>) {
4130 use futures::FutureExt as _;
4131
4132 let mut snapshot = self.state.lock().snapshot.clone();
4133 let mut ignores_to_update = Vec::new();
4134 let mut ignores_to_delete = Vec::new();
4135 let abs_path = snapshot.abs_path.clone();
4136 for (parent_abs_path, (_, needs_update)) in &mut snapshot.ignores_by_parent_abs_path {
4137 if let Ok(parent_path) = parent_abs_path.strip_prefix(&abs_path) {
4138 if *needs_update {
4139 *needs_update = false;
4140 if snapshot.snapshot.entry_for_path(parent_path).is_some() {
4141 ignores_to_update.push(parent_abs_path.clone());
4142 }
4143 }
4144
4145 let ignore_path = parent_path.join(&*GITIGNORE);
4146 if snapshot.snapshot.entry_for_path(ignore_path).is_none() {
4147 ignores_to_delete.push(parent_abs_path.clone());
4148 }
4149 }
4150 }
4151
4152 for parent_abs_path in ignores_to_delete {
4153 snapshot.ignores_by_parent_abs_path.remove(&parent_abs_path);
4154 self.state
4155 .lock()
4156 .snapshot
4157 .ignores_by_parent_abs_path
4158 .remove(&parent_abs_path);
4159 }
4160
4161 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
4162 ignores_to_update.sort_unstable();
4163 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
4164 while let Some(parent_abs_path) = ignores_to_update.next() {
4165 while ignores_to_update
4166 .peek()
4167 .map_or(false, |p| p.starts_with(&parent_abs_path))
4168 {
4169 ignores_to_update.next().unwrap();
4170 }
4171
4172 let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true);
4173 smol::block_on(ignore_queue_tx.send(UpdateIgnoreStatusJob {
4174 abs_path: parent_abs_path,
4175 ignore_stack,
4176 ignore_queue: ignore_queue_tx.clone(),
4177 scan_queue: scan_job_tx.clone(),
4178 }))
4179 .unwrap();
4180 }
4181 drop(ignore_queue_tx);
4182
4183 self.executor
4184 .scoped(|scope| {
4185 for _ in 0..self.executor.num_cpus() {
4186 scope.spawn(async {
4187 loop {
4188 select_biased! {
4189 // Process any path refresh requests before moving on to process
4190 // the queue of ignore statuses.
4191 request = self.scan_requests_rx.recv().fuse() => {
4192 let Ok(request) = request else { break };
4193 if !self.process_scan_request(request, true).await {
4194 return;
4195 }
4196 }
4197
4198 // Recursively process directories whose ignores have changed.
4199 job = ignore_queue_rx.recv().fuse() => {
4200 let Ok(job) = job else { break };
4201 self.update_ignore_status(job, &snapshot).await;
4202 }
4203 }
4204 }
4205 });
4206 }
4207 })
4208 .await;
4209 }
4210
4211 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
4212 log::trace!("update ignore status {:?}", job.abs_path);
4213
4214 let mut ignore_stack = job.ignore_stack;
4215 if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) {
4216 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
4217 }
4218
4219 let mut entries_by_id_edits = Vec::new();
4220 let mut entries_by_path_edits = Vec::new();
4221 let path = job.abs_path.strip_prefix(&snapshot.abs_path).unwrap();
4222 for mut entry in snapshot.child_entries(path).cloned() {
4223 let was_ignored = entry.is_ignored;
4224 let abs_path: Arc<Path> = snapshot.abs_path().join(&entry.path).into();
4225 entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir());
4226 if entry.is_dir() {
4227 let child_ignore_stack = if entry.is_ignored {
4228 IgnoreStack::all()
4229 } else {
4230 ignore_stack.clone()
4231 };
4232
4233 // Scan any directories that were previously ignored and weren't previously scanned.
4234 if was_ignored && !entry.is_ignored && entry.kind.is_unloaded() {
4235 let state = self.state.lock();
4236 if state.should_scan_directory(&entry) {
4237 state.enqueue_scan_dir(abs_path.clone(), &entry, &job.scan_queue);
4238 }
4239 }
4240
4241 job.ignore_queue
4242 .send(UpdateIgnoreStatusJob {
4243 abs_path: abs_path.clone(),
4244 ignore_stack: child_ignore_stack,
4245 ignore_queue: job.ignore_queue.clone(),
4246 scan_queue: job.scan_queue.clone(),
4247 })
4248 .await
4249 .unwrap();
4250 }
4251
4252 if entry.is_ignored != was_ignored {
4253 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
4254 path_entry.scan_id = snapshot.scan_id;
4255 path_entry.is_ignored = entry.is_ignored;
4256 entries_by_id_edits.push(Edit::Insert(path_entry));
4257 entries_by_path_edits.push(Edit::Insert(entry));
4258 }
4259 }
4260
4261 let state = &mut self.state.lock();
4262 for edit in &entries_by_path_edits {
4263 if let Edit::Insert(entry) = edit {
4264 if let Err(ix) = state.changed_paths.binary_search(&entry.path) {
4265 state.changed_paths.insert(ix, entry.path.clone());
4266 }
4267 }
4268 }
4269
4270 state
4271 .snapshot
4272 .entries_by_path
4273 .edit(entries_by_path_edits, &());
4274 state.snapshot.entries_by_id.edit(entries_by_id_edits, &());
4275 }
4276
4277 fn build_change_set(
4278 &self,
4279 old_snapshot: &Snapshot,
4280 new_snapshot: &Snapshot,
4281 event_paths: &[Arc<Path>],
4282 ) -> UpdatedEntriesSet {
4283 use BackgroundScannerPhase::*;
4284 use PathChange::{Added, AddedOrUpdated, Loaded, Removed, Updated};
4285
4286 // Identify which paths have changed. Use the known set of changed
4287 // parent paths to optimize the search.
4288 let mut changes = Vec::new();
4289 let mut old_paths = old_snapshot.entries_by_path.cursor::<PathKey>();
4290 let mut new_paths = new_snapshot.entries_by_path.cursor::<PathKey>();
4291 let mut last_newly_loaded_dir_path = None;
4292 old_paths.next(&());
4293 new_paths.next(&());
4294 for path in event_paths {
4295 let path = PathKey(path.clone());
4296 if old_paths.item().map_or(false, |e| e.path < path.0) {
4297 old_paths.seek_forward(&path, Bias::Left, &());
4298 }
4299 if new_paths.item().map_or(false, |e| e.path < path.0) {
4300 new_paths.seek_forward(&path, Bias::Left, &());
4301 }
4302 loop {
4303 match (old_paths.item(), new_paths.item()) {
4304 (Some(old_entry), Some(new_entry)) => {
4305 if old_entry.path > path.0
4306 && new_entry.path > path.0
4307 && !old_entry.path.starts_with(&path.0)
4308 && !new_entry.path.starts_with(&path.0)
4309 {
4310 break;
4311 }
4312
4313 match Ord::cmp(&old_entry.path, &new_entry.path) {
4314 Ordering::Less => {
4315 changes.push((old_entry.path.clone(), old_entry.id, Removed));
4316 old_paths.next(&());
4317 }
4318 Ordering::Equal => {
4319 if self.phase == EventsReceivedDuringInitialScan {
4320 if old_entry.id != new_entry.id {
4321 changes.push((
4322 old_entry.path.clone(),
4323 old_entry.id,
4324 Removed,
4325 ));
4326 }
4327 // If the worktree was not fully initialized when this event was generated,
4328 // we can't know whether this entry was added during the scan or whether
4329 // it was merely updated.
4330 changes.push((
4331 new_entry.path.clone(),
4332 new_entry.id,
4333 AddedOrUpdated,
4334 ));
4335 } else if old_entry.id != new_entry.id {
4336 changes.push((old_entry.path.clone(), old_entry.id, Removed));
4337 changes.push((new_entry.path.clone(), new_entry.id, Added));
4338 } else if old_entry != new_entry {
4339 if old_entry.kind.is_unloaded() {
4340 last_newly_loaded_dir_path = Some(&new_entry.path);
4341 changes.push((
4342 new_entry.path.clone(),
4343 new_entry.id,
4344 Loaded,
4345 ));
4346 } else {
4347 changes.push((
4348 new_entry.path.clone(),
4349 new_entry.id,
4350 Updated,
4351 ));
4352 }
4353 }
4354 old_paths.next(&());
4355 new_paths.next(&());
4356 }
4357 Ordering::Greater => {
4358 let is_newly_loaded = self.phase == InitialScan
4359 || last_newly_loaded_dir_path
4360 .as_ref()
4361 .map_or(false, |dir| new_entry.path.starts_with(&dir));
4362 changes.push((
4363 new_entry.path.clone(),
4364 new_entry.id,
4365 if is_newly_loaded { Loaded } else { Added },
4366 ));
4367 new_paths.next(&());
4368 }
4369 }
4370 }
4371 (Some(old_entry), None) => {
4372 changes.push((old_entry.path.clone(), old_entry.id, Removed));
4373 old_paths.next(&());
4374 }
4375 (None, Some(new_entry)) => {
4376 let is_newly_loaded = self.phase == InitialScan
4377 || last_newly_loaded_dir_path
4378 .as_ref()
4379 .map_or(false, |dir| new_entry.path.starts_with(&dir));
4380 changes.push((
4381 new_entry.path.clone(),
4382 new_entry.id,
4383 if is_newly_loaded { Loaded } else { Added },
4384 ));
4385 new_paths.next(&());
4386 }
4387 (None, None) => break,
4388 }
4389 }
4390 }
4391
4392 changes.into()
4393 }
4394
4395 async fn progress_timer(&self, running: bool) {
4396 if !running {
4397 return futures::future::pending().await;
4398 }
4399
4400 #[cfg(any(test, feature = "test-support"))]
4401 if self.fs.is_fake() {
4402 return self.executor.simulate_random_delay().await;
4403 }
4404
4405 smol::Timer::after(FS_WATCH_LATENCY).await;
4406 }
4407}
4408
4409fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
4410 let mut result = root_char_bag;
4411 result.extend(
4412 path.to_string_lossy()
4413 .chars()
4414 .map(|c| c.to_ascii_lowercase()),
4415 );
4416 result
4417}
4418
4419struct ScanJob {
4420 abs_path: Arc<Path>,
4421 path: Arc<Path>,
4422 ignore_stack: Arc<IgnoreStack>,
4423 scan_queue: Sender<ScanJob>,
4424 ancestor_inodes: TreeSet<u64>,
4425 is_external: bool,
4426 containing_repository: Option<(
4427 RepositoryWorkDirectory,
4428 Arc<Mutex<dyn GitRepository>>,
4429 TreeMap<RepoPath, GitFileStatus>,
4430 )>,
4431}
4432
4433struct UpdateIgnoreStatusJob {
4434 abs_path: Arc<Path>,
4435 ignore_stack: Arc<IgnoreStack>,
4436 ignore_queue: Sender<UpdateIgnoreStatusJob>,
4437 scan_queue: Sender<ScanJob>,
4438}
4439
4440pub trait WorktreeModelHandle {
4441 #[cfg(any(test, feature = "test-support"))]
4442 fn flush_fs_events<'a>(
4443 &self,
4444 cx: &'a mut gpui::TestAppContext,
4445 ) -> futures::future::LocalBoxFuture<'a, ()>;
4446}
4447
4448impl WorktreeModelHandle for Model<Worktree> {
4449 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
4450 // occurred before the worktree was constructed. These events can cause the worktree to perform
4451 // extra directory scans, and emit extra scan-state notifications.
4452 //
4453 // This function mutates the worktree's directory and waits for those mutations to be picked up,
4454 // to ensure that all redundant FS events have already been processed.
4455 #[cfg(any(test, feature = "test-support"))]
4456 fn flush_fs_events<'a>(
4457 &self,
4458 cx: &'a mut gpui::TestAppContext,
4459 ) -> futures::future::LocalBoxFuture<'a, ()> {
4460 let file_name = "fs-event-sentinel";
4461
4462 let tree = self.clone();
4463 let (fs, root_path) = self.update(cx, |tree, _| {
4464 let tree = tree.as_local().unwrap();
4465 (tree.fs.clone(), tree.abs_path().clone())
4466 });
4467
4468 async move {
4469 fs.create_file(&root_path.join(file_name), Default::default())
4470 .await
4471 .unwrap();
4472
4473 cx.condition(&tree, |tree, _| tree.entry_for_path(file_name).is_some())
4474 .await;
4475
4476 fs.remove_file(&root_path.join(file_name), Default::default())
4477 .await
4478 .unwrap();
4479 cx.condition(&tree, |tree, _| tree.entry_for_path(file_name).is_none())
4480 .await;
4481
4482 cx.update(|cx| tree.read(cx).as_local().unwrap().scan_complete())
4483 .await;
4484 }
4485 .boxed_local()
4486 }
4487}
4488
4489#[derive(Clone, Debug)]
4490struct TraversalProgress<'a> {
4491 max_path: &'a Path,
4492 count: usize,
4493 non_ignored_count: usize,
4494 file_count: usize,
4495 non_ignored_file_count: usize,
4496}
4497
4498impl<'a> TraversalProgress<'a> {
4499 fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
4500 match (include_ignored, include_dirs) {
4501 (true, true) => self.count,
4502 (true, false) => self.file_count,
4503 (false, true) => self.non_ignored_count,
4504 (false, false) => self.non_ignored_file_count,
4505 }
4506 }
4507}
4508
4509impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
4510 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
4511 self.max_path = summary.max_path.as_ref();
4512 self.count += summary.count;
4513 self.non_ignored_count += summary.non_ignored_count;
4514 self.file_count += summary.file_count;
4515 self.non_ignored_file_count += summary.non_ignored_file_count;
4516 }
4517}
4518
4519impl<'a> Default for TraversalProgress<'a> {
4520 fn default() -> Self {
4521 Self {
4522 max_path: Path::new(""),
4523 count: 0,
4524 non_ignored_count: 0,
4525 file_count: 0,
4526 non_ignored_file_count: 0,
4527 }
4528 }
4529}
4530
4531#[derive(Clone, Debug, Default, Copy)]
4532struct GitStatuses {
4533 added: usize,
4534 modified: usize,
4535 conflict: usize,
4536}
4537
4538impl AddAssign for GitStatuses {
4539 fn add_assign(&mut self, rhs: Self) {
4540 self.added += rhs.added;
4541 self.modified += rhs.modified;
4542 self.conflict += rhs.conflict;
4543 }
4544}
4545
4546impl Sub for GitStatuses {
4547 type Output = GitStatuses;
4548
4549 fn sub(self, rhs: Self) -> Self::Output {
4550 GitStatuses {
4551 added: self.added - rhs.added,
4552 modified: self.modified - rhs.modified,
4553 conflict: self.conflict - rhs.conflict,
4554 }
4555 }
4556}
4557
4558impl<'a> sum_tree::Dimension<'a, EntrySummary> for GitStatuses {
4559 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
4560 *self += summary.statuses
4561 }
4562}
4563
4564pub struct Traversal<'a> {
4565 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
4566 include_ignored: bool,
4567 include_dirs: bool,
4568}
4569
4570impl<'a> Traversal<'a> {
4571 pub fn advance(&mut self) -> bool {
4572 self.cursor.seek_forward(
4573 &TraversalTarget::Count {
4574 count: self.end_offset() + 1,
4575 include_dirs: self.include_dirs,
4576 include_ignored: self.include_ignored,
4577 },
4578 Bias::Left,
4579 &(),
4580 )
4581 }
4582
4583 pub fn advance_to_sibling(&mut self) -> bool {
4584 while let Some(entry) = self.cursor.item() {
4585 self.cursor.seek_forward(
4586 &TraversalTarget::PathSuccessor(&entry.path),
4587 Bias::Left,
4588 &(),
4589 );
4590 if let Some(entry) = self.cursor.item() {
4591 if (self.include_dirs || !entry.is_dir())
4592 && (self.include_ignored || !entry.is_ignored)
4593 {
4594 return true;
4595 }
4596 }
4597 }
4598 false
4599 }
4600
4601 pub fn entry(&self) -> Option<&'a Entry> {
4602 self.cursor.item()
4603 }
4604
4605 pub fn start_offset(&self) -> usize {
4606 self.cursor
4607 .start()
4608 .count(self.include_dirs, self.include_ignored)
4609 }
4610
4611 pub fn end_offset(&self) -> usize {
4612 self.cursor
4613 .end(&())
4614 .count(self.include_dirs, self.include_ignored)
4615 }
4616}
4617
4618impl<'a> Iterator for Traversal<'a> {
4619 type Item = &'a Entry;
4620
4621 fn next(&mut self) -> Option<Self::Item> {
4622 if let Some(item) = self.entry() {
4623 self.advance();
4624 Some(item)
4625 } else {
4626 None
4627 }
4628 }
4629}
4630
4631#[derive(Debug)]
4632enum TraversalTarget<'a> {
4633 Path(&'a Path),
4634 PathSuccessor(&'a Path),
4635 Count {
4636 count: usize,
4637 include_ignored: bool,
4638 include_dirs: bool,
4639 },
4640}
4641
4642impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
4643 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
4644 match self {
4645 TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
4646 TraversalTarget::PathSuccessor(path) => {
4647 if !cursor_location.max_path.starts_with(path) {
4648 Ordering::Equal
4649 } else {
4650 Ordering::Greater
4651 }
4652 }
4653 TraversalTarget::Count {
4654 count,
4655 include_dirs,
4656 include_ignored,
4657 } => Ord::cmp(
4658 count,
4659 &cursor_location.count(*include_dirs, *include_ignored),
4660 ),
4661 }
4662 }
4663}
4664
4665impl<'a, 'b> SeekTarget<'a, EntrySummary, (TraversalProgress<'a>, GitStatuses)>
4666 for TraversalTarget<'b>
4667{
4668 fn cmp(&self, cursor_location: &(TraversalProgress<'a>, GitStatuses), _: &()) -> Ordering {
4669 self.cmp(&cursor_location.0, &())
4670 }
4671}
4672
4673struct ChildEntriesIter<'a> {
4674 parent_path: &'a Path,
4675 traversal: Traversal<'a>,
4676}
4677
4678impl<'a> Iterator for ChildEntriesIter<'a> {
4679 type Item = &'a Entry;
4680
4681 fn next(&mut self) -> Option<Self::Item> {
4682 if let Some(item) = self.traversal.entry() {
4683 if item.path.starts_with(&self.parent_path) {
4684 self.traversal.advance_to_sibling();
4685 return Some(item);
4686 }
4687 }
4688 None
4689 }
4690}
4691
4692pub struct DescendentEntriesIter<'a> {
4693 parent_path: &'a Path,
4694 traversal: Traversal<'a>,
4695}
4696
4697impl<'a> Iterator for DescendentEntriesIter<'a> {
4698 type Item = &'a Entry;
4699
4700 fn next(&mut self) -> Option<Self::Item> {
4701 if let Some(item) = self.traversal.entry() {
4702 if item.path.starts_with(&self.parent_path) {
4703 self.traversal.advance();
4704 return Some(item);
4705 }
4706 }
4707 None
4708 }
4709}
4710
4711impl<'a> From<&'a Entry> for proto::Entry {
4712 fn from(entry: &'a Entry) -> Self {
4713 Self {
4714 id: entry.id.to_proto(),
4715 is_dir: entry.is_dir(),
4716 path: entry.path.to_string_lossy().into(),
4717 inode: entry.inode,
4718 mtime: entry.mtime.map(|time| time.into()),
4719 is_symlink: entry.is_symlink,
4720 is_ignored: entry.is_ignored,
4721 is_external: entry.is_external,
4722 git_status: entry.git_status.map(git_status_to_proto),
4723 }
4724 }
4725}
4726
4727impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
4728 type Error = anyhow::Error;
4729
4730 fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
4731 let kind = if entry.is_dir {
4732 EntryKind::Dir
4733 } else {
4734 let mut char_bag = *root_char_bag;
4735 char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase()));
4736 EntryKind::File(char_bag)
4737 };
4738 let path: Arc<Path> = PathBuf::from(entry.path).into();
4739 Ok(Entry {
4740 id: ProjectEntryId::from_proto(entry.id),
4741 kind,
4742 path,
4743 inode: entry.inode,
4744 mtime: entry.mtime.map(|time| time.into()),
4745 is_symlink: entry.is_symlink,
4746 is_ignored: entry.is_ignored,
4747 is_external: entry.is_external,
4748 git_status: git_status_from_proto(entry.git_status),
4749 is_private: false,
4750 })
4751 }
4752}
4753
4754fn combine_git_statuses(
4755 staged: Option<GitFileStatus>,
4756 unstaged: Option<GitFileStatus>,
4757) -> Option<GitFileStatus> {
4758 if let Some(staged) = staged {
4759 if let Some(unstaged) = unstaged {
4760 if unstaged != staged {
4761 Some(GitFileStatus::Modified)
4762 } else {
4763 Some(staged)
4764 }
4765 } else {
4766 Some(staged)
4767 }
4768 } else {
4769 unstaged
4770 }
4771}
4772
4773fn git_status_from_proto(git_status: Option<i32>) -> Option<GitFileStatus> {
4774 git_status.and_then(|status| {
4775 proto::GitStatus::from_i32(status).map(|status| match status {
4776 proto::GitStatus::Added => GitFileStatus::Added,
4777 proto::GitStatus::Modified => GitFileStatus::Modified,
4778 proto::GitStatus::Conflict => GitFileStatus::Conflict,
4779 })
4780 })
4781}
4782
4783fn git_status_to_proto(status: GitFileStatus) -> i32 {
4784 match status {
4785 GitFileStatus::Added => proto::GitStatus::Added as i32,
4786 GitFileStatus::Modified => proto::GitStatus::Modified as i32,
4787 GitFileStatus::Conflict => proto::GitStatus::Conflict as i32,
4788 }
4789}
4790
4791#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
4792pub struct ProjectEntryId(usize);
4793
4794impl ProjectEntryId {
4795 pub const MAX: Self = Self(usize::MAX);
4796
4797 pub fn new(counter: &AtomicUsize) -> Self {
4798 Self(counter.fetch_add(1, SeqCst))
4799 }
4800
4801 pub fn from_proto(id: u64) -> Self {
4802 Self(id as usize)
4803 }
4804
4805 pub fn to_proto(&self) -> u64 {
4806 self.0 as u64
4807 }
4808
4809 pub fn to_usize(&self) -> usize {
4810 self.0
4811 }
4812}
4813
4814#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize)]
4815pub struct DiagnosticSummary {
4816 pub error_count: usize,
4817 pub warning_count: usize,
4818}
4819
4820impl DiagnosticSummary {
4821 fn new<'a, T: 'a>(diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<T>>) -> Self {
4822 let mut this = Self {
4823 error_count: 0,
4824 warning_count: 0,
4825 };
4826
4827 for entry in diagnostics {
4828 if entry.diagnostic.is_primary {
4829 match entry.diagnostic.severity {
4830 DiagnosticSeverity::ERROR => this.error_count += 1,
4831 DiagnosticSeverity::WARNING => this.warning_count += 1,
4832 _ => {}
4833 }
4834 }
4835 }
4836
4837 this
4838 }
4839
4840 pub fn is_empty(&self) -> bool {
4841 self.error_count == 0 && self.warning_count == 0
4842 }
4843
4844 pub fn to_proto(
4845 &self,
4846 language_server_id: LanguageServerId,
4847 path: &Path,
4848 ) -> proto::DiagnosticSummary {
4849 proto::DiagnosticSummary {
4850 path: path.to_string_lossy().to_string(),
4851 language_server_id: language_server_id.0 as u64,
4852 error_count: self.error_count as u32,
4853 warning_count: self.warning_count as u32,
4854 }
4855 }
4856}