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