worktree.rs

   1use crate::{ProjectEntryId, RemoveOptions};
   2
   3use super::{
   4    fs::{self, Fs},
   5    ignore::IgnoreStack,
   6    DiagnosticSummary,
   7};
   8use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
   9use anyhow::{anyhow, Context, Result};
  10use client::{proto, Client, TypedEnvelope};
  11use clock::ReplicaId;
  12use collections::HashMap;
  13use futures::{
  14    channel::{
  15        mpsc::{self, UnboundedSender},
  16        oneshot,
  17    },
  18    Stream, StreamExt,
  19};
  20use fuzzy::CharBag;
  21use gpui::{
  22    executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
  23    Task,
  24};
  25use language::{
  26    proto::{deserialize_version, serialize_version},
  27    Buffer, DiagnosticEntry, PointUtf16, Rope,
  28};
  29use lazy_static::lazy_static;
  30use parking_lot::Mutex;
  31use postage::{
  32    prelude::{Sink as _, Stream as _},
  33    watch,
  34};
  35use smol::channel::{self, Sender};
  36use std::{
  37    any::Any,
  38    cmp::{self, Ordering},
  39    convert::TryFrom,
  40    ffi::{OsStr, OsString},
  41    fmt,
  42    future::Future,
  43    mem,
  44    ops::{Deref, DerefMut},
  45    os::unix::prelude::{OsStrExt, OsStringExt},
  46    path::{Path, PathBuf},
  47    sync::{atomic::AtomicUsize, Arc},
  48    time::{Duration, SystemTime},
  49};
  50use sum_tree::{Bias, Edit, SeekTarget, SumTree, TreeMap};
  51use util::{ResultExt, TryFutureExt};
  52
  53lazy_static! {
  54    static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
  55}
  56
  57#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
  58pub struct WorktreeId(usize);
  59
  60pub enum Worktree {
  61    Local(LocalWorktree),
  62    Remote(RemoteWorktree),
  63}
  64
  65pub struct LocalWorktree {
  66    snapshot: LocalSnapshot,
  67    background_snapshot: Arc<Mutex<LocalSnapshot>>,
  68    last_scan_state_rx: watch::Receiver<ScanState>,
  69    _background_scanner_task: Option<Task<()>>,
  70    poll_task: Option<Task<()>>,
  71    share: Option<ShareState>,
  72    diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
  73    diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
  74    client: Arc<Client>,
  75    fs: Arc<dyn Fs>,
  76    visible: bool,
  77}
  78
  79pub struct RemoteWorktree {
  80    pub snapshot: Snapshot,
  81    pub(crate) background_snapshot: Arc<Mutex<Snapshot>>,
  82    project_id: u64,
  83    client: Arc<Client>,
  84    updates_tx: UnboundedSender<proto::UpdateWorktree>,
  85    last_scan_id_rx: watch::Receiver<usize>,
  86    replica_id: ReplicaId,
  87    diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
  88    visible: bool,
  89}
  90
  91#[derive(Clone)]
  92pub struct Snapshot {
  93    id: WorktreeId,
  94    root_name: String,
  95    root_char_bag: CharBag,
  96    entries_by_path: SumTree<Entry>,
  97    entries_by_id: SumTree<PathEntry>,
  98    scan_id: usize,
  99}
 100
 101#[derive(Clone)]
 102pub struct LocalSnapshot {
 103    abs_path: Arc<Path>,
 104    ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
 105    removed_entry_ids: HashMap<u64, ProjectEntryId>,
 106    next_entry_id: Arc<AtomicUsize>,
 107    snapshot: Snapshot,
 108}
 109
 110impl Deref for LocalSnapshot {
 111    type Target = Snapshot;
 112
 113    fn deref(&self) -> &Self::Target {
 114        &self.snapshot
 115    }
 116}
 117
 118impl DerefMut for LocalSnapshot {
 119    fn deref_mut(&mut self) -> &mut Self::Target {
 120        &mut self.snapshot
 121    }
 122}
 123
 124#[derive(Clone, Debug)]
 125enum ScanState {
 126    Idle,
 127    Scanning,
 128    Err(Arc<anyhow::Error>),
 129}
 130
 131struct ShareState {
 132    project_id: u64,
 133    snapshots_tx: Sender<LocalSnapshot>,
 134    _maintain_remote_snapshot: Option<Task<Option<()>>>,
 135}
 136
 137pub enum Event {
 138    UpdatedEntries,
 139}
 140
 141impl Entity for Worktree {
 142    type Event = Event;
 143}
 144
 145impl Worktree {
 146    pub async fn local(
 147        client: Arc<Client>,
 148        path: impl Into<Arc<Path>>,
 149        visible: bool,
 150        fs: Arc<dyn Fs>,
 151        next_entry_id: Arc<AtomicUsize>,
 152        cx: &mut AsyncAppContext,
 153    ) -> Result<ModelHandle<Self>> {
 154        let (tree, scan_states_tx) =
 155            LocalWorktree::new(client, path, visible, fs.clone(), next_entry_id, cx).await?;
 156        tree.update(cx, |tree, cx| {
 157            let tree = tree.as_local_mut().unwrap();
 158            let abs_path = tree.abs_path().clone();
 159            let background_snapshot = tree.background_snapshot.clone();
 160            let background = cx.background().clone();
 161            tree._background_scanner_task = Some(cx.background().spawn(async move {
 162                let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
 163                let scanner =
 164                    BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
 165                scanner.run(events).await;
 166            }));
 167        });
 168        Ok(tree)
 169    }
 170
 171    pub fn remote(
 172        project_remote_id: u64,
 173        replica_id: ReplicaId,
 174        worktree: proto::Worktree,
 175        client: Arc<Client>,
 176        cx: &mut MutableAppContext,
 177    ) -> (ModelHandle<Self>, Task<()>) {
 178        let remote_id = worktree.id;
 179        let root_char_bag: CharBag = worktree
 180            .root_name
 181            .chars()
 182            .map(|c| c.to_ascii_lowercase())
 183            .collect();
 184        let root_name = worktree.root_name.clone();
 185        let visible = worktree.visible;
 186        let snapshot = Snapshot {
 187            id: WorktreeId(remote_id as usize),
 188            root_name,
 189            root_char_bag,
 190            entries_by_path: Default::default(),
 191            entries_by_id: Default::default(),
 192            scan_id: worktree.scan_id as usize,
 193        };
 194
 195        let (updates_tx, mut updates_rx) = mpsc::unbounded();
 196        let background_snapshot = Arc::new(Mutex::new(snapshot.clone()));
 197        let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
 198        let (mut last_scan_id_tx, last_scan_id_rx) = watch::channel_with(worktree.scan_id as usize);
 199        let worktree_handle = cx.add_model(|_: &mut ModelContext<Worktree>| {
 200            Worktree::Remote(RemoteWorktree {
 201                project_id: project_remote_id,
 202                replica_id,
 203                snapshot: snapshot.clone(),
 204                background_snapshot: background_snapshot.clone(),
 205                updates_tx,
 206                last_scan_id_rx,
 207                client: client.clone(),
 208                diagnostic_summaries: TreeMap::from_ordered_entries(
 209                    worktree.diagnostic_summaries.into_iter().map(|summary| {
 210                        (
 211                            PathKey(PathBuf::from(summary.path).into()),
 212                            DiagnosticSummary {
 213                                error_count: summary.error_count as usize,
 214                                warning_count: summary.warning_count as usize,
 215                            },
 216                        )
 217                    }),
 218                ),
 219                visible,
 220            })
 221        });
 222
 223        let deserialize_task = cx.spawn({
 224            let worktree_handle = worktree_handle.clone();
 225            |cx| async move {
 226                let (entries_by_path, entries_by_id) = cx
 227                    .background()
 228                    .spawn(async move {
 229                        let mut entries_by_path_edits = Vec::new();
 230                        let mut entries_by_id_edits = Vec::new();
 231                        for entry in worktree.entries {
 232                            match Entry::try_from((&root_char_bag, entry)) {
 233                                Ok(entry) => {
 234                                    entries_by_id_edits.push(Edit::Insert(PathEntry {
 235                                        id: entry.id,
 236                                        path: entry.path.clone(),
 237                                        is_ignored: entry.is_ignored,
 238                                        scan_id: 0,
 239                                    }));
 240                                    entries_by_path_edits.push(Edit::Insert(entry));
 241                                }
 242                                Err(err) => log::warn!("error for remote worktree entry {:?}", err),
 243                            }
 244                        }
 245
 246                        let mut entries_by_path = SumTree::new();
 247                        let mut entries_by_id = SumTree::new();
 248                        entries_by_path.edit(entries_by_path_edits, &());
 249                        entries_by_id.edit(entries_by_id_edits, &());
 250
 251                        (entries_by_path, entries_by_id)
 252                    })
 253                    .await;
 254
 255                {
 256                    let mut snapshot = background_snapshot.lock();
 257                    snapshot.entries_by_path = entries_by_path;
 258                    snapshot.entries_by_id = entries_by_id;
 259                    snapshot_updated_tx.send(()).await.ok();
 260                }
 261
 262                cx.background()
 263                    .spawn(async move {
 264                        while let Some(update) = updates_rx.next().await {
 265                            if let Err(error) =
 266                                background_snapshot.lock().apply_remote_update(update)
 267                            {
 268                                log::error!("error applying worktree update: {}", error);
 269                            }
 270                            snapshot_updated_tx.send(()).await.ok();
 271                        }
 272                    })
 273                    .detach();
 274
 275                cx.spawn(|mut cx| {
 276                    let this = worktree_handle.downgrade();
 277                    async move {
 278                        while let Some(_) = snapshot_updated_rx.recv().await {
 279                            if let Some(this) = this.upgrade(&cx) {
 280                                this.update(&mut cx, |this, cx| {
 281                                    this.poll_snapshot(cx);
 282                                    let this = this.as_remote_mut().unwrap();
 283                                    *last_scan_id_tx.borrow_mut() = this.snapshot.scan_id;
 284                                });
 285                            } else {
 286                                break;
 287                            }
 288                        }
 289                    }
 290                })
 291                .detach();
 292            }
 293        });
 294        (worktree_handle, deserialize_task)
 295    }
 296
 297    pub fn as_local(&self) -> Option<&LocalWorktree> {
 298        if let Worktree::Local(worktree) = self {
 299            Some(worktree)
 300        } else {
 301            None
 302        }
 303    }
 304
 305    pub fn as_remote(&self) -> Option<&RemoteWorktree> {
 306        if let Worktree::Remote(worktree) = self {
 307            Some(worktree)
 308        } else {
 309            None
 310        }
 311    }
 312
 313    pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
 314        if let Worktree::Local(worktree) = self {
 315            Some(worktree)
 316        } else {
 317            None
 318        }
 319    }
 320
 321    pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
 322        if let Worktree::Remote(worktree) = self {
 323            Some(worktree)
 324        } else {
 325            None
 326        }
 327    }
 328
 329    pub fn is_local(&self) -> bool {
 330        matches!(self, Worktree::Local(_))
 331    }
 332
 333    pub fn is_remote(&self) -> bool {
 334        !self.is_local()
 335    }
 336
 337    pub fn snapshot(&self) -> Snapshot {
 338        match self {
 339            Worktree::Local(worktree) => worktree.snapshot().snapshot,
 340            Worktree::Remote(worktree) => worktree.snapshot(),
 341        }
 342    }
 343
 344    pub fn scan_id(&self) -> usize {
 345        match self {
 346            Worktree::Local(worktree) => worktree.snapshot.scan_id,
 347            Worktree::Remote(worktree) => worktree.snapshot.scan_id,
 348        }
 349    }
 350
 351    pub fn is_visible(&self) -> bool {
 352        match self {
 353            Worktree::Local(worktree) => worktree.visible,
 354            Worktree::Remote(worktree) => worktree.visible,
 355        }
 356    }
 357
 358    pub fn replica_id(&self) -> ReplicaId {
 359        match self {
 360            Worktree::Local(_) => 0,
 361            Worktree::Remote(worktree) => worktree.replica_id,
 362        }
 363    }
 364
 365    pub fn diagnostic_summaries<'a>(
 366        &'a self,
 367    ) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
 368        match self {
 369            Worktree::Local(worktree) => &worktree.diagnostic_summaries,
 370            Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
 371        }
 372        .iter()
 373        .map(|(path, summary)| (path.0.clone(), summary.clone()))
 374    }
 375
 376    fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
 377        match self {
 378            Self::Local(worktree) => {
 379                let is_fake_fs = worktree.fs.is_fake();
 380                worktree.snapshot = worktree.background_snapshot.lock().clone();
 381                if worktree.is_scanning() {
 382                    if worktree.poll_task.is_none() {
 383                        worktree.poll_task = Some(cx.spawn_weak(|this, mut cx| async move {
 384                            if is_fake_fs {
 385                                #[cfg(any(test, feature = "test-support"))]
 386                                cx.background().simulate_random_delay().await;
 387                            } else {
 388                                smol::Timer::after(Duration::from_millis(100)).await;
 389                            }
 390                            if let Some(this) = this.upgrade(&cx) {
 391                                this.update(&mut cx, |this, cx| {
 392                                    this.as_local_mut().unwrap().poll_task = None;
 393                                    this.poll_snapshot(cx);
 394                                });
 395                            }
 396                        }));
 397                    }
 398                } else {
 399                    worktree.poll_task.take();
 400                    cx.emit(Event::UpdatedEntries);
 401                }
 402            }
 403            Self::Remote(worktree) => {
 404                worktree.snapshot = worktree.background_snapshot.lock().clone();
 405                cx.emit(Event::UpdatedEntries);
 406            }
 407        };
 408
 409        cx.notify();
 410    }
 411}
 412
 413impl LocalWorktree {
 414    async fn new(
 415        client: Arc<Client>,
 416        path: impl Into<Arc<Path>>,
 417        visible: bool,
 418        fs: Arc<dyn Fs>,
 419        next_entry_id: Arc<AtomicUsize>,
 420        cx: &mut AsyncAppContext,
 421    ) -> Result<(ModelHandle<Worktree>, UnboundedSender<ScanState>)> {
 422        let abs_path = path.into();
 423        let path: Arc<Path> = Arc::from(Path::new(""));
 424
 425        // After determining whether the root entry is a file or a directory, populate the
 426        // snapshot's "root name", which will be used for the purpose of fuzzy matching.
 427        let root_name = abs_path
 428            .file_name()
 429            .map_or(String::new(), |f| f.to_string_lossy().to_string());
 430        let root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
 431        let metadata = fs
 432            .metadata(&abs_path)
 433            .await
 434            .context("failed to stat worktree path")?;
 435
 436        let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
 437        let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning);
 438        let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
 439            let mut snapshot = LocalSnapshot {
 440                abs_path,
 441                ignores: Default::default(),
 442                removed_entry_ids: Default::default(),
 443                next_entry_id,
 444                snapshot: Snapshot {
 445                    id: WorktreeId::from_usize(cx.model_id()),
 446                    root_name: root_name.clone(),
 447                    root_char_bag,
 448                    entries_by_path: Default::default(),
 449                    entries_by_id: Default::default(),
 450                    scan_id: 0,
 451                },
 452            };
 453            if let Some(metadata) = metadata {
 454                let entry = Entry::new(
 455                    path.into(),
 456                    &metadata,
 457                    &snapshot.next_entry_id,
 458                    snapshot.root_char_bag,
 459                );
 460                snapshot.insert_entry(entry, fs.as_ref());
 461            }
 462
 463            let tree = Self {
 464                snapshot: snapshot.clone(),
 465                background_snapshot: Arc::new(Mutex::new(snapshot)),
 466                last_scan_state_rx,
 467                _background_scanner_task: None,
 468                share: None,
 469                poll_task: None,
 470                diagnostics: Default::default(),
 471                diagnostic_summaries: Default::default(),
 472                client,
 473                fs,
 474                visible,
 475            };
 476
 477            cx.spawn_weak(|this, mut cx| async move {
 478                while let Some(scan_state) = scan_states_rx.next().await {
 479                    if let Some(this) = this.upgrade(&cx) {
 480                        last_scan_state_tx.blocking_send(scan_state).ok();
 481                        this.update(&mut cx, |this, cx| {
 482                            this.poll_snapshot(cx);
 483                            this.as_local().unwrap().broadcast_snapshot()
 484                        })
 485                        .await;
 486                    } else {
 487                        break;
 488                    }
 489                }
 490            })
 491            .detach();
 492
 493            Worktree::Local(tree)
 494        });
 495
 496        Ok((tree, scan_states_tx))
 497    }
 498
 499    pub fn contains_abs_path(&self, path: &Path) -> bool {
 500        path.starts_with(&self.abs_path)
 501    }
 502
 503    fn absolutize(&self, path: &Path) -> PathBuf {
 504        if path.file_name().is_some() {
 505            self.abs_path.join(path)
 506        } else {
 507            self.abs_path.to_path_buf()
 508        }
 509    }
 510
 511    pub(crate) fn load_buffer(
 512        &mut self,
 513        path: &Path,
 514        cx: &mut ModelContext<Worktree>,
 515    ) -> Task<Result<ModelHandle<Buffer>>> {
 516        let path = Arc::from(path);
 517        cx.spawn(move |this, mut cx| async move {
 518            let (file, contents) = this
 519                .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
 520                .await?;
 521            Ok(cx.add_model(|cx| Buffer::from_file(0, contents, Box::new(file), cx)))
 522        })
 523    }
 524
 525    pub fn diagnostics_for_path(&self, path: &Path) -> Option<Vec<DiagnosticEntry<PointUtf16>>> {
 526        self.diagnostics.get(path).cloned()
 527    }
 528
 529    pub fn update_diagnostics(
 530        &mut self,
 531        worktree_path: Arc<Path>,
 532        diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
 533        _: &mut ModelContext<Worktree>,
 534    ) -> Result<bool> {
 535        self.diagnostics.remove(&worktree_path);
 536        let old_summary = self
 537            .diagnostic_summaries
 538            .remove(&PathKey(worktree_path.clone()))
 539            .unwrap_or_default();
 540        let new_summary = DiagnosticSummary::new(&diagnostics);
 541        if !new_summary.is_empty() {
 542            self.diagnostic_summaries
 543                .insert(PathKey(worktree_path.clone()), new_summary);
 544            self.diagnostics.insert(worktree_path.clone(), diagnostics);
 545        }
 546
 547        let updated = !old_summary.is_empty() || !new_summary.is_empty();
 548        if updated {
 549            if let Some(share) = self.share.as_ref() {
 550                self.client
 551                    .send(proto::UpdateDiagnosticSummary {
 552                        project_id: share.project_id,
 553                        worktree_id: self.id().to_proto(),
 554                        summary: Some(proto::DiagnosticSummary {
 555                            path: worktree_path.to_string_lossy().to_string(),
 556                            error_count: new_summary.error_count as u32,
 557                            warning_count: new_summary.warning_count as u32,
 558                        }),
 559                    })
 560                    .log_err();
 561            }
 562        }
 563
 564        Ok(updated)
 565    }
 566
 567    pub fn scan_complete(&self) -> impl Future<Output = ()> {
 568        let mut scan_state_rx = self.last_scan_state_rx.clone();
 569        async move {
 570            let mut scan_state = Some(scan_state_rx.borrow().clone());
 571            while let Some(ScanState::Scanning) = scan_state {
 572                scan_state = scan_state_rx.recv().await;
 573            }
 574        }
 575    }
 576
 577    fn is_scanning(&self) -> bool {
 578        if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
 579            true
 580        } else {
 581            false
 582        }
 583    }
 584
 585    pub fn snapshot(&self) -> LocalSnapshot {
 586        self.snapshot.clone()
 587    }
 588
 589    pub fn metadata_proto(&self) -> proto::WorktreeMetadata {
 590        proto::WorktreeMetadata {
 591            id: self.id().to_proto(),
 592            root_name: self.root_name().to_string(),
 593            visible: self.visible,
 594        }
 595    }
 596
 597    fn load(&self, path: &Path, cx: &mut ModelContext<Worktree>) -> Task<Result<(File, String)>> {
 598        let handle = cx.handle();
 599        let path = Arc::from(path);
 600        let abs_path = self.absolutize(&path);
 601        let fs = self.fs.clone();
 602        cx.spawn(|this, mut cx| async move {
 603            let text = fs.load(&abs_path).await?;
 604            // Eagerly populate the snapshot with an updated entry for the loaded file
 605            let entry = this
 606                .update(&mut cx, |this, cx| {
 607                    this.as_local()
 608                        .unwrap()
 609                        .refresh_entry(path, abs_path, None, cx)
 610                })
 611                .await?;
 612            this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
 613            Ok((
 614                File {
 615                    entry_id: Some(entry.id),
 616                    worktree: handle,
 617                    path: entry.path,
 618                    mtime: entry.mtime,
 619                    is_local: true,
 620                },
 621                text,
 622            ))
 623        })
 624    }
 625
 626    pub fn save_buffer_as(
 627        &self,
 628        buffer_handle: ModelHandle<Buffer>,
 629        path: impl Into<Arc<Path>>,
 630        cx: &mut ModelContext<Worktree>,
 631    ) -> Task<Result<()>> {
 632        let buffer = buffer_handle.read(cx);
 633        let text = buffer.as_rope().clone();
 634        let version = buffer.version();
 635        let save = self.write_file(path, text, cx);
 636        let handle = cx.handle();
 637        cx.as_mut().spawn(|mut cx| async move {
 638            let entry = save.await?;
 639            let file = File {
 640                entry_id: Some(entry.id),
 641                worktree: handle,
 642                path: entry.path,
 643                mtime: entry.mtime,
 644                is_local: true,
 645            };
 646
 647            buffer_handle.update(&mut cx, |buffer, cx| {
 648                buffer.did_save(version, file.mtime, Some(Box::new(file)), cx);
 649            });
 650
 651            Ok(())
 652        })
 653    }
 654
 655    pub fn create_entry(
 656        &self,
 657        path: impl Into<Arc<Path>>,
 658        is_dir: bool,
 659        cx: &mut ModelContext<Worktree>,
 660    ) -> Task<Result<Entry>> {
 661        self.write_entry_internal(
 662            path,
 663            if is_dir {
 664                None
 665            } else {
 666                Some(Default::default())
 667            },
 668            cx,
 669        )
 670    }
 671
 672    pub fn write_file(
 673        &self,
 674        path: impl Into<Arc<Path>>,
 675        text: Rope,
 676        cx: &mut ModelContext<Worktree>,
 677    ) -> Task<Result<Entry>> {
 678        self.write_entry_internal(path, Some(text), cx)
 679    }
 680
 681    pub fn delete_entry(
 682        &self,
 683        entry_id: ProjectEntryId,
 684        cx: &mut ModelContext<Worktree>,
 685    ) -> Option<Task<Result<()>>> {
 686        let entry = self.entry_for_id(entry_id)?.clone();
 687        let abs_path = self.absolutize(&entry.path);
 688        let delete = cx.background().spawn({
 689            let fs = self.fs.clone();
 690            let abs_path = abs_path.clone();
 691            async move {
 692                if entry.is_file() {
 693                    fs.remove_file(&abs_path, Default::default()).await
 694                } else {
 695                    fs.remove_dir(
 696                        &abs_path,
 697                        RemoveOptions {
 698                            recursive: true,
 699                            ignore_if_not_exists: false,
 700                        },
 701                    )
 702                    .await
 703                }
 704            }
 705        });
 706
 707        Some(cx.spawn(|this, mut cx| async move {
 708            delete.await?;
 709            this.update(&mut cx, |this, _| {
 710                let this = this.as_local_mut().unwrap();
 711                let mut snapshot = this.background_snapshot.lock();
 712                snapshot.delete_entry(entry_id);
 713            });
 714            this.update(&mut cx, |this, cx| {
 715                this.poll_snapshot(cx);
 716                this.as_local().unwrap().broadcast_snapshot()
 717            })
 718            .await;
 719            Ok(())
 720        }))
 721    }
 722
 723    pub fn rename_entry(
 724        &self,
 725        entry_id: ProjectEntryId,
 726        new_path: impl Into<Arc<Path>>,
 727        cx: &mut ModelContext<Worktree>,
 728    ) -> Option<Task<Result<Entry>>> {
 729        let old_path = self.entry_for_id(entry_id)?.path.clone();
 730        let new_path = new_path.into();
 731        let abs_old_path = self.absolutize(&old_path);
 732        let abs_new_path = self.absolutize(&new_path);
 733        let rename = cx.background().spawn({
 734            let fs = self.fs.clone();
 735            let abs_new_path = abs_new_path.clone();
 736            async move {
 737                fs.rename(&abs_old_path, &abs_new_path, Default::default())
 738                    .await
 739            }
 740        });
 741
 742        Some(cx.spawn(|this, mut cx| async move {
 743            rename.await?;
 744            let entry = this
 745                .update(&mut cx, |this, cx| {
 746                    this.as_local_mut().unwrap().refresh_entry(
 747                        new_path.clone(),
 748                        abs_new_path,
 749                        Some(old_path),
 750                        cx,
 751                    )
 752                })
 753                .await?;
 754            this.update(&mut cx, |this, cx| {
 755                this.poll_snapshot(cx);
 756                this.as_local().unwrap().broadcast_snapshot()
 757            })
 758            .await;
 759            Ok(entry)
 760        }))
 761    }
 762
 763    pub fn copy_entry(
 764        &self,
 765        entry_id: ProjectEntryId,
 766        new_path: impl Into<Arc<Path>>,
 767        cx: &mut ModelContext<Worktree>,
 768    ) -> Option<Task<Result<Entry>>> {
 769        let old_path = self.entry_for_id(entry_id)?.path.clone();
 770        let new_path = new_path.into();
 771        let abs_old_path = self.absolutize(&old_path);
 772        let abs_new_path = self.absolutize(&new_path);
 773        let copy = cx.background().spawn({
 774            let fs = self.fs.clone();
 775            let abs_new_path = abs_new_path.clone();
 776            async move {
 777                fs.copy(&abs_old_path, &abs_new_path, Default::default())
 778                    .await
 779            }
 780        });
 781
 782        Some(cx.spawn(|this, mut cx| async move {
 783            copy.await?;
 784            let entry = this
 785                .update(&mut cx, |this, cx| {
 786                    this.as_local_mut().unwrap().refresh_entry(
 787                        new_path.clone(),
 788                        abs_new_path,
 789                        None,
 790                        cx,
 791                    )
 792                })
 793                .await?;
 794            this.update(&mut cx, |this, cx| {
 795                this.poll_snapshot(cx);
 796                this.as_local().unwrap().broadcast_snapshot()
 797            })
 798            .await;
 799            Ok(entry)
 800        }))
 801    }
 802
 803    fn write_entry_internal(
 804        &self,
 805        path: impl Into<Arc<Path>>,
 806        text_if_file: Option<Rope>,
 807        cx: &mut ModelContext<Worktree>,
 808    ) -> Task<Result<Entry>> {
 809        let path = path.into();
 810        let abs_path = self.absolutize(&path);
 811        let write = cx.background().spawn({
 812            let fs = self.fs.clone();
 813            let abs_path = abs_path.clone();
 814            async move {
 815                if let Some(text) = text_if_file {
 816                    fs.save(&abs_path, &text).await
 817                } else {
 818                    fs.create_dir(&abs_path).await
 819                }
 820            }
 821        });
 822
 823        cx.spawn(|this, mut cx| async move {
 824            write.await?;
 825            let entry = this
 826                .update(&mut cx, |this, cx| {
 827                    this.as_local_mut()
 828                        .unwrap()
 829                        .refresh_entry(path, abs_path, None, cx)
 830                })
 831                .await?;
 832            this.update(&mut cx, |this, cx| {
 833                this.poll_snapshot(cx);
 834                this.as_local().unwrap().broadcast_snapshot()
 835            })
 836            .await;
 837            Ok(entry)
 838        })
 839    }
 840
 841    fn refresh_entry(
 842        &self,
 843        path: Arc<Path>,
 844        abs_path: PathBuf,
 845        old_path: Option<Arc<Path>>,
 846        cx: &mut ModelContext<Worktree>,
 847    ) -> Task<Result<Entry>> {
 848        let fs = self.fs.clone();
 849        let root_char_bag;
 850        let next_entry_id;
 851        {
 852            let snapshot = self.background_snapshot.lock();
 853            root_char_bag = snapshot.root_char_bag;
 854            next_entry_id = snapshot.next_entry_id.clone();
 855        }
 856        cx.spawn_weak(|this, mut cx| async move {
 857            let mut entry = Entry::new(
 858                path.clone(),
 859                &fs.metadata(&abs_path)
 860                    .await?
 861                    .ok_or_else(|| anyhow!("could not read saved file metadata"))?,
 862                &next_entry_id,
 863                root_char_bag,
 864            );
 865
 866            let this = this
 867                .upgrade(&cx)
 868                .ok_or_else(|| anyhow!("worktree was dropped"))?;
 869            let (entry, snapshot, snapshots_tx) = this.read_with(&cx, |this, _| {
 870                let this = this.as_local().unwrap();
 871                let mut snapshot = this.background_snapshot.lock();
 872                entry.is_ignored = snapshot
 873                    .ignore_stack_for_path(&path, entry.is_dir())
 874                    .is_path_ignored(&path, entry.is_dir());
 875                if let Some(old_path) = old_path {
 876                    snapshot.remove_path(&old_path);
 877                }
 878                let entry = snapshot.insert_entry(entry, fs.as_ref());
 879                snapshot.scan_id += 1;
 880                let snapshots_tx = this.share.as_ref().map(|s| s.snapshots_tx.clone());
 881                (entry, snapshot.clone(), snapshots_tx)
 882            });
 883            this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
 884
 885            if let Some(snapshots_tx) = snapshots_tx {
 886                snapshots_tx.send(snapshot).await.ok();
 887            }
 888
 889            Ok(entry)
 890        })
 891    }
 892
 893    pub fn share(&mut self, project_id: u64, cx: &mut ModelContext<Worktree>) -> Task<Result<()>> {
 894        let (share_tx, share_rx) = oneshot::channel();
 895        let (snapshots_to_send_tx, snapshots_to_send_rx) =
 896            smol::channel::unbounded::<LocalSnapshot>();
 897        if self.share.is_some() {
 898            let _ = share_tx.send(Ok(()));
 899        } else {
 900            let rpc = self.client.clone();
 901            let worktree_id = cx.model_id() as u64;
 902            let maintain_remote_snapshot = cx.background().spawn({
 903                let rpc = rpc.clone();
 904                let diagnostic_summaries = self.diagnostic_summaries.clone();
 905                async move {
 906                    let mut prev_snapshot = match snapshots_to_send_rx.recv().await {
 907                        Ok(snapshot) => {
 908                            if let Err(error) = rpc
 909                                .request(proto::UpdateWorktree {
 910                                    project_id,
 911                                    worktree_id,
 912                                    root_name: snapshot.root_name().to_string(),
 913                                    updated_entries: snapshot
 914                                        .entries_by_path
 915                                        .iter()
 916                                        .filter(|e| !e.is_ignored)
 917                                        .map(Into::into)
 918                                        .collect(),
 919                                    removed_entries: Default::default(),
 920                                    scan_id: snapshot.scan_id as u64,
 921                                })
 922                                .await
 923                            {
 924                                let _ = share_tx.send(Err(error));
 925                                return Err(anyhow!("failed to send initial update worktree"));
 926                            } else {
 927                                let _ = share_tx.send(Ok(()));
 928                                snapshot
 929                            }
 930                        }
 931                        Err(error) => {
 932                            let _ = share_tx.send(Err(error.into()));
 933                            return Err(anyhow!("failed to send initial update worktree"));
 934                        }
 935                    };
 936
 937                    for (path, summary) in diagnostic_summaries.iter() {
 938                        rpc.send(proto::UpdateDiagnosticSummary {
 939                            project_id,
 940                            worktree_id,
 941                            summary: Some(summary.to_proto(&path.0)),
 942                        })?;
 943                    }
 944
 945                    // Stream ignored entries in chunks.
 946                    {
 947                        let mut ignored_entries = prev_snapshot
 948                            .entries_by_path
 949                            .iter()
 950                            .filter(|e| e.is_ignored);
 951                        let mut ignored_entries_to_send = Vec::new();
 952                        loop {
 953                            #[cfg(any(test, feature = "test-support"))]
 954                            const CHUNK_SIZE: usize = 2;
 955                            #[cfg(not(any(test, feature = "test-support")))]
 956                            const CHUNK_SIZE: usize = 256;
 957
 958                            let entry = ignored_entries.next();
 959                            if ignored_entries_to_send.len() >= CHUNK_SIZE || entry.is_none() {
 960                                rpc.request(proto::UpdateWorktree {
 961                                    project_id,
 962                                    worktree_id,
 963                                    root_name: prev_snapshot.root_name().to_string(),
 964                                    updated_entries: mem::take(&mut ignored_entries_to_send),
 965                                    removed_entries: Default::default(),
 966                                    scan_id: prev_snapshot.scan_id as u64,
 967                                })
 968                                .await?;
 969                            }
 970
 971                            if let Some(entry) = entry {
 972                                ignored_entries_to_send.push(entry.into());
 973                            } else {
 974                                break;
 975                            }
 976                        }
 977                    }
 978
 979                    while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
 980                        let message =
 981                            snapshot.build_update(&prev_snapshot, project_id, worktree_id, true);
 982                        rpc.request(message).await?;
 983                        prev_snapshot = snapshot;
 984                    }
 985
 986                    Ok::<_, anyhow::Error>(())
 987                }
 988                .log_err()
 989            });
 990            self.share = Some(ShareState {
 991                project_id,
 992                snapshots_tx: snapshots_to_send_tx.clone(),
 993                _maintain_remote_snapshot: Some(maintain_remote_snapshot),
 994            });
 995        }
 996
 997        cx.spawn_weak(|this, cx| async move {
 998            if let Some(this) = this.upgrade(&cx) {
 999                this.read_with(&cx, |this, _| {
1000                    let this = this.as_local().unwrap();
1001                    let _ = snapshots_to_send_tx.try_send(this.snapshot());
1002                });
1003            }
1004            share_rx
1005                .await
1006                .unwrap_or_else(|_| Err(anyhow!("share ended")))
1007        })
1008    }
1009
1010    pub fn unshare(&mut self) {
1011        self.share.take();
1012    }
1013
1014    pub fn is_shared(&self) -> bool {
1015        self.share.is_some()
1016    }
1017
1018    fn broadcast_snapshot(&self) -> impl Future<Output = ()> {
1019        let mut to_send = None;
1020        if !self.is_scanning() {
1021            if let Some(share) = self.share.as_ref() {
1022                to_send = Some((self.snapshot(), share.snapshots_tx.clone()));
1023            }
1024        }
1025
1026        async move {
1027            if let Some((snapshot, snapshots_to_send_tx)) = to_send {
1028                if let Err(err) = snapshots_to_send_tx.send(snapshot).await {
1029                    log::error!("error submitting snapshot to send {}", err);
1030                }
1031            }
1032        }
1033    }
1034}
1035
1036impl RemoteWorktree {
1037    fn snapshot(&self) -> Snapshot {
1038        self.snapshot.clone()
1039    }
1040
1041    pub fn update_from_remote(
1042        &mut self,
1043        envelope: TypedEnvelope<proto::UpdateWorktree>,
1044    ) -> Result<()> {
1045        self.updates_tx
1046            .unbounded_send(envelope.payload)
1047            .expect("consumer runs to completion");
1048        Ok(())
1049    }
1050
1051    fn wait_for_snapshot(&self, scan_id: usize) -> impl Future<Output = ()> {
1052        let mut rx = self.last_scan_id_rx.clone();
1053        async move {
1054            while let Some(applied_scan_id) = rx.next().await {
1055                if applied_scan_id >= scan_id {
1056                    return;
1057                }
1058            }
1059        }
1060    }
1061
1062    pub fn update_diagnostic_summary(
1063        &mut self,
1064        path: Arc<Path>,
1065        summary: &proto::DiagnosticSummary,
1066    ) {
1067        let summary = DiagnosticSummary {
1068            error_count: summary.error_count as usize,
1069            warning_count: summary.warning_count as usize,
1070        };
1071        if summary.is_empty() {
1072            self.diagnostic_summaries.remove(&PathKey(path.clone()));
1073        } else {
1074            self.diagnostic_summaries
1075                .insert(PathKey(path.clone()), summary);
1076        }
1077    }
1078
1079    pub fn insert_entry(
1080        &self,
1081        entry: proto::Entry,
1082        scan_id: usize,
1083        cx: &mut ModelContext<Worktree>,
1084    ) -> Task<Result<Entry>> {
1085        let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1086        cx.spawn(|this, mut cx| async move {
1087            wait_for_snapshot.await;
1088            this.update(&mut cx, |worktree, _| {
1089                let worktree = worktree.as_remote_mut().unwrap();
1090                let mut snapshot = worktree.background_snapshot.lock();
1091                let entry = snapshot.insert_entry(entry);
1092                worktree.snapshot = snapshot.clone();
1093                entry
1094            })
1095        })
1096    }
1097
1098    pub(crate) fn delete_entry(
1099        &self,
1100        id: ProjectEntryId,
1101        scan_id: usize,
1102        cx: &mut ModelContext<Worktree>,
1103    ) -> Task<Result<()>> {
1104        let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1105        cx.spawn(|this, mut cx| async move {
1106            wait_for_snapshot.await;
1107            this.update(&mut cx, |worktree, _| {
1108                let worktree = worktree.as_remote_mut().unwrap();
1109                let mut snapshot = worktree.background_snapshot.lock();
1110                snapshot.delete_entry(id);
1111                worktree.snapshot = snapshot.clone();
1112            });
1113            Ok(())
1114        })
1115    }
1116}
1117
1118impl Snapshot {
1119    pub fn id(&self) -> WorktreeId {
1120        self.id
1121    }
1122
1123    pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
1124        self.entries_by_id.get(&entry_id, &()).is_some()
1125    }
1126
1127    pub(crate) fn insert_entry(&mut self, entry: proto::Entry) -> Result<Entry> {
1128        let entry = Entry::try_from((&self.root_char_bag, entry))?;
1129        let old_entry = self.entries_by_id.insert_or_replace(
1130            PathEntry {
1131                id: entry.id,
1132                path: entry.path.clone(),
1133                is_ignored: entry.is_ignored,
1134                scan_id: 0,
1135            },
1136            &(),
1137        );
1138        if let Some(old_entry) = old_entry {
1139            self.entries_by_path.remove(&PathKey(old_entry.path), &());
1140        }
1141        self.entries_by_path.insert_or_replace(entry.clone(), &());
1142        Ok(entry)
1143    }
1144
1145    fn delete_entry(&mut self, entry_id: ProjectEntryId) -> bool {
1146        if let Some(removed_entry) = self.entries_by_id.remove(&entry_id, &()) {
1147            self.entries_by_path = {
1148                let mut cursor = self.entries_by_path.cursor();
1149                let mut new_entries_by_path =
1150                    cursor.slice(&TraversalTarget::Path(&removed_entry.path), Bias::Left, &());
1151                while let Some(entry) = cursor.item() {
1152                    if entry.path.starts_with(&removed_entry.path) {
1153                        self.entries_by_id.remove(&entry.id, &());
1154                        cursor.next(&());
1155                    } else {
1156                        break;
1157                    }
1158                }
1159                new_entries_by_path.push_tree(cursor.suffix(&()), &());
1160                new_entries_by_path
1161            };
1162
1163            true
1164        } else {
1165            false
1166        }
1167    }
1168
1169    pub(crate) fn apply_remote_update(&mut self, update: proto::UpdateWorktree) -> Result<()> {
1170        let mut entries_by_path_edits = Vec::new();
1171        let mut entries_by_id_edits = Vec::new();
1172        for entry_id in update.removed_entries {
1173            let entry = self
1174                .entry_for_id(ProjectEntryId::from_proto(entry_id))
1175                .ok_or_else(|| anyhow!("unknown entry"))?;
1176            entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
1177            entries_by_id_edits.push(Edit::Remove(entry.id));
1178        }
1179
1180        for entry in update.updated_entries {
1181            let entry = Entry::try_from((&self.root_char_bag, entry))?;
1182            if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
1183                entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
1184            }
1185            entries_by_id_edits.push(Edit::Insert(PathEntry {
1186                id: entry.id,
1187                path: entry.path.clone(),
1188                is_ignored: entry.is_ignored,
1189                scan_id: 0,
1190            }));
1191            entries_by_path_edits.push(Edit::Insert(entry));
1192        }
1193
1194        self.entries_by_path.edit(entries_by_path_edits, &());
1195        self.entries_by_id.edit(entries_by_id_edits, &());
1196        self.scan_id = update.scan_id as usize;
1197
1198        Ok(())
1199    }
1200
1201    pub fn file_count(&self) -> usize {
1202        self.entries_by_path.summary().file_count
1203    }
1204
1205    pub fn visible_file_count(&self) -> usize {
1206        self.entries_by_path.summary().visible_file_count
1207    }
1208
1209    fn traverse_from_offset(
1210        &self,
1211        include_dirs: bool,
1212        include_ignored: bool,
1213        start_offset: usize,
1214    ) -> Traversal {
1215        let mut cursor = self.entries_by_path.cursor();
1216        cursor.seek(
1217            &TraversalTarget::Count {
1218                count: start_offset,
1219                include_dirs,
1220                include_ignored,
1221            },
1222            Bias::Right,
1223            &(),
1224        );
1225        Traversal {
1226            cursor,
1227            include_dirs,
1228            include_ignored,
1229        }
1230    }
1231
1232    fn traverse_from_path(
1233        &self,
1234        include_dirs: bool,
1235        include_ignored: bool,
1236        path: &Path,
1237    ) -> Traversal {
1238        let mut cursor = self.entries_by_path.cursor();
1239        cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1240        Traversal {
1241            cursor,
1242            include_dirs,
1243            include_ignored,
1244        }
1245    }
1246
1247    pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1248        self.traverse_from_offset(false, include_ignored, start)
1249    }
1250
1251    pub fn entries(&self, include_ignored: bool) -> Traversal {
1252        self.traverse_from_offset(true, include_ignored, 0)
1253    }
1254
1255    pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1256        let empty_path = Path::new("");
1257        self.entries_by_path
1258            .cursor::<()>()
1259            .filter(move |entry| entry.path.as_ref() != empty_path)
1260            .map(|entry| &entry.path)
1261    }
1262
1263    fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
1264        let mut cursor = self.entries_by_path.cursor();
1265        cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
1266        let traversal = Traversal {
1267            cursor,
1268            include_dirs: true,
1269            include_ignored: true,
1270        };
1271        ChildEntriesIter {
1272            traversal,
1273            parent_path,
1274        }
1275    }
1276
1277    pub fn root_entry(&self) -> Option<&Entry> {
1278        self.entry_for_path("")
1279    }
1280
1281    pub fn root_name(&self) -> &str {
1282        &self.root_name
1283    }
1284
1285    pub fn scan_id(&self) -> usize {
1286        self.scan_id
1287    }
1288
1289    pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
1290        let path = path.as_ref();
1291        self.traverse_from_path(true, true, path)
1292            .entry()
1293            .and_then(|entry| {
1294                if entry.path.as_ref() == path {
1295                    Some(entry)
1296                } else {
1297                    None
1298                }
1299            })
1300    }
1301
1302    pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
1303        let entry = self.entries_by_id.get(&id, &())?;
1304        self.entry_for_path(&entry.path)
1305    }
1306
1307    pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
1308        self.entry_for_path(path.as_ref()).map(|e| e.inode)
1309    }
1310}
1311
1312impl LocalSnapshot {
1313    pub fn abs_path(&self) -> &Arc<Path> {
1314        &self.abs_path
1315    }
1316
1317    #[cfg(test)]
1318    pub(crate) fn to_proto(
1319        &self,
1320        diagnostic_summaries: &TreeMap<PathKey, DiagnosticSummary>,
1321        visible: bool,
1322    ) -> proto::Worktree {
1323        let root_name = self.root_name.clone();
1324        proto::Worktree {
1325            id: self.id.0 as u64,
1326            root_name,
1327            entries: self
1328                .entries_by_path
1329                .iter()
1330                .filter(|e| !e.is_ignored)
1331                .map(Into::into)
1332                .collect(),
1333            diagnostic_summaries: diagnostic_summaries
1334                .iter()
1335                .map(|(path, summary)| summary.to_proto(&path.0))
1336                .collect(),
1337            visible,
1338            scan_id: self.scan_id as u64,
1339        }
1340    }
1341
1342    pub(crate) fn build_update(
1343        &self,
1344        other: &Self,
1345        project_id: u64,
1346        worktree_id: u64,
1347        include_ignored: bool,
1348    ) -> proto::UpdateWorktree {
1349        let mut updated_entries = Vec::new();
1350        let mut removed_entries = Vec::new();
1351        let mut self_entries = self
1352            .entries_by_id
1353            .cursor::<()>()
1354            .filter(|e| include_ignored || !e.is_ignored)
1355            .peekable();
1356        let mut other_entries = other
1357            .entries_by_id
1358            .cursor::<()>()
1359            .filter(|e| include_ignored || !e.is_ignored)
1360            .peekable();
1361        loop {
1362            match (self_entries.peek(), other_entries.peek()) {
1363                (Some(self_entry), Some(other_entry)) => {
1364                    match Ord::cmp(&self_entry.id, &other_entry.id) {
1365                        Ordering::Less => {
1366                            let entry = self.entry_for_id(self_entry.id).unwrap().into();
1367                            updated_entries.push(entry);
1368                            self_entries.next();
1369                        }
1370                        Ordering::Equal => {
1371                            if self_entry.scan_id != other_entry.scan_id {
1372                                let entry = self.entry_for_id(self_entry.id).unwrap().into();
1373                                updated_entries.push(entry);
1374                            }
1375
1376                            self_entries.next();
1377                            other_entries.next();
1378                        }
1379                        Ordering::Greater => {
1380                            removed_entries.push(other_entry.id.to_proto());
1381                            other_entries.next();
1382                        }
1383                    }
1384                }
1385                (Some(self_entry), None) => {
1386                    let entry = self.entry_for_id(self_entry.id).unwrap().into();
1387                    updated_entries.push(entry);
1388                    self_entries.next();
1389                }
1390                (None, Some(other_entry)) => {
1391                    removed_entries.push(other_entry.id.to_proto());
1392                    other_entries.next();
1393                }
1394                (None, None) => break,
1395            }
1396        }
1397
1398        proto::UpdateWorktree {
1399            project_id,
1400            worktree_id,
1401            root_name: self.root_name().to_string(),
1402            updated_entries,
1403            removed_entries,
1404            scan_id: self.scan_id as u64,
1405        }
1406    }
1407
1408    fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
1409        if !entry.is_dir() && entry.path.file_name() == Some(&GITIGNORE) {
1410            let abs_path = self.abs_path.join(&entry.path);
1411            match build_gitignore(&abs_path, fs) {
1412                Ok(ignore) => {
1413                    let ignore_dir_path = entry.path.parent().unwrap();
1414                    self.ignores
1415                        .insert(ignore_dir_path.into(), (Arc::new(ignore), self.scan_id));
1416                }
1417                Err(error) => {
1418                    log::error!(
1419                        "error loading .gitignore file {:?} - {:?}",
1420                        &entry.path,
1421                        error
1422                    );
1423                }
1424            }
1425        }
1426
1427        self.reuse_entry_id(&mut entry);
1428        self.entries_by_path.insert_or_replace(entry.clone(), &());
1429        let scan_id = self.scan_id;
1430        self.entries_by_id.insert_or_replace(
1431            PathEntry {
1432                id: entry.id,
1433                path: entry.path.clone(),
1434                is_ignored: entry.is_ignored,
1435                scan_id,
1436            },
1437            &(),
1438        );
1439        entry
1440    }
1441
1442    fn populate_dir(
1443        &mut self,
1444        parent_path: Arc<Path>,
1445        entries: impl IntoIterator<Item = Entry>,
1446        ignore: Option<Arc<Gitignore>>,
1447    ) {
1448        let mut parent_entry = if let Some(parent_entry) =
1449            self.entries_by_path.get(&PathKey(parent_path.clone()), &())
1450        {
1451            parent_entry.clone()
1452        } else {
1453            log::warn!(
1454                "populating a directory {:?} that has been removed",
1455                parent_path
1456            );
1457            return;
1458        };
1459
1460        if let Some(ignore) = ignore {
1461            self.ignores.insert(parent_path, (ignore, self.scan_id));
1462        }
1463        if matches!(parent_entry.kind, EntryKind::PendingDir) {
1464            parent_entry.kind = EntryKind::Dir;
1465        } else {
1466            unreachable!();
1467        }
1468
1469        let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
1470        let mut entries_by_id_edits = Vec::new();
1471
1472        for mut entry in entries {
1473            self.reuse_entry_id(&mut entry);
1474            entries_by_id_edits.push(Edit::Insert(PathEntry {
1475                id: entry.id,
1476                path: entry.path.clone(),
1477                is_ignored: entry.is_ignored,
1478                scan_id: self.scan_id,
1479            }));
1480            entries_by_path_edits.push(Edit::Insert(entry));
1481        }
1482
1483        self.entries_by_path.edit(entries_by_path_edits, &());
1484        self.entries_by_id.edit(entries_by_id_edits, &());
1485    }
1486
1487    fn reuse_entry_id(&mut self, entry: &mut Entry) {
1488        if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
1489            entry.id = removed_entry_id;
1490        } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
1491            entry.id = existing_entry.id;
1492        }
1493    }
1494
1495    fn remove_path(&mut self, path: &Path) {
1496        let mut new_entries;
1497        let removed_entries;
1498        {
1499            let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
1500            new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
1501            removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
1502            new_entries.push_tree(cursor.suffix(&()), &());
1503        }
1504        self.entries_by_path = new_entries;
1505
1506        let mut entries_by_id_edits = Vec::new();
1507        for entry in removed_entries.cursor::<()>() {
1508            let removed_entry_id = self
1509                .removed_entry_ids
1510                .entry(entry.inode)
1511                .or_insert(entry.id);
1512            *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
1513            entries_by_id_edits.push(Edit::Remove(entry.id));
1514        }
1515        self.entries_by_id.edit(entries_by_id_edits, &());
1516
1517        if path.file_name() == Some(&GITIGNORE) {
1518            if let Some((_, scan_id)) = self.ignores.get_mut(path.parent().unwrap()) {
1519                *scan_id = self.snapshot.scan_id;
1520            }
1521        }
1522    }
1523
1524    fn ignore_stack_for_path(&self, path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
1525        let mut new_ignores = Vec::new();
1526        for ancestor in path.ancestors().skip(1) {
1527            if let Some((ignore, _)) = self.ignores.get(ancestor) {
1528                new_ignores.push((ancestor, Some(ignore.clone())));
1529            } else {
1530                new_ignores.push((ancestor, None));
1531            }
1532        }
1533
1534        let mut ignore_stack = IgnoreStack::none();
1535        for (parent_path, ignore) in new_ignores.into_iter().rev() {
1536            if ignore_stack.is_path_ignored(&parent_path, true) {
1537                ignore_stack = IgnoreStack::all();
1538                break;
1539            } else if let Some(ignore) = ignore {
1540                ignore_stack = ignore_stack.append(Arc::from(parent_path), ignore);
1541            }
1542        }
1543
1544        if ignore_stack.is_path_ignored(path, is_dir) {
1545            ignore_stack = IgnoreStack::all();
1546        }
1547
1548        ignore_stack
1549    }
1550}
1551
1552fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
1553    let contents = smol::block_on(fs.load(&abs_path))?;
1554    let parent = abs_path.parent().unwrap_or(Path::new("/"));
1555    let mut builder = GitignoreBuilder::new(parent);
1556    for line in contents.lines() {
1557        builder.add_line(Some(abs_path.into()), line)?;
1558    }
1559    Ok(builder.build()?)
1560}
1561
1562impl WorktreeId {
1563    pub fn from_usize(handle_id: usize) -> Self {
1564        Self(handle_id)
1565    }
1566
1567    pub(crate) fn from_proto(id: u64) -> Self {
1568        Self(id as usize)
1569    }
1570
1571    pub fn to_proto(&self) -> u64 {
1572        self.0 as u64
1573    }
1574
1575    pub fn to_usize(&self) -> usize {
1576        self.0
1577    }
1578}
1579
1580impl fmt::Display for WorktreeId {
1581    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1582        self.0.fmt(f)
1583    }
1584}
1585
1586impl Deref for Worktree {
1587    type Target = Snapshot;
1588
1589    fn deref(&self) -> &Self::Target {
1590        match self {
1591            Worktree::Local(worktree) => &worktree.snapshot,
1592            Worktree::Remote(worktree) => &worktree.snapshot,
1593        }
1594    }
1595}
1596
1597impl Deref for LocalWorktree {
1598    type Target = LocalSnapshot;
1599
1600    fn deref(&self) -> &Self::Target {
1601        &self.snapshot
1602    }
1603}
1604
1605impl Deref for RemoteWorktree {
1606    type Target = Snapshot;
1607
1608    fn deref(&self) -> &Self::Target {
1609        &self.snapshot
1610    }
1611}
1612
1613impl fmt::Debug for LocalWorktree {
1614    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1615        self.snapshot.fmt(f)
1616    }
1617}
1618
1619impl fmt::Debug for Snapshot {
1620    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1621        struct EntriesById<'a>(&'a SumTree<PathEntry>);
1622        struct EntriesByPath<'a>(&'a SumTree<Entry>);
1623
1624        impl<'a> fmt::Debug for EntriesByPath<'a> {
1625            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1626                f.debug_map()
1627                    .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
1628                    .finish()
1629            }
1630        }
1631
1632        impl<'a> fmt::Debug for EntriesById<'a> {
1633            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1634                f.debug_list().entries(self.0.iter()).finish()
1635            }
1636        }
1637
1638        f.debug_struct("Snapshot")
1639            .field("id", &self.id)
1640            .field("root_name", &self.root_name)
1641            .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
1642            .field("entries_by_id", &EntriesById(&self.entries_by_id))
1643            .finish()
1644    }
1645}
1646
1647#[derive(Clone, PartialEq)]
1648pub struct File {
1649    pub worktree: ModelHandle<Worktree>,
1650    pub path: Arc<Path>,
1651    pub mtime: SystemTime,
1652    pub(crate) entry_id: Option<ProjectEntryId>,
1653    pub(crate) is_local: bool,
1654}
1655
1656impl language::File for File {
1657    fn as_local(&self) -> Option<&dyn language::LocalFile> {
1658        if self.is_local {
1659            Some(self)
1660        } else {
1661            None
1662        }
1663    }
1664
1665    fn mtime(&self) -> SystemTime {
1666        self.mtime
1667    }
1668
1669    fn path(&self) -> &Arc<Path> {
1670        &self.path
1671    }
1672
1673    fn full_path(&self, cx: &AppContext) -> PathBuf {
1674        let mut full_path = PathBuf::new();
1675        full_path.push(self.worktree.read(cx).root_name());
1676        if self.path.components().next().is_some() {
1677            full_path.push(&self.path);
1678        }
1679        full_path
1680    }
1681
1682    /// Returns the last component of this handle's absolute path. If this handle refers to the root
1683    /// of its worktree, then this method will return the name of the worktree itself.
1684    fn file_name(&self, cx: &AppContext) -> OsString {
1685        self.path
1686            .file_name()
1687            .map(|name| name.into())
1688            .unwrap_or_else(|| OsString::from(&self.worktree.read(cx).root_name))
1689    }
1690
1691    fn is_deleted(&self) -> bool {
1692        self.entry_id.is_none()
1693    }
1694
1695    fn save(
1696        &self,
1697        buffer_id: u64,
1698        text: Rope,
1699        version: clock::Global,
1700        cx: &mut MutableAppContext,
1701    ) -> Task<Result<(clock::Global, SystemTime)>> {
1702        self.worktree.update(cx, |worktree, cx| match worktree {
1703            Worktree::Local(worktree) => {
1704                let rpc = worktree.client.clone();
1705                let project_id = worktree.share.as_ref().map(|share| share.project_id);
1706                let save = worktree.write_file(self.path.clone(), text, cx);
1707                cx.background().spawn(async move {
1708                    let entry = save.await?;
1709                    if let Some(project_id) = project_id {
1710                        rpc.send(proto::BufferSaved {
1711                            project_id,
1712                            buffer_id,
1713                            version: serialize_version(&version),
1714                            mtime: Some(entry.mtime.into()),
1715                        })?;
1716                    }
1717                    Ok((version, entry.mtime))
1718                })
1719            }
1720            Worktree::Remote(worktree) => {
1721                let rpc = worktree.client.clone();
1722                let project_id = worktree.project_id;
1723                cx.foreground().spawn(async move {
1724                    let response = rpc
1725                        .request(proto::SaveBuffer {
1726                            project_id,
1727                            buffer_id,
1728                            version: serialize_version(&version),
1729                        })
1730                        .await?;
1731                    let version = deserialize_version(response.version);
1732                    let mtime = response
1733                        .mtime
1734                        .ok_or_else(|| anyhow!("missing mtime"))?
1735                        .into();
1736                    Ok((version, mtime))
1737                })
1738            }
1739        })
1740    }
1741
1742    fn as_any(&self) -> &dyn Any {
1743        self
1744    }
1745
1746    fn to_proto(&self) -> rpc::proto::File {
1747        rpc::proto::File {
1748            worktree_id: self.worktree.id() as u64,
1749            entry_id: self.entry_id.map(|entry_id| entry_id.to_proto()),
1750            path: self.path.to_string_lossy().into(),
1751            mtime: Some(self.mtime.into()),
1752        }
1753    }
1754}
1755
1756impl language::LocalFile for File {
1757    fn abs_path(&self, cx: &AppContext) -> PathBuf {
1758        self.worktree
1759            .read(cx)
1760            .as_local()
1761            .unwrap()
1762            .abs_path
1763            .join(&self.path)
1764    }
1765
1766    fn load(&self, cx: &AppContext) -> Task<Result<String>> {
1767        let worktree = self.worktree.read(cx).as_local().unwrap();
1768        let abs_path = worktree.absolutize(&self.path);
1769        let fs = worktree.fs.clone();
1770        cx.background()
1771            .spawn(async move { fs.load(&abs_path).await })
1772    }
1773
1774    fn buffer_reloaded(
1775        &self,
1776        buffer_id: u64,
1777        version: &clock::Global,
1778        mtime: SystemTime,
1779        cx: &mut MutableAppContext,
1780    ) {
1781        let worktree = self.worktree.read(cx).as_local().unwrap();
1782        if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) {
1783            worktree
1784                .client
1785                .send(proto::BufferReloaded {
1786                    project_id,
1787                    buffer_id,
1788                    version: serialize_version(&version),
1789                    mtime: Some(mtime.into()),
1790                })
1791                .log_err();
1792        }
1793    }
1794}
1795
1796impl File {
1797    pub fn from_proto(
1798        proto: rpc::proto::File,
1799        worktree: ModelHandle<Worktree>,
1800        cx: &AppContext,
1801    ) -> Result<Self> {
1802        let worktree_id = worktree
1803            .read(cx)
1804            .as_remote()
1805            .ok_or_else(|| anyhow!("not remote"))?
1806            .id();
1807
1808        if worktree_id.to_proto() != proto.worktree_id {
1809            return Err(anyhow!("worktree id does not match file"));
1810        }
1811
1812        Ok(Self {
1813            worktree,
1814            path: Path::new(&proto.path).into(),
1815            mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(),
1816            entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
1817            is_local: false,
1818        })
1819    }
1820
1821    pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
1822        file.and_then(|f| f.as_any().downcast_ref())
1823    }
1824
1825    pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
1826        self.worktree.read(cx).id()
1827    }
1828
1829    pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
1830        self.entry_id
1831    }
1832}
1833
1834#[derive(Clone, Debug, PartialEq, Eq)]
1835pub struct Entry {
1836    pub id: ProjectEntryId,
1837    pub kind: EntryKind,
1838    pub path: Arc<Path>,
1839    pub inode: u64,
1840    pub mtime: SystemTime,
1841    pub is_symlink: bool,
1842    pub is_ignored: bool,
1843}
1844
1845#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1846pub enum EntryKind {
1847    PendingDir,
1848    Dir,
1849    File(CharBag),
1850}
1851
1852impl Entry {
1853    fn new(
1854        path: Arc<Path>,
1855        metadata: &fs::Metadata,
1856        next_entry_id: &AtomicUsize,
1857        root_char_bag: CharBag,
1858    ) -> Self {
1859        Self {
1860            id: ProjectEntryId::new(next_entry_id),
1861            kind: if metadata.is_dir {
1862                EntryKind::PendingDir
1863            } else {
1864                EntryKind::File(char_bag_for_path(root_char_bag, &path))
1865            },
1866            path,
1867            inode: metadata.inode,
1868            mtime: metadata.mtime,
1869            is_symlink: metadata.is_symlink,
1870            is_ignored: false,
1871        }
1872    }
1873
1874    pub fn is_dir(&self) -> bool {
1875        matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
1876    }
1877
1878    pub fn is_file(&self) -> bool {
1879        matches!(self.kind, EntryKind::File(_))
1880    }
1881}
1882
1883impl sum_tree::Item for Entry {
1884    type Summary = EntrySummary;
1885
1886    fn summary(&self) -> Self::Summary {
1887        let visible_count = if self.is_ignored { 0 } else { 1 };
1888        let file_count;
1889        let visible_file_count;
1890        if self.is_file() {
1891            file_count = 1;
1892            visible_file_count = visible_count;
1893        } else {
1894            file_count = 0;
1895            visible_file_count = 0;
1896        }
1897
1898        EntrySummary {
1899            max_path: self.path.clone(),
1900            count: 1,
1901            visible_count,
1902            file_count,
1903            visible_file_count,
1904        }
1905    }
1906}
1907
1908impl sum_tree::KeyedItem for Entry {
1909    type Key = PathKey;
1910
1911    fn key(&self) -> Self::Key {
1912        PathKey(self.path.clone())
1913    }
1914}
1915
1916#[derive(Clone, Debug)]
1917pub struct EntrySummary {
1918    max_path: Arc<Path>,
1919    count: usize,
1920    visible_count: usize,
1921    file_count: usize,
1922    visible_file_count: usize,
1923}
1924
1925impl Default for EntrySummary {
1926    fn default() -> Self {
1927        Self {
1928            max_path: Arc::from(Path::new("")),
1929            count: 0,
1930            visible_count: 0,
1931            file_count: 0,
1932            visible_file_count: 0,
1933        }
1934    }
1935}
1936
1937impl sum_tree::Summary for EntrySummary {
1938    type Context = ();
1939
1940    fn add_summary(&mut self, rhs: &Self, _: &()) {
1941        self.max_path = rhs.max_path.clone();
1942        self.count += rhs.count;
1943        self.visible_count += rhs.visible_count;
1944        self.file_count += rhs.file_count;
1945        self.visible_file_count += rhs.visible_file_count;
1946    }
1947}
1948
1949#[derive(Clone, Debug)]
1950struct PathEntry {
1951    id: ProjectEntryId,
1952    path: Arc<Path>,
1953    is_ignored: bool,
1954    scan_id: usize,
1955}
1956
1957impl sum_tree::Item for PathEntry {
1958    type Summary = PathEntrySummary;
1959
1960    fn summary(&self) -> Self::Summary {
1961        PathEntrySummary { max_id: self.id }
1962    }
1963}
1964
1965impl sum_tree::KeyedItem for PathEntry {
1966    type Key = ProjectEntryId;
1967
1968    fn key(&self) -> Self::Key {
1969        self.id
1970    }
1971}
1972
1973#[derive(Clone, Debug, Default)]
1974struct PathEntrySummary {
1975    max_id: ProjectEntryId,
1976}
1977
1978impl sum_tree::Summary for PathEntrySummary {
1979    type Context = ();
1980
1981    fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
1982        self.max_id = summary.max_id;
1983    }
1984}
1985
1986impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
1987    fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
1988        *self = summary.max_id;
1989    }
1990}
1991
1992#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
1993pub struct PathKey(Arc<Path>);
1994
1995impl Default for PathKey {
1996    fn default() -> Self {
1997        Self(Path::new("").into())
1998    }
1999}
2000
2001impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
2002    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2003        self.0 = summary.max_path.clone();
2004    }
2005}
2006
2007struct BackgroundScanner {
2008    fs: Arc<dyn Fs>,
2009    snapshot: Arc<Mutex<LocalSnapshot>>,
2010    notify: UnboundedSender<ScanState>,
2011    executor: Arc<executor::Background>,
2012}
2013
2014impl BackgroundScanner {
2015    fn new(
2016        snapshot: Arc<Mutex<LocalSnapshot>>,
2017        notify: UnboundedSender<ScanState>,
2018        fs: Arc<dyn Fs>,
2019        executor: Arc<executor::Background>,
2020    ) -> Self {
2021        Self {
2022            fs,
2023            snapshot,
2024            notify,
2025            executor,
2026        }
2027    }
2028
2029    fn abs_path(&self) -> Arc<Path> {
2030        self.snapshot.lock().abs_path.clone()
2031    }
2032
2033    fn snapshot(&self) -> LocalSnapshot {
2034        self.snapshot.lock().clone()
2035    }
2036
2037    async fn run(mut self, events_rx: impl Stream<Item = Vec<fsevent::Event>>) {
2038        if self.notify.unbounded_send(ScanState::Scanning).is_err() {
2039            return;
2040        }
2041
2042        if let Err(err) = self.scan_dirs().await {
2043            if self
2044                .notify
2045                .unbounded_send(ScanState::Err(Arc::new(err)))
2046                .is_err()
2047            {
2048                return;
2049            }
2050        }
2051
2052        if self.notify.unbounded_send(ScanState::Idle).is_err() {
2053            return;
2054        }
2055
2056        futures::pin_mut!(events_rx);
2057        while let Some(events) = events_rx.next().await {
2058            if self.notify.unbounded_send(ScanState::Scanning).is_err() {
2059                break;
2060            }
2061
2062            if !self.process_events(events).await {
2063                break;
2064            }
2065
2066            if self.notify.unbounded_send(ScanState::Idle).is_err() {
2067                break;
2068            }
2069        }
2070    }
2071
2072    async fn scan_dirs(&mut self) -> Result<()> {
2073        let root_char_bag;
2074        let next_entry_id;
2075        let is_dir;
2076        {
2077            let snapshot = self.snapshot.lock();
2078            root_char_bag = snapshot.root_char_bag;
2079            next_entry_id = snapshot.next_entry_id.clone();
2080            is_dir = snapshot.root_entry().map_or(false, |e| e.is_dir())
2081        };
2082
2083        if is_dir {
2084            let path: Arc<Path> = Arc::from(Path::new(""));
2085            let abs_path = self.abs_path();
2086            let (tx, rx) = channel::unbounded();
2087            self.executor
2088                .block(tx.send(ScanJob {
2089                    abs_path: abs_path.to_path_buf(),
2090                    path,
2091                    ignore_stack: IgnoreStack::none(),
2092                    scan_queue: tx.clone(),
2093                }))
2094                .unwrap();
2095            drop(tx);
2096
2097            self.executor
2098                .scoped(|scope| {
2099                    for _ in 0..self.executor.num_cpus() {
2100                        scope.spawn(async {
2101                            while let Ok(job) = rx.recv().await {
2102                                if let Err(err) = self
2103                                    .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2104                                    .await
2105                                {
2106                                    log::error!("error scanning {:?}: {}", job.abs_path, err);
2107                                }
2108                            }
2109                        });
2110                    }
2111                })
2112                .await;
2113        }
2114
2115        Ok(())
2116    }
2117
2118    async fn scan_dir(
2119        &self,
2120        root_char_bag: CharBag,
2121        next_entry_id: Arc<AtomicUsize>,
2122        job: &ScanJob,
2123    ) -> Result<()> {
2124        let mut new_entries: Vec<Entry> = Vec::new();
2125        let mut new_jobs: Vec<ScanJob> = Vec::new();
2126        let mut ignore_stack = job.ignore_stack.clone();
2127        let mut new_ignore = None;
2128
2129        let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2130        while let Some(child_abs_path) = child_paths.next().await {
2131            let child_abs_path = match child_abs_path {
2132                Ok(child_abs_path) => child_abs_path,
2133                Err(error) => {
2134                    log::error!("error processing entry {:?}", error);
2135                    continue;
2136                }
2137            };
2138            let child_name = child_abs_path.file_name().unwrap();
2139            let child_path: Arc<Path> = job.path.join(child_name).into();
2140            let child_metadata = match self.fs.metadata(&child_abs_path).await? {
2141                Some(metadata) => metadata,
2142                None => continue,
2143            };
2144
2145            // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2146            if child_name == *GITIGNORE {
2147                match build_gitignore(&child_abs_path, self.fs.as_ref()) {
2148                    Ok(ignore) => {
2149                        let ignore = Arc::new(ignore);
2150                        ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2151                        new_ignore = Some(ignore);
2152                    }
2153                    Err(error) => {
2154                        log::error!(
2155                            "error loading .gitignore file {:?} - {:?}",
2156                            child_name,
2157                            error
2158                        );
2159                    }
2160                }
2161
2162                // Update ignore status of any child entries we've already processed to reflect the
2163                // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2164                // there should rarely be too numerous. Update the ignore stack associated with any
2165                // new jobs as well.
2166                let mut new_jobs = new_jobs.iter_mut();
2167                for entry in &mut new_entries {
2168                    entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2169                    if entry.is_dir() {
2170                        new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
2171                            IgnoreStack::all()
2172                        } else {
2173                            ignore_stack.clone()
2174                        };
2175                    }
2176                }
2177            }
2178
2179            let mut child_entry = Entry::new(
2180                child_path.clone(),
2181                &child_metadata,
2182                &next_entry_id,
2183                root_char_bag,
2184            );
2185
2186            if child_metadata.is_dir {
2187                let is_ignored = ignore_stack.is_path_ignored(&child_path, true);
2188                child_entry.is_ignored = is_ignored;
2189                new_entries.push(child_entry);
2190                new_jobs.push(ScanJob {
2191                    abs_path: child_abs_path,
2192                    path: child_path,
2193                    ignore_stack: if is_ignored {
2194                        IgnoreStack::all()
2195                    } else {
2196                        ignore_stack.clone()
2197                    },
2198                    scan_queue: job.scan_queue.clone(),
2199                });
2200            } else {
2201                child_entry.is_ignored = ignore_stack.is_path_ignored(&child_path, false);
2202                new_entries.push(child_entry);
2203            };
2204        }
2205
2206        self.snapshot
2207            .lock()
2208            .populate_dir(job.path.clone(), new_entries, new_ignore);
2209        for new_job in new_jobs {
2210            job.scan_queue.send(new_job).await.unwrap();
2211        }
2212
2213        Ok(())
2214    }
2215
2216    async fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
2217        events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2218        events.dedup_by(|a, b| a.path.starts_with(&b.path));
2219
2220        let root_char_bag;
2221        let root_abs_path;
2222        let next_entry_id;
2223        {
2224            let snapshot = self.snapshot.lock();
2225            root_char_bag = snapshot.root_char_bag;
2226            root_abs_path = snapshot.abs_path.clone();
2227            next_entry_id = snapshot.next_entry_id.clone();
2228        }
2229
2230        let root_abs_path = if let Ok(abs_path) = self.fs.canonicalize(&root_abs_path).await {
2231            abs_path
2232        } else {
2233            return false;
2234        };
2235        let metadata = futures::future::join_all(
2236            events
2237                .iter()
2238                .map(|event| self.fs.metadata(&event.path))
2239                .collect::<Vec<_>>(),
2240        )
2241        .await;
2242
2243        // Hold the snapshot lock while clearing and re-inserting the root entries
2244        // for each event. This way, the snapshot is not observable to the foreground
2245        // thread while this operation is in-progress.
2246        let (scan_queue_tx, scan_queue_rx) = channel::unbounded();
2247        {
2248            let mut snapshot = self.snapshot.lock();
2249            snapshot.scan_id += 1;
2250            for event in &events {
2251                if let Ok(path) = event.path.strip_prefix(&root_abs_path) {
2252                    snapshot.remove_path(&path);
2253                }
2254            }
2255
2256            for (event, metadata) in events.into_iter().zip(metadata.into_iter()) {
2257                let path: Arc<Path> = match event.path.strip_prefix(&root_abs_path) {
2258                    Ok(path) => Arc::from(path.to_path_buf()),
2259                    Err(_) => {
2260                        log::error!(
2261                            "unexpected event {:?} for root path {:?}",
2262                            event.path,
2263                            root_abs_path
2264                        );
2265                        continue;
2266                    }
2267                };
2268
2269                match metadata {
2270                    Ok(Some(metadata)) => {
2271                        let ignore_stack = snapshot.ignore_stack_for_path(&path, metadata.is_dir);
2272                        let mut fs_entry = Entry::new(
2273                            path.clone(),
2274                            &metadata,
2275                            snapshot.next_entry_id.as_ref(),
2276                            snapshot.root_char_bag,
2277                        );
2278                        fs_entry.is_ignored = ignore_stack.is_all();
2279                        snapshot.insert_entry(fs_entry, self.fs.as_ref());
2280                        if metadata.is_dir {
2281                            self.executor
2282                                .block(scan_queue_tx.send(ScanJob {
2283                                    abs_path: event.path,
2284                                    path,
2285                                    ignore_stack,
2286                                    scan_queue: scan_queue_tx.clone(),
2287                                }))
2288                                .unwrap();
2289                        }
2290                    }
2291                    Ok(None) => {}
2292                    Err(err) => {
2293                        // TODO - create a special 'error' entry in the entries tree to mark this
2294                        log::error!("error reading file on event {:?}", err);
2295                    }
2296                }
2297            }
2298            drop(scan_queue_tx);
2299        }
2300
2301        // Scan any directories that were created as part of this event batch.
2302        self.executor
2303            .scoped(|scope| {
2304                for _ in 0..self.executor.num_cpus() {
2305                    scope.spawn(async {
2306                        while let Ok(job) = scan_queue_rx.recv().await {
2307                            if let Err(err) = self
2308                                .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2309                                .await
2310                            {
2311                                log::error!("error scanning {:?}: {}", job.abs_path, err);
2312                            }
2313                        }
2314                    });
2315                }
2316            })
2317            .await;
2318
2319        // Attempt to detect renames only over a single batch of file-system events.
2320        self.snapshot.lock().removed_entry_ids.clear();
2321
2322        self.update_ignore_statuses().await;
2323        true
2324    }
2325
2326    async fn update_ignore_statuses(&self) {
2327        let mut snapshot = self.snapshot();
2328
2329        let mut ignores_to_update = Vec::new();
2330        let mut ignores_to_delete = Vec::new();
2331        for (parent_path, (_, scan_id)) in &snapshot.ignores {
2332            if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
2333                ignores_to_update.push(parent_path.clone());
2334            }
2335
2336            let ignore_path = parent_path.join(&*GITIGNORE);
2337            if snapshot.entry_for_path(ignore_path).is_none() {
2338                ignores_to_delete.push(parent_path.clone());
2339            }
2340        }
2341
2342        for parent_path in ignores_to_delete {
2343            snapshot.ignores.remove(&parent_path);
2344            self.snapshot.lock().ignores.remove(&parent_path);
2345        }
2346
2347        let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2348        ignores_to_update.sort_unstable();
2349        let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2350        while let Some(parent_path) = ignores_to_update.next() {
2351            while ignores_to_update
2352                .peek()
2353                .map_or(false, |p| p.starts_with(&parent_path))
2354            {
2355                ignores_to_update.next().unwrap();
2356            }
2357
2358            let ignore_stack = snapshot.ignore_stack_for_path(&parent_path, true);
2359            ignore_queue_tx
2360                .send(UpdateIgnoreStatusJob {
2361                    path: parent_path,
2362                    ignore_stack,
2363                    ignore_queue: ignore_queue_tx.clone(),
2364                })
2365                .await
2366                .unwrap();
2367        }
2368        drop(ignore_queue_tx);
2369
2370        self.executor
2371            .scoped(|scope| {
2372                for _ in 0..self.executor.num_cpus() {
2373                    scope.spawn(async {
2374                        while let Ok(job) = ignore_queue_rx.recv().await {
2375                            self.update_ignore_status(job, &snapshot).await;
2376                        }
2377                    });
2378                }
2379            })
2380            .await;
2381    }
2382
2383    async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
2384        let mut ignore_stack = job.ignore_stack;
2385        if let Some((ignore, _)) = snapshot.ignores.get(&job.path) {
2386            ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2387        }
2388
2389        let mut entries_by_id_edits = Vec::new();
2390        let mut entries_by_path_edits = Vec::new();
2391        for mut entry in snapshot.child_entries(&job.path).cloned() {
2392            let was_ignored = entry.is_ignored;
2393            entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2394            if entry.is_dir() {
2395                let child_ignore_stack = if entry.is_ignored {
2396                    IgnoreStack::all()
2397                } else {
2398                    ignore_stack.clone()
2399                };
2400                job.ignore_queue
2401                    .send(UpdateIgnoreStatusJob {
2402                        path: entry.path.clone(),
2403                        ignore_stack: child_ignore_stack,
2404                        ignore_queue: job.ignore_queue.clone(),
2405                    })
2406                    .await
2407                    .unwrap();
2408            }
2409
2410            if entry.is_ignored != was_ignored {
2411                let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
2412                path_entry.scan_id = snapshot.scan_id;
2413                path_entry.is_ignored = entry.is_ignored;
2414                entries_by_id_edits.push(Edit::Insert(path_entry));
2415                entries_by_path_edits.push(Edit::Insert(entry));
2416            }
2417        }
2418
2419        let mut snapshot = self.snapshot.lock();
2420        snapshot.entries_by_path.edit(entries_by_path_edits, &());
2421        snapshot.entries_by_id.edit(entries_by_id_edits, &());
2422    }
2423}
2424
2425fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
2426    let mut result = root_char_bag;
2427    result.extend(
2428        path.to_string_lossy()
2429            .chars()
2430            .map(|c| c.to_ascii_lowercase()),
2431    );
2432    result
2433}
2434
2435struct ScanJob {
2436    abs_path: PathBuf,
2437    path: Arc<Path>,
2438    ignore_stack: Arc<IgnoreStack>,
2439    scan_queue: Sender<ScanJob>,
2440}
2441
2442struct UpdateIgnoreStatusJob {
2443    path: Arc<Path>,
2444    ignore_stack: Arc<IgnoreStack>,
2445    ignore_queue: Sender<UpdateIgnoreStatusJob>,
2446}
2447
2448pub trait WorktreeHandle {
2449    #[cfg(any(test, feature = "test-support"))]
2450    fn flush_fs_events<'a>(
2451        &self,
2452        cx: &'a gpui::TestAppContext,
2453    ) -> futures::future::LocalBoxFuture<'a, ()>;
2454}
2455
2456impl WorktreeHandle for ModelHandle<Worktree> {
2457    // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
2458    // occurred before the worktree was constructed. These events can cause the worktree to perfrom
2459    // extra directory scans, and emit extra scan-state notifications.
2460    //
2461    // This function mutates the worktree's directory and waits for those mutations to be picked up,
2462    // to ensure that all redundant FS events have already been processed.
2463    #[cfg(any(test, feature = "test-support"))]
2464    fn flush_fs_events<'a>(
2465        &self,
2466        cx: &'a gpui::TestAppContext,
2467    ) -> futures::future::LocalBoxFuture<'a, ()> {
2468        use smol::future::FutureExt;
2469
2470        let filename = "fs-event-sentinel";
2471        let tree = self.clone();
2472        let (fs, root_path) = self.read_with(cx, |tree, _| {
2473            let tree = tree.as_local().unwrap();
2474            (tree.fs.clone(), tree.abs_path().clone())
2475        });
2476
2477        async move {
2478            fs.create_file(&root_path.join(filename), Default::default())
2479                .await
2480                .unwrap();
2481            tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_some())
2482                .await;
2483
2484            fs.remove_file(&root_path.join(filename), Default::default())
2485                .await
2486                .unwrap();
2487            tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_none())
2488                .await;
2489
2490            cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2491                .await;
2492        }
2493        .boxed_local()
2494    }
2495}
2496
2497#[derive(Clone, Debug)]
2498struct TraversalProgress<'a> {
2499    max_path: &'a Path,
2500    count: usize,
2501    visible_count: usize,
2502    file_count: usize,
2503    visible_file_count: usize,
2504}
2505
2506impl<'a> TraversalProgress<'a> {
2507    fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
2508        match (include_ignored, include_dirs) {
2509            (true, true) => self.count,
2510            (true, false) => self.file_count,
2511            (false, true) => self.visible_count,
2512            (false, false) => self.visible_file_count,
2513        }
2514    }
2515}
2516
2517impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
2518    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2519        self.max_path = summary.max_path.as_ref();
2520        self.count += summary.count;
2521        self.visible_count += summary.visible_count;
2522        self.file_count += summary.file_count;
2523        self.visible_file_count += summary.visible_file_count;
2524    }
2525}
2526
2527impl<'a> Default for TraversalProgress<'a> {
2528    fn default() -> Self {
2529        Self {
2530            max_path: Path::new(""),
2531            count: 0,
2532            visible_count: 0,
2533            file_count: 0,
2534            visible_file_count: 0,
2535        }
2536    }
2537}
2538
2539pub struct Traversal<'a> {
2540    cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
2541    include_ignored: bool,
2542    include_dirs: bool,
2543}
2544
2545impl<'a> Traversal<'a> {
2546    pub fn advance(&mut self) -> bool {
2547        self.advance_to_offset(self.offset() + 1)
2548    }
2549
2550    pub fn advance_to_offset(&mut self, offset: usize) -> bool {
2551        self.cursor.seek_forward(
2552            &TraversalTarget::Count {
2553                count: offset,
2554                include_dirs: self.include_dirs,
2555                include_ignored: self.include_ignored,
2556            },
2557            Bias::Right,
2558            &(),
2559        )
2560    }
2561
2562    pub fn advance_to_sibling(&mut self) -> bool {
2563        while let Some(entry) = self.cursor.item() {
2564            self.cursor.seek_forward(
2565                &TraversalTarget::PathSuccessor(&entry.path),
2566                Bias::Left,
2567                &(),
2568            );
2569            if let Some(entry) = self.cursor.item() {
2570                if (self.include_dirs || !entry.is_dir())
2571                    && (self.include_ignored || !entry.is_ignored)
2572                {
2573                    return true;
2574                }
2575            }
2576        }
2577        false
2578    }
2579
2580    pub fn entry(&self) -> Option<&'a Entry> {
2581        self.cursor.item()
2582    }
2583
2584    pub fn offset(&self) -> usize {
2585        self.cursor
2586            .start()
2587            .count(self.include_dirs, self.include_ignored)
2588    }
2589}
2590
2591impl<'a> Iterator for Traversal<'a> {
2592    type Item = &'a Entry;
2593
2594    fn next(&mut self) -> Option<Self::Item> {
2595        if let Some(item) = self.entry() {
2596            self.advance();
2597            Some(item)
2598        } else {
2599            None
2600        }
2601    }
2602}
2603
2604#[derive(Debug)]
2605enum TraversalTarget<'a> {
2606    Path(&'a Path),
2607    PathSuccessor(&'a Path),
2608    Count {
2609        count: usize,
2610        include_ignored: bool,
2611        include_dirs: bool,
2612    },
2613}
2614
2615impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
2616    fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
2617        match self {
2618            TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
2619            TraversalTarget::PathSuccessor(path) => {
2620                if !cursor_location.max_path.starts_with(path) {
2621                    Ordering::Equal
2622                } else {
2623                    Ordering::Greater
2624                }
2625            }
2626            TraversalTarget::Count {
2627                count,
2628                include_dirs,
2629                include_ignored,
2630            } => Ord::cmp(
2631                count,
2632                &cursor_location.count(*include_dirs, *include_ignored),
2633            ),
2634        }
2635    }
2636}
2637
2638struct ChildEntriesIter<'a> {
2639    parent_path: &'a Path,
2640    traversal: Traversal<'a>,
2641}
2642
2643impl<'a> Iterator for ChildEntriesIter<'a> {
2644    type Item = &'a Entry;
2645
2646    fn next(&mut self) -> Option<Self::Item> {
2647        if let Some(item) = self.traversal.entry() {
2648            if item.path.starts_with(&self.parent_path) {
2649                self.traversal.advance_to_sibling();
2650                return Some(item);
2651            }
2652        }
2653        None
2654    }
2655}
2656
2657impl<'a> From<&'a Entry> for proto::Entry {
2658    fn from(entry: &'a Entry) -> Self {
2659        Self {
2660            id: entry.id.to_proto(),
2661            is_dir: entry.is_dir(),
2662            path: entry.path.as_os_str().as_bytes().to_vec(),
2663            inode: entry.inode,
2664            mtime: Some(entry.mtime.into()),
2665            is_symlink: entry.is_symlink,
2666            is_ignored: entry.is_ignored,
2667        }
2668    }
2669}
2670
2671impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
2672    type Error = anyhow::Error;
2673
2674    fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
2675        if let Some(mtime) = entry.mtime {
2676            let kind = if entry.is_dir {
2677                EntryKind::Dir
2678            } else {
2679                let mut char_bag = root_char_bag.clone();
2680                char_bag.extend(
2681                    String::from_utf8_lossy(&entry.path)
2682                        .chars()
2683                        .map(|c| c.to_ascii_lowercase()),
2684                );
2685                EntryKind::File(char_bag)
2686            };
2687            let path: Arc<Path> = PathBuf::from(OsString::from_vec(entry.path)).into();
2688            Ok(Entry {
2689                id: ProjectEntryId::from_proto(entry.id),
2690                kind,
2691                path: path.clone(),
2692                inode: entry.inode,
2693                mtime: mtime.into(),
2694                is_symlink: entry.is_symlink,
2695                is_ignored: entry.is_ignored,
2696            })
2697        } else {
2698            Err(anyhow!(
2699                "missing mtime in remote worktree entry {:?}",
2700                entry.path
2701            ))
2702        }
2703    }
2704}
2705
2706#[cfg(test)]
2707mod tests {
2708    use super::*;
2709    use crate::fs::FakeFs;
2710    use anyhow::Result;
2711    use client::test::FakeHttpClient;
2712    use fs::RealFs;
2713    use gpui::TestAppContext;
2714    use rand::prelude::*;
2715    use serde_json::json;
2716    use std::{
2717        env,
2718        fmt::Write,
2719        time::{SystemTime, UNIX_EPOCH},
2720    };
2721    use util::test::temp_tree;
2722
2723    #[gpui::test]
2724    async fn test_traversal(cx: &mut TestAppContext) {
2725        let fs = FakeFs::new(cx.background());
2726        fs.insert_tree(
2727            "/root",
2728            json!({
2729               ".gitignore": "a/b\n",
2730               "a": {
2731                   "b": "",
2732                   "c": "",
2733               }
2734            }),
2735        )
2736        .await;
2737
2738        let http_client = FakeHttpClient::with_404_response();
2739        let client = Client::new(http_client);
2740
2741        let tree = Worktree::local(
2742            client,
2743            Arc::from(Path::new("/root")),
2744            true,
2745            fs,
2746            Default::default(),
2747            &mut cx.to_async(),
2748        )
2749        .await
2750        .unwrap();
2751        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2752            .await;
2753
2754        tree.read_with(cx, |tree, _| {
2755            assert_eq!(
2756                tree.entries(false)
2757                    .map(|entry| entry.path.as_ref())
2758                    .collect::<Vec<_>>(),
2759                vec![
2760                    Path::new(""),
2761                    Path::new(".gitignore"),
2762                    Path::new("a"),
2763                    Path::new("a/c"),
2764                ]
2765            );
2766            assert_eq!(
2767                tree.entries(true)
2768                    .map(|entry| entry.path.as_ref())
2769                    .collect::<Vec<_>>(),
2770                vec![
2771                    Path::new(""),
2772                    Path::new(".gitignore"),
2773                    Path::new("a"),
2774                    Path::new("a/b"),
2775                    Path::new("a/c"),
2776                ]
2777            );
2778        })
2779    }
2780
2781    #[gpui::test]
2782    async fn test_rescan_with_gitignore(cx: &mut TestAppContext) {
2783        let dir = temp_tree(json!({
2784            ".git": {},
2785            ".gitignore": "ignored-dir\n",
2786            "tracked-dir": {
2787                "tracked-file1": "tracked contents",
2788            },
2789            "ignored-dir": {
2790                "ignored-file1": "ignored contents",
2791            }
2792        }));
2793
2794        let http_client = FakeHttpClient::with_404_response();
2795        let client = Client::new(http_client.clone());
2796
2797        let tree = Worktree::local(
2798            client,
2799            dir.path(),
2800            true,
2801            Arc::new(RealFs),
2802            Default::default(),
2803            &mut cx.to_async(),
2804        )
2805        .await
2806        .unwrap();
2807        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2808            .await;
2809        tree.flush_fs_events(&cx).await;
2810        cx.read(|cx| {
2811            let tree = tree.read(cx);
2812            let tracked = tree.entry_for_path("tracked-dir/tracked-file1").unwrap();
2813            let ignored = tree.entry_for_path("ignored-dir/ignored-file1").unwrap();
2814            assert_eq!(tracked.is_ignored, false);
2815            assert_eq!(ignored.is_ignored, true);
2816        });
2817
2818        std::fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
2819        std::fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
2820        tree.flush_fs_events(&cx).await;
2821        cx.read(|cx| {
2822            let tree = tree.read(cx);
2823            let dot_git = tree.entry_for_path(".git").unwrap();
2824            let tracked = tree.entry_for_path("tracked-dir/tracked-file2").unwrap();
2825            let ignored = tree.entry_for_path("ignored-dir/ignored-file2").unwrap();
2826            assert_eq!(tracked.is_ignored, false);
2827            assert_eq!(ignored.is_ignored, true);
2828            assert_eq!(dot_git.is_ignored, true);
2829        });
2830    }
2831
2832    #[gpui::test]
2833    async fn test_write_file(cx: &mut TestAppContext) {
2834        let dir = temp_tree(json!({
2835            ".git": {},
2836            ".gitignore": "ignored-dir\n",
2837            "tracked-dir": {},
2838            "ignored-dir": {}
2839        }));
2840
2841        let http_client = FakeHttpClient::with_404_response();
2842        let client = Client::new(http_client.clone());
2843
2844        let tree = Worktree::local(
2845            client,
2846            dir.path(),
2847            true,
2848            Arc::new(RealFs),
2849            Default::default(),
2850            &mut cx.to_async(),
2851        )
2852        .await
2853        .unwrap();
2854        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2855            .await;
2856        tree.flush_fs_events(&cx).await;
2857
2858        tree.update(cx, |tree, cx| {
2859            tree.as_local().unwrap().write_file(
2860                Path::new("tracked-dir/file.txt"),
2861                "hello".into(),
2862                cx,
2863            )
2864        })
2865        .await
2866        .unwrap();
2867        tree.update(cx, |tree, cx| {
2868            tree.as_local().unwrap().write_file(
2869                Path::new("ignored-dir/file.txt"),
2870                "world".into(),
2871                cx,
2872            )
2873        })
2874        .await
2875        .unwrap();
2876
2877        tree.read_with(cx, |tree, _| {
2878            let tracked = tree.entry_for_path("tracked-dir/file.txt").unwrap();
2879            let ignored = tree.entry_for_path("ignored-dir/file.txt").unwrap();
2880            assert_eq!(tracked.is_ignored, false);
2881            assert_eq!(ignored.is_ignored, true);
2882        });
2883    }
2884
2885    #[gpui::test(iterations = 100)]
2886    fn test_random(mut rng: StdRng) {
2887        let operations = env::var("OPERATIONS")
2888            .map(|o| o.parse().unwrap())
2889            .unwrap_or(40);
2890        let initial_entries = env::var("INITIAL_ENTRIES")
2891            .map(|o| o.parse().unwrap())
2892            .unwrap_or(20);
2893
2894        let root_dir = tempdir::TempDir::new("worktree-test").unwrap();
2895        for _ in 0..initial_entries {
2896            randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
2897        }
2898        log::info!("Generated initial tree");
2899
2900        let (notify_tx, _notify_rx) = mpsc::unbounded();
2901        let fs = Arc::new(RealFs);
2902        let next_entry_id = Arc::new(AtomicUsize::new(0));
2903        let mut initial_snapshot = LocalSnapshot {
2904            abs_path: root_dir.path().into(),
2905            removed_entry_ids: Default::default(),
2906            ignores: Default::default(),
2907            next_entry_id: next_entry_id.clone(),
2908            snapshot: Snapshot {
2909                id: WorktreeId::from_usize(0),
2910                entries_by_path: Default::default(),
2911                entries_by_id: Default::default(),
2912                root_name: Default::default(),
2913                root_char_bag: Default::default(),
2914                scan_id: 0,
2915            },
2916        };
2917        initial_snapshot.insert_entry(
2918            Entry::new(
2919                Path::new("").into(),
2920                &smol::block_on(fs.metadata(root_dir.path()))
2921                    .unwrap()
2922                    .unwrap(),
2923                &next_entry_id,
2924                Default::default(),
2925            ),
2926            fs.as_ref(),
2927        );
2928        let mut scanner = BackgroundScanner::new(
2929            Arc::new(Mutex::new(initial_snapshot.clone())),
2930            notify_tx,
2931            fs.clone(),
2932            Arc::new(gpui::executor::Background::new()),
2933        );
2934        smol::block_on(scanner.scan_dirs()).unwrap();
2935        scanner.snapshot().check_invariants();
2936
2937        let mut events = Vec::new();
2938        let mut snapshots = Vec::new();
2939        let mut mutations_len = operations;
2940        while mutations_len > 1 {
2941            if !events.is_empty() && rng.gen_bool(0.4) {
2942                let len = rng.gen_range(0..=events.len());
2943                let to_deliver = events.drain(0..len).collect::<Vec<_>>();
2944                log::info!("Delivering events: {:#?}", to_deliver);
2945                smol::block_on(scanner.process_events(to_deliver));
2946                scanner.snapshot().check_invariants();
2947            } else {
2948                events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
2949                mutations_len -= 1;
2950            }
2951
2952            if rng.gen_bool(0.2) {
2953                snapshots.push(scanner.snapshot());
2954            }
2955        }
2956        log::info!("Quiescing: {:#?}", events);
2957        smol::block_on(scanner.process_events(events));
2958        scanner.snapshot().check_invariants();
2959
2960        let (notify_tx, _notify_rx) = mpsc::unbounded();
2961        let mut new_scanner = BackgroundScanner::new(
2962            Arc::new(Mutex::new(initial_snapshot)),
2963            notify_tx,
2964            scanner.fs.clone(),
2965            scanner.executor.clone(),
2966        );
2967        smol::block_on(new_scanner.scan_dirs()).unwrap();
2968        assert_eq!(
2969            scanner.snapshot().to_vec(true),
2970            new_scanner.snapshot().to_vec(true)
2971        );
2972
2973        for mut prev_snapshot in snapshots {
2974            let include_ignored = rng.gen::<bool>();
2975            if !include_ignored {
2976                let mut entries_by_path_edits = Vec::new();
2977                let mut entries_by_id_edits = Vec::new();
2978                for entry in prev_snapshot
2979                    .entries_by_id
2980                    .cursor::<()>()
2981                    .filter(|e| e.is_ignored)
2982                {
2983                    entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
2984                    entries_by_id_edits.push(Edit::Remove(entry.id));
2985                }
2986
2987                prev_snapshot
2988                    .entries_by_path
2989                    .edit(entries_by_path_edits, &());
2990                prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
2991            }
2992
2993            let update = scanner
2994                .snapshot()
2995                .build_update(&prev_snapshot, 0, 0, include_ignored);
2996            prev_snapshot.apply_remote_update(update).unwrap();
2997            assert_eq!(
2998                prev_snapshot.to_vec(true),
2999                scanner.snapshot().to_vec(include_ignored)
3000            );
3001        }
3002    }
3003
3004    fn randomly_mutate_tree(
3005        root_path: &Path,
3006        insertion_probability: f64,
3007        rng: &mut impl Rng,
3008    ) -> Result<Vec<fsevent::Event>> {
3009        let root_path = root_path.canonicalize().unwrap();
3010        let (dirs, files) = read_dir_recursive(root_path.clone());
3011
3012        let mut events = Vec::new();
3013        let mut record_event = |path: PathBuf| {
3014            events.push(fsevent::Event {
3015                event_id: SystemTime::now()
3016                    .duration_since(UNIX_EPOCH)
3017                    .unwrap()
3018                    .as_secs(),
3019                flags: fsevent::StreamFlags::empty(),
3020                path,
3021            });
3022        };
3023
3024        if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
3025            let path = dirs.choose(rng).unwrap();
3026            let new_path = path.join(gen_name(rng));
3027
3028            if rng.gen() {
3029                log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
3030                std::fs::create_dir(&new_path)?;
3031            } else {
3032                log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
3033                std::fs::write(&new_path, "")?;
3034            }
3035            record_event(new_path);
3036        } else if rng.gen_bool(0.05) {
3037            let ignore_dir_path = dirs.choose(rng).unwrap();
3038            let ignore_path = ignore_dir_path.join(&*GITIGNORE);
3039
3040            let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
3041            let files_to_ignore = {
3042                let len = rng.gen_range(0..=subfiles.len());
3043                subfiles.choose_multiple(rng, len)
3044            };
3045            let dirs_to_ignore = {
3046                let len = rng.gen_range(0..subdirs.len());
3047                subdirs.choose_multiple(rng, len)
3048            };
3049
3050            let mut ignore_contents = String::new();
3051            for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
3052                write!(
3053                    ignore_contents,
3054                    "{}\n",
3055                    path_to_ignore
3056                        .strip_prefix(&ignore_dir_path)?
3057                        .to_str()
3058                        .unwrap()
3059                )
3060                .unwrap();
3061            }
3062            log::info!(
3063                "Creating {:?} with contents:\n{}",
3064                ignore_path.strip_prefix(&root_path)?,
3065                ignore_contents
3066            );
3067            std::fs::write(&ignore_path, ignore_contents).unwrap();
3068            record_event(ignore_path);
3069        } else {
3070            let old_path = {
3071                let file_path = files.choose(rng);
3072                let dir_path = dirs[1..].choose(rng);
3073                file_path.into_iter().chain(dir_path).choose(rng).unwrap()
3074            };
3075
3076            let is_rename = rng.gen();
3077            if is_rename {
3078                let new_path_parent = dirs
3079                    .iter()
3080                    .filter(|d| !d.starts_with(old_path))
3081                    .choose(rng)
3082                    .unwrap();
3083
3084                let overwrite_existing_dir =
3085                    !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
3086                let new_path = if overwrite_existing_dir {
3087                    std::fs::remove_dir_all(&new_path_parent).ok();
3088                    new_path_parent.to_path_buf()
3089                } else {
3090                    new_path_parent.join(gen_name(rng))
3091                };
3092
3093                log::info!(
3094                    "Renaming {:?} to {}{:?}",
3095                    old_path.strip_prefix(&root_path)?,
3096                    if overwrite_existing_dir {
3097                        "overwrite "
3098                    } else {
3099                        ""
3100                    },
3101                    new_path.strip_prefix(&root_path)?
3102                );
3103                std::fs::rename(&old_path, &new_path)?;
3104                record_event(old_path.clone());
3105                record_event(new_path);
3106            } else if old_path.is_dir() {
3107                let (dirs, files) = read_dir_recursive(old_path.clone());
3108
3109                log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
3110                std::fs::remove_dir_all(&old_path).unwrap();
3111                for file in files {
3112                    record_event(file);
3113                }
3114                for dir in dirs {
3115                    record_event(dir);
3116                }
3117            } else {
3118                log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
3119                std::fs::remove_file(old_path).unwrap();
3120                record_event(old_path.clone());
3121            }
3122        }
3123
3124        Ok(events)
3125    }
3126
3127    fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
3128        let child_entries = std::fs::read_dir(&path).unwrap();
3129        let mut dirs = vec![path];
3130        let mut files = Vec::new();
3131        for child_entry in child_entries {
3132            let child_path = child_entry.unwrap().path();
3133            if child_path.is_dir() {
3134                let (child_dirs, child_files) = read_dir_recursive(child_path);
3135                dirs.extend(child_dirs);
3136                files.extend(child_files);
3137            } else {
3138                files.push(child_path);
3139            }
3140        }
3141        (dirs, files)
3142    }
3143
3144    fn gen_name(rng: &mut impl Rng) -> String {
3145        (0..6)
3146            .map(|_| rng.sample(rand::distributions::Alphanumeric))
3147            .map(char::from)
3148            .collect()
3149    }
3150
3151    impl LocalSnapshot {
3152        fn check_invariants(&self) {
3153            let mut files = self.files(true, 0);
3154            let mut visible_files = self.files(false, 0);
3155            for entry in self.entries_by_path.cursor::<()>() {
3156                if entry.is_file() {
3157                    assert_eq!(files.next().unwrap().inode, entry.inode);
3158                    if !entry.is_ignored {
3159                        assert_eq!(visible_files.next().unwrap().inode, entry.inode);
3160                    }
3161                }
3162            }
3163            assert!(files.next().is_none());
3164            assert!(visible_files.next().is_none());
3165
3166            let mut bfs_paths = Vec::new();
3167            let mut stack = vec![Path::new("")];
3168            while let Some(path) = stack.pop() {
3169                bfs_paths.push(path);
3170                let ix = stack.len();
3171                for child_entry in self.child_entries(path) {
3172                    stack.insert(ix, &child_entry.path);
3173                }
3174            }
3175
3176            let dfs_paths_via_iter = self
3177                .entries_by_path
3178                .cursor::<()>()
3179                .map(|e| e.path.as_ref())
3180                .collect::<Vec<_>>();
3181            assert_eq!(bfs_paths, dfs_paths_via_iter);
3182
3183            let dfs_paths_via_traversal = self
3184                .entries(true)
3185                .map(|e| e.path.as_ref())
3186                .collect::<Vec<_>>();
3187            assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter);
3188
3189            for (ignore_parent_path, _) in &self.ignores {
3190                assert!(self.entry_for_path(ignore_parent_path).is_some());
3191                assert!(self
3192                    .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
3193                    .is_some());
3194            }
3195        }
3196
3197        fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
3198            let mut paths = Vec::new();
3199            for entry in self.entries_by_path.cursor::<()>() {
3200                if include_ignored || !entry.is_ignored {
3201                    paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
3202                }
3203            }
3204            paths.sort_by(|a, b| a.0.cmp(&b.0));
3205            paths
3206        }
3207    }
3208}