1use crate::ProjectEntryId;
2
3use super::{
4 fs::{self, Fs},
5 ignore::IgnoreStack,
6 DiagnosticSummary,
7};
8use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
9use anyhow::{anyhow, Context, Result};
10use client::{proto, Client, TypedEnvelope};
11use clock::ReplicaId;
12use collections::HashMap;
13use futures::{
14 channel::{
15 mpsc::{self, UnboundedSender},
16 oneshot,
17 },
18 Stream, StreamExt,
19};
20use fuzzy::CharBag;
21use gpui::{
22 executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
23 Task,
24};
25use language::{
26 proto::{deserialize_version, serialize_version},
27 Buffer, DiagnosticEntry, PointUtf16, Rope,
28};
29use lazy_static::lazy_static;
30use parking_lot::Mutex;
31use postage::{
32 prelude::{Sink as _, Stream as _},
33 watch,
34};
35use serde::Deserialize;
36use smol::channel::{self, Sender};
37use std::{
38 any::Any,
39 cmp::{self, Ordering},
40 convert::TryFrom,
41 ffi::{OsStr, OsString},
42 fmt,
43 future::Future,
44 ops::{Deref, DerefMut},
45 os::unix::prelude::{OsStrExt, OsStringExt},
46 path::{Path, PathBuf},
47 sync::{atomic::AtomicUsize, Arc},
48 time::{Duration, SystemTime},
49};
50use sum_tree::{Bias, Edit, SeekTarget, SumTree, TreeMap};
51use util::{ResultExt, TryFutureExt};
52
53lazy_static! {
54 static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
55}
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 config: WorktreeConfig,
68 background_snapshot: Arc<Mutex<LocalSnapshot>>,
69 last_scan_state_rx: watch::Receiver<ScanState>,
70 _background_scanner_task: Option<Task<()>>,
71 poll_task: Option<Task<()>>,
72 registration: Registration,
73 share: Option<ShareState>,
74 diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
75 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
76 client: Arc<Client>,
77 fs: Arc<dyn Fs>,
78 visible: bool,
79}
80
81pub struct RemoteWorktree {
82 pub(crate) snapshot: Snapshot,
83 project_id: u64,
84 snapshot_rx: watch::Receiver<Snapshot>,
85 client: Arc<Client>,
86 updates_tx: UnboundedSender<proto::UpdateWorktree>,
87 replica_id: ReplicaId,
88 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
89 visible: bool,
90}
91
92#[derive(Clone)]
93pub struct Snapshot {
94 id: WorktreeId,
95 root_name: String,
96 root_char_bag: CharBag,
97 entries_by_path: SumTree<Entry>,
98 entries_by_id: SumTree<PathEntry>,
99}
100
101#[derive(Clone)]
102pub struct LocalSnapshot {
103 abs_path: Arc<Path>,
104 scan_id: usize,
105 ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
106 removed_entry_ids: HashMap<u64, ProjectEntryId>,
107 next_entry_id: Arc<AtomicUsize>,
108 snapshot: Snapshot,
109}
110
111impl Deref for LocalSnapshot {
112 type Target = Snapshot;
113
114 fn deref(&self) -> &Self::Target {
115 &self.snapshot
116 }
117}
118
119impl DerefMut for LocalSnapshot {
120 fn deref_mut(&mut self) -> &mut Self::Target {
121 &mut self.snapshot
122 }
123}
124
125#[derive(Clone, Debug)]
126enum ScanState {
127 Idle,
128 Scanning,
129 Err(Arc<anyhow::Error>),
130}
131
132#[derive(Debug, Eq, PartialEq)]
133enum Registration {
134 None,
135 Pending,
136 Done { project_id: u64 },
137}
138
139struct ShareState {
140 project_id: u64,
141 snapshots_tx: Sender<LocalSnapshot>,
142 _maintain_remote_snapshot: Option<Task<Option<()>>>,
143}
144
145#[derive(Default, Deserialize)]
146struct WorktreeConfig {
147 collaborators: Vec<String>,
148}
149
150pub enum Event {
151 UpdatedEntries,
152}
153
154impl Entity for Worktree {
155 type Event = Event;
156
157 fn release(&mut self, _: &mut MutableAppContext) {
158 if let Some(worktree) = self.as_local_mut() {
159 if let Registration::Done { project_id } = worktree.registration {
160 let client = worktree.client.clone();
161 let unregister_message = proto::UnregisterWorktree {
162 project_id,
163 worktree_id: worktree.id().to_proto(),
164 };
165 client.send(unregister_message).log_err();
166 }
167 }
168 }
169}
170
171impl Worktree {
172 pub async fn local(
173 client: Arc<Client>,
174 path: impl Into<Arc<Path>>,
175 visible: bool,
176 fs: Arc<dyn Fs>,
177 next_entry_id: Arc<AtomicUsize>,
178 cx: &mut AsyncAppContext,
179 ) -> Result<ModelHandle<Self>> {
180 let (tree, scan_states_tx) =
181 LocalWorktree::new(client, path, visible, fs.clone(), next_entry_id, cx).await?;
182 tree.update(cx, |tree, cx| {
183 let tree = tree.as_local_mut().unwrap();
184 let abs_path = tree.abs_path().clone();
185 let background_snapshot = tree.background_snapshot.clone();
186 let background = cx.background().clone();
187 tree._background_scanner_task = Some(cx.background().spawn(async move {
188 let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
189 let scanner =
190 BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
191 scanner.run(events).await;
192 }));
193 });
194 Ok(tree)
195 }
196
197 pub fn remote(
198 project_remote_id: u64,
199 replica_id: ReplicaId,
200 worktree: proto::Worktree,
201 client: Arc<Client>,
202 cx: &mut MutableAppContext,
203 ) -> (ModelHandle<Self>, Task<()>) {
204 let remote_id = worktree.id;
205 let root_char_bag: CharBag = worktree
206 .root_name
207 .chars()
208 .map(|c| c.to_ascii_lowercase())
209 .collect();
210 let root_name = worktree.root_name.clone();
211 let visible = worktree.visible;
212 let snapshot = Snapshot {
213 id: WorktreeId(remote_id as usize),
214 root_name,
215 root_char_bag,
216 entries_by_path: Default::default(),
217 entries_by_id: Default::default(),
218 };
219
220 let (updates_tx, mut updates_rx) = mpsc::unbounded();
221 let (mut snapshot_tx, snapshot_rx) = watch::channel_with(snapshot.clone());
222 let worktree_handle = cx.add_model(|_: &mut ModelContext<Worktree>| {
223 Worktree::Remote(RemoteWorktree {
224 project_id: project_remote_id,
225 replica_id,
226 snapshot: snapshot.clone(),
227 snapshot_rx: snapshot_rx.clone(),
228 updates_tx,
229 client: client.clone(),
230 diagnostic_summaries: TreeMap::from_ordered_entries(
231 worktree.diagnostic_summaries.into_iter().map(|summary| {
232 (
233 PathKey(PathBuf::from(summary.path).into()),
234 DiagnosticSummary {
235 error_count: summary.error_count as usize,
236 warning_count: summary.warning_count as usize,
237 },
238 )
239 }),
240 ),
241 visible,
242 })
243 });
244
245 let deserialize_task = cx.spawn({
246 let worktree_handle = worktree_handle.clone();
247 |cx| async move {
248 let (entries_by_path, entries_by_id) = cx
249 .background()
250 .spawn(async move {
251 let mut entries_by_path_edits = Vec::new();
252 let mut entries_by_id_edits = Vec::new();
253 for entry in worktree.entries {
254 match Entry::try_from((&root_char_bag, entry)) {
255 Ok(entry) => {
256 entries_by_id_edits.push(Edit::Insert(PathEntry {
257 id: entry.id,
258 path: entry.path.clone(),
259 is_ignored: entry.is_ignored,
260 scan_id: 0,
261 }));
262 entries_by_path_edits.push(Edit::Insert(entry));
263 }
264 Err(err) => log::warn!("error for remote worktree entry {:?}", err),
265 }
266 }
267
268 let mut entries_by_path = SumTree::new();
269 let mut entries_by_id = SumTree::new();
270 entries_by_path.edit(entries_by_path_edits, &());
271 entries_by_id.edit(entries_by_id_edits, &());
272
273 (entries_by_path, entries_by_id)
274 })
275 .await;
276
277 {
278 let mut snapshot = snapshot_tx.borrow_mut();
279 snapshot.entries_by_path = entries_by_path;
280 snapshot.entries_by_id = entries_by_id;
281 }
282
283 cx.background()
284 .spawn(async move {
285 while let Some(update) = updates_rx.next().await {
286 let mut snapshot = snapshot_tx.borrow().clone();
287 if let Err(error) = snapshot.apply_remote_update(update) {
288 log::error!("error applying worktree update: {}", error);
289 }
290 *snapshot_tx.borrow_mut() = snapshot;
291 }
292 })
293 .detach();
294
295 {
296 let mut snapshot_rx = snapshot_rx.clone();
297 let this = worktree_handle.downgrade();
298 cx.spawn(|mut cx| async move {
299 while let Some(_) = snapshot_rx.recv().await {
300 if let Some(this) = this.upgrade(&cx) {
301 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
302 } else {
303 break;
304 }
305 }
306 })
307 .detach();
308 }
309 }
310 });
311 (worktree_handle, deserialize_task)
312 }
313
314 pub fn as_local(&self) -> Option<&LocalWorktree> {
315 if let Worktree::Local(worktree) = self {
316 Some(worktree)
317 } else {
318 None
319 }
320 }
321
322 pub fn as_remote(&self) -> Option<&RemoteWorktree> {
323 if let Worktree::Remote(worktree) = self {
324 Some(worktree)
325 } else {
326 None
327 }
328 }
329
330 pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
331 if let Worktree::Local(worktree) = self {
332 Some(worktree)
333 } else {
334 None
335 }
336 }
337
338 pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
339 if let Worktree::Remote(worktree) = self {
340 Some(worktree)
341 } else {
342 None
343 }
344 }
345
346 pub fn is_local(&self) -> bool {
347 matches!(self, Worktree::Local(_))
348 }
349
350 pub fn is_remote(&self) -> bool {
351 !self.is_local()
352 }
353
354 pub fn snapshot(&self) -> Snapshot {
355 match self {
356 Worktree::Local(worktree) => worktree.snapshot().snapshot,
357 Worktree::Remote(worktree) => worktree.snapshot(),
358 }
359 }
360
361 pub fn is_visible(&self) -> bool {
362 match self {
363 Worktree::Local(worktree) => worktree.visible,
364 Worktree::Remote(worktree) => worktree.visible,
365 }
366 }
367
368 pub fn replica_id(&self) -> ReplicaId {
369 match self {
370 Worktree::Local(_) => 0,
371 Worktree::Remote(worktree) => worktree.replica_id,
372 }
373 }
374
375 pub fn diagnostic_summaries<'a>(
376 &'a self,
377 ) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
378 match self {
379 Worktree::Local(worktree) => &worktree.diagnostic_summaries,
380 Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
381 }
382 .iter()
383 .map(|(path, summary)| (path.0.clone(), summary.clone()))
384 }
385
386 fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
387 match self {
388 Self::Local(worktree) => {
389 let is_fake_fs = worktree.fs.is_fake();
390 worktree.snapshot = worktree.background_snapshot.lock().clone();
391 if worktree.is_scanning() {
392 if worktree.poll_task.is_none() {
393 worktree.poll_task = Some(cx.spawn_weak(|this, mut cx| async move {
394 if is_fake_fs {
395 #[cfg(any(test, feature = "test-support"))]
396 cx.background().simulate_random_delay().await;
397 } else {
398 smol::Timer::after(Duration::from_millis(100)).await;
399 }
400 if let Some(this) = this.upgrade(&cx) {
401 this.update(&mut cx, |this, cx| {
402 this.as_local_mut().unwrap().poll_task = None;
403 this.poll_snapshot(cx);
404 });
405 }
406 }));
407 }
408 } else {
409 worktree.poll_task.take();
410 cx.emit(Event::UpdatedEntries);
411 }
412 }
413 Self::Remote(worktree) => {
414 worktree.snapshot = worktree.snapshot_rx.borrow().clone();
415 cx.emit(Event::UpdatedEntries);
416 }
417 };
418
419 cx.notify();
420 }
421}
422
423impl LocalWorktree {
424 async fn new(
425 client: Arc<Client>,
426 path: impl Into<Arc<Path>>,
427 visible: bool,
428 fs: Arc<dyn Fs>,
429 next_entry_id: Arc<AtomicUsize>,
430 cx: &mut AsyncAppContext,
431 ) -> Result<(ModelHandle<Worktree>, UnboundedSender<ScanState>)> {
432 let abs_path = path.into();
433 let path: Arc<Path> = Arc::from(Path::new(""));
434
435 // After determining whether the root entry is a file or a directory, populate the
436 // snapshot's "root name", which will be used for the purpose of fuzzy matching.
437 let root_name = abs_path
438 .file_name()
439 .map_or(String::new(), |f| f.to_string_lossy().to_string());
440 let root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
441 let metadata = fs
442 .metadata(&abs_path)
443 .await
444 .context("failed to stat worktree path")?;
445
446 let mut config = WorktreeConfig::default();
447 if let Ok(zed_toml) = fs.load(&abs_path.join(".zed.toml")).await {
448 if let Ok(parsed) = toml::from_str(&zed_toml) {
449 config = parsed;
450 }
451 }
452
453 let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
454 let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning);
455 let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
456 let mut snapshot = LocalSnapshot {
457 abs_path,
458 scan_id: 0,
459 ignores: Default::default(),
460 removed_entry_ids: Default::default(),
461 next_entry_id,
462 snapshot: Snapshot {
463 id: WorktreeId::from_usize(cx.model_id()),
464 root_name: root_name.clone(),
465 root_char_bag,
466 entries_by_path: Default::default(),
467 entries_by_id: Default::default(),
468 },
469 };
470 if let Some(metadata) = metadata {
471 let entry = Entry::new(
472 path.into(),
473 &metadata,
474 &snapshot.next_entry_id,
475 snapshot.root_char_bag,
476 );
477 snapshot.insert_entry(entry, fs.as_ref());
478 }
479
480 let tree = Self {
481 snapshot: snapshot.clone(),
482 config,
483 background_snapshot: Arc::new(Mutex::new(snapshot)),
484 last_scan_state_rx,
485 _background_scanner_task: None,
486 registration: Registration::None,
487 share: None,
488 poll_task: None,
489 diagnostics: Default::default(),
490 diagnostic_summaries: Default::default(),
491 client,
492 fs,
493 visible,
494 };
495
496 cx.spawn_weak(|this, mut cx| async move {
497 while let Some(scan_state) = scan_states_rx.next().await {
498 if let Some(handle) = this.upgrade(&cx) {
499 let to_send = handle.update(&mut cx, |this, cx| {
500 last_scan_state_tx.blocking_send(scan_state).ok();
501 this.poll_snapshot(cx);
502 let tree = this.as_local_mut().unwrap();
503 if !tree.is_scanning() {
504 if let Some(share) = tree.share.as_ref() {
505 return Some((tree.snapshot(), share.snapshots_tx.clone()));
506 }
507 }
508 None
509 });
510
511 if let Some((snapshot, snapshots_to_send_tx)) = to_send {
512 if let Err(err) = snapshots_to_send_tx.send(snapshot).await {
513 log::error!("error submitting snapshot to send {}", err);
514 }
515 }
516 } else {
517 break;
518 }
519 }
520 })
521 .detach();
522
523 Worktree::Local(tree)
524 });
525
526 Ok((tree, scan_states_tx))
527 }
528
529 pub fn contains_abs_path(&self, path: &Path) -> bool {
530 path.starts_with(&self.abs_path)
531 }
532
533 fn absolutize(&self, path: &Path) -> PathBuf {
534 if path.file_name().is_some() {
535 self.abs_path.join(path)
536 } else {
537 self.abs_path.to_path_buf()
538 }
539 }
540
541 pub fn authorized_logins(&self) -> Vec<String> {
542 self.config.collaborators.clone()
543 }
544
545 pub(crate) fn load_buffer(
546 &mut self,
547 path: &Path,
548 cx: &mut ModelContext<Worktree>,
549 ) -> Task<Result<ModelHandle<Buffer>>> {
550 let path = Arc::from(path);
551 cx.spawn(move |this, mut cx| async move {
552 let (file, contents) = this
553 .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
554 .await?;
555 Ok(cx.add_model(|cx| Buffer::from_file(0, contents, Box::new(file), cx)))
556 })
557 }
558
559 pub fn diagnostics_for_path(&self, path: &Path) -> Option<Vec<DiagnosticEntry<PointUtf16>>> {
560 self.diagnostics.get(path).cloned()
561 }
562
563 pub fn update_diagnostics(
564 &mut self,
565 worktree_path: Arc<Path>,
566 diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
567 _: &mut ModelContext<Worktree>,
568 ) -> Result<bool> {
569 self.diagnostics.remove(&worktree_path);
570 let old_summary = self
571 .diagnostic_summaries
572 .remove(&PathKey(worktree_path.clone()))
573 .unwrap_or_default();
574 let new_summary = DiagnosticSummary::new(&diagnostics);
575 if !new_summary.is_empty() {
576 self.diagnostic_summaries
577 .insert(PathKey(worktree_path.clone()), new_summary);
578 self.diagnostics.insert(worktree_path.clone(), diagnostics);
579 }
580
581 let updated = !old_summary.is_empty() || !new_summary.is_empty();
582 if updated {
583 if let Some(share) = self.share.as_ref() {
584 self.client
585 .send(proto::UpdateDiagnosticSummary {
586 project_id: share.project_id,
587 worktree_id: self.id().to_proto(),
588 summary: Some(proto::DiagnosticSummary {
589 path: worktree_path.to_string_lossy().to_string(),
590 error_count: new_summary.error_count as u32,
591 warning_count: new_summary.warning_count as u32,
592 }),
593 })
594 .log_err();
595 }
596 }
597
598 Ok(updated)
599 }
600
601 pub fn scan_complete(&self) -> impl Future<Output = ()> {
602 let mut scan_state_rx = self.last_scan_state_rx.clone();
603 async move {
604 let mut scan_state = Some(scan_state_rx.borrow().clone());
605 while let Some(ScanState::Scanning) = scan_state {
606 scan_state = scan_state_rx.recv().await;
607 }
608 }
609 }
610
611 fn is_scanning(&self) -> bool {
612 if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
613 true
614 } else {
615 false
616 }
617 }
618
619 pub fn snapshot(&self) -> LocalSnapshot {
620 self.snapshot.clone()
621 }
622
623 fn load(&self, path: &Path, cx: &mut ModelContext<Worktree>) -> Task<Result<(File, String)>> {
624 let handle = cx.handle();
625 let path = Arc::from(path);
626 let abs_path = self.absolutize(&path);
627 let fs = self.fs.clone();
628 cx.spawn(|this, mut cx| async move {
629 let text = fs.load(&abs_path).await?;
630 // Eagerly populate the snapshot with an updated entry for the loaded file
631 let entry = this
632 .update(&mut cx, |this, _| {
633 this.as_local().unwrap().refresh_entry(path, abs_path, None)
634 })
635 .await?;
636 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
637 Ok((
638 File {
639 entry_id: Some(entry.id),
640 worktree: handle,
641 path: entry.path,
642 mtime: entry.mtime,
643 is_local: true,
644 },
645 text,
646 ))
647 })
648 }
649
650 pub fn save_buffer_as(
651 &self,
652 buffer_handle: ModelHandle<Buffer>,
653 path: impl Into<Arc<Path>>,
654 cx: &mut ModelContext<Worktree>,
655 ) -> Task<Result<()>> {
656 let buffer = buffer_handle.read(cx);
657 let text = buffer.as_rope().clone();
658 let version = buffer.version();
659 let save = self.write_file(path, text, cx);
660 let handle = cx.handle();
661 cx.as_mut().spawn(|mut cx| async move {
662 let entry = save.await?;
663 let file = File {
664 entry_id: Some(entry.id),
665 worktree: handle,
666 path: entry.path,
667 mtime: entry.mtime,
668 is_local: true,
669 };
670
671 buffer_handle.update(&mut cx, |buffer, cx| {
672 buffer.did_save(version, file.mtime, Some(Box::new(file)), cx);
673 });
674
675 Ok(())
676 })
677 }
678
679 pub fn write_file(
680 &self,
681 path: impl Into<Arc<Path>>,
682 text: Rope,
683 cx: &mut ModelContext<Worktree>,
684 ) -> Task<Result<Entry>> {
685 let path = path.into();
686 let abs_path = self.absolutize(&path);
687 let save = cx.background().spawn({
688 let fs = self.fs.clone();
689 let abs_path = abs_path.clone();
690 async move { fs.save(&abs_path, &text).await }
691 });
692
693 cx.spawn(|this, mut cx| async move {
694 save.await?;
695 let entry = this
696 .update(&mut cx, |this, _| {
697 this.as_local_mut()
698 .unwrap()
699 .refresh_entry(path, abs_path, None)
700 })
701 .await?;
702 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
703 Ok(entry)
704 })
705 }
706
707 pub fn rename(
708 &self,
709 old_path: impl Into<Arc<Path>>,
710 new_path: impl Into<Arc<Path>>,
711 cx: &mut ModelContext<Worktree>,
712 ) -> Task<Result<Entry>> {
713 let old_path = old_path.into();
714 let new_path = new_path.into();
715 let abs_old_path = self.absolutize(&old_path);
716 let abs_new_path = self.absolutize(&new_path);
717 let rename = cx.background().spawn({
718 let fs = self.fs.clone();
719 let abs_new_path = abs_new_path.clone();
720 async move {
721 fs.rename(&abs_old_path, &abs_new_path, Default::default())
722 .await
723 }
724 });
725
726 cx.spawn(|this, mut cx| async move {
727 rename.await?;
728 let entry = this
729 .update(&mut cx, |this, _| {
730 this.as_local_mut().unwrap().refresh_entry(
731 new_path.clone(),
732 abs_new_path,
733 Some(old_path),
734 )
735 })
736 .await?;
737 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
738 Ok(entry)
739 })
740 }
741
742 fn refresh_entry(
743 &self,
744 path: Arc<Path>,
745 abs_path: PathBuf,
746 old_path: Option<Arc<Path>>,
747 ) -> impl Future<Output = Result<Entry>> {
748 let root_char_bag;
749 let next_entry_id;
750 let fs = self.fs.clone();
751 let shared_snapshots_tx = self.share.as_ref().map(|share| share.snapshots_tx.clone());
752 let snapshot = self.background_snapshot.clone();
753 {
754 let snapshot = snapshot.lock();
755 root_char_bag = snapshot.root_char_bag;
756 next_entry_id = snapshot.next_entry_id.clone();
757 }
758 async move {
759 let entry = Entry::new(
760 path,
761 &fs.metadata(&abs_path)
762 .await?
763 .ok_or_else(|| anyhow!("could not read saved file metadata"))?,
764 &next_entry_id,
765 root_char_bag,
766 );
767 let mut snapshot = snapshot.lock();
768 if let Some(old_path) = old_path {
769 snapshot.remove_path(&old_path);
770 }
771 let entry = snapshot.insert_entry(entry, fs.as_ref());
772 if let Some(tx) = shared_snapshots_tx {
773 tx.send(snapshot.clone()).await.ok();
774 }
775 Ok(entry)
776 }
777 }
778
779 pub fn register(
780 &mut self,
781 project_id: u64,
782 cx: &mut ModelContext<Worktree>,
783 ) -> Task<anyhow::Result<()>> {
784 if self.registration != Registration::None {
785 return Task::ready(Ok(()));
786 }
787
788 self.registration = Registration::Pending;
789 let client = self.client.clone();
790 let register_message = proto::RegisterWorktree {
791 project_id,
792 worktree_id: self.id().to_proto(),
793 root_name: self.root_name().to_string(),
794 authorized_logins: self.authorized_logins(),
795 visible: self.visible,
796 };
797 let request = client.request(register_message);
798 cx.spawn(|this, mut cx| async move {
799 let response = request.await;
800 this.update(&mut cx, |this, _| {
801 let worktree = this.as_local_mut().unwrap();
802 match response {
803 Ok(_) => {
804 if worktree.registration == Registration::Pending {
805 worktree.registration = Registration::Done { project_id };
806 }
807 Ok(())
808 }
809 Err(error) => {
810 worktree.registration = Registration::None;
811 Err(error)
812 }
813 }
814 })
815 })
816 }
817
818 pub fn share(&mut self, project_id: u64, cx: &mut ModelContext<Worktree>) -> Task<Result<()>> {
819 let register = self.register(project_id, cx);
820 let (share_tx, share_rx) = oneshot::channel();
821 let (snapshots_to_send_tx, snapshots_to_send_rx) =
822 smol::channel::unbounded::<LocalSnapshot>();
823 if self.share.is_some() {
824 let _ = share_tx.send(Ok(()));
825 } else {
826 let rpc = self.client.clone();
827 let worktree_id = cx.model_id() as u64;
828 let maintain_remote_snapshot = cx.background().spawn({
829 let rpc = rpc.clone();
830 let diagnostic_summaries = self.diagnostic_summaries.clone();
831 async move {
832 let mut prev_snapshot = match snapshots_to_send_rx.recv().await {
833 Ok(snapshot) => {
834 if let Err(error) = rpc
835 .request(proto::UpdateWorktree {
836 project_id,
837 worktree_id,
838 root_name: snapshot.root_name().to_string(),
839 updated_entries: snapshot
840 .entries_by_path
841 .iter()
842 .filter(|e| !e.is_ignored)
843 .map(Into::into)
844 .collect(),
845 removed_entries: Default::default(),
846 })
847 .await
848 {
849 let _ = share_tx.send(Err(error));
850 return Err(anyhow!("failed to send initial update worktree"));
851 } else {
852 let _ = share_tx.send(Ok(()));
853 snapshot
854 }
855 }
856 Err(error) => {
857 let _ = share_tx.send(Err(error.into()));
858 return Err(anyhow!("failed to send initial update worktree"));
859 }
860 };
861
862 for (path, summary) in diagnostic_summaries.iter() {
863 rpc.send(proto::UpdateDiagnosticSummary {
864 project_id,
865 worktree_id,
866 summary: Some(summary.to_proto(&path.0)),
867 })?;
868 }
869
870 while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
871 let message =
872 snapshot.build_update(&prev_snapshot, project_id, worktree_id, false);
873 rpc.request(message).await?;
874 prev_snapshot = snapshot;
875 }
876
877 Ok::<_, anyhow::Error>(())
878 }
879 .log_err()
880 });
881 self.share = Some(ShareState {
882 project_id,
883 snapshots_tx: snapshots_to_send_tx.clone(),
884 _maintain_remote_snapshot: Some(maintain_remote_snapshot),
885 });
886 }
887
888 cx.spawn_weak(|this, cx| async move {
889 register.await?;
890 if let Some(this) = this.upgrade(&cx) {
891 this.read_with(&cx, |this, _| {
892 let this = this.as_local().unwrap();
893 let _ = snapshots_to_send_tx.try_send(this.snapshot());
894 });
895 }
896 share_rx
897 .await
898 .unwrap_or_else(|_| Err(anyhow!("share ended")))
899 })
900 }
901
902 pub fn unregister(&mut self) {
903 self.unshare();
904 self.registration = Registration::None;
905 }
906
907 pub fn unshare(&mut self) {
908 self.share.take();
909 }
910
911 pub fn is_shared(&self) -> bool {
912 self.share.is_some()
913 }
914}
915
916impl RemoteWorktree {
917 fn snapshot(&self) -> Snapshot {
918 self.snapshot.clone()
919 }
920
921 pub fn update_from_remote(
922 &mut self,
923 envelope: TypedEnvelope<proto::UpdateWorktree>,
924 ) -> Result<()> {
925 self.updates_tx
926 .unbounded_send(envelope.payload)
927 .expect("consumer runs to completion");
928
929 Ok(())
930 }
931
932 pub fn update_diagnostic_summary(
933 &mut self,
934 path: Arc<Path>,
935 summary: &proto::DiagnosticSummary,
936 ) {
937 let summary = DiagnosticSummary {
938 error_count: summary.error_count as usize,
939 warning_count: summary.warning_count as usize,
940 };
941 if summary.is_empty() {
942 self.diagnostic_summaries.remove(&PathKey(path.clone()));
943 } else {
944 self.diagnostic_summaries
945 .insert(PathKey(path.clone()), summary);
946 }
947 }
948}
949
950impl Snapshot {
951 pub fn id(&self) -> WorktreeId {
952 self.id
953 }
954
955 pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
956 self.entries_by_id.get(&entry_id, &()).is_some()
957 }
958
959 pub(crate) fn insert_entry(&mut self, entry: proto::Entry) -> Result<Entry> {
960 let entry = Entry::try_from((&self.root_char_bag, entry))?;
961 self.entries_by_id.insert_or_replace(
962 PathEntry {
963 id: entry.id,
964 path: entry.path.clone(),
965 is_ignored: entry.is_ignored,
966 scan_id: 0,
967 },
968 &(),
969 );
970 self.entries_by_path.insert_or_replace(entry.clone(), &());
971 Ok(entry)
972 }
973
974 pub(crate) fn apply_remote_update(&mut self, update: proto::UpdateWorktree) -> Result<()> {
975 let mut entries_by_path_edits = Vec::new();
976 let mut entries_by_id_edits = Vec::new();
977 for entry_id in update.removed_entries {
978 let entry = self
979 .entry_for_id(ProjectEntryId::from_proto(entry_id))
980 .ok_or_else(|| anyhow!("unknown entry"))?;
981 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
982 entries_by_id_edits.push(Edit::Remove(entry.id));
983 }
984
985 for entry in update.updated_entries {
986 let entry = Entry::try_from((&self.root_char_bag, entry))?;
987 if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
988 entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
989 }
990 entries_by_id_edits.push(Edit::Insert(PathEntry {
991 id: entry.id,
992 path: entry.path.clone(),
993 is_ignored: entry.is_ignored,
994 scan_id: 0,
995 }));
996 entries_by_path_edits.push(Edit::Insert(entry));
997 }
998
999 self.entries_by_path.edit(entries_by_path_edits, &());
1000 self.entries_by_id.edit(entries_by_id_edits, &());
1001
1002 Ok(())
1003 }
1004
1005 pub fn file_count(&self) -> usize {
1006 self.entries_by_path.summary().file_count
1007 }
1008
1009 pub fn visible_file_count(&self) -> usize {
1010 self.entries_by_path.summary().visible_file_count
1011 }
1012
1013 fn traverse_from_offset(
1014 &self,
1015 include_dirs: bool,
1016 include_ignored: bool,
1017 start_offset: usize,
1018 ) -> Traversal {
1019 let mut cursor = self.entries_by_path.cursor();
1020 cursor.seek(
1021 &TraversalTarget::Count {
1022 count: start_offset,
1023 include_dirs,
1024 include_ignored,
1025 },
1026 Bias::Right,
1027 &(),
1028 );
1029 Traversal {
1030 cursor,
1031 include_dirs,
1032 include_ignored,
1033 }
1034 }
1035
1036 fn traverse_from_path(
1037 &self,
1038 include_dirs: bool,
1039 include_ignored: bool,
1040 path: &Path,
1041 ) -> Traversal {
1042 let mut cursor = self.entries_by_path.cursor();
1043 cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1044 Traversal {
1045 cursor,
1046 include_dirs,
1047 include_ignored,
1048 }
1049 }
1050
1051 pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1052 self.traverse_from_offset(false, include_ignored, start)
1053 }
1054
1055 pub fn entries(&self, include_ignored: bool) -> Traversal {
1056 self.traverse_from_offset(true, include_ignored, 0)
1057 }
1058
1059 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1060 let empty_path = Path::new("");
1061 self.entries_by_path
1062 .cursor::<()>()
1063 .filter(move |entry| entry.path.as_ref() != empty_path)
1064 .map(|entry| &entry.path)
1065 }
1066
1067 fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
1068 let mut cursor = self.entries_by_path.cursor();
1069 cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
1070 let traversal = Traversal {
1071 cursor,
1072 include_dirs: true,
1073 include_ignored: true,
1074 };
1075 ChildEntriesIter {
1076 traversal,
1077 parent_path,
1078 }
1079 }
1080
1081 pub fn root_entry(&self) -> Option<&Entry> {
1082 self.entry_for_path("")
1083 }
1084
1085 pub fn root_name(&self) -> &str {
1086 &self.root_name
1087 }
1088
1089 pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
1090 let path = path.as_ref();
1091 self.traverse_from_path(true, true, path)
1092 .entry()
1093 .and_then(|entry| {
1094 if entry.path.as_ref() == path {
1095 Some(entry)
1096 } else {
1097 None
1098 }
1099 })
1100 }
1101
1102 pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
1103 let entry = self.entries_by_id.get(&id, &())?;
1104 self.entry_for_path(&entry.path)
1105 }
1106
1107 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
1108 self.entry_for_path(path.as_ref()).map(|e| e.inode)
1109 }
1110}
1111
1112impl LocalSnapshot {
1113 pub fn abs_path(&self) -> &Arc<Path> {
1114 &self.abs_path
1115 }
1116
1117 #[cfg(test)]
1118 pub(crate) fn to_proto(
1119 &self,
1120 diagnostic_summaries: &TreeMap<PathKey, DiagnosticSummary>,
1121 visible: bool,
1122 ) -> proto::Worktree {
1123 let root_name = self.root_name.clone();
1124 proto::Worktree {
1125 id: self.id.0 as u64,
1126 root_name,
1127 entries: self
1128 .entries_by_path
1129 .iter()
1130 .filter(|e| !e.is_ignored)
1131 .map(Into::into)
1132 .collect(),
1133 diagnostic_summaries: diagnostic_summaries
1134 .iter()
1135 .map(|(path, summary)| summary.to_proto(&path.0))
1136 .collect(),
1137 visible,
1138 }
1139 }
1140
1141 pub(crate) fn build_update(
1142 &self,
1143 other: &Self,
1144 project_id: u64,
1145 worktree_id: u64,
1146 include_ignored: bool,
1147 ) -> proto::UpdateWorktree {
1148 let mut updated_entries = Vec::new();
1149 let mut removed_entries = Vec::new();
1150 let mut self_entries = self
1151 .entries_by_id
1152 .cursor::<()>()
1153 .filter(|e| include_ignored || !e.is_ignored)
1154 .peekable();
1155 let mut other_entries = other
1156 .entries_by_id
1157 .cursor::<()>()
1158 .filter(|e| include_ignored || !e.is_ignored)
1159 .peekable();
1160 loop {
1161 match (self_entries.peek(), other_entries.peek()) {
1162 (Some(self_entry), Some(other_entry)) => {
1163 match Ord::cmp(&self_entry.id, &other_entry.id) {
1164 Ordering::Less => {
1165 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1166 updated_entries.push(entry);
1167 self_entries.next();
1168 }
1169 Ordering::Equal => {
1170 if self_entry.scan_id != other_entry.scan_id {
1171 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1172 updated_entries.push(entry);
1173 }
1174
1175 self_entries.next();
1176 other_entries.next();
1177 }
1178 Ordering::Greater => {
1179 removed_entries.push(other_entry.id.to_proto());
1180 other_entries.next();
1181 }
1182 }
1183 }
1184 (Some(self_entry), None) => {
1185 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1186 updated_entries.push(entry);
1187 self_entries.next();
1188 }
1189 (None, Some(other_entry)) => {
1190 removed_entries.push(other_entry.id.to_proto());
1191 other_entries.next();
1192 }
1193 (None, None) => break,
1194 }
1195 }
1196
1197 proto::UpdateWorktree {
1198 project_id,
1199 worktree_id,
1200 root_name: self.root_name().to_string(),
1201 updated_entries,
1202 removed_entries,
1203 }
1204 }
1205
1206 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
1207 if !entry.is_dir() && entry.path.file_name() == Some(&GITIGNORE) {
1208 let abs_path = self.abs_path.join(&entry.path);
1209 match build_gitignore(&abs_path, fs) {
1210 Ok(ignore) => {
1211 let ignore_dir_path = entry.path.parent().unwrap();
1212 self.ignores
1213 .insert(ignore_dir_path.into(), (Arc::new(ignore), self.scan_id));
1214 }
1215 Err(error) => {
1216 log::error!(
1217 "error loading .gitignore file {:?} - {:?}",
1218 &entry.path,
1219 error
1220 );
1221 }
1222 }
1223 }
1224
1225 self.reuse_entry_id(&mut entry);
1226 self.entries_by_path.insert_or_replace(entry.clone(), &());
1227 let scan_id = self.scan_id;
1228 self.entries_by_id.insert_or_replace(
1229 PathEntry {
1230 id: entry.id,
1231 path: entry.path.clone(),
1232 is_ignored: entry.is_ignored,
1233 scan_id,
1234 },
1235 &(),
1236 );
1237 entry
1238 }
1239
1240 fn populate_dir(
1241 &mut self,
1242 parent_path: Arc<Path>,
1243 entries: impl IntoIterator<Item = Entry>,
1244 ignore: Option<Arc<Gitignore>>,
1245 ) {
1246 let mut parent_entry = self
1247 .entries_by_path
1248 .get(&PathKey(parent_path.clone()), &())
1249 .unwrap()
1250 .clone();
1251 if let Some(ignore) = ignore {
1252 self.ignores.insert(parent_path, (ignore, self.scan_id));
1253 }
1254 if matches!(parent_entry.kind, EntryKind::PendingDir) {
1255 parent_entry.kind = EntryKind::Dir;
1256 } else {
1257 unreachable!();
1258 }
1259
1260 let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
1261 let mut entries_by_id_edits = Vec::new();
1262
1263 for mut entry in entries {
1264 self.reuse_entry_id(&mut entry);
1265 entries_by_id_edits.push(Edit::Insert(PathEntry {
1266 id: entry.id,
1267 path: entry.path.clone(),
1268 is_ignored: entry.is_ignored,
1269 scan_id: self.scan_id,
1270 }));
1271 entries_by_path_edits.push(Edit::Insert(entry));
1272 }
1273
1274 self.entries_by_path.edit(entries_by_path_edits, &());
1275 self.entries_by_id.edit(entries_by_id_edits, &());
1276 }
1277
1278 fn reuse_entry_id(&mut self, entry: &mut Entry) {
1279 if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
1280 entry.id = removed_entry_id;
1281 } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
1282 entry.id = existing_entry.id;
1283 }
1284 }
1285
1286 fn remove_path(&mut self, path: &Path) {
1287 let mut new_entries;
1288 let removed_entries;
1289 {
1290 let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
1291 new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
1292 removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
1293 new_entries.push_tree(cursor.suffix(&()), &());
1294 }
1295 self.entries_by_path = new_entries;
1296
1297 let mut entries_by_id_edits = Vec::new();
1298 for entry in removed_entries.cursor::<()>() {
1299 let removed_entry_id = self
1300 .removed_entry_ids
1301 .entry(entry.inode)
1302 .or_insert(entry.id);
1303 *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
1304 entries_by_id_edits.push(Edit::Remove(entry.id));
1305 }
1306 self.entries_by_id.edit(entries_by_id_edits, &());
1307
1308 if path.file_name() == Some(&GITIGNORE) {
1309 if let Some((_, scan_id)) = self.ignores.get_mut(path.parent().unwrap()) {
1310 *scan_id = self.scan_id;
1311 }
1312 }
1313 }
1314
1315 fn ignore_stack_for_path(&self, path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
1316 let mut new_ignores = Vec::new();
1317 for ancestor in path.ancestors().skip(1) {
1318 if let Some((ignore, _)) = self.ignores.get(ancestor) {
1319 new_ignores.push((ancestor, Some(ignore.clone())));
1320 } else {
1321 new_ignores.push((ancestor, None));
1322 }
1323 }
1324
1325 let mut ignore_stack = IgnoreStack::none();
1326 for (parent_path, ignore) in new_ignores.into_iter().rev() {
1327 if ignore_stack.is_path_ignored(&parent_path, true) {
1328 ignore_stack = IgnoreStack::all();
1329 break;
1330 } else if let Some(ignore) = ignore {
1331 ignore_stack = ignore_stack.append(Arc::from(parent_path), ignore);
1332 }
1333 }
1334
1335 if ignore_stack.is_path_ignored(path, is_dir) {
1336 ignore_stack = IgnoreStack::all();
1337 }
1338
1339 ignore_stack
1340 }
1341}
1342
1343fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
1344 let contents = smol::block_on(fs.load(&abs_path))?;
1345 let parent = abs_path.parent().unwrap_or(Path::new("/"));
1346 let mut builder = GitignoreBuilder::new(parent);
1347 for line in contents.lines() {
1348 builder.add_line(Some(abs_path.into()), line)?;
1349 }
1350 Ok(builder.build()?)
1351}
1352
1353impl WorktreeId {
1354 pub fn from_usize(handle_id: usize) -> Self {
1355 Self(handle_id)
1356 }
1357
1358 pub(crate) fn from_proto(id: u64) -> Self {
1359 Self(id as usize)
1360 }
1361
1362 pub fn to_proto(&self) -> u64 {
1363 self.0 as u64
1364 }
1365
1366 pub fn to_usize(&self) -> usize {
1367 self.0
1368 }
1369}
1370
1371impl fmt::Display for WorktreeId {
1372 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1373 self.0.fmt(f)
1374 }
1375}
1376
1377impl Deref for Worktree {
1378 type Target = Snapshot;
1379
1380 fn deref(&self) -> &Self::Target {
1381 match self {
1382 Worktree::Local(worktree) => &worktree.snapshot,
1383 Worktree::Remote(worktree) => &worktree.snapshot,
1384 }
1385 }
1386}
1387
1388impl Deref for LocalWorktree {
1389 type Target = LocalSnapshot;
1390
1391 fn deref(&self) -> &Self::Target {
1392 &self.snapshot
1393 }
1394}
1395
1396impl Deref for RemoteWorktree {
1397 type Target = Snapshot;
1398
1399 fn deref(&self) -> &Self::Target {
1400 &self.snapshot
1401 }
1402}
1403
1404impl fmt::Debug for LocalWorktree {
1405 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1406 self.snapshot.fmt(f)
1407 }
1408}
1409
1410impl fmt::Debug for Snapshot {
1411 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1412 struct EntriesById<'a>(&'a SumTree<PathEntry>);
1413 struct EntriesByPath<'a>(&'a SumTree<Entry>);
1414
1415 impl<'a> fmt::Debug for EntriesByPath<'a> {
1416 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1417 f.debug_map()
1418 .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
1419 .finish()
1420 }
1421 }
1422
1423 impl<'a> fmt::Debug for EntriesById<'a> {
1424 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1425 f.debug_list().entries(self.0.iter()).finish()
1426 }
1427 }
1428
1429 f.debug_struct("Snapshot")
1430 .field("id", &self.id)
1431 .field("root_name", &self.root_name)
1432 .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
1433 .field("entries_by_id", &EntriesById(&self.entries_by_id))
1434 .finish()
1435 }
1436}
1437
1438#[derive(Clone, PartialEq)]
1439pub struct File {
1440 pub worktree: ModelHandle<Worktree>,
1441 pub path: Arc<Path>,
1442 pub mtime: SystemTime,
1443 pub(crate) entry_id: Option<ProjectEntryId>,
1444 pub(crate) is_local: bool,
1445}
1446
1447impl language::File for File {
1448 fn as_local(&self) -> Option<&dyn language::LocalFile> {
1449 if self.is_local {
1450 Some(self)
1451 } else {
1452 None
1453 }
1454 }
1455
1456 fn mtime(&self) -> SystemTime {
1457 self.mtime
1458 }
1459
1460 fn path(&self) -> &Arc<Path> {
1461 &self.path
1462 }
1463
1464 fn full_path(&self, cx: &AppContext) -> PathBuf {
1465 let mut full_path = PathBuf::new();
1466 full_path.push(self.worktree.read(cx).root_name());
1467 if self.path.components().next().is_some() {
1468 full_path.push(&self.path);
1469 }
1470 full_path
1471 }
1472
1473 /// Returns the last component of this handle's absolute path. If this handle refers to the root
1474 /// of its worktree, then this method will return the name of the worktree itself.
1475 fn file_name(&self, cx: &AppContext) -> OsString {
1476 self.path
1477 .file_name()
1478 .map(|name| name.into())
1479 .unwrap_or_else(|| OsString::from(&self.worktree.read(cx).root_name))
1480 }
1481
1482 fn is_deleted(&self) -> bool {
1483 self.entry_id.is_none()
1484 }
1485
1486 fn save(
1487 &self,
1488 buffer_id: u64,
1489 text: Rope,
1490 version: clock::Global,
1491 cx: &mut MutableAppContext,
1492 ) -> Task<Result<(clock::Global, SystemTime)>> {
1493 self.worktree.update(cx, |worktree, cx| match worktree {
1494 Worktree::Local(worktree) => {
1495 let rpc = worktree.client.clone();
1496 let project_id = worktree.share.as_ref().map(|share| share.project_id);
1497 let save = worktree.write_file(self.path.clone(), text, cx);
1498 cx.background().spawn(async move {
1499 let entry = save.await?;
1500 if let Some(project_id) = project_id {
1501 rpc.send(proto::BufferSaved {
1502 project_id,
1503 buffer_id,
1504 version: serialize_version(&version),
1505 mtime: Some(entry.mtime.into()),
1506 })?;
1507 }
1508 Ok((version, entry.mtime))
1509 })
1510 }
1511 Worktree::Remote(worktree) => {
1512 let rpc = worktree.client.clone();
1513 let project_id = worktree.project_id;
1514 cx.foreground().spawn(async move {
1515 let response = rpc
1516 .request(proto::SaveBuffer {
1517 project_id,
1518 buffer_id,
1519 version: serialize_version(&version),
1520 })
1521 .await?;
1522 let version = deserialize_version(response.version);
1523 let mtime = response
1524 .mtime
1525 .ok_or_else(|| anyhow!("missing mtime"))?
1526 .into();
1527 Ok((version, mtime))
1528 })
1529 }
1530 })
1531 }
1532
1533 fn as_any(&self) -> &dyn Any {
1534 self
1535 }
1536
1537 fn to_proto(&self) -> rpc::proto::File {
1538 rpc::proto::File {
1539 worktree_id: self.worktree.id() as u64,
1540 entry_id: self.entry_id.map(|entry_id| entry_id.to_proto()),
1541 path: self.path.to_string_lossy().into(),
1542 mtime: Some(self.mtime.into()),
1543 }
1544 }
1545}
1546
1547impl language::LocalFile for File {
1548 fn abs_path(&self, cx: &AppContext) -> PathBuf {
1549 self.worktree
1550 .read(cx)
1551 .as_local()
1552 .unwrap()
1553 .abs_path
1554 .join(&self.path)
1555 }
1556
1557 fn load(&self, cx: &AppContext) -> Task<Result<String>> {
1558 let worktree = self.worktree.read(cx).as_local().unwrap();
1559 let abs_path = worktree.absolutize(&self.path);
1560 let fs = worktree.fs.clone();
1561 cx.background()
1562 .spawn(async move { fs.load(&abs_path).await })
1563 }
1564
1565 fn buffer_reloaded(
1566 &self,
1567 buffer_id: u64,
1568 version: &clock::Global,
1569 mtime: SystemTime,
1570 cx: &mut MutableAppContext,
1571 ) {
1572 let worktree = self.worktree.read(cx).as_local().unwrap();
1573 if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) {
1574 worktree
1575 .client
1576 .send(proto::BufferReloaded {
1577 project_id,
1578 buffer_id,
1579 version: serialize_version(&version),
1580 mtime: Some(mtime.into()),
1581 })
1582 .log_err();
1583 }
1584 }
1585}
1586
1587impl File {
1588 pub fn from_proto(
1589 proto: rpc::proto::File,
1590 worktree: ModelHandle<Worktree>,
1591 cx: &AppContext,
1592 ) -> Result<Self> {
1593 let worktree_id = worktree
1594 .read(cx)
1595 .as_remote()
1596 .ok_or_else(|| anyhow!("not remote"))?
1597 .id();
1598
1599 if worktree_id.to_proto() != proto.worktree_id {
1600 return Err(anyhow!("worktree id does not match file"));
1601 }
1602
1603 Ok(Self {
1604 worktree,
1605 path: Path::new(&proto.path).into(),
1606 mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(),
1607 entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
1608 is_local: false,
1609 })
1610 }
1611
1612 pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
1613 file.and_then(|f| f.as_any().downcast_ref())
1614 }
1615
1616 pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
1617 self.worktree.read(cx).id()
1618 }
1619
1620 pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
1621 self.entry_id
1622 }
1623}
1624
1625#[derive(Clone, Debug, PartialEq, Eq)]
1626pub struct Entry {
1627 pub id: ProjectEntryId,
1628 pub kind: EntryKind,
1629 pub path: Arc<Path>,
1630 pub inode: u64,
1631 pub mtime: SystemTime,
1632 pub is_symlink: bool,
1633 pub is_ignored: bool,
1634}
1635
1636#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1637pub enum EntryKind {
1638 PendingDir,
1639 Dir,
1640 File(CharBag),
1641}
1642
1643impl Entry {
1644 fn new(
1645 path: Arc<Path>,
1646 metadata: &fs::Metadata,
1647 next_entry_id: &AtomicUsize,
1648 root_char_bag: CharBag,
1649 ) -> Self {
1650 Self {
1651 id: ProjectEntryId::new(next_entry_id),
1652 kind: if metadata.is_dir {
1653 EntryKind::PendingDir
1654 } else {
1655 EntryKind::File(char_bag_for_path(root_char_bag, &path))
1656 },
1657 path,
1658 inode: metadata.inode,
1659 mtime: metadata.mtime,
1660 is_symlink: metadata.is_symlink,
1661 is_ignored: false,
1662 }
1663 }
1664
1665 pub fn is_dir(&self) -> bool {
1666 matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
1667 }
1668
1669 pub fn is_file(&self) -> bool {
1670 matches!(self.kind, EntryKind::File(_))
1671 }
1672}
1673
1674impl sum_tree::Item for Entry {
1675 type Summary = EntrySummary;
1676
1677 fn summary(&self) -> Self::Summary {
1678 let visible_count = if self.is_ignored { 0 } else { 1 };
1679 let file_count;
1680 let visible_file_count;
1681 if self.is_file() {
1682 file_count = 1;
1683 visible_file_count = visible_count;
1684 } else {
1685 file_count = 0;
1686 visible_file_count = 0;
1687 }
1688
1689 EntrySummary {
1690 max_path: self.path.clone(),
1691 count: 1,
1692 visible_count,
1693 file_count,
1694 visible_file_count,
1695 }
1696 }
1697}
1698
1699impl sum_tree::KeyedItem for Entry {
1700 type Key = PathKey;
1701
1702 fn key(&self) -> Self::Key {
1703 PathKey(self.path.clone())
1704 }
1705}
1706
1707#[derive(Clone, Debug)]
1708pub struct EntrySummary {
1709 max_path: Arc<Path>,
1710 count: usize,
1711 visible_count: usize,
1712 file_count: usize,
1713 visible_file_count: usize,
1714}
1715
1716impl Default for EntrySummary {
1717 fn default() -> Self {
1718 Self {
1719 max_path: Arc::from(Path::new("")),
1720 count: 0,
1721 visible_count: 0,
1722 file_count: 0,
1723 visible_file_count: 0,
1724 }
1725 }
1726}
1727
1728impl sum_tree::Summary for EntrySummary {
1729 type Context = ();
1730
1731 fn add_summary(&mut self, rhs: &Self, _: &()) {
1732 self.max_path = rhs.max_path.clone();
1733 self.visible_count += rhs.visible_count;
1734 self.file_count += rhs.file_count;
1735 self.visible_file_count += rhs.visible_file_count;
1736 }
1737}
1738
1739#[derive(Clone, Debug)]
1740struct PathEntry {
1741 id: ProjectEntryId,
1742 path: Arc<Path>,
1743 is_ignored: bool,
1744 scan_id: usize,
1745}
1746
1747impl sum_tree::Item for PathEntry {
1748 type Summary = PathEntrySummary;
1749
1750 fn summary(&self) -> Self::Summary {
1751 PathEntrySummary { max_id: self.id }
1752 }
1753}
1754
1755impl sum_tree::KeyedItem for PathEntry {
1756 type Key = ProjectEntryId;
1757
1758 fn key(&self) -> Self::Key {
1759 self.id
1760 }
1761}
1762
1763#[derive(Clone, Debug, Default)]
1764struct PathEntrySummary {
1765 max_id: ProjectEntryId,
1766}
1767
1768impl sum_tree::Summary for PathEntrySummary {
1769 type Context = ();
1770
1771 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
1772 self.max_id = summary.max_id;
1773 }
1774}
1775
1776impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
1777 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
1778 *self = summary.max_id;
1779 }
1780}
1781
1782#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
1783pub struct PathKey(Arc<Path>);
1784
1785impl Default for PathKey {
1786 fn default() -> Self {
1787 Self(Path::new("").into())
1788 }
1789}
1790
1791impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
1792 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
1793 self.0 = summary.max_path.clone();
1794 }
1795}
1796
1797struct BackgroundScanner {
1798 fs: Arc<dyn Fs>,
1799 snapshot: Arc<Mutex<LocalSnapshot>>,
1800 notify: UnboundedSender<ScanState>,
1801 executor: Arc<executor::Background>,
1802}
1803
1804impl BackgroundScanner {
1805 fn new(
1806 snapshot: Arc<Mutex<LocalSnapshot>>,
1807 notify: UnboundedSender<ScanState>,
1808 fs: Arc<dyn Fs>,
1809 executor: Arc<executor::Background>,
1810 ) -> Self {
1811 Self {
1812 fs,
1813 snapshot,
1814 notify,
1815 executor,
1816 }
1817 }
1818
1819 fn abs_path(&self) -> Arc<Path> {
1820 self.snapshot.lock().abs_path.clone()
1821 }
1822
1823 fn snapshot(&self) -> LocalSnapshot {
1824 self.snapshot.lock().clone()
1825 }
1826
1827 async fn run(mut self, events_rx: impl Stream<Item = Vec<fsevent::Event>>) {
1828 if self.notify.unbounded_send(ScanState::Scanning).is_err() {
1829 return;
1830 }
1831
1832 if let Err(err) = self.scan_dirs().await {
1833 if self
1834 .notify
1835 .unbounded_send(ScanState::Err(Arc::new(err)))
1836 .is_err()
1837 {
1838 return;
1839 }
1840 }
1841
1842 if self.notify.unbounded_send(ScanState::Idle).is_err() {
1843 return;
1844 }
1845
1846 futures::pin_mut!(events_rx);
1847 while let Some(events) = events_rx.next().await {
1848 if self.notify.unbounded_send(ScanState::Scanning).is_err() {
1849 break;
1850 }
1851
1852 if !self.process_events(events).await {
1853 break;
1854 }
1855
1856 if self.notify.unbounded_send(ScanState::Idle).is_err() {
1857 break;
1858 }
1859 }
1860 }
1861
1862 async fn scan_dirs(&mut self) -> Result<()> {
1863 let root_char_bag;
1864 let next_entry_id;
1865 let is_dir;
1866 {
1867 let snapshot = self.snapshot.lock();
1868 root_char_bag = snapshot.root_char_bag;
1869 next_entry_id = snapshot.next_entry_id.clone();
1870 is_dir = snapshot.root_entry().map_or(false, |e| e.is_dir())
1871 };
1872
1873 if is_dir {
1874 let path: Arc<Path> = Arc::from(Path::new(""));
1875 let abs_path = self.abs_path();
1876 let (tx, rx) = channel::unbounded();
1877 self.executor
1878 .block(tx.send(ScanJob {
1879 abs_path: abs_path.to_path_buf(),
1880 path,
1881 ignore_stack: IgnoreStack::none(),
1882 scan_queue: tx.clone(),
1883 }))
1884 .unwrap();
1885 drop(tx);
1886
1887 self.executor
1888 .scoped(|scope| {
1889 for _ in 0..self.executor.num_cpus() {
1890 scope.spawn(async {
1891 while let Ok(job) = rx.recv().await {
1892 if let Err(err) = self
1893 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
1894 .await
1895 {
1896 log::error!("error scanning {:?}: {}", job.abs_path, err);
1897 }
1898 }
1899 });
1900 }
1901 })
1902 .await;
1903 }
1904
1905 Ok(())
1906 }
1907
1908 async fn scan_dir(
1909 &self,
1910 root_char_bag: CharBag,
1911 next_entry_id: Arc<AtomicUsize>,
1912 job: &ScanJob,
1913 ) -> Result<()> {
1914 let mut new_entries: Vec<Entry> = Vec::new();
1915 let mut new_jobs: Vec<ScanJob> = Vec::new();
1916 let mut ignore_stack = job.ignore_stack.clone();
1917 let mut new_ignore = None;
1918
1919 let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
1920 while let Some(child_abs_path) = child_paths.next().await {
1921 let child_abs_path = match child_abs_path {
1922 Ok(child_abs_path) => child_abs_path,
1923 Err(error) => {
1924 log::error!("error processing entry {:?}", error);
1925 continue;
1926 }
1927 };
1928 let child_name = child_abs_path.file_name().unwrap();
1929 let child_path: Arc<Path> = job.path.join(child_name).into();
1930 let child_metadata = match self.fs.metadata(&child_abs_path).await? {
1931 Some(metadata) => metadata,
1932 None => continue,
1933 };
1934
1935 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
1936 if child_name == *GITIGNORE {
1937 match build_gitignore(&child_abs_path, self.fs.as_ref()) {
1938 Ok(ignore) => {
1939 let ignore = Arc::new(ignore);
1940 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
1941 new_ignore = Some(ignore);
1942 }
1943 Err(error) => {
1944 log::error!(
1945 "error loading .gitignore file {:?} - {:?}",
1946 child_name,
1947 error
1948 );
1949 }
1950 }
1951
1952 // Update ignore status of any child entries we've already processed to reflect the
1953 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
1954 // there should rarely be too numerous. Update the ignore stack associated with any
1955 // new jobs as well.
1956 let mut new_jobs = new_jobs.iter_mut();
1957 for entry in &mut new_entries {
1958 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
1959 if entry.is_dir() {
1960 new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
1961 IgnoreStack::all()
1962 } else {
1963 ignore_stack.clone()
1964 };
1965 }
1966 }
1967 }
1968
1969 let mut child_entry = Entry::new(
1970 child_path.clone(),
1971 &child_metadata,
1972 &next_entry_id,
1973 root_char_bag,
1974 );
1975
1976 if child_metadata.is_dir {
1977 let is_ignored = ignore_stack.is_path_ignored(&child_path, true);
1978 child_entry.is_ignored = is_ignored;
1979 new_entries.push(child_entry);
1980 new_jobs.push(ScanJob {
1981 abs_path: child_abs_path,
1982 path: child_path,
1983 ignore_stack: if is_ignored {
1984 IgnoreStack::all()
1985 } else {
1986 ignore_stack.clone()
1987 },
1988 scan_queue: job.scan_queue.clone(),
1989 });
1990 } else {
1991 child_entry.is_ignored = ignore_stack.is_path_ignored(&child_path, false);
1992 new_entries.push(child_entry);
1993 };
1994 }
1995
1996 self.snapshot
1997 .lock()
1998 .populate_dir(job.path.clone(), new_entries, new_ignore);
1999 for new_job in new_jobs {
2000 job.scan_queue.send(new_job).await.unwrap();
2001 }
2002
2003 Ok(())
2004 }
2005
2006 async fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
2007 events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2008 events.dedup_by(|a, b| a.path.starts_with(&b.path));
2009
2010 let root_char_bag;
2011 let root_abs_path;
2012 let next_entry_id;
2013 {
2014 let mut snapshot = self.snapshot.lock();
2015 snapshot.scan_id += 1;
2016 root_char_bag = snapshot.root_char_bag;
2017 root_abs_path = snapshot.abs_path.clone();
2018 next_entry_id = snapshot.next_entry_id.clone();
2019 }
2020
2021 let root_abs_path = if let Ok(abs_path) = self.fs.canonicalize(&root_abs_path).await {
2022 abs_path
2023 } else {
2024 return false;
2025 };
2026 let metadata = futures::future::join_all(
2027 events
2028 .iter()
2029 .map(|event| self.fs.metadata(&event.path))
2030 .collect::<Vec<_>>(),
2031 )
2032 .await;
2033
2034 // Hold the snapshot lock while clearing and re-inserting the root entries
2035 // for each event. This way, the snapshot is not observable to the foreground
2036 // thread while this operation is in-progress.
2037 let (scan_queue_tx, scan_queue_rx) = channel::unbounded();
2038 {
2039 let mut snapshot = self.snapshot.lock();
2040 for event in &events {
2041 if let Ok(path) = event.path.strip_prefix(&root_abs_path) {
2042 snapshot.remove_path(&path);
2043 }
2044 }
2045
2046 for (event, metadata) in events.into_iter().zip(metadata.into_iter()) {
2047 let path: Arc<Path> = match event.path.strip_prefix(&root_abs_path) {
2048 Ok(path) => Arc::from(path.to_path_buf()),
2049 Err(_) => {
2050 log::error!(
2051 "unexpected event {:?} for root path {:?}",
2052 event.path,
2053 root_abs_path
2054 );
2055 continue;
2056 }
2057 };
2058
2059 match metadata {
2060 Ok(Some(metadata)) => {
2061 let ignore_stack = snapshot.ignore_stack_for_path(&path, metadata.is_dir);
2062 let mut fs_entry = Entry::new(
2063 path.clone(),
2064 &metadata,
2065 snapshot.next_entry_id.as_ref(),
2066 snapshot.root_char_bag,
2067 );
2068 fs_entry.is_ignored = ignore_stack.is_all();
2069 snapshot.insert_entry(fs_entry, self.fs.as_ref());
2070 if metadata.is_dir {
2071 self.executor
2072 .block(scan_queue_tx.send(ScanJob {
2073 abs_path: event.path,
2074 path,
2075 ignore_stack,
2076 scan_queue: scan_queue_tx.clone(),
2077 }))
2078 .unwrap();
2079 }
2080 }
2081 Ok(None) => {}
2082 Err(err) => {
2083 // TODO - create a special 'error' entry in the entries tree to mark this
2084 log::error!("error reading file on event {:?}", err);
2085 }
2086 }
2087 }
2088 drop(scan_queue_tx);
2089 }
2090
2091 // Scan any directories that were created as part of this event batch.
2092 self.executor
2093 .scoped(|scope| {
2094 for _ in 0..self.executor.num_cpus() {
2095 scope.spawn(async {
2096 while let Ok(job) = scan_queue_rx.recv().await {
2097 if let Err(err) = self
2098 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2099 .await
2100 {
2101 log::error!("error scanning {:?}: {}", job.abs_path, err);
2102 }
2103 }
2104 });
2105 }
2106 })
2107 .await;
2108
2109 // Attempt to detect renames only over a single batch of file-system events.
2110 self.snapshot.lock().removed_entry_ids.clear();
2111
2112 self.update_ignore_statuses().await;
2113 true
2114 }
2115
2116 async fn update_ignore_statuses(&self) {
2117 let mut snapshot = self.snapshot();
2118
2119 let mut ignores_to_update = Vec::new();
2120 let mut ignores_to_delete = Vec::new();
2121 for (parent_path, (_, scan_id)) in &snapshot.ignores {
2122 if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
2123 ignores_to_update.push(parent_path.clone());
2124 }
2125
2126 let ignore_path = parent_path.join(&*GITIGNORE);
2127 if snapshot.entry_for_path(ignore_path).is_none() {
2128 ignores_to_delete.push(parent_path.clone());
2129 }
2130 }
2131
2132 for parent_path in ignores_to_delete {
2133 snapshot.ignores.remove(&parent_path);
2134 self.snapshot.lock().ignores.remove(&parent_path);
2135 }
2136
2137 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2138 ignores_to_update.sort_unstable();
2139 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2140 while let Some(parent_path) = ignores_to_update.next() {
2141 while ignores_to_update
2142 .peek()
2143 .map_or(false, |p| p.starts_with(&parent_path))
2144 {
2145 ignores_to_update.next().unwrap();
2146 }
2147
2148 let ignore_stack = snapshot.ignore_stack_for_path(&parent_path, true);
2149 ignore_queue_tx
2150 .send(UpdateIgnoreStatusJob {
2151 path: parent_path,
2152 ignore_stack,
2153 ignore_queue: ignore_queue_tx.clone(),
2154 })
2155 .await
2156 .unwrap();
2157 }
2158 drop(ignore_queue_tx);
2159
2160 self.executor
2161 .scoped(|scope| {
2162 for _ in 0..self.executor.num_cpus() {
2163 scope.spawn(async {
2164 while let Ok(job) = ignore_queue_rx.recv().await {
2165 self.update_ignore_status(job, &snapshot).await;
2166 }
2167 });
2168 }
2169 })
2170 .await;
2171 }
2172
2173 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
2174 let mut ignore_stack = job.ignore_stack;
2175 if let Some((ignore, _)) = snapshot.ignores.get(&job.path) {
2176 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2177 }
2178
2179 let mut entries_by_id_edits = Vec::new();
2180 let mut entries_by_path_edits = Vec::new();
2181 for mut entry in snapshot.child_entries(&job.path).cloned() {
2182 let was_ignored = entry.is_ignored;
2183 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2184 if entry.is_dir() {
2185 let child_ignore_stack = if entry.is_ignored {
2186 IgnoreStack::all()
2187 } else {
2188 ignore_stack.clone()
2189 };
2190 job.ignore_queue
2191 .send(UpdateIgnoreStatusJob {
2192 path: entry.path.clone(),
2193 ignore_stack: child_ignore_stack,
2194 ignore_queue: job.ignore_queue.clone(),
2195 })
2196 .await
2197 .unwrap();
2198 }
2199
2200 if entry.is_ignored != was_ignored {
2201 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
2202 path_entry.scan_id = snapshot.scan_id;
2203 path_entry.is_ignored = entry.is_ignored;
2204 entries_by_id_edits.push(Edit::Insert(path_entry));
2205 entries_by_path_edits.push(Edit::Insert(entry));
2206 }
2207 }
2208
2209 let mut snapshot = self.snapshot.lock();
2210 snapshot.entries_by_path.edit(entries_by_path_edits, &());
2211 snapshot.entries_by_id.edit(entries_by_id_edits, &());
2212 }
2213}
2214
2215fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
2216 let mut result = root_char_bag;
2217 result.extend(
2218 path.to_string_lossy()
2219 .chars()
2220 .map(|c| c.to_ascii_lowercase()),
2221 );
2222 result
2223}
2224
2225struct ScanJob {
2226 abs_path: PathBuf,
2227 path: Arc<Path>,
2228 ignore_stack: Arc<IgnoreStack>,
2229 scan_queue: Sender<ScanJob>,
2230}
2231
2232struct UpdateIgnoreStatusJob {
2233 path: Arc<Path>,
2234 ignore_stack: Arc<IgnoreStack>,
2235 ignore_queue: Sender<UpdateIgnoreStatusJob>,
2236}
2237
2238pub trait WorktreeHandle {
2239 #[cfg(any(test, feature = "test-support"))]
2240 fn flush_fs_events<'a>(
2241 &self,
2242 cx: &'a gpui::TestAppContext,
2243 ) -> futures::future::LocalBoxFuture<'a, ()>;
2244}
2245
2246impl WorktreeHandle for ModelHandle<Worktree> {
2247 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
2248 // occurred before the worktree was constructed. These events can cause the worktree to perfrom
2249 // extra directory scans, and emit extra scan-state notifications.
2250 //
2251 // This function mutates the worktree's directory and waits for those mutations to be picked up,
2252 // to ensure that all redundant FS events have already been processed.
2253 #[cfg(any(test, feature = "test-support"))]
2254 fn flush_fs_events<'a>(
2255 &self,
2256 cx: &'a gpui::TestAppContext,
2257 ) -> futures::future::LocalBoxFuture<'a, ()> {
2258 use smol::future::FutureExt;
2259
2260 let filename = "fs-event-sentinel";
2261 let tree = self.clone();
2262 let (fs, root_path) = self.read_with(cx, |tree, _| {
2263 let tree = tree.as_local().unwrap();
2264 (tree.fs.clone(), tree.abs_path().clone())
2265 });
2266
2267 async move {
2268 fs.create_file(&root_path.join(filename), Default::default())
2269 .await
2270 .unwrap();
2271 tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_some())
2272 .await;
2273
2274 fs.remove_file(&root_path.join(filename), Default::default())
2275 .await
2276 .unwrap();
2277 tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_none())
2278 .await;
2279
2280 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2281 .await;
2282 }
2283 .boxed_local()
2284 }
2285}
2286
2287#[derive(Clone, Debug)]
2288struct TraversalProgress<'a> {
2289 max_path: &'a Path,
2290 count: usize,
2291 visible_count: usize,
2292 file_count: usize,
2293 visible_file_count: usize,
2294}
2295
2296impl<'a> TraversalProgress<'a> {
2297 fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
2298 match (include_ignored, include_dirs) {
2299 (true, true) => self.count,
2300 (true, false) => self.file_count,
2301 (false, true) => self.visible_count,
2302 (false, false) => self.visible_file_count,
2303 }
2304 }
2305}
2306
2307impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
2308 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2309 self.max_path = summary.max_path.as_ref();
2310 self.count += summary.count;
2311 self.visible_count += summary.visible_count;
2312 self.file_count += summary.file_count;
2313 self.visible_file_count += summary.visible_file_count;
2314 }
2315}
2316
2317impl<'a> Default for TraversalProgress<'a> {
2318 fn default() -> Self {
2319 Self {
2320 max_path: Path::new(""),
2321 count: 0,
2322 visible_count: 0,
2323 file_count: 0,
2324 visible_file_count: 0,
2325 }
2326 }
2327}
2328
2329pub struct Traversal<'a> {
2330 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
2331 include_ignored: bool,
2332 include_dirs: bool,
2333}
2334
2335impl<'a> Traversal<'a> {
2336 pub fn advance(&mut self) -> bool {
2337 self.advance_to_offset(self.offset() + 1)
2338 }
2339
2340 pub fn advance_to_offset(&mut self, offset: usize) -> bool {
2341 self.cursor.seek_forward(
2342 &TraversalTarget::Count {
2343 count: offset,
2344 include_dirs: self.include_dirs,
2345 include_ignored: self.include_ignored,
2346 },
2347 Bias::Right,
2348 &(),
2349 )
2350 }
2351
2352 pub fn advance_to_sibling(&mut self) -> bool {
2353 while let Some(entry) = self.cursor.item() {
2354 self.cursor.seek_forward(
2355 &TraversalTarget::PathSuccessor(&entry.path),
2356 Bias::Left,
2357 &(),
2358 );
2359 if let Some(entry) = self.cursor.item() {
2360 if (self.include_dirs || !entry.is_dir())
2361 && (self.include_ignored || !entry.is_ignored)
2362 {
2363 return true;
2364 }
2365 }
2366 }
2367 false
2368 }
2369
2370 pub fn entry(&self) -> Option<&'a Entry> {
2371 self.cursor.item()
2372 }
2373
2374 pub fn offset(&self) -> usize {
2375 self.cursor
2376 .start()
2377 .count(self.include_dirs, self.include_ignored)
2378 }
2379}
2380
2381impl<'a> Iterator for Traversal<'a> {
2382 type Item = &'a Entry;
2383
2384 fn next(&mut self) -> Option<Self::Item> {
2385 if let Some(item) = self.entry() {
2386 self.advance();
2387 Some(item)
2388 } else {
2389 None
2390 }
2391 }
2392}
2393
2394#[derive(Debug)]
2395enum TraversalTarget<'a> {
2396 Path(&'a Path),
2397 PathSuccessor(&'a Path),
2398 Count {
2399 count: usize,
2400 include_ignored: bool,
2401 include_dirs: bool,
2402 },
2403}
2404
2405impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
2406 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
2407 match self {
2408 TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
2409 TraversalTarget::PathSuccessor(path) => {
2410 if !cursor_location.max_path.starts_with(path) {
2411 Ordering::Equal
2412 } else {
2413 Ordering::Greater
2414 }
2415 }
2416 TraversalTarget::Count {
2417 count,
2418 include_dirs,
2419 include_ignored,
2420 } => Ord::cmp(
2421 count,
2422 &cursor_location.count(*include_dirs, *include_ignored),
2423 ),
2424 }
2425 }
2426}
2427
2428struct ChildEntriesIter<'a> {
2429 parent_path: &'a Path,
2430 traversal: Traversal<'a>,
2431}
2432
2433impl<'a> Iterator for ChildEntriesIter<'a> {
2434 type Item = &'a Entry;
2435
2436 fn next(&mut self) -> Option<Self::Item> {
2437 if let Some(item) = self.traversal.entry() {
2438 if item.path.starts_with(&self.parent_path) {
2439 self.traversal.advance_to_sibling();
2440 return Some(item);
2441 }
2442 }
2443 None
2444 }
2445}
2446
2447impl<'a> From<&'a Entry> for proto::Entry {
2448 fn from(entry: &'a Entry) -> Self {
2449 Self {
2450 id: entry.id.to_proto(),
2451 is_dir: entry.is_dir(),
2452 path: entry.path.as_os_str().as_bytes().to_vec(),
2453 inode: entry.inode,
2454 mtime: Some(entry.mtime.into()),
2455 is_symlink: entry.is_symlink,
2456 is_ignored: entry.is_ignored,
2457 }
2458 }
2459}
2460
2461impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
2462 type Error = anyhow::Error;
2463
2464 fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
2465 if let Some(mtime) = entry.mtime {
2466 let kind = if entry.is_dir {
2467 EntryKind::Dir
2468 } else {
2469 let mut char_bag = root_char_bag.clone();
2470 char_bag.extend(
2471 String::from_utf8_lossy(&entry.path)
2472 .chars()
2473 .map(|c| c.to_ascii_lowercase()),
2474 );
2475 EntryKind::File(char_bag)
2476 };
2477 let path: Arc<Path> = PathBuf::from(OsString::from_vec(entry.path)).into();
2478 Ok(Entry {
2479 id: ProjectEntryId::from_proto(entry.id),
2480 kind,
2481 path: path.clone(),
2482 inode: entry.inode,
2483 mtime: mtime.into(),
2484 is_symlink: entry.is_symlink,
2485 is_ignored: entry.is_ignored,
2486 })
2487 } else {
2488 Err(anyhow!(
2489 "missing mtime in remote worktree entry {:?}",
2490 entry.path
2491 ))
2492 }
2493 }
2494}
2495
2496#[cfg(test)]
2497mod tests {
2498 use super::*;
2499 use crate::fs::FakeFs;
2500 use anyhow::Result;
2501 use client::test::FakeHttpClient;
2502 use fs::RealFs;
2503 use rand::prelude::*;
2504 use serde_json::json;
2505 use std::{
2506 env,
2507 fmt::Write,
2508 time::{SystemTime, UNIX_EPOCH},
2509 };
2510 use util::test::temp_tree;
2511
2512 #[gpui::test]
2513 async fn test_traversal(cx: &mut gpui::TestAppContext) {
2514 let fs = FakeFs::new(cx.background());
2515 fs.insert_tree(
2516 "/root",
2517 json!({
2518 ".gitignore": "a/b\n",
2519 "a": {
2520 "b": "",
2521 "c": "",
2522 }
2523 }),
2524 )
2525 .await;
2526
2527 let http_client = FakeHttpClient::with_404_response();
2528 let client = Client::new(http_client);
2529
2530 let tree = Worktree::local(
2531 client,
2532 Arc::from(Path::new("/root")),
2533 true,
2534 fs,
2535 Default::default(),
2536 &mut cx.to_async(),
2537 )
2538 .await
2539 .unwrap();
2540 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2541 .await;
2542
2543 tree.read_with(cx, |tree, _| {
2544 assert_eq!(
2545 tree.entries(false)
2546 .map(|entry| entry.path.as_ref())
2547 .collect::<Vec<_>>(),
2548 vec![
2549 Path::new(""),
2550 Path::new(".gitignore"),
2551 Path::new("a"),
2552 Path::new("a/c"),
2553 ]
2554 );
2555 })
2556 }
2557
2558 #[gpui::test]
2559 async fn test_rescan_with_gitignore(cx: &mut gpui::TestAppContext) {
2560 let dir = temp_tree(json!({
2561 ".git": {},
2562 ".gitignore": "ignored-dir\n",
2563 "tracked-dir": {
2564 "tracked-file1": "tracked contents",
2565 },
2566 "ignored-dir": {
2567 "ignored-file1": "ignored contents",
2568 }
2569 }));
2570
2571 let http_client = FakeHttpClient::with_404_response();
2572 let client = Client::new(http_client.clone());
2573
2574 let tree = Worktree::local(
2575 client,
2576 dir.path(),
2577 true,
2578 Arc::new(RealFs),
2579 Default::default(),
2580 &mut cx.to_async(),
2581 )
2582 .await
2583 .unwrap();
2584 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2585 .await;
2586 tree.flush_fs_events(&cx).await;
2587 cx.read(|cx| {
2588 let tree = tree.read(cx);
2589 let tracked = tree.entry_for_path("tracked-dir/tracked-file1").unwrap();
2590 let ignored = tree.entry_for_path("ignored-dir/ignored-file1").unwrap();
2591 assert_eq!(tracked.is_ignored, false);
2592 assert_eq!(ignored.is_ignored, true);
2593 });
2594
2595 std::fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
2596 std::fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
2597 tree.flush_fs_events(&cx).await;
2598 cx.read(|cx| {
2599 let tree = tree.read(cx);
2600 let dot_git = tree.entry_for_path(".git").unwrap();
2601 let tracked = tree.entry_for_path("tracked-dir/tracked-file2").unwrap();
2602 let ignored = tree.entry_for_path("ignored-dir/ignored-file2").unwrap();
2603 assert_eq!(tracked.is_ignored, false);
2604 assert_eq!(ignored.is_ignored, true);
2605 assert_eq!(dot_git.is_ignored, true);
2606 });
2607 }
2608
2609 #[gpui::test(iterations = 100)]
2610 fn test_random(mut rng: StdRng) {
2611 let operations = env::var("OPERATIONS")
2612 .map(|o| o.parse().unwrap())
2613 .unwrap_or(40);
2614 let initial_entries = env::var("INITIAL_ENTRIES")
2615 .map(|o| o.parse().unwrap())
2616 .unwrap_or(20);
2617
2618 let root_dir = tempdir::TempDir::new("worktree-test").unwrap();
2619 for _ in 0..initial_entries {
2620 randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
2621 }
2622 log::info!("Generated initial tree");
2623
2624 let (notify_tx, _notify_rx) = mpsc::unbounded();
2625 let fs = Arc::new(RealFs);
2626 let next_entry_id = Arc::new(AtomicUsize::new(0));
2627 let mut initial_snapshot = LocalSnapshot {
2628 abs_path: root_dir.path().into(),
2629 scan_id: 0,
2630 removed_entry_ids: Default::default(),
2631 ignores: Default::default(),
2632 next_entry_id: next_entry_id.clone(),
2633 snapshot: Snapshot {
2634 id: WorktreeId::from_usize(0),
2635 entries_by_path: Default::default(),
2636 entries_by_id: Default::default(),
2637 root_name: Default::default(),
2638 root_char_bag: Default::default(),
2639 },
2640 };
2641 initial_snapshot.insert_entry(
2642 Entry::new(
2643 Path::new("").into(),
2644 &smol::block_on(fs.metadata(root_dir.path()))
2645 .unwrap()
2646 .unwrap(),
2647 &next_entry_id,
2648 Default::default(),
2649 ),
2650 fs.as_ref(),
2651 );
2652 let mut scanner = BackgroundScanner::new(
2653 Arc::new(Mutex::new(initial_snapshot.clone())),
2654 notify_tx,
2655 fs.clone(),
2656 Arc::new(gpui::executor::Background::new()),
2657 );
2658 smol::block_on(scanner.scan_dirs()).unwrap();
2659 scanner.snapshot().check_invariants();
2660
2661 let mut events = Vec::new();
2662 let mut snapshots = Vec::new();
2663 let mut mutations_len = operations;
2664 while mutations_len > 1 {
2665 if !events.is_empty() && rng.gen_bool(0.4) {
2666 let len = rng.gen_range(0..=events.len());
2667 let to_deliver = events.drain(0..len).collect::<Vec<_>>();
2668 log::info!("Delivering events: {:#?}", to_deliver);
2669 smol::block_on(scanner.process_events(to_deliver));
2670 scanner.snapshot().check_invariants();
2671 } else {
2672 events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
2673 mutations_len -= 1;
2674 }
2675
2676 if rng.gen_bool(0.2) {
2677 snapshots.push(scanner.snapshot());
2678 }
2679 }
2680 log::info!("Quiescing: {:#?}", events);
2681 smol::block_on(scanner.process_events(events));
2682 scanner.snapshot().check_invariants();
2683
2684 let (notify_tx, _notify_rx) = mpsc::unbounded();
2685 let mut new_scanner = BackgroundScanner::new(
2686 Arc::new(Mutex::new(initial_snapshot)),
2687 notify_tx,
2688 scanner.fs.clone(),
2689 scanner.executor.clone(),
2690 );
2691 smol::block_on(new_scanner.scan_dirs()).unwrap();
2692 assert_eq!(
2693 scanner.snapshot().to_vec(true),
2694 new_scanner.snapshot().to_vec(true)
2695 );
2696
2697 for mut prev_snapshot in snapshots {
2698 let include_ignored = rng.gen::<bool>();
2699 if !include_ignored {
2700 let mut entries_by_path_edits = Vec::new();
2701 let mut entries_by_id_edits = Vec::new();
2702 for entry in prev_snapshot
2703 .entries_by_id
2704 .cursor::<()>()
2705 .filter(|e| e.is_ignored)
2706 {
2707 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
2708 entries_by_id_edits.push(Edit::Remove(entry.id));
2709 }
2710
2711 prev_snapshot
2712 .entries_by_path
2713 .edit(entries_by_path_edits, &());
2714 prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
2715 }
2716
2717 let update = scanner
2718 .snapshot()
2719 .build_update(&prev_snapshot, 0, 0, include_ignored);
2720 prev_snapshot.apply_remote_update(update).unwrap();
2721 assert_eq!(
2722 prev_snapshot.to_vec(true),
2723 scanner.snapshot().to_vec(include_ignored)
2724 );
2725 }
2726 }
2727
2728 fn randomly_mutate_tree(
2729 root_path: &Path,
2730 insertion_probability: f64,
2731 rng: &mut impl Rng,
2732 ) -> Result<Vec<fsevent::Event>> {
2733 let root_path = root_path.canonicalize().unwrap();
2734 let (dirs, files) = read_dir_recursive(root_path.clone());
2735
2736 let mut events = Vec::new();
2737 let mut record_event = |path: PathBuf| {
2738 events.push(fsevent::Event {
2739 event_id: SystemTime::now()
2740 .duration_since(UNIX_EPOCH)
2741 .unwrap()
2742 .as_secs(),
2743 flags: fsevent::StreamFlags::empty(),
2744 path,
2745 });
2746 };
2747
2748 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
2749 let path = dirs.choose(rng).unwrap();
2750 let new_path = path.join(gen_name(rng));
2751
2752 if rng.gen() {
2753 log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
2754 std::fs::create_dir(&new_path)?;
2755 } else {
2756 log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
2757 std::fs::write(&new_path, "")?;
2758 }
2759 record_event(new_path);
2760 } else if rng.gen_bool(0.05) {
2761 let ignore_dir_path = dirs.choose(rng).unwrap();
2762 let ignore_path = ignore_dir_path.join(&*GITIGNORE);
2763
2764 let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
2765 let files_to_ignore = {
2766 let len = rng.gen_range(0..=subfiles.len());
2767 subfiles.choose_multiple(rng, len)
2768 };
2769 let dirs_to_ignore = {
2770 let len = rng.gen_range(0..subdirs.len());
2771 subdirs.choose_multiple(rng, len)
2772 };
2773
2774 let mut ignore_contents = String::new();
2775 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
2776 write!(
2777 ignore_contents,
2778 "{}\n",
2779 path_to_ignore
2780 .strip_prefix(&ignore_dir_path)?
2781 .to_str()
2782 .unwrap()
2783 )
2784 .unwrap();
2785 }
2786 log::info!(
2787 "Creating {:?} with contents:\n{}",
2788 ignore_path.strip_prefix(&root_path)?,
2789 ignore_contents
2790 );
2791 std::fs::write(&ignore_path, ignore_contents).unwrap();
2792 record_event(ignore_path);
2793 } else {
2794 let old_path = {
2795 let file_path = files.choose(rng);
2796 let dir_path = dirs[1..].choose(rng);
2797 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
2798 };
2799
2800 let is_rename = rng.gen();
2801 if is_rename {
2802 let new_path_parent = dirs
2803 .iter()
2804 .filter(|d| !d.starts_with(old_path))
2805 .choose(rng)
2806 .unwrap();
2807
2808 let overwrite_existing_dir =
2809 !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
2810 let new_path = if overwrite_existing_dir {
2811 std::fs::remove_dir_all(&new_path_parent).ok();
2812 new_path_parent.to_path_buf()
2813 } else {
2814 new_path_parent.join(gen_name(rng))
2815 };
2816
2817 log::info!(
2818 "Renaming {:?} to {}{:?}",
2819 old_path.strip_prefix(&root_path)?,
2820 if overwrite_existing_dir {
2821 "overwrite "
2822 } else {
2823 ""
2824 },
2825 new_path.strip_prefix(&root_path)?
2826 );
2827 std::fs::rename(&old_path, &new_path)?;
2828 record_event(old_path.clone());
2829 record_event(new_path);
2830 } else if old_path.is_dir() {
2831 let (dirs, files) = read_dir_recursive(old_path.clone());
2832
2833 log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
2834 std::fs::remove_dir_all(&old_path).unwrap();
2835 for file in files {
2836 record_event(file);
2837 }
2838 for dir in dirs {
2839 record_event(dir);
2840 }
2841 } else {
2842 log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
2843 std::fs::remove_file(old_path).unwrap();
2844 record_event(old_path.clone());
2845 }
2846 }
2847
2848 Ok(events)
2849 }
2850
2851 fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
2852 let child_entries = std::fs::read_dir(&path).unwrap();
2853 let mut dirs = vec![path];
2854 let mut files = Vec::new();
2855 for child_entry in child_entries {
2856 let child_path = child_entry.unwrap().path();
2857 if child_path.is_dir() {
2858 let (child_dirs, child_files) = read_dir_recursive(child_path);
2859 dirs.extend(child_dirs);
2860 files.extend(child_files);
2861 } else {
2862 files.push(child_path);
2863 }
2864 }
2865 (dirs, files)
2866 }
2867
2868 fn gen_name(rng: &mut impl Rng) -> String {
2869 (0..6)
2870 .map(|_| rng.sample(rand::distributions::Alphanumeric))
2871 .map(char::from)
2872 .collect()
2873 }
2874
2875 impl LocalSnapshot {
2876 fn check_invariants(&self) {
2877 let mut files = self.files(true, 0);
2878 let mut visible_files = self.files(false, 0);
2879 for entry in self.entries_by_path.cursor::<()>() {
2880 if entry.is_file() {
2881 assert_eq!(files.next().unwrap().inode, entry.inode);
2882 if !entry.is_ignored {
2883 assert_eq!(visible_files.next().unwrap().inode, entry.inode);
2884 }
2885 }
2886 }
2887 assert!(files.next().is_none());
2888 assert!(visible_files.next().is_none());
2889
2890 let mut bfs_paths = Vec::new();
2891 let mut stack = vec![Path::new("")];
2892 while let Some(path) = stack.pop() {
2893 bfs_paths.push(path);
2894 let ix = stack.len();
2895 for child_entry in self.child_entries(path) {
2896 stack.insert(ix, &child_entry.path);
2897 }
2898 }
2899
2900 let dfs_paths = self
2901 .entries_by_path
2902 .cursor::<()>()
2903 .map(|e| e.path.as_ref())
2904 .collect::<Vec<_>>();
2905 assert_eq!(bfs_paths, dfs_paths);
2906
2907 for (ignore_parent_path, _) in &self.ignores {
2908 assert!(self.entry_for_path(ignore_parent_path).is_some());
2909 assert!(self
2910 .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
2911 .is_some());
2912 }
2913 }
2914
2915 fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
2916 let mut paths = Vec::new();
2917 for entry in self.entries_by_path.cursor::<()>() {
2918 if include_ignored || !entry.is_ignored {
2919 paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
2920 }
2921 }
2922 paths.sort_by(|a, b| a.0.cmp(&b.0));
2923 paths
2924 }
2925 }
2926}