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