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