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