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