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