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