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