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>, ProjectEntryId), 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>, ProjectEntryId), 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<BackgroundScannerState>,
2403    finished_initial_scan: bool,
2404}
2405
2406struct BackgroundScannerState {
2407    snapshot: Snapshot,
2408    event_paths: Vec<Arc<Path>>,
2409}
2410
2411impl BackgroundScanner {
2412    fn new(
2413        snapshot: LocalSnapshot,
2414        fs: Arc<dyn Fs>,
2415        status_updates_tx: UnboundedSender<ScanState>,
2416        executor: Arc<executor::Background>,
2417        refresh_requests_rx: channel::Receiver<(Vec<PathBuf>, barrier::Sender)>,
2418    ) -> Self {
2419        Self {
2420            fs,
2421            status_updates_tx,
2422            executor,
2423            refresh_requests_rx,
2424            prev_state: Mutex::new(BackgroundScannerState {
2425                snapshot: snapshot.snapshot.clone(),
2426                event_paths: Default::default(),
2427            }),
2428            snapshot: Mutex::new(snapshot),
2429            finished_initial_scan: false,
2430        }
2431    }
2432
2433    async fn run(
2434        &mut self,
2435        mut events_rx: Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>>,
2436    ) {
2437        use futures::FutureExt as _;
2438
2439        let (root_abs_path, root_inode) = {
2440            let snapshot = self.snapshot.lock();
2441            (
2442                snapshot.abs_path.clone(),
2443                snapshot.root_entry().map(|e| e.inode),
2444            )
2445        };
2446
2447        // Populate ignores above the root.
2448        let ignore_stack;
2449        for ancestor in root_abs_path.ancestors().skip(1) {
2450            if let Ok(ignore) = build_gitignore(&ancestor.join(&*GITIGNORE), self.fs.as_ref()).await
2451            {
2452                self.snapshot
2453                    .lock()
2454                    .ignores_by_parent_abs_path
2455                    .insert(ancestor.into(), (ignore.into(), 0));
2456            }
2457        }
2458        {
2459            let mut snapshot = self.snapshot.lock();
2460            snapshot.scan_id += 1;
2461            ignore_stack = snapshot.ignore_stack_for_abs_path(&root_abs_path, true);
2462            if ignore_stack.is_all() {
2463                if let Some(mut root_entry) = snapshot.root_entry().cloned() {
2464                    root_entry.is_ignored = true;
2465                    snapshot.insert_entry(root_entry, self.fs.as_ref());
2466                }
2467            }
2468        };
2469
2470        // Perform an initial scan of the directory.
2471        let (scan_job_tx, scan_job_rx) = channel::unbounded();
2472        smol::block_on(scan_job_tx.send(ScanJob {
2473            abs_path: root_abs_path,
2474            path: Arc::from(Path::new("")),
2475            ignore_stack,
2476            ancestor_inodes: TreeSet::from_ordered_entries(root_inode),
2477            scan_queue: scan_job_tx.clone(),
2478        }))
2479        .unwrap();
2480        drop(scan_job_tx);
2481        self.scan_dirs(true, scan_job_rx).await;
2482        {
2483            let mut snapshot = self.snapshot.lock();
2484            snapshot.completed_scan_id = snapshot.scan_id;
2485        }
2486        self.send_status_update(false, None);
2487
2488        // Process any any FS events that occurred while performing the initial scan.
2489        // For these events, update events cannot be as precise, because we didn't
2490        // have the previous state loaded yet.
2491        if let Poll::Ready(Some(events)) = futures::poll!(events_rx.next()) {
2492            let mut paths = events.into_iter().map(|e| e.path).collect::<Vec<_>>();
2493            while let Poll::Ready(Some(more_events)) = futures::poll!(events_rx.next()) {
2494                paths.extend(more_events.into_iter().map(|e| e.path));
2495            }
2496            self.process_events(paths).await;
2497        }
2498
2499        self.finished_initial_scan = true;
2500
2501        // Continue processing events until the worktree is dropped.
2502        loop {
2503            select_biased! {
2504                // Process any path refresh requests from the worktree. Prioritize
2505                // these before handling changes reported by the filesystem.
2506                request = self.refresh_requests_rx.recv().fuse() => {
2507                    let Ok((paths, barrier)) = request else { break };
2508                    if !self.process_refresh_request(paths, barrier).await {
2509                        return;
2510                    }
2511                }
2512
2513                events = events_rx.next().fuse() => {
2514                    let Some(events) = events else { break };
2515                    let mut paths = events.into_iter().map(|e| e.path).collect::<Vec<_>>();
2516                    while let Poll::Ready(Some(more_events)) = futures::poll!(events_rx.next()) {
2517                        paths.extend(more_events.into_iter().map(|e| e.path));
2518                    }
2519                    self.process_events(paths).await;
2520                }
2521            }
2522        }
2523    }
2524
2525    async fn process_refresh_request(&self, paths: Vec<PathBuf>, barrier: barrier::Sender) -> bool {
2526        if let Some(mut paths) = self.reload_entries_for_paths(paths, None).await {
2527            paths.sort_unstable();
2528            util::extend_sorted(
2529                &mut self.prev_state.lock().event_paths,
2530                paths,
2531                usize::MAX,
2532                Ord::cmp,
2533            );
2534        }
2535        self.send_status_update(false, Some(barrier))
2536    }
2537
2538    async fn process_events(&mut self, paths: Vec<PathBuf>) {
2539        let (scan_job_tx, scan_job_rx) = channel::unbounded();
2540        if let Some(mut paths) = self
2541            .reload_entries_for_paths(paths, Some(scan_job_tx.clone()))
2542            .await
2543        {
2544            paths.sort_unstable();
2545            util::extend_sorted(
2546                &mut self.prev_state.lock().event_paths,
2547                paths,
2548                usize::MAX,
2549                Ord::cmp,
2550            );
2551        }
2552        drop(scan_job_tx);
2553        self.scan_dirs(false, scan_job_rx).await;
2554
2555        self.update_ignore_statuses().await;
2556
2557        let mut snapshot = self.snapshot.lock();
2558
2559        let mut git_repositories = mem::take(&mut snapshot.git_repositories);
2560        git_repositories.retain(|work_directory_id, _| {
2561            snapshot
2562                .entry_for_id(*work_directory_id)
2563                .map_or(false, |entry| {
2564                    snapshot.entry_for_path(entry.path.join(*DOT_GIT)).is_some()
2565                })
2566        });
2567        snapshot.git_repositories = git_repositories;
2568
2569        let mut git_repository_entries = mem::take(&mut snapshot.snapshot.repository_entries);
2570        git_repository_entries.retain(|_, entry| {
2571            snapshot
2572                .git_repositories
2573                .get(&entry.work_directory.0)
2574                .is_some()
2575        });
2576        snapshot.snapshot.repository_entries = git_repository_entries;
2577
2578        snapshot.removed_entry_ids.clear();
2579        snapshot.completed_scan_id = snapshot.scan_id;
2580
2581        drop(snapshot);
2582
2583        self.send_status_update(false, None);
2584        self.prev_state.lock().event_paths.clear();
2585    }
2586
2587    async fn scan_dirs(
2588        &self,
2589        enable_progress_updates: bool,
2590        scan_jobs_rx: channel::Receiver<ScanJob>,
2591    ) {
2592        use futures::FutureExt as _;
2593
2594        if self
2595            .status_updates_tx
2596            .unbounded_send(ScanState::Started)
2597            .is_err()
2598        {
2599            return;
2600        }
2601
2602        let progress_update_count = AtomicUsize::new(0);
2603        self.executor
2604            .scoped(|scope| {
2605                for _ in 0..self.executor.num_cpus() {
2606                    scope.spawn(async {
2607                        let mut last_progress_update_count = 0;
2608                        let progress_update_timer = self.progress_timer(enable_progress_updates).fuse();
2609                        futures::pin_mut!(progress_update_timer);
2610
2611                        loop {
2612                            select_biased! {
2613                                // Process any path refresh requests before moving on to process
2614                                // the scan queue, so that user operations are prioritized.
2615                                request = self.refresh_requests_rx.recv().fuse() => {
2616                                    let Ok((paths, barrier)) = request else { break };
2617                                    if !self.process_refresh_request(paths, barrier).await {
2618                                        return;
2619                                    }
2620                                }
2621
2622                                // Send periodic progress updates to the worktree. Use an atomic counter
2623                                // to ensure that only one of the workers sends a progress update after
2624                                // the update interval elapses.
2625                                _ = progress_update_timer => {
2626                                    match progress_update_count.compare_exchange(
2627                                        last_progress_update_count,
2628                                        last_progress_update_count + 1,
2629                                        SeqCst,
2630                                        SeqCst
2631                                    ) {
2632                                        Ok(_) => {
2633                                            last_progress_update_count += 1;
2634                                            self.send_status_update(true, None);
2635                                        }
2636                                        Err(count) => {
2637                                            last_progress_update_count = count;
2638                                        }
2639                                    }
2640                                    progress_update_timer.set(self.progress_timer(enable_progress_updates).fuse());
2641                                }
2642
2643                                // Recursively load directories from the file system.
2644                                job = scan_jobs_rx.recv().fuse() => {
2645                                    let Ok(job) = job else { break };
2646                                    if let Err(err) = self.scan_dir(&job).await {
2647                                        if job.path.as_ref() != Path::new("") {
2648                                            log::error!("error scanning directory {:?}: {}", job.abs_path, err);
2649                                        }
2650                                    }
2651                                }
2652                            }
2653                        }
2654                    })
2655                }
2656            })
2657            .await;
2658    }
2659
2660    fn send_status_update(&self, scanning: bool, barrier: Option<barrier::Sender>) -> bool {
2661        let mut prev_state = self.prev_state.lock();
2662        let new_snapshot = self.snapshot.lock().clone();
2663        let old_snapshot = mem::replace(&mut prev_state.snapshot, new_snapshot.snapshot.clone());
2664
2665        let changes = self.build_change_set(
2666            &old_snapshot,
2667            &new_snapshot.snapshot,
2668            &prev_state.event_paths,
2669        );
2670
2671        self.status_updates_tx
2672            .unbounded_send(ScanState::Updated {
2673                snapshot: new_snapshot,
2674                changes,
2675                scanning,
2676                barrier,
2677            })
2678            .is_ok()
2679    }
2680
2681    async fn scan_dir(&self, job: &ScanJob) -> Result<()> {
2682        let mut new_entries: Vec<Entry> = Vec::new();
2683        let mut new_jobs: Vec<Option<ScanJob>> = Vec::new();
2684        let mut ignore_stack = job.ignore_stack.clone();
2685        let mut new_ignore = None;
2686        let (root_abs_path, root_char_bag, next_entry_id) = {
2687            let snapshot = self.snapshot.lock();
2688            (
2689                snapshot.abs_path().clone(),
2690                snapshot.root_char_bag,
2691                snapshot.next_entry_id.clone(),
2692            )
2693        };
2694        let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2695        while let Some(child_abs_path) = child_paths.next().await {
2696            let child_abs_path: Arc<Path> = match child_abs_path {
2697                Ok(child_abs_path) => child_abs_path.into(),
2698                Err(error) => {
2699                    log::error!("error processing entry {:?}", error);
2700                    continue;
2701                }
2702            };
2703
2704            let child_name = child_abs_path.file_name().unwrap();
2705            let child_path: Arc<Path> = job.path.join(child_name).into();
2706            let child_metadata = match self.fs.metadata(&child_abs_path).await {
2707                Ok(Some(metadata)) => metadata,
2708                Ok(None) => continue,
2709                Err(err) => {
2710                    log::error!("error processing {:?}: {:?}", child_abs_path, err);
2711                    continue;
2712                }
2713            };
2714
2715            // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2716            if child_name == *GITIGNORE {
2717                match build_gitignore(&child_abs_path, self.fs.as_ref()).await {
2718                    Ok(ignore) => {
2719                        let ignore = Arc::new(ignore);
2720                        ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
2721                        new_ignore = Some(ignore);
2722                    }
2723                    Err(error) => {
2724                        log::error!(
2725                            "error loading .gitignore file {:?} - {:?}",
2726                            child_name,
2727                            error
2728                        );
2729                    }
2730                }
2731
2732                // Update ignore status of any child entries we've already processed to reflect the
2733                // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2734                // there should rarely be too numerous. Update the ignore stack associated with any
2735                // new jobs as well.
2736                let mut new_jobs = new_jobs.iter_mut();
2737                for entry in &mut new_entries {
2738                    let entry_abs_path = root_abs_path.join(&entry.path);
2739                    entry.is_ignored =
2740                        ignore_stack.is_abs_path_ignored(&entry_abs_path, entry.is_dir());
2741
2742                    if entry.is_dir() {
2743                        if let Some(job) = new_jobs.next().expect("Missing scan job for entry") {
2744                            job.ignore_stack = if entry.is_ignored {
2745                                IgnoreStack::all()
2746                            } else {
2747                                ignore_stack.clone()
2748                            };
2749                        }
2750                    }
2751                }
2752            }
2753
2754            let mut child_entry = Entry::new(
2755                child_path.clone(),
2756                &child_metadata,
2757                &next_entry_id,
2758                root_char_bag,
2759            );
2760
2761            if child_entry.is_dir() {
2762                let is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true);
2763                child_entry.is_ignored = is_ignored;
2764
2765                // Avoid recursing until crash in the case of a recursive symlink
2766                if !job.ancestor_inodes.contains(&child_entry.inode) {
2767                    let mut ancestor_inodes = job.ancestor_inodes.clone();
2768                    ancestor_inodes.insert(child_entry.inode);
2769
2770                    new_jobs.push(Some(ScanJob {
2771                        abs_path: child_abs_path,
2772                        path: child_path,
2773                        ignore_stack: if is_ignored {
2774                            IgnoreStack::all()
2775                        } else {
2776                            ignore_stack.clone()
2777                        },
2778                        ancestor_inodes,
2779                        scan_queue: job.scan_queue.clone(),
2780                    }));
2781                } else {
2782                    new_jobs.push(None);
2783                }
2784            } else {
2785                child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false);
2786            }
2787
2788            new_entries.push(child_entry);
2789        }
2790
2791        self.snapshot.lock().populate_dir(
2792            job.path.clone(),
2793            new_entries,
2794            new_ignore,
2795            self.fs.as_ref(),
2796        );
2797
2798        for new_job in new_jobs {
2799            if let Some(new_job) = new_job {
2800                job.scan_queue.send(new_job).await.unwrap();
2801            }
2802        }
2803
2804        Ok(())
2805    }
2806
2807    async fn reload_entries_for_paths(
2808        &self,
2809        mut abs_paths: Vec<PathBuf>,
2810        scan_queue_tx: Option<Sender<ScanJob>>,
2811    ) -> Option<Vec<Arc<Path>>> {
2812        let doing_recursive_update = scan_queue_tx.is_some();
2813
2814        abs_paths.sort_unstable();
2815        abs_paths.dedup_by(|a, b| a.starts_with(&b));
2816
2817        let root_abs_path = self.snapshot.lock().abs_path.clone();
2818        let root_canonical_path = self.fs.canonicalize(&root_abs_path).await.log_err()?;
2819        let metadata = futures::future::join_all(
2820            abs_paths
2821                .iter()
2822                .map(|abs_path| self.fs.metadata(&abs_path))
2823                .collect::<Vec<_>>(),
2824        )
2825        .await;
2826
2827        let mut snapshot = self.snapshot.lock();
2828        let is_idle = snapshot.completed_scan_id == snapshot.scan_id;
2829        snapshot.scan_id += 1;
2830        if is_idle && !doing_recursive_update {
2831            snapshot.completed_scan_id = snapshot.scan_id;
2832        }
2833
2834        // Remove any entries for paths that no longer exist or are being recursively
2835        // refreshed. Do this before adding any new entries, so that renames can be
2836        // detected regardless of the order of the paths.
2837        let mut event_paths = Vec::<Arc<Path>>::with_capacity(abs_paths.len());
2838        for (abs_path, metadata) in abs_paths.iter().zip(metadata.iter()) {
2839            if let Ok(path) = abs_path.strip_prefix(&root_canonical_path) {
2840                if matches!(metadata, Ok(None)) || doing_recursive_update {
2841                    snapshot.remove_path(path);
2842                }
2843                event_paths.push(path.into());
2844            } else {
2845                log::error!(
2846                    "unexpected event {:?} for root path {:?}",
2847                    abs_path,
2848                    root_canonical_path
2849                );
2850            }
2851        }
2852
2853        for (path, metadata) in event_paths.iter().cloned().zip(metadata.into_iter()) {
2854            let abs_path: Arc<Path> = root_abs_path.join(&path).into();
2855
2856            match metadata {
2857                Ok(Some(metadata)) => {
2858                    let ignore_stack =
2859                        snapshot.ignore_stack_for_abs_path(&abs_path, metadata.is_dir);
2860                    let mut fs_entry = Entry::new(
2861                        path.clone(),
2862                        &metadata,
2863                        snapshot.next_entry_id.as_ref(),
2864                        snapshot.root_char_bag,
2865                    );
2866                    fs_entry.is_ignored = ignore_stack.is_all();
2867                    snapshot.insert_entry(fs_entry, self.fs.as_ref());
2868
2869                    let scan_id = snapshot.scan_id;
2870
2871                    let repo_with_path_in_dotgit = snapshot.repo_for_metadata(&path);
2872                    if let Some((entry_id, repo)) = repo_with_path_in_dotgit {
2873                        let work_dir = snapshot
2874                            .entry_for_id(entry_id)
2875                            .map(|entry| RepositoryWorkDirectory(entry.path.clone()))?;
2876
2877                        let repo = repo.lock();
2878                        repo.reload_index();
2879                        let branch = repo.branch_name();
2880
2881                        snapshot.git_repositories.update(&entry_id, |entry| {
2882                            entry.scan_id = scan_id;
2883                        });
2884
2885                        snapshot
2886                            .repository_entries
2887                            .update(&work_dir, |entry| entry.branch = branch.map(Into::into));
2888                    }
2889
2890                    if let Some(scan_queue_tx) = &scan_queue_tx {
2891                        let mut ancestor_inodes = snapshot.ancestor_inodes_for_path(&path);
2892                        if metadata.is_dir && !ancestor_inodes.contains(&metadata.inode) {
2893                            ancestor_inodes.insert(metadata.inode);
2894                            smol::block_on(scan_queue_tx.send(ScanJob {
2895                                abs_path,
2896                                path,
2897                                ignore_stack,
2898                                ancestor_inodes,
2899                                scan_queue: scan_queue_tx.clone(),
2900                            }))
2901                            .unwrap();
2902                        }
2903                    }
2904                }
2905                Ok(None) => {}
2906                Err(err) => {
2907                    // TODO - create a special 'error' entry in the entries tree to mark this
2908                    log::error!("error reading file on event {:?}", err);
2909                }
2910            }
2911        }
2912
2913        Some(event_paths)
2914    }
2915
2916    async fn update_ignore_statuses(&self) {
2917        use futures::FutureExt as _;
2918
2919        let mut snapshot = self.snapshot.lock().clone();
2920        let mut ignores_to_update = Vec::new();
2921        let mut ignores_to_delete = Vec::new();
2922        for (parent_abs_path, (_, scan_id)) in &snapshot.ignores_by_parent_abs_path {
2923            if let Ok(parent_path) = parent_abs_path.strip_prefix(&snapshot.abs_path) {
2924                if *scan_id > snapshot.completed_scan_id
2925                    && snapshot.entry_for_path(parent_path).is_some()
2926                {
2927                    ignores_to_update.push(parent_abs_path.clone());
2928                }
2929
2930                let ignore_path = parent_path.join(&*GITIGNORE);
2931                if snapshot.entry_for_path(ignore_path).is_none() {
2932                    ignores_to_delete.push(parent_abs_path.clone());
2933                }
2934            }
2935        }
2936
2937        for parent_abs_path in ignores_to_delete {
2938            snapshot.ignores_by_parent_abs_path.remove(&parent_abs_path);
2939            self.snapshot
2940                .lock()
2941                .ignores_by_parent_abs_path
2942                .remove(&parent_abs_path);
2943        }
2944
2945        let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2946        ignores_to_update.sort_unstable();
2947        let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2948        while let Some(parent_abs_path) = ignores_to_update.next() {
2949            while ignores_to_update
2950                .peek()
2951                .map_or(false, |p| p.starts_with(&parent_abs_path))
2952            {
2953                ignores_to_update.next().unwrap();
2954            }
2955
2956            let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true);
2957            smol::block_on(ignore_queue_tx.send(UpdateIgnoreStatusJob {
2958                abs_path: parent_abs_path,
2959                ignore_stack,
2960                ignore_queue: ignore_queue_tx.clone(),
2961            }))
2962            .unwrap();
2963        }
2964        drop(ignore_queue_tx);
2965
2966        self.executor
2967            .scoped(|scope| {
2968                for _ in 0..self.executor.num_cpus() {
2969                    scope.spawn(async {
2970                        loop {
2971                            select_biased! {
2972                                // Process any path refresh requests before moving on to process
2973                                // the queue of ignore statuses.
2974                                request = self.refresh_requests_rx.recv().fuse() => {
2975                                    let Ok((paths, barrier)) = request else { break };
2976                                    if !self.process_refresh_request(paths, barrier).await {
2977                                        return;
2978                                    }
2979                                }
2980
2981                                // Recursively process directories whose ignores have changed.
2982                                job = ignore_queue_rx.recv().fuse() => {
2983                                    let Ok(job) = job else { break };
2984                                    self.update_ignore_status(job, &snapshot).await;
2985                                }
2986                            }
2987                        }
2988                    });
2989                }
2990            })
2991            .await;
2992    }
2993
2994    async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
2995        let mut ignore_stack = job.ignore_stack;
2996        if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) {
2997            ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
2998        }
2999
3000        let mut entries_by_id_edits = Vec::new();
3001        let mut entries_by_path_edits = Vec::new();
3002        let path = job.abs_path.strip_prefix(&snapshot.abs_path).unwrap();
3003        for mut entry in snapshot.child_entries(path).cloned() {
3004            let was_ignored = entry.is_ignored;
3005            let abs_path = snapshot.abs_path().join(&entry.path);
3006            entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir());
3007            if entry.is_dir() {
3008                let child_ignore_stack = if entry.is_ignored {
3009                    IgnoreStack::all()
3010                } else {
3011                    ignore_stack.clone()
3012                };
3013                job.ignore_queue
3014                    .send(UpdateIgnoreStatusJob {
3015                        abs_path: abs_path.into(),
3016                        ignore_stack: child_ignore_stack,
3017                        ignore_queue: job.ignore_queue.clone(),
3018                    })
3019                    .await
3020                    .unwrap();
3021            }
3022
3023            if entry.is_ignored != was_ignored {
3024                let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
3025                path_entry.scan_id = snapshot.scan_id;
3026                path_entry.is_ignored = entry.is_ignored;
3027                entries_by_id_edits.push(Edit::Insert(path_entry));
3028                entries_by_path_edits.push(Edit::Insert(entry));
3029            }
3030        }
3031
3032        let mut snapshot = self.snapshot.lock();
3033        snapshot.entries_by_path.edit(entries_by_path_edits, &());
3034        snapshot.entries_by_id.edit(entries_by_id_edits, &());
3035    }
3036
3037    fn build_change_set(
3038        &self,
3039        old_snapshot: &Snapshot,
3040        new_snapshot: &Snapshot,
3041        event_paths: &[Arc<Path>],
3042    ) -> HashMap<(Arc<Path>, ProjectEntryId), PathChange> {
3043        use PathChange::{Added, AddedOrUpdated, Removed, Updated};
3044
3045        let mut changes = HashMap::default();
3046        let mut old_paths = old_snapshot.entries_by_path.cursor::<PathKey>();
3047        let mut new_paths = new_snapshot.entries_by_path.cursor::<PathKey>();
3048        let received_before_initialized = !self.finished_initial_scan;
3049
3050        for path in event_paths {
3051            let path = PathKey(path.clone());
3052            old_paths.seek(&path, Bias::Left, &());
3053            new_paths.seek(&path, Bias::Left, &());
3054
3055            loop {
3056                match (old_paths.item(), new_paths.item()) {
3057                    (Some(old_entry), Some(new_entry)) => {
3058                        if old_entry.path > path.0
3059                            && new_entry.path > path.0
3060                            && !old_entry.path.starts_with(&path.0)
3061                            && !new_entry.path.starts_with(&path.0)
3062                        {
3063                            break;
3064                        }
3065
3066                        match Ord::cmp(&old_entry.path, &new_entry.path) {
3067                            Ordering::Less => {
3068                                changes.insert((old_entry.path.clone(), old_entry.id), Removed);
3069                                old_paths.next(&());
3070                            }
3071                            Ordering::Equal => {
3072                                if received_before_initialized {
3073                                    // If the worktree was not fully initialized when this event was generated,
3074                                    // we can't know whether this entry was added during the scan or whether
3075                                    // it was merely updated.
3076                                    changes.insert(
3077                                        (new_entry.path.clone(), new_entry.id),
3078                                        AddedOrUpdated,
3079                                    );
3080                                } else if old_entry.mtime != new_entry.mtime {
3081                                    changes.insert((new_entry.path.clone(), new_entry.id), Updated);
3082                                }
3083                                old_paths.next(&());
3084                                new_paths.next(&());
3085                            }
3086                            Ordering::Greater => {
3087                                changes.insert((new_entry.path.clone(), new_entry.id), Added);
3088                                new_paths.next(&());
3089                            }
3090                        }
3091                    }
3092                    (Some(old_entry), None) => {
3093                        changes.insert((old_entry.path.clone(), old_entry.id), Removed);
3094                        old_paths.next(&());
3095                    }
3096                    (None, Some(new_entry)) => {
3097                        changes.insert((new_entry.path.clone(), new_entry.id), Added);
3098                        new_paths.next(&());
3099                    }
3100                    (None, None) => break,
3101                }
3102            }
3103        }
3104
3105        changes
3106    }
3107
3108    async fn progress_timer(&self, running: bool) {
3109        if !running {
3110            return futures::future::pending().await;
3111        }
3112
3113        #[cfg(any(test, feature = "test-support"))]
3114        if self.fs.is_fake() {
3115            return self.executor.simulate_random_delay().await;
3116        }
3117
3118        smol::Timer::after(Duration::from_millis(100)).await;
3119    }
3120}
3121
3122fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
3123    let mut result = root_char_bag;
3124    result.extend(
3125        path.to_string_lossy()
3126            .chars()
3127            .map(|c| c.to_ascii_lowercase()),
3128    );
3129    result
3130}
3131
3132struct ScanJob {
3133    abs_path: Arc<Path>,
3134    path: Arc<Path>,
3135    ignore_stack: Arc<IgnoreStack>,
3136    scan_queue: Sender<ScanJob>,
3137    ancestor_inodes: TreeSet<u64>,
3138}
3139
3140struct UpdateIgnoreStatusJob {
3141    abs_path: Arc<Path>,
3142    ignore_stack: Arc<IgnoreStack>,
3143    ignore_queue: Sender<UpdateIgnoreStatusJob>,
3144}
3145
3146pub trait WorktreeHandle {
3147    #[cfg(any(test, feature = "test-support"))]
3148    fn flush_fs_events<'a>(
3149        &self,
3150        cx: &'a gpui::TestAppContext,
3151    ) -> futures::future::LocalBoxFuture<'a, ()>;
3152}
3153
3154impl WorktreeHandle for ModelHandle<Worktree> {
3155    // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
3156    // occurred before the worktree was constructed. These events can cause the worktree to perfrom
3157    // extra directory scans, and emit extra scan-state notifications.
3158    //
3159    // This function mutates the worktree's directory and waits for those mutations to be picked up,
3160    // to ensure that all redundant FS events have already been processed.
3161    #[cfg(any(test, feature = "test-support"))]
3162    fn flush_fs_events<'a>(
3163        &self,
3164        cx: &'a gpui::TestAppContext,
3165    ) -> futures::future::LocalBoxFuture<'a, ()> {
3166        use smol::future::FutureExt;
3167
3168        let filename = "fs-event-sentinel";
3169        let tree = self.clone();
3170        let (fs, root_path) = self.read_with(cx, |tree, _| {
3171            let tree = tree.as_local().unwrap();
3172            (tree.fs.clone(), tree.abs_path().clone())
3173        });
3174
3175        async move {
3176            fs.create_file(&root_path.join(filename), Default::default())
3177                .await
3178                .unwrap();
3179            tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_some())
3180                .await;
3181
3182            fs.remove_file(&root_path.join(filename), Default::default())
3183                .await
3184                .unwrap();
3185            tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_none())
3186                .await;
3187
3188            cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3189                .await;
3190        }
3191        .boxed_local()
3192    }
3193}
3194
3195#[derive(Clone, Debug)]
3196struct TraversalProgress<'a> {
3197    max_path: &'a Path,
3198    count: usize,
3199    visible_count: usize,
3200    file_count: usize,
3201    visible_file_count: usize,
3202}
3203
3204impl<'a> TraversalProgress<'a> {
3205    fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
3206        match (include_ignored, include_dirs) {
3207            (true, true) => self.count,
3208            (true, false) => self.file_count,
3209            (false, true) => self.visible_count,
3210            (false, false) => self.visible_file_count,
3211        }
3212    }
3213}
3214
3215impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
3216    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
3217        self.max_path = summary.max_path.as_ref();
3218        self.count += summary.count;
3219        self.visible_count += summary.visible_count;
3220        self.file_count += summary.file_count;
3221        self.visible_file_count += summary.visible_file_count;
3222    }
3223}
3224
3225impl<'a> Default for TraversalProgress<'a> {
3226    fn default() -> Self {
3227        Self {
3228            max_path: Path::new(""),
3229            count: 0,
3230            visible_count: 0,
3231            file_count: 0,
3232            visible_file_count: 0,
3233        }
3234    }
3235}
3236
3237pub struct Traversal<'a> {
3238    cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
3239    include_ignored: bool,
3240    include_dirs: bool,
3241}
3242
3243impl<'a> Traversal<'a> {
3244    pub fn advance(&mut self) -> bool {
3245        self.advance_to_offset(self.offset() + 1)
3246    }
3247
3248    pub fn advance_to_offset(&mut self, offset: usize) -> bool {
3249        self.cursor.seek_forward(
3250            &TraversalTarget::Count {
3251                count: offset,
3252                include_dirs: self.include_dirs,
3253                include_ignored: self.include_ignored,
3254            },
3255            Bias::Right,
3256            &(),
3257        )
3258    }
3259
3260    pub fn advance_to_sibling(&mut self) -> bool {
3261        while let Some(entry) = self.cursor.item() {
3262            self.cursor.seek_forward(
3263                &TraversalTarget::PathSuccessor(&entry.path),
3264                Bias::Left,
3265                &(),
3266            );
3267            if let Some(entry) = self.cursor.item() {
3268                if (self.include_dirs || !entry.is_dir())
3269                    && (self.include_ignored || !entry.is_ignored)
3270                {
3271                    return true;
3272                }
3273            }
3274        }
3275        false
3276    }
3277
3278    pub fn entry(&self) -> Option<&'a Entry> {
3279        self.cursor.item()
3280    }
3281
3282    pub fn offset(&self) -> usize {
3283        self.cursor
3284            .start()
3285            .count(self.include_dirs, self.include_ignored)
3286    }
3287}
3288
3289impl<'a> Iterator for Traversal<'a> {
3290    type Item = &'a Entry;
3291
3292    fn next(&mut self) -> Option<Self::Item> {
3293        if let Some(item) = self.entry() {
3294            self.advance();
3295            Some(item)
3296        } else {
3297            None
3298        }
3299    }
3300}
3301
3302#[derive(Debug)]
3303enum TraversalTarget<'a> {
3304    Path(&'a Path),
3305    PathSuccessor(&'a Path),
3306    Count {
3307        count: usize,
3308        include_ignored: bool,
3309        include_dirs: bool,
3310    },
3311}
3312
3313impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
3314    fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
3315        match self {
3316            TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
3317            TraversalTarget::PathSuccessor(path) => {
3318                if !cursor_location.max_path.starts_with(path) {
3319                    Ordering::Equal
3320                } else {
3321                    Ordering::Greater
3322                }
3323            }
3324            TraversalTarget::Count {
3325                count,
3326                include_dirs,
3327                include_ignored,
3328            } => Ord::cmp(
3329                count,
3330                &cursor_location.count(*include_dirs, *include_ignored),
3331            ),
3332        }
3333    }
3334}
3335
3336struct ChildEntriesIter<'a> {
3337    parent_path: &'a Path,
3338    traversal: Traversal<'a>,
3339}
3340
3341impl<'a> Iterator for ChildEntriesIter<'a> {
3342    type Item = &'a Entry;
3343
3344    fn next(&mut self) -> Option<Self::Item> {
3345        if let Some(item) = self.traversal.entry() {
3346            if item.path.starts_with(&self.parent_path) {
3347                self.traversal.advance_to_sibling();
3348                return Some(item);
3349            }
3350        }
3351        None
3352    }
3353}
3354
3355impl<'a> From<&'a Entry> for proto::Entry {
3356    fn from(entry: &'a Entry) -> Self {
3357        Self {
3358            id: entry.id.to_proto(),
3359            is_dir: entry.is_dir(),
3360            path: entry.path.to_string_lossy().into(),
3361            inode: entry.inode,
3362            mtime: Some(entry.mtime.into()),
3363            is_symlink: entry.is_symlink,
3364            is_ignored: entry.is_ignored,
3365        }
3366    }
3367}
3368
3369impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
3370    type Error = anyhow::Error;
3371
3372    fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
3373        if let Some(mtime) = entry.mtime {
3374            let kind = if entry.is_dir {
3375                EntryKind::Dir
3376            } else {
3377                let mut char_bag = *root_char_bag;
3378                char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase()));
3379                EntryKind::File(char_bag)
3380            };
3381            let path: Arc<Path> = PathBuf::from(entry.path).into();
3382            Ok(Entry {
3383                id: ProjectEntryId::from_proto(entry.id),
3384                kind,
3385                path,
3386                inode: entry.inode,
3387                mtime: mtime.into(),
3388                is_symlink: entry.is_symlink,
3389                is_ignored: entry.is_ignored,
3390            })
3391        } else {
3392            Err(anyhow!(
3393                "missing mtime in remote worktree entry {:?}",
3394                entry.path
3395            ))
3396        }
3397    }
3398}
3399
3400#[cfg(test)]
3401mod tests {
3402    use super::*;
3403    use fs::{FakeFs, RealFs};
3404    use gpui::{executor::Deterministic, TestAppContext};
3405    use pretty_assertions::assert_eq;
3406    use rand::prelude::*;
3407    use serde_json::json;
3408    use std::{env, fmt::Write};
3409    use util::{http::FakeHttpClient, test::temp_tree};
3410
3411    #[gpui::test]
3412    async fn test_traversal(cx: &mut TestAppContext) {
3413        let fs = FakeFs::new(cx.background());
3414        fs.insert_tree(
3415            "/root",
3416            json!({
3417               ".gitignore": "a/b\n",
3418               "a": {
3419                   "b": "",
3420                   "c": "",
3421               }
3422            }),
3423        )
3424        .await;
3425
3426        let http_client = FakeHttpClient::with_404_response();
3427        let client = cx.read(|cx| Client::new(http_client, cx));
3428
3429        let tree = Worktree::local(
3430            client,
3431            Path::new("/root"),
3432            true,
3433            fs,
3434            Default::default(),
3435            &mut cx.to_async(),
3436        )
3437        .await
3438        .unwrap();
3439        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3440            .await;
3441
3442        tree.read_with(cx, |tree, _| {
3443            assert_eq!(
3444                tree.entries(false)
3445                    .map(|entry| entry.path.as_ref())
3446                    .collect::<Vec<_>>(),
3447                vec![
3448                    Path::new(""),
3449                    Path::new(".gitignore"),
3450                    Path::new("a"),
3451                    Path::new("a/c"),
3452                ]
3453            );
3454            assert_eq!(
3455                tree.entries(true)
3456                    .map(|entry| entry.path.as_ref())
3457                    .collect::<Vec<_>>(),
3458                vec![
3459                    Path::new(""),
3460                    Path::new(".gitignore"),
3461                    Path::new("a"),
3462                    Path::new("a/b"),
3463                    Path::new("a/c"),
3464                ]
3465            );
3466        })
3467    }
3468
3469    #[gpui::test(iterations = 10)]
3470    async fn test_circular_symlinks(executor: Arc<Deterministic>, cx: &mut TestAppContext) {
3471        let fs = FakeFs::new(cx.background());
3472        fs.insert_tree(
3473            "/root",
3474            json!({
3475                "lib": {
3476                    "a": {
3477                        "a.txt": ""
3478                    },
3479                    "b": {
3480                        "b.txt": ""
3481                    }
3482                }
3483            }),
3484        )
3485        .await;
3486        fs.insert_symlink("/root/lib/a/lib", "..".into()).await;
3487        fs.insert_symlink("/root/lib/b/lib", "..".into()).await;
3488
3489        let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3490        let tree = Worktree::local(
3491            client,
3492            Path::new("/root"),
3493            true,
3494            fs.clone(),
3495            Default::default(),
3496            &mut cx.to_async(),
3497        )
3498        .await
3499        .unwrap();
3500
3501        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3502            .await;
3503
3504        tree.read_with(cx, |tree, _| {
3505            assert_eq!(
3506                tree.entries(false)
3507                    .map(|entry| entry.path.as_ref())
3508                    .collect::<Vec<_>>(),
3509                vec![
3510                    Path::new(""),
3511                    Path::new("lib"),
3512                    Path::new("lib/a"),
3513                    Path::new("lib/a/a.txt"),
3514                    Path::new("lib/a/lib"),
3515                    Path::new("lib/b"),
3516                    Path::new("lib/b/b.txt"),
3517                    Path::new("lib/b/lib"),
3518                ]
3519            );
3520        });
3521
3522        fs.rename(
3523            Path::new("/root/lib/a/lib"),
3524            Path::new("/root/lib/a/lib-2"),
3525            Default::default(),
3526        )
3527        .await
3528        .unwrap();
3529        executor.run_until_parked();
3530        tree.read_with(cx, |tree, _| {
3531            assert_eq!(
3532                tree.entries(false)
3533                    .map(|entry| entry.path.as_ref())
3534                    .collect::<Vec<_>>(),
3535                vec![
3536                    Path::new(""),
3537                    Path::new("lib"),
3538                    Path::new("lib/a"),
3539                    Path::new("lib/a/a.txt"),
3540                    Path::new("lib/a/lib-2"),
3541                    Path::new("lib/b"),
3542                    Path::new("lib/b/b.txt"),
3543                    Path::new("lib/b/lib"),
3544                ]
3545            );
3546        });
3547    }
3548
3549    #[gpui::test]
3550    async fn test_rescan_with_gitignore(cx: &mut TestAppContext) {
3551        let parent_dir = temp_tree(json!({
3552            ".gitignore": "ancestor-ignored-file1\nancestor-ignored-file2\n",
3553            "tree": {
3554                ".git": {},
3555                ".gitignore": "ignored-dir\n",
3556                "tracked-dir": {
3557                    "tracked-file1": "",
3558                    "ancestor-ignored-file1": "",
3559                },
3560                "ignored-dir": {
3561                    "ignored-file1": ""
3562                }
3563            }
3564        }));
3565        let dir = parent_dir.path().join("tree");
3566
3567        let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3568
3569        let tree = Worktree::local(
3570            client,
3571            dir.as_path(),
3572            true,
3573            Arc::new(RealFs),
3574            Default::default(),
3575            &mut cx.to_async(),
3576        )
3577        .await
3578        .unwrap();
3579        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3580            .await;
3581        tree.flush_fs_events(cx).await;
3582        cx.read(|cx| {
3583            let tree = tree.read(cx);
3584            assert!(
3585                !tree
3586                    .entry_for_path("tracked-dir/tracked-file1")
3587                    .unwrap()
3588                    .is_ignored
3589            );
3590            assert!(
3591                tree.entry_for_path("tracked-dir/ancestor-ignored-file1")
3592                    .unwrap()
3593                    .is_ignored
3594            );
3595            assert!(
3596                tree.entry_for_path("ignored-dir/ignored-file1")
3597                    .unwrap()
3598                    .is_ignored
3599            );
3600        });
3601
3602        std::fs::write(dir.join("tracked-dir/tracked-file2"), "").unwrap();
3603        std::fs::write(dir.join("tracked-dir/ancestor-ignored-file2"), "").unwrap();
3604        std::fs::write(dir.join("ignored-dir/ignored-file2"), "").unwrap();
3605        tree.flush_fs_events(cx).await;
3606        cx.read(|cx| {
3607            let tree = tree.read(cx);
3608            assert!(
3609                !tree
3610                    .entry_for_path("tracked-dir/tracked-file2")
3611                    .unwrap()
3612                    .is_ignored
3613            );
3614            assert!(
3615                tree.entry_for_path("tracked-dir/ancestor-ignored-file2")
3616                    .unwrap()
3617                    .is_ignored
3618            );
3619            assert!(
3620                tree.entry_for_path("ignored-dir/ignored-file2")
3621                    .unwrap()
3622                    .is_ignored
3623            );
3624            assert!(tree.entry_for_path(".git").unwrap().is_ignored);
3625        });
3626    }
3627
3628    #[gpui::test]
3629    async fn test_git_repository_for_path(cx: &mut TestAppContext) {
3630        let root = temp_tree(json!({
3631            "dir1": {
3632                ".git": {},
3633                "deps": {
3634                    "dep1": {
3635                        ".git": {},
3636                        "src": {
3637                            "a.txt": ""
3638                        }
3639                    }
3640                },
3641                "src": {
3642                    "b.txt": ""
3643                }
3644            },
3645            "c.txt": "",
3646        }));
3647
3648        let http_client = FakeHttpClient::with_404_response();
3649        let client = cx.read(|cx| Client::new(http_client, cx));
3650        let tree = Worktree::local(
3651            client,
3652            root.path(),
3653            true,
3654            Arc::new(RealFs),
3655            Default::default(),
3656            &mut cx.to_async(),
3657        )
3658        .await
3659        .unwrap();
3660
3661        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3662            .await;
3663        tree.flush_fs_events(cx).await;
3664
3665        tree.read_with(cx, |tree, _cx| {
3666            let tree = tree.as_local().unwrap();
3667
3668            assert!(tree.repo_for("c.txt".as_ref()).is_none());
3669
3670            let entry = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap();
3671            assert_eq!(
3672                entry
3673                    .work_directory(tree)
3674                    .map(|directory| directory.as_ref().to_owned()),
3675                Some(Path::new("dir1").to_owned())
3676            );
3677
3678            let entry = tree.repo_for("dir1/deps/dep1/src/a.txt".as_ref()).unwrap();
3679            assert_eq!(
3680                entry
3681                    .work_directory(tree)
3682                    .map(|directory| directory.as_ref().to_owned()),
3683                Some(Path::new("dir1/deps/dep1").to_owned())
3684            );
3685        });
3686
3687        let repo_update_events = Arc::new(Mutex::new(vec![]));
3688        tree.update(cx, |_, cx| {
3689            let repo_update_events = repo_update_events.clone();
3690            cx.subscribe(&tree, move |_, _, event, _| {
3691                if let Event::UpdatedGitRepositories(update) = event {
3692                    repo_update_events.lock().push(update.clone());
3693                }
3694            })
3695            .detach();
3696        });
3697
3698        std::fs::write(root.path().join("dir1/.git/random_new_file"), "hello").unwrap();
3699        tree.flush_fs_events(cx).await;
3700
3701        assert_eq!(
3702            repo_update_events.lock()[0]
3703                .keys()
3704                .cloned()
3705                .collect::<Vec<Arc<Path>>>(),
3706            vec![Path::new("dir1").into()]
3707        );
3708
3709        std::fs::remove_dir_all(root.path().join("dir1/.git")).unwrap();
3710        tree.flush_fs_events(cx).await;
3711
3712        tree.read_with(cx, |tree, _cx| {
3713            let tree = tree.as_local().unwrap();
3714
3715            assert!(tree.repo_for("dir1/src/b.txt".as_ref()).is_none());
3716        });
3717    }
3718
3719    #[gpui::test]
3720    async fn test_write_file(cx: &mut TestAppContext) {
3721        let dir = temp_tree(json!({
3722            ".git": {},
3723            ".gitignore": "ignored-dir\n",
3724            "tracked-dir": {},
3725            "ignored-dir": {}
3726        }));
3727
3728        let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3729
3730        let tree = Worktree::local(
3731            client,
3732            dir.path(),
3733            true,
3734            Arc::new(RealFs),
3735            Default::default(),
3736            &mut cx.to_async(),
3737        )
3738        .await
3739        .unwrap();
3740        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3741            .await;
3742        tree.flush_fs_events(cx).await;
3743
3744        tree.update(cx, |tree, cx| {
3745            tree.as_local().unwrap().write_file(
3746                Path::new("tracked-dir/file.txt"),
3747                "hello".into(),
3748                Default::default(),
3749                cx,
3750            )
3751        })
3752        .await
3753        .unwrap();
3754        tree.update(cx, |tree, cx| {
3755            tree.as_local().unwrap().write_file(
3756                Path::new("ignored-dir/file.txt"),
3757                "world".into(),
3758                Default::default(),
3759                cx,
3760            )
3761        })
3762        .await
3763        .unwrap();
3764
3765        tree.read_with(cx, |tree, _| {
3766            let tracked = tree.entry_for_path("tracked-dir/file.txt").unwrap();
3767            let ignored = tree.entry_for_path("ignored-dir/file.txt").unwrap();
3768            assert!(!tracked.is_ignored);
3769            assert!(ignored.is_ignored);
3770        });
3771    }
3772
3773    #[gpui::test(iterations = 30)]
3774    async fn test_create_directory_during_initial_scan(cx: &mut TestAppContext) {
3775        let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3776
3777        let fs = FakeFs::new(cx.background());
3778        fs.insert_tree(
3779            "/root",
3780            json!({
3781                "b": {},
3782                "c": {},
3783                "d": {},
3784            }),
3785        )
3786        .await;
3787
3788        let tree = Worktree::local(
3789            client,
3790            "/root".as_ref(),
3791            true,
3792            fs,
3793            Default::default(),
3794            &mut cx.to_async(),
3795        )
3796        .await
3797        .unwrap();
3798
3799        let mut snapshot1 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
3800
3801        let entry = tree
3802            .update(cx, |tree, cx| {
3803                tree.as_local_mut()
3804                    .unwrap()
3805                    .create_entry("a/e".as_ref(), true, cx)
3806            })
3807            .await
3808            .unwrap();
3809        assert!(entry.is_dir());
3810
3811        cx.foreground().run_until_parked();
3812        tree.read_with(cx, |tree, _| {
3813            assert_eq!(tree.entry_for_path("a/e").unwrap().kind, EntryKind::Dir);
3814        });
3815
3816        let snapshot2 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
3817        let update = snapshot2.build_update(&snapshot1, 0, 0, true);
3818        snapshot1.apply_remote_update(update).unwrap();
3819        assert_eq!(snapshot1.to_vec(true), snapshot2.to_vec(true),);
3820    }
3821
3822    #[gpui::test(iterations = 100)]
3823    async fn test_random_worktree_operations_during_initial_scan(
3824        cx: &mut TestAppContext,
3825        mut rng: StdRng,
3826    ) {
3827        let operations = env::var("OPERATIONS")
3828            .map(|o| o.parse().unwrap())
3829            .unwrap_or(5);
3830        let initial_entries = env::var("INITIAL_ENTRIES")
3831            .map(|o| o.parse().unwrap())
3832            .unwrap_or(20);
3833
3834        let root_dir = Path::new("/test");
3835        let fs = FakeFs::new(cx.background()) as Arc<dyn Fs>;
3836        fs.as_fake().insert_tree(root_dir, json!({})).await;
3837        for _ in 0..initial_entries {
3838            randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
3839        }
3840        log::info!("generated initial tree");
3841
3842        let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3843        let worktree = Worktree::local(
3844            client.clone(),
3845            root_dir,
3846            true,
3847            fs.clone(),
3848            Default::default(),
3849            &mut cx.to_async(),
3850        )
3851        .await
3852        .unwrap();
3853
3854        let mut snapshot = worktree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
3855
3856        for _ in 0..operations {
3857            worktree
3858                .update(cx, |worktree, cx| {
3859                    randomly_mutate_worktree(worktree, &mut rng, cx)
3860                })
3861                .await
3862                .log_err();
3863            worktree.read_with(cx, |tree, _| {
3864                tree.as_local().unwrap().snapshot.check_invariants()
3865            });
3866
3867            if rng.gen_bool(0.6) {
3868                let new_snapshot =
3869                    worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
3870                let update = new_snapshot.build_update(&snapshot, 0, 0, true);
3871                snapshot.apply_remote_update(update.clone()).unwrap();
3872                assert_eq!(
3873                    snapshot.to_vec(true),
3874                    new_snapshot.to_vec(true),
3875                    "incorrect snapshot after update {:?}",
3876                    update
3877                );
3878            }
3879        }
3880
3881        worktree
3882            .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
3883            .await;
3884        worktree.read_with(cx, |tree, _| {
3885            tree.as_local().unwrap().snapshot.check_invariants()
3886        });
3887
3888        let new_snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
3889        let update = new_snapshot.build_update(&snapshot, 0, 0, true);
3890        snapshot.apply_remote_update(update.clone()).unwrap();
3891        assert_eq!(
3892            snapshot.to_vec(true),
3893            new_snapshot.to_vec(true),
3894            "incorrect snapshot after update {:?}",
3895            update
3896        );
3897    }
3898
3899    #[gpui::test(iterations = 100)]
3900    async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng) {
3901        let operations = env::var("OPERATIONS")
3902            .map(|o| o.parse().unwrap())
3903            .unwrap_or(40);
3904        let initial_entries = env::var("INITIAL_ENTRIES")
3905            .map(|o| o.parse().unwrap())
3906            .unwrap_or(20);
3907
3908        let root_dir = Path::new("/test");
3909        let fs = FakeFs::new(cx.background()) as Arc<dyn Fs>;
3910        fs.as_fake().insert_tree(root_dir, json!({})).await;
3911        for _ in 0..initial_entries {
3912            randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
3913        }
3914        log::info!("generated initial tree");
3915
3916        let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3917        let worktree = Worktree::local(
3918            client.clone(),
3919            root_dir,
3920            true,
3921            fs.clone(),
3922            Default::default(),
3923            &mut cx.to_async(),
3924        )
3925        .await
3926        .unwrap();
3927
3928        worktree
3929            .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
3930            .await;
3931
3932        // After the initial scan is complete, the `UpdatedEntries` event can
3933        // be used to follow along with all changes to the worktree's snapshot.
3934        worktree.update(cx, |tree, cx| {
3935            let mut paths = tree
3936                .as_local()
3937                .unwrap()
3938                .paths()
3939                .cloned()
3940                .collect::<Vec<_>>();
3941
3942            cx.subscribe(&worktree, move |tree, _, event, _| {
3943                if let Event::UpdatedEntries(changes) = event {
3944                    for ((path, _), change_type) in changes.iter() {
3945                        let path = path.clone();
3946                        let ix = match paths.binary_search(&path) {
3947                            Ok(ix) | Err(ix) => ix,
3948                        };
3949                        match change_type {
3950                            PathChange::Added => {
3951                                assert_ne!(paths.get(ix), Some(&path));
3952                                paths.insert(ix, path);
3953                            }
3954
3955                            PathChange::Removed => {
3956                                assert_eq!(paths.get(ix), Some(&path));
3957                                paths.remove(ix);
3958                            }
3959
3960                            PathChange::Updated => {
3961                                assert_eq!(paths.get(ix), Some(&path));
3962                            }
3963
3964                            PathChange::AddedOrUpdated => {
3965                                if paths[ix] != path {
3966                                    paths.insert(ix, path);
3967                                }
3968                            }
3969                        }
3970                    }
3971
3972                    let new_paths = tree.paths().cloned().collect::<Vec<_>>();
3973                    assert_eq!(paths, new_paths, "incorrect changes: {:?}", changes);
3974                }
3975            })
3976            .detach();
3977        });
3978
3979        let mut snapshots = Vec::new();
3980        let mut mutations_len = operations;
3981        while mutations_len > 1 {
3982            if rng.gen_bool(0.2) {
3983                worktree
3984                    .update(cx, |worktree, cx| {
3985                        randomly_mutate_worktree(worktree, &mut rng, cx)
3986                    })
3987                    .await
3988                    .unwrap();
3989            } else {
3990                randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
3991            }
3992
3993            let buffered_event_count = fs.as_fake().buffered_event_count().await;
3994            if buffered_event_count > 0 && rng.gen_bool(0.3) {
3995                let len = rng.gen_range(0..=buffered_event_count);
3996                log::info!("flushing {} events", len);
3997                fs.as_fake().flush_events(len).await;
3998            } else {
3999                randomly_mutate_fs(&fs, root_dir, 0.6, &mut rng).await;
4000                mutations_len -= 1;
4001            }
4002
4003            cx.foreground().run_until_parked();
4004            if rng.gen_bool(0.2) {
4005                log::info!("storing snapshot {}", snapshots.len());
4006                let snapshot =
4007                    worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4008                snapshots.push(snapshot);
4009            }
4010        }
4011
4012        log::info!("quiescing");
4013        fs.as_fake().flush_events(usize::MAX).await;
4014        cx.foreground().run_until_parked();
4015        let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4016        snapshot.check_invariants();
4017
4018        {
4019            let new_worktree = Worktree::local(
4020                client.clone(),
4021                root_dir,
4022                true,
4023                fs.clone(),
4024                Default::default(),
4025                &mut cx.to_async(),
4026            )
4027            .await
4028            .unwrap();
4029            new_worktree
4030                .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
4031                .await;
4032            let new_snapshot =
4033                new_worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4034            assert_eq!(snapshot.to_vec(true), new_snapshot.to_vec(true));
4035        }
4036
4037        for (i, mut prev_snapshot) in snapshots.into_iter().enumerate() {
4038            let include_ignored = rng.gen::<bool>();
4039            if !include_ignored {
4040                let mut entries_by_path_edits = Vec::new();
4041                let mut entries_by_id_edits = Vec::new();
4042                for entry in prev_snapshot
4043                    .entries_by_id
4044                    .cursor::<()>()
4045                    .filter(|e| e.is_ignored)
4046                {
4047                    entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
4048                    entries_by_id_edits.push(Edit::Remove(entry.id));
4049                }
4050
4051                prev_snapshot
4052                    .entries_by_path
4053                    .edit(entries_by_path_edits, &());
4054                prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
4055            }
4056
4057            let update = snapshot.build_update(&prev_snapshot, 0, 0, include_ignored);
4058            prev_snapshot.apply_remote_update(update.clone()).unwrap();
4059            assert_eq!(
4060                prev_snapshot.to_vec(include_ignored),
4061                snapshot.to_vec(include_ignored),
4062                "wrong update for snapshot {i}. update: {:?}",
4063                update
4064            );
4065        }
4066    }
4067
4068    fn randomly_mutate_worktree(
4069        worktree: &mut Worktree,
4070        rng: &mut impl Rng,
4071        cx: &mut ModelContext<Worktree>,
4072    ) -> Task<Result<()>> {
4073        let worktree = worktree.as_local_mut().unwrap();
4074        let snapshot = worktree.snapshot();
4075        let entry = snapshot.entries(false).choose(rng).unwrap();
4076
4077        match rng.gen_range(0_u32..100) {
4078            0..=33 if entry.path.as_ref() != Path::new("") => {
4079                log::info!("deleting entry {:?} ({})", entry.path, entry.id.0);
4080                worktree.delete_entry(entry.id, cx).unwrap()
4081            }
4082            ..=66 if entry.path.as_ref() != Path::new("") => {
4083                let other_entry = snapshot.entries(false).choose(rng).unwrap();
4084                let new_parent_path = if other_entry.is_dir() {
4085                    other_entry.path.clone()
4086                } else {
4087                    other_entry.path.parent().unwrap().into()
4088                };
4089                let mut new_path = new_parent_path.join(gen_name(rng));
4090                if new_path.starts_with(&entry.path) {
4091                    new_path = gen_name(rng).into();
4092                }
4093
4094                log::info!(
4095                    "renaming entry {:?} ({}) to {:?}",
4096                    entry.path,
4097                    entry.id.0,
4098                    new_path
4099                );
4100                let task = worktree.rename_entry(entry.id, new_path, cx).unwrap();
4101                cx.foreground().spawn(async move {
4102                    task.await?;
4103                    Ok(())
4104                })
4105            }
4106            _ => {
4107                let task = if entry.is_dir() {
4108                    let child_path = entry.path.join(gen_name(rng));
4109                    let is_dir = rng.gen_bool(0.3);
4110                    log::info!(
4111                        "creating {} at {:?}",
4112                        if is_dir { "dir" } else { "file" },
4113                        child_path,
4114                    );
4115                    worktree.create_entry(child_path, is_dir, cx)
4116                } else {
4117                    log::info!("overwriting file {:?} ({})", entry.path, entry.id.0);
4118                    worktree.write_file(entry.path.clone(), "".into(), Default::default(), cx)
4119                };
4120                cx.foreground().spawn(async move {
4121                    task.await?;
4122                    Ok(())
4123                })
4124            }
4125        }
4126    }
4127
4128    async fn randomly_mutate_fs(
4129        fs: &Arc<dyn Fs>,
4130        root_path: &Path,
4131        insertion_probability: f64,
4132        rng: &mut impl Rng,
4133    ) {
4134        let mut files = Vec::new();
4135        let mut dirs = Vec::new();
4136        for path in fs.as_fake().paths() {
4137            if path.starts_with(root_path) {
4138                if fs.is_file(&path).await {
4139                    files.push(path);
4140                } else {
4141                    dirs.push(path);
4142                }
4143            }
4144        }
4145
4146        if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
4147            let path = dirs.choose(rng).unwrap();
4148            let new_path = path.join(gen_name(rng));
4149
4150            if rng.gen() {
4151                log::info!(
4152                    "creating dir {:?}",
4153                    new_path.strip_prefix(root_path).unwrap()
4154                );
4155                fs.create_dir(&new_path).await.unwrap();
4156            } else {
4157                log::info!(
4158                    "creating file {:?}",
4159                    new_path.strip_prefix(root_path).unwrap()
4160                );
4161                fs.create_file(&new_path, Default::default()).await.unwrap();
4162            }
4163        } else if rng.gen_bool(0.05) {
4164            let ignore_dir_path = dirs.choose(rng).unwrap();
4165            let ignore_path = ignore_dir_path.join(&*GITIGNORE);
4166
4167            let subdirs = dirs
4168                .iter()
4169                .filter(|d| d.starts_with(&ignore_dir_path))
4170                .cloned()
4171                .collect::<Vec<_>>();
4172            let subfiles = files
4173                .iter()
4174                .filter(|d| d.starts_with(&ignore_dir_path))
4175                .cloned()
4176                .collect::<Vec<_>>();
4177            let files_to_ignore = {
4178                let len = rng.gen_range(0..=subfiles.len());
4179                subfiles.choose_multiple(rng, len)
4180            };
4181            let dirs_to_ignore = {
4182                let len = rng.gen_range(0..subdirs.len());
4183                subdirs.choose_multiple(rng, len)
4184            };
4185
4186            let mut ignore_contents = String::new();
4187            for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
4188                writeln!(
4189                    ignore_contents,
4190                    "{}",
4191                    path_to_ignore
4192                        .strip_prefix(&ignore_dir_path)
4193                        .unwrap()
4194                        .to_str()
4195                        .unwrap()
4196                )
4197                .unwrap();
4198            }
4199            log::info!(
4200                "creating gitignore {:?} with contents:\n{}",
4201                ignore_path.strip_prefix(&root_path).unwrap(),
4202                ignore_contents
4203            );
4204            fs.save(
4205                &ignore_path,
4206                &ignore_contents.as_str().into(),
4207                Default::default(),
4208            )
4209            .await
4210            .unwrap();
4211        } else {
4212            let old_path = {
4213                let file_path = files.choose(rng);
4214                let dir_path = dirs[1..].choose(rng);
4215                file_path.into_iter().chain(dir_path).choose(rng).unwrap()
4216            };
4217
4218            let is_rename = rng.gen();
4219            if is_rename {
4220                let new_path_parent = dirs
4221                    .iter()
4222                    .filter(|d| !d.starts_with(old_path))
4223                    .choose(rng)
4224                    .unwrap();
4225
4226                let overwrite_existing_dir =
4227                    !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
4228                let new_path = if overwrite_existing_dir {
4229                    fs.remove_dir(
4230                        &new_path_parent,
4231                        RemoveOptions {
4232                            recursive: true,
4233                            ignore_if_not_exists: true,
4234                        },
4235                    )
4236                    .await
4237                    .unwrap();
4238                    new_path_parent.to_path_buf()
4239                } else {
4240                    new_path_parent.join(gen_name(rng))
4241                };
4242
4243                log::info!(
4244                    "renaming {:?} to {}{:?}",
4245                    old_path.strip_prefix(&root_path).unwrap(),
4246                    if overwrite_existing_dir {
4247                        "overwrite "
4248                    } else {
4249                        ""
4250                    },
4251                    new_path.strip_prefix(&root_path).unwrap()
4252                );
4253                fs.rename(
4254                    &old_path,
4255                    &new_path,
4256                    fs::RenameOptions {
4257                        overwrite: true,
4258                        ignore_if_exists: true,
4259                    },
4260                )
4261                .await
4262                .unwrap();
4263            } else if fs.is_file(&old_path).await {
4264                log::info!(
4265                    "deleting file {:?}",
4266                    old_path.strip_prefix(&root_path).unwrap()
4267                );
4268                fs.remove_file(old_path, Default::default()).await.unwrap();
4269            } else {
4270                log::info!(
4271                    "deleting dir {:?}",
4272                    old_path.strip_prefix(&root_path).unwrap()
4273                );
4274                fs.remove_dir(
4275                    &old_path,
4276                    RemoveOptions {
4277                        recursive: true,
4278                        ignore_if_not_exists: true,
4279                    },
4280                )
4281                .await
4282                .unwrap();
4283            }
4284        }
4285    }
4286
4287    fn gen_name(rng: &mut impl Rng) -> String {
4288        (0..6)
4289            .map(|_| rng.sample(rand::distributions::Alphanumeric))
4290            .map(char::from)
4291            .collect()
4292    }
4293
4294    impl LocalSnapshot {
4295        fn check_invariants(&self) {
4296            assert_eq!(
4297                self.entries_by_path
4298                    .cursor::<()>()
4299                    .map(|e| (&e.path, e.id))
4300                    .collect::<Vec<_>>(),
4301                self.entries_by_id
4302                    .cursor::<()>()
4303                    .map(|e| (&e.path, e.id))
4304                    .collect::<collections::BTreeSet<_>>()
4305                    .into_iter()
4306                    .collect::<Vec<_>>(),
4307                "entries_by_path and entries_by_id are inconsistent"
4308            );
4309
4310            let mut files = self.files(true, 0);
4311            let mut visible_files = self.files(false, 0);
4312            for entry in self.entries_by_path.cursor::<()>() {
4313                if entry.is_file() {
4314                    assert_eq!(files.next().unwrap().inode, entry.inode);
4315                    if !entry.is_ignored {
4316                        assert_eq!(visible_files.next().unwrap().inode, entry.inode);
4317                    }
4318                }
4319            }
4320
4321            assert!(files.next().is_none());
4322            assert!(visible_files.next().is_none());
4323
4324            let mut bfs_paths = Vec::new();
4325            let mut stack = vec![Path::new("")];
4326            while let Some(path) = stack.pop() {
4327                bfs_paths.push(path);
4328                let ix = stack.len();
4329                for child_entry in self.child_entries(path) {
4330                    stack.insert(ix, &child_entry.path);
4331                }
4332            }
4333
4334            let dfs_paths_via_iter = self
4335                .entries_by_path
4336                .cursor::<()>()
4337                .map(|e| e.path.as_ref())
4338                .collect::<Vec<_>>();
4339            assert_eq!(bfs_paths, dfs_paths_via_iter);
4340
4341            let dfs_paths_via_traversal = self
4342                .entries(true)
4343                .map(|e| e.path.as_ref())
4344                .collect::<Vec<_>>();
4345            assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter);
4346
4347            for ignore_parent_abs_path in self.ignores_by_parent_abs_path.keys() {
4348                let ignore_parent_path =
4349                    ignore_parent_abs_path.strip_prefix(&self.abs_path).unwrap();
4350                assert!(self.entry_for_path(&ignore_parent_path).is_some());
4351                assert!(self
4352                    .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
4353                    .is_some());
4354            }
4355        }
4356
4357        fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
4358            let mut paths = Vec::new();
4359            for entry in self.entries_by_path.cursor::<()>() {
4360                if include_ignored || !entry.is_ignored {
4361                    paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
4362                }
4363            }
4364            paths.sort_by(|a, b| a.0.cmp(b.0));
4365            paths
4366        }
4367    }
4368}