worktree.rs

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