worktree.rs

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