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