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