worktree.rs

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