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