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