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