worktree.rs

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