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