worktree.rs

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