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