worktree.rs

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