worktree.rs

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