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