worktree.rs

   1mod ignore;
   2mod worktree_settings;
   3#[cfg(test)]
   4mod worktree_tests;
   5
   6use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
   7use anyhow::{anyhow, Context as _, Result};
   8use clock::ReplicaId;
   9use collections::{HashMap, HashSet, VecDeque};
  10use fs::{copy_recursive, Fs, MTime, PathEvent, RemoveOptions, Watcher};
  11use futures::{
  12    channel::{
  13        mpsc::{self, UnboundedSender},
  14        oneshot,
  15    },
  16    select_biased,
  17    task::Poll,
  18    FutureExt as _, Stream, StreamExt,
  19};
  20use fuzzy::CharBag;
  21use git::GitHostingProviderRegistry;
  22use git::{
  23    repository::{GitFileStatus, GitRepository, RepoPath},
  24    COOKIES, DOT_GIT, FSMONITOR_DAEMON, GITIGNORE,
  25};
  26use gpui::{
  27    AppContext, AsyncAppContext, BackgroundExecutor, Context, EventEmitter, Model, ModelContext,
  28    Task,
  29};
  30use ignore::IgnoreStack;
  31use language::DiskState;
  32
  33use parking_lot::Mutex;
  34use paths::local_settings_folder_relative_path;
  35use postage::{
  36    barrier,
  37    prelude::{Sink as _, Stream as _},
  38    watch,
  39};
  40use rpc::{
  41    proto::{self, split_worktree_update},
  42    AnyProtoClient,
  43};
  44pub use settings::WorktreeId;
  45use settings::{Settings, SettingsLocation, SettingsStore};
  46use smallvec::{smallvec, SmallVec};
  47use smol::channel::{self, Sender};
  48use std::{
  49    any::Any,
  50    cmp::Ordering,
  51    collections::hash_map,
  52    convert::TryFrom,
  53    ffi::OsStr,
  54    fmt,
  55    future::Future,
  56    mem::{self},
  57    ops::{Deref, DerefMut},
  58    path::{Path, PathBuf},
  59    pin::Pin,
  60    sync::{
  61        atomic::{AtomicUsize, Ordering::SeqCst},
  62        Arc,
  63    },
  64    time::{Duration, Instant},
  65};
  66use sum_tree::{
  67    Bias, Cursor, Edit, KeyedItem, SeekTarget, SumTree, Summary, TreeMap, TreeSet, Unit,
  68};
  69use text::{LineEnding, Rope};
  70use util::{
  71    paths::{home_dir, PathMatcher, SanitizedPath},
  72    ResultExt,
  73};
  74pub use worktree_settings::WorktreeSettings;
  75
  76#[cfg(feature = "test-support")]
  77pub const FS_WATCH_LATENCY: Duration = Duration::from_millis(100);
  78#[cfg(not(feature = "test-support"))]
  79pub const FS_WATCH_LATENCY: Duration = Duration::from_millis(100);
  80
  81/// A set of local or remote files that are being opened as part of a project.
  82/// Responsible for tracking related FS (for local)/collab (for remote) events and corresponding updates.
  83/// Stores git repositories data and the diagnostics for the file(s).
  84///
  85/// Has an absolute path, and may be set to be visible in Zed UI or not.
  86/// May correspond to a directory or a single file.
  87/// Possible examples:
  88/// * a drag and dropped file — may be added as an invisible, "ephemeral" entry to the current worktree
  89/// * a directory opened in Zed — may be added as a visible entry to the current worktree
  90///
  91/// Uses [`Entry`] to track the state of each file/directory, can look up absolute paths for entries.
  92pub enum Worktree {
  93    Local(LocalWorktree),
  94    Remote(RemoteWorktree),
  95}
  96
  97/// An entry, created in the worktree.
  98#[derive(Debug)]
  99pub enum CreatedEntry {
 100    /// Got created and indexed by the worktree, receiving a corresponding entry.
 101    Included(Entry),
 102    /// Got created, but not indexed due to falling under exclusion filters.
 103    Excluded { abs_path: PathBuf },
 104}
 105
 106pub struct LoadedFile {
 107    pub file: Arc<File>,
 108    pub text: String,
 109}
 110
 111pub struct LoadedBinaryFile {
 112    pub file: Arc<File>,
 113    pub content: Vec<u8>,
 114}
 115
 116pub struct LocalWorktree {
 117    snapshot: LocalSnapshot,
 118    scan_requests_tx: channel::Sender<ScanRequest>,
 119    path_prefixes_to_scan_tx: channel::Sender<Arc<Path>>,
 120    is_scanning: (watch::Sender<bool>, watch::Receiver<bool>),
 121    _background_scanner_tasks: Vec<Task<()>>,
 122    update_observer: Option<UpdateObservationState>,
 123    fs: Arc<dyn Fs>,
 124    fs_case_sensitive: bool,
 125    visible: bool,
 126    next_entry_id: Arc<AtomicUsize>,
 127    settings: WorktreeSettings,
 128    share_private_files: bool,
 129}
 130
 131struct ScanRequest {
 132    relative_paths: Vec<Arc<Path>>,
 133    done: SmallVec<[barrier::Sender; 1]>,
 134}
 135
 136pub struct RemoteWorktree {
 137    snapshot: Snapshot,
 138    background_snapshot: Arc<Mutex<(Snapshot, Vec<proto::UpdateWorktree>)>>,
 139    project_id: u64,
 140    client: AnyProtoClient,
 141    file_scan_inclusions: PathMatcher,
 142    updates_tx: Option<UnboundedSender<proto::UpdateWorktree>>,
 143    update_observer: Option<mpsc::UnboundedSender<proto::UpdateWorktree>>,
 144    snapshot_subscriptions: VecDeque<(usize, oneshot::Sender<()>)>,
 145    replica_id: ReplicaId,
 146    visible: bool,
 147    disconnected: bool,
 148}
 149
 150#[derive(Clone)]
 151pub struct Snapshot {
 152    id: WorktreeId,
 153    abs_path: SanitizedPath,
 154    root_name: String,
 155    root_char_bag: CharBag,
 156    entries_by_path: SumTree<Entry>,
 157    entries_by_id: SumTree<PathEntry>,
 158    always_included_entries: Vec<Arc<Path>>,
 159    repositories: SumTree<RepositoryEntry>,
 160
 161    /// A number that increases every time the worktree begins scanning
 162    /// a set of paths from the filesystem. This scanning could be caused
 163    /// by some operation performed on the worktree, such as reading or
 164    /// writing a file, or by an event reported by the filesystem.
 165    scan_id: usize,
 166
 167    /// The latest scan id that has completed, and whose preceding scans
 168    /// have all completed. The current `scan_id` could be more than one
 169    /// greater than the `completed_scan_id` if operations are performed
 170    /// on the worktree while it is processing a file-system event.
 171    completed_scan_id: usize,
 172}
 173
 174#[derive(Clone, Debug, PartialEq, Eq)]
 175pub struct RepositoryEntry {
 176    /// The git status entries for this repository.
 177    /// Note that the paths on this repository are relative to the git work directory.
 178    /// If the .git folder is external to Zed, these paths will be relative to that folder,
 179    /// and this data structure might reference files external to this worktree.
 180    ///
 181    /// For example:
 182    ///
 183    ///     my_root_folder/          <-- repository root
 184    ///       .git
 185    ///       my_sub_folder_1/
 186    ///         project_root/        <-- Project root, Zed opened here
 187    ///           changed_file_1     <-- File with changes, in worktree
 188    ///       my_sub_folder_2/
 189    ///         changed_file_2       <-- File with changes, out of worktree
 190    ///           ...
 191    ///
 192    /// With this setup, this field would contain 2 entries, like so:
 193    ///     - my_sub_folder_1/project_root/changed_file_1
 194    ///     - my_sub_folder_2/changed_file_2
 195    pub(crate) statuses_by_path: SumTree<StatusEntry>,
 196    pub(crate) work_directory_id: ProjectEntryId,
 197    pub(crate) work_directory: WorkDirectory,
 198    pub(crate) branch: Option<Arc<str>>,
 199}
 200
 201impl Deref for RepositoryEntry {
 202    type Target = WorkDirectory;
 203
 204    fn deref(&self) -> &Self::Target {
 205        &self.work_directory
 206    }
 207}
 208
 209impl AsRef<Path> for RepositoryEntry {
 210    fn as_ref(&self) -> &Path {
 211        &self.path
 212    }
 213}
 214
 215impl RepositoryEntry {
 216    pub fn branch(&self) -> Option<Arc<str>> {
 217        self.branch.clone()
 218    }
 219
 220    pub fn work_directory_id(&self) -> ProjectEntryId {
 221        self.work_directory_id
 222    }
 223
 224    pub fn status(&self) -> impl Iterator<Item = StatusEntry> + '_ {
 225        self.statuses_by_path.iter().cloned()
 226    }
 227
 228    pub fn initial_update(&self) -> proto::RepositoryEntry {
 229        proto::RepositoryEntry {
 230            work_directory_id: self.work_directory_id.to_proto(),
 231            branch: self.branch.as_ref().map(|branch| branch.to_string()),
 232            updated_statuses: self
 233                .statuses_by_path
 234                .iter()
 235                .map(|entry| proto::StatusEntry {
 236                    repo_path: entry.repo_path.to_string_lossy().to_string(),
 237                    status: git_status_to_proto(entry.status),
 238                })
 239                .collect(),
 240            removed_statuses: Default::default(),
 241        }
 242    }
 243
 244    pub fn build_update(&self, old: &Self) -> proto::RepositoryEntry {
 245        let mut updated_statuses: Vec<proto::StatusEntry> = Vec::new();
 246        let mut removed_statuses: Vec<String> = Vec::new();
 247
 248        let mut new_statuses = self.statuses_by_path.iter().peekable();
 249        let mut old_statuses = old.statuses_by_path.iter().peekable();
 250
 251        let mut current_new_entry = new_statuses.next();
 252        let mut current_old_entry = old_statuses.next();
 253        loop {
 254            match (current_new_entry, current_old_entry) {
 255                (Some(new_entry), Some(old_entry)) => {
 256                    match new_entry.repo_path.cmp(&old_entry.repo_path) {
 257                        Ordering::Less => {
 258                            updated_statuses.push(new_entry.to_proto());
 259                            current_new_entry = new_statuses.next();
 260                        }
 261                        Ordering::Equal => {
 262                            if new_entry.status != old_entry.status {
 263                                updated_statuses.push(new_entry.to_proto());
 264                            }
 265                            current_old_entry = old_statuses.next();
 266                            current_new_entry = new_statuses.next();
 267                        }
 268                        Ordering::Greater => {
 269                            removed_statuses.push(old_entry.repo_path.to_proto());
 270                            current_old_entry = old_statuses.next();
 271                        }
 272                    }
 273                }
 274                (None, Some(old_entry)) => {
 275                    removed_statuses.push(old_entry.repo_path.to_proto());
 276                    current_old_entry = old_statuses.next();
 277                }
 278                (Some(new_entry), None) => {
 279                    updated_statuses.push(new_entry.to_proto());
 280                    current_new_entry = new_statuses.next();
 281                }
 282                (None, None) => break,
 283            }
 284        }
 285
 286        proto::RepositoryEntry {
 287            work_directory_id: self.work_directory_id.to_proto(),
 288            branch: self.branch.as_ref().map(|branch| branch.to_string()),
 289            updated_statuses,
 290            removed_statuses,
 291        }
 292    }
 293}
 294
 295/// This path corresponds to the 'content path' of a repository in relation
 296/// to Zed's project root.
 297/// In the majority of the cases, this is the folder that contains the .git folder.
 298/// But if a sub-folder of a git repository is opened, this corresponds to the
 299/// project root and the .git folder is located in a parent directory.
 300#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
 301pub struct WorkDirectory {
 302    path: Arc<Path>,
 303
 304    /// If location_in_repo is set, it means the .git folder is external
 305    /// and in a parent folder of the project root.
 306    /// In that case, the work_directory field will point to the
 307    /// project-root and location_in_repo contains the location of the
 308    /// project-root in the repository.
 309    ///
 310    /// Example:
 311    ///
 312    ///     my_root_folder/          <-- repository root
 313    ///       .git
 314    ///       my_sub_folder_1/
 315    ///         project_root/        <-- Project root, Zed opened here
 316    ///           ...
 317    ///
 318    /// For this setup, the attributes will have the following values:
 319    ///
 320    ///     work_directory: pointing to "" entry
 321    ///     location_in_repo: Some("my_sub_folder_1/project_root")
 322    pub(crate) location_in_repo: Option<Arc<Path>>,
 323}
 324
 325impl WorkDirectory {
 326    pub fn path_key(&self) -> PathKey {
 327        PathKey(self.path.clone())
 328    }
 329
 330    /// Returns true if the given path is a child of the work directory.
 331    ///
 332    /// Note that the path may not be a member of this repository, if there
 333    /// is a repository in a directory between these two paths
 334    /// external .git folder in a parent folder of the project root.
 335    pub fn directory_contains(&self, path: impl AsRef<Path>) -> bool {
 336        let path = path.as_ref();
 337        path.starts_with(&self.path)
 338    }
 339
 340    /// relativize returns the given project path relative to the root folder of the
 341    /// repository.
 342    /// If the root of the repository (and its .git folder) are located in a parent folder
 343    /// of the project root folder, then the returned RepoPath is relative to the root
 344    /// of the repository and not a valid path inside the project.
 345    pub fn relativize(&self, path: &Path) -> Result<RepoPath> {
 346        if let Some(location_in_repo) = &self.location_in_repo {
 347            Ok(location_in_repo.join(path).into())
 348        } else {
 349            let relativized_path = path
 350                .strip_prefix(&self.path)
 351                .map_err(|_| anyhow!("could not relativize {:?} against {:?}", path, self.path))?;
 352
 353            Ok(relativized_path.into())
 354        }
 355    }
 356
 357    /// This is the opposite operation to `relativize` above
 358    pub fn unrelativize(&self, path: &RepoPath) -> Option<Arc<Path>> {
 359        if let Some(location) = &self.location_in_repo {
 360            // If we fail to strip the prefix, that means this status entry is
 361            // external to this worktree, and we definitely won't have an entry_id
 362            path.strip_prefix(location).ok().map(Into::into)
 363        } else {
 364            Some(self.path.join(path).into())
 365        }
 366    }
 367}
 368
 369impl Default for WorkDirectory {
 370    fn default() -> Self {
 371        Self {
 372            path: Arc::from(Path::new("")),
 373            location_in_repo: None,
 374        }
 375    }
 376}
 377
 378impl Deref for WorkDirectory {
 379    type Target = Path;
 380
 381    fn deref(&self) -> &Self::Target {
 382        self.as_ref()
 383    }
 384}
 385
 386impl AsRef<Path> for WorkDirectory {
 387    fn as_ref(&self) -> &Path {
 388        self.path.as_ref()
 389    }
 390}
 391
 392#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
 393pub struct WorkDirectoryEntry(ProjectEntryId);
 394
 395impl Deref for WorkDirectoryEntry {
 396    type Target = ProjectEntryId;
 397
 398    fn deref(&self) -> &Self::Target {
 399        &self.0
 400    }
 401}
 402
 403impl From<ProjectEntryId> for WorkDirectoryEntry {
 404    fn from(value: ProjectEntryId) -> Self {
 405        WorkDirectoryEntry(value)
 406    }
 407}
 408
 409#[derive(Debug, Clone)]
 410pub struct LocalSnapshot {
 411    snapshot: Snapshot,
 412    /// All of the gitignore files in the worktree, indexed by their relative path.
 413    /// The boolean indicates whether the gitignore needs to be updated.
 414    ignores_by_parent_abs_path: HashMap<Arc<Path>, (Arc<Gitignore>, bool)>,
 415    /// All of the git repositories in the worktree, indexed by the project entry
 416    /// id of their parent directory.
 417    git_repositories: TreeMap<ProjectEntryId, LocalRepositoryEntry>,
 418    /// The file handle of the root dir
 419    /// (so we can find it after it's been moved)
 420    root_file_handle: Option<Arc<dyn fs::FileHandle>>,
 421}
 422
 423struct BackgroundScannerState {
 424    snapshot: LocalSnapshot,
 425    scanned_dirs: HashSet<ProjectEntryId>,
 426    path_prefixes_to_scan: HashSet<Arc<Path>>,
 427    paths_to_scan: HashSet<Arc<Path>>,
 428    /// The ids of all of the entries that were removed from the snapshot
 429    /// as part of the current update. These entry ids may be re-used
 430    /// if the same inode is discovered at a new path, or if the given
 431    /// path is re-created after being deleted.
 432    removed_entries: HashMap<u64, Entry>,
 433    changed_paths: Vec<Arc<Path>>,
 434    prev_snapshot: Snapshot,
 435    git_hosting_provider_registry: Option<Arc<GitHostingProviderRegistry>>,
 436}
 437
 438#[derive(Debug, Clone)]
 439pub struct LocalRepositoryEntry {
 440    pub(crate) work_directory: WorkDirectory,
 441    pub(crate) git_dir_scan_id: usize,
 442    pub(crate) status_scan_id: usize,
 443    pub(crate) repo_ptr: Arc<dyn GitRepository>,
 444    /// Absolute path to the actual .git folder.
 445    /// Note: if .git is a file, this points to the folder indicated by the .git file
 446    pub(crate) dot_git_dir_abs_path: Arc<Path>,
 447    /// Absolute path to the .git file, if we're in a git worktree.
 448    pub(crate) dot_git_worktree_abs_path: Option<Arc<Path>>,
 449}
 450
 451impl sum_tree::Item for LocalRepositoryEntry {
 452    type Summary = PathSummary<Unit>;
 453
 454    fn summary(&self, _: &<Self::Summary as Summary>::Context) -> Self::Summary {
 455        PathSummary {
 456            max_path: self.work_directory.path.clone(),
 457            item_summary: Unit,
 458        }
 459    }
 460}
 461
 462impl KeyedItem for LocalRepositoryEntry {
 463    type Key = PathKey;
 464
 465    fn key(&self) -> Self::Key {
 466        PathKey(self.work_directory.path.clone())
 467    }
 468}
 469
 470impl LocalRepositoryEntry {
 471    pub fn repo(&self) -> &Arc<dyn GitRepository> {
 472        &self.repo_ptr
 473    }
 474}
 475
 476impl Deref for LocalRepositoryEntry {
 477    type Target = WorkDirectory;
 478
 479    fn deref(&self) -> &Self::Target {
 480        &self.work_directory
 481    }
 482}
 483
 484impl Deref for LocalSnapshot {
 485    type Target = Snapshot;
 486
 487    fn deref(&self) -> &Self::Target {
 488        &self.snapshot
 489    }
 490}
 491
 492impl DerefMut for LocalSnapshot {
 493    fn deref_mut(&mut self) -> &mut Self::Target {
 494        &mut self.snapshot
 495    }
 496}
 497
 498enum ScanState {
 499    Started,
 500    Updated {
 501        snapshot: LocalSnapshot,
 502        changes: UpdatedEntriesSet,
 503        barrier: SmallVec<[barrier::Sender; 1]>,
 504        scanning: bool,
 505    },
 506    RootUpdated {
 507        new_path: Option<SanitizedPath>,
 508    },
 509}
 510
 511struct UpdateObservationState {
 512    snapshots_tx:
 513        mpsc::UnboundedSender<(LocalSnapshot, UpdatedEntriesSet, UpdatedGitRepositoriesSet)>,
 514    resume_updates: watch::Sender<()>,
 515    _maintain_remote_snapshot: Task<Option<()>>,
 516}
 517
 518#[derive(Clone)]
 519pub enum Event {
 520    UpdatedEntries(UpdatedEntriesSet),
 521    UpdatedGitRepositories(UpdatedGitRepositoriesSet),
 522    DeletedEntry(ProjectEntryId),
 523}
 524
 525const EMPTY_PATH: &str = "";
 526
 527impl EventEmitter<Event> for Worktree {}
 528
 529impl Worktree {
 530    pub async fn local(
 531        path: impl Into<Arc<Path>>,
 532        visible: bool,
 533        fs: Arc<dyn Fs>,
 534        next_entry_id: Arc<AtomicUsize>,
 535        cx: &mut AsyncAppContext,
 536    ) -> Result<Model<Self>> {
 537        let abs_path = path.into();
 538        let metadata = fs
 539            .metadata(&abs_path)
 540            .await
 541            .context("failed to stat worktree path")?;
 542
 543        let fs_case_sensitive = fs.is_case_sensitive().await.unwrap_or_else(|e| {
 544            log::error!(
 545                "Failed to determine whether filesystem is case sensitive (falling back to true) due to error: {e:#}"
 546            );
 547            true
 548        });
 549
 550        let root_file_handle = fs.open_handle(&abs_path).await.log_err();
 551
 552        cx.new_model(move |cx: &mut ModelContext<Worktree>| {
 553            let mut snapshot = LocalSnapshot {
 554                ignores_by_parent_abs_path: Default::default(),
 555                git_repositories: Default::default(),
 556                snapshot: Snapshot::new(
 557                    cx.entity_id().as_u64(),
 558                    abs_path
 559                        .file_name()
 560                        .map_or(String::new(), |f| f.to_string_lossy().to_string()),
 561                    abs_path.clone(),
 562                ),
 563                root_file_handle,
 564            };
 565
 566            let worktree_id = snapshot.id();
 567            let settings_location = Some(SettingsLocation {
 568                worktree_id,
 569                path: Path::new(EMPTY_PATH),
 570            });
 571
 572            let settings = WorktreeSettings::get(settings_location, cx).clone();
 573            cx.observe_global::<SettingsStore>(move |this, cx| {
 574                if let Self::Local(this) = this {
 575                    let settings = WorktreeSettings::get(settings_location, cx).clone();
 576                    if this.settings != settings {
 577                        this.settings = settings;
 578                        this.restart_background_scanners(cx);
 579                    }
 580                }
 581            })
 582            .detach();
 583
 584            let share_private_files = false;
 585            if let Some(metadata) = metadata {
 586                let mut entry = Entry::new(
 587                    Arc::from(Path::new("")),
 588                    &metadata,
 589                    &next_entry_id,
 590                    snapshot.root_char_bag,
 591                    None,
 592                );
 593                if !metadata.is_dir {
 594                    entry.is_private = !share_private_files
 595                        && settings.is_path_private(abs_path.file_name().unwrap().as_ref());
 596                }
 597                snapshot.insert_entry(entry, fs.as_ref());
 598            }
 599
 600            let (scan_requests_tx, scan_requests_rx) = channel::unbounded();
 601            let (path_prefixes_to_scan_tx, path_prefixes_to_scan_rx) = channel::unbounded();
 602            let mut worktree = LocalWorktree {
 603                share_private_files,
 604                next_entry_id,
 605                snapshot,
 606                is_scanning: watch::channel_with(true),
 607                update_observer: None,
 608                scan_requests_tx,
 609                path_prefixes_to_scan_tx,
 610                _background_scanner_tasks: Vec::new(),
 611                fs,
 612                fs_case_sensitive,
 613                visible,
 614                settings,
 615            };
 616            worktree.start_background_scanner(scan_requests_rx, path_prefixes_to_scan_rx, cx);
 617            Worktree::Local(worktree)
 618        })
 619    }
 620
 621    pub fn remote(
 622        project_id: u64,
 623        replica_id: ReplicaId,
 624        worktree: proto::WorktreeMetadata,
 625        client: AnyProtoClient,
 626        cx: &mut AppContext,
 627    ) -> Model<Self> {
 628        cx.new_model(|cx: &mut ModelContext<Self>| {
 629            let snapshot = Snapshot::new(
 630                worktree.id,
 631                worktree.root_name,
 632                Arc::from(PathBuf::from(worktree.abs_path)),
 633            );
 634
 635            let background_snapshot = Arc::new(Mutex::new((snapshot.clone(), Vec::new())));
 636            let (background_updates_tx, mut background_updates_rx) = mpsc::unbounded();
 637            let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
 638
 639            let worktree_id = snapshot.id();
 640            let settings_location = Some(SettingsLocation {
 641                worktree_id,
 642                path: Path::new(EMPTY_PATH),
 643            });
 644
 645            let settings = WorktreeSettings::get(settings_location, cx).clone();
 646            let worktree = RemoteWorktree {
 647                client,
 648                project_id,
 649                replica_id,
 650                snapshot,
 651                file_scan_inclusions: settings.file_scan_inclusions.clone(),
 652                background_snapshot: background_snapshot.clone(),
 653                updates_tx: Some(background_updates_tx),
 654                update_observer: None,
 655                snapshot_subscriptions: Default::default(),
 656                visible: worktree.visible,
 657                disconnected: false,
 658            };
 659
 660            // Apply updates to a separate snapshot in a background task, then
 661            // send them to a foreground task which updates the model.
 662            cx.background_executor()
 663                .spawn(async move {
 664                    while let Some(update) = background_updates_rx.next().await {
 665                        {
 666                            let mut lock = background_snapshot.lock();
 667                            if let Err(error) = lock
 668                                .0
 669                                .apply_remote_update(update.clone(), &settings.file_scan_inclusions)
 670                            {
 671                                log::error!("error applying worktree update: {}", error);
 672                            }
 673                            lock.1.push(update);
 674                        }
 675                        snapshot_updated_tx.send(()).await.ok();
 676                    }
 677                })
 678                .detach();
 679
 680            // On the foreground task, update to the latest snapshot and notify
 681            // any update observer of all updates that led to that snapshot.
 682            cx.spawn(|this, mut cx| async move {
 683                while (snapshot_updated_rx.recv().await).is_some() {
 684                    this.update(&mut cx, |this, cx| {
 685                        let this = this.as_remote_mut().unwrap();
 686                        {
 687                            let mut lock = this.background_snapshot.lock();
 688                            this.snapshot = lock.0.clone();
 689                            if let Some(tx) = &this.update_observer {
 690                                for update in lock.1.drain(..) {
 691                                    tx.unbounded_send(update).ok();
 692                                }
 693                            }
 694                        };
 695                        cx.emit(Event::UpdatedEntries(Arc::default()));
 696                        cx.notify();
 697                        while let Some((scan_id, _)) = this.snapshot_subscriptions.front() {
 698                            if this.observed_snapshot(*scan_id) {
 699                                let (_, tx) = this.snapshot_subscriptions.pop_front().unwrap();
 700                                let _ = tx.send(());
 701                            } else {
 702                                break;
 703                            }
 704                        }
 705                    })?;
 706                }
 707                anyhow::Ok(())
 708            })
 709            .detach();
 710
 711            Worktree::Remote(worktree)
 712        })
 713    }
 714
 715    pub fn as_local(&self) -> Option<&LocalWorktree> {
 716        if let Worktree::Local(worktree) = self {
 717            Some(worktree)
 718        } else {
 719            None
 720        }
 721    }
 722
 723    pub fn as_remote(&self) -> Option<&RemoteWorktree> {
 724        if let Worktree::Remote(worktree) = self {
 725            Some(worktree)
 726        } else {
 727            None
 728        }
 729    }
 730
 731    pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
 732        if let Worktree::Local(worktree) = self {
 733            Some(worktree)
 734        } else {
 735            None
 736        }
 737    }
 738
 739    pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
 740        if let Worktree::Remote(worktree) = self {
 741            Some(worktree)
 742        } else {
 743            None
 744        }
 745    }
 746
 747    pub fn is_local(&self) -> bool {
 748        matches!(self, Worktree::Local(_))
 749    }
 750
 751    pub fn is_remote(&self) -> bool {
 752        !self.is_local()
 753    }
 754
 755    pub fn settings_location(&self, _: &ModelContext<Self>) -> SettingsLocation<'static> {
 756        SettingsLocation {
 757            worktree_id: self.id(),
 758            path: Path::new(EMPTY_PATH),
 759        }
 760    }
 761
 762    pub fn snapshot(&self) -> Snapshot {
 763        match self {
 764            Worktree::Local(worktree) => worktree.snapshot.snapshot.clone(),
 765            Worktree::Remote(worktree) => worktree.snapshot.clone(),
 766        }
 767    }
 768
 769    pub fn scan_id(&self) -> usize {
 770        match self {
 771            Worktree::Local(worktree) => worktree.snapshot.scan_id,
 772            Worktree::Remote(worktree) => worktree.snapshot.scan_id,
 773        }
 774    }
 775
 776    pub fn metadata_proto(&self) -> proto::WorktreeMetadata {
 777        proto::WorktreeMetadata {
 778            id: self.id().to_proto(),
 779            root_name: self.root_name().to_string(),
 780            visible: self.is_visible(),
 781            abs_path: self.abs_path().as_os_str().to_string_lossy().into(),
 782        }
 783    }
 784
 785    pub fn completed_scan_id(&self) -> usize {
 786        match self {
 787            Worktree::Local(worktree) => worktree.snapshot.completed_scan_id,
 788            Worktree::Remote(worktree) => worktree.snapshot.completed_scan_id,
 789        }
 790    }
 791
 792    pub fn is_visible(&self) -> bool {
 793        match self {
 794            Worktree::Local(worktree) => worktree.visible,
 795            Worktree::Remote(worktree) => worktree.visible,
 796        }
 797    }
 798
 799    pub fn replica_id(&self) -> ReplicaId {
 800        match self {
 801            Worktree::Local(_) => 0,
 802            Worktree::Remote(worktree) => worktree.replica_id,
 803        }
 804    }
 805
 806    pub fn abs_path(&self) -> Arc<Path> {
 807        match self {
 808            Worktree::Local(worktree) => worktree.abs_path.clone().into(),
 809            Worktree::Remote(worktree) => worktree.abs_path.clone().into(),
 810        }
 811    }
 812
 813    pub fn root_file(&self, cx: &ModelContext<Self>) -> Option<Arc<File>> {
 814        let entry = self.root_entry()?;
 815        Some(File::for_entry(entry.clone(), cx.handle()))
 816    }
 817
 818    pub fn observe_updates<F, Fut>(
 819        &mut self,
 820        project_id: u64,
 821        cx: &ModelContext<Worktree>,
 822        callback: F,
 823    ) where
 824        F: 'static + Send + Fn(proto::UpdateWorktree) -> Fut,
 825        Fut: 'static + Send + Future<Output = bool>,
 826    {
 827        match self {
 828            Worktree::Local(this) => this.observe_updates(project_id, cx, callback),
 829            Worktree::Remote(this) => this.observe_updates(project_id, cx, callback),
 830        }
 831    }
 832
 833    pub fn stop_observing_updates(&mut self) {
 834        match self {
 835            Worktree::Local(this) => {
 836                this.update_observer.take();
 837            }
 838            Worktree::Remote(this) => {
 839                this.update_observer.take();
 840            }
 841        }
 842    }
 843
 844    #[cfg(any(test, feature = "test-support"))]
 845    pub fn has_update_observer(&self) -> bool {
 846        match self {
 847            Worktree::Local(this) => this.update_observer.is_some(),
 848            Worktree::Remote(this) => this.update_observer.is_some(),
 849        }
 850    }
 851
 852    pub fn load_file(&self, path: &Path, cx: &ModelContext<Worktree>) -> Task<Result<LoadedFile>> {
 853        match self {
 854            Worktree::Local(this) => this.load_file(path, cx),
 855            Worktree::Remote(_) => {
 856                Task::ready(Err(anyhow!("remote worktrees can't yet load files")))
 857            }
 858        }
 859    }
 860
 861    pub fn load_staged_file(&self, path: &Path, cx: &AppContext) -> Task<Result<Option<String>>> {
 862        match self {
 863            Worktree::Local(this) => {
 864                let path = Arc::from(path);
 865                let snapshot = this.snapshot();
 866                cx.background_executor().spawn(async move {
 867                    if let Some(repo) = snapshot.repository_for_path(&path) {
 868                        if let Some(repo_path) = repo.relativize(&path).log_err() {
 869                            if let Some(git_repo) =
 870                                snapshot.git_repositories.get(&repo.work_directory_id)
 871                            {
 872                                return Ok(git_repo.repo_ptr.load_index_text(&repo_path));
 873                            }
 874                        }
 875                    }
 876                    Ok(None)
 877                })
 878            }
 879            Worktree::Remote(_) => {
 880                Task::ready(Err(anyhow!("remote worktrees can't yet load staged files")))
 881            }
 882        }
 883    }
 884
 885    pub fn load_binary_file(
 886        &self,
 887        path: &Path,
 888        cx: &ModelContext<Worktree>,
 889    ) -> Task<Result<LoadedBinaryFile>> {
 890        match self {
 891            Worktree::Local(this) => this.load_binary_file(path, cx),
 892            Worktree::Remote(_) => {
 893                Task::ready(Err(anyhow!("remote worktrees can't yet load binary files")))
 894            }
 895        }
 896    }
 897
 898    pub fn write_file(
 899        &self,
 900        path: &Path,
 901        text: Rope,
 902        line_ending: LineEnding,
 903        cx: &ModelContext<Worktree>,
 904    ) -> Task<Result<Arc<File>>> {
 905        match self {
 906            Worktree::Local(this) => this.write_file(path, text, line_ending, cx),
 907            Worktree::Remote(_) => {
 908                Task::ready(Err(anyhow!("remote worktree can't yet write files")))
 909            }
 910        }
 911    }
 912
 913    pub fn create_entry(
 914        &mut self,
 915        path: impl Into<Arc<Path>>,
 916        is_directory: bool,
 917        cx: &ModelContext<Worktree>,
 918    ) -> Task<Result<CreatedEntry>> {
 919        let path = path.into();
 920        let worktree_id = self.id();
 921        match self {
 922            Worktree::Local(this) => this.create_entry(path, is_directory, cx),
 923            Worktree::Remote(this) => {
 924                let project_id = this.project_id;
 925                let request = this.client.request(proto::CreateProjectEntry {
 926                    worktree_id: worktree_id.to_proto(),
 927                    project_id,
 928                    path: path.to_string_lossy().into(),
 929                    is_directory,
 930                });
 931                cx.spawn(move |this, mut cx| async move {
 932                    let response = request.await?;
 933                    match response.entry {
 934                        Some(entry) => this
 935                            .update(&mut cx, |worktree, cx| {
 936                                worktree.as_remote_mut().unwrap().insert_entry(
 937                                    entry,
 938                                    response.worktree_scan_id as usize,
 939                                    cx,
 940                                )
 941                            })?
 942                            .await
 943                            .map(CreatedEntry::Included),
 944                        None => {
 945                            let abs_path = this.update(&mut cx, |worktree, _| {
 946                                worktree
 947                                    .absolutize(&path)
 948                                    .with_context(|| format!("absolutizing {path:?}"))
 949                            })??;
 950                            Ok(CreatedEntry::Excluded { abs_path })
 951                        }
 952                    }
 953                })
 954            }
 955        }
 956    }
 957
 958    pub fn delete_entry(
 959        &mut self,
 960        entry_id: ProjectEntryId,
 961        trash: bool,
 962        cx: &mut ModelContext<Worktree>,
 963    ) -> Option<Task<Result<()>>> {
 964        let task = match self {
 965            Worktree::Local(this) => this.delete_entry(entry_id, trash, cx),
 966            Worktree::Remote(this) => this.delete_entry(entry_id, trash, cx),
 967        }?;
 968
 969        let entry = match self {
 970            Worktree::Local(ref this) => this.entry_for_id(entry_id),
 971            Worktree::Remote(ref this) => this.entry_for_id(entry_id),
 972        }?;
 973
 974        let mut ids = vec![entry_id];
 975        let path = &*entry.path;
 976
 977        self.get_children_ids_recursive(path, &mut ids);
 978
 979        for id in ids {
 980            cx.emit(Event::DeletedEntry(id));
 981        }
 982        Some(task)
 983    }
 984
 985    fn get_children_ids_recursive(&self, path: &Path, ids: &mut Vec<ProjectEntryId>) {
 986        let children_iter = self.child_entries(path);
 987        for child in children_iter {
 988            ids.push(child.id);
 989            self.get_children_ids_recursive(&child.path, ids);
 990        }
 991    }
 992
 993    pub fn rename_entry(
 994        &mut self,
 995        entry_id: ProjectEntryId,
 996        new_path: impl Into<Arc<Path>>,
 997        cx: &ModelContext<Self>,
 998    ) -> Task<Result<CreatedEntry>> {
 999        let new_path = new_path.into();
1000        match self {
1001            Worktree::Local(this) => this.rename_entry(entry_id, new_path, cx),
1002            Worktree::Remote(this) => this.rename_entry(entry_id, new_path, cx),
1003        }
1004    }
1005
1006    pub fn copy_entry(
1007        &mut self,
1008        entry_id: ProjectEntryId,
1009        relative_worktree_source_path: Option<PathBuf>,
1010        new_path: impl Into<Arc<Path>>,
1011        cx: &ModelContext<Self>,
1012    ) -> Task<Result<Option<Entry>>> {
1013        let new_path = new_path.into();
1014        match self {
1015            Worktree::Local(this) => {
1016                this.copy_entry(entry_id, relative_worktree_source_path, new_path, cx)
1017            }
1018            Worktree::Remote(this) => {
1019                let relative_worktree_source_path =
1020                    relative_worktree_source_path.map(|relative_worktree_source_path| {
1021                        relative_worktree_source_path.to_string_lossy().into()
1022                    });
1023                let response = this.client.request(proto::CopyProjectEntry {
1024                    project_id: this.project_id,
1025                    entry_id: entry_id.to_proto(),
1026                    relative_worktree_source_path,
1027                    new_path: new_path.to_string_lossy().into(),
1028                });
1029                cx.spawn(move |this, mut cx| async move {
1030                    let response = response.await?;
1031                    match response.entry {
1032                        Some(entry) => this
1033                            .update(&mut cx, |worktree, cx| {
1034                                worktree.as_remote_mut().unwrap().insert_entry(
1035                                    entry,
1036                                    response.worktree_scan_id as usize,
1037                                    cx,
1038                                )
1039                            })?
1040                            .await
1041                            .map(Some),
1042                        None => Ok(None),
1043                    }
1044                })
1045            }
1046        }
1047    }
1048
1049    pub fn copy_external_entries(
1050        &mut self,
1051        target_directory: PathBuf,
1052        paths: Vec<Arc<Path>>,
1053        overwrite_existing_files: bool,
1054        cx: &ModelContext<Worktree>,
1055    ) -> Task<Result<Vec<ProjectEntryId>>> {
1056        match self {
1057            Worktree::Local(this) => {
1058                this.copy_external_entries(target_directory, paths, overwrite_existing_files, cx)
1059            }
1060            _ => Task::ready(Err(anyhow!(
1061                "Copying external entries is not supported for remote worktrees"
1062            ))),
1063        }
1064    }
1065
1066    pub fn expand_entry(
1067        &mut self,
1068        entry_id: ProjectEntryId,
1069        cx: &ModelContext<Worktree>,
1070    ) -> Option<Task<Result<()>>> {
1071        match self {
1072            Worktree::Local(this) => this.expand_entry(entry_id, cx),
1073            Worktree::Remote(this) => {
1074                let response = this.client.request(proto::ExpandProjectEntry {
1075                    project_id: this.project_id,
1076                    entry_id: entry_id.to_proto(),
1077                });
1078                Some(cx.spawn(move |this, mut cx| async move {
1079                    let response = response.await?;
1080                    this.update(&mut cx, |this, _| {
1081                        this.as_remote_mut()
1082                            .unwrap()
1083                            .wait_for_snapshot(response.worktree_scan_id as usize)
1084                    })?
1085                    .await?;
1086                    Ok(())
1087                }))
1088            }
1089        }
1090    }
1091
1092    pub async fn handle_create_entry(
1093        this: Model<Self>,
1094        request: proto::CreateProjectEntry,
1095        mut cx: AsyncAppContext,
1096    ) -> Result<proto::ProjectEntryResponse> {
1097        let (scan_id, entry) = this.update(&mut cx, |this, cx| {
1098            (
1099                this.scan_id(),
1100                this.create_entry(PathBuf::from(request.path), request.is_directory, cx),
1101            )
1102        })?;
1103        Ok(proto::ProjectEntryResponse {
1104            entry: match &entry.await? {
1105                CreatedEntry::Included(entry) => Some(entry.into()),
1106                CreatedEntry::Excluded { .. } => None,
1107            },
1108            worktree_scan_id: scan_id as u64,
1109        })
1110    }
1111
1112    pub async fn handle_delete_entry(
1113        this: Model<Self>,
1114        request: proto::DeleteProjectEntry,
1115        mut cx: AsyncAppContext,
1116    ) -> Result<proto::ProjectEntryResponse> {
1117        let (scan_id, task) = this.update(&mut cx, |this, cx| {
1118            (
1119                this.scan_id(),
1120                this.delete_entry(
1121                    ProjectEntryId::from_proto(request.entry_id),
1122                    request.use_trash,
1123                    cx,
1124                ),
1125            )
1126        })?;
1127        task.ok_or_else(|| anyhow!("invalid entry"))?.await?;
1128        Ok(proto::ProjectEntryResponse {
1129            entry: None,
1130            worktree_scan_id: scan_id as u64,
1131        })
1132    }
1133
1134    pub async fn handle_expand_entry(
1135        this: Model<Self>,
1136        request: proto::ExpandProjectEntry,
1137        mut cx: AsyncAppContext,
1138    ) -> Result<proto::ExpandProjectEntryResponse> {
1139        let task = this.update(&mut cx, |this, cx| {
1140            this.expand_entry(ProjectEntryId::from_proto(request.entry_id), cx)
1141        })?;
1142        task.ok_or_else(|| anyhow!("no such entry"))?.await?;
1143        let scan_id = this.read_with(&cx, |this, _| this.scan_id())?;
1144        Ok(proto::ExpandProjectEntryResponse {
1145            worktree_scan_id: scan_id as u64,
1146        })
1147    }
1148
1149    pub async fn handle_rename_entry(
1150        this: Model<Self>,
1151        request: proto::RenameProjectEntry,
1152        mut cx: AsyncAppContext,
1153    ) -> Result<proto::ProjectEntryResponse> {
1154        let (scan_id, task) = this.update(&mut cx, |this, cx| {
1155            (
1156                this.scan_id(),
1157                this.rename_entry(
1158                    ProjectEntryId::from_proto(request.entry_id),
1159                    PathBuf::from(request.new_path),
1160                    cx,
1161                ),
1162            )
1163        })?;
1164        Ok(proto::ProjectEntryResponse {
1165            entry: match &task.await? {
1166                CreatedEntry::Included(entry) => Some(entry.into()),
1167                CreatedEntry::Excluded { .. } => None,
1168            },
1169            worktree_scan_id: scan_id as u64,
1170        })
1171    }
1172
1173    pub async fn handle_copy_entry(
1174        this: Model<Self>,
1175        request: proto::CopyProjectEntry,
1176        mut cx: AsyncAppContext,
1177    ) -> Result<proto::ProjectEntryResponse> {
1178        let (scan_id, task) = this.update(&mut cx, |this, cx| {
1179            let relative_worktree_source_path =
1180                request.relative_worktree_source_path.map(PathBuf::from);
1181            (
1182                this.scan_id(),
1183                this.copy_entry(
1184                    ProjectEntryId::from_proto(request.entry_id),
1185                    relative_worktree_source_path,
1186                    PathBuf::from(request.new_path),
1187                    cx,
1188                ),
1189            )
1190        })?;
1191        Ok(proto::ProjectEntryResponse {
1192            entry: task.await?.as_ref().map(|e| e.into()),
1193            worktree_scan_id: scan_id as u64,
1194        })
1195    }
1196}
1197
1198impl LocalWorktree {
1199    pub fn fs(&self) -> &Arc<dyn Fs> {
1200        &self.fs
1201    }
1202
1203    pub fn contains_abs_path(&self, path: &Path) -> bool {
1204        let path = SanitizedPath::from(path);
1205        path.starts_with(&self.abs_path)
1206    }
1207
1208    pub fn is_path_private(&self, path: &Path) -> bool {
1209        !self.share_private_files && self.settings.is_path_private(path)
1210    }
1211
1212    fn restart_background_scanners(&mut self, cx: &ModelContext<Worktree>) {
1213        let (scan_requests_tx, scan_requests_rx) = channel::unbounded();
1214        let (path_prefixes_to_scan_tx, path_prefixes_to_scan_rx) = channel::unbounded();
1215        self.scan_requests_tx = scan_requests_tx;
1216        self.path_prefixes_to_scan_tx = path_prefixes_to_scan_tx;
1217
1218        self.start_background_scanner(scan_requests_rx, path_prefixes_to_scan_rx, cx);
1219        let always_included_entries = mem::take(&mut self.snapshot.always_included_entries);
1220        log::debug!(
1221            "refreshing entries for the following always included paths: {:?}",
1222            always_included_entries
1223        );
1224
1225        // Cleans up old always included entries to ensure they get updated properly. Otherwise,
1226        // nested always included entries may not get updated and will result in out-of-date info.
1227        self.refresh_entries_for_paths(always_included_entries);
1228    }
1229
1230    fn start_background_scanner(
1231        &mut self,
1232        scan_requests_rx: channel::Receiver<ScanRequest>,
1233        path_prefixes_to_scan_rx: channel::Receiver<Arc<Path>>,
1234        cx: &ModelContext<Worktree>,
1235    ) {
1236        let snapshot = self.snapshot();
1237        let share_private_files = self.share_private_files;
1238        let next_entry_id = self.next_entry_id.clone();
1239        let fs = self.fs.clone();
1240        let git_hosting_provider_registry = GitHostingProviderRegistry::try_global(cx);
1241        let settings = self.settings.clone();
1242        let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
1243        let background_scanner = cx.background_executor().spawn({
1244            let abs_path = &snapshot.abs_path;
1245            #[cfg(target_os = "windows")]
1246            let abs_path = abs_path
1247                .as_path()
1248                .canonicalize()
1249                .unwrap_or_else(|_| abs_path.as_path().to_path_buf());
1250            #[cfg(not(target_os = "windows"))]
1251            let abs_path = abs_path.as_path().to_path_buf();
1252            let background = cx.background_executor().clone();
1253            async move {
1254                let (events, watcher) = fs.watch(&abs_path, FS_WATCH_LATENCY).await;
1255                let fs_case_sensitive = fs.is_case_sensitive().await.unwrap_or_else(|e| {
1256                    log::error!("Failed to determine whether filesystem is case sensitive: {e:#}");
1257                    true
1258                });
1259
1260                let mut scanner = BackgroundScanner {
1261                    fs,
1262                    fs_case_sensitive,
1263                    status_updates_tx: scan_states_tx,
1264                    executor: background,
1265                    scan_requests_rx,
1266                    path_prefixes_to_scan_rx,
1267                    next_entry_id,
1268                    state: Mutex::new(BackgroundScannerState {
1269                        prev_snapshot: snapshot.snapshot.clone(),
1270                        snapshot,
1271                        scanned_dirs: Default::default(),
1272                        path_prefixes_to_scan: Default::default(),
1273                        paths_to_scan: Default::default(),
1274                        removed_entries: Default::default(),
1275                        changed_paths: Default::default(),
1276                        git_hosting_provider_registry,
1277                    }),
1278                    phase: BackgroundScannerPhase::InitialScan,
1279                    share_private_files,
1280                    settings,
1281                    watcher,
1282                };
1283
1284                scanner
1285                    .run(Box::pin(
1286                        events.map(|events| events.into_iter().map(Into::into).collect()),
1287                    ))
1288                    .await;
1289            }
1290        });
1291        let scan_state_updater = cx.spawn(|this, mut cx| async move {
1292            while let Some((state, this)) = scan_states_rx.next().await.zip(this.upgrade()) {
1293                this.update(&mut cx, |this, cx| {
1294                    let this = this.as_local_mut().unwrap();
1295                    match state {
1296                        ScanState::Started => {
1297                            *this.is_scanning.0.borrow_mut() = true;
1298                        }
1299                        ScanState::Updated {
1300                            snapshot,
1301                            changes,
1302                            barrier,
1303                            scanning,
1304                        } => {
1305                            *this.is_scanning.0.borrow_mut() = scanning;
1306                            this.set_snapshot(snapshot, changes, cx);
1307                            drop(barrier);
1308                        }
1309                        ScanState::RootUpdated { new_path } => {
1310                            if let Some(new_path) = new_path {
1311                                this.snapshot.git_repositories = Default::default();
1312                                this.snapshot.ignores_by_parent_abs_path = Default::default();
1313                                let root_name = new_path
1314                                    .as_path()
1315                                    .file_name()
1316                                    .map_or(String::new(), |f| f.to_string_lossy().to_string());
1317                                this.snapshot.update_abs_path(new_path, root_name);
1318                            }
1319                            this.restart_background_scanners(cx);
1320                        }
1321                    }
1322                    cx.notify();
1323                })
1324                .ok();
1325            }
1326        });
1327        self._background_scanner_tasks = vec![background_scanner, scan_state_updater];
1328        self.is_scanning = watch::channel_with(true);
1329    }
1330
1331    fn set_snapshot(
1332        &mut self,
1333        new_snapshot: LocalSnapshot,
1334        entry_changes: UpdatedEntriesSet,
1335        cx: &mut ModelContext<Worktree>,
1336    ) {
1337        let repo_changes = self.changed_repos(&self.snapshot, &new_snapshot);
1338        self.snapshot = new_snapshot;
1339
1340        if let Some(share) = self.update_observer.as_mut() {
1341            share
1342                .snapshots_tx
1343                .unbounded_send((
1344                    self.snapshot.clone(),
1345                    entry_changes.clone(),
1346                    repo_changes.clone(),
1347                ))
1348                .ok();
1349        }
1350
1351        if !entry_changes.is_empty() {
1352            cx.emit(Event::UpdatedEntries(entry_changes));
1353        }
1354        if !repo_changes.is_empty() {
1355            cx.emit(Event::UpdatedGitRepositories(repo_changes));
1356        }
1357    }
1358
1359    fn changed_repos(
1360        &self,
1361        old_snapshot: &LocalSnapshot,
1362        new_snapshot: &LocalSnapshot,
1363    ) -> UpdatedGitRepositoriesSet {
1364        let mut changes = Vec::new();
1365        let mut old_repos = old_snapshot.git_repositories.iter().peekable();
1366        let mut new_repos = new_snapshot.git_repositories.iter().peekable();
1367
1368        loop {
1369            match (new_repos.peek().map(clone), old_repos.peek().map(clone)) {
1370                (Some((new_entry_id, new_repo)), Some((old_entry_id, old_repo))) => {
1371                    match Ord::cmp(&new_entry_id, &old_entry_id) {
1372                        Ordering::Less => {
1373                            if let Some(entry) = new_snapshot.entry_for_id(new_entry_id) {
1374                                changes.push((
1375                                    entry.path.clone(),
1376                                    GitRepositoryChange {
1377                                        old_repository: None,
1378                                    },
1379                                ));
1380                            }
1381                            new_repos.next();
1382                        }
1383                        Ordering::Equal => {
1384                            if new_repo.git_dir_scan_id != old_repo.git_dir_scan_id
1385                                || new_repo.status_scan_id != old_repo.status_scan_id
1386                            {
1387                                if let Some(entry) = new_snapshot.entry_for_id(new_entry_id) {
1388                                    let old_repo = old_snapshot
1389                                        .repositories
1390                                        .get(&PathKey(entry.path.clone()), &())
1391                                        .cloned();
1392                                    changes.push((
1393                                        entry.path.clone(),
1394                                        GitRepositoryChange {
1395                                            old_repository: old_repo,
1396                                        },
1397                                    ));
1398                                }
1399                            }
1400                            new_repos.next();
1401                            old_repos.next();
1402                        }
1403                        Ordering::Greater => {
1404                            if let Some(entry) = old_snapshot.entry_for_id(old_entry_id) {
1405                                let old_repo = old_snapshot
1406                                    .repositories
1407                                    .get(&PathKey(entry.path.clone()), &())
1408                                    .cloned();
1409                                changes.push((
1410                                    entry.path.clone(),
1411                                    GitRepositoryChange {
1412                                        old_repository: old_repo,
1413                                    },
1414                                ));
1415                            }
1416                            old_repos.next();
1417                        }
1418                    }
1419                }
1420                (Some((entry_id, _)), None) => {
1421                    if let Some(entry) = new_snapshot.entry_for_id(entry_id) {
1422                        changes.push((
1423                            entry.path.clone(),
1424                            GitRepositoryChange {
1425                                old_repository: None,
1426                            },
1427                        ));
1428                    }
1429                    new_repos.next();
1430                }
1431                (None, Some((entry_id, _))) => {
1432                    if let Some(entry) = old_snapshot.entry_for_id(entry_id) {
1433                        let old_repo = old_snapshot
1434                            .repositories
1435                            .get(&PathKey(entry.path.clone()), &())
1436                            .cloned();
1437                        changes.push((
1438                            entry.path.clone(),
1439                            GitRepositoryChange {
1440                                old_repository: old_repo,
1441                            },
1442                        ));
1443                    }
1444                    old_repos.next();
1445                }
1446                (None, None) => break,
1447            }
1448        }
1449
1450        fn clone<T: Clone, U: Clone>(value: &(&T, &U)) -> (T, U) {
1451            (value.0.clone(), value.1.clone())
1452        }
1453
1454        changes.into()
1455    }
1456
1457    pub fn scan_complete(&self) -> impl Future<Output = ()> {
1458        let mut is_scanning_rx = self.is_scanning.1.clone();
1459        async move {
1460            let mut is_scanning = *is_scanning_rx.borrow();
1461            while is_scanning {
1462                if let Some(value) = is_scanning_rx.recv().await {
1463                    is_scanning = value;
1464                } else {
1465                    break;
1466                }
1467            }
1468        }
1469    }
1470
1471    pub fn snapshot(&self) -> LocalSnapshot {
1472        self.snapshot.clone()
1473    }
1474
1475    pub fn settings(&self) -> WorktreeSettings {
1476        self.settings.clone()
1477    }
1478
1479    pub fn local_git_repo(&self, path: &Path) -> Option<Arc<dyn GitRepository>> {
1480        self.local_repo_for_path(path)
1481            .map(|local_repo| local_repo.repo_ptr.clone())
1482    }
1483
1484    pub fn get_local_repo(&self, repo: &RepositoryEntry) -> Option<&LocalRepositoryEntry> {
1485        self.git_repositories.get(&repo.work_directory_id)
1486    }
1487
1488    fn load_binary_file(
1489        &self,
1490        path: &Path,
1491        cx: &ModelContext<Worktree>,
1492    ) -> Task<Result<LoadedBinaryFile>> {
1493        let path = Arc::from(path);
1494        let abs_path = self.absolutize(&path);
1495        let fs = self.fs.clone();
1496        let entry = self.refresh_entry(path.clone(), None, cx);
1497        let is_private = self.is_path_private(path.as_ref());
1498
1499        let worktree = cx.weak_model();
1500        cx.background_executor().spawn(async move {
1501            let abs_path = abs_path?;
1502            let content = fs.load_bytes(&abs_path).await?;
1503
1504            let worktree = worktree
1505                .upgrade()
1506                .ok_or_else(|| anyhow!("worktree was dropped"))?;
1507            let file = match entry.await? {
1508                Some(entry) => File::for_entry(entry, worktree),
1509                None => {
1510                    let metadata = fs
1511                        .metadata(&abs_path)
1512                        .await
1513                        .with_context(|| {
1514                            format!("Loading metadata for excluded file {abs_path:?}")
1515                        })?
1516                        .with_context(|| {
1517                            format!("Excluded file {abs_path:?} got removed during loading")
1518                        })?;
1519                    Arc::new(File {
1520                        entry_id: None,
1521                        worktree,
1522                        path,
1523                        disk_state: DiskState::Present {
1524                            mtime: metadata.mtime,
1525                        },
1526                        is_local: true,
1527                        is_private,
1528                    })
1529                }
1530            };
1531
1532            Ok(LoadedBinaryFile { file, content })
1533        })
1534    }
1535
1536    fn load_file(&self, path: &Path, cx: &ModelContext<Worktree>) -> Task<Result<LoadedFile>> {
1537        let path = Arc::from(path);
1538        let abs_path = self.absolutize(&path);
1539        let fs = self.fs.clone();
1540        let entry = self.refresh_entry(path.clone(), None, cx);
1541        let is_private = self.is_path_private(path.as_ref());
1542
1543        cx.spawn(|this, _cx| async move {
1544            let abs_path = abs_path?;
1545            let text = fs.load(&abs_path).await?;
1546
1547            let worktree = this
1548                .upgrade()
1549                .ok_or_else(|| anyhow!("worktree was dropped"))?;
1550            let file = match entry.await? {
1551                Some(entry) => File::for_entry(entry, worktree),
1552                None => {
1553                    let metadata = fs
1554                        .metadata(&abs_path)
1555                        .await
1556                        .with_context(|| {
1557                            format!("Loading metadata for excluded file {abs_path:?}")
1558                        })?
1559                        .with_context(|| {
1560                            format!("Excluded file {abs_path:?} got removed during loading")
1561                        })?;
1562                    Arc::new(File {
1563                        entry_id: None,
1564                        worktree,
1565                        path,
1566                        disk_state: DiskState::Present {
1567                            mtime: metadata.mtime,
1568                        },
1569                        is_local: true,
1570                        is_private,
1571                    })
1572                }
1573            };
1574
1575            Ok(LoadedFile { file, text })
1576        })
1577    }
1578
1579    /// Find the lowest path in the worktree's datastructures that is an ancestor
1580    fn lowest_ancestor(&self, path: &Path) -> PathBuf {
1581        let mut lowest_ancestor = None;
1582        for path in path.ancestors() {
1583            if self.entry_for_path(path).is_some() {
1584                lowest_ancestor = Some(path.to_path_buf());
1585                break;
1586            }
1587        }
1588
1589        lowest_ancestor.unwrap_or_else(|| PathBuf::from(""))
1590    }
1591
1592    fn create_entry(
1593        &self,
1594        path: impl Into<Arc<Path>>,
1595        is_dir: bool,
1596        cx: &ModelContext<Worktree>,
1597    ) -> Task<Result<CreatedEntry>> {
1598        let path = path.into();
1599        let abs_path = match self.absolutize(&path) {
1600            Ok(path) => path,
1601            Err(e) => return Task::ready(Err(e.context(format!("absolutizing path {path:?}")))),
1602        };
1603        let path_excluded = self.settings.is_path_excluded(&abs_path);
1604        let fs = self.fs.clone();
1605        let task_abs_path = abs_path.clone();
1606        let write = cx.background_executor().spawn(async move {
1607            if is_dir {
1608                fs.create_dir(&task_abs_path)
1609                    .await
1610                    .with_context(|| format!("creating directory {task_abs_path:?}"))
1611            } else {
1612                fs.save(&task_abs_path, &Rope::default(), LineEnding::default())
1613                    .await
1614                    .with_context(|| format!("creating file {task_abs_path:?}"))
1615            }
1616        });
1617
1618        let lowest_ancestor = self.lowest_ancestor(&path);
1619        cx.spawn(|this, mut cx| async move {
1620            write.await?;
1621            if path_excluded {
1622                return Ok(CreatedEntry::Excluded { abs_path });
1623            }
1624
1625            let (result, refreshes) = this.update(&mut cx, |this, cx| {
1626                let mut refreshes = Vec::new();
1627                let refresh_paths = path.strip_prefix(&lowest_ancestor).unwrap();
1628                for refresh_path in refresh_paths.ancestors() {
1629                    if refresh_path == Path::new("") {
1630                        continue;
1631                    }
1632                    let refresh_full_path = lowest_ancestor.join(refresh_path);
1633
1634                    refreshes.push(this.as_local_mut().unwrap().refresh_entry(
1635                        refresh_full_path.into(),
1636                        None,
1637                        cx,
1638                    ));
1639                }
1640                (
1641                    this.as_local_mut().unwrap().refresh_entry(path, None, cx),
1642                    refreshes,
1643                )
1644            })?;
1645            for refresh in refreshes {
1646                refresh.await.log_err();
1647            }
1648
1649            Ok(result
1650                .await?
1651                .map(CreatedEntry::Included)
1652                .unwrap_or_else(|| CreatedEntry::Excluded { abs_path }))
1653        })
1654    }
1655
1656    fn write_file(
1657        &self,
1658        path: impl Into<Arc<Path>>,
1659        text: Rope,
1660        line_ending: LineEnding,
1661        cx: &ModelContext<Worktree>,
1662    ) -> Task<Result<Arc<File>>> {
1663        let path = path.into();
1664        let fs = self.fs.clone();
1665        let is_private = self.is_path_private(&path);
1666        let Ok(abs_path) = self.absolutize(&path) else {
1667            return Task::ready(Err(anyhow!("invalid path {path:?}")));
1668        };
1669
1670        let write = cx.background_executor().spawn({
1671            let fs = fs.clone();
1672            let abs_path = abs_path.clone();
1673            async move { fs.save(&abs_path, &text, line_ending).await }
1674        });
1675
1676        cx.spawn(move |this, mut cx| async move {
1677            write.await?;
1678            let entry = this
1679                .update(&mut cx, |this, cx| {
1680                    this.as_local_mut()
1681                        .unwrap()
1682                        .refresh_entry(path.clone(), None, cx)
1683                })?
1684                .await?;
1685            let worktree = this.upgrade().ok_or_else(|| anyhow!("worktree dropped"))?;
1686            if let Some(entry) = entry {
1687                Ok(File::for_entry(entry, worktree))
1688            } else {
1689                let metadata = fs
1690                    .metadata(&abs_path)
1691                    .await
1692                    .with_context(|| {
1693                        format!("Fetching metadata after saving the excluded buffer {abs_path:?}")
1694                    })?
1695                    .with_context(|| {
1696                        format!("Excluded buffer {path:?} got removed during saving")
1697                    })?;
1698                Ok(Arc::new(File {
1699                    worktree,
1700                    path,
1701                    disk_state: DiskState::Present {
1702                        mtime: metadata.mtime,
1703                    },
1704                    entry_id: None,
1705                    is_local: true,
1706                    is_private,
1707                }))
1708            }
1709        })
1710    }
1711
1712    fn delete_entry(
1713        &self,
1714        entry_id: ProjectEntryId,
1715        trash: bool,
1716        cx: &ModelContext<Worktree>,
1717    ) -> Option<Task<Result<()>>> {
1718        let entry = self.entry_for_id(entry_id)?.clone();
1719        let abs_path = self.absolutize(&entry.path);
1720        let fs = self.fs.clone();
1721
1722        let delete = cx.background_executor().spawn(async move {
1723            if entry.is_file() {
1724                if trash {
1725                    fs.trash_file(&abs_path?, Default::default()).await?;
1726                } else {
1727                    fs.remove_file(&abs_path?, Default::default()).await?;
1728                }
1729            } else if trash {
1730                fs.trash_dir(
1731                    &abs_path?,
1732                    RemoveOptions {
1733                        recursive: true,
1734                        ignore_if_not_exists: false,
1735                    },
1736                )
1737                .await?;
1738            } else {
1739                fs.remove_dir(
1740                    &abs_path?,
1741                    RemoveOptions {
1742                        recursive: true,
1743                        ignore_if_not_exists: false,
1744                    },
1745                )
1746                .await?;
1747            }
1748            anyhow::Ok(entry.path)
1749        });
1750
1751        Some(cx.spawn(|this, mut cx| async move {
1752            let path = delete.await?;
1753            this.update(&mut cx, |this, _| {
1754                this.as_local_mut()
1755                    .unwrap()
1756                    .refresh_entries_for_paths(vec![path])
1757            })?
1758            .recv()
1759            .await;
1760            Ok(())
1761        }))
1762    }
1763
1764    fn rename_entry(
1765        &self,
1766        entry_id: ProjectEntryId,
1767        new_path: impl Into<Arc<Path>>,
1768        cx: &ModelContext<Worktree>,
1769    ) -> Task<Result<CreatedEntry>> {
1770        let old_path = match self.entry_for_id(entry_id) {
1771            Some(entry) => entry.path.clone(),
1772            None => return Task::ready(Err(anyhow!("no entry to rename for id {entry_id:?}"))),
1773        };
1774        let new_path = new_path.into();
1775        let abs_old_path = self.absolutize(&old_path);
1776        let Ok(abs_new_path) = self.absolutize(&new_path) else {
1777            return Task::ready(Err(anyhow!("absolutizing path {new_path:?}")));
1778        };
1779        let abs_path = abs_new_path.clone();
1780        let fs = self.fs.clone();
1781        let case_sensitive = self.fs_case_sensitive;
1782        let rename = cx.background_executor().spawn(async move {
1783            let abs_old_path = abs_old_path?;
1784            let abs_new_path = abs_new_path;
1785
1786            let abs_old_path_lower = abs_old_path.to_str().map(|p| p.to_lowercase());
1787            let abs_new_path_lower = abs_new_path.to_str().map(|p| p.to_lowercase());
1788
1789            // If we're on a case-insensitive FS and we're doing a case-only rename (i.e. `foobar` to `FOOBAR`)
1790            // we want to overwrite, because otherwise we run into a file-already-exists error.
1791            let overwrite = !case_sensitive
1792                && abs_old_path != abs_new_path
1793                && abs_old_path_lower == abs_new_path_lower;
1794
1795            fs.rename(
1796                &abs_old_path,
1797                &abs_new_path,
1798                fs::RenameOptions {
1799                    overwrite,
1800                    ..Default::default()
1801                },
1802            )
1803            .await
1804            .with_context(|| format!("Renaming {abs_old_path:?} into {abs_new_path:?}"))
1805        });
1806
1807        cx.spawn(|this, mut cx| async move {
1808            rename.await?;
1809            Ok(this
1810                .update(&mut cx, |this, cx| {
1811                    this.as_local_mut()
1812                        .unwrap()
1813                        .refresh_entry(new_path.clone(), Some(old_path), cx)
1814                })?
1815                .await?
1816                .map(CreatedEntry::Included)
1817                .unwrap_or_else(|| CreatedEntry::Excluded { abs_path }))
1818        })
1819    }
1820
1821    fn copy_entry(
1822        &self,
1823        entry_id: ProjectEntryId,
1824        relative_worktree_source_path: Option<PathBuf>,
1825        new_path: impl Into<Arc<Path>>,
1826        cx: &ModelContext<Worktree>,
1827    ) -> Task<Result<Option<Entry>>> {
1828        let old_path = match self.entry_for_id(entry_id) {
1829            Some(entry) => entry.path.clone(),
1830            None => return Task::ready(Ok(None)),
1831        };
1832        let new_path = new_path.into();
1833        let abs_old_path =
1834            if let Some(relative_worktree_source_path) = relative_worktree_source_path {
1835                Ok(self.abs_path().join(relative_worktree_source_path))
1836            } else {
1837                self.absolutize(&old_path)
1838            };
1839        let abs_new_path = self.absolutize(&new_path);
1840        let fs = self.fs.clone();
1841        let copy = cx.background_executor().spawn(async move {
1842            copy_recursive(
1843                fs.as_ref(),
1844                &abs_old_path?,
1845                &abs_new_path?,
1846                Default::default(),
1847            )
1848            .await
1849        });
1850
1851        cx.spawn(|this, mut cx| async move {
1852            copy.await?;
1853            this.update(&mut cx, |this, cx| {
1854                this.as_local_mut()
1855                    .unwrap()
1856                    .refresh_entry(new_path.clone(), None, cx)
1857            })?
1858            .await
1859        })
1860    }
1861
1862    pub fn copy_external_entries(
1863        &self,
1864        target_directory: PathBuf,
1865        paths: Vec<Arc<Path>>,
1866        overwrite_existing_files: bool,
1867        cx: &ModelContext<Worktree>,
1868    ) -> Task<Result<Vec<ProjectEntryId>>> {
1869        let worktree_path = self.abs_path().clone();
1870        let fs = self.fs.clone();
1871        let paths = paths
1872            .into_iter()
1873            .filter_map(|source| {
1874                let file_name = source.file_name()?;
1875                let mut target = target_directory.clone();
1876                target.push(file_name);
1877
1878                // Do not allow copying the same file to itself.
1879                if source.as_ref() != target.as_path() {
1880                    Some((source, target))
1881                } else {
1882                    None
1883                }
1884            })
1885            .collect::<Vec<_>>();
1886
1887        let paths_to_refresh = paths
1888            .iter()
1889            .filter_map(|(_, target)| Some(target.strip_prefix(&worktree_path).ok()?.into()))
1890            .collect::<Vec<_>>();
1891
1892        cx.spawn(|this, cx| async move {
1893            cx.background_executor()
1894                .spawn(async move {
1895                    for (source, target) in paths {
1896                        copy_recursive(
1897                            fs.as_ref(),
1898                            &source,
1899                            &target,
1900                            fs::CopyOptions {
1901                                overwrite: overwrite_existing_files,
1902                                ..Default::default()
1903                            },
1904                        )
1905                        .await
1906                        .with_context(|| {
1907                            anyhow!("Failed to copy file from {source:?} to {target:?}")
1908                        })?;
1909                    }
1910                    Ok::<(), anyhow::Error>(())
1911                })
1912                .await
1913                .log_err();
1914            let mut refresh = cx.read_model(
1915                &this.upgrade().with_context(|| "Dropped worktree")?,
1916                |this, _| {
1917                    Ok::<postage::barrier::Receiver, anyhow::Error>(
1918                        this.as_local()
1919                            .with_context(|| "Worktree is not local")?
1920                            .refresh_entries_for_paths(paths_to_refresh.clone()),
1921                    )
1922                },
1923            )??;
1924
1925            cx.background_executor()
1926                .spawn(async move {
1927                    refresh.next().await;
1928                    Ok::<(), anyhow::Error>(())
1929                })
1930                .await
1931                .log_err();
1932
1933            let this = this.upgrade().with_context(|| "Dropped worktree")?;
1934            cx.read_model(&this, |this, _| {
1935                paths_to_refresh
1936                    .iter()
1937                    .filter_map(|path| Some(this.entry_for_path(path)?.id))
1938                    .collect()
1939            })
1940        })
1941    }
1942
1943    fn expand_entry(
1944        &self,
1945        entry_id: ProjectEntryId,
1946        cx: &ModelContext<Worktree>,
1947    ) -> Option<Task<Result<()>>> {
1948        let path = self.entry_for_id(entry_id)?.path.clone();
1949        let mut refresh = self.refresh_entries_for_paths(vec![path]);
1950        Some(cx.background_executor().spawn(async move {
1951            refresh.next().await;
1952            Ok(())
1953        }))
1954    }
1955
1956    fn refresh_entries_for_paths(&self, paths: Vec<Arc<Path>>) -> barrier::Receiver {
1957        let (tx, rx) = barrier::channel();
1958        self.scan_requests_tx
1959            .try_send(ScanRequest {
1960                relative_paths: paths,
1961                done: smallvec![tx],
1962            })
1963            .ok();
1964        rx
1965    }
1966
1967    pub fn add_path_prefix_to_scan(&self, path_prefix: Arc<Path>) {
1968        self.path_prefixes_to_scan_tx.try_send(path_prefix).ok();
1969    }
1970
1971    fn refresh_entry(
1972        &self,
1973        path: Arc<Path>,
1974        old_path: Option<Arc<Path>>,
1975        cx: &ModelContext<Worktree>,
1976    ) -> Task<Result<Option<Entry>>> {
1977        if self.settings.is_path_excluded(&path) {
1978            return Task::ready(Ok(None));
1979        }
1980        let paths = if let Some(old_path) = old_path.as_ref() {
1981            vec![old_path.clone(), path.clone()]
1982        } else {
1983            vec![path.clone()]
1984        };
1985        let t0 = Instant::now();
1986        let mut refresh = self.refresh_entries_for_paths(paths);
1987        cx.spawn(move |this, mut cx| async move {
1988            refresh.recv().await;
1989            log::trace!("refreshed entry {path:?} in {:?}", t0.elapsed());
1990            let new_entry = this.update(&mut cx, |this, _| {
1991                this.entry_for_path(path)
1992                    .cloned()
1993                    .ok_or_else(|| anyhow!("failed to read path after update"))
1994            })??;
1995            Ok(Some(new_entry))
1996        })
1997    }
1998
1999    fn observe_updates<F, Fut>(&mut self, project_id: u64, cx: &ModelContext<Worktree>, callback: F)
2000    where
2001        F: 'static + Send + Fn(proto::UpdateWorktree) -> Fut,
2002        Fut: Send + Future<Output = bool>,
2003    {
2004        if let Some(observer) = self.update_observer.as_mut() {
2005            *observer.resume_updates.borrow_mut() = ();
2006            return;
2007        }
2008
2009        let (resume_updates_tx, mut resume_updates_rx) = watch::channel::<()>();
2010        let (snapshots_tx, mut snapshots_rx) =
2011            mpsc::unbounded::<(LocalSnapshot, UpdatedEntriesSet, UpdatedGitRepositoriesSet)>();
2012        snapshots_tx
2013            .unbounded_send((self.snapshot(), Arc::default(), Arc::default()))
2014            .ok();
2015
2016        let worktree_id = cx.entity_id().as_u64();
2017        let _maintain_remote_snapshot = cx.background_executor().spawn(async move {
2018            let mut is_first = true;
2019            while let Some((snapshot, entry_changes, repo_changes)) = snapshots_rx.next().await {
2020                let update;
2021                if is_first {
2022                    update = snapshot.build_initial_update(project_id, worktree_id);
2023                    is_first = false;
2024                } else {
2025                    update =
2026                        snapshot.build_update(project_id, worktree_id, entry_changes, repo_changes);
2027                }
2028
2029                for update in proto::split_worktree_update(update) {
2030                    let _ = resume_updates_rx.try_recv();
2031                    loop {
2032                        let result = callback(update.clone());
2033                        if result.await {
2034                            break;
2035                        } else {
2036                            log::info!("waiting to resume updates");
2037                            if resume_updates_rx.next().await.is_none() {
2038                                return Some(());
2039                            }
2040                        }
2041                    }
2042                }
2043            }
2044            Some(())
2045        });
2046
2047        self.update_observer = Some(UpdateObservationState {
2048            snapshots_tx,
2049            resume_updates: resume_updates_tx,
2050            _maintain_remote_snapshot,
2051        });
2052    }
2053
2054    pub fn share_private_files(&mut self, cx: &ModelContext<Worktree>) {
2055        self.share_private_files = true;
2056        self.restart_background_scanners(cx);
2057    }
2058}
2059
2060impl RemoteWorktree {
2061    pub fn project_id(&self) -> u64 {
2062        self.project_id
2063    }
2064
2065    pub fn client(&self) -> AnyProtoClient {
2066        self.client.clone()
2067    }
2068
2069    pub fn disconnected_from_host(&mut self) {
2070        self.updates_tx.take();
2071        self.snapshot_subscriptions.clear();
2072        self.disconnected = true;
2073    }
2074
2075    pub fn update_from_remote(&self, update: proto::UpdateWorktree) {
2076        if let Some(updates_tx) = &self.updates_tx {
2077            updates_tx
2078                .unbounded_send(update)
2079                .expect("consumer runs to completion");
2080        }
2081    }
2082
2083    fn observe_updates<F, Fut>(&mut self, project_id: u64, cx: &ModelContext<Worktree>, callback: F)
2084    where
2085        F: 'static + Send + Fn(proto::UpdateWorktree) -> Fut,
2086        Fut: 'static + Send + Future<Output = bool>,
2087    {
2088        let (tx, mut rx) = mpsc::unbounded();
2089        let initial_update = self
2090            .snapshot
2091            .build_initial_update(project_id, self.id().to_proto());
2092        self.update_observer = Some(tx);
2093        cx.spawn(|this, mut cx| async move {
2094            let mut update = initial_update;
2095            'outer: loop {
2096                // SSH projects use a special project ID of 0, and we need to
2097                // remap it to the correct one here.
2098                update.project_id = project_id;
2099
2100                for chunk in split_worktree_update(update) {
2101                    if !callback(chunk).await {
2102                        break 'outer;
2103                    }
2104                }
2105
2106                if let Some(next_update) = rx.next().await {
2107                    update = next_update;
2108                } else {
2109                    break;
2110                }
2111            }
2112            this.update(&mut cx, |this, _| {
2113                let this = this.as_remote_mut().unwrap();
2114                this.update_observer.take();
2115            })
2116        })
2117        .detach();
2118    }
2119
2120    fn observed_snapshot(&self, scan_id: usize) -> bool {
2121        self.completed_scan_id >= scan_id
2122    }
2123
2124    pub fn wait_for_snapshot(&mut self, scan_id: usize) -> impl Future<Output = Result<()>> {
2125        let (tx, rx) = oneshot::channel();
2126        if self.observed_snapshot(scan_id) {
2127            let _ = tx.send(());
2128        } else if self.disconnected {
2129            drop(tx);
2130        } else {
2131            match self
2132                .snapshot_subscriptions
2133                .binary_search_by_key(&scan_id, |probe| probe.0)
2134            {
2135                Ok(ix) | Err(ix) => self.snapshot_subscriptions.insert(ix, (scan_id, tx)),
2136            }
2137        }
2138
2139        async move {
2140            rx.await?;
2141            Ok(())
2142        }
2143    }
2144
2145    fn insert_entry(
2146        &mut self,
2147        entry: proto::Entry,
2148        scan_id: usize,
2149        cx: &ModelContext<Worktree>,
2150    ) -> Task<Result<Entry>> {
2151        let wait_for_snapshot = self.wait_for_snapshot(scan_id);
2152        cx.spawn(|this, mut cx| async move {
2153            wait_for_snapshot.await?;
2154            this.update(&mut cx, |worktree, _| {
2155                let worktree = worktree.as_remote_mut().unwrap();
2156                let snapshot = &mut worktree.background_snapshot.lock().0;
2157                let entry = snapshot.insert_entry(entry, &worktree.file_scan_inclusions);
2158                worktree.snapshot = snapshot.clone();
2159                entry
2160            })?
2161        })
2162    }
2163
2164    fn delete_entry(
2165        &self,
2166        entry_id: ProjectEntryId,
2167        trash: bool,
2168        cx: &ModelContext<Worktree>,
2169    ) -> Option<Task<Result<()>>> {
2170        let response = self.client.request(proto::DeleteProjectEntry {
2171            project_id: self.project_id,
2172            entry_id: entry_id.to_proto(),
2173            use_trash: trash,
2174        });
2175        Some(cx.spawn(move |this, mut cx| async move {
2176            let response = response.await?;
2177            let scan_id = response.worktree_scan_id as usize;
2178
2179            this.update(&mut cx, move |this, _| {
2180                this.as_remote_mut().unwrap().wait_for_snapshot(scan_id)
2181            })?
2182            .await?;
2183
2184            this.update(&mut cx, |this, _| {
2185                let this = this.as_remote_mut().unwrap();
2186                let snapshot = &mut this.background_snapshot.lock().0;
2187                snapshot.delete_entry(entry_id);
2188                this.snapshot = snapshot.clone();
2189            })
2190        }))
2191    }
2192
2193    fn rename_entry(
2194        &self,
2195        entry_id: ProjectEntryId,
2196        new_path: impl Into<Arc<Path>>,
2197        cx: &ModelContext<Worktree>,
2198    ) -> Task<Result<CreatedEntry>> {
2199        let new_path = new_path.into();
2200        let response = self.client.request(proto::RenameProjectEntry {
2201            project_id: self.project_id,
2202            entry_id: entry_id.to_proto(),
2203            new_path: new_path.to_string_lossy().into(),
2204        });
2205        cx.spawn(move |this, mut cx| async move {
2206            let response = response.await?;
2207            match response.entry {
2208                Some(entry) => this
2209                    .update(&mut cx, |this, cx| {
2210                        this.as_remote_mut().unwrap().insert_entry(
2211                            entry,
2212                            response.worktree_scan_id as usize,
2213                            cx,
2214                        )
2215                    })?
2216                    .await
2217                    .map(CreatedEntry::Included),
2218                None => {
2219                    let abs_path = this.update(&mut cx, |worktree, _| {
2220                        worktree
2221                            .absolutize(&new_path)
2222                            .with_context(|| format!("absolutizing {new_path:?}"))
2223                    })??;
2224                    Ok(CreatedEntry::Excluded { abs_path })
2225                }
2226            }
2227        })
2228    }
2229}
2230
2231impl Snapshot {
2232    pub fn new(id: u64, root_name: String, abs_path: Arc<Path>) -> Self {
2233        Snapshot {
2234            id: WorktreeId::from_usize(id as usize),
2235            abs_path: abs_path.into(),
2236            root_char_bag: root_name.chars().map(|c| c.to_ascii_lowercase()).collect(),
2237            root_name,
2238            always_included_entries: Default::default(),
2239            entries_by_path: Default::default(),
2240            entries_by_id: Default::default(),
2241            repositories: Default::default(),
2242            scan_id: 1,
2243            completed_scan_id: 0,
2244        }
2245    }
2246
2247    pub fn id(&self) -> WorktreeId {
2248        self.id
2249    }
2250
2251    // TODO:
2252    // Consider the following:
2253    //
2254    // ```rust
2255    // let abs_path: Arc<Path> = snapshot.abs_path(); // e.g. "C:\Users\user\Desktop\project"
2256    // let some_non_trimmed_path = Path::new("\\\\?\\C:\\Users\\user\\Desktop\\project\\main.rs");
2257    // // The caller perform some actions here:
2258    // some_non_trimmed_path.strip_prefix(abs_path);  // This fails
2259    // some_non_trimmed_path.starts_with(abs_path);   // This fails too
2260    // ```
2261    //
2262    // This is definitely a bug, but it's not clear if we should handle it here or not.
2263    pub fn abs_path(&self) -> &Arc<Path> {
2264        self.abs_path.as_path()
2265    }
2266
2267    fn build_initial_update(&self, project_id: u64, worktree_id: u64) -> proto::UpdateWorktree {
2268        let mut updated_entries = self
2269            .entries_by_path
2270            .iter()
2271            .map(proto::Entry::from)
2272            .collect::<Vec<_>>();
2273        updated_entries.sort_unstable_by_key(|e| e.id);
2274
2275        let mut updated_repositories = self
2276            .repositories
2277            .iter()
2278            .map(|repository| repository.initial_update())
2279            .collect::<Vec<_>>();
2280        updated_repositories.sort_unstable_by_key(|e| e.work_directory_id);
2281
2282        proto::UpdateWorktree {
2283            project_id,
2284            worktree_id,
2285            abs_path: self.abs_path().to_string_lossy().into(),
2286            root_name: self.root_name().to_string(),
2287            updated_entries,
2288            removed_entries: Vec::new(),
2289            scan_id: self.scan_id as u64,
2290            is_last_update: self.completed_scan_id == self.scan_id,
2291            updated_repositories,
2292            removed_repositories: Vec::new(),
2293        }
2294    }
2295
2296    pub fn absolutize(&self, path: &Path) -> Result<PathBuf> {
2297        if path
2298            .components()
2299            .any(|component| !matches!(component, std::path::Component::Normal(_)))
2300        {
2301            return Err(anyhow!("invalid path"));
2302        }
2303        if path.file_name().is_some() {
2304            Ok(self.abs_path.as_path().join(path))
2305        } else {
2306            Ok(self.abs_path.as_path().to_path_buf())
2307        }
2308    }
2309
2310    pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
2311        self.entries_by_id.get(&entry_id, &()).is_some()
2312    }
2313
2314    fn insert_entry(
2315        &mut self,
2316        entry: proto::Entry,
2317        always_included_paths: &PathMatcher,
2318    ) -> Result<Entry> {
2319        let entry = Entry::try_from((&self.root_char_bag, always_included_paths, entry))?;
2320        let old_entry = self.entries_by_id.insert_or_replace(
2321            PathEntry {
2322                id: entry.id,
2323                path: entry.path.clone(),
2324                is_ignored: entry.is_ignored,
2325                scan_id: 0,
2326            },
2327            &(),
2328        );
2329        if let Some(old_entry) = old_entry {
2330            self.entries_by_path.remove(&PathKey(old_entry.path), &());
2331        }
2332        self.entries_by_path.insert_or_replace(entry.clone(), &());
2333        Ok(entry)
2334    }
2335
2336    fn delete_entry(&mut self, entry_id: ProjectEntryId) -> Option<Arc<Path>> {
2337        let removed_entry = self.entries_by_id.remove(&entry_id, &())?;
2338        self.entries_by_path = {
2339            let mut cursor = self.entries_by_path.cursor::<TraversalProgress>(&());
2340            let mut new_entries_by_path =
2341                cursor.slice(&TraversalTarget::path(&removed_entry.path), Bias::Left, &());
2342            while let Some(entry) = cursor.item() {
2343                if entry.path.starts_with(&removed_entry.path) {
2344                    self.entries_by_id.remove(&entry.id, &());
2345                    cursor.next(&());
2346                } else {
2347                    break;
2348                }
2349            }
2350            new_entries_by_path.append(cursor.suffix(&()), &());
2351            new_entries_by_path
2352        };
2353
2354        Some(removed_entry.path)
2355    }
2356
2357    pub fn status_for_file(&self, path: impl AsRef<Path>) -> Option<GitFileStatus> {
2358        let path = path.as_ref();
2359        self.repository_for_path(path).and_then(|repo| {
2360            let repo_path = repo.relativize(path).unwrap();
2361            repo.statuses_by_path
2362                .get(&PathKey(repo_path.0), &())
2363                .map(|entry| entry.status)
2364        })
2365    }
2366
2367    fn update_abs_path(&mut self, abs_path: SanitizedPath, root_name: String) {
2368        self.abs_path = abs_path;
2369        if root_name != self.root_name {
2370            self.root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
2371            self.root_name = root_name;
2372        }
2373    }
2374
2375    pub(crate) fn apply_remote_update(
2376        &mut self,
2377        mut update: proto::UpdateWorktree,
2378        always_included_paths: &PathMatcher,
2379    ) -> Result<()> {
2380        log::trace!(
2381            "applying remote worktree update. {} entries updated, {} removed",
2382            update.updated_entries.len(),
2383            update.removed_entries.len()
2384        );
2385        self.update_abs_path(
2386            SanitizedPath::from(PathBuf::from(update.abs_path)),
2387            update.root_name,
2388        );
2389
2390        let mut entries_by_path_edits = Vec::new();
2391        let mut entries_by_id_edits = Vec::new();
2392
2393        for entry_id in update.removed_entries {
2394            let entry_id = ProjectEntryId::from_proto(entry_id);
2395            entries_by_id_edits.push(Edit::Remove(entry_id));
2396            if let Some(entry) = self.entry_for_id(entry_id) {
2397                entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
2398            }
2399        }
2400
2401        for entry in update.updated_entries {
2402            let entry = Entry::try_from((&self.root_char_bag, always_included_paths, entry))?;
2403            if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
2404                entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
2405            }
2406            if let Some(old_entry) = self.entries_by_path.get(&PathKey(entry.path.clone()), &()) {
2407                if old_entry.id != entry.id {
2408                    entries_by_id_edits.push(Edit::Remove(old_entry.id));
2409                }
2410            }
2411            entries_by_id_edits.push(Edit::Insert(PathEntry {
2412                id: entry.id,
2413                path: entry.path.clone(),
2414                is_ignored: entry.is_ignored,
2415                scan_id: 0,
2416            }));
2417            entries_by_path_edits.push(Edit::Insert(entry));
2418        }
2419
2420        self.entries_by_path.edit(entries_by_path_edits, &());
2421        self.entries_by_id.edit(entries_by_id_edits, &());
2422
2423        update.removed_repositories.sort_unstable();
2424        self.repositories.retain(&(), |entry: &RepositoryEntry| {
2425            update
2426                .removed_repositories
2427                .binary_search(&entry.work_directory_id.to_proto())
2428                .is_err()
2429        });
2430
2431        for repository in update.updated_repositories {
2432            let work_directory_id = ProjectEntryId::from_proto(repository.work_directory_id);
2433            if let Some(work_dir_entry) = self.entry_for_id(work_directory_id) {
2434                if self
2435                    .repositories
2436                    .contains(&PathKey(work_dir_entry.path.clone()), &())
2437                {
2438                    let edits = repository
2439                        .removed_statuses
2440                        .into_iter()
2441                        .map(|path| Edit::Remove(PathKey(Path::new(&path).into())))
2442                        .chain(repository.updated_statuses.into_iter().filter_map(
2443                            |updated_status| {
2444                                Some(Edit::Insert(updated_status.try_into().log_err()?))
2445                            },
2446                        ))
2447                        .collect::<Vec<_>>();
2448
2449                    self.repositories
2450                        .update(&PathKey(work_dir_entry.path.clone()), &(), |repo| {
2451                            repo.branch = repository.branch.map(Into::into);
2452                            repo.statuses_by_path.edit(edits, &());
2453                        });
2454                } else {
2455                    let statuses = SumTree::from_iter(
2456                        repository
2457                            .updated_statuses
2458                            .into_iter()
2459                            .filter_map(|updated_status| updated_status.try_into().log_err()),
2460                        &(),
2461                    );
2462
2463                    self.repositories.insert_or_replace(
2464                        RepositoryEntry {
2465                            work_directory_id,
2466                            work_directory: WorkDirectory {
2467                                path: work_dir_entry.path.clone(),
2468                                // When syncing repository entries from a peer, we don't need
2469                                // the location_in_repo field, since git operations don't happen locally
2470                                // anyway.
2471                                location_in_repo: None,
2472                            },
2473                            branch: repository.branch.map(Into::into),
2474                            statuses_by_path: statuses,
2475                        },
2476                        &(),
2477                    );
2478                }
2479            } else {
2480                log::error!(
2481                    "no work directory entry for repository {:?}",
2482                    repository.work_directory_id
2483                )
2484            }
2485        }
2486
2487        self.scan_id = update.scan_id as usize;
2488        if update.is_last_update {
2489            self.completed_scan_id = update.scan_id as usize;
2490        }
2491
2492        Ok(())
2493    }
2494
2495    pub fn entry_count(&self) -> usize {
2496        self.entries_by_path.summary().count
2497    }
2498
2499    pub fn visible_entry_count(&self) -> usize {
2500        self.entries_by_path.summary().non_ignored_count
2501    }
2502
2503    pub fn dir_count(&self) -> usize {
2504        let summary = self.entries_by_path.summary();
2505        summary.count - summary.file_count
2506    }
2507
2508    pub fn visible_dir_count(&self) -> usize {
2509        let summary = self.entries_by_path.summary();
2510        summary.non_ignored_count - summary.non_ignored_file_count
2511    }
2512
2513    pub fn file_count(&self) -> usize {
2514        self.entries_by_path.summary().file_count
2515    }
2516
2517    pub fn visible_file_count(&self) -> usize {
2518        self.entries_by_path.summary().non_ignored_file_count
2519    }
2520
2521    fn traverse_from_offset(
2522        &self,
2523        include_files: bool,
2524        include_dirs: bool,
2525        include_ignored: bool,
2526        start_offset: usize,
2527    ) -> Traversal {
2528        let mut cursor = self.entries_by_path.cursor(&());
2529        cursor.seek(
2530            &TraversalTarget::Count {
2531                count: start_offset,
2532                include_files,
2533                include_dirs,
2534                include_ignored,
2535            },
2536            Bias::Right,
2537            &(),
2538        );
2539        Traversal {
2540            snapshot: self,
2541            cursor,
2542            include_files,
2543            include_dirs,
2544            include_ignored,
2545        }
2546    }
2547
2548    pub fn traverse_from_path(
2549        &self,
2550        include_files: bool,
2551        include_dirs: bool,
2552        include_ignored: bool,
2553        path: &Path,
2554    ) -> Traversal {
2555        Traversal::new(self, include_files, include_dirs, include_ignored, path)
2556    }
2557
2558    pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
2559        self.traverse_from_offset(true, false, include_ignored, start)
2560    }
2561
2562    pub fn directories(&self, include_ignored: bool, start: usize) -> Traversal {
2563        self.traverse_from_offset(false, true, include_ignored, start)
2564    }
2565
2566    pub fn entries(&self, include_ignored: bool, start: usize) -> Traversal {
2567        self.traverse_from_offset(true, true, include_ignored, start)
2568    }
2569
2570    #[cfg(any(feature = "test-support", test))]
2571    pub fn git_status(&self, work_dir: &Path) -> Option<Vec<StatusEntry>> {
2572        self.repositories
2573            .get(&PathKey(work_dir.into()), &())
2574            .map(|repo| repo.status().collect())
2575    }
2576
2577    pub fn repositories(&self) -> impl Iterator<Item = &RepositoryEntry> {
2578        self.repositories.iter()
2579    }
2580
2581    /// Get the repository whose work directory corresponds to the given path.
2582    pub(crate) fn repository(&self, work_directory: PathKey) -> Option<RepositoryEntry> {
2583        self.repositories.get(&work_directory, &()).cloned()
2584    }
2585
2586    /// Get the repository whose work directory contains the given path.
2587    pub fn repository_for_path(&self, path: &Path) -> Option<&RepositoryEntry> {
2588        let mut cursor = self.repositories.cursor::<PathProgress>(&());
2589        let mut repository = None;
2590
2591        // Git repositories may contain other git repositories. As a side effect of
2592        // lexicographic sorting by path, deeper repositories will be after higher repositories
2593        // So, let's loop through every matching repository until we can't find any more to find
2594        // the deepest repository that could contain this path.
2595        while cursor.seek_forward(&PathTarget::Contains(path), Bias::Left, &())
2596            && cursor.item().is_some()
2597        {
2598            repository = cursor.item();
2599            cursor.next(&());
2600        }
2601
2602        repository
2603    }
2604
2605    /// Given an ordered iterator of entries, returns an iterator of those entries,
2606    /// along with their containing git repository.
2607    pub fn entries_with_repositories<'a>(
2608        &'a self,
2609        entries: impl 'a + Iterator<Item = &'a Entry>,
2610    ) -> impl 'a + Iterator<Item = (&'a Entry, Option<&'a RepositoryEntry>)> {
2611        let mut containing_repos = Vec::<&RepositoryEntry>::new();
2612        let mut repositories = self.repositories().peekable();
2613        entries.map(move |entry| {
2614            while let Some(repository) = containing_repos.last() {
2615                if repository.directory_contains(&entry.path) {
2616                    break;
2617                } else {
2618                    containing_repos.pop();
2619                }
2620            }
2621            while let Some(repository) = repositories.peek() {
2622                if repository.directory_contains(&entry.path) {
2623                    containing_repos.push(repositories.next().unwrap());
2624                } else {
2625                    break;
2626                }
2627            }
2628            let repo = containing_repos.last().copied();
2629            (entry, repo)
2630        })
2631    }
2632
2633    pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
2634        let empty_path = Path::new("");
2635        self.entries_by_path
2636            .cursor::<()>(&())
2637            .filter(move |entry| entry.path.as_ref() != empty_path)
2638            .map(|entry| &entry.path)
2639    }
2640
2641    pub fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
2642        let mut cursor = self.entries_by_path.cursor(&());
2643        cursor.seek(&TraversalTarget::path(parent_path), Bias::Right, &());
2644        let traversal = Traversal {
2645            snapshot: self,
2646            cursor,
2647            include_files: true,
2648            include_dirs: true,
2649            include_ignored: true,
2650        };
2651        ChildEntriesIter {
2652            traversal,
2653            parent_path,
2654        }
2655    }
2656
2657    pub fn root_entry(&self) -> Option<&Entry> {
2658        self.entry_for_path("")
2659    }
2660
2661    pub fn root_dir(&self) -> Option<Arc<Path>> {
2662        self.root_entry()
2663            .filter(|entry| entry.is_dir())
2664            .map(|_| self.abs_path().clone())
2665    }
2666
2667    pub fn root_name(&self) -> &str {
2668        &self.root_name
2669    }
2670
2671    pub fn root_git_entry(&self) -> Option<RepositoryEntry> {
2672        self.repositories
2673            .get(&PathKey(Path::new("").into()), &())
2674            .map(|entry| entry.to_owned())
2675    }
2676
2677    pub fn git_entry(&self, work_directory_path: Arc<Path>) -> Option<RepositoryEntry> {
2678        self.repositories
2679            .get(&PathKey(work_directory_path), &())
2680            .map(|entry| entry.to_owned())
2681    }
2682
2683    pub fn git_entries(&self) -> impl Iterator<Item = &RepositoryEntry> {
2684        self.repositories.iter()
2685    }
2686
2687    pub fn scan_id(&self) -> usize {
2688        self.scan_id
2689    }
2690
2691    pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
2692        let path = path.as_ref();
2693        self.traverse_from_path(true, true, true, path)
2694            .entry()
2695            .and_then(|entry| {
2696                if entry.path.as_ref() == path {
2697                    Some(entry)
2698                } else {
2699                    None
2700                }
2701            })
2702    }
2703
2704    pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
2705        let entry = self.entries_by_id.get(&id, &())?;
2706        self.entry_for_path(&entry.path)
2707    }
2708
2709    pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
2710        self.entry_for_path(path.as_ref()).map(|e| e.inode)
2711    }
2712}
2713
2714impl LocalSnapshot {
2715    pub fn local_repo_for_path(&self, path: &Path) -> Option<&LocalRepositoryEntry> {
2716        let repository_entry = self.repository_for_path(path)?;
2717        let work_directory_id = repository_entry.work_directory_id();
2718        self.git_repositories.get(&work_directory_id)
2719    }
2720
2721    fn build_update(
2722        &self,
2723        project_id: u64,
2724        worktree_id: u64,
2725        entry_changes: UpdatedEntriesSet,
2726        repo_changes: UpdatedGitRepositoriesSet,
2727    ) -> proto::UpdateWorktree {
2728        let mut updated_entries = Vec::new();
2729        let mut removed_entries = Vec::new();
2730        let mut updated_repositories = Vec::new();
2731        let mut removed_repositories = Vec::new();
2732
2733        for (_, entry_id, path_change) in entry_changes.iter() {
2734            if let PathChange::Removed = path_change {
2735                removed_entries.push(entry_id.0 as u64);
2736            } else if let Some(entry) = self.entry_for_id(*entry_id) {
2737                updated_entries.push(proto::Entry::from(entry));
2738            }
2739        }
2740
2741        for (work_dir_path, change) in repo_changes.iter() {
2742            let new_repo = self.repositories.get(&PathKey(work_dir_path.clone()), &());
2743            match (&change.old_repository, new_repo) {
2744                (Some(old_repo), Some(new_repo)) => {
2745                    updated_repositories.push(new_repo.build_update(old_repo));
2746                }
2747                (None, Some(new_repo)) => {
2748                    updated_repositories.push(new_repo.initial_update());
2749                }
2750                (Some(old_repo), None) => {
2751                    removed_repositories.push(old_repo.work_directory_id.to_proto());
2752                }
2753                _ => {}
2754            }
2755        }
2756
2757        removed_entries.sort_unstable();
2758        updated_entries.sort_unstable_by_key(|e| e.id);
2759        removed_repositories.sort_unstable();
2760        updated_repositories.sort_unstable_by_key(|e| e.work_directory_id);
2761
2762        // TODO - optimize, knowing that removed_entries are sorted.
2763        removed_entries.retain(|id| updated_entries.binary_search_by_key(id, |e| e.id).is_err());
2764
2765        proto::UpdateWorktree {
2766            project_id,
2767            worktree_id,
2768            abs_path: self.abs_path().to_string_lossy().into(),
2769            root_name: self.root_name().to_string(),
2770            updated_entries,
2771            removed_entries,
2772            scan_id: self.scan_id as u64,
2773            is_last_update: self.completed_scan_id == self.scan_id,
2774            updated_repositories,
2775            removed_repositories,
2776        }
2777    }
2778
2779    fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
2780        if entry.is_file() && entry.path.file_name() == Some(&GITIGNORE) {
2781            let abs_path = self.abs_path.as_path().join(&entry.path);
2782            match smol::block_on(build_gitignore(&abs_path, fs)) {
2783                Ok(ignore) => {
2784                    self.ignores_by_parent_abs_path
2785                        .insert(abs_path.parent().unwrap().into(), (Arc::new(ignore), true));
2786                }
2787                Err(error) => {
2788                    log::error!(
2789                        "error loading .gitignore file {:?} - {:?}",
2790                        &entry.path,
2791                        error
2792                    );
2793                }
2794            }
2795        }
2796
2797        if entry.kind == EntryKind::PendingDir {
2798            if let Some(existing_entry) =
2799                self.entries_by_path.get(&PathKey(entry.path.clone()), &())
2800            {
2801                entry.kind = existing_entry.kind;
2802            }
2803        }
2804
2805        let scan_id = self.scan_id;
2806        let removed = self.entries_by_path.insert_or_replace(entry.clone(), &());
2807        if let Some(removed) = removed {
2808            if removed.id != entry.id {
2809                self.entries_by_id.remove(&removed.id, &());
2810            }
2811        }
2812        self.entries_by_id.insert_or_replace(
2813            PathEntry {
2814                id: entry.id,
2815                path: entry.path.clone(),
2816                is_ignored: entry.is_ignored,
2817                scan_id,
2818            },
2819            &(),
2820        );
2821
2822        entry
2823    }
2824
2825    fn ancestor_inodes_for_path(&self, path: &Path) -> TreeSet<u64> {
2826        let mut inodes = TreeSet::default();
2827        for ancestor in path.ancestors().skip(1) {
2828            if let Some(entry) = self.entry_for_path(ancestor) {
2829                inodes.insert(entry.inode);
2830            }
2831        }
2832        inodes
2833    }
2834
2835    fn ignore_stack_for_abs_path(&self, abs_path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
2836        let mut new_ignores = Vec::new();
2837        for (index, ancestor) in abs_path.ancestors().enumerate() {
2838            if index > 0 {
2839                if let Some((ignore, _)) = self.ignores_by_parent_abs_path.get(ancestor) {
2840                    new_ignores.push((ancestor, Some(ignore.clone())));
2841                } else {
2842                    new_ignores.push((ancestor, None));
2843                }
2844            }
2845            if ancestor.join(*DOT_GIT).exists() {
2846                break;
2847            }
2848        }
2849
2850        let mut ignore_stack = IgnoreStack::none();
2851        for (parent_abs_path, ignore) in new_ignores.into_iter().rev() {
2852            if ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
2853                ignore_stack = IgnoreStack::all();
2854                break;
2855            } else if let Some(ignore) = ignore {
2856                ignore_stack = ignore_stack.append(parent_abs_path.into(), ignore);
2857            }
2858        }
2859
2860        if ignore_stack.is_abs_path_ignored(abs_path, is_dir) {
2861            ignore_stack = IgnoreStack::all();
2862        }
2863
2864        ignore_stack
2865    }
2866
2867    #[cfg(test)]
2868    pub(crate) fn expanded_entries(&self) -> impl Iterator<Item = &Entry> {
2869        self.entries_by_path
2870            .cursor::<()>(&())
2871            .filter(|entry| entry.kind == EntryKind::Dir && (entry.is_external || entry.is_ignored))
2872    }
2873
2874    #[cfg(test)]
2875    pub fn check_invariants(&self, git_state: bool) {
2876        use pretty_assertions::assert_eq;
2877
2878        assert_eq!(
2879            self.entries_by_path
2880                .cursor::<()>(&())
2881                .map(|e| (&e.path, e.id))
2882                .collect::<Vec<_>>(),
2883            self.entries_by_id
2884                .cursor::<()>(&())
2885                .map(|e| (&e.path, e.id))
2886                .collect::<collections::BTreeSet<_>>()
2887                .into_iter()
2888                .collect::<Vec<_>>(),
2889            "entries_by_path and entries_by_id are inconsistent"
2890        );
2891
2892        let mut files = self.files(true, 0);
2893        let mut visible_files = self.files(false, 0);
2894        for entry in self.entries_by_path.cursor::<()>(&()) {
2895            if entry.is_file() {
2896                assert_eq!(files.next().unwrap().inode, entry.inode);
2897                if (!entry.is_ignored && !entry.is_external) || entry.is_always_included {
2898                    assert_eq!(visible_files.next().unwrap().inode, entry.inode);
2899                }
2900            }
2901        }
2902
2903        assert!(files.next().is_none());
2904        assert!(visible_files.next().is_none());
2905
2906        let mut bfs_paths = Vec::new();
2907        let mut stack = self
2908            .root_entry()
2909            .map(|e| e.path.as_ref())
2910            .into_iter()
2911            .collect::<Vec<_>>();
2912        while let Some(path) = stack.pop() {
2913            bfs_paths.push(path);
2914            let ix = stack.len();
2915            for child_entry in self.child_entries(path) {
2916                stack.insert(ix, &child_entry.path);
2917            }
2918        }
2919
2920        let dfs_paths_via_iter = self
2921            .entries_by_path
2922            .cursor::<()>(&())
2923            .map(|e| e.path.as_ref())
2924            .collect::<Vec<_>>();
2925        assert_eq!(bfs_paths, dfs_paths_via_iter);
2926
2927        let dfs_paths_via_traversal = self
2928            .entries(true, 0)
2929            .map(|e| e.path.as_ref())
2930            .collect::<Vec<_>>();
2931        assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter);
2932
2933        if git_state {
2934            for ignore_parent_abs_path in self.ignores_by_parent_abs_path.keys() {
2935                let ignore_parent_path = ignore_parent_abs_path
2936                    .strip_prefix(self.abs_path.as_path())
2937                    .unwrap();
2938                assert!(self.entry_for_path(ignore_parent_path).is_some());
2939                assert!(self
2940                    .entry_for_path(ignore_parent_path.join(*GITIGNORE))
2941                    .is_some());
2942            }
2943        }
2944    }
2945
2946    #[cfg(test)]
2947    fn check_git_invariants(&self) {
2948        let dotgit_paths = self
2949            .git_repositories
2950            .iter()
2951            .map(|repo| repo.1.dot_git_dir_abs_path.clone())
2952            .collect::<HashSet<_>>();
2953        let work_dir_paths = self
2954            .repositories
2955            .iter()
2956            .map(|repo| repo.work_directory.path.clone())
2957            .collect::<HashSet<_>>();
2958        assert_eq!(dotgit_paths.len(), work_dir_paths.len());
2959        assert_eq!(self.repositories.iter().count(), work_dir_paths.len());
2960        assert_eq!(self.git_repositories.iter().count(), work_dir_paths.len());
2961        for entry in self.repositories.iter() {
2962            self.git_repositories.get(&entry.work_directory_id).unwrap();
2963        }
2964    }
2965
2966    #[cfg(test)]
2967    pub fn entries_without_ids(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
2968        let mut paths = Vec::new();
2969        for entry in self.entries_by_path.cursor::<()>(&()) {
2970            if include_ignored || !entry.is_ignored {
2971                paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
2972            }
2973        }
2974        paths.sort_by(|a, b| a.0.cmp(b.0));
2975        paths
2976    }
2977}
2978
2979impl BackgroundScannerState {
2980    fn should_scan_directory(&self, entry: &Entry) -> bool {
2981        (!entry.is_external && (!entry.is_ignored || entry.is_always_included))
2982            || entry.path.file_name() == Some(*DOT_GIT)
2983            || entry.path.file_name() == Some(local_settings_folder_relative_path().as_os_str())
2984            || self.scanned_dirs.contains(&entry.id) // If we've ever scanned it, keep scanning
2985            || self
2986                .paths_to_scan
2987                .iter()
2988                .any(|p| p.starts_with(&entry.path))
2989            || self
2990                .path_prefixes_to_scan
2991                .iter()
2992                .any(|p| entry.path.starts_with(p))
2993    }
2994
2995    fn enqueue_scan_dir(&self, abs_path: Arc<Path>, entry: &Entry, scan_job_tx: &Sender<ScanJob>) {
2996        let path = entry.path.clone();
2997        let ignore_stack = self.snapshot.ignore_stack_for_abs_path(&abs_path, true);
2998        let mut ancestor_inodes = self.snapshot.ancestor_inodes_for_path(&path);
2999
3000        if !ancestor_inodes.contains(&entry.inode) {
3001            ancestor_inodes.insert(entry.inode);
3002            scan_job_tx
3003                .try_send(ScanJob {
3004                    abs_path,
3005                    path,
3006                    ignore_stack,
3007                    scan_queue: scan_job_tx.clone(),
3008                    ancestor_inodes,
3009                    is_external: entry.is_external,
3010                })
3011                .unwrap();
3012        }
3013    }
3014
3015    fn reuse_entry_id(&mut self, entry: &mut Entry) {
3016        if let Some(mtime) = entry.mtime {
3017            // If an entry with the same inode was removed from the worktree during this scan,
3018            // then it *might* represent the same file or directory. But the OS might also have
3019            // re-used the inode for a completely different file or directory.
3020            //
3021            // Conditionally reuse the old entry's id:
3022            // * if the mtime is the same, the file was probably been renamed.
3023            // * if the path is the same, the file may just have been updated
3024            if let Some(removed_entry) = self.removed_entries.remove(&entry.inode) {
3025                if removed_entry.mtime == Some(mtime) || removed_entry.path == entry.path {
3026                    entry.id = removed_entry.id;
3027                }
3028            } else if let Some(existing_entry) = self.snapshot.entry_for_path(&entry.path) {
3029                entry.id = existing_entry.id;
3030            }
3031        }
3032    }
3033
3034    fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs, watcher: &dyn Watcher) -> Entry {
3035        self.reuse_entry_id(&mut entry);
3036        let entry = self.snapshot.insert_entry(entry, fs);
3037        if entry.path.file_name() == Some(&DOT_GIT) {
3038            self.insert_git_repository(entry.path.clone(), fs, watcher);
3039        }
3040
3041        #[cfg(test)]
3042        self.snapshot.check_invariants(false);
3043
3044        entry
3045    }
3046
3047    fn populate_dir(
3048        &mut self,
3049        parent_path: &Arc<Path>,
3050        entries: impl IntoIterator<Item = Entry>,
3051        ignore: Option<Arc<Gitignore>>,
3052    ) {
3053        let mut parent_entry = if let Some(parent_entry) = self
3054            .snapshot
3055            .entries_by_path
3056            .get(&PathKey(parent_path.clone()), &())
3057        {
3058            parent_entry.clone()
3059        } else {
3060            log::warn!(
3061                "populating a directory {:?} that has been removed",
3062                parent_path
3063            );
3064            return;
3065        };
3066
3067        match parent_entry.kind {
3068            EntryKind::PendingDir | EntryKind::UnloadedDir => parent_entry.kind = EntryKind::Dir,
3069            EntryKind::Dir => {}
3070            _ => return,
3071        }
3072
3073        if let Some(ignore) = ignore {
3074            let abs_parent_path = self.snapshot.abs_path.as_path().join(parent_path).into();
3075            self.snapshot
3076                .ignores_by_parent_abs_path
3077                .insert(abs_parent_path, (ignore, false));
3078        }
3079
3080        let parent_entry_id = parent_entry.id;
3081        self.scanned_dirs.insert(parent_entry_id);
3082        let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
3083        let mut entries_by_id_edits = Vec::new();
3084
3085        for entry in entries {
3086            entries_by_id_edits.push(Edit::Insert(PathEntry {
3087                id: entry.id,
3088                path: entry.path.clone(),
3089                is_ignored: entry.is_ignored,
3090                scan_id: self.snapshot.scan_id,
3091            }));
3092            entries_by_path_edits.push(Edit::Insert(entry));
3093        }
3094
3095        self.snapshot
3096            .entries_by_path
3097            .edit(entries_by_path_edits, &());
3098        self.snapshot.entries_by_id.edit(entries_by_id_edits, &());
3099
3100        if let Err(ix) = self.changed_paths.binary_search(parent_path) {
3101            self.changed_paths.insert(ix, parent_path.clone());
3102        }
3103
3104        #[cfg(test)]
3105        self.snapshot.check_invariants(false);
3106    }
3107
3108    fn remove_path(&mut self, path: &Path) {
3109        let mut new_entries;
3110        let removed_entries;
3111        {
3112            let mut cursor = self
3113                .snapshot
3114                .entries_by_path
3115                .cursor::<TraversalProgress>(&());
3116            new_entries = cursor.slice(&TraversalTarget::path(path), Bias::Left, &());
3117            removed_entries = cursor.slice(&TraversalTarget::successor(path), Bias::Left, &());
3118            new_entries.append(cursor.suffix(&()), &());
3119        }
3120        self.snapshot.entries_by_path = new_entries;
3121
3122        let mut removed_ids = Vec::with_capacity(removed_entries.summary().count);
3123        for entry in removed_entries.cursor::<()>(&()) {
3124            match self.removed_entries.entry(entry.inode) {
3125                hash_map::Entry::Occupied(mut e) => {
3126                    let prev_removed_entry = e.get_mut();
3127                    if entry.id > prev_removed_entry.id {
3128                        *prev_removed_entry = entry.clone();
3129                    }
3130                }
3131                hash_map::Entry::Vacant(e) => {
3132                    e.insert(entry.clone());
3133                }
3134            }
3135
3136            if entry.path.file_name() == Some(&GITIGNORE) {
3137                let abs_parent_path = self
3138                    .snapshot
3139                    .abs_path
3140                    .as_path()
3141                    .join(entry.path.parent().unwrap());
3142                if let Some((_, needs_update)) = self
3143                    .snapshot
3144                    .ignores_by_parent_abs_path
3145                    .get_mut(abs_parent_path.as_path())
3146                {
3147                    *needs_update = true;
3148                }
3149            }
3150
3151            if let Err(ix) = removed_ids.binary_search(&entry.id) {
3152                removed_ids.insert(ix, entry.id);
3153            }
3154        }
3155
3156        self.snapshot.entries_by_id.edit(
3157            removed_ids.iter().map(|&id| Edit::Remove(id)).collect(),
3158            &(),
3159        );
3160        self.snapshot
3161            .git_repositories
3162            .retain(|id, _| removed_ids.binary_search(id).is_err());
3163        self.snapshot.repositories.retain(&(), |repository| {
3164            !repository.work_directory.starts_with(path)
3165        });
3166
3167        #[cfg(test)]
3168        self.snapshot.check_invariants(false);
3169    }
3170
3171    fn insert_git_repository(
3172        &mut self,
3173        dot_git_path: Arc<Path>,
3174        fs: &dyn Fs,
3175        watcher: &dyn Watcher,
3176    ) -> Option<LocalRepositoryEntry> {
3177        let work_dir_path: Arc<Path> = match dot_git_path.parent() {
3178            Some(parent_dir) => {
3179                // Guard against repositories inside the repository metadata
3180                if parent_dir.iter().any(|component| component == *DOT_GIT) {
3181                    log::info!(
3182                        "not building git repository for nested `.git` directory, `.git` path in the worktree: {dot_git_path:?}"
3183                    );
3184                    return None;
3185                };
3186                log::info!(
3187                    "building git repository, `.git` path in the worktree: {dot_git_path:?}"
3188                );
3189
3190                parent_dir.into()
3191            }
3192            None => {
3193                // `dot_git_path.parent().is_none()` means `.git` directory is the opened worktree itself,
3194                // no files inside that directory are tracked by git, so no need to build the repo around it
3195                log::info!(
3196                    "not building git repository for the worktree itself, `.git` path in the worktree: {dot_git_path:?}"
3197                );
3198                return None;
3199            }
3200        };
3201
3202        self.insert_git_repository_for_path(work_dir_path, dot_git_path, None, fs, watcher)
3203    }
3204
3205    fn insert_git_repository_for_path(
3206        &mut self,
3207        work_dir_path: Arc<Path>,
3208        dot_git_path: Arc<Path>,
3209        location_in_repo: Option<Arc<Path>>,
3210        fs: &dyn Fs,
3211        watcher: &dyn Watcher,
3212    ) -> Option<LocalRepositoryEntry> {
3213        let work_dir_id = self
3214            .snapshot
3215            .entry_for_path(work_dir_path.clone())
3216            .map(|entry| entry.id)?;
3217
3218        if self.snapshot.git_repositories.get(&work_dir_id).is_some() {
3219            return None;
3220        }
3221
3222        let dot_git_abs_path = self.snapshot.abs_path.as_path().join(&dot_git_path);
3223
3224        let t0 = Instant::now();
3225        let repository = fs.open_repo(&dot_git_abs_path)?;
3226
3227        let actual_repo_path = repository.dot_git_dir();
3228
3229        let actual_dot_git_dir_abs_path = smol::block_on(find_git_dir(&actual_repo_path, fs))?;
3230        watcher.add(&actual_repo_path).log_err()?;
3231
3232        let dot_git_worktree_abs_path = if actual_dot_git_dir_abs_path.as_ref() == dot_git_abs_path
3233        {
3234            None
3235        } else {
3236            // The two paths could be different because we opened a git worktree.
3237            // When that happens, the .git path in the worktree (`dot_git_abs_path`) is a file that
3238            // points to the worktree-subdirectory in the actual .git directory (`git_dir_path`)
3239            watcher.add(&dot_git_abs_path).log_err()?;
3240            Some(Arc::from(dot_git_abs_path))
3241        };
3242
3243        log::trace!("constructed libgit2 repo in {:?}", t0.elapsed());
3244        let work_directory = WorkDirectory {
3245            path: work_dir_path.clone(),
3246            location_in_repo,
3247        };
3248
3249        if let Some(git_hosting_provider_registry) = self.git_hosting_provider_registry.clone() {
3250            git_hosting_providers::register_additional_providers(
3251                git_hosting_provider_registry,
3252                repository.clone(),
3253            );
3254        }
3255
3256        self.snapshot.repositories.insert_or_replace(
3257            RepositoryEntry {
3258                work_directory_id: work_dir_id,
3259                work_directory: work_directory.clone(),
3260                branch: repository.branch_name().map(Into::into),
3261                statuses_by_path: Default::default(),
3262            },
3263            &(),
3264        );
3265
3266        let local_repository = LocalRepositoryEntry {
3267            work_directory: work_directory.clone(),
3268            git_dir_scan_id: 0,
3269            status_scan_id: 0,
3270            repo_ptr: repository.clone(),
3271            dot_git_dir_abs_path: actual_dot_git_dir_abs_path,
3272            dot_git_worktree_abs_path,
3273        };
3274
3275        self.snapshot
3276            .git_repositories
3277            .insert(work_dir_id, local_repository.clone());
3278
3279        Some(local_repository)
3280    }
3281}
3282
3283async fn is_git_dir(path: &Path, fs: &dyn Fs) -> bool {
3284    if path.file_name() == Some(&*DOT_GIT) {
3285        return true;
3286    }
3287
3288    // If we're in a bare repository, we are not inside a `.git` folder. In a
3289    // bare repository, the root folder contains what would normally be in the
3290    // `.git` folder.
3291    let head_metadata = fs.metadata(&path.join("HEAD")).await;
3292    if !matches!(head_metadata, Ok(Some(_))) {
3293        return false;
3294    }
3295    let config_metadata = fs.metadata(&path.join("config")).await;
3296    matches!(config_metadata, Ok(Some(_)))
3297}
3298
3299async fn find_git_dir(path: &Path, fs: &dyn Fs) -> Option<Arc<Path>> {
3300    for ancestor in path.ancestors() {
3301        if is_git_dir(ancestor, fs).await {
3302            return Some(Arc::from(ancestor));
3303        }
3304    }
3305    None
3306}
3307
3308async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
3309    let contents = fs.load(abs_path).await?;
3310    let parent = abs_path.parent().unwrap_or_else(|| Path::new("/"));
3311    let mut builder = GitignoreBuilder::new(parent);
3312    for line in contents.lines() {
3313        builder.add_line(Some(abs_path.into()), line)?;
3314    }
3315    Ok(builder.build()?)
3316}
3317
3318impl Deref for Worktree {
3319    type Target = Snapshot;
3320
3321    fn deref(&self) -> &Self::Target {
3322        match self {
3323            Worktree::Local(worktree) => &worktree.snapshot,
3324            Worktree::Remote(worktree) => &worktree.snapshot,
3325        }
3326    }
3327}
3328
3329impl Deref for LocalWorktree {
3330    type Target = LocalSnapshot;
3331
3332    fn deref(&self) -> &Self::Target {
3333        &self.snapshot
3334    }
3335}
3336
3337impl Deref for RemoteWorktree {
3338    type Target = Snapshot;
3339
3340    fn deref(&self) -> &Self::Target {
3341        &self.snapshot
3342    }
3343}
3344
3345impl fmt::Debug for LocalWorktree {
3346    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3347        self.snapshot.fmt(f)
3348    }
3349}
3350
3351impl fmt::Debug for Snapshot {
3352    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3353        struct EntriesById<'a>(&'a SumTree<PathEntry>);
3354        struct EntriesByPath<'a>(&'a SumTree<Entry>);
3355
3356        impl<'a> fmt::Debug for EntriesByPath<'a> {
3357            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3358                f.debug_map()
3359                    .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
3360                    .finish()
3361            }
3362        }
3363
3364        impl<'a> fmt::Debug for EntriesById<'a> {
3365            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3366                f.debug_list().entries(self.0.iter()).finish()
3367            }
3368        }
3369
3370        f.debug_struct("Snapshot")
3371            .field("id", &self.id)
3372            .field("root_name", &self.root_name)
3373            .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
3374            .field("entries_by_id", &EntriesById(&self.entries_by_id))
3375            .finish()
3376    }
3377}
3378
3379#[derive(Clone, PartialEq)]
3380pub struct File {
3381    pub worktree: Model<Worktree>,
3382    pub path: Arc<Path>,
3383    pub disk_state: DiskState,
3384    pub entry_id: Option<ProjectEntryId>,
3385    pub is_local: bool,
3386    pub is_private: bool,
3387}
3388
3389impl language::File for File {
3390    fn as_local(&self) -> Option<&dyn language::LocalFile> {
3391        if self.is_local {
3392            Some(self)
3393        } else {
3394            None
3395        }
3396    }
3397
3398    fn disk_state(&self) -> DiskState {
3399        self.disk_state
3400    }
3401
3402    fn path(&self) -> &Arc<Path> {
3403        &self.path
3404    }
3405
3406    fn full_path(&self, cx: &AppContext) -> PathBuf {
3407        let mut full_path = PathBuf::new();
3408        let worktree = self.worktree.read(cx);
3409
3410        if worktree.is_visible() {
3411            full_path.push(worktree.root_name());
3412        } else {
3413            let path = worktree.abs_path();
3414
3415            if worktree.is_local() && path.starts_with(home_dir().as_path()) {
3416                full_path.push("~");
3417                full_path.push(path.strip_prefix(home_dir().as_path()).unwrap());
3418            } else {
3419                full_path.push(path)
3420            }
3421        }
3422
3423        if self.path.components().next().is_some() {
3424            full_path.push(&self.path);
3425        }
3426
3427        full_path
3428    }
3429
3430    /// Returns the last component of this handle's absolute path. If this handle refers to the root
3431    /// of its worktree, then this method will return the name of the worktree itself.
3432    fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr {
3433        self.path
3434            .file_name()
3435            .unwrap_or_else(|| OsStr::new(&self.worktree.read(cx).root_name))
3436    }
3437
3438    fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
3439        self.worktree.read(cx).id()
3440    }
3441
3442    fn as_any(&self) -> &dyn Any {
3443        self
3444    }
3445
3446    fn to_proto(&self, cx: &AppContext) -> rpc::proto::File {
3447        rpc::proto::File {
3448            worktree_id: self.worktree.read(cx).id().to_proto(),
3449            entry_id: self.entry_id.map(|id| id.to_proto()),
3450            path: self.path.to_string_lossy().into(),
3451            mtime: self.disk_state.mtime().map(|time| time.into()),
3452            is_deleted: self.disk_state == DiskState::Deleted,
3453        }
3454    }
3455
3456    fn is_private(&self) -> bool {
3457        self.is_private
3458    }
3459}
3460
3461impl language::LocalFile for File {
3462    fn abs_path(&self, cx: &AppContext) -> PathBuf {
3463        let worktree_path = &self.worktree.read(cx).as_local().unwrap().abs_path;
3464        if self.path.as_ref() == Path::new("") {
3465            worktree_path.as_path().to_path_buf()
3466        } else {
3467            worktree_path.as_path().join(&self.path)
3468        }
3469    }
3470
3471    fn load(&self, cx: &AppContext) -> Task<Result<String>> {
3472        let worktree = self.worktree.read(cx).as_local().unwrap();
3473        let abs_path = worktree.absolutize(&self.path);
3474        let fs = worktree.fs.clone();
3475        cx.background_executor()
3476            .spawn(async move { fs.load(&abs_path?).await })
3477    }
3478
3479    fn load_bytes(&self, cx: &AppContext) -> Task<Result<Vec<u8>>> {
3480        let worktree = self.worktree.read(cx).as_local().unwrap();
3481        let abs_path = worktree.absolutize(&self.path);
3482        let fs = worktree.fs.clone();
3483        cx.background_executor()
3484            .spawn(async move { fs.load_bytes(&abs_path?).await })
3485    }
3486}
3487
3488impl File {
3489    pub fn for_entry(entry: Entry, worktree: Model<Worktree>) -> Arc<Self> {
3490        Arc::new(Self {
3491            worktree,
3492            path: entry.path.clone(),
3493            disk_state: if let Some(mtime) = entry.mtime {
3494                DiskState::Present { mtime }
3495            } else {
3496                DiskState::New
3497            },
3498            entry_id: Some(entry.id),
3499            is_local: true,
3500            is_private: entry.is_private,
3501        })
3502    }
3503
3504    pub fn from_proto(
3505        proto: rpc::proto::File,
3506        worktree: Model<Worktree>,
3507        cx: &AppContext,
3508    ) -> Result<Self> {
3509        let worktree_id = worktree
3510            .read(cx)
3511            .as_remote()
3512            .ok_or_else(|| anyhow!("not remote"))?
3513            .id();
3514
3515        if worktree_id.to_proto() != proto.worktree_id {
3516            return Err(anyhow!("worktree id does not match file"));
3517        }
3518
3519        let disk_state = if proto.is_deleted {
3520            DiskState::Deleted
3521        } else {
3522            if let Some(mtime) = proto.mtime.map(&Into::into) {
3523                DiskState::Present { mtime }
3524            } else {
3525                DiskState::New
3526            }
3527        };
3528
3529        Ok(Self {
3530            worktree,
3531            path: Path::new(&proto.path).into(),
3532            disk_state,
3533            entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
3534            is_local: false,
3535            is_private: false,
3536        })
3537    }
3538
3539    pub fn from_dyn(file: Option<&Arc<dyn language::File>>) -> Option<&Self> {
3540        file.and_then(|f| f.as_any().downcast_ref())
3541    }
3542
3543    pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
3544        self.worktree.read(cx).id()
3545    }
3546
3547    pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
3548        match self.disk_state {
3549            DiskState::Deleted => None,
3550            _ => self.entry_id,
3551        }
3552    }
3553}
3554
3555#[derive(Clone, Debug, PartialEq, Eq)]
3556pub struct Entry {
3557    pub id: ProjectEntryId,
3558    pub kind: EntryKind,
3559    pub path: Arc<Path>,
3560    pub inode: u64,
3561    pub mtime: Option<MTime>,
3562
3563    pub canonical_path: Option<Box<Path>>,
3564    /// Whether this entry is ignored by Git.
3565    ///
3566    /// We only scan ignored entries once the directory is expanded and
3567    /// exclude them from searches.
3568    pub is_ignored: bool,
3569
3570    /// Whether this entry is always included in searches.
3571    ///
3572    /// This is used for entries that are always included in searches, even
3573    /// if they are ignored by git. Overridden by file_scan_exclusions.
3574    pub is_always_included: bool,
3575
3576    /// Whether this entry's canonical path is outside of the worktree.
3577    /// This means the entry is only accessible from the worktree root via a
3578    /// symlink.
3579    ///
3580    /// We only scan entries outside of the worktree once the symlinked
3581    /// directory is expanded. External entries are treated like gitignored
3582    /// entries in that they are not included in searches.
3583    pub is_external: bool,
3584
3585    /// Whether this entry is considered to be a `.env` file.
3586    pub is_private: bool,
3587    /// The entry's size on disk, in bytes.
3588    pub size: u64,
3589    pub char_bag: CharBag,
3590    pub is_fifo: bool,
3591}
3592
3593#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3594pub enum EntryKind {
3595    UnloadedDir,
3596    PendingDir,
3597    Dir,
3598    File,
3599}
3600
3601#[derive(Clone, Copy, Debug, PartialEq)]
3602pub enum PathChange {
3603    /// A filesystem entry was was created.
3604    Added,
3605    /// A filesystem entry was removed.
3606    Removed,
3607    /// A filesystem entry was updated.
3608    Updated,
3609    /// A filesystem entry was either updated or added. We don't know
3610    /// whether or not it already existed, because the path had not
3611    /// been loaded before the event.
3612    AddedOrUpdated,
3613    /// A filesystem entry was found during the initial scan of the worktree.
3614    Loaded,
3615}
3616
3617#[derive(Debug)]
3618pub struct GitRepositoryChange {
3619    /// The previous state of the repository, if it already existed.
3620    pub old_repository: Option<RepositoryEntry>,
3621}
3622
3623pub type UpdatedEntriesSet = Arc<[(Arc<Path>, ProjectEntryId, PathChange)]>;
3624pub type UpdatedGitRepositoriesSet = Arc<[(Arc<Path>, GitRepositoryChange)]>;
3625
3626#[derive(Clone, Debug, PartialEq, Eq)]
3627pub struct StatusEntry {
3628    pub repo_path: RepoPath,
3629    pub status: GitFileStatus,
3630}
3631
3632impl StatusEntry {
3633    fn to_proto(&self) -> proto::StatusEntry {
3634        proto::StatusEntry {
3635            repo_path: self.repo_path.to_proto(),
3636            status: git_status_to_proto(self.status),
3637        }
3638    }
3639}
3640
3641impl TryFrom<proto::StatusEntry> for StatusEntry {
3642    type Error = anyhow::Error;
3643    fn try_from(value: proto::StatusEntry) -> Result<Self, Self::Error> {
3644        Ok(Self {
3645            repo_path: RepoPath(Path::new(&value.repo_path).into()),
3646            status: git_status_from_proto(Some(value.status))
3647                .ok_or_else(|| anyhow!("Unable to parse status value {}", value.status))?,
3648        })
3649    }
3650}
3651
3652#[derive(Clone, Debug)]
3653struct PathProgress<'a> {
3654    max_path: &'a Path,
3655}
3656
3657#[derive(Clone, Debug)]
3658pub struct PathSummary<S> {
3659    max_path: Arc<Path>,
3660    item_summary: S,
3661}
3662
3663impl<S: Summary> Summary for PathSummary<S> {
3664    type Context = S::Context;
3665
3666    fn zero(cx: &Self::Context) -> Self {
3667        Self {
3668            max_path: Path::new("").into(),
3669            item_summary: S::zero(cx),
3670        }
3671    }
3672
3673    fn add_summary(&mut self, rhs: &Self, cx: &Self::Context) {
3674        self.max_path = rhs.max_path.clone();
3675        self.item_summary.add_summary(&rhs.item_summary, cx);
3676    }
3677}
3678
3679impl<'a, S: Summary> sum_tree::Dimension<'a, PathSummary<S>> for PathProgress<'a> {
3680    fn zero(_: &<PathSummary<S> as Summary>::Context) -> Self {
3681        Self {
3682            max_path: Path::new(""),
3683        }
3684    }
3685
3686    fn add_summary(
3687        &mut self,
3688        summary: &'a PathSummary<S>,
3689        _: &<PathSummary<S> as Summary>::Context,
3690    ) {
3691        self.max_path = summary.max_path.as_ref()
3692    }
3693}
3694
3695impl sum_tree::Item for RepositoryEntry {
3696    type Summary = PathSummary<Unit>;
3697
3698    fn summary(&self, _: &<Self::Summary as Summary>::Context) -> Self::Summary {
3699        PathSummary {
3700            max_path: self.work_directory.path.clone(),
3701            item_summary: Unit,
3702        }
3703    }
3704}
3705
3706impl sum_tree::KeyedItem for RepositoryEntry {
3707    type Key = PathKey;
3708
3709    fn key(&self) -> Self::Key {
3710        PathKey(self.work_directory.path.clone())
3711    }
3712}
3713
3714impl sum_tree::Summary for GitStatuses {
3715    type Context = ();
3716
3717    fn zero(_: &Self::Context) -> Self {
3718        Default::default()
3719    }
3720
3721    fn add_summary(&mut self, rhs: &Self, _: &Self::Context) {
3722        *self += *rhs;
3723    }
3724}
3725
3726impl sum_tree::Item for StatusEntry {
3727    type Summary = PathSummary<GitStatuses>;
3728
3729    fn summary(&self, _: &<Self::Summary as Summary>::Context) -> Self::Summary {
3730        PathSummary {
3731            max_path: self.repo_path.0.clone(),
3732            item_summary: match self.status {
3733                GitFileStatus::Added => GitStatuses {
3734                    added: 1,
3735                    ..Default::default()
3736                },
3737                GitFileStatus::Modified => GitStatuses {
3738                    modified: 1,
3739                    ..Default::default()
3740                },
3741                GitFileStatus::Conflict => GitStatuses {
3742                    conflict: 1,
3743                    ..Default::default()
3744                },
3745                GitFileStatus::Deleted => Default::default(),
3746                GitFileStatus::Untracked => GitStatuses {
3747                    untracked: 1,
3748                    ..Default::default()
3749                },
3750            },
3751        }
3752    }
3753}
3754
3755impl sum_tree::KeyedItem for StatusEntry {
3756    type Key = PathKey;
3757
3758    fn key(&self) -> Self::Key {
3759        PathKey(self.repo_path.0.clone())
3760    }
3761}
3762
3763#[derive(Clone, Debug, Default, Copy, PartialEq, Eq)]
3764pub struct GitStatuses {
3765    added: usize,
3766    modified: usize,
3767    conflict: usize,
3768    untracked: usize,
3769}
3770
3771impl GitStatuses {
3772    pub fn to_status(&self) -> Option<GitFileStatus> {
3773        if self.conflict > 0 {
3774            Some(GitFileStatus::Conflict)
3775        } else if self.modified > 0 {
3776            Some(GitFileStatus::Modified)
3777        } else if self.added > 0 || self.untracked > 0 {
3778            Some(GitFileStatus::Added)
3779        } else {
3780            None
3781        }
3782    }
3783}
3784
3785impl std::ops::Add<Self> for GitStatuses {
3786    type Output = Self;
3787
3788    fn add(self, rhs: Self) -> Self {
3789        GitStatuses {
3790            added: self.added + rhs.added,
3791            modified: self.modified + rhs.modified,
3792            conflict: self.conflict + rhs.conflict,
3793            untracked: self.untracked + rhs.untracked,
3794        }
3795    }
3796}
3797
3798impl std::ops::AddAssign for GitStatuses {
3799    fn add_assign(&mut self, rhs: Self) {
3800        self.added += rhs.added;
3801        self.modified += rhs.modified;
3802        self.conflict += rhs.conflict;
3803        self.untracked += rhs.untracked;
3804    }
3805}
3806
3807impl std::ops::Sub for GitStatuses {
3808    type Output = GitStatuses;
3809
3810    fn sub(self, rhs: Self) -> Self::Output {
3811        GitStatuses {
3812            added: self.added - rhs.added,
3813            modified: self.modified - rhs.modified,
3814            conflict: self.conflict - rhs.conflict,
3815            untracked: self.untracked - rhs.untracked,
3816        }
3817    }
3818}
3819
3820impl<'a> sum_tree::Dimension<'a, PathSummary<GitStatuses>> for GitStatuses {
3821    fn zero(_cx: &()) -> Self {
3822        Default::default()
3823    }
3824
3825    fn add_summary(&mut self, summary: &'a PathSummary<GitStatuses>, _: &()) {
3826        *self += summary.item_summary
3827    }
3828}
3829
3830impl<'a, S: Summary> sum_tree::Dimension<'a, PathSummary<S>> for PathKey {
3831    fn zero(_: &S::Context) -> Self {
3832        Default::default()
3833    }
3834
3835    fn add_summary(&mut self, summary: &'a PathSummary<S>, _: &S::Context) {
3836        self.0 = summary.max_path.clone();
3837    }
3838}
3839
3840impl<'a, S: Summary> sum_tree::Dimension<'a, PathSummary<S>> for TraversalProgress<'a> {
3841    fn zero(_cx: &S::Context) -> Self {
3842        Default::default()
3843    }
3844
3845    fn add_summary(&mut self, summary: &'a PathSummary<S>, _: &S::Context) {
3846        self.max_path = summary.max_path.as_ref();
3847    }
3848}
3849
3850impl Entry {
3851    fn new(
3852        path: Arc<Path>,
3853        metadata: &fs::Metadata,
3854        next_entry_id: &AtomicUsize,
3855        root_char_bag: CharBag,
3856        canonical_path: Option<Box<Path>>,
3857    ) -> Self {
3858        let char_bag = char_bag_for_path(root_char_bag, &path);
3859        Self {
3860            id: ProjectEntryId::new(next_entry_id),
3861            kind: if metadata.is_dir {
3862                EntryKind::PendingDir
3863            } else {
3864                EntryKind::File
3865            },
3866            path,
3867            inode: metadata.inode,
3868            mtime: Some(metadata.mtime),
3869            size: metadata.len,
3870            canonical_path,
3871            is_ignored: false,
3872            is_always_included: false,
3873            is_external: false,
3874            is_private: false,
3875            char_bag,
3876            is_fifo: metadata.is_fifo,
3877        }
3878    }
3879
3880    pub fn is_created(&self) -> bool {
3881        self.mtime.is_some()
3882    }
3883
3884    pub fn is_dir(&self) -> bool {
3885        self.kind.is_dir()
3886    }
3887
3888    pub fn is_file(&self) -> bool {
3889        self.kind.is_file()
3890    }
3891}
3892
3893impl EntryKind {
3894    pub fn is_dir(&self) -> bool {
3895        matches!(
3896            self,
3897            EntryKind::Dir | EntryKind::PendingDir | EntryKind::UnloadedDir
3898        )
3899    }
3900
3901    pub fn is_unloaded(&self) -> bool {
3902        matches!(self, EntryKind::UnloadedDir)
3903    }
3904
3905    pub fn is_file(&self) -> bool {
3906        matches!(self, EntryKind::File)
3907    }
3908}
3909
3910impl sum_tree::Item for Entry {
3911    type Summary = EntrySummary;
3912
3913    fn summary(&self, _cx: &()) -> Self::Summary {
3914        let non_ignored_count = if (self.is_ignored || self.is_external) && !self.is_always_included
3915        {
3916            0
3917        } else {
3918            1
3919        };
3920        let file_count;
3921        let non_ignored_file_count;
3922        if self.is_file() {
3923            file_count = 1;
3924            non_ignored_file_count = non_ignored_count;
3925        } else {
3926            file_count = 0;
3927            non_ignored_file_count = 0;
3928        }
3929
3930        EntrySummary {
3931            max_path: self.path.clone(),
3932            count: 1,
3933            non_ignored_count,
3934            file_count,
3935            non_ignored_file_count,
3936        }
3937    }
3938}
3939
3940impl sum_tree::KeyedItem for Entry {
3941    type Key = PathKey;
3942
3943    fn key(&self) -> Self::Key {
3944        PathKey(self.path.clone())
3945    }
3946}
3947
3948#[derive(Clone, Debug)]
3949pub struct EntrySummary {
3950    max_path: Arc<Path>,
3951    count: usize,
3952    non_ignored_count: usize,
3953    file_count: usize,
3954    non_ignored_file_count: usize,
3955}
3956
3957impl Default for EntrySummary {
3958    fn default() -> Self {
3959        Self {
3960            max_path: Arc::from(Path::new("")),
3961            count: 0,
3962            non_ignored_count: 0,
3963            file_count: 0,
3964            non_ignored_file_count: 0,
3965        }
3966    }
3967}
3968
3969impl sum_tree::Summary for EntrySummary {
3970    type Context = ();
3971
3972    fn zero(_cx: &()) -> Self {
3973        Default::default()
3974    }
3975
3976    fn add_summary(&mut self, rhs: &Self, _: &()) {
3977        self.max_path = rhs.max_path.clone();
3978        self.count += rhs.count;
3979        self.non_ignored_count += rhs.non_ignored_count;
3980        self.file_count += rhs.file_count;
3981        self.non_ignored_file_count += rhs.non_ignored_file_count;
3982    }
3983}
3984
3985#[derive(Clone, Debug)]
3986struct PathEntry {
3987    id: ProjectEntryId,
3988    path: Arc<Path>,
3989    is_ignored: bool,
3990    scan_id: usize,
3991}
3992
3993impl sum_tree::Item for PathEntry {
3994    type Summary = PathEntrySummary;
3995
3996    fn summary(&self, _cx: &()) -> Self::Summary {
3997        PathEntrySummary { max_id: self.id }
3998    }
3999}
4000
4001impl sum_tree::KeyedItem for PathEntry {
4002    type Key = ProjectEntryId;
4003
4004    fn key(&self) -> Self::Key {
4005        self.id
4006    }
4007}
4008
4009#[derive(Clone, Debug, Default)]
4010struct PathEntrySummary {
4011    max_id: ProjectEntryId,
4012}
4013
4014impl sum_tree::Summary for PathEntrySummary {
4015    type Context = ();
4016
4017    fn zero(_cx: &Self::Context) -> Self {
4018        Default::default()
4019    }
4020
4021    fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
4022        self.max_id = summary.max_id;
4023    }
4024}
4025
4026impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
4027    fn zero(_cx: &()) -> Self {
4028        Default::default()
4029    }
4030
4031    fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
4032        *self = summary.max_id;
4033    }
4034}
4035
4036#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
4037pub struct PathKey(Arc<Path>);
4038
4039impl Default for PathKey {
4040    fn default() -> Self {
4041        Self(Path::new("").into())
4042    }
4043}
4044
4045impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
4046    fn zero(_cx: &()) -> Self {
4047        Default::default()
4048    }
4049
4050    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
4051        self.0 = summary.max_path.clone();
4052    }
4053}
4054
4055struct BackgroundScanner {
4056    state: Mutex<BackgroundScannerState>,
4057    fs: Arc<dyn Fs>,
4058    fs_case_sensitive: bool,
4059    status_updates_tx: UnboundedSender<ScanState>,
4060    executor: BackgroundExecutor,
4061    scan_requests_rx: channel::Receiver<ScanRequest>,
4062    path_prefixes_to_scan_rx: channel::Receiver<Arc<Path>>,
4063    next_entry_id: Arc<AtomicUsize>,
4064    phase: BackgroundScannerPhase,
4065    watcher: Arc<dyn Watcher>,
4066    settings: WorktreeSettings,
4067    share_private_files: bool,
4068}
4069
4070#[derive(PartialEq)]
4071enum BackgroundScannerPhase {
4072    InitialScan,
4073    EventsReceivedDuringInitialScan,
4074    Events,
4075}
4076
4077impl BackgroundScanner {
4078    async fn run(&mut self, mut fs_events_rx: Pin<Box<dyn Send + Stream<Item = Vec<PathEvent>>>>) {
4079        use futures::FutureExt as _;
4080
4081        // If the worktree root does not contain a git repository, then find
4082        // the git repository in an ancestor directory. Find any gitignore files
4083        // in ancestor directories.
4084        let root_abs_path = self.state.lock().snapshot.abs_path.clone();
4085        for (index, ancestor) in root_abs_path.as_path().ancestors().enumerate() {
4086            if index != 0 {
4087                if let Ok(ignore) =
4088                    build_gitignore(&ancestor.join(*GITIGNORE), self.fs.as_ref()).await
4089                {
4090                    self.state
4091                        .lock()
4092                        .snapshot
4093                        .ignores_by_parent_abs_path
4094                        .insert(ancestor.into(), (ignore.into(), false));
4095                }
4096            }
4097
4098            let ancestor_dot_git = ancestor.join(*DOT_GIT);
4099            // Check whether the directory or file called `.git` exists (in the
4100            // case of worktrees it's a file.)
4101            if self
4102                .fs
4103                .metadata(&ancestor_dot_git)
4104                .await
4105                .is_ok_and(|metadata| metadata.is_some())
4106            {
4107                if index != 0 {
4108                    // We canonicalize, since the FS events use the canonicalized path.
4109                    if let Some(ancestor_dot_git) =
4110                        self.fs.canonicalize(&ancestor_dot_git).await.log_err()
4111                    {
4112                        // We associate the external git repo with our root folder and
4113                        // also mark where in the git repo the root folder is located.
4114                        self.state.lock().insert_git_repository_for_path(
4115                            Path::new("").into(),
4116                            ancestor_dot_git.into(),
4117                            Some(
4118                                root_abs_path
4119                                    .as_path()
4120                                    .strip_prefix(ancestor)
4121                                    .unwrap()
4122                                    .into(),
4123                            ),
4124                            self.fs.as_ref(),
4125                            self.watcher.as_ref(),
4126                        );
4127                    };
4128                }
4129
4130                // Reached root of git repository.
4131                break;
4132            }
4133        }
4134
4135        let (scan_job_tx, scan_job_rx) = channel::unbounded();
4136        {
4137            let mut state = self.state.lock();
4138            state.snapshot.scan_id += 1;
4139            if let Some(mut root_entry) = state.snapshot.root_entry().cloned() {
4140                let ignore_stack = state
4141                    .snapshot
4142                    .ignore_stack_for_abs_path(root_abs_path.as_path(), true);
4143                if ignore_stack.is_abs_path_ignored(root_abs_path.as_path(), true) {
4144                    root_entry.is_ignored = true;
4145                    state.insert_entry(root_entry.clone(), self.fs.as_ref(), self.watcher.as_ref());
4146                }
4147                state.enqueue_scan_dir(root_abs_path.into(), &root_entry, &scan_job_tx);
4148            }
4149        };
4150
4151        // Perform an initial scan of the directory.
4152        drop(scan_job_tx);
4153        self.scan_dirs(true, scan_job_rx).await;
4154        {
4155            let mut state = self.state.lock();
4156            state.snapshot.completed_scan_id = state.snapshot.scan_id;
4157        }
4158
4159        self.send_status_update(false, SmallVec::new());
4160
4161        // Process any any FS events that occurred while performing the initial scan.
4162        // For these events, update events cannot be as precise, because we didn't
4163        // have the previous state loaded yet.
4164        self.phase = BackgroundScannerPhase::EventsReceivedDuringInitialScan;
4165        if let Poll::Ready(Some(mut paths)) = futures::poll!(fs_events_rx.next()) {
4166            while let Poll::Ready(Some(more_paths)) = futures::poll!(fs_events_rx.next()) {
4167                paths.extend(more_paths);
4168            }
4169            self.process_events(paths.into_iter().map(Into::into).collect())
4170                .await;
4171        }
4172
4173        // Continue processing events until the worktree is dropped.
4174        self.phase = BackgroundScannerPhase::Events;
4175
4176        loop {
4177            select_biased! {
4178                // Process any path refresh requests from the worktree. Prioritize
4179                // these before handling changes reported by the filesystem.
4180                request = self.next_scan_request().fuse() => {
4181                    let Ok(request) = request else { break };
4182                    if !self.process_scan_request(request, false).await {
4183                        return;
4184                    }
4185                }
4186
4187                path_prefix = self.path_prefixes_to_scan_rx.recv().fuse() => {
4188                    let Ok(path_prefix) = path_prefix else { break };
4189                    log::trace!("adding path prefix {:?}", path_prefix);
4190
4191                    let did_scan = self.forcibly_load_paths(&[path_prefix.clone()]).await;
4192                    if did_scan {
4193                        let abs_path =
4194                        {
4195                            let mut state = self.state.lock();
4196                            state.path_prefixes_to_scan.insert(path_prefix.clone());
4197                            state.snapshot.abs_path.as_path().join(&path_prefix)
4198                        };
4199
4200                        if let Some(abs_path) = self.fs.canonicalize(&abs_path).await.log_err() {
4201                            self.process_events(vec![abs_path]).await;
4202                        }
4203                    }
4204                }
4205
4206                paths = fs_events_rx.next().fuse() => {
4207                    let Some(mut paths) = paths else { break };
4208                    while let Poll::Ready(Some(more_paths)) = futures::poll!(fs_events_rx.next()) {
4209                        paths.extend(more_paths);
4210                    }
4211                    self.process_events(paths.into_iter().map(Into::into).collect()).await;
4212                }
4213            }
4214        }
4215    }
4216
4217    async fn process_scan_request(&self, mut request: ScanRequest, scanning: bool) -> bool {
4218        log::debug!("rescanning paths {:?}", request.relative_paths);
4219
4220        request.relative_paths.sort_unstable();
4221        self.forcibly_load_paths(&request.relative_paths).await;
4222
4223        let root_path = self.state.lock().snapshot.abs_path.clone();
4224        let root_canonical_path = match self.fs.canonicalize(root_path.as_path()).await {
4225            Ok(path) => path,
4226            Err(err) => {
4227                log::error!("failed to canonicalize root path: {}", err);
4228                return true;
4229            }
4230        };
4231        let abs_paths = request
4232            .relative_paths
4233            .iter()
4234            .map(|path| {
4235                if path.file_name().is_some() {
4236                    root_canonical_path.join(path)
4237                } else {
4238                    root_canonical_path.clone()
4239                }
4240            })
4241            .collect::<Vec<_>>();
4242
4243        {
4244            let mut state = self.state.lock();
4245            let is_idle = state.snapshot.completed_scan_id == state.snapshot.scan_id;
4246            state.snapshot.scan_id += 1;
4247            if is_idle {
4248                state.snapshot.completed_scan_id = state.snapshot.scan_id;
4249            }
4250        }
4251
4252        self.reload_entries_for_paths(
4253            root_path.into(),
4254            root_canonical_path,
4255            &request.relative_paths,
4256            abs_paths,
4257            None,
4258        )
4259        .await;
4260
4261        self.send_status_update(scanning, request.done)
4262    }
4263
4264    async fn process_events(&self, mut abs_paths: Vec<PathBuf>) {
4265        let root_path = self.state.lock().snapshot.abs_path.clone();
4266        let root_canonical_path = match self.fs.canonicalize(root_path.as_path()).await {
4267            Ok(path) => path,
4268            Err(err) => {
4269                let new_path = self
4270                    .state
4271                    .lock()
4272                    .snapshot
4273                    .root_file_handle
4274                    .clone()
4275                    .and_then(|handle| handle.current_path(&self.fs).log_err())
4276                    .map(SanitizedPath::from)
4277                    .filter(|new_path| *new_path != root_path);
4278
4279                if let Some(new_path) = new_path.as_ref() {
4280                    log::info!(
4281                        "root renamed from {} to {}",
4282                        root_path.as_path().display(),
4283                        new_path.as_path().display()
4284                    )
4285                } else {
4286                    log::warn!("root path could not be canonicalized: {}", err);
4287                }
4288                self.status_updates_tx
4289                    .unbounded_send(ScanState::RootUpdated { new_path })
4290                    .ok();
4291                return;
4292            }
4293        };
4294
4295        let mut relative_paths = Vec::with_capacity(abs_paths.len());
4296        let mut dot_git_abs_paths = Vec::new();
4297        abs_paths.sort_unstable();
4298        abs_paths.dedup_by(|a, b| a.starts_with(b));
4299        abs_paths.retain(|abs_path| {
4300            let snapshot = &self.state.lock().snapshot;
4301            {
4302                let mut is_git_related = false;
4303
4304                // We don't want to trigger .git rescan for events within .git/fsmonitor--daemon/cookies directory.
4305                #[derive(PartialEq)]
4306                enum FsMonitorParseState {
4307                    Cookies,
4308                    FsMonitor
4309                }
4310                let mut fsmonitor_parse_state = None;
4311                if let Some(dot_git_abs_path) = abs_path
4312                    .ancestors()
4313                    .find(|ancestor| {
4314                        let file_name = ancestor.file_name();
4315                        if file_name == Some(*COOKIES) {
4316                            fsmonitor_parse_state = Some(FsMonitorParseState::Cookies);
4317                            false
4318                        } else if fsmonitor_parse_state == Some(FsMonitorParseState::Cookies) && file_name == Some(*FSMONITOR_DAEMON) {
4319                            fsmonitor_parse_state = Some(FsMonitorParseState::FsMonitor);
4320                            false
4321                        } else if fsmonitor_parse_state != Some(FsMonitorParseState::FsMonitor) && smol::block_on(is_git_dir(ancestor, self.fs.as_ref())) {
4322                            true
4323                        } else {
4324                            fsmonitor_parse_state.take();
4325                            false
4326                        }
4327
4328                    })
4329                {
4330                    let dot_git_abs_path = dot_git_abs_path.to_path_buf();
4331                    if !dot_git_abs_paths.contains(&dot_git_abs_path) {
4332                        dot_git_abs_paths.push(dot_git_abs_path);
4333                    }
4334                    is_git_related = true;
4335                }
4336
4337                let relative_path: Arc<Path> =
4338                    if let Ok(path) = abs_path.strip_prefix(&root_canonical_path) {
4339                        path.into()
4340                    } else {
4341                        if is_git_related {
4342                            log::debug!(
4343                              "ignoring event {abs_path:?}, since it's in git dir outside of root path {root_canonical_path:?}",
4344                            );
4345                        } else {
4346                            log::error!(
4347                              "ignoring event {abs_path:?} outside of root path {root_canonical_path:?}",
4348                            );
4349                        }
4350                        return false;
4351                    };
4352
4353                let parent_dir_is_loaded = relative_path.parent().map_or(true, |parent| {
4354                    snapshot
4355                        .entry_for_path(parent)
4356                        .map_or(false, |entry| entry.kind == EntryKind::Dir)
4357                });
4358                if !parent_dir_is_loaded {
4359                    log::debug!("ignoring event {relative_path:?} within unloaded directory");
4360                    return false;
4361                }
4362
4363                if self.settings.is_path_excluded(&relative_path) {
4364                    if !is_git_related {
4365                        log::debug!("ignoring FS event for excluded path {relative_path:?}");
4366                    }
4367                    return false;
4368                }
4369
4370                relative_paths.push(relative_path);
4371                true
4372            }
4373        });
4374
4375        if relative_paths.is_empty() && dot_git_abs_paths.is_empty() {
4376            return;
4377        }
4378
4379        self.state.lock().snapshot.scan_id += 1;
4380
4381        let (scan_job_tx, scan_job_rx) = channel::unbounded();
4382        log::debug!("received fs events {:?}", relative_paths);
4383        self.reload_entries_for_paths(
4384            root_path.into(),
4385            root_canonical_path,
4386            &relative_paths,
4387            abs_paths,
4388            Some(scan_job_tx.clone()),
4389        )
4390        .await;
4391
4392        self.update_ignore_statuses(scan_job_tx).await;
4393        self.scan_dirs(false, scan_job_rx).await;
4394
4395        if !dot_git_abs_paths.is_empty() {
4396            self.update_git_repositories(dot_git_abs_paths).await;
4397        }
4398
4399        {
4400            let mut state = self.state.lock();
4401            state.snapshot.completed_scan_id = state.snapshot.scan_id;
4402            for (_, entry) in mem::take(&mut state.removed_entries) {
4403                state.scanned_dirs.remove(&entry.id);
4404            }
4405        }
4406
4407        #[cfg(test)]
4408        self.state.lock().snapshot.check_git_invariants();
4409
4410        self.send_status_update(false, SmallVec::new());
4411    }
4412
4413    async fn forcibly_load_paths(&self, paths: &[Arc<Path>]) -> bool {
4414        let (scan_job_tx, mut scan_job_rx) = channel::unbounded();
4415        {
4416            let mut state = self.state.lock();
4417            let root_path = state.snapshot.abs_path.clone();
4418            for path in paths {
4419                for ancestor in path.ancestors() {
4420                    if let Some(entry) = state.snapshot.entry_for_path(ancestor) {
4421                        if entry.kind == EntryKind::UnloadedDir {
4422                            let abs_path = root_path.as_path().join(ancestor);
4423                            state.enqueue_scan_dir(abs_path.into(), entry, &scan_job_tx);
4424                            state.paths_to_scan.insert(path.clone());
4425                            break;
4426                        }
4427                    }
4428                }
4429            }
4430            drop(scan_job_tx);
4431        }
4432        while let Some(job) = scan_job_rx.next().await {
4433            self.scan_dir(&job).await.log_err();
4434        }
4435
4436        !mem::take(&mut self.state.lock().paths_to_scan).is_empty()
4437    }
4438
4439    async fn scan_dirs(
4440        &self,
4441        enable_progress_updates: bool,
4442        scan_jobs_rx: channel::Receiver<ScanJob>,
4443    ) {
4444        use futures::FutureExt as _;
4445
4446        if self
4447            .status_updates_tx
4448            .unbounded_send(ScanState::Started)
4449            .is_err()
4450        {
4451            return;
4452        }
4453
4454        let progress_update_count = AtomicUsize::new(0);
4455        self.executor
4456            .scoped(|scope| {
4457                for _ in 0..self.executor.num_cpus() {
4458                    scope.spawn(async {
4459                        let mut last_progress_update_count = 0;
4460                        let progress_update_timer = self.progress_timer(enable_progress_updates).fuse();
4461                        futures::pin_mut!(progress_update_timer);
4462
4463                        loop {
4464                            select_biased! {
4465                                // Process any path refresh requests before moving on to process
4466                                // the scan queue, so that user operations are prioritized.
4467                                request = self.next_scan_request().fuse() => {
4468                                    let Ok(request) = request else { break };
4469                                    if !self.process_scan_request(request, true).await {
4470                                        return;
4471                                    }
4472                                }
4473
4474                                // Send periodic progress updates to the worktree. Use an atomic counter
4475                                // to ensure that only one of the workers sends a progress update after
4476                                // the update interval elapses.
4477                                _ = progress_update_timer => {
4478                                    match progress_update_count.compare_exchange(
4479                                        last_progress_update_count,
4480                                        last_progress_update_count + 1,
4481                                        SeqCst,
4482                                        SeqCst
4483                                    ) {
4484                                        Ok(_) => {
4485                                            last_progress_update_count += 1;
4486                                            self.send_status_update(true, SmallVec::new());
4487                                        }
4488                                        Err(count) => {
4489                                            last_progress_update_count = count;
4490                                        }
4491                                    }
4492                                    progress_update_timer.set(self.progress_timer(enable_progress_updates).fuse());
4493                                }
4494
4495                                // Recursively load directories from the file system.
4496                                job = scan_jobs_rx.recv().fuse() => {
4497                                    let Ok(job) = job else { break };
4498                                    if let Err(err) = self.scan_dir(&job).await {
4499                                        if job.path.as_ref() != Path::new("") {
4500                                            log::error!("error scanning directory {:?}: {}", job.abs_path, err);
4501                                        }
4502                                    }
4503                                }
4504                            }
4505                        }
4506                    })
4507                }
4508            })
4509            .await;
4510    }
4511
4512    fn send_status_update(&self, scanning: bool, barrier: SmallVec<[barrier::Sender; 1]>) -> bool {
4513        let mut state = self.state.lock();
4514        if state.changed_paths.is_empty() && scanning {
4515            return true;
4516        }
4517
4518        let new_snapshot = state.snapshot.clone();
4519        let old_snapshot = mem::replace(&mut state.prev_snapshot, new_snapshot.snapshot.clone());
4520        let changes = self.build_change_set(&old_snapshot, &new_snapshot, &state.changed_paths);
4521        state.changed_paths.clear();
4522
4523        self.status_updates_tx
4524            .unbounded_send(ScanState::Updated {
4525                snapshot: new_snapshot,
4526                changes,
4527                scanning,
4528                barrier,
4529            })
4530            .is_ok()
4531    }
4532
4533    async fn scan_dir(&self, job: &ScanJob) -> Result<()> {
4534        let root_abs_path;
4535        let root_char_bag;
4536        {
4537            let snapshot = &self.state.lock().snapshot;
4538            if self.settings.is_path_excluded(&job.path) {
4539                log::error!("skipping excluded directory {:?}", job.path);
4540                return Ok(());
4541            }
4542            log::debug!("scanning directory {:?}", job.path);
4543            root_abs_path = snapshot.abs_path().clone();
4544            root_char_bag = snapshot.root_char_bag;
4545        }
4546
4547        let next_entry_id = self.next_entry_id.clone();
4548        let mut ignore_stack = job.ignore_stack.clone();
4549        let mut new_ignore = None;
4550        let mut root_canonical_path = None;
4551        let mut new_entries: Vec<Entry> = Vec::new();
4552        let mut new_jobs: Vec<Option<ScanJob>> = Vec::new();
4553        let mut child_paths = self
4554            .fs
4555            .read_dir(&job.abs_path)
4556            .await?
4557            .filter_map(|entry| async {
4558                match entry {
4559                    Ok(entry) => Some(entry),
4560                    Err(error) => {
4561                        log::error!("error processing entry {:?}", error);
4562                        None
4563                    }
4564                }
4565            })
4566            .collect::<Vec<_>>()
4567            .await;
4568
4569        // Ensure that .git and .gitignore are processed first.
4570        swap_to_front(&mut child_paths, *GITIGNORE);
4571        swap_to_front(&mut child_paths, *DOT_GIT);
4572
4573        for child_abs_path in child_paths {
4574            let child_abs_path: Arc<Path> = child_abs_path.into();
4575            let child_name = child_abs_path.file_name().unwrap();
4576            let child_path: Arc<Path> = job.path.join(child_name).into();
4577
4578            if child_name == *DOT_GIT {
4579                let repo = self.state.lock().insert_git_repository(
4580                    child_path.clone(),
4581                    self.fs.as_ref(),
4582                    self.watcher.as_ref(),
4583                );
4584
4585                if let Some(local_repo) = repo {
4586                    self.update_git_statuses(UpdateGitStatusesJob {
4587                        local_repository: local_repo,
4588                    });
4589                }
4590            } else if child_name == *GITIGNORE {
4591                match build_gitignore(&child_abs_path, self.fs.as_ref()).await {
4592                    Ok(ignore) => {
4593                        let ignore = Arc::new(ignore);
4594                        ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
4595                        new_ignore = Some(ignore);
4596                    }
4597                    Err(error) => {
4598                        log::error!(
4599                            "error loading .gitignore file {:?} - {:?}",
4600                            child_name,
4601                            error
4602                        );
4603                    }
4604                }
4605            }
4606
4607            if self.settings.is_path_excluded(&child_path) {
4608                log::debug!("skipping excluded child entry {child_path:?}");
4609                self.state.lock().remove_path(&child_path);
4610                continue;
4611            }
4612
4613            let child_metadata = match self.fs.metadata(&child_abs_path).await {
4614                Ok(Some(metadata)) => metadata,
4615                Ok(None) => continue,
4616                Err(err) => {
4617                    log::error!("error processing {child_abs_path:?}: {err:?}");
4618                    continue;
4619                }
4620            };
4621
4622            let mut child_entry = Entry::new(
4623                child_path.clone(),
4624                &child_metadata,
4625                &next_entry_id,
4626                root_char_bag,
4627                None,
4628            );
4629
4630            if job.is_external {
4631                child_entry.is_external = true;
4632            } else if child_metadata.is_symlink {
4633                let canonical_path = match self.fs.canonicalize(&child_abs_path).await {
4634                    Ok(path) => path,
4635                    Err(err) => {
4636                        log::error!(
4637                            "error reading target of symlink {:?}: {:?}",
4638                            child_abs_path,
4639                            err
4640                        );
4641                        continue;
4642                    }
4643                };
4644
4645                // lazily canonicalize the root path in order to determine if
4646                // symlinks point outside of the worktree.
4647                let root_canonical_path = match &root_canonical_path {
4648                    Some(path) => path,
4649                    None => match self.fs.canonicalize(&root_abs_path).await {
4650                        Ok(path) => root_canonical_path.insert(path),
4651                        Err(err) => {
4652                            log::error!("error canonicalizing root {:?}: {:?}", root_abs_path, err);
4653                            continue;
4654                        }
4655                    },
4656                };
4657
4658                if !canonical_path.starts_with(root_canonical_path) {
4659                    child_entry.is_external = true;
4660                }
4661
4662                child_entry.canonical_path = Some(canonical_path.into());
4663            }
4664
4665            if child_entry.is_dir() {
4666                child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true);
4667                child_entry.is_always_included = self.settings.is_path_always_included(&child_path);
4668
4669                // Avoid recursing until crash in the case of a recursive symlink
4670                if job.ancestor_inodes.contains(&child_entry.inode) {
4671                    new_jobs.push(None);
4672                } else {
4673                    let mut ancestor_inodes = job.ancestor_inodes.clone();
4674                    ancestor_inodes.insert(child_entry.inode);
4675
4676                    new_jobs.push(Some(ScanJob {
4677                        abs_path: child_abs_path.clone(),
4678                        path: child_path,
4679                        is_external: child_entry.is_external,
4680                        ignore_stack: if child_entry.is_ignored {
4681                            IgnoreStack::all()
4682                        } else {
4683                            ignore_stack.clone()
4684                        },
4685                        ancestor_inodes,
4686                        scan_queue: job.scan_queue.clone(),
4687                    }));
4688                }
4689            } else {
4690                child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false);
4691                child_entry.is_always_included = self.settings.is_path_always_included(&child_path);
4692            }
4693
4694            {
4695                let relative_path = job.path.join(child_name);
4696                if self.is_path_private(&relative_path) {
4697                    log::debug!("detected private file: {relative_path:?}");
4698                    child_entry.is_private = true;
4699                }
4700            }
4701
4702            new_entries.push(child_entry);
4703        }
4704
4705        let mut state = self.state.lock();
4706
4707        // Identify any subdirectories that should not be scanned.
4708        let mut job_ix = 0;
4709        for entry in &mut new_entries {
4710            state.reuse_entry_id(entry);
4711            if entry.is_dir() {
4712                if state.should_scan_directory(entry) {
4713                    job_ix += 1;
4714                } else {
4715                    log::debug!("defer scanning directory {:?}", entry.path);
4716                    entry.kind = EntryKind::UnloadedDir;
4717                    new_jobs.remove(job_ix);
4718                }
4719            }
4720            if entry.is_always_included {
4721                state
4722                    .snapshot
4723                    .always_included_entries
4724                    .push(entry.path.clone());
4725            }
4726        }
4727
4728        state.populate_dir(&job.path, new_entries, new_ignore);
4729        self.watcher.add(job.abs_path.as_ref()).log_err();
4730
4731        for new_job in new_jobs.into_iter().flatten() {
4732            job.scan_queue
4733                .try_send(new_job)
4734                .expect("channel is unbounded");
4735        }
4736
4737        Ok(())
4738    }
4739
4740    /// All list arguments should be sorted before calling this function
4741    async fn reload_entries_for_paths(
4742        &self,
4743        root_abs_path: Arc<Path>,
4744        root_canonical_path: PathBuf,
4745        relative_paths: &[Arc<Path>],
4746        abs_paths: Vec<PathBuf>,
4747        scan_queue_tx: Option<Sender<ScanJob>>,
4748    ) {
4749        // grab metadata for all requested paths
4750        let metadata = futures::future::join_all(
4751            abs_paths
4752                .iter()
4753                .map(|abs_path| async move {
4754                    let metadata = self.fs.metadata(abs_path).await?;
4755                    if let Some(metadata) = metadata {
4756                        let canonical_path = self.fs.canonicalize(abs_path).await?;
4757
4758                        // If we're on a case-insensitive filesystem (default on macOS), we want
4759                        // to only ignore metadata for non-symlink files if their absolute-path matches
4760                        // the canonical-path.
4761                        // Because if not, this might be a case-only-renaming (`mv test.txt TEST.TXT`)
4762                        // and we want to ignore the metadata for the old path (`test.txt`) so it's
4763                        // treated as removed.
4764                        if !self.fs_case_sensitive && !metadata.is_symlink {
4765                            let canonical_file_name = canonical_path.file_name();
4766                            let file_name = abs_path.file_name();
4767                            if canonical_file_name != file_name {
4768                                return Ok(None);
4769                            }
4770                        }
4771
4772                        anyhow::Ok(Some((metadata, canonical_path)))
4773                    } else {
4774                        Ok(None)
4775                    }
4776                })
4777                .collect::<Vec<_>>(),
4778        )
4779        .await;
4780
4781        let mut state = self.state.lock();
4782        let doing_recursive_update = scan_queue_tx.is_some();
4783
4784        // Remove any entries for paths that no longer exist or are being recursively
4785        // refreshed. Do this before adding any new entries, so that renames can be
4786        // detected regardless of the order of the paths.
4787        for (path, metadata) in relative_paths.iter().zip(metadata.iter()) {
4788            if matches!(metadata, Ok(None)) || doing_recursive_update {
4789                log::trace!("remove path {:?}", path);
4790                state.remove_path(path);
4791            }
4792        }
4793
4794        // Group all relative paths by their git repository.
4795        let mut paths_by_git_repo = HashMap::default();
4796        for relative_path in relative_paths.iter() {
4797            let repository_data = state
4798                .snapshot
4799                .local_repo_for_path(relative_path)
4800                .zip(state.snapshot.repository_for_path(relative_path));
4801            if let Some((local_repo, entry)) = repository_data {
4802                if let Ok(repo_path) = local_repo.relativize(relative_path) {
4803                    paths_by_git_repo
4804                        .entry(local_repo.work_directory.clone())
4805                        .or_insert_with(|| RepoPaths {
4806                            entry: entry.clone(),
4807                            repo: local_repo.repo_ptr.clone(),
4808                            repo_paths: Default::default(),
4809                        })
4810                        .add_path(repo_path);
4811                }
4812            }
4813        }
4814
4815        for (work_directory, mut paths) in paths_by_git_repo {
4816            if let Ok(status) = paths.repo.status(&paths.repo_paths) {
4817                let mut changed_path_statuses = Vec::new();
4818                let statuses = paths.entry.statuses_by_path.clone();
4819                let mut cursor = statuses.cursor::<PathProgress>(&());
4820
4821                for (repo_path, status) in &*status.entries {
4822                    paths.remove_repo_path(repo_path);
4823                    if cursor.seek_forward(&PathTarget::Path(&repo_path), Bias::Left, &()) {
4824                        if cursor.item().unwrap().status == *status {
4825                            continue;
4826                        }
4827                    }
4828
4829                    changed_path_statuses.push(Edit::Insert(StatusEntry {
4830                        repo_path: repo_path.clone(),
4831                        status: *status,
4832                    }));
4833                }
4834
4835                let mut cursor = statuses.cursor::<PathProgress>(&());
4836                for path in paths.repo_paths {
4837                    if cursor.seek_forward(&PathTarget::Path(&path), Bias::Left, &()) {
4838                        changed_path_statuses.push(Edit::Remove(PathKey(path.0)));
4839                    }
4840                }
4841
4842                if !changed_path_statuses.is_empty() {
4843                    let work_directory_id = state.snapshot.repositories.update(
4844                        &work_directory.path_key(),
4845                        &(),
4846                        move |repository_entry| {
4847                            repository_entry
4848                                .statuses_by_path
4849                                .edit(changed_path_statuses, &());
4850
4851                            repository_entry.work_directory_id
4852                        },
4853                    );
4854
4855                    if let Some(work_directory_id) = work_directory_id {
4856                        let scan_id = state.snapshot.scan_id;
4857                        state.snapshot.git_repositories.update(
4858                            &work_directory_id,
4859                            |local_repository_entry| {
4860                                local_repository_entry.status_scan_id = scan_id;
4861                            },
4862                        );
4863                    }
4864                }
4865            }
4866        }
4867
4868        for (path, metadata) in relative_paths.iter().zip(metadata.into_iter()) {
4869            let abs_path: Arc<Path> = root_abs_path.join(path).into();
4870            match metadata {
4871                Ok(Some((metadata, canonical_path))) => {
4872                    let ignore_stack = state
4873                        .snapshot
4874                        .ignore_stack_for_abs_path(&abs_path, metadata.is_dir);
4875                    let is_external = !canonical_path.starts_with(&root_canonical_path);
4876                    let mut fs_entry = Entry::new(
4877                        path.clone(),
4878                        &metadata,
4879                        self.next_entry_id.as_ref(),
4880                        state.snapshot.root_char_bag,
4881                        if metadata.is_symlink {
4882                            Some(canonical_path.into())
4883                        } else {
4884                            None
4885                        },
4886                    );
4887
4888                    let is_dir = fs_entry.is_dir();
4889                    fs_entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, is_dir);
4890                    fs_entry.is_external = is_external;
4891                    fs_entry.is_private = self.is_path_private(path);
4892                    fs_entry.is_always_included = self.settings.is_path_always_included(path);
4893
4894                    if let (Some(scan_queue_tx), true) = (&scan_queue_tx, is_dir) {
4895                        if state.should_scan_directory(&fs_entry)
4896                            || (fs_entry.path.as_os_str().is_empty()
4897                                && abs_path.file_name() == Some(*DOT_GIT))
4898                        {
4899                            state.enqueue_scan_dir(abs_path, &fs_entry, scan_queue_tx);
4900                        } else {
4901                            fs_entry.kind = EntryKind::UnloadedDir;
4902                        }
4903                    }
4904
4905                    state.insert_entry(fs_entry.clone(), self.fs.as_ref(), self.watcher.as_ref());
4906                }
4907                Ok(None) => {
4908                    self.remove_repo_path(path, &mut state.snapshot);
4909                }
4910                Err(err) => {
4911                    log::error!("error reading file {abs_path:?} on event: {err:#}");
4912                }
4913            }
4914        }
4915
4916        util::extend_sorted(
4917            &mut state.changed_paths,
4918            relative_paths.iter().cloned(),
4919            usize::MAX,
4920            Ord::cmp,
4921        );
4922    }
4923
4924    fn remove_repo_path(&self, path: &Arc<Path>, snapshot: &mut LocalSnapshot) -> Option<()> {
4925        if !path
4926            .components()
4927            .any(|component| component.as_os_str() == *DOT_GIT)
4928        {
4929            if let Some(repository) = snapshot.repository(PathKey(path.clone())) {
4930                snapshot
4931                    .git_repositories
4932                    .remove(&repository.work_directory_id);
4933                snapshot
4934                    .snapshot
4935                    .repositories
4936                    .remove(&PathKey(repository.work_directory.path.clone()), &());
4937                return Some(());
4938            }
4939        }
4940
4941        Some(())
4942    }
4943
4944    async fn update_ignore_statuses(&self, scan_job_tx: Sender<ScanJob>) {
4945        use futures::FutureExt as _;
4946
4947        let mut ignores_to_update = Vec::new();
4948        let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
4949        let prev_snapshot;
4950        {
4951            let snapshot = &mut self.state.lock().snapshot;
4952            let abs_path = snapshot.abs_path.clone();
4953            snapshot
4954                .ignores_by_parent_abs_path
4955                .retain(|parent_abs_path, (_, needs_update)| {
4956                    if let Ok(parent_path) = parent_abs_path.strip_prefix(abs_path.as_path()) {
4957                        if *needs_update {
4958                            *needs_update = false;
4959                            if snapshot.snapshot.entry_for_path(parent_path).is_some() {
4960                                ignores_to_update.push(parent_abs_path.clone());
4961                            }
4962                        }
4963
4964                        let ignore_path = parent_path.join(*GITIGNORE);
4965                        if snapshot.snapshot.entry_for_path(ignore_path).is_none() {
4966                            return false;
4967                        }
4968                    }
4969                    true
4970                });
4971
4972            ignores_to_update.sort_unstable();
4973            let mut ignores_to_update = ignores_to_update.into_iter().peekable();
4974            while let Some(parent_abs_path) = ignores_to_update.next() {
4975                while ignores_to_update
4976                    .peek()
4977                    .map_or(false, |p| p.starts_with(&parent_abs_path))
4978                {
4979                    ignores_to_update.next().unwrap();
4980                }
4981
4982                let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true);
4983                ignore_queue_tx
4984                    .send_blocking(UpdateIgnoreStatusJob {
4985                        abs_path: parent_abs_path,
4986                        ignore_stack,
4987                        ignore_queue: ignore_queue_tx.clone(),
4988                        scan_queue: scan_job_tx.clone(),
4989                    })
4990                    .unwrap();
4991            }
4992
4993            prev_snapshot = snapshot.clone();
4994        }
4995        drop(ignore_queue_tx);
4996
4997        self.executor
4998            .scoped(|scope| {
4999                for _ in 0..self.executor.num_cpus() {
5000                    scope.spawn(async {
5001                        loop {
5002                            select_biased! {
5003                                // Process any path refresh requests before moving on to process
5004                                // the queue of ignore statuses.
5005                                request = self.next_scan_request().fuse() => {
5006                                    let Ok(request) = request else { break };
5007                                    if !self.process_scan_request(request, true).await {
5008                                        return;
5009                                    }
5010                                }
5011
5012                                // Recursively process directories whose ignores have changed.
5013                                job = ignore_queue_rx.recv().fuse() => {
5014                                    let Ok(job) = job else { break };
5015                                    self.update_ignore_status(job, &prev_snapshot).await;
5016                                }
5017                            }
5018                        }
5019                    });
5020                }
5021            })
5022            .await;
5023    }
5024
5025    async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
5026        log::trace!("update ignore status {:?}", job.abs_path);
5027
5028        let mut ignore_stack = job.ignore_stack;
5029        if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) {
5030            ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
5031        }
5032
5033        let mut entries_by_id_edits = Vec::new();
5034        let mut entries_by_path_edits = Vec::new();
5035        let path = job
5036            .abs_path
5037            .strip_prefix(snapshot.abs_path.as_path())
5038            .unwrap();
5039
5040        for mut entry in snapshot.child_entries(path).cloned() {
5041            let was_ignored = entry.is_ignored;
5042            let abs_path: Arc<Path> = snapshot.abs_path().join(&entry.path).into();
5043            entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir());
5044
5045            if entry.is_dir() {
5046                let child_ignore_stack = if entry.is_ignored {
5047                    IgnoreStack::all()
5048                } else {
5049                    ignore_stack.clone()
5050                };
5051
5052                // Scan any directories that were previously ignored and weren't previously scanned.
5053                if was_ignored && !entry.is_ignored && entry.kind.is_unloaded() {
5054                    let state = self.state.lock();
5055                    if state.should_scan_directory(&entry) {
5056                        state.enqueue_scan_dir(abs_path.clone(), &entry, &job.scan_queue);
5057                    }
5058                }
5059
5060                job.ignore_queue
5061                    .send(UpdateIgnoreStatusJob {
5062                        abs_path: abs_path.clone(),
5063                        ignore_stack: child_ignore_stack,
5064                        ignore_queue: job.ignore_queue.clone(),
5065                        scan_queue: job.scan_queue.clone(),
5066                    })
5067                    .await
5068                    .unwrap();
5069            }
5070
5071            if entry.is_ignored != was_ignored {
5072                let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
5073                path_entry.scan_id = snapshot.scan_id;
5074                path_entry.is_ignored = entry.is_ignored;
5075                entries_by_id_edits.push(Edit::Insert(path_entry));
5076                entries_by_path_edits.push(Edit::Insert(entry));
5077            }
5078        }
5079
5080        let state = &mut self.state.lock();
5081        for edit in &entries_by_path_edits {
5082            if let Edit::Insert(entry) = edit {
5083                if let Err(ix) = state.changed_paths.binary_search(&entry.path) {
5084                    state.changed_paths.insert(ix, entry.path.clone());
5085                }
5086            }
5087        }
5088
5089        state
5090            .snapshot
5091            .entries_by_path
5092            .edit(entries_by_path_edits, &());
5093        state.snapshot.entries_by_id.edit(entries_by_id_edits, &());
5094    }
5095
5096    async fn update_git_repositories(&self, dot_git_paths: Vec<PathBuf>) {
5097        log::debug!("reloading repositories: {dot_git_paths:?}");
5098
5099        let mut repo_updates = Vec::new();
5100        {
5101            let mut state = self.state.lock();
5102            let scan_id = state.snapshot.scan_id;
5103            for dot_git_dir in dot_git_paths {
5104                let existing_repository_entry =
5105                    state
5106                        .snapshot
5107                        .git_repositories
5108                        .iter()
5109                        .find_map(|(entry_id, repo)| {
5110                            if repo.dot_git_dir_abs_path.as_ref() == &dot_git_dir
5111                                || repo.dot_git_worktree_abs_path.as_deref() == Some(&dot_git_dir)
5112                            {
5113                                Some((*entry_id, repo.clone()))
5114                            } else {
5115                                None
5116                            }
5117                        });
5118
5119                let local_repository = match existing_repository_entry {
5120                    None => {
5121                        match state.insert_git_repository(
5122                            dot_git_dir.into(),
5123                            self.fs.as_ref(),
5124                            self.watcher.as_ref(),
5125                        ) {
5126                            Some(output) => output,
5127                            None => continue,
5128                        }
5129                    }
5130                    Some((entry_id, local_repository)) => {
5131                        if local_repository.git_dir_scan_id == scan_id {
5132                            continue;
5133                        }
5134                        let Some(work_dir) = state
5135                            .snapshot
5136                            .entry_for_id(entry_id)
5137                            .map(|entry| entry.path.clone())
5138                        else {
5139                            continue;
5140                        };
5141
5142                        let branch = local_repository.repo_ptr.branch_name();
5143                        local_repository.repo_ptr.reload_index();
5144
5145                        state.snapshot.git_repositories.update(&entry_id, |entry| {
5146                            entry.git_dir_scan_id = scan_id;
5147                            entry.status_scan_id = scan_id;
5148                        });
5149                        state.snapshot.snapshot.repositories.update(
5150                            &PathKey(work_dir.clone()),
5151                            &(),
5152                            |entry| entry.branch = branch.map(Into::into),
5153                        );
5154
5155                        local_repository
5156                    }
5157                };
5158
5159                repo_updates.push(UpdateGitStatusesJob { local_repository });
5160            }
5161
5162            // Remove any git repositories whose .git entry no longer exists.
5163            let snapshot = &mut state.snapshot;
5164            let mut ids_to_preserve = HashSet::default();
5165            for (&work_directory_id, entry) in snapshot.git_repositories.iter() {
5166                let exists_in_snapshot = snapshot
5167                    .entry_for_id(work_directory_id)
5168                    .map_or(false, |entry| {
5169                        snapshot.entry_for_path(entry.path.join(*DOT_GIT)).is_some()
5170                    });
5171
5172                if exists_in_snapshot
5173                    || matches!(
5174                        smol::block_on(self.fs.metadata(&entry.dot_git_dir_abs_path)),
5175                        Ok(Some(_))
5176                    )
5177                {
5178                    ids_to_preserve.insert(work_directory_id);
5179                }
5180            }
5181
5182            snapshot
5183                .git_repositories
5184                .retain(|work_directory_id, _| ids_to_preserve.contains(work_directory_id));
5185            snapshot.repositories.retain(&(), |entry| {
5186                ids_to_preserve.contains(&entry.work_directory_id)
5187            });
5188        }
5189
5190        let (mut updates_done_tx, mut updates_done_rx) = barrier::channel();
5191        self.executor
5192            .scoped(|scope| {
5193                scope.spawn(async {
5194                    for repo_update in repo_updates {
5195                        self.update_git_statuses(repo_update);
5196                    }
5197                    updates_done_tx.blocking_send(()).ok();
5198                });
5199
5200                scope.spawn(async {
5201                    loop {
5202                        select_biased! {
5203                            // Process any path refresh requests before moving on to process
5204                            // the queue of git statuses.
5205                            request = self.next_scan_request().fuse() => {
5206                                let Ok(request) = request else { break };
5207                                if !self.process_scan_request(request, true).await {
5208                                    return;
5209                                }
5210                            }
5211                            _ = updates_done_rx.recv().fuse() =>  break,
5212                        }
5213                    }
5214                });
5215            })
5216            .await;
5217    }
5218
5219    /// Update the git statuses for a given batch of entries.
5220    fn update_git_statuses(&self, job: UpdateGitStatusesJob) {
5221        log::trace!(
5222            "updating git statuses for repo {:?}",
5223            job.local_repository.work_directory.path
5224        );
5225        let t0 = Instant::now();
5226
5227        let Some(statuses) = job
5228            .local_repository
5229            .repo()
5230            .status(&[git::WORK_DIRECTORY_REPO_PATH.clone()])
5231            .log_err()
5232        else {
5233            return;
5234        };
5235        log::trace!(
5236            "computed git statuses for repo {:?} in {:?}",
5237            job.local_repository.work_directory.path,
5238            t0.elapsed()
5239        );
5240
5241        let t0 = Instant::now();
5242        let mut changed_paths = Vec::new();
5243        let snapshot = self.state.lock().snapshot.snapshot.clone();
5244
5245        let Some(mut repository) =
5246            snapshot.repository(job.local_repository.work_directory.path_key())
5247        else {
5248            log::error!("Got an UpdateGitStatusesJob for a repository that isn't in the snapshot");
5249            debug_assert!(false);
5250            return;
5251        };
5252
5253        let mut new_entries_by_path = SumTree::new(&());
5254        for (repo_path, status) in statuses.entries.iter() {
5255            let project_path = repository.work_directory.unrelativize(repo_path);
5256
5257            new_entries_by_path.insert_or_replace(
5258                StatusEntry {
5259                    repo_path: repo_path.clone(),
5260                    status: *status,
5261                },
5262                &(),
5263            );
5264
5265            if let Some(path) = project_path {
5266                changed_paths.push(path);
5267            }
5268        }
5269
5270        repository.statuses_by_path = new_entries_by_path;
5271        let mut state = self.state.lock();
5272        state
5273            .snapshot
5274            .repositories
5275            .insert_or_replace(repository, &());
5276
5277        util::extend_sorted(
5278            &mut state.changed_paths,
5279            changed_paths,
5280            usize::MAX,
5281            Ord::cmp,
5282        );
5283
5284        log::trace!(
5285            "applied git status updates for repo {:?} in {:?}",
5286            job.local_repository.work_directory.path,
5287            t0.elapsed(),
5288        );
5289    }
5290
5291    fn build_change_set(
5292        &self,
5293        old_snapshot: &Snapshot,
5294        new_snapshot: &Snapshot,
5295        event_paths: &[Arc<Path>],
5296    ) -> UpdatedEntriesSet {
5297        use BackgroundScannerPhase::*;
5298        use PathChange::{Added, AddedOrUpdated, Loaded, Removed, Updated};
5299
5300        // Identify which paths have changed. Use the known set of changed
5301        // parent paths to optimize the search.
5302        let mut changes = Vec::new();
5303        let mut old_paths = old_snapshot.entries_by_path.cursor::<PathKey>(&());
5304        let mut new_paths = new_snapshot.entries_by_path.cursor::<PathKey>(&());
5305        let mut last_newly_loaded_dir_path = None;
5306        old_paths.next(&());
5307        new_paths.next(&());
5308        for path in event_paths {
5309            let path = PathKey(path.clone());
5310            if old_paths.item().map_or(false, |e| e.path < path.0) {
5311                old_paths.seek_forward(&path, Bias::Left, &());
5312            }
5313            if new_paths.item().map_or(false, |e| e.path < path.0) {
5314                new_paths.seek_forward(&path, Bias::Left, &());
5315            }
5316            loop {
5317                match (old_paths.item(), new_paths.item()) {
5318                    (Some(old_entry), Some(new_entry)) => {
5319                        if old_entry.path > path.0
5320                            && new_entry.path > path.0
5321                            && !old_entry.path.starts_with(&path.0)
5322                            && !new_entry.path.starts_with(&path.0)
5323                        {
5324                            break;
5325                        }
5326
5327                        match Ord::cmp(&old_entry.path, &new_entry.path) {
5328                            Ordering::Less => {
5329                                changes.push((old_entry.path.clone(), old_entry.id, Removed));
5330                                old_paths.next(&());
5331                            }
5332                            Ordering::Equal => {
5333                                if self.phase == EventsReceivedDuringInitialScan {
5334                                    if old_entry.id != new_entry.id {
5335                                        changes.push((
5336                                            old_entry.path.clone(),
5337                                            old_entry.id,
5338                                            Removed,
5339                                        ));
5340                                    }
5341                                    // If the worktree was not fully initialized when this event was generated,
5342                                    // we can't know whether this entry was added during the scan or whether
5343                                    // it was merely updated.
5344                                    changes.push((
5345                                        new_entry.path.clone(),
5346                                        new_entry.id,
5347                                        AddedOrUpdated,
5348                                    ));
5349                                } else if old_entry.id != new_entry.id {
5350                                    changes.push((old_entry.path.clone(), old_entry.id, Removed));
5351                                    changes.push((new_entry.path.clone(), new_entry.id, Added));
5352                                } else if old_entry != new_entry {
5353                                    if old_entry.kind.is_unloaded() {
5354                                        last_newly_loaded_dir_path = Some(&new_entry.path);
5355                                        changes.push((
5356                                            new_entry.path.clone(),
5357                                            new_entry.id,
5358                                            Loaded,
5359                                        ));
5360                                    } else {
5361                                        changes.push((
5362                                            new_entry.path.clone(),
5363                                            new_entry.id,
5364                                            Updated,
5365                                        ));
5366                                    }
5367                                }
5368                                old_paths.next(&());
5369                                new_paths.next(&());
5370                            }
5371                            Ordering::Greater => {
5372                                let is_newly_loaded = self.phase == InitialScan
5373                                    || last_newly_loaded_dir_path
5374                                        .as_ref()
5375                                        .map_or(false, |dir| new_entry.path.starts_with(dir));
5376                                changes.push((
5377                                    new_entry.path.clone(),
5378                                    new_entry.id,
5379                                    if is_newly_loaded { Loaded } else { Added },
5380                                ));
5381                                new_paths.next(&());
5382                            }
5383                        }
5384                    }
5385                    (Some(old_entry), None) => {
5386                        changes.push((old_entry.path.clone(), old_entry.id, Removed));
5387                        old_paths.next(&());
5388                    }
5389                    (None, Some(new_entry)) => {
5390                        let is_newly_loaded = self.phase == InitialScan
5391                            || last_newly_loaded_dir_path
5392                                .as_ref()
5393                                .map_or(false, |dir| new_entry.path.starts_with(dir));
5394                        changes.push((
5395                            new_entry.path.clone(),
5396                            new_entry.id,
5397                            if is_newly_loaded { Loaded } else { Added },
5398                        ));
5399                        new_paths.next(&());
5400                    }
5401                    (None, None) => break,
5402                }
5403            }
5404        }
5405
5406        changes.into()
5407    }
5408
5409    async fn progress_timer(&self, running: bool) {
5410        if !running {
5411            return futures::future::pending().await;
5412        }
5413
5414        #[cfg(any(test, feature = "test-support"))]
5415        if self.fs.is_fake() {
5416            return self.executor.simulate_random_delay().await;
5417        }
5418
5419        smol::Timer::after(FS_WATCH_LATENCY).await;
5420    }
5421
5422    fn is_path_private(&self, path: &Path) -> bool {
5423        !self.share_private_files && self.settings.is_path_private(path)
5424    }
5425
5426    async fn next_scan_request(&self) -> Result<ScanRequest> {
5427        let mut request = self.scan_requests_rx.recv().await?;
5428        while let Ok(next_request) = self.scan_requests_rx.try_recv() {
5429            request.relative_paths.extend(next_request.relative_paths);
5430            request.done.extend(next_request.done);
5431        }
5432        Ok(request)
5433    }
5434}
5435
5436fn swap_to_front(child_paths: &mut Vec<PathBuf>, file: &OsStr) {
5437    let position = child_paths
5438        .iter()
5439        .position(|path| path.file_name().unwrap() == file);
5440    if let Some(position) = position {
5441        let temp = child_paths.remove(position);
5442        child_paths.insert(0, temp);
5443    }
5444}
5445
5446fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
5447    let mut result = root_char_bag;
5448    result.extend(
5449        path.to_string_lossy()
5450            .chars()
5451            .map(|c| c.to_ascii_lowercase()),
5452    );
5453    result
5454}
5455
5456#[derive(Debug)]
5457struct RepoPaths {
5458    repo: Arc<dyn GitRepository>,
5459    entry: RepositoryEntry,
5460    // sorted
5461    repo_paths: Vec<RepoPath>,
5462}
5463
5464impl RepoPaths {
5465    fn add_path(&mut self, repo_path: RepoPath) {
5466        match self.repo_paths.binary_search(&repo_path) {
5467            Ok(_) => {}
5468            Err(ix) => self.repo_paths.insert(ix, repo_path),
5469        }
5470    }
5471
5472    fn remove_repo_path(&mut self, repo_path: &RepoPath) {
5473        match self.repo_paths.binary_search(&repo_path) {
5474            Ok(ix) => {
5475                self.repo_paths.remove(ix);
5476            }
5477            Err(_) => {}
5478        }
5479    }
5480}
5481
5482struct ScanJob {
5483    abs_path: Arc<Path>,
5484    path: Arc<Path>,
5485    ignore_stack: Arc<IgnoreStack>,
5486    scan_queue: Sender<ScanJob>,
5487    ancestor_inodes: TreeSet<u64>,
5488    is_external: bool,
5489}
5490
5491struct UpdateIgnoreStatusJob {
5492    abs_path: Arc<Path>,
5493    ignore_stack: Arc<IgnoreStack>,
5494    ignore_queue: Sender<UpdateIgnoreStatusJob>,
5495    scan_queue: Sender<ScanJob>,
5496}
5497
5498struct UpdateGitStatusesJob {
5499    local_repository: LocalRepositoryEntry,
5500}
5501
5502pub trait WorktreeModelHandle {
5503    #[cfg(any(test, feature = "test-support"))]
5504    fn flush_fs_events<'a>(
5505        &self,
5506        cx: &'a mut gpui::TestAppContext,
5507    ) -> futures::future::LocalBoxFuture<'a, ()>;
5508
5509    #[cfg(any(test, feature = "test-support"))]
5510    fn flush_fs_events_in_root_git_repository<'a>(
5511        &self,
5512        cx: &'a mut gpui::TestAppContext,
5513    ) -> futures::future::LocalBoxFuture<'a, ()>;
5514}
5515
5516impl WorktreeModelHandle for Model<Worktree> {
5517    // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
5518    // occurred before the worktree was constructed. These events can cause the worktree to perform
5519    // extra directory scans, and emit extra scan-state notifications.
5520    //
5521    // This function mutates the worktree's directory and waits for those mutations to be picked up,
5522    // to ensure that all redundant FS events have already been processed.
5523    #[cfg(any(test, feature = "test-support"))]
5524    fn flush_fs_events<'a>(
5525        &self,
5526        cx: &'a mut gpui::TestAppContext,
5527    ) -> futures::future::LocalBoxFuture<'a, ()> {
5528        let file_name = "fs-event-sentinel";
5529
5530        let tree = self.clone();
5531        let (fs, root_path) = self.update(cx, |tree, _| {
5532            let tree = tree.as_local().unwrap();
5533            (tree.fs.clone(), tree.abs_path().clone())
5534        });
5535
5536        async move {
5537            fs.create_file(&root_path.join(file_name), Default::default())
5538                .await
5539                .unwrap();
5540
5541            cx.condition(&tree, |tree, _| tree.entry_for_path(file_name).is_some())
5542                .await;
5543
5544            fs.remove_file(&root_path.join(file_name), Default::default())
5545                .await
5546                .unwrap();
5547            cx.condition(&tree, |tree, _| tree.entry_for_path(file_name).is_none())
5548                .await;
5549
5550            cx.update(|cx| tree.read(cx).as_local().unwrap().scan_complete())
5551                .await;
5552        }
5553        .boxed_local()
5554    }
5555
5556    // This function is similar to flush_fs_events, except that it waits for events to be flushed in
5557    // the .git folder of the root repository.
5558    // The reason for its existence is that a repository's .git folder might live *outside* of the
5559    // worktree and thus its FS events might go through a different path.
5560    // In order to flush those, we need to create artificial events in the .git folder and wait
5561    // for the repository to be reloaded.
5562    #[cfg(any(test, feature = "test-support"))]
5563    fn flush_fs_events_in_root_git_repository<'a>(
5564        &self,
5565        cx: &'a mut gpui::TestAppContext,
5566    ) -> futures::future::LocalBoxFuture<'a, ()> {
5567        let file_name = "fs-event-sentinel";
5568
5569        let tree = self.clone();
5570        let (fs, root_path, mut git_dir_scan_id) = self.update(cx, |tree, _| {
5571            let tree = tree.as_local().unwrap();
5572            let root_entry = tree.root_git_entry().unwrap();
5573            let local_repo_entry = tree.get_local_repo(&root_entry).unwrap();
5574            (
5575                tree.fs.clone(),
5576                local_repo_entry.dot_git_dir_abs_path.clone(),
5577                local_repo_entry.git_dir_scan_id,
5578            )
5579        });
5580
5581        let scan_id_increased = |tree: &mut Worktree, git_dir_scan_id: &mut usize| {
5582            let root_entry = tree.root_git_entry().unwrap();
5583            let local_repo_entry = tree
5584                .as_local()
5585                .unwrap()
5586                .get_local_repo(&root_entry)
5587                .unwrap();
5588
5589            if local_repo_entry.git_dir_scan_id > *git_dir_scan_id {
5590                *git_dir_scan_id = local_repo_entry.git_dir_scan_id;
5591                true
5592            } else {
5593                false
5594            }
5595        };
5596
5597        async move {
5598            fs.create_file(&root_path.join(file_name), Default::default())
5599                .await
5600                .unwrap();
5601
5602            cx.condition(&tree, |tree, _| {
5603                scan_id_increased(tree, &mut git_dir_scan_id)
5604            })
5605            .await;
5606
5607            fs.remove_file(&root_path.join(file_name), Default::default())
5608                .await
5609                .unwrap();
5610
5611            cx.condition(&tree, |tree, _| {
5612                scan_id_increased(tree, &mut git_dir_scan_id)
5613            })
5614            .await;
5615
5616            cx.update(|cx| tree.read(cx).as_local().unwrap().scan_complete())
5617                .await;
5618        }
5619        .boxed_local()
5620    }
5621}
5622
5623#[derive(Clone, Debug)]
5624struct TraversalProgress<'a> {
5625    max_path: &'a Path,
5626    count: usize,
5627    non_ignored_count: usize,
5628    file_count: usize,
5629    non_ignored_file_count: usize,
5630}
5631
5632impl<'a> TraversalProgress<'a> {
5633    fn count(&self, include_files: bool, include_dirs: bool, include_ignored: bool) -> usize {
5634        match (include_files, include_dirs, include_ignored) {
5635            (true, true, true) => self.count,
5636            (true, true, false) => self.non_ignored_count,
5637            (true, false, true) => self.file_count,
5638            (true, false, false) => self.non_ignored_file_count,
5639            (false, true, true) => self.count - self.file_count,
5640            (false, true, false) => self.non_ignored_count - self.non_ignored_file_count,
5641            (false, false, _) => 0,
5642        }
5643    }
5644}
5645
5646impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
5647    fn zero(_cx: &()) -> Self {
5648        Default::default()
5649    }
5650
5651    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
5652        self.max_path = summary.max_path.as_ref();
5653        self.count += summary.count;
5654        self.non_ignored_count += summary.non_ignored_count;
5655        self.file_count += summary.file_count;
5656        self.non_ignored_file_count += summary.non_ignored_file_count;
5657    }
5658}
5659
5660impl<'a> Default for TraversalProgress<'a> {
5661    fn default() -> Self {
5662        Self {
5663            max_path: Path::new(""),
5664            count: 0,
5665            non_ignored_count: 0,
5666            file_count: 0,
5667            non_ignored_file_count: 0,
5668        }
5669    }
5670}
5671
5672#[derive(Debug, Clone, Copy)]
5673pub struct GitEntryRef<'a> {
5674    pub entry: &'a Entry,
5675    pub git_status: Option<GitFileStatus>,
5676}
5677
5678impl<'a> GitEntryRef<'a> {
5679    pub fn to_owned(&self) -> GitEntry {
5680        GitEntry {
5681            entry: self.entry.clone(),
5682            git_status: self.git_status,
5683        }
5684    }
5685}
5686
5687impl<'a> Deref for GitEntryRef<'a> {
5688    type Target = Entry;
5689
5690    fn deref(&self) -> &Self::Target {
5691        &self.entry
5692    }
5693}
5694
5695impl<'a> AsRef<Entry> for GitEntryRef<'a> {
5696    fn as_ref(&self) -> &Entry {
5697        self.entry
5698    }
5699}
5700
5701#[derive(Debug, Clone, PartialEq, Eq)]
5702pub struct GitEntry {
5703    pub entry: Entry,
5704    pub git_status: Option<GitFileStatus>,
5705}
5706
5707impl GitEntry {
5708    pub fn to_ref(&self) -> GitEntryRef {
5709        GitEntryRef {
5710            entry: &self.entry,
5711            git_status: self.git_status,
5712        }
5713    }
5714}
5715
5716impl Deref for GitEntry {
5717    type Target = Entry;
5718
5719    fn deref(&self) -> &Self::Target {
5720        &self.entry
5721    }
5722}
5723
5724impl AsRef<Entry> for GitEntry {
5725    fn as_ref(&self) -> &Entry {
5726        &self.entry
5727    }
5728}
5729
5730/// Walks the worktree entries and their associated git statuses.
5731pub struct GitTraversal<'a> {
5732    traversal: Traversal<'a>,
5733    current_entry_status: Option<GitFileStatus>,
5734    repo_location: Option<(
5735        &'a RepositoryEntry,
5736        Cursor<'a, StatusEntry, PathProgress<'a>>,
5737    )>,
5738}
5739
5740impl<'a> GitTraversal<'a> {
5741    fn synchronize_statuses(&mut self, reset: bool) {
5742        self.current_entry_status = None;
5743
5744        let Some(entry) = self.traversal.cursor.item() else {
5745            return;
5746        };
5747
5748        let Some(repo) = self.traversal.snapshot.repository_for_path(&entry.path) else {
5749            self.repo_location = None;
5750            return;
5751        };
5752
5753        // Update our state if we changed repositories.
5754        if reset || self.repo_location.as_ref().map(|(prev_repo, _)| prev_repo) != Some(&repo) {
5755            self.repo_location = Some((repo, repo.statuses_by_path.cursor::<PathProgress>(&())));
5756        }
5757
5758        let Some((repo, statuses)) = &mut self.repo_location else {
5759            return;
5760        };
5761
5762        let repo_path = repo.relativize(&entry.path).unwrap();
5763
5764        if entry.is_dir() {
5765            let mut statuses = statuses.clone();
5766            statuses.seek_forward(&PathTarget::Path(repo_path.as_ref()), Bias::Left, &());
5767            let summary: GitStatuses =
5768                statuses.summary(&PathTarget::Successor(repo_path.as_ref()), Bias::Left, &());
5769
5770            self.current_entry_status = summary.to_status();
5771        } else if entry.is_file() {
5772            // For a file entry, park the cursor on the corresponding status
5773            if statuses.seek_forward(&PathTarget::Path(repo_path.as_ref()), Bias::Left, &()) {
5774                self.current_entry_status = Some(statuses.item().unwrap().status);
5775            }
5776        }
5777    }
5778
5779    pub fn advance(&mut self) -> bool {
5780        self.advance_by(1)
5781    }
5782
5783    pub fn advance_by(&mut self, count: usize) -> bool {
5784        let found = self.traversal.advance_by(count);
5785        self.synchronize_statuses(false);
5786        found
5787    }
5788
5789    pub fn advance_to_sibling(&mut self) -> bool {
5790        let found = self.traversal.advance_to_sibling();
5791        self.synchronize_statuses(false);
5792        found
5793    }
5794
5795    pub fn back_to_parent(&mut self) -> bool {
5796        let found = self.traversal.back_to_parent();
5797        self.synchronize_statuses(true);
5798        found
5799    }
5800
5801    pub fn start_offset(&self) -> usize {
5802        self.traversal.start_offset()
5803    }
5804
5805    pub fn end_offset(&self) -> usize {
5806        self.traversal.end_offset()
5807    }
5808
5809    pub fn entry(&self) -> Option<GitEntryRef<'a>> {
5810        Some(GitEntryRef {
5811            entry: self.traversal.cursor.item()?,
5812            git_status: self.current_entry_status,
5813        })
5814    }
5815}
5816
5817impl<'a> Iterator for GitTraversal<'a> {
5818    type Item = GitEntryRef<'a>;
5819    fn next(&mut self) -> Option<Self::Item> {
5820        if let Some(item) = self.entry() {
5821            self.advance();
5822            Some(item)
5823        } else {
5824            None
5825        }
5826    }
5827}
5828
5829#[derive(Debug)]
5830pub struct Traversal<'a> {
5831    snapshot: &'a Snapshot,
5832    cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
5833    include_ignored: bool,
5834    include_files: bool,
5835    include_dirs: bool,
5836}
5837
5838impl<'a> Traversal<'a> {
5839    fn new(
5840        snapshot: &'a Snapshot,
5841        include_files: bool,
5842        include_dirs: bool,
5843        include_ignored: bool,
5844        start_path: &Path,
5845    ) -> Self {
5846        let mut cursor = snapshot.entries_by_path.cursor(&());
5847        cursor.seek(&TraversalTarget::path(start_path), Bias::Left, &());
5848        let mut traversal = Self {
5849            snapshot,
5850            cursor,
5851            include_files,
5852            include_dirs,
5853            include_ignored,
5854        };
5855        if traversal.end_offset() == traversal.start_offset() {
5856            traversal.next();
5857        }
5858        traversal
5859    }
5860
5861    pub fn with_git_statuses(self) -> GitTraversal<'a> {
5862        let mut this = GitTraversal {
5863            traversal: self,
5864            current_entry_status: None,
5865            repo_location: None,
5866        };
5867        this.synchronize_statuses(true);
5868        this
5869    }
5870
5871    pub fn advance(&mut self) -> bool {
5872        self.advance_by(1)
5873    }
5874
5875    pub fn advance_by(&mut self, count: usize) -> bool {
5876        self.cursor.seek_forward(
5877            &TraversalTarget::Count {
5878                count: self.end_offset() + count,
5879                include_dirs: self.include_dirs,
5880                include_files: self.include_files,
5881                include_ignored: self.include_ignored,
5882            },
5883            Bias::Left,
5884            &(),
5885        )
5886    }
5887
5888    pub fn advance_to_sibling(&mut self) -> bool {
5889        while let Some(entry) = self.cursor.item() {
5890            self.cursor
5891                .seek_forward(&TraversalTarget::successor(&entry.path), Bias::Left, &());
5892            if let Some(entry) = self.cursor.item() {
5893                if (self.include_files || !entry.is_file())
5894                    && (self.include_dirs || !entry.is_dir())
5895                    && (self.include_ignored || !entry.is_ignored || entry.is_always_included)
5896                {
5897                    return true;
5898                }
5899            }
5900        }
5901        false
5902    }
5903
5904    pub fn back_to_parent(&mut self) -> bool {
5905        let Some(parent_path) = self.cursor.item().and_then(|entry| entry.path.parent()) else {
5906            return false;
5907        };
5908        self.cursor
5909            .seek(&TraversalTarget::path(parent_path), Bias::Left, &())
5910    }
5911
5912    pub fn entry(&self) -> Option<&'a Entry> {
5913        self.cursor.item()
5914    }
5915
5916    pub fn start_offset(&self) -> usize {
5917        self.cursor
5918            .start()
5919            .count(self.include_files, self.include_dirs, self.include_ignored)
5920    }
5921
5922    pub fn end_offset(&self) -> usize {
5923        self.cursor
5924            .end(&())
5925            .count(self.include_files, self.include_dirs, self.include_ignored)
5926    }
5927}
5928
5929impl<'a> Iterator for Traversal<'a> {
5930    type Item = &'a Entry;
5931
5932    fn next(&mut self) -> Option<Self::Item> {
5933        if let Some(item) = self.entry() {
5934            self.advance();
5935            Some(item)
5936        } else {
5937            None
5938        }
5939    }
5940}
5941
5942#[derive(Debug, Clone, Copy)]
5943enum PathTarget<'a> {
5944    Path(&'a Path),
5945    Successor(&'a Path),
5946    Contains(&'a Path),
5947}
5948
5949impl<'a> PathTarget<'a> {
5950    fn cmp_path(&self, other: &Path) -> Ordering {
5951        match self {
5952            PathTarget::Path(path) => path.cmp(&other),
5953            PathTarget::Successor(path) => {
5954                if other.starts_with(path) {
5955                    Ordering::Greater
5956                } else {
5957                    Ordering::Equal
5958                }
5959            }
5960            PathTarget::Contains(path) => {
5961                if path.starts_with(other) {
5962                    Ordering::Equal
5963                } else {
5964                    Ordering::Greater
5965                }
5966            }
5967        }
5968    }
5969}
5970
5971impl<'a, 'b, S: Summary> SeekTarget<'a, PathSummary<S>, PathProgress<'a>> for PathTarget<'b> {
5972    fn cmp(&self, cursor_location: &PathProgress<'a>, _: &S::Context) -> Ordering {
5973        self.cmp_path(&cursor_location.max_path)
5974    }
5975}
5976
5977impl<'a, 'b, S: Summary> SeekTarget<'a, PathSummary<S>, TraversalProgress<'a>> for PathTarget<'b> {
5978    fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &S::Context) -> Ordering {
5979        self.cmp_path(&cursor_location.max_path)
5980    }
5981}
5982
5983impl<'a, 'b> SeekTarget<'a, PathSummary<GitStatuses>, (TraversalProgress<'a>, GitStatuses)>
5984    for PathTarget<'b>
5985{
5986    fn cmp(&self, cursor_location: &(TraversalProgress<'a>, GitStatuses), _: &()) -> Ordering {
5987        self.cmp_path(&cursor_location.0.max_path)
5988    }
5989}
5990
5991#[derive(Debug)]
5992enum TraversalTarget<'a> {
5993    Path(PathTarget<'a>),
5994    Count {
5995        count: usize,
5996        include_files: bool,
5997        include_ignored: bool,
5998        include_dirs: bool,
5999    },
6000}
6001
6002impl<'a> TraversalTarget<'a> {
6003    fn path(path: &'a Path) -> Self {
6004        Self::Path(PathTarget::Path(path))
6005    }
6006
6007    fn successor(path: &'a Path) -> Self {
6008        Self::Path(PathTarget::Successor(path))
6009    }
6010
6011    fn cmp_progress(&self, progress: &TraversalProgress) -> Ordering {
6012        match self {
6013            TraversalTarget::Path(path) => path.cmp_path(&progress.max_path),
6014            TraversalTarget::Count {
6015                count,
6016                include_files,
6017                include_dirs,
6018                include_ignored,
6019            } => Ord::cmp(
6020                count,
6021                &progress.count(*include_files, *include_dirs, *include_ignored),
6022            ),
6023        }
6024    }
6025}
6026
6027impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
6028    fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
6029        self.cmp_progress(cursor_location)
6030    }
6031}
6032
6033impl<'a, 'b> SeekTarget<'a, PathSummary<Unit>, TraversalProgress<'a>> for TraversalTarget<'b> {
6034    fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
6035        self.cmp_progress(cursor_location)
6036    }
6037}
6038
6039pub struct ChildEntriesIter<'a> {
6040    parent_path: &'a Path,
6041    traversal: Traversal<'a>,
6042}
6043
6044impl<'a> ChildEntriesIter<'a> {
6045    pub fn with_git_statuses(self) -> ChildEntriesGitIter<'a> {
6046        ChildEntriesGitIter {
6047            parent_path: self.parent_path,
6048            traversal: self.traversal.with_git_statuses(),
6049        }
6050    }
6051}
6052
6053pub struct ChildEntriesGitIter<'a> {
6054    parent_path: &'a Path,
6055    traversal: GitTraversal<'a>,
6056}
6057
6058impl<'a> Iterator for ChildEntriesIter<'a> {
6059    type Item = &'a Entry;
6060
6061    fn next(&mut self) -> Option<Self::Item> {
6062        if let Some(item) = self.traversal.entry() {
6063            if item.path.starts_with(self.parent_path) {
6064                self.traversal.advance_to_sibling();
6065                return Some(item);
6066            }
6067        }
6068        None
6069    }
6070}
6071
6072impl<'a> Iterator for ChildEntriesGitIter<'a> {
6073    type Item = GitEntryRef<'a>;
6074
6075    fn next(&mut self) -> Option<Self::Item> {
6076        if let Some(item) = self.traversal.entry() {
6077            if item.path.starts_with(self.parent_path) {
6078                self.traversal.advance_to_sibling();
6079                return Some(item);
6080            }
6081        }
6082        None
6083    }
6084}
6085
6086impl<'a> From<&'a Entry> for proto::Entry {
6087    fn from(entry: &'a Entry) -> Self {
6088        Self {
6089            id: entry.id.to_proto(),
6090            is_dir: entry.is_dir(),
6091            path: entry.path.to_string_lossy().into(),
6092            inode: entry.inode,
6093            mtime: entry.mtime.map(|time| time.into()),
6094            is_ignored: entry.is_ignored,
6095            is_external: entry.is_external,
6096            is_fifo: entry.is_fifo,
6097            size: Some(entry.size),
6098            canonical_path: entry
6099                .canonical_path
6100                .as_ref()
6101                .map(|path| path.to_string_lossy().to_string()),
6102        }
6103    }
6104}
6105
6106impl<'a> TryFrom<(&'a CharBag, &PathMatcher, proto::Entry)> for Entry {
6107    type Error = anyhow::Error;
6108
6109    fn try_from(
6110        (root_char_bag, always_included, entry): (&'a CharBag, &PathMatcher, proto::Entry),
6111    ) -> Result<Self> {
6112        let kind = if entry.is_dir {
6113            EntryKind::Dir
6114        } else {
6115            EntryKind::File
6116        };
6117        let path: Arc<Path> = PathBuf::from(entry.path).into();
6118        let char_bag = char_bag_for_path(*root_char_bag, &path);
6119        Ok(Entry {
6120            id: ProjectEntryId::from_proto(entry.id),
6121            kind,
6122            path: path.clone(),
6123            inode: entry.inode,
6124            mtime: entry.mtime.map(|time| time.into()),
6125            size: entry.size.unwrap_or(0),
6126            canonical_path: entry
6127                .canonical_path
6128                .map(|path_string| Box::from(Path::new(&path_string))),
6129            is_ignored: entry.is_ignored,
6130            is_always_included: always_included.is_match(path.as_ref()),
6131            is_external: entry.is_external,
6132            is_private: false,
6133            char_bag,
6134            is_fifo: entry.is_fifo,
6135        })
6136    }
6137}
6138
6139fn git_status_from_proto(git_status: Option<i32>) -> Option<GitFileStatus> {
6140    git_status.and_then(|status| {
6141        proto::GitStatus::from_i32(status).map(|status| match status {
6142            proto::GitStatus::Added => GitFileStatus::Added,
6143            proto::GitStatus::Modified => GitFileStatus::Modified,
6144            proto::GitStatus::Conflict => GitFileStatus::Conflict,
6145            proto::GitStatus::Deleted => GitFileStatus::Deleted,
6146        })
6147    })
6148}
6149
6150fn git_status_to_proto(status: GitFileStatus) -> i32 {
6151    match status {
6152        GitFileStatus::Added => proto::GitStatus::Added as i32,
6153        GitFileStatus::Modified => proto::GitStatus::Modified as i32,
6154        GitFileStatus::Conflict => proto::GitStatus::Conflict as i32,
6155        GitFileStatus::Deleted => proto::GitStatus::Deleted as i32,
6156        GitFileStatus::Untracked => proto::GitStatus::Added as i32, // TODO
6157    }
6158}
6159
6160#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
6161pub struct ProjectEntryId(usize);
6162
6163impl ProjectEntryId {
6164    pub const MAX: Self = Self(usize::MAX);
6165    pub const MIN: Self = Self(usize::MIN);
6166
6167    pub fn new(counter: &AtomicUsize) -> Self {
6168        Self(counter.fetch_add(1, SeqCst))
6169    }
6170
6171    pub fn from_proto(id: u64) -> Self {
6172        Self(id as usize)
6173    }
6174
6175    pub fn to_proto(&self) -> u64 {
6176        self.0 as u64
6177    }
6178
6179    pub fn to_usize(&self) -> usize {
6180        self.0
6181    }
6182}
6183
6184#[cfg(any(test, feature = "test-support"))]
6185impl CreatedEntry {
6186    pub fn to_included(self) -> Option<Entry> {
6187        match self {
6188            CreatedEntry::Included(entry) => Some(entry),
6189            CreatedEntry::Excluded { .. } => None,
6190        }
6191    }
6192}