worktree.rs

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