worktree.rs

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