use crate::{ copy_recursive, ignore::IgnoreStack, DiagnosticSummary, ProjectEntryId, RemoveOptions, }; use ::ignore::gitignore::{Gitignore, GitignoreBuilder}; use anyhow::{anyhow, Context, Result}; use client::{proto, Client}; use clock::ReplicaId; use collections::{HashMap, VecDeque}; use fs::{repository::GitRepository, Fs, LineEnding}; use futures::{ channel::{ mpsc::{self, UnboundedSender}, oneshot, }, select_biased, task::Poll, Stream, StreamExt, }; use fuzzy::CharBag; use git::{DOT_GIT, GITIGNORE}; use gpui::{executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task}; use language::{ proto::{ deserialize_fingerprint, deserialize_version, serialize_fingerprint, serialize_line_ending, serialize_version, }, Buffer, DiagnosticEntry, File as _, PointUtf16, Rope, RopeFingerprint, Unclipped, }; use lsp::LanguageServerId; use parking_lot::Mutex; use postage::{ barrier, prelude::{Sink as _, Stream as _}, watch, }; use smol::channel::{self, Sender}; use std::{ any::Any, cmp::{self, Ordering}, convert::TryFrom, ffi::OsStr, fmt, future::Future, mem, ops::{Deref, DerefMut}, path::{Path, PathBuf}, pin::Pin, sync::{ atomic::{AtomicUsize, Ordering::SeqCst}, Arc, }, time::{Duration, SystemTime}, }; use sum_tree::{Bias, Edit, SeekTarget, SumTree, TreeSet}; use util::{paths::HOME, ResultExt, TryFutureExt}; #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)] pub struct WorktreeId(usize); pub enum Worktree { Local(LocalWorktree), Remote(RemoteWorktree), } pub struct LocalWorktree { snapshot: LocalSnapshot, path_changes_tx: channel::Sender<(Vec, barrier::Sender)>, is_scanning: (watch::Sender, watch::Receiver), _background_scanner_task: Task<()>, share: Option, diagnostics: HashMap< Arc, Vec<( LanguageServerId, Vec>>, )>, >, diagnostic_summaries: HashMap, HashMap>, client: Arc, fs: Arc, visible: bool, } pub struct RemoteWorktree { snapshot: Snapshot, background_snapshot: Arc>, project_id: u64, client: Arc, updates_tx: Option>, snapshot_subscriptions: VecDeque<(usize, oneshot::Sender<()>)>, replica_id: ReplicaId, diagnostic_summaries: HashMap, HashMap>, visible: bool, disconnected: bool, } #[derive(Clone)] pub struct Snapshot { id: WorktreeId, abs_path: Arc, root_name: String, root_char_bag: CharBag, entries_by_path: SumTree, entries_by_id: SumTree, /// A number that increases every time the worktree begins scanning /// a set of paths from the filesystem. This scanning could be caused /// by some operation performed on the worktree, such as reading or /// writing a file, or by an event reported by the filesystem. scan_id: usize, /// The latest scan id that has completed, and whose preceding scans /// have all completed. The current `scan_id` could be more than one /// greater than the `completed_scan_id` if operations are performed /// on the worktree while it is processing a file-system event. completed_scan_id: usize, } #[derive(Clone)] pub struct GitRepositoryEntry { pub(crate) repo: Arc>, pub(crate) scan_id: usize, // Path to folder containing the .git file or directory pub(crate) content_path: Arc, // Path to the actual .git folder. // Note: if .git is a file, this points to the folder indicated by the .git file pub(crate) git_dir_path: Arc, } impl std::fmt::Debug for GitRepositoryEntry { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("GitRepositoryEntry") .field("content_path", &self.content_path) .field("git_dir_path", &self.git_dir_path) .finish() } } #[derive(Debug)] pub struct LocalSnapshot { ignores_by_parent_abs_path: HashMap, (Arc, usize)>, git_repositories: Vec, removed_entry_ids: HashMap, next_entry_id: Arc, snapshot: Snapshot, } impl Clone for LocalSnapshot { fn clone(&self) -> Self { Self { ignores_by_parent_abs_path: self.ignores_by_parent_abs_path.clone(), git_repositories: self.git_repositories.iter().cloned().collect(), removed_entry_ids: self.removed_entry_ids.clone(), next_entry_id: self.next_entry_id.clone(), snapshot: self.snapshot.clone(), } } } impl Deref for LocalSnapshot { type Target = Snapshot; fn deref(&self) -> &Self::Target { &self.snapshot } } impl DerefMut for LocalSnapshot { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.snapshot } } enum ScanState { Started, Updated { snapshot: LocalSnapshot, changes: HashMap, PathChange>, barrier: Option, scanning: bool, }, } struct ShareState { project_id: u64, snapshots_tx: watch::Sender, resume_updates: watch::Sender<()>, _maintain_remote_snapshot: Task>, } pub enum Event { UpdatedEntries(HashMap, PathChange>), UpdatedGitRepositories(Vec), } impl Entity for Worktree { type Event = Event; } impl Worktree { pub async fn local( client: Arc, path: impl Into>, visible: bool, fs: Arc, next_entry_id: Arc, cx: &mut AsyncAppContext, ) -> Result> { // After determining whether the root entry is a file or a directory, populate the // snapshot's "root name", which will be used for the purpose of fuzzy matching. let abs_path = path.into(); let metadata = fs .metadata(&abs_path) .await .context("failed to stat worktree path")?; Ok(cx.add_model(move |cx: &mut ModelContext| { let root_name = abs_path .file_name() .map_or(String::new(), |f| f.to_string_lossy().to_string()); let mut snapshot = LocalSnapshot { ignores_by_parent_abs_path: Default::default(), git_repositories: Default::default(), removed_entry_ids: Default::default(), next_entry_id, snapshot: Snapshot { id: WorktreeId::from_usize(cx.model_id()), abs_path: abs_path.clone(), root_name: root_name.clone(), root_char_bag: root_name.chars().map(|c| c.to_ascii_lowercase()).collect(), entries_by_path: Default::default(), entries_by_id: Default::default(), scan_id: 1, completed_scan_id: 0, }, }; if let Some(metadata) = metadata { snapshot.insert_entry( Entry::new( Arc::from(Path::new("")), &metadata, &snapshot.next_entry_id, snapshot.root_char_bag, ), fs.as_ref(), ); } let (path_changes_tx, path_changes_rx) = channel::unbounded(); let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded(); cx.spawn_weak(|this, mut cx| async move { while let Some((state, this)) = scan_states_rx.next().await.zip(this.upgrade(&cx)) { this.update(&mut cx, |this, cx| { let this = this.as_local_mut().unwrap(); match state { ScanState::Started => { *this.is_scanning.0.borrow_mut() = true; } ScanState::Updated { snapshot, changes, barrier, scanning, } => { *this.is_scanning.0.borrow_mut() = scanning; this.set_snapshot(snapshot, cx); cx.emit(Event::UpdatedEntries(changes)); drop(barrier); } } cx.notify(); }); } }) .detach(); let background_scanner_task = cx.background().spawn({ let fs = fs.clone(); let snapshot = snapshot.clone(); let background = cx.background().clone(); async move { let events = fs.watch(&abs_path, Duration::from_millis(100)).await; BackgroundScanner::new( snapshot, fs, scan_states_tx, background, path_changes_rx, ) .run(events) .await; } }); Worktree::Local(LocalWorktree { snapshot, is_scanning: watch::channel_with(true), share: None, path_changes_tx, _background_scanner_task: background_scanner_task, diagnostics: Default::default(), diagnostic_summaries: Default::default(), client, fs, visible, }) })) } pub fn remote( project_remote_id: u64, replica_id: ReplicaId, worktree: proto::WorktreeMetadata, client: Arc, cx: &mut AppContext, ) -> ModelHandle { cx.add_model(|cx: &mut ModelContext| { let snapshot = Snapshot { id: WorktreeId(worktree.id as usize), abs_path: Arc::from(PathBuf::from(worktree.abs_path)), root_name: worktree.root_name.clone(), root_char_bag: worktree .root_name .chars() .map(|c| c.to_ascii_lowercase()) .collect(), entries_by_path: Default::default(), entries_by_id: Default::default(), scan_id: 1, completed_scan_id: 0, }; let (updates_tx, mut updates_rx) = mpsc::unbounded(); let background_snapshot = Arc::new(Mutex::new(snapshot.clone())); let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel(); cx.background() .spawn({ let background_snapshot = background_snapshot.clone(); async move { while let Some(update) = updates_rx.next().await { if let Err(error) = background_snapshot.lock().apply_remote_update(update) { log::error!("error applying worktree update: {}", error); } snapshot_updated_tx.send(()).await.ok(); } } }) .detach(); cx.spawn_weak(|this, mut cx| async move { while (snapshot_updated_rx.recv().await).is_some() { if let Some(this) = this.upgrade(&cx) { this.update(&mut cx, |this, cx| { let this = this.as_remote_mut().unwrap(); this.snapshot = this.background_snapshot.lock().clone(); cx.emit(Event::UpdatedEntries(Default::default())); cx.notify(); while let Some((scan_id, _)) = this.snapshot_subscriptions.front() { if this.observed_snapshot(*scan_id) { let (_, tx) = this.snapshot_subscriptions.pop_front().unwrap(); let _ = tx.send(()); } else { break; } } }); } else { break; } } }) .detach(); Worktree::Remote(RemoteWorktree { project_id: project_remote_id, replica_id, snapshot: snapshot.clone(), background_snapshot, updates_tx: Some(updates_tx), snapshot_subscriptions: Default::default(), client: client.clone(), diagnostic_summaries: Default::default(), visible: worktree.visible, disconnected: false, }) }) } pub fn as_local(&self) -> Option<&LocalWorktree> { if let Worktree::Local(worktree) = self { Some(worktree) } else { None } } pub fn as_remote(&self) -> Option<&RemoteWorktree> { if let Worktree::Remote(worktree) = self { Some(worktree) } else { None } } pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> { if let Worktree::Local(worktree) = self { Some(worktree) } else { None } } pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> { if let Worktree::Remote(worktree) = self { Some(worktree) } else { None } } pub fn is_local(&self) -> bool { matches!(self, Worktree::Local(_)) } pub fn is_remote(&self) -> bool { !self.is_local() } pub fn snapshot(&self) -> Snapshot { match self { Worktree::Local(worktree) => worktree.snapshot().snapshot, Worktree::Remote(worktree) => worktree.snapshot(), } } pub fn scan_id(&self) -> usize { match self { Worktree::Local(worktree) => worktree.snapshot.scan_id, Worktree::Remote(worktree) => worktree.snapshot.scan_id, } } pub fn completed_scan_id(&self) -> usize { match self { Worktree::Local(worktree) => worktree.snapshot.completed_scan_id, Worktree::Remote(worktree) => worktree.snapshot.completed_scan_id, } } pub fn is_visible(&self) -> bool { match self { Worktree::Local(worktree) => worktree.visible, Worktree::Remote(worktree) => worktree.visible, } } pub fn replica_id(&self) -> ReplicaId { match self { Worktree::Local(_) => 0, Worktree::Remote(worktree) => worktree.replica_id, } } pub fn diagnostic_summaries( &self, ) -> impl Iterator, LanguageServerId, DiagnosticSummary)> + '_ { match self { Worktree::Local(worktree) => &worktree.diagnostic_summaries, Worktree::Remote(worktree) => &worktree.diagnostic_summaries, } .iter() .flat_map(|(path, summaries)| { summaries .iter() .map(move |(&server_id, &summary)| (path.clone(), server_id, summary)) }) } pub fn abs_path(&self) -> Arc { match self { Worktree::Local(worktree) => worktree.abs_path.clone(), Worktree::Remote(worktree) => worktree.abs_path.clone(), } } } impl LocalWorktree { pub fn contains_abs_path(&self, path: &Path) -> bool { path.starts_with(&self.abs_path) } fn absolutize(&self, path: &Path) -> PathBuf { if path.file_name().is_some() { self.abs_path.join(path) } else { self.abs_path.to_path_buf() } } pub(crate) fn load_buffer( &mut self, path: &Path, cx: &mut ModelContext, ) -> Task>> { let path = Arc::from(path); cx.spawn(move |this, mut cx| async move { let (file, contents, diff_base) = this .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx)) .await?; Ok(cx.add_model(|cx| { let mut buffer = Buffer::from_file(0, contents, diff_base, Arc::new(file), cx); buffer.git_diff_recalc(cx); buffer })) }) } pub fn diagnostics_for_path( &self, path: &Path, ) -> Vec<( LanguageServerId, Vec>>, )> { self.diagnostics.get(path).cloned().unwrap_or_default() } pub fn update_diagnostics( &mut self, server_id: LanguageServerId, worktree_path: Arc, diagnostics: Vec>>, _: &mut ModelContext, ) -> Result { let summaries_by_server_id = self .diagnostic_summaries .entry(worktree_path.clone()) .or_default(); let old_summary = summaries_by_server_id .remove(&server_id) .unwrap_or_default(); let new_summary = DiagnosticSummary::new(&diagnostics); if new_summary.is_empty() { if let Some(diagnostics_by_server_id) = self.diagnostics.get_mut(&worktree_path) { if let Ok(ix) = diagnostics_by_server_id.binary_search_by_key(&server_id, |e| e.0) { diagnostics_by_server_id.remove(ix); } if diagnostics_by_server_id.is_empty() { self.diagnostics.remove(&worktree_path); } } } else { summaries_by_server_id.insert(server_id, new_summary); let diagnostics_by_server_id = self.diagnostics.entry(worktree_path.clone()).or_default(); match diagnostics_by_server_id.binary_search_by_key(&server_id, |e| e.0) { Ok(ix) => { diagnostics_by_server_id[ix] = (server_id, diagnostics); } Err(ix) => { diagnostics_by_server_id.insert(ix, (server_id, diagnostics)); } } } if !old_summary.is_empty() || !new_summary.is_empty() { if let Some(share) = self.share.as_ref() { self.client .send(proto::UpdateDiagnosticSummary { project_id: share.project_id, worktree_id: self.id().to_proto(), summary: Some(proto::DiagnosticSummary { path: worktree_path.to_string_lossy().to_string(), language_server_id: server_id.0 as u64, error_count: new_summary.error_count as u32, warning_count: new_summary.warning_count as u32, }), }) .log_err(); } } Ok(!old_summary.is_empty() || !new_summary.is_empty()) } fn set_snapshot(&mut self, new_snapshot: LocalSnapshot, cx: &mut ModelContext) { let updated_repos = Self::changed_repos( &self.snapshot.git_repositories, &new_snapshot.git_repositories, ); self.snapshot = new_snapshot; if let Some(share) = self.share.as_mut() { *share.snapshots_tx.borrow_mut() = self.snapshot.clone(); } if !updated_repos.is_empty() { cx.emit(Event::UpdatedGitRepositories(updated_repos)); } } fn changed_repos( old_repos: &[GitRepositoryEntry], new_repos: &[GitRepositoryEntry], ) -> Vec { fn diff<'a>( a: &'a [GitRepositoryEntry], b: &'a [GitRepositoryEntry], updated: &mut HashMap<&'a Path, GitRepositoryEntry>, ) { for a_repo in a { let matched = b.iter().find(|b_repo| { a_repo.git_dir_path == b_repo.git_dir_path && a_repo.scan_id == b_repo.scan_id }); if matched.is_none() { updated.insert(a_repo.git_dir_path.as_ref(), a_repo.clone()); } } } let mut updated = HashMap::<&Path, GitRepositoryEntry>::default(); diff(old_repos, new_repos, &mut updated); diff(new_repos, old_repos, &mut updated); updated.into_values().collect() } pub fn scan_complete(&self) -> impl Future { let mut is_scanning_rx = self.is_scanning.1.clone(); async move { let mut is_scanning = is_scanning_rx.borrow().clone(); while is_scanning { if let Some(value) = is_scanning_rx.recv().await { is_scanning = value; } else { break; } } } } pub fn snapshot(&self) -> LocalSnapshot { self.snapshot.clone() } pub fn metadata_proto(&self) -> proto::WorktreeMetadata { proto::WorktreeMetadata { id: self.id().to_proto(), root_name: self.root_name().to_string(), visible: self.visible, abs_path: self.abs_path().as_os_str().to_string_lossy().into(), } } fn load( &self, path: &Path, cx: &mut ModelContext, ) -> Task)>> { let handle = cx.handle(); let path = Arc::from(path); let abs_path = self.absolutize(&path); let fs = self.fs.clone(); let snapshot = self.snapshot(); cx.spawn(|this, mut cx| async move { let text = fs.load(&abs_path).await?; let diff_base = if let Some(repo) = snapshot.repo_for(&path) { if let Ok(repo_relative) = path.strip_prefix(repo.content_path) { let repo_relative = repo_relative.to_owned(); cx.background() .spawn(async move { repo.repo.lock().load_index_text(&repo_relative) }) .await } else { None } } else { None }; // Eagerly populate the snapshot with an updated entry for the loaded file let entry = this .update(&mut cx, |this, cx| { this.as_local().unwrap().refresh_entry(path, None, cx) }) .await?; Ok(( File { entry_id: entry.id, worktree: handle, path: entry.path, mtime: entry.mtime, is_local: true, is_deleted: false, }, text, diff_base, )) }) } pub fn save_buffer( &self, buffer_handle: ModelHandle, path: Arc, has_changed_file: bool, cx: &mut ModelContext, ) -> Task> { let handle = cx.handle(); let buffer = buffer_handle.read(cx); let rpc = self.client.clone(); let buffer_id = buffer.remote_id(); let project_id = self.share.as_ref().map(|share| share.project_id); let text = buffer.as_rope().clone(); let fingerprint = text.fingerprint(); let version = buffer.version(); let save = self.write_file(path, text, buffer.line_ending(), cx); cx.as_mut().spawn(|mut cx| async move { let entry = save.await?; if has_changed_file { let new_file = Arc::new(File { entry_id: entry.id, worktree: handle, path: entry.path, mtime: entry.mtime, is_local: true, is_deleted: false, }); if let Some(project_id) = project_id { rpc.send(proto::UpdateBufferFile { project_id, buffer_id, file: Some(new_file.to_proto()), }) .log_err(); } buffer_handle.update(&mut cx, |buffer, cx| { if has_changed_file { buffer.file_updated(new_file, cx).detach(); } }); } if let Some(project_id) = project_id { rpc.send(proto::BufferSaved { project_id, buffer_id, version: serialize_version(&version), mtime: Some(entry.mtime.into()), fingerprint: serialize_fingerprint(fingerprint), })?; } buffer_handle.update(&mut cx, |buffer, cx| { buffer.did_save(version.clone(), fingerprint, entry.mtime, cx); }); Ok((version, fingerprint, entry.mtime)) }) } pub fn create_entry( &self, path: impl Into>, is_dir: bool, cx: &mut ModelContext, ) -> Task> { let path = path.into(); let abs_path = self.absolutize(&path); let fs = self.fs.clone(); let write = cx.background().spawn(async move { if is_dir { fs.create_dir(&abs_path).await } else { fs.save(&abs_path, &Default::default(), Default::default()) .await } }); cx.spawn(|this, mut cx| async move { write.await?; this.update(&mut cx, |this, cx| { this.as_local_mut().unwrap().refresh_entry(path, None, cx) }) .await }) } pub fn write_file( &self, path: impl Into>, text: Rope, line_ending: LineEnding, cx: &mut ModelContext, ) -> Task> { let path = path.into(); let abs_path = self.absolutize(&path); let fs = self.fs.clone(); let write = cx .background() .spawn(async move { fs.save(&abs_path, &text, line_ending).await }); cx.spawn(|this, mut cx| async move { write.await?; this.update(&mut cx, |this, cx| { this.as_local_mut().unwrap().refresh_entry(path, None, cx) }) .await }) } pub fn delete_entry( &self, entry_id: ProjectEntryId, cx: &mut ModelContext, ) -> Option>> { let entry = self.entry_for_id(entry_id)?.clone(); let abs_path = self.abs_path.clone(); let fs = self.fs.clone(); let delete = cx.background().spawn(async move { let mut abs_path = fs.canonicalize(&abs_path).await?; if entry.path.file_name().is_some() { abs_path = abs_path.join(&entry.path); } if entry.is_file() { fs.remove_file(&abs_path, Default::default()).await?; } else { fs.remove_dir( &abs_path, RemoveOptions { recursive: true, ignore_if_not_exists: false, }, ) .await?; } anyhow::Ok(abs_path) }); Some(cx.spawn(|this, mut cx| async move { let abs_path = delete.await?; let (tx, mut rx) = barrier::channel(); this.update(&mut cx, |this, _| { this.as_local_mut() .unwrap() .path_changes_tx .try_send((vec![abs_path], tx)) })?; rx.recv().await; Ok(()) })) } pub fn rename_entry( &self, entry_id: ProjectEntryId, new_path: impl Into>, cx: &mut ModelContext, ) -> Option>> { let old_path = self.entry_for_id(entry_id)?.path.clone(); let new_path = new_path.into(); let abs_old_path = self.absolutize(&old_path); let abs_new_path = self.absolutize(&new_path); let fs = self.fs.clone(); let rename = cx.background().spawn(async move { fs.rename(&abs_old_path, &abs_new_path, Default::default()) .await }); Some(cx.spawn(|this, mut cx| async move { rename.await?; this.update(&mut cx, |this, cx| { this.as_local_mut() .unwrap() .refresh_entry(new_path.clone(), Some(old_path), cx) }) .await })) } pub fn copy_entry( &self, entry_id: ProjectEntryId, new_path: impl Into>, cx: &mut ModelContext, ) -> Option>> { let old_path = self.entry_for_id(entry_id)?.path.clone(); let new_path = new_path.into(); let abs_old_path = self.absolutize(&old_path); let abs_new_path = self.absolutize(&new_path); let fs = self.fs.clone(); let copy = cx.background().spawn(async move { copy_recursive( fs.as_ref(), &abs_old_path, &abs_new_path, Default::default(), ) .await }); Some(cx.spawn(|this, mut cx| async move { copy.await?; this.update(&mut cx, |this, cx| { this.as_local_mut() .unwrap() .refresh_entry(new_path.clone(), None, cx) }) .await })) } fn refresh_entry( &self, path: Arc, old_path: Option>, cx: &mut ModelContext, ) -> Task> { let fs = self.fs.clone(); let abs_root_path = self.abs_path.clone(); let path_changes_tx = self.path_changes_tx.clone(); cx.spawn_weak(move |this, mut cx| async move { let abs_path = fs.canonicalize(&abs_root_path).await?; let mut paths = Vec::with_capacity(2); paths.push(if path.file_name().is_some() { abs_path.join(&path) } else { abs_path.clone() }); if let Some(old_path) = old_path { paths.push(if old_path.file_name().is_some() { abs_path.join(&old_path) } else { abs_path.clone() }); } let (tx, mut rx) = barrier::channel(); path_changes_tx.try_send((paths, tx))?; rx.recv().await; this.upgrade(&cx) .ok_or_else(|| anyhow!("worktree was dropped"))? .update(&mut cx, |this, _| { this.entry_for_path(path) .cloned() .ok_or_else(|| anyhow!("failed to read path after update")) }) }) } pub fn share(&mut self, project_id: u64, cx: &mut ModelContext) -> Task> { let (share_tx, share_rx) = oneshot::channel(); if let Some(share) = self.share.as_mut() { let _ = share_tx.send(()); *share.resume_updates.borrow_mut() = (); } else { let (snapshots_tx, mut snapshots_rx) = watch::channel_with(self.snapshot()); let (resume_updates_tx, mut resume_updates_rx) = watch::channel(); let worktree_id = cx.model_id() as u64; for (path, summaries) in &self.diagnostic_summaries { for (&server_id, summary) in summaries { if let Err(e) = self.client.send(proto::UpdateDiagnosticSummary { project_id, worktree_id, summary: Some(summary.to_proto(server_id, &path)), }) { return Task::ready(Err(e)); } } } let _maintain_remote_snapshot = cx.background().spawn({ let client = self.client.clone(); async move { let mut share_tx = Some(share_tx); let mut prev_snapshot = LocalSnapshot { ignores_by_parent_abs_path: Default::default(), git_repositories: Default::default(), removed_entry_ids: Default::default(), next_entry_id: Default::default(), snapshot: Snapshot { id: WorktreeId(worktree_id as usize), abs_path: Path::new("").into(), root_name: Default::default(), root_char_bag: Default::default(), entries_by_path: Default::default(), entries_by_id: Default::default(), scan_id: 0, completed_scan_id: 0, }, }; while let Some(snapshot) = snapshots_rx.recv().await { #[cfg(any(test, feature = "test-support"))] const MAX_CHUNK_SIZE: usize = 2; #[cfg(not(any(test, feature = "test-support")))] const MAX_CHUNK_SIZE: usize = 256; let update = snapshot.build_update(&prev_snapshot, project_id, worktree_id, true); for update in proto::split_worktree_update(update, MAX_CHUNK_SIZE) { let _ = resume_updates_rx.try_recv(); while let Err(error) = client.request(update.clone()).await { log::error!("failed to send worktree update: {}", error); log::info!("waiting to resume updates"); if resume_updates_rx.next().await.is_none() { return Ok(()); } } } if let Some(share_tx) = share_tx.take() { let _ = share_tx.send(()); } prev_snapshot = snapshot; } Ok::<_, anyhow::Error>(()) } .log_err() }); self.share = Some(ShareState { project_id, snapshots_tx, resume_updates: resume_updates_tx, _maintain_remote_snapshot, }); } cx.foreground() .spawn(async move { share_rx.await.map_err(|_| anyhow!("share ended")) }) } pub fn unshare(&mut self) { self.share.take(); } pub fn is_shared(&self) -> bool { self.share.is_some() } } impl RemoteWorktree { fn snapshot(&self) -> Snapshot { self.snapshot.clone() } pub fn disconnected_from_host(&mut self) { self.updates_tx.take(); self.snapshot_subscriptions.clear(); self.disconnected = true; } pub fn save_buffer( &self, buffer_handle: ModelHandle, cx: &mut ModelContext, ) -> Task> { let buffer = buffer_handle.read(cx); let buffer_id = buffer.remote_id(); let version = buffer.version(); let rpc = self.client.clone(); let project_id = self.project_id; cx.as_mut().spawn(|mut cx| async move { let response = rpc .request(proto::SaveBuffer { project_id, buffer_id, version: serialize_version(&version), }) .await?; let version = deserialize_version(&response.version); let fingerprint = deserialize_fingerprint(&response.fingerprint)?; let mtime = response .mtime .ok_or_else(|| anyhow!("missing mtime"))? .into(); buffer_handle.update(&mut cx, |buffer, cx| { buffer.did_save(version.clone(), fingerprint, mtime, cx); }); Ok((version, fingerprint, mtime)) }) } pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) { if let Some(updates_tx) = &self.updates_tx { updates_tx .unbounded_send(update) .expect("consumer runs to completion"); } } fn observed_snapshot(&self, scan_id: usize) -> bool { self.completed_scan_id >= scan_id } fn wait_for_snapshot(&mut self, scan_id: usize) -> impl Future> { let (tx, rx) = oneshot::channel(); if self.observed_snapshot(scan_id) { let _ = tx.send(()); } else if self.disconnected { drop(tx); } else { match self .snapshot_subscriptions .binary_search_by_key(&scan_id, |probe| probe.0) { Ok(ix) | Err(ix) => self.snapshot_subscriptions.insert(ix, (scan_id, tx)), } } async move { rx.await?; Ok(()) } } pub fn update_diagnostic_summary( &mut self, path: Arc, summary: &proto::DiagnosticSummary, ) { let server_id = LanguageServerId(summary.language_server_id as usize); let summary = DiagnosticSummary { error_count: summary.error_count as usize, warning_count: summary.warning_count as usize, }; if summary.is_empty() { if let Some(summaries) = self.diagnostic_summaries.get_mut(&path) { summaries.remove(&server_id); if summaries.is_empty() { self.diagnostic_summaries.remove(&path); } } } else { self.diagnostic_summaries .entry(path) .or_default() .insert(server_id, summary); } } pub fn insert_entry( &mut self, entry: proto::Entry, scan_id: usize, cx: &mut ModelContext, ) -> Task> { let wait_for_snapshot = self.wait_for_snapshot(scan_id); cx.spawn(|this, mut cx| async move { wait_for_snapshot.await?; this.update(&mut cx, |worktree, _| { let worktree = worktree.as_remote_mut().unwrap(); let mut snapshot = worktree.background_snapshot.lock(); let entry = snapshot.insert_entry(entry); worktree.snapshot = snapshot.clone(); entry }) }) } pub(crate) fn delete_entry( &mut self, id: ProjectEntryId, scan_id: usize, cx: &mut ModelContext, ) -> Task> { let wait_for_snapshot = self.wait_for_snapshot(scan_id); cx.spawn(|this, mut cx| async move { wait_for_snapshot.await?; this.update(&mut cx, |worktree, _| { let worktree = worktree.as_remote_mut().unwrap(); let mut snapshot = worktree.background_snapshot.lock(); snapshot.delete_entry(id); worktree.snapshot = snapshot.clone(); }); Ok(()) }) } } impl Snapshot { pub fn id(&self) -> WorktreeId { self.id } pub fn abs_path(&self) -> &Arc { &self.abs_path } pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool { self.entries_by_id.get(&entry_id, &()).is_some() } pub(crate) fn insert_entry(&mut self, entry: proto::Entry) -> Result { let entry = Entry::try_from((&self.root_char_bag, entry))?; let old_entry = self.entries_by_id.insert_or_replace( PathEntry { id: entry.id, path: entry.path.clone(), is_ignored: entry.is_ignored, scan_id: 0, }, &(), ); if let Some(old_entry) = old_entry { self.entries_by_path.remove(&PathKey(old_entry.path), &()); } self.entries_by_path.insert_or_replace(entry.clone(), &()); Ok(entry) } fn delete_entry(&mut self, entry_id: ProjectEntryId) -> Option> { let removed_entry = self.entries_by_id.remove(&entry_id, &())?; self.entries_by_path = { let mut cursor = self.entries_by_path.cursor(); let mut new_entries_by_path = cursor.slice(&TraversalTarget::Path(&removed_entry.path), Bias::Left, &()); while let Some(entry) = cursor.item() { if entry.path.starts_with(&removed_entry.path) { self.entries_by_id.remove(&entry.id, &()); cursor.next(&()); } else { break; } } new_entries_by_path.push_tree(cursor.suffix(&()), &()); new_entries_by_path }; Some(removed_entry.path) } pub(crate) fn apply_remote_update(&mut self, update: proto::UpdateWorktree) -> Result<()> { let mut entries_by_path_edits = Vec::new(); let mut entries_by_id_edits = Vec::new(); for entry_id in update.removed_entries { if let Some(entry) = self.entry_for_id(ProjectEntryId::from_proto(entry_id)) { entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone()))); entries_by_id_edits.push(Edit::Remove(entry.id)); } } for entry in update.updated_entries { let entry = Entry::try_from((&self.root_char_bag, entry))?; if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) { entries_by_path_edits.push(Edit::Remove(PathKey(path.clone()))); } entries_by_id_edits.push(Edit::Insert(PathEntry { id: entry.id, path: entry.path.clone(), is_ignored: entry.is_ignored, scan_id: 0, })); entries_by_path_edits.push(Edit::Insert(entry)); } self.entries_by_path.edit(entries_by_path_edits, &()); self.entries_by_id.edit(entries_by_id_edits, &()); self.scan_id = update.scan_id as usize; if update.is_last_update { self.completed_scan_id = update.scan_id as usize; } Ok(()) } pub fn file_count(&self) -> usize { self.entries_by_path.summary().file_count } pub fn visible_file_count(&self) -> usize { self.entries_by_path.summary().visible_file_count } fn traverse_from_offset( &self, include_dirs: bool, include_ignored: bool, start_offset: usize, ) -> Traversal { let mut cursor = self.entries_by_path.cursor(); cursor.seek( &TraversalTarget::Count { count: start_offset, include_dirs, include_ignored, }, Bias::Right, &(), ); Traversal { cursor, include_dirs, include_ignored, } } fn traverse_from_path( &self, include_dirs: bool, include_ignored: bool, path: &Path, ) -> Traversal { let mut cursor = self.entries_by_path.cursor(); cursor.seek(&TraversalTarget::Path(path), Bias::Left, &()); Traversal { cursor, include_dirs, include_ignored, } } pub fn files(&self, include_ignored: bool, start: usize) -> Traversal { self.traverse_from_offset(false, include_ignored, start) } pub fn entries(&self, include_ignored: bool) -> Traversal { self.traverse_from_offset(true, include_ignored, 0) } pub fn paths(&self) -> impl Iterator> { let empty_path = Path::new(""); self.entries_by_path .cursor::<()>() .filter(move |entry| entry.path.as_ref() != empty_path) .map(|entry| &entry.path) } fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> { let mut cursor = self.entries_by_path.cursor(); cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &()); let traversal = Traversal { cursor, include_dirs: true, include_ignored: true, }; ChildEntriesIter { traversal, parent_path, } } pub fn root_entry(&self) -> Option<&Entry> { self.entry_for_path("") } pub fn root_name(&self) -> &str { &self.root_name } pub fn scan_id(&self) -> usize { self.scan_id } pub fn entry_for_path(&self, path: impl AsRef) -> Option<&Entry> { let path = path.as_ref(); self.traverse_from_path(true, true, path) .entry() .and_then(|entry| { if entry.path.as_ref() == path { Some(entry) } else { None } }) } pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> { let entry = self.entries_by_id.get(&id, &())?; self.entry_for_path(&entry.path) } pub fn inode_for_path(&self, path: impl AsRef) -> Option { self.entry_for_path(path.as_ref()).map(|e| e.inode) } } impl LocalSnapshot { // Gives the most specific git repository for a given path pub(crate) fn repo_for(&self, path: &Path) -> Option { self.git_repositories .iter() .rev() //git_repository is ordered lexicographically .find(|repo| repo.manages(path)) .cloned() } pub(crate) fn repo_with_dot_git_containing( &mut self, path: &Path, ) -> Option<&mut GitRepositoryEntry> { // Git repositories cannot be nested, so we don't need to reverse the order self.git_repositories .iter_mut() .find(|repo| repo.in_dot_git(path)) } #[cfg(test)] pub(crate) fn build_initial_update(&self, project_id: u64) -> proto::UpdateWorktree { let root_name = self.root_name.clone(); proto::UpdateWorktree { project_id, worktree_id: self.id().to_proto(), abs_path: self.abs_path().to_string_lossy().into(), root_name, updated_entries: self.entries_by_path.iter().map(Into::into).collect(), removed_entries: Default::default(), scan_id: self.scan_id as u64, is_last_update: true, } } pub(crate) fn build_update( &self, other: &Self, project_id: u64, worktree_id: u64, include_ignored: bool, ) -> proto::UpdateWorktree { let mut updated_entries = Vec::new(); let mut removed_entries = Vec::new(); let mut self_entries = self .entries_by_id .cursor::<()>() .filter(|e| include_ignored || !e.is_ignored) .peekable(); let mut other_entries = other .entries_by_id .cursor::<()>() .filter(|e| include_ignored || !e.is_ignored) .peekable(); loop { match (self_entries.peek(), other_entries.peek()) { (Some(self_entry), Some(other_entry)) => { match Ord::cmp(&self_entry.id, &other_entry.id) { Ordering::Less => { let entry = self.entry_for_id(self_entry.id).unwrap().into(); updated_entries.push(entry); self_entries.next(); } Ordering::Equal => { if self_entry.scan_id != other_entry.scan_id { let entry = self.entry_for_id(self_entry.id).unwrap().into(); updated_entries.push(entry); } self_entries.next(); other_entries.next(); } Ordering::Greater => { removed_entries.push(other_entry.id.to_proto()); other_entries.next(); } } } (Some(self_entry), None) => { let entry = self.entry_for_id(self_entry.id).unwrap().into(); updated_entries.push(entry); self_entries.next(); } (None, Some(other_entry)) => { removed_entries.push(other_entry.id.to_proto()); other_entries.next(); } (None, None) => break, } } proto::UpdateWorktree { project_id, worktree_id, abs_path: self.abs_path().to_string_lossy().into(), root_name: self.root_name().to_string(), updated_entries, removed_entries, scan_id: self.scan_id as u64, is_last_update: self.completed_scan_id == self.scan_id, } } fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry { if entry.is_file() && entry.path.file_name() == Some(&GITIGNORE) { let abs_path = self.abs_path.join(&entry.path); match smol::block_on(build_gitignore(&abs_path, fs)) { Ok(ignore) => { self.ignores_by_parent_abs_path.insert( abs_path.parent().unwrap().into(), (Arc::new(ignore), self.scan_id), ); } Err(error) => { log::error!( "error loading .gitignore file {:?} - {:?}", &entry.path, error ); } } } self.reuse_entry_id(&mut entry); if entry.kind == EntryKind::PendingDir { if let Some(existing_entry) = self.entries_by_path.get(&PathKey(entry.path.clone()), &()) { entry.kind = existing_entry.kind; } } let scan_id = self.scan_id; let removed = self.entries_by_path.insert_or_replace(entry.clone(), &()); if let Some(removed) = removed { if removed.id != entry.id { self.entries_by_id.remove(&removed.id, &()); } } self.entries_by_id.insert_or_replace( PathEntry { id: entry.id, path: entry.path.clone(), is_ignored: entry.is_ignored, scan_id, }, &(), ); entry } fn populate_dir( &mut self, parent_path: Arc, entries: impl IntoIterator, ignore: Option>, fs: &dyn Fs, ) { let mut parent_entry = if let Some(parent_entry) = self.entries_by_path.get(&PathKey(parent_path.clone()), &()) { parent_entry.clone() } else { log::warn!( "populating a directory {:?} that has been removed", parent_path ); return; }; match parent_entry.kind { EntryKind::PendingDir => { parent_entry.kind = EntryKind::Dir; } EntryKind::Dir => {} _ => return, } if let Some(ignore) = ignore { self.ignores_by_parent_abs_path.insert( self.abs_path.join(&parent_path).into(), (ignore, self.scan_id), ); } if parent_path.file_name() == Some(&DOT_GIT) { let abs_path = self.abs_path.join(&parent_path); let content_path: Arc = parent_path.parent().unwrap().into(); if let Err(ix) = self .git_repositories .binary_search_by_key(&&content_path, |repo| &repo.content_path) { if let Some(repo) = fs.open_repo(abs_path.as_path()) { self.git_repositories.insert( ix, GitRepositoryEntry { repo, scan_id: 0, content_path, git_dir_path: parent_path, }, ); } } } let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)]; let mut entries_by_id_edits = Vec::new(); for mut entry in entries { self.reuse_entry_id(&mut entry); entries_by_id_edits.push(Edit::Insert(PathEntry { id: entry.id, path: entry.path.clone(), is_ignored: entry.is_ignored, scan_id: self.scan_id, })); entries_by_path_edits.push(Edit::Insert(entry)); } self.entries_by_path.edit(entries_by_path_edits, &()); self.entries_by_id.edit(entries_by_id_edits, &()); } fn reuse_entry_id(&mut self, entry: &mut Entry) { if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) { entry.id = removed_entry_id; } else if let Some(existing_entry) = self.entry_for_path(&entry.path) { entry.id = existing_entry.id; } } fn remove_path(&mut self, path: &Path) { let mut new_entries; let removed_entries; { let mut cursor = self.entries_by_path.cursor::(); new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &()); removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &()); new_entries.push_tree(cursor.suffix(&()), &()); } self.entries_by_path = new_entries; let mut entries_by_id_edits = Vec::new(); for entry in removed_entries.cursor::<()>() { let removed_entry_id = self .removed_entry_ids .entry(entry.inode) .or_insert(entry.id); *removed_entry_id = cmp::max(*removed_entry_id, entry.id); entries_by_id_edits.push(Edit::Remove(entry.id)); } self.entries_by_id.edit(entries_by_id_edits, &()); if path.file_name() == Some(&GITIGNORE) { let abs_parent_path = self.abs_path.join(path.parent().unwrap()); if let Some((_, scan_id)) = self .ignores_by_parent_abs_path .get_mut(abs_parent_path.as_path()) { *scan_id = self.snapshot.scan_id; } } else if path.file_name() == Some(&DOT_GIT) { let parent_path = path.parent().unwrap(); if let Ok(ix) = self .git_repositories .binary_search_by_key(&parent_path, |repo| repo.git_dir_path.as_ref()) { self.git_repositories[ix].scan_id = self.snapshot.scan_id; } } } fn ancestor_inodes_for_path(&self, path: &Path) -> TreeSet { let mut inodes = TreeSet::default(); for ancestor in path.ancestors().skip(1) { if let Some(entry) = self.entry_for_path(ancestor) { inodes.insert(entry.inode); } } inodes } fn ignore_stack_for_abs_path(&self, abs_path: &Path, is_dir: bool) -> Arc { let mut new_ignores = Vec::new(); for ancestor in abs_path.ancestors().skip(1) { if let Some((ignore, _)) = self.ignores_by_parent_abs_path.get(ancestor) { new_ignores.push((ancestor, Some(ignore.clone()))); } else { new_ignores.push((ancestor, None)); } } let mut ignore_stack = IgnoreStack::none(); for (parent_abs_path, ignore) in new_ignores.into_iter().rev() { if ignore_stack.is_abs_path_ignored(parent_abs_path, true) { ignore_stack = IgnoreStack::all(); break; } else if let Some(ignore) = ignore { ignore_stack = ignore_stack.append(parent_abs_path.into(), ignore); } } if ignore_stack.is_abs_path_ignored(abs_path, is_dir) { ignore_stack = IgnoreStack::all(); } ignore_stack } pub fn git_repo_entries(&self) -> &[GitRepositoryEntry] { &self.git_repositories } } impl GitRepositoryEntry { // Note that these paths should be relative to the worktree root. pub(crate) fn manages(&self, path: &Path) -> bool { path.starts_with(self.content_path.as_ref()) } // Note that this path should be relative to the worktree root. pub(crate) fn in_dot_git(&self, path: &Path) -> bool { path.starts_with(self.git_dir_path.as_ref()) } } async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result { let contents = fs.load(abs_path).await?; let parent = abs_path.parent().unwrap_or_else(|| Path::new("/")); let mut builder = GitignoreBuilder::new(parent); for line in contents.lines() { builder.add_line(Some(abs_path.into()), line)?; } Ok(builder.build()?) } impl WorktreeId { pub fn from_usize(handle_id: usize) -> Self { Self(handle_id) } pub(crate) fn from_proto(id: u64) -> Self { Self(id as usize) } pub fn to_proto(&self) -> u64 { self.0 as u64 } pub fn to_usize(&self) -> usize { self.0 } } impl fmt::Display for WorktreeId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl Deref for Worktree { type Target = Snapshot; fn deref(&self) -> &Self::Target { match self { Worktree::Local(worktree) => &worktree.snapshot, Worktree::Remote(worktree) => &worktree.snapshot, } } } impl Deref for LocalWorktree { type Target = LocalSnapshot; fn deref(&self) -> &Self::Target { &self.snapshot } } impl Deref for RemoteWorktree { type Target = Snapshot; fn deref(&self) -> &Self::Target { &self.snapshot } } impl fmt::Debug for LocalWorktree { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.snapshot.fmt(f) } } impl fmt::Debug for Snapshot { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { struct EntriesById<'a>(&'a SumTree); struct EntriesByPath<'a>(&'a SumTree); impl<'a> fmt::Debug for EntriesByPath<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_map() .entries(self.0.iter().map(|entry| (&entry.path, entry.id))) .finish() } } impl<'a> fmt::Debug for EntriesById<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.0.iter()).finish() } } f.debug_struct("Snapshot") .field("id", &self.id) .field("root_name", &self.root_name) .field("entries_by_path", &EntriesByPath(&self.entries_by_path)) .field("entries_by_id", &EntriesById(&self.entries_by_id)) .finish() } } #[derive(Clone, PartialEq)] pub struct File { pub worktree: ModelHandle, pub path: Arc, pub mtime: SystemTime, pub(crate) entry_id: ProjectEntryId, pub(crate) is_local: bool, pub(crate) is_deleted: bool, } impl language::File for File { fn as_local(&self) -> Option<&dyn language::LocalFile> { if self.is_local { Some(self) } else { None } } fn mtime(&self) -> SystemTime { self.mtime } fn path(&self) -> &Arc { &self.path } fn full_path(&self, cx: &AppContext) -> PathBuf { let mut full_path = PathBuf::new(); let worktree = self.worktree.read(cx); if worktree.is_visible() { full_path.push(worktree.root_name()); } else { let path = worktree.abs_path(); if worktree.is_local() && path.starts_with(HOME.as_path()) { full_path.push("~"); full_path.push(path.strip_prefix(HOME.as_path()).unwrap()); } else { full_path.push(path) } } if self.path.components().next().is_some() { full_path.push(&self.path); } full_path } /// Returns the last component of this handle's absolute path. If this handle refers to the root /// of its worktree, then this method will return the name of the worktree itself. fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr { self.path .file_name() .unwrap_or_else(|| OsStr::new(&self.worktree.read(cx).root_name)) } fn is_deleted(&self) -> bool { self.is_deleted } fn as_any(&self) -> &dyn Any { self } fn to_proto(&self) -> rpc::proto::File { rpc::proto::File { worktree_id: self.worktree.id() as u64, entry_id: self.entry_id.to_proto(), path: self.path.to_string_lossy().into(), mtime: Some(self.mtime.into()), is_deleted: self.is_deleted, } } } impl language::LocalFile for File { fn abs_path(&self, cx: &AppContext) -> PathBuf { self.worktree .read(cx) .as_local() .unwrap() .abs_path .join(&self.path) } fn load(&self, cx: &AppContext) -> Task> { let worktree = self.worktree.read(cx).as_local().unwrap(); let abs_path = worktree.absolutize(&self.path); let fs = worktree.fs.clone(); cx.background() .spawn(async move { fs.load(&abs_path).await }) } fn buffer_reloaded( &self, buffer_id: u64, version: &clock::Global, fingerprint: RopeFingerprint, line_ending: LineEnding, mtime: SystemTime, cx: &mut AppContext, ) { let worktree = self.worktree.read(cx).as_local().unwrap(); if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) { worktree .client .send(proto::BufferReloaded { project_id, buffer_id, version: serialize_version(version), mtime: Some(mtime.into()), fingerprint: serialize_fingerprint(fingerprint), line_ending: serialize_line_ending(line_ending) as i32, }) .log_err(); } } } impl File { pub fn from_proto( proto: rpc::proto::File, worktree: ModelHandle, cx: &AppContext, ) -> Result { let worktree_id = worktree .read(cx) .as_remote() .ok_or_else(|| anyhow!("not remote"))? .id(); if worktree_id.to_proto() != proto.worktree_id { return Err(anyhow!("worktree id does not match file")); } Ok(Self { worktree, path: Path::new(&proto.path).into(), mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(), entry_id: ProjectEntryId::from_proto(proto.entry_id), is_local: false, is_deleted: proto.is_deleted, }) } pub fn from_dyn(file: Option<&Arc>) -> Option<&Self> { file.and_then(|f| f.as_any().downcast_ref()) } pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId { self.worktree.read(cx).id() } pub fn project_entry_id(&self, _: &AppContext) -> Option { if self.is_deleted { None } else { Some(self.entry_id) } } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct Entry { pub id: ProjectEntryId, pub kind: EntryKind, pub path: Arc, pub inode: u64, pub mtime: SystemTime, pub is_symlink: bool, pub is_ignored: bool, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum EntryKind { PendingDir, Dir, File(CharBag), } #[derive(Clone, Copy, Debug)] pub enum PathChange { Added, Removed, Updated, AddedOrUpdated, } impl Entry { fn new( path: Arc, metadata: &fs::Metadata, next_entry_id: &AtomicUsize, root_char_bag: CharBag, ) -> Self { Self { id: ProjectEntryId::new(next_entry_id), kind: if metadata.is_dir { EntryKind::PendingDir } else { EntryKind::File(char_bag_for_path(root_char_bag, &path)) }, path, inode: metadata.inode, mtime: metadata.mtime, is_symlink: metadata.is_symlink, is_ignored: false, } } pub fn is_dir(&self) -> bool { matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir) } pub fn is_file(&self) -> bool { matches!(self.kind, EntryKind::File(_)) } } impl sum_tree::Item for Entry { type Summary = EntrySummary; fn summary(&self) -> Self::Summary { let visible_count = if self.is_ignored { 0 } else { 1 }; let file_count; let visible_file_count; if self.is_file() { file_count = 1; visible_file_count = visible_count; } else { file_count = 0; visible_file_count = 0; } EntrySummary { max_path: self.path.clone(), count: 1, visible_count, file_count, visible_file_count, } } } impl sum_tree::KeyedItem for Entry { type Key = PathKey; fn key(&self) -> Self::Key { PathKey(self.path.clone()) } } #[derive(Clone, Debug)] pub struct EntrySummary { max_path: Arc, count: usize, visible_count: usize, file_count: usize, visible_file_count: usize, } impl Default for EntrySummary { fn default() -> Self { Self { max_path: Arc::from(Path::new("")), count: 0, visible_count: 0, file_count: 0, visible_file_count: 0, } } } impl sum_tree::Summary for EntrySummary { type Context = (); fn add_summary(&mut self, rhs: &Self, _: &()) { self.max_path = rhs.max_path.clone(); self.count += rhs.count; self.visible_count += rhs.visible_count; self.file_count += rhs.file_count; self.visible_file_count += rhs.visible_file_count; } } #[derive(Clone, Debug)] struct PathEntry { id: ProjectEntryId, path: Arc, is_ignored: bool, scan_id: usize, } impl sum_tree::Item for PathEntry { type Summary = PathEntrySummary; fn summary(&self) -> Self::Summary { PathEntrySummary { max_id: self.id } } } impl sum_tree::KeyedItem for PathEntry { type Key = ProjectEntryId; fn key(&self) -> Self::Key { self.id } } #[derive(Clone, Debug, Default)] struct PathEntrySummary { max_id: ProjectEntryId, } impl sum_tree::Summary for PathEntrySummary { type Context = (); fn add_summary(&mut self, summary: &Self, _: &Self::Context) { self.max_id = summary.max_id; } } impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId { fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) { *self = summary.max_id; } } #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub struct PathKey(Arc); impl Default for PathKey { fn default() -> Self { Self(Path::new("").into()) } } impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey { fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { self.0 = summary.max_path.clone(); } } struct BackgroundScanner { snapshot: Mutex, fs: Arc, status_updates_tx: UnboundedSender, executor: Arc, refresh_requests_rx: channel::Receiver<(Vec, barrier::Sender)>, prev_state: Mutex<(Snapshot, Vec>)>, finished_initial_scan: bool, } impl BackgroundScanner { fn new( snapshot: LocalSnapshot, fs: Arc, status_updates_tx: UnboundedSender, executor: Arc, refresh_requests_rx: channel::Receiver<(Vec, barrier::Sender)>, ) -> Self { Self { fs, status_updates_tx, executor, refresh_requests_rx, prev_state: Mutex::new((snapshot.snapshot.clone(), Vec::new())), snapshot: Mutex::new(snapshot), finished_initial_scan: false, } } async fn run( &mut self, mut events_rx: Pin>>>, ) { use futures::FutureExt as _; let (root_abs_path, root_inode) = { let snapshot = self.snapshot.lock(); ( snapshot.abs_path.clone(), snapshot.root_entry().map(|e| e.inode), ) }; // Populate ignores above the root. let ignore_stack; for ancestor in root_abs_path.ancestors().skip(1) { if let Ok(ignore) = build_gitignore(&ancestor.join(&*GITIGNORE), self.fs.as_ref()).await { self.snapshot .lock() .ignores_by_parent_abs_path .insert(ancestor.into(), (ignore.into(), 0)); } } { let mut snapshot = self.snapshot.lock(); snapshot.scan_id += 1; ignore_stack = snapshot.ignore_stack_for_abs_path(&root_abs_path, true); if ignore_stack.is_all() { if let Some(mut root_entry) = snapshot.root_entry().cloned() { root_entry.is_ignored = true; snapshot.insert_entry(root_entry, self.fs.as_ref()); } } }; // Perform an initial scan of the directory. let (scan_job_tx, scan_job_rx) = channel::unbounded(); smol::block_on(scan_job_tx.send(ScanJob { abs_path: root_abs_path, path: Arc::from(Path::new("")), ignore_stack, ancestor_inodes: TreeSet::from_ordered_entries(root_inode), scan_queue: scan_job_tx.clone(), })) .unwrap(); drop(scan_job_tx); self.scan_dirs(true, scan_job_rx).await; { let mut snapshot = self.snapshot.lock(); snapshot.completed_scan_id = snapshot.scan_id; } self.send_status_update(false, None); // Process any any FS events that occurred while performing the initial scan. // For these events, update events cannot be as precise, because we didn't // have the previous state loaded yet. if let Poll::Ready(Some(events)) = futures::poll!(events_rx.next()) { let mut paths = events.into_iter().map(|e| e.path).collect::>(); while let Poll::Ready(Some(more_events)) = futures::poll!(events_rx.next()) { paths.extend(more_events.into_iter().map(|e| e.path)); } self.process_events(paths).await; } self.finished_initial_scan = true; // Continue processing events until the worktree is dropped. loop { select_biased! { // Process any path refresh requests from the worktree. Prioritize // these before handling changes reported by the filesystem. request = self.refresh_requests_rx.recv().fuse() => { let Ok((paths, barrier)) = request else { break }; if !self.process_refresh_request(paths, barrier).await { return; } } events = events_rx.next().fuse() => { let Some(events) = events else { break }; let mut paths = events.into_iter().map(|e| e.path).collect::>(); while let Poll::Ready(Some(more_events)) = futures::poll!(events_rx.next()) { paths.extend(more_events.into_iter().map(|e| e.path)); } self.process_events(paths).await; } } } } async fn process_refresh_request(&self, paths: Vec, barrier: barrier::Sender) -> bool { self.reload_entries_for_paths(paths, None).await; self.send_status_update(false, Some(barrier)) } async fn process_events(&mut self, paths: Vec) { let (scan_job_tx, scan_job_rx) = channel::unbounded(); if let Some(mut paths) = self .reload_entries_for_paths(paths, Some(scan_job_tx.clone())) .await { paths.sort_unstable(); util::extend_sorted(&mut self.prev_state.lock().1, paths, usize::MAX, Ord::cmp); } drop(scan_job_tx); self.scan_dirs(false, scan_job_rx).await; self.update_ignore_statuses().await; let mut snapshot = self.snapshot.lock(); let mut git_repositories = mem::take(&mut snapshot.git_repositories); git_repositories.retain(|repo| snapshot.entry_for_path(&repo.git_dir_path).is_some()); snapshot.git_repositories = git_repositories; snapshot.removed_entry_ids.clear(); snapshot.completed_scan_id = snapshot.scan_id; drop(snapshot); self.send_status_update(false, None); } async fn scan_dirs( &self, enable_progress_updates: bool, scan_jobs_rx: channel::Receiver, ) { use futures::FutureExt as _; if self .status_updates_tx .unbounded_send(ScanState::Started) .is_err() { return; } let progress_update_count = AtomicUsize::new(0); self.executor .scoped(|scope| { for _ in 0..self.executor.num_cpus() { scope.spawn(async { let mut last_progress_update_count = 0; let progress_update_timer = self.progress_timer(enable_progress_updates).fuse(); futures::pin_mut!(progress_update_timer); loop { select_biased! { // Process any path refresh requests before moving on to process // the scan queue, so that user operations are prioritized. request = self.refresh_requests_rx.recv().fuse() => { let Ok((paths, barrier)) = request else { break }; if !self.process_refresh_request(paths, barrier).await { return; } } // Send periodic progress updates to the worktree. Use an atomic counter // to ensure that only one of the workers sends a progress update after // the update interval elapses. _ = progress_update_timer => { match progress_update_count.compare_exchange( last_progress_update_count, last_progress_update_count + 1, SeqCst, SeqCst ) { Ok(_) => { last_progress_update_count += 1; self.send_status_update(true, None); } Err(count) => { last_progress_update_count = count; } } progress_update_timer.set(self.progress_timer(enable_progress_updates).fuse()); } // Recursively load directories from the file system. job = scan_jobs_rx.recv().fuse() => { let Ok(job) = job else { break }; if let Err(err) = self.scan_dir(&job).await { if job.path.as_ref() != Path::new("") { log::error!("error scanning directory {:?}: {}", job.abs_path, err); } } } } } }) } }) .await; } fn send_status_update(&self, scanning: bool, barrier: Option) -> bool { let mut prev_state = self.prev_state.lock(); let snapshot = self.snapshot.lock().clone(); let mut old_snapshot = snapshot.snapshot.clone(); mem::swap(&mut old_snapshot, &mut prev_state.0); let changed_paths = mem::take(&mut prev_state.1); let changes = self.build_change_set(&old_snapshot, &snapshot.snapshot, changed_paths); self.status_updates_tx .unbounded_send(ScanState::Updated { snapshot, changes, scanning, barrier, }) .is_ok() } async fn scan_dir(&self, job: &ScanJob) -> Result<()> { let mut new_entries: Vec = Vec::new(); let mut new_jobs: Vec> = Vec::new(); let mut ignore_stack = job.ignore_stack.clone(); let mut new_ignore = None; let (root_abs_path, root_char_bag, next_entry_id) = { let snapshot = self.snapshot.lock(); ( snapshot.abs_path().clone(), snapshot.root_char_bag, snapshot.next_entry_id.clone(), ) }; let mut child_paths = self.fs.read_dir(&job.abs_path).await?; while let Some(child_abs_path) = child_paths.next().await { let child_abs_path: Arc = match child_abs_path { Ok(child_abs_path) => child_abs_path.into(), Err(error) => { log::error!("error processing entry {:?}", error); continue; } }; let child_name = child_abs_path.file_name().unwrap(); let child_path: Arc = job.path.join(child_name).into(); let child_metadata = match self.fs.metadata(&child_abs_path).await { Ok(Some(metadata)) => metadata, Ok(None) => continue, Err(err) => { log::error!("error processing {:?}: {:?}", child_abs_path, err); continue; } }; // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored if child_name == *GITIGNORE { match build_gitignore(&child_abs_path, self.fs.as_ref()).await { Ok(ignore) => { let ignore = Arc::new(ignore); ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone()); new_ignore = Some(ignore); } Err(error) => { log::error!( "error loading .gitignore file {:?} - {:?}", child_name, error ); } } // Update ignore status of any child entries we've already processed to reflect the // ignore file in the current directory. Because `.gitignore` starts with a `.`, // there should rarely be too numerous. Update the ignore stack associated with any // new jobs as well. let mut new_jobs = new_jobs.iter_mut(); for entry in &mut new_entries { let entry_abs_path = root_abs_path.join(&entry.path); entry.is_ignored = ignore_stack.is_abs_path_ignored(&entry_abs_path, entry.is_dir()); if entry.is_dir() { if let Some(job) = new_jobs.next().expect("Missing scan job for entry") { job.ignore_stack = if entry.is_ignored { IgnoreStack::all() } else { ignore_stack.clone() }; } } } } let mut child_entry = Entry::new( child_path.clone(), &child_metadata, &next_entry_id, root_char_bag, ); if child_entry.is_dir() { let is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true); child_entry.is_ignored = is_ignored; // Avoid recursing until crash in the case of a recursive symlink if !job.ancestor_inodes.contains(&child_entry.inode) { let mut ancestor_inodes = job.ancestor_inodes.clone(); ancestor_inodes.insert(child_entry.inode); new_jobs.push(Some(ScanJob { abs_path: child_abs_path, path: child_path, ignore_stack: if is_ignored { IgnoreStack::all() } else { ignore_stack.clone() }, ancestor_inodes, scan_queue: job.scan_queue.clone(), })); } else { new_jobs.push(None); } } else { child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false); } new_entries.push(child_entry); } self.snapshot.lock().populate_dir( job.path.clone(), new_entries, new_ignore, self.fs.as_ref(), ); for new_job in new_jobs { if let Some(new_job) = new_job { job.scan_queue.send(new_job).await.unwrap(); } } Ok(()) } async fn reload_entries_for_paths( &self, mut abs_paths: Vec, scan_queue_tx: Option>, ) -> Option>> { let doing_recursive_update = scan_queue_tx.is_some(); abs_paths.sort_unstable(); abs_paths.dedup_by(|a, b| a.starts_with(&b)); let root_abs_path = self.snapshot.lock().abs_path.clone(); let root_canonical_path = self.fs.canonicalize(&root_abs_path).await.log_err()?; let metadata = futures::future::join_all( abs_paths .iter() .map(|abs_path| self.fs.metadata(&abs_path)) .collect::>(), ) .await; let mut snapshot = self.snapshot.lock(); let is_idle = snapshot.completed_scan_id == snapshot.scan_id; snapshot.scan_id += 1; if is_idle && !doing_recursive_update { snapshot.completed_scan_id = snapshot.scan_id; } // Remove any entries for paths that no longer exist or are being recursively // refreshed. Do this before adding any new entries, so that renames can be // detected regardless of the order of the paths. let mut event_paths = Vec::>::with_capacity(abs_paths.len()); for (abs_path, metadata) in abs_paths.iter().zip(metadata.iter()) { if let Ok(path) = abs_path.strip_prefix(&root_canonical_path) { if matches!(metadata, Ok(None)) || doing_recursive_update { snapshot.remove_path(path); } event_paths.push(path.into()); } else { log::error!( "unexpected event {:?} for root path {:?}", abs_path, root_canonical_path ); } } for (path, metadata) in event_paths.iter().cloned().zip(metadata.into_iter()) { let abs_path: Arc = root_abs_path.join(&path).into(); match metadata { Ok(Some(metadata)) => { let ignore_stack = snapshot.ignore_stack_for_abs_path(&abs_path, metadata.is_dir); let mut fs_entry = Entry::new( path.clone(), &metadata, snapshot.next_entry_id.as_ref(), snapshot.root_char_bag, ); fs_entry.is_ignored = ignore_stack.is_all(); snapshot.insert_entry(fs_entry, self.fs.as_ref()); let scan_id = snapshot.scan_id; if let Some(repo) = snapshot.repo_with_dot_git_containing(&path) { repo.repo.lock().reload_index(); repo.scan_id = scan_id; } if let Some(scan_queue_tx) = &scan_queue_tx { let mut ancestor_inodes = snapshot.ancestor_inodes_for_path(&path); if metadata.is_dir && !ancestor_inodes.contains(&metadata.inode) { ancestor_inodes.insert(metadata.inode); smol::block_on(scan_queue_tx.send(ScanJob { abs_path, path, ignore_stack, ancestor_inodes, scan_queue: scan_queue_tx.clone(), })) .unwrap(); } } } Ok(None) => {} Err(err) => { // TODO - create a special 'error' entry in the entries tree to mark this log::error!("error reading file on event {:?}", err); } } } Some(event_paths) } async fn update_ignore_statuses(&self) { use futures::FutureExt as _; let mut snapshot = self.snapshot.lock().clone(); let mut ignores_to_update = Vec::new(); let mut ignores_to_delete = Vec::new(); for (parent_abs_path, (_, scan_id)) in &snapshot.ignores_by_parent_abs_path { if let Ok(parent_path) = parent_abs_path.strip_prefix(&snapshot.abs_path) { if *scan_id > snapshot.completed_scan_id && snapshot.entry_for_path(parent_path).is_some() { ignores_to_update.push(parent_abs_path.clone()); } let ignore_path = parent_path.join(&*GITIGNORE); if snapshot.entry_for_path(ignore_path).is_none() { ignores_to_delete.push(parent_abs_path.clone()); } } } for parent_abs_path in ignores_to_delete { snapshot.ignores_by_parent_abs_path.remove(&parent_abs_path); self.snapshot .lock() .ignores_by_parent_abs_path .remove(&parent_abs_path); } let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded(); ignores_to_update.sort_unstable(); let mut ignores_to_update = ignores_to_update.into_iter().peekable(); while let Some(parent_abs_path) = ignores_to_update.next() { while ignores_to_update .peek() .map_or(false, |p| p.starts_with(&parent_abs_path)) { ignores_to_update.next().unwrap(); } let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true); smol::block_on(ignore_queue_tx.send(UpdateIgnoreStatusJob { abs_path: parent_abs_path, ignore_stack, ignore_queue: ignore_queue_tx.clone(), })) .unwrap(); } drop(ignore_queue_tx); self.executor .scoped(|scope| { for _ in 0..self.executor.num_cpus() { scope.spawn(async { loop { select_biased! { // Process any path refresh requests before moving on to process // the queue of ignore statuses. request = self.refresh_requests_rx.recv().fuse() => { let Ok((paths, barrier)) = request else { break }; if !self.process_refresh_request(paths, barrier).await { return; } } // Recursively process directories whose ignores have changed. job = ignore_queue_rx.recv().fuse() => { let Ok(job) = job else { break }; self.update_ignore_status(job, &snapshot).await; } } } }); } }) .await; } async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) { let mut ignore_stack = job.ignore_stack; if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) { ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone()); } let mut entries_by_id_edits = Vec::new(); let mut entries_by_path_edits = Vec::new(); let path = job.abs_path.strip_prefix(&snapshot.abs_path).unwrap(); for mut entry in snapshot.child_entries(path).cloned() { let was_ignored = entry.is_ignored; let abs_path = snapshot.abs_path().join(&entry.path); entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir()); if entry.is_dir() { let child_ignore_stack = if entry.is_ignored { IgnoreStack::all() } else { ignore_stack.clone() }; job.ignore_queue .send(UpdateIgnoreStatusJob { abs_path: abs_path.into(), ignore_stack: child_ignore_stack, ignore_queue: job.ignore_queue.clone(), }) .await .unwrap(); } if entry.is_ignored != was_ignored { let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone(); path_entry.scan_id = snapshot.scan_id; path_entry.is_ignored = entry.is_ignored; entries_by_id_edits.push(Edit::Insert(path_entry)); entries_by_path_edits.push(Edit::Insert(entry)); } } let mut snapshot = self.snapshot.lock(); snapshot.entries_by_path.edit(entries_by_path_edits, &()); snapshot.entries_by_id.edit(entries_by_id_edits, &()); } fn build_change_set( &self, old_snapshot: &Snapshot, new_snapshot: &Snapshot, event_paths: Vec>, ) -> HashMap, PathChange> { use PathChange::{Added, AddedOrUpdated, Removed, Updated}; let mut changes = HashMap::default(); let mut old_paths = old_snapshot.entries_by_path.cursor::(); let mut new_paths = new_snapshot.entries_by_path.cursor::(); let received_before_initialized = !self.finished_initial_scan; for path in event_paths { let path = PathKey(path); old_paths.seek(&path, Bias::Left, &()); new_paths.seek(&path, Bias::Left, &()); loop { match (old_paths.item(), new_paths.item()) { (Some(old_entry), Some(new_entry)) => { if old_entry.path > path.0 && new_entry.path > path.0 && !old_entry.path.starts_with(&path.0) && !new_entry.path.starts_with(&path.0) { break; } match Ord::cmp(&old_entry.path, &new_entry.path) { Ordering::Less => { changes.insert(old_entry.path.clone(), Removed); old_paths.next(&()); } Ordering::Equal => { if received_before_initialized { // If the worktree was not fully initialized when this event was generated, // we can't know whether this entry was added during the scan or whether // it was merely updated. changes.insert(new_entry.path.clone(), AddedOrUpdated); } else if old_entry.mtime != new_entry.mtime { changes.insert(new_entry.path.clone(), Updated); } old_paths.next(&()); new_paths.next(&()); } Ordering::Greater => { changes.insert(new_entry.path.clone(), Added); new_paths.next(&()); } } } (Some(old_entry), None) => { changes.insert(old_entry.path.clone(), Removed); old_paths.next(&()); } (None, Some(new_entry)) => { changes.insert(new_entry.path.clone(), Added); new_paths.next(&()); } (None, None) => break, } } } changes } async fn progress_timer(&self, running: bool) { if !running { return futures::future::pending().await; } #[cfg(any(test, feature = "test-support"))] if self.fs.is_fake() { return self.executor.simulate_random_delay().await; } smol::Timer::after(Duration::from_millis(100)).await; } } fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag { let mut result = root_char_bag; result.extend( path.to_string_lossy() .chars() .map(|c| c.to_ascii_lowercase()), ); result } struct ScanJob { abs_path: Arc, path: Arc, ignore_stack: Arc, scan_queue: Sender, ancestor_inodes: TreeSet, } struct UpdateIgnoreStatusJob { abs_path: Arc, ignore_stack: Arc, ignore_queue: Sender, } pub trait WorktreeHandle { #[cfg(any(test, feature = "test-support"))] fn flush_fs_events<'a>( &self, cx: &'a gpui::TestAppContext, ) -> futures::future::LocalBoxFuture<'a, ()>; } impl WorktreeHandle for ModelHandle { // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that // occurred before the worktree was constructed. These events can cause the worktree to perfrom // extra directory scans, and emit extra scan-state notifications. // // This function mutates the worktree's directory and waits for those mutations to be picked up, // to ensure that all redundant FS events have already been processed. #[cfg(any(test, feature = "test-support"))] fn flush_fs_events<'a>( &self, cx: &'a gpui::TestAppContext, ) -> futures::future::LocalBoxFuture<'a, ()> { use smol::future::FutureExt; let filename = "fs-event-sentinel"; let tree = self.clone(); let (fs, root_path) = self.read_with(cx, |tree, _| { let tree = tree.as_local().unwrap(); (tree.fs.clone(), tree.abs_path().clone()) }); async move { fs.create_file(&root_path.join(filename), Default::default()) .await .unwrap(); tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_some()) .await; fs.remove_file(&root_path.join(filename), Default::default()) .await .unwrap(); tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_none()) .await; cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) .await; } .boxed_local() } } #[derive(Clone, Debug)] struct TraversalProgress<'a> { max_path: &'a Path, count: usize, visible_count: usize, file_count: usize, visible_file_count: usize, } impl<'a> TraversalProgress<'a> { fn count(&self, include_dirs: bool, include_ignored: bool) -> usize { match (include_ignored, include_dirs) { (true, true) => self.count, (true, false) => self.file_count, (false, true) => self.visible_count, (false, false) => self.visible_file_count, } } } impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> { fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { self.max_path = summary.max_path.as_ref(); self.count += summary.count; self.visible_count += summary.visible_count; self.file_count += summary.file_count; self.visible_file_count += summary.visible_file_count; } } impl<'a> Default for TraversalProgress<'a> { fn default() -> Self { Self { max_path: Path::new(""), count: 0, visible_count: 0, file_count: 0, visible_file_count: 0, } } } pub struct Traversal<'a> { cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>, include_ignored: bool, include_dirs: bool, } impl<'a> Traversal<'a> { pub fn advance(&mut self) -> bool { self.advance_to_offset(self.offset() + 1) } pub fn advance_to_offset(&mut self, offset: usize) -> bool { self.cursor.seek_forward( &TraversalTarget::Count { count: offset, include_dirs: self.include_dirs, include_ignored: self.include_ignored, }, Bias::Right, &(), ) } pub fn advance_to_sibling(&mut self) -> bool { while let Some(entry) = self.cursor.item() { self.cursor.seek_forward( &TraversalTarget::PathSuccessor(&entry.path), Bias::Left, &(), ); if let Some(entry) = self.cursor.item() { if (self.include_dirs || !entry.is_dir()) && (self.include_ignored || !entry.is_ignored) { return true; } } } false } pub fn entry(&self) -> Option<&'a Entry> { self.cursor.item() } pub fn offset(&self) -> usize { self.cursor .start() .count(self.include_dirs, self.include_ignored) } } impl<'a> Iterator for Traversal<'a> { type Item = &'a Entry; fn next(&mut self) -> Option { if let Some(item) = self.entry() { self.advance(); Some(item) } else { None } } } #[derive(Debug)] enum TraversalTarget<'a> { Path(&'a Path), PathSuccessor(&'a Path), Count { count: usize, include_ignored: bool, include_dirs: bool, }, } impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> { fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering { match self { TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path), TraversalTarget::PathSuccessor(path) => { if !cursor_location.max_path.starts_with(path) { Ordering::Equal } else { Ordering::Greater } } TraversalTarget::Count { count, include_dirs, include_ignored, } => Ord::cmp( count, &cursor_location.count(*include_dirs, *include_ignored), ), } } } struct ChildEntriesIter<'a> { parent_path: &'a Path, traversal: Traversal<'a>, } impl<'a> Iterator for ChildEntriesIter<'a> { type Item = &'a Entry; fn next(&mut self) -> Option { if let Some(item) = self.traversal.entry() { if item.path.starts_with(&self.parent_path) { self.traversal.advance_to_sibling(); return Some(item); } } None } } impl<'a> From<&'a Entry> for proto::Entry { fn from(entry: &'a Entry) -> Self { Self { id: entry.id.to_proto(), is_dir: entry.is_dir(), path: entry.path.to_string_lossy().into(), inode: entry.inode, mtime: Some(entry.mtime.into()), is_symlink: entry.is_symlink, is_ignored: entry.is_ignored, } } } impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry { type Error = anyhow::Error; fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result { if let Some(mtime) = entry.mtime { let kind = if entry.is_dir { EntryKind::Dir } else { let mut char_bag = *root_char_bag; char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase())); EntryKind::File(char_bag) }; let path: Arc = PathBuf::from(entry.path).into(); Ok(Entry { id: ProjectEntryId::from_proto(entry.id), kind, path, inode: entry.inode, mtime: mtime.into(), is_symlink: entry.is_symlink, is_ignored: entry.is_ignored, }) } else { Err(anyhow!( "missing mtime in remote worktree entry {:?}", entry.path )) } } } #[cfg(test)] mod tests { use super::*; use fs::repository::FakeGitRepository; use fs::{FakeFs, RealFs}; use gpui::{executor::Deterministic, TestAppContext}; use pretty_assertions::assert_eq; use rand::prelude::*; use serde_json::json; use std::{env, fmt::Write}; use util::{http::FakeHttpClient, test::temp_tree}; #[gpui::test] async fn test_traversal(cx: &mut TestAppContext) { let fs = FakeFs::new(cx.background()); fs.insert_tree( "/root", json!({ ".gitignore": "a/b\n", "a": { "b": "", "c": "", } }), ) .await; let http_client = FakeHttpClient::with_404_response(); let client = cx.read(|cx| Client::new(http_client, cx)); let tree = Worktree::local( client, Path::new("/root"), true, fs, Default::default(), &mut cx.to_async(), ) .await .unwrap(); cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) .await; tree.read_with(cx, |tree, _| { assert_eq!( tree.entries(false) .map(|entry| entry.path.as_ref()) .collect::>(), vec![ Path::new(""), Path::new(".gitignore"), Path::new("a"), Path::new("a/c"), ] ); assert_eq!( tree.entries(true) .map(|entry| entry.path.as_ref()) .collect::>(), vec![ Path::new(""), Path::new(".gitignore"), Path::new("a"), Path::new("a/b"), Path::new("a/c"), ] ); }) } #[gpui::test(iterations = 10)] async fn test_circular_symlinks(executor: Arc, cx: &mut TestAppContext) { let fs = FakeFs::new(cx.background()); fs.insert_tree( "/root", json!({ "lib": { "a": { "a.txt": "" }, "b": { "b.txt": "" } } }), ) .await; fs.insert_symlink("/root/lib/a/lib", "..".into()).await; fs.insert_symlink("/root/lib/b/lib", "..".into()).await; let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx)); let tree = Worktree::local( client, Path::new("/root"), true, fs.clone(), Default::default(), &mut cx.to_async(), ) .await .unwrap(); cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) .await; tree.read_with(cx, |tree, _| { assert_eq!( tree.entries(false) .map(|entry| entry.path.as_ref()) .collect::>(), vec![ Path::new(""), Path::new("lib"), Path::new("lib/a"), Path::new("lib/a/a.txt"), Path::new("lib/a/lib"), Path::new("lib/b"), Path::new("lib/b/b.txt"), Path::new("lib/b/lib"), ] ); }); fs.rename( Path::new("/root/lib/a/lib"), Path::new("/root/lib/a/lib-2"), Default::default(), ) .await .unwrap(); executor.run_until_parked(); tree.read_with(cx, |tree, _| { assert_eq!( tree.entries(false) .map(|entry| entry.path.as_ref()) .collect::>(), vec![ Path::new(""), Path::new("lib"), Path::new("lib/a"), Path::new("lib/a/a.txt"), Path::new("lib/a/lib-2"), Path::new("lib/b"), Path::new("lib/b/b.txt"), Path::new("lib/b/lib"), ] ); }); } #[gpui::test] async fn test_rescan_with_gitignore(cx: &mut TestAppContext) { let parent_dir = temp_tree(json!({ ".gitignore": "ancestor-ignored-file1\nancestor-ignored-file2\n", "tree": { ".git": {}, ".gitignore": "ignored-dir\n", "tracked-dir": { "tracked-file1": "", "ancestor-ignored-file1": "", }, "ignored-dir": { "ignored-file1": "" } } })); let dir = parent_dir.path().join("tree"); let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx)); let tree = Worktree::local( client, dir.as_path(), true, Arc::new(RealFs), Default::default(), &mut cx.to_async(), ) .await .unwrap(); cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) .await; tree.flush_fs_events(cx).await; cx.read(|cx| { let tree = tree.read(cx); assert!( !tree .entry_for_path("tracked-dir/tracked-file1") .unwrap() .is_ignored ); assert!( tree.entry_for_path("tracked-dir/ancestor-ignored-file1") .unwrap() .is_ignored ); assert!( tree.entry_for_path("ignored-dir/ignored-file1") .unwrap() .is_ignored ); }); std::fs::write(dir.join("tracked-dir/tracked-file2"), "").unwrap(); std::fs::write(dir.join("tracked-dir/ancestor-ignored-file2"), "").unwrap(); std::fs::write(dir.join("ignored-dir/ignored-file2"), "").unwrap(); tree.flush_fs_events(cx).await; cx.read(|cx| { let tree = tree.read(cx); assert!( !tree .entry_for_path("tracked-dir/tracked-file2") .unwrap() .is_ignored ); assert!( tree.entry_for_path("tracked-dir/ancestor-ignored-file2") .unwrap() .is_ignored ); assert!( tree.entry_for_path("ignored-dir/ignored-file2") .unwrap() .is_ignored ); assert!(tree.entry_for_path(".git").unwrap().is_ignored); }); } #[gpui::test] async fn test_git_repository_for_path(cx: &mut TestAppContext) { let root = temp_tree(json!({ "dir1": { ".git": {}, "deps": { "dep1": { ".git": {}, "src": { "a.txt": "" } } }, "src": { "b.txt": "" } }, "c.txt": "", })); let http_client = FakeHttpClient::with_404_response(); let client = cx.read(|cx| Client::new(http_client, cx)); let tree = Worktree::local( client, root.path(), true, Arc::new(RealFs), Default::default(), &mut cx.to_async(), ) .await .unwrap(); cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) .await; tree.flush_fs_events(cx).await; tree.read_with(cx, |tree, _cx| { let tree = tree.as_local().unwrap(); assert!(tree.repo_for("c.txt".as_ref()).is_none()); let repo = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap(); assert_eq!(repo.content_path.as_ref(), Path::new("dir1")); assert_eq!(repo.git_dir_path.as_ref(), Path::new("dir1/.git")); let repo = tree.repo_for("dir1/deps/dep1/src/a.txt".as_ref()).unwrap(); assert_eq!(repo.content_path.as_ref(), Path::new("dir1/deps/dep1")); assert_eq!(repo.git_dir_path.as_ref(), Path::new("dir1/deps/dep1/.git"),); }); let original_scan_id = tree.read_with(cx, |tree, _cx| { let tree = tree.as_local().unwrap(); tree.repo_for("dir1/src/b.txt".as_ref()).unwrap().scan_id }); std::fs::write(root.path().join("dir1/.git/random_new_file"), "hello").unwrap(); tree.flush_fs_events(cx).await; tree.read_with(cx, |tree, _cx| { let tree = tree.as_local().unwrap(); let new_scan_id = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap().scan_id; assert_ne!( original_scan_id, new_scan_id, "original {original_scan_id}, new {new_scan_id}" ); }); std::fs::remove_dir_all(root.path().join("dir1/.git")).unwrap(); tree.flush_fs_events(cx).await; tree.read_with(cx, |tree, _cx| { let tree = tree.as_local().unwrap(); assert!(tree.repo_for("dir1/src/b.txt".as_ref()).is_none()); }); } #[test] fn test_changed_repos() { fn fake_entry(git_dir_path: impl AsRef, scan_id: usize) -> GitRepositoryEntry { GitRepositoryEntry { repo: Arc::new(Mutex::new(FakeGitRepository::default())), scan_id, content_path: git_dir_path.as_ref().parent().unwrap().into(), git_dir_path: git_dir_path.as_ref().into(), } } let prev_repos: Vec = vec![ fake_entry("/.git", 0), fake_entry("/a/.git", 0), fake_entry("/a/b/.git", 0), ]; let new_repos: Vec = vec![ fake_entry("/a/.git", 1), fake_entry("/a/b/.git", 0), fake_entry("/a/c/.git", 0), ]; let res = LocalWorktree::changed_repos(&prev_repos, &new_repos); // Deletion retained assert!(res .iter() .find(|repo| repo.git_dir_path.as_ref() == Path::new("/.git") && repo.scan_id == 0) .is_some()); // Update retained assert!(res .iter() .find(|repo| repo.git_dir_path.as_ref() == Path::new("/a/.git") && repo.scan_id == 1) .is_some()); // Addition retained assert!(res .iter() .find(|repo| repo.git_dir_path.as_ref() == Path::new("/a/c/.git") && repo.scan_id == 0) .is_some()); // Nochange, not retained assert!(res .iter() .find(|repo| repo.git_dir_path.as_ref() == Path::new("/a/b/.git") && repo.scan_id == 0) .is_none()); } #[gpui::test] async fn test_write_file(cx: &mut TestAppContext) { let dir = temp_tree(json!({ ".git": {}, ".gitignore": "ignored-dir\n", "tracked-dir": {}, "ignored-dir": {} })); let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx)); let tree = Worktree::local( client, dir.path(), true, Arc::new(RealFs), Default::default(), &mut cx.to_async(), ) .await .unwrap(); cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) .await; tree.flush_fs_events(cx).await; tree.update(cx, |tree, cx| { tree.as_local().unwrap().write_file( Path::new("tracked-dir/file.txt"), "hello".into(), Default::default(), cx, ) }) .await .unwrap(); tree.update(cx, |tree, cx| { tree.as_local().unwrap().write_file( Path::new("ignored-dir/file.txt"), "world".into(), Default::default(), cx, ) }) .await .unwrap(); tree.read_with(cx, |tree, _| { let tracked = tree.entry_for_path("tracked-dir/file.txt").unwrap(); let ignored = tree.entry_for_path("ignored-dir/file.txt").unwrap(); assert!(!tracked.is_ignored); assert!(ignored.is_ignored); }); } #[gpui::test(iterations = 30)] async fn test_create_directory_during_initial_scan(cx: &mut TestAppContext) { let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx)); let fs = FakeFs::new(cx.background()); fs.insert_tree( "/root", json!({ "b": {}, "c": {}, "d": {}, }), ) .await; let tree = Worktree::local( client, "/root".as_ref(), true, fs, Default::default(), &mut cx.to_async(), ) .await .unwrap(); let mut snapshot1 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot()); let entry = tree .update(cx, |tree, cx| { tree.as_local_mut() .unwrap() .create_entry("a/e".as_ref(), true, cx) }) .await .unwrap(); assert!(entry.is_dir()); cx.foreground().run_until_parked(); tree.read_with(cx, |tree, _| { assert_eq!(tree.entry_for_path("a/e").unwrap().kind, EntryKind::Dir); }); let snapshot2 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot()); let update = snapshot2.build_update(&snapshot1, 0, 0, true); snapshot1.apply_remote_update(update).unwrap(); assert_eq!(snapshot1.to_vec(true), snapshot2.to_vec(true),); } #[gpui::test(iterations = 100)] async fn test_random_worktree_operations_during_initial_scan( cx: &mut TestAppContext, mut rng: StdRng, ) { let operations = env::var("OPERATIONS") .map(|o| o.parse().unwrap()) .unwrap_or(5); let initial_entries = env::var("INITIAL_ENTRIES") .map(|o| o.parse().unwrap()) .unwrap_or(20); let root_dir = Path::new("/test"); let fs = FakeFs::new(cx.background()) as Arc; fs.as_fake().insert_tree(root_dir, json!({})).await; for _ in 0..initial_entries { randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await; } log::info!("generated initial tree"); let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx)); let worktree = Worktree::local( client.clone(), root_dir, true, fs.clone(), Default::default(), &mut cx.to_async(), ) .await .unwrap(); let mut snapshot = worktree.update(cx, |tree, _| tree.as_local().unwrap().snapshot()); for _ in 0..operations { worktree .update(cx, |worktree, cx| { randomly_mutate_worktree(worktree, &mut rng, cx) }) .await .log_err(); worktree.read_with(cx, |tree, _| { tree.as_local().unwrap().snapshot.check_invariants() }); if rng.gen_bool(0.6) { let new_snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()); let update = new_snapshot.build_update(&snapshot, 0, 0, true); snapshot.apply_remote_update(update.clone()).unwrap(); assert_eq!( snapshot.to_vec(true), new_snapshot.to_vec(true), "incorrect snapshot after update {:?}", update ); } } worktree .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete()) .await; worktree.read_with(cx, |tree, _| { tree.as_local().unwrap().snapshot.check_invariants() }); let new_snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()); let update = new_snapshot.build_update(&snapshot, 0, 0, true); snapshot.apply_remote_update(update.clone()).unwrap(); assert_eq!( snapshot.to_vec(true), new_snapshot.to_vec(true), "incorrect snapshot after update {:?}", update ); } #[gpui::test(iterations = 100)] async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng) { let operations = env::var("OPERATIONS") .map(|o| o.parse().unwrap()) .unwrap_or(40); let initial_entries = env::var("INITIAL_ENTRIES") .map(|o| o.parse().unwrap()) .unwrap_or(20); let root_dir = Path::new("/test"); let fs = FakeFs::new(cx.background()) as Arc; fs.as_fake().insert_tree(root_dir, json!({})).await; for _ in 0..initial_entries { randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await; } log::info!("generated initial tree"); let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx)); let worktree = Worktree::local( client.clone(), root_dir, true, fs.clone(), Default::default(), &mut cx.to_async(), ) .await .unwrap(); worktree .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete()) .await; // After the initial scan is complete, the `UpdatedEntries` event can // be used to follow along with all changes to the worktree's snapshot. worktree.update(cx, |tree, cx| { let mut paths = tree .as_local() .unwrap() .paths() .cloned() .collect::>(); cx.subscribe(&worktree, move |tree, _, event, _| { if let Event::UpdatedEntries(changes) = event { for (path, change_type) in changes.iter() { let path = path.clone(); let ix = match paths.binary_search(&path) { Ok(ix) | Err(ix) => ix, }; match change_type { PathChange::Added => { assert_ne!(paths.get(ix), Some(&path)); paths.insert(ix, path); } PathChange::Removed => { assert_eq!(paths.get(ix), Some(&path)); paths.remove(ix); } PathChange::Updated => { assert_eq!(paths.get(ix), Some(&path)); } PathChange::AddedOrUpdated => { if paths[ix] != path { paths.insert(ix, path); } } } } let new_paths = tree.paths().cloned().collect::>(); assert_eq!(paths, new_paths, "incorrect changes: {:?}", changes); } }) .detach(); }); let mut snapshots = Vec::new(); let mut mutations_len = operations; while mutations_len > 1 { randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await; let buffered_event_count = fs.as_fake().buffered_event_count().await; if buffered_event_count > 0 && rng.gen_bool(0.3) { let len = rng.gen_range(0..=buffered_event_count); log::info!("flushing {} events", len); fs.as_fake().flush_events(len).await; } else { randomly_mutate_fs(&fs, root_dir, 0.6, &mut rng).await; mutations_len -= 1; } cx.foreground().run_until_parked(); if rng.gen_bool(0.2) { log::info!("storing snapshot {}", snapshots.len()); let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()); snapshots.push(snapshot); } } log::info!("quiescing"); fs.as_fake().flush_events(usize::MAX).await; cx.foreground().run_until_parked(); let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()); snapshot.check_invariants(); { let new_worktree = Worktree::local( client.clone(), root_dir, true, fs.clone(), Default::default(), &mut cx.to_async(), ) .await .unwrap(); new_worktree .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete()) .await; let new_snapshot = new_worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()); assert_eq!(snapshot.to_vec(true), new_snapshot.to_vec(true)); } for (i, mut prev_snapshot) in snapshots.into_iter().enumerate() { let include_ignored = rng.gen::(); if !include_ignored { let mut entries_by_path_edits = Vec::new(); let mut entries_by_id_edits = Vec::new(); for entry in prev_snapshot .entries_by_id .cursor::<()>() .filter(|e| e.is_ignored) { entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone()))); entries_by_id_edits.push(Edit::Remove(entry.id)); } prev_snapshot .entries_by_path .edit(entries_by_path_edits, &()); prev_snapshot.entries_by_id.edit(entries_by_id_edits, &()); } let update = snapshot.build_update(&prev_snapshot, 0, 0, include_ignored); prev_snapshot.apply_remote_update(update.clone()).unwrap(); assert_eq!( prev_snapshot.to_vec(include_ignored), snapshot.to_vec(include_ignored), "wrong update for snapshot {i}. update: {:?}", update ); } } fn randomly_mutate_worktree( worktree: &mut Worktree, rng: &mut impl Rng, cx: &mut ModelContext, ) -> Task> { let worktree = worktree.as_local_mut().unwrap(); let snapshot = worktree.snapshot(); let entry = snapshot.entries(false).choose(rng).unwrap(); match rng.gen_range(0_u32..100) { 0..=33 if entry.path.as_ref() != Path::new("") => { log::info!("deleting entry {:?} ({})", entry.path, entry.id.0); worktree.delete_entry(entry.id, cx).unwrap() } ..=66 if entry.path.as_ref() != Path::new("") => { let other_entry = snapshot.entries(false).choose(rng).unwrap(); let new_parent_path = if other_entry.is_dir() { other_entry.path.clone() } else { other_entry.path.parent().unwrap().into() }; let mut new_path = new_parent_path.join(gen_name(rng)); if new_path.starts_with(&entry.path) { new_path = gen_name(rng).into(); } log::info!( "renaming entry {:?} ({}) to {:?}", entry.path, entry.id.0, new_path ); let task = worktree.rename_entry(entry.id, new_path, cx).unwrap(); cx.foreground().spawn(async move { task.await?; Ok(()) }) } _ => { let task = if entry.is_dir() { let child_path = entry.path.join(gen_name(rng)); let is_dir = rng.gen_bool(0.3); log::info!( "creating {} at {:?}", if is_dir { "dir" } else { "file" }, child_path, ); worktree.create_entry(child_path, is_dir, cx) } else { log::info!("overwriting file {:?} ({})", entry.path, entry.id.0); worktree.write_file(entry.path.clone(), "".into(), Default::default(), cx) }; cx.foreground().spawn(async move { task.await?; Ok(()) }) } } } async fn randomly_mutate_fs( fs: &Arc, root_path: &Path, insertion_probability: f64, rng: &mut impl Rng, ) { let mut files = Vec::new(); let mut dirs = Vec::new(); for path in fs.as_fake().paths() { if path.starts_with(root_path) { if fs.is_file(&path).await { files.push(path); } else { dirs.push(path); } } } if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) { let path = dirs.choose(rng).unwrap(); let new_path = path.join(gen_name(rng)); if rng.gen() { log::info!( "creating dir {:?}", new_path.strip_prefix(root_path).unwrap() ); fs.create_dir(&new_path).await.unwrap(); } else { log::info!( "creating file {:?}", new_path.strip_prefix(root_path).unwrap() ); fs.create_file(&new_path, Default::default()).await.unwrap(); } } else if rng.gen_bool(0.05) { let ignore_dir_path = dirs.choose(rng).unwrap(); let ignore_path = ignore_dir_path.join(&*GITIGNORE); let subdirs = dirs .iter() .filter(|d| d.starts_with(&ignore_dir_path)) .cloned() .collect::>(); let subfiles = files .iter() .filter(|d| d.starts_with(&ignore_dir_path)) .cloned() .collect::>(); let files_to_ignore = { let len = rng.gen_range(0..=subfiles.len()); subfiles.choose_multiple(rng, len) }; let dirs_to_ignore = { let len = rng.gen_range(0..subdirs.len()); subdirs.choose_multiple(rng, len) }; let mut ignore_contents = String::new(); for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) { writeln!( ignore_contents, "{}", path_to_ignore .strip_prefix(&ignore_dir_path) .unwrap() .to_str() .unwrap() ) .unwrap(); } log::info!( "creating gitignore {:?} with contents:\n{}", ignore_path.strip_prefix(&root_path).unwrap(), ignore_contents ); fs.save( &ignore_path, &ignore_contents.as_str().into(), Default::default(), ) .await .unwrap(); } else { let old_path = { let file_path = files.choose(rng); let dir_path = dirs[1..].choose(rng); file_path.into_iter().chain(dir_path).choose(rng).unwrap() }; let is_rename = rng.gen(); if is_rename { let new_path_parent = dirs .iter() .filter(|d| !d.starts_with(old_path)) .choose(rng) .unwrap(); let overwrite_existing_dir = !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3); let new_path = if overwrite_existing_dir { fs.remove_dir( &new_path_parent, RemoveOptions { recursive: true, ignore_if_not_exists: true, }, ) .await .unwrap(); new_path_parent.to_path_buf() } else { new_path_parent.join(gen_name(rng)) }; log::info!( "renaming {:?} to {}{:?}", old_path.strip_prefix(&root_path).unwrap(), if overwrite_existing_dir { "overwrite " } else { "" }, new_path.strip_prefix(&root_path).unwrap() ); fs.rename( &old_path, &new_path, fs::RenameOptions { overwrite: true, ignore_if_exists: true, }, ) .await .unwrap(); } else if fs.is_file(&old_path).await { log::info!( "deleting file {:?}", old_path.strip_prefix(&root_path).unwrap() ); fs.remove_file(old_path, Default::default()).await.unwrap(); } else { log::info!( "deleting dir {:?}", old_path.strip_prefix(&root_path).unwrap() ); fs.remove_dir( &old_path, RemoveOptions { recursive: true, ignore_if_not_exists: true, }, ) .await .unwrap(); } } } fn gen_name(rng: &mut impl Rng) -> String { (0..6) .map(|_| rng.sample(rand::distributions::Alphanumeric)) .map(char::from) .collect() } impl LocalSnapshot { fn check_invariants(&self) { assert_eq!( self.entries_by_path .cursor::<()>() .map(|e| (&e.path, e.id)) .collect::>(), self.entries_by_id .cursor::<()>() .map(|e| (&e.path, e.id)) .collect::>() .into_iter() .collect::>(), "entries_by_path and entries_by_id are inconsistent" ); let mut files = self.files(true, 0); let mut visible_files = self.files(false, 0); for entry in self.entries_by_path.cursor::<()>() { if entry.is_file() { assert_eq!(files.next().unwrap().inode, entry.inode); if !entry.is_ignored { assert_eq!(visible_files.next().unwrap().inode, entry.inode); } } } assert!(files.next().is_none()); assert!(visible_files.next().is_none()); let mut bfs_paths = Vec::new(); let mut stack = vec![Path::new("")]; while let Some(path) = stack.pop() { bfs_paths.push(path); let ix = stack.len(); for child_entry in self.child_entries(path) { stack.insert(ix, &child_entry.path); } } let dfs_paths_via_iter = self .entries_by_path .cursor::<()>() .map(|e| e.path.as_ref()) .collect::>(); assert_eq!(bfs_paths, dfs_paths_via_iter); let dfs_paths_via_traversal = self .entries(true) .map(|e| e.path.as_ref()) .collect::>(); assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter); for ignore_parent_abs_path in self.ignores_by_parent_abs_path.keys() { let ignore_parent_path = ignore_parent_abs_path.strip_prefix(&self.abs_path).unwrap(); assert!(self.entry_for_path(&ignore_parent_path).is_some()); assert!(self .entry_for_path(ignore_parent_path.join(&*GITIGNORE)) .is_some()); } } fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> { let mut paths = Vec::new(); for entry in self.entries_by_path.cursor::<()>() { if include_ignored || !entry.is_ignored { paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored)); } } paths.sort_by(|a, b| a.0.cmp(b.0)); paths } } }