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