worktree.rs

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