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