1use crate::{
2 copy_recursive, ignore::IgnoreStack, DiagnosticSummary, ProjectEntryId, RemoveOptions,
3};
4use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
5use anyhow::{anyhow, Context, Result};
6use client::{proto, Client};
7use clock::ReplicaId;
8use collections::{HashMap, VecDeque};
9use fs::{
10 repository::{GitFileStatus, GitRepository, RepoPath, RepoPathDescendants},
11 Fs, LineEnding,
12};
13use futures::{
14 channel::{
15 mpsc::{self, UnboundedSender},
16 oneshot,
17 },
18 select_biased,
19 task::Poll,
20 Stream, StreamExt,
21};
22use fuzzy::CharBag;
23use git::{DOT_GIT, GITIGNORE};
24use gpui::{executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
25use language::{
26 proto::{
27 deserialize_fingerprint, deserialize_version, serialize_fingerprint, serialize_line_ending,
28 serialize_version,
29 },
30 Buffer, DiagnosticEntry, File as _, PointUtf16, Rope, RopeFingerprint, Unclipped,
31};
32use lsp::LanguageServerId;
33use parking_lot::Mutex;
34use postage::{
35 barrier,
36 prelude::{Sink as _, Stream as _},
37 watch,
38};
39use smol::channel::{self, Sender};
40use std::{
41 any::Any,
42 cmp::{self, Ordering},
43 convert::TryFrom,
44 ffi::OsStr,
45 fmt,
46 future::Future,
47 mem,
48 ops::{Deref, DerefMut},
49 path::{Path, PathBuf},
50 pin::Pin,
51 sync::{
52 atomic::{AtomicUsize, Ordering::SeqCst},
53 Arc,
54 },
55 time::{Duration, SystemTime},
56};
57use sum_tree::{Bias, Edit, SeekTarget, SumTree, TreeMap, TreeSet};
58use util::{paths::HOME, ResultExt, TakeUntilExt, TryFutureExt};
59
60#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
61pub struct WorktreeId(usize);
62
63pub enum Worktree {
64 Local(LocalWorktree),
65 Remote(RemoteWorktree),
66}
67
68pub struct LocalWorktree {
69 snapshot: LocalSnapshot,
70 path_changes_tx: channel::Sender<(Vec<PathBuf>, barrier::Sender)>,
71 is_scanning: (watch::Sender<bool>, watch::Receiver<bool>),
72 _background_scanner_task: Task<()>,
73 share: Option<ShareState>,
74 diagnostics: HashMap<
75 Arc<Path>,
76 Vec<(
77 LanguageServerId,
78 Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
79 )>,
80 >,
81 diagnostic_summaries: HashMap<Arc<Path>, HashMap<LanguageServerId, DiagnosticSummary>>,
82 client: Arc<Client>,
83 fs: Arc<dyn Fs>,
84 visible: bool,
85}
86
87pub struct RemoteWorktree {
88 snapshot: Snapshot,
89 background_snapshot: Arc<Mutex<Snapshot>>,
90 project_id: u64,
91 client: Arc<Client>,
92 updates_tx: Option<UnboundedSender<proto::UpdateWorktree>>,
93 snapshot_subscriptions: VecDeque<(usize, oneshot::Sender<()>)>,
94 replica_id: ReplicaId,
95 diagnostic_summaries: HashMap<Arc<Path>, HashMap<LanguageServerId, DiagnosticSummary>>,
96 visible: bool,
97 disconnected: bool,
98}
99
100#[derive(Clone)]
101pub struct Snapshot {
102 id: WorktreeId,
103 abs_path: Arc<Path>,
104 root_name: String,
105 root_char_bag: CharBag,
106 entries_by_path: SumTree<Entry>,
107 entries_by_id: SumTree<PathEntry>,
108 repository_entries: TreeMap<RepositoryWorkDirectory, RepositoryEntry>,
109
110 /// A number that increases every time the worktree begins scanning
111 /// a set of paths from the filesystem. This scanning could be caused
112 /// by some operation performed on the worktree, such as reading or
113 /// writing a file, or by an event reported by the filesystem.
114 scan_id: usize,
115
116 /// The latest scan id that has completed, and whose preceding scans
117 /// have all completed. The current `scan_id` could be more than one
118 /// greater than the `completed_scan_id` if operations are performed
119 /// on the worktree while it is processing a file-system event.
120 completed_scan_id: usize,
121}
122
123impl Snapshot {
124 pub fn repo_for(&self, path: &Path) -> Option<RepositoryEntry> {
125 let mut max_len = 0;
126 let mut current_candidate = None;
127 for (work_directory, repo) in (&self.repository_entries).iter() {
128 if path.starts_with(&work_directory.0) {
129 if work_directory.0.as_os_str().len() >= max_len {
130 current_candidate = Some(repo);
131 max_len = work_directory.0.as_os_str().len();
132 } else {
133 break;
134 }
135 }
136 }
137
138 current_candidate.map(|entry| entry.to_owned())
139 }
140}
141
142#[derive(Clone, Debug, PartialEq, Eq)]
143pub struct RepositoryEntry {
144 pub(crate) work_directory: WorkDirectoryEntry,
145 pub(crate) branch: Option<Arc<str>>,
146 pub(crate) statuses: TreeMap<RepoPath, GitFileStatus>,
147}
148
149fn read_git_status(git_status: i32) -> Option<GitFileStatus> {
150 proto::GitStatus::from_i32(git_status).map(|status| match status {
151 proto::GitStatus::Added => GitFileStatus::Added,
152 proto::GitStatus::Modified => GitFileStatus::Modified,
153 proto::GitStatus::Conflict => GitFileStatus::Conflict,
154 })
155}
156
157impl RepositoryEntry {
158 pub fn branch(&self) -> Option<Arc<str>> {
159 self.branch.clone()
160 }
161
162 pub fn work_directory_id(&self) -> ProjectEntryId {
163 *self.work_directory
164 }
165
166 pub fn work_directory(&self, snapshot: &Snapshot) -> Option<RepositoryWorkDirectory> {
167 snapshot
168 .entry_for_id(self.work_directory_id())
169 .map(|entry| RepositoryWorkDirectory(entry.path.clone()))
170 }
171
172 pub fn status_for_file(&self, snapshot: &Snapshot, path: &Path) -> Option<GitFileStatus> {
173 self.work_directory
174 .relativize(snapshot, path)
175 .and_then(|repo_path| self.statuses.get(&repo_path))
176 .cloned()
177 }
178
179 pub fn status_for_path(&self, snapshot: &Snapshot, path: &Path) -> Option<GitFileStatus> {
180 self.work_directory
181 .relativize(snapshot, path)
182 .and_then(|repo_path| {
183 self.statuses
184 .iter_from(&repo_path)
185 .take_while(|(key, _)| key.starts_with(&repo_path))
186 // Short circut once we've found the highest level
187 .take_until(|(_, status)| status == &&GitFileStatus::Conflict)
188 .map(|(_, status)| status)
189 .reduce(
190 |status_first, status_second| match (status_first, status_second) {
191 (GitFileStatus::Conflict, _) | (_, GitFileStatus::Conflict) => {
192 &GitFileStatus::Conflict
193 }
194 (GitFileStatus::Modified, _) | (_, GitFileStatus::Modified) => {
195 &GitFileStatus::Modified
196 }
197 _ => &GitFileStatus::Added,
198 },
199 )
200 .copied()
201 })
202 }
203
204 pub fn build_update(&self, other: &Self) -> proto::RepositoryEntry {
205 let mut updated_statuses: Vec<proto::StatusEntry> = Vec::new();
206 let mut removed_statuses: Vec<String> = Vec::new();
207
208 let mut self_statuses = self.statuses.iter().peekable();
209 let mut other_statuses = other.statuses.iter().peekable();
210 loop {
211 match (self_statuses.peek(), other_statuses.peek()) {
212 (Some((self_repo_path, self_status)), Some((other_repo_path, other_status))) => {
213 match Ord::cmp(self_repo_path, other_repo_path) {
214 Ordering::Less => {
215 updated_statuses.push(make_status_entry(self_repo_path, self_status));
216 self_statuses.next();
217 }
218 Ordering::Equal => {
219 if self_status != other_status {
220 updated_statuses
221 .push(make_status_entry(self_repo_path, self_status));
222 }
223
224 self_statuses.next();
225 other_statuses.next();
226 }
227 Ordering::Greater => {
228 removed_statuses.push(make_repo_path(other_repo_path));
229 other_statuses.next();
230 }
231 }
232 }
233 (Some((self_repo_path, self_status)), None) => {
234 updated_statuses.push(make_status_entry(self_repo_path, self_status));
235 self_statuses.next();
236 }
237 (None, Some((other_repo_path, _))) => {
238 removed_statuses.push(make_repo_path(other_repo_path));
239 other_statuses.next();
240 }
241 (None, None) => break,
242 }
243 }
244
245 proto::RepositoryEntry {
246 work_directory_id: self.work_directory_id().to_proto(),
247 branch: self.branch.as_ref().map(|str| str.to_string()),
248 removed_repo_paths: removed_statuses,
249 updated_statuses: updated_statuses,
250 }
251 }
252}
253
254fn make_repo_path(path: &RepoPath) -> String {
255 path.as_os_str().to_string_lossy().to_string()
256}
257
258fn make_status_entry(path: &RepoPath, status: &GitFileStatus) -> proto::StatusEntry {
259 proto::StatusEntry {
260 repo_path: make_repo_path(path),
261 status: match status {
262 GitFileStatus::Added => proto::GitStatus::Added.into(),
263 GitFileStatus::Modified => proto::GitStatus::Modified.into(),
264 GitFileStatus::Conflict => proto::GitStatus::Conflict.into(),
265 },
266 }
267}
268
269impl From<&RepositoryEntry> for proto::RepositoryEntry {
270 fn from(value: &RepositoryEntry) -> Self {
271 proto::RepositoryEntry {
272 work_directory_id: value.work_directory.to_proto(),
273 branch: value.branch.as_ref().map(|str| str.to_string()),
274 updated_statuses: value
275 .statuses
276 .iter()
277 .map(|(repo_path, status)| make_status_entry(repo_path, status))
278 .collect(),
279 removed_repo_paths: Default::default(),
280 }
281 }
282}
283
284/// This path corresponds to the 'content path' (the folder that contains the .git)
285#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
286pub struct RepositoryWorkDirectory(Arc<Path>);
287
288impl Default for RepositoryWorkDirectory {
289 fn default() -> Self {
290 RepositoryWorkDirectory(Arc::from(Path::new("")))
291 }
292}
293
294impl AsRef<Path> for RepositoryWorkDirectory {
295 fn as_ref(&self) -> &Path {
296 self.0.as_ref()
297 }
298}
299
300#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
301pub struct WorkDirectoryEntry(ProjectEntryId);
302
303impl WorkDirectoryEntry {
304 pub(crate) fn relativize(&self, worktree: &Snapshot, path: &Path) -> Option<RepoPath> {
305 worktree.entry_for_id(self.0).and_then(|entry| {
306 path.strip_prefix(&entry.path)
307 .ok()
308 .map(move |path| path.into())
309 })
310 }
311}
312
313impl Deref for WorkDirectoryEntry {
314 type Target = ProjectEntryId;
315
316 fn deref(&self) -> &Self::Target {
317 &self.0
318 }
319}
320
321impl<'a> From<ProjectEntryId> for WorkDirectoryEntry {
322 fn from(value: ProjectEntryId) -> Self {
323 WorkDirectoryEntry(value)
324 }
325}
326
327#[derive(Debug, Clone)]
328pub struct LocalSnapshot {
329 ignores_by_parent_abs_path: HashMap<Arc<Path>, (Arc<Gitignore>, bool)>, // (gitignore, needs_update)
330 // The ProjectEntryId corresponds to the entry for the .git dir
331 // work_directory_id
332 git_repositories: TreeMap<ProjectEntryId, LocalRepositoryEntry>,
333 removed_entry_ids: HashMap<u64, ProjectEntryId>,
334 snapshot: Snapshot,
335}
336
337#[derive(Debug, Clone)]
338pub struct LocalRepositoryEntry {
339 pub(crate) scan_id: usize,
340 pub(crate) full_scan_id: usize,
341 pub(crate) repo_ptr: Arc<Mutex<dyn GitRepository>>,
342 /// Path to the actual .git folder.
343 /// Note: if .git is a file, this points to the folder indicated by the .git file
344 pub(crate) git_dir_path: Arc<Path>,
345}
346
347impl LocalRepositoryEntry {
348 // Note that this path should be relative to the worktree root.
349 pub(crate) fn in_dot_git(&self, path: &Path) -> bool {
350 path.starts_with(self.git_dir_path.as_ref())
351 }
352}
353
354impl Deref for LocalSnapshot {
355 type Target = Snapshot;
356
357 fn deref(&self) -> &Self::Target {
358 &self.snapshot
359 }
360}
361
362impl DerefMut for LocalSnapshot {
363 fn deref_mut(&mut self) -> &mut Self::Target {
364 &mut self.snapshot
365 }
366}
367
368enum ScanState {
369 Started,
370 Updated {
371 snapshot: LocalSnapshot,
372 changes: HashMap<(Arc<Path>, ProjectEntryId), PathChange>,
373 barrier: Option<barrier::Sender>,
374 scanning: bool,
375 },
376}
377
378struct ShareState {
379 project_id: u64,
380 snapshots_tx: watch::Sender<LocalSnapshot>,
381 resume_updates: watch::Sender<()>,
382 _maintain_remote_snapshot: Task<Option<()>>,
383}
384
385pub enum Event {
386 UpdatedEntries(HashMap<(Arc<Path>, ProjectEntryId), PathChange>),
387 UpdatedGitRepositories(HashMap<Arc<Path>, LocalRepositoryEntry>),
388}
389
390impl Entity for Worktree {
391 type Event = Event;
392}
393
394impl Worktree {
395 pub async fn local(
396 client: Arc<Client>,
397 path: impl Into<Arc<Path>>,
398 visible: bool,
399 fs: Arc<dyn Fs>,
400 next_entry_id: Arc<AtomicUsize>,
401 cx: &mut AsyncAppContext,
402 ) -> Result<ModelHandle<Self>> {
403 // After determining whether the root entry is a file or a directory, populate the
404 // snapshot's "root name", which will be used for the purpose of fuzzy matching.
405 let abs_path = path.into();
406 let metadata = fs
407 .metadata(&abs_path)
408 .await
409 .context("failed to stat worktree path")?;
410
411 Ok(cx.add_model(move |cx: &mut ModelContext<Worktree>| {
412 let root_name = abs_path
413 .file_name()
414 .map_or(String::new(), |f| f.to_string_lossy().to_string());
415
416 let mut snapshot = LocalSnapshot {
417 ignores_by_parent_abs_path: Default::default(),
418 removed_entry_ids: Default::default(),
419 git_repositories: Default::default(),
420 snapshot: Snapshot {
421 id: WorktreeId::from_usize(cx.model_id()),
422 abs_path: abs_path.clone(),
423 root_name: root_name.clone(),
424 root_char_bag: root_name.chars().map(|c| c.to_ascii_lowercase()).collect(),
425 entries_by_path: Default::default(),
426 entries_by_id: Default::default(),
427 repository_entries: Default::default(),
428 scan_id: 1,
429 completed_scan_id: 0,
430 },
431 };
432
433 if let Some(metadata) = metadata {
434 snapshot.insert_entry(
435 Entry::new(
436 Arc::from(Path::new("")),
437 &metadata,
438 &next_entry_id,
439 snapshot.root_char_bag,
440 ),
441 fs.as_ref(),
442 );
443 }
444
445 let (path_changes_tx, path_changes_rx) = channel::unbounded();
446 let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
447
448 cx.spawn_weak(|this, mut cx| async move {
449 while let Some((state, this)) = scan_states_rx.next().await.zip(this.upgrade(&cx)) {
450 this.update(&mut cx, |this, cx| {
451 let this = this.as_local_mut().unwrap();
452 match state {
453 ScanState::Started => {
454 *this.is_scanning.0.borrow_mut() = true;
455 }
456 ScanState::Updated {
457 snapshot,
458 changes,
459 barrier,
460 scanning,
461 } => {
462 *this.is_scanning.0.borrow_mut() = scanning;
463 this.set_snapshot(snapshot, cx);
464 cx.emit(Event::UpdatedEntries(changes));
465 drop(barrier);
466 }
467 }
468 cx.notify();
469 });
470 }
471 })
472 .detach();
473
474 let background_scanner_task = cx.background().spawn({
475 let fs = fs.clone();
476 let snapshot = snapshot.clone();
477 let background = cx.background().clone();
478 async move {
479 let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
480 BackgroundScanner::new(
481 snapshot,
482 next_entry_id,
483 fs,
484 scan_states_tx,
485 background,
486 path_changes_rx,
487 )
488 .run(events)
489 .await;
490 }
491 });
492
493 Worktree::Local(LocalWorktree {
494 snapshot,
495 is_scanning: watch::channel_with(true),
496 share: None,
497 path_changes_tx,
498 _background_scanner_task: background_scanner_task,
499 diagnostics: Default::default(),
500 diagnostic_summaries: Default::default(),
501 client,
502 fs,
503 visible,
504 })
505 }))
506 }
507
508 pub fn remote(
509 project_remote_id: u64,
510 replica_id: ReplicaId,
511 worktree: proto::WorktreeMetadata,
512 client: Arc<Client>,
513 cx: &mut AppContext,
514 ) -> ModelHandle<Self> {
515 cx.add_model(|cx: &mut ModelContext<Self>| {
516 let snapshot = Snapshot {
517 id: WorktreeId(worktree.id as usize),
518 abs_path: Arc::from(PathBuf::from(worktree.abs_path)),
519 root_name: worktree.root_name.clone(),
520 root_char_bag: worktree
521 .root_name
522 .chars()
523 .map(|c| c.to_ascii_lowercase())
524 .collect(),
525 entries_by_path: Default::default(),
526 entries_by_id: Default::default(),
527 repository_entries: Default::default(),
528 scan_id: 1,
529 completed_scan_id: 0,
530 };
531
532 let (updates_tx, mut updates_rx) = mpsc::unbounded();
533 let background_snapshot = Arc::new(Mutex::new(snapshot.clone()));
534 let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
535
536 cx.background()
537 .spawn({
538 let background_snapshot = background_snapshot.clone();
539 async move {
540 while let Some(update) = updates_rx.next().await {
541 if let Err(error) =
542 background_snapshot.lock().apply_remote_update(update)
543 {
544 log::error!("error applying worktree update: {}", error);
545 }
546 snapshot_updated_tx.send(()).await.ok();
547 }
548 }
549 })
550 .detach();
551
552 cx.spawn_weak(|this, mut cx| async move {
553 while (snapshot_updated_rx.recv().await).is_some() {
554 if let Some(this) = this.upgrade(&cx) {
555 this.update(&mut cx, |this, cx| {
556 let this = this.as_remote_mut().unwrap();
557 this.snapshot = this.background_snapshot.lock().clone();
558 cx.emit(Event::UpdatedEntries(Default::default()));
559 cx.notify();
560 while let Some((scan_id, _)) = this.snapshot_subscriptions.front() {
561 if this.observed_snapshot(*scan_id) {
562 let (_, tx) = this.snapshot_subscriptions.pop_front().unwrap();
563 let _ = tx.send(());
564 } else {
565 break;
566 }
567 }
568 });
569 } else {
570 break;
571 }
572 }
573 })
574 .detach();
575
576 Worktree::Remote(RemoteWorktree {
577 project_id: project_remote_id,
578 replica_id,
579 snapshot: snapshot.clone(),
580 background_snapshot,
581 updates_tx: Some(updates_tx),
582 snapshot_subscriptions: Default::default(),
583 client: client.clone(),
584 diagnostic_summaries: Default::default(),
585 visible: worktree.visible,
586 disconnected: false,
587 })
588 })
589 }
590
591 pub fn as_local(&self) -> Option<&LocalWorktree> {
592 if let Worktree::Local(worktree) = self {
593 Some(worktree)
594 } else {
595 None
596 }
597 }
598
599 pub fn as_remote(&self) -> Option<&RemoteWorktree> {
600 if let Worktree::Remote(worktree) = self {
601 Some(worktree)
602 } else {
603 None
604 }
605 }
606
607 pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
608 if let Worktree::Local(worktree) = self {
609 Some(worktree)
610 } else {
611 None
612 }
613 }
614
615 pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
616 if let Worktree::Remote(worktree) = self {
617 Some(worktree)
618 } else {
619 None
620 }
621 }
622
623 pub fn is_local(&self) -> bool {
624 matches!(self, Worktree::Local(_))
625 }
626
627 pub fn is_remote(&self) -> bool {
628 !self.is_local()
629 }
630
631 pub fn snapshot(&self) -> Snapshot {
632 match self {
633 Worktree::Local(worktree) => worktree.snapshot().snapshot,
634 Worktree::Remote(worktree) => worktree.snapshot(),
635 }
636 }
637
638 pub fn scan_id(&self) -> usize {
639 match self {
640 Worktree::Local(worktree) => worktree.snapshot.scan_id,
641 Worktree::Remote(worktree) => worktree.snapshot.scan_id,
642 }
643 }
644
645 pub fn completed_scan_id(&self) -> usize {
646 match self {
647 Worktree::Local(worktree) => worktree.snapshot.completed_scan_id,
648 Worktree::Remote(worktree) => worktree.snapshot.completed_scan_id,
649 }
650 }
651
652 pub fn is_visible(&self) -> bool {
653 match self {
654 Worktree::Local(worktree) => worktree.visible,
655 Worktree::Remote(worktree) => worktree.visible,
656 }
657 }
658
659 pub fn replica_id(&self) -> ReplicaId {
660 match self {
661 Worktree::Local(_) => 0,
662 Worktree::Remote(worktree) => worktree.replica_id,
663 }
664 }
665
666 pub fn diagnostic_summaries(
667 &self,
668 ) -> impl Iterator<Item = (Arc<Path>, LanguageServerId, DiagnosticSummary)> + '_ {
669 match self {
670 Worktree::Local(worktree) => &worktree.diagnostic_summaries,
671 Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
672 }
673 .iter()
674 .flat_map(|(path, summaries)| {
675 summaries
676 .iter()
677 .map(move |(&server_id, &summary)| (path.clone(), server_id, summary))
678 })
679 }
680
681 pub fn abs_path(&self) -> Arc<Path> {
682 match self {
683 Worktree::Local(worktree) => worktree.abs_path.clone(),
684 Worktree::Remote(worktree) => worktree.abs_path.clone(),
685 }
686 }
687}
688
689impl LocalWorktree {
690 pub fn contains_abs_path(&self, path: &Path) -> bool {
691 path.starts_with(&self.abs_path)
692 }
693
694 fn absolutize(&self, path: &Path) -> PathBuf {
695 if path.file_name().is_some() {
696 self.abs_path.join(path)
697 } else {
698 self.abs_path.to_path_buf()
699 }
700 }
701
702 pub(crate) fn load_buffer(
703 &mut self,
704 id: u64,
705 path: &Path,
706 cx: &mut ModelContext<Worktree>,
707 ) -> Task<Result<ModelHandle<Buffer>>> {
708 let path = Arc::from(path);
709 cx.spawn(move |this, mut cx| async move {
710 let (file, contents, diff_base) = this
711 .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
712 .await?;
713 let text_buffer = cx
714 .background()
715 .spawn(async move { text::Buffer::new(0, id, contents) })
716 .await;
717 Ok(cx.add_model(|cx| {
718 let mut buffer = Buffer::build(text_buffer, diff_base, Some(Arc::new(file)));
719 buffer.git_diff_recalc(cx);
720 buffer
721 }))
722 })
723 }
724
725 pub fn diagnostics_for_path(
726 &self,
727 path: &Path,
728 ) -> Vec<(
729 LanguageServerId,
730 Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
731 )> {
732 self.diagnostics.get(path).cloned().unwrap_or_default()
733 }
734
735 pub fn update_diagnostics(
736 &mut self,
737 server_id: LanguageServerId,
738 worktree_path: Arc<Path>,
739 diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
740 _: &mut ModelContext<Worktree>,
741 ) -> Result<bool> {
742 let summaries_by_server_id = self
743 .diagnostic_summaries
744 .entry(worktree_path.clone())
745 .or_default();
746
747 let old_summary = summaries_by_server_id
748 .remove(&server_id)
749 .unwrap_or_default();
750
751 let new_summary = DiagnosticSummary::new(&diagnostics);
752 if new_summary.is_empty() {
753 if let Some(diagnostics_by_server_id) = self.diagnostics.get_mut(&worktree_path) {
754 if let Ok(ix) = diagnostics_by_server_id.binary_search_by_key(&server_id, |e| e.0) {
755 diagnostics_by_server_id.remove(ix);
756 }
757 if diagnostics_by_server_id.is_empty() {
758 self.diagnostics.remove(&worktree_path);
759 }
760 }
761 } else {
762 summaries_by_server_id.insert(server_id, new_summary);
763 let diagnostics_by_server_id =
764 self.diagnostics.entry(worktree_path.clone()).or_default();
765 match diagnostics_by_server_id.binary_search_by_key(&server_id, |e| e.0) {
766 Ok(ix) => {
767 diagnostics_by_server_id[ix] = (server_id, diagnostics);
768 }
769 Err(ix) => {
770 diagnostics_by_server_id.insert(ix, (server_id, diagnostics));
771 }
772 }
773 }
774
775 if !old_summary.is_empty() || !new_summary.is_empty() {
776 if let Some(share) = self.share.as_ref() {
777 self.client
778 .send(proto::UpdateDiagnosticSummary {
779 project_id: share.project_id,
780 worktree_id: self.id().to_proto(),
781 summary: Some(proto::DiagnosticSummary {
782 path: worktree_path.to_string_lossy().to_string(),
783 language_server_id: server_id.0 as u64,
784 error_count: new_summary.error_count as u32,
785 warning_count: new_summary.warning_count as u32,
786 }),
787 })
788 .log_err();
789 }
790 }
791
792 Ok(!old_summary.is_empty() || !new_summary.is_empty())
793 }
794
795 fn set_snapshot(&mut self, new_snapshot: LocalSnapshot, cx: &mut ModelContext<Worktree>) {
796 let updated_repos =
797 self.changed_repos(&self.git_repositories, &new_snapshot.git_repositories);
798 self.snapshot = new_snapshot;
799
800 if let Some(share) = self.share.as_mut() {
801 *share.snapshots_tx.borrow_mut() = self.snapshot.clone();
802 }
803
804 if !updated_repos.is_empty() {
805 cx.emit(Event::UpdatedGitRepositories(updated_repos));
806 }
807 }
808
809 fn changed_repos(
810 &self,
811 old_repos: &TreeMap<ProjectEntryId, LocalRepositoryEntry>,
812 new_repos: &TreeMap<ProjectEntryId, LocalRepositoryEntry>,
813 ) -> HashMap<Arc<Path>, LocalRepositoryEntry> {
814 let mut diff = HashMap::default();
815 let mut old_repos = old_repos.iter().peekable();
816 let mut new_repos = new_repos.iter().peekable();
817 loop {
818 match (old_repos.peek(), new_repos.peek()) {
819 (Some((old_entry_id, old_repo)), Some((new_entry_id, new_repo))) => {
820 match Ord::cmp(old_entry_id, new_entry_id) {
821 Ordering::Less => {
822 if let Some(entry) = self.entry_for_id(**old_entry_id) {
823 diff.insert(entry.path.clone(), (*old_repo).clone());
824 }
825 old_repos.next();
826 }
827 Ordering::Equal => {
828 if old_repo.scan_id != new_repo.scan_id {
829 if let Some(entry) = self.entry_for_id(**new_entry_id) {
830 diff.insert(entry.path.clone(), (*new_repo).clone());
831 }
832 }
833
834 old_repos.next();
835 new_repos.next();
836 }
837 Ordering::Greater => {
838 if let Some(entry) = self.entry_for_id(**new_entry_id) {
839 diff.insert(entry.path.clone(), (*new_repo).clone());
840 }
841 new_repos.next();
842 }
843 }
844 }
845 (Some((old_entry_id, old_repo)), None) => {
846 if let Some(entry) = self.entry_for_id(**old_entry_id) {
847 diff.insert(entry.path.clone(), (*old_repo).clone());
848 }
849 old_repos.next();
850 }
851 (None, Some((new_entry_id, new_repo))) => {
852 if let Some(entry) = self.entry_for_id(**new_entry_id) {
853 diff.insert(entry.path.clone(), (*new_repo).clone());
854 }
855 new_repos.next();
856 }
857 (None, None) => break,
858 }
859 }
860 diff
861 }
862
863 pub fn scan_complete(&self) -> impl Future<Output = ()> {
864 let mut is_scanning_rx = self.is_scanning.1.clone();
865 async move {
866 let mut is_scanning = is_scanning_rx.borrow().clone();
867 while is_scanning {
868 if let Some(value) = is_scanning_rx.recv().await {
869 is_scanning = value;
870 } else {
871 break;
872 }
873 }
874 }
875 }
876
877 pub fn snapshot(&self) -> LocalSnapshot {
878 self.snapshot.clone()
879 }
880
881 pub fn metadata_proto(&self) -> proto::WorktreeMetadata {
882 proto::WorktreeMetadata {
883 id: self.id().to_proto(),
884 root_name: self.root_name().to_string(),
885 visible: self.visible,
886 abs_path: self.abs_path().as_os_str().to_string_lossy().into(),
887 }
888 }
889
890 fn load(
891 &self,
892 path: &Path,
893 cx: &mut ModelContext<Worktree>,
894 ) -> Task<Result<(File, String, Option<String>)>> {
895 let handle = cx.handle();
896 let path = Arc::from(path);
897 let abs_path = self.absolutize(&path);
898 let fs = self.fs.clone();
899 let snapshot = self.snapshot();
900
901 let mut index_task = None;
902
903 if let Some(repo) = snapshot.repo_for(&path) {
904 let repo_path = repo.work_directory.relativize(self, &path).unwrap();
905 if let Some(repo) = self.git_repositories.get(&*repo.work_directory) {
906 let repo = repo.repo_ptr.to_owned();
907 index_task = Some(
908 cx.background()
909 .spawn(async move { repo.lock().load_index_text(&repo_path) }),
910 );
911 }
912 }
913
914 cx.spawn(|this, mut cx| async move {
915 let text = fs.load(&abs_path).await?;
916
917 let diff_base = if let Some(index_task) = index_task {
918 index_task.await
919 } else {
920 None
921 };
922
923 // Eagerly populate the snapshot with an updated entry for the loaded file
924 let entry = this
925 .update(&mut cx, |this, cx| {
926 this.as_local().unwrap().refresh_entry(path, None, cx)
927 })
928 .await?;
929
930 Ok((
931 File {
932 entry_id: entry.id,
933 worktree: handle,
934 path: entry.path,
935 mtime: entry.mtime,
936 is_local: true,
937 is_deleted: false,
938 },
939 text,
940 diff_base,
941 ))
942 })
943 }
944
945 pub fn save_buffer(
946 &self,
947 buffer_handle: ModelHandle<Buffer>,
948 path: Arc<Path>,
949 has_changed_file: bool,
950 cx: &mut ModelContext<Worktree>,
951 ) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
952 let handle = cx.handle();
953 let buffer = buffer_handle.read(cx);
954
955 let rpc = self.client.clone();
956 let buffer_id = buffer.remote_id();
957 let project_id = self.share.as_ref().map(|share| share.project_id);
958
959 let text = buffer.as_rope().clone();
960 let fingerprint = text.fingerprint();
961 let version = buffer.version();
962 let save = self.write_file(path, text, buffer.line_ending(), cx);
963
964 cx.as_mut().spawn(|mut cx| async move {
965 let entry = save.await?;
966
967 if has_changed_file {
968 let new_file = Arc::new(File {
969 entry_id: entry.id,
970 worktree: handle,
971 path: entry.path,
972 mtime: entry.mtime,
973 is_local: true,
974 is_deleted: false,
975 });
976
977 if let Some(project_id) = project_id {
978 rpc.send(proto::UpdateBufferFile {
979 project_id,
980 buffer_id,
981 file: Some(new_file.to_proto()),
982 })
983 .log_err();
984 }
985
986 buffer_handle.update(&mut cx, |buffer, cx| {
987 if has_changed_file {
988 buffer.file_updated(new_file, cx).detach();
989 }
990 });
991 }
992
993 if let Some(project_id) = project_id {
994 rpc.send(proto::BufferSaved {
995 project_id,
996 buffer_id,
997 version: serialize_version(&version),
998 mtime: Some(entry.mtime.into()),
999 fingerprint: serialize_fingerprint(fingerprint),
1000 })?;
1001 }
1002
1003 buffer_handle.update(&mut cx, |buffer, cx| {
1004 buffer.did_save(version.clone(), fingerprint, entry.mtime, cx);
1005 });
1006
1007 Ok((version, fingerprint, entry.mtime))
1008 })
1009 }
1010
1011 pub fn create_entry(
1012 &self,
1013 path: impl Into<Arc<Path>>,
1014 is_dir: bool,
1015 cx: &mut ModelContext<Worktree>,
1016 ) -> Task<Result<Entry>> {
1017 let path = path.into();
1018 let abs_path = self.absolutize(&path);
1019 let fs = self.fs.clone();
1020 let write = cx.background().spawn(async move {
1021 if is_dir {
1022 fs.create_dir(&abs_path).await
1023 } else {
1024 fs.save(&abs_path, &Default::default(), Default::default())
1025 .await
1026 }
1027 });
1028
1029 cx.spawn(|this, mut cx| async move {
1030 write.await?;
1031 this.update(&mut cx, |this, cx| {
1032 this.as_local_mut().unwrap().refresh_entry(path, None, cx)
1033 })
1034 .await
1035 })
1036 }
1037
1038 pub fn write_file(
1039 &self,
1040 path: impl Into<Arc<Path>>,
1041 text: Rope,
1042 line_ending: LineEnding,
1043 cx: &mut ModelContext<Worktree>,
1044 ) -> Task<Result<Entry>> {
1045 let path = path.into();
1046 let abs_path = self.absolutize(&path);
1047 let fs = self.fs.clone();
1048 let write = cx
1049 .background()
1050 .spawn(async move { fs.save(&abs_path, &text, line_ending).await });
1051
1052 cx.spawn(|this, mut cx| async move {
1053 write.await?;
1054 this.update(&mut cx, |this, cx| {
1055 this.as_local_mut().unwrap().refresh_entry(path, None, cx)
1056 })
1057 .await
1058 })
1059 }
1060
1061 pub fn delete_entry(
1062 &self,
1063 entry_id: ProjectEntryId,
1064 cx: &mut ModelContext<Worktree>,
1065 ) -> Option<Task<Result<()>>> {
1066 let entry = self.entry_for_id(entry_id)?.clone();
1067 let abs_path = self.abs_path.clone();
1068 let fs = self.fs.clone();
1069
1070 let delete = cx.background().spawn(async move {
1071 let mut abs_path = fs.canonicalize(&abs_path).await?;
1072 if entry.path.file_name().is_some() {
1073 abs_path = abs_path.join(&entry.path);
1074 }
1075 if entry.is_file() {
1076 fs.remove_file(&abs_path, Default::default()).await?;
1077 } else {
1078 fs.remove_dir(
1079 &abs_path,
1080 RemoveOptions {
1081 recursive: true,
1082 ignore_if_not_exists: false,
1083 },
1084 )
1085 .await?;
1086 }
1087 anyhow::Ok(abs_path)
1088 });
1089
1090 Some(cx.spawn(|this, mut cx| async move {
1091 let abs_path = delete.await?;
1092 let (tx, mut rx) = barrier::channel();
1093 this.update(&mut cx, |this, _| {
1094 this.as_local_mut()
1095 .unwrap()
1096 .path_changes_tx
1097 .try_send((vec![abs_path], tx))
1098 })?;
1099 rx.recv().await;
1100 Ok(())
1101 }))
1102 }
1103
1104 pub fn rename_entry(
1105 &self,
1106 entry_id: ProjectEntryId,
1107 new_path: impl Into<Arc<Path>>,
1108 cx: &mut ModelContext<Worktree>,
1109 ) -> Option<Task<Result<Entry>>> {
1110 let old_path = self.entry_for_id(entry_id)?.path.clone();
1111 let new_path = new_path.into();
1112 let abs_old_path = self.absolutize(&old_path);
1113 let abs_new_path = self.absolutize(&new_path);
1114 let fs = self.fs.clone();
1115 let rename = cx.background().spawn(async move {
1116 fs.rename(&abs_old_path, &abs_new_path, Default::default())
1117 .await
1118 });
1119
1120 Some(cx.spawn(|this, mut cx| async move {
1121 rename.await?;
1122 this.update(&mut cx, |this, cx| {
1123 this.as_local_mut()
1124 .unwrap()
1125 .refresh_entry(new_path.clone(), Some(old_path), cx)
1126 })
1127 .await
1128 }))
1129 }
1130
1131 pub fn copy_entry(
1132 &self,
1133 entry_id: ProjectEntryId,
1134 new_path: impl Into<Arc<Path>>,
1135 cx: &mut ModelContext<Worktree>,
1136 ) -> Option<Task<Result<Entry>>> {
1137 let old_path = self.entry_for_id(entry_id)?.path.clone();
1138 let new_path = new_path.into();
1139 let abs_old_path = self.absolutize(&old_path);
1140 let abs_new_path = self.absolutize(&new_path);
1141 let fs = self.fs.clone();
1142 let copy = cx.background().spawn(async move {
1143 copy_recursive(
1144 fs.as_ref(),
1145 &abs_old_path,
1146 &abs_new_path,
1147 Default::default(),
1148 )
1149 .await
1150 });
1151
1152 Some(cx.spawn(|this, mut cx| async move {
1153 copy.await?;
1154 this.update(&mut cx, |this, cx| {
1155 this.as_local_mut()
1156 .unwrap()
1157 .refresh_entry(new_path.clone(), None, cx)
1158 })
1159 .await
1160 }))
1161 }
1162
1163 fn refresh_entry(
1164 &self,
1165 path: Arc<Path>,
1166 old_path: Option<Arc<Path>>,
1167 cx: &mut ModelContext<Worktree>,
1168 ) -> Task<Result<Entry>> {
1169 let fs = self.fs.clone();
1170 let abs_root_path = self.abs_path.clone();
1171 let path_changes_tx = self.path_changes_tx.clone();
1172 cx.spawn_weak(move |this, mut cx| async move {
1173 let abs_path = fs.canonicalize(&abs_root_path).await?;
1174 let mut paths = Vec::with_capacity(2);
1175 paths.push(if path.file_name().is_some() {
1176 abs_path.join(&path)
1177 } else {
1178 abs_path.clone()
1179 });
1180 if let Some(old_path) = old_path {
1181 paths.push(if old_path.file_name().is_some() {
1182 abs_path.join(&old_path)
1183 } else {
1184 abs_path.clone()
1185 });
1186 }
1187
1188 let (tx, mut rx) = barrier::channel();
1189 path_changes_tx.try_send((paths, tx))?;
1190 rx.recv().await;
1191 this.upgrade(&cx)
1192 .ok_or_else(|| anyhow!("worktree was dropped"))?
1193 .update(&mut cx, |this, _| {
1194 this.entry_for_path(path)
1195 .cloned()
1196 .ok_or_else(|| anyhow!("failed to read path after update"))
1197 })
1198 })
1199 }
1200
1201 pub fn share(&mut self, project_id: u64, cx: &mut ModelContext<Worktree>) -> Task<Result<()>> {
1202 let (share_tx, share_rx) = oneshot::channel();
1203
1204 if let Some(share) = self.share.as_mut() {
1205 let _ = share_tx.send(());
1206 *share.resume_updates.borrow_mut() = ();
1207 } else {
1208 let (snapshots_tx, mut snapshots_rx) = watch::channel_with(self.snapshot());
1209 let (resume_updates_tx, mut resume_updates_rx) = watch::channel();
1210 let worktree_id = cx.model_id() as u64;
1211
1212 for (path, summaries) in &self.diagnostic_summaries {
1213 for (&server_id, summary) in summaries {
1214 if let Err(e) = self.client.send(proto::UpdateDiagnosticSummary {
1215 project_id,
1216 worktree_id,
1217 summary: Some(summary.to_proto(server_id, &path)),
1218 }) {
1219 return Task::ready(Err(e));
1220 }
1221 }
1222 }
1223
1224 let _maintain_remote_snapshot = cx.background().spawn({
1225 let client = self.client.clone();
1226 async move {
1227 let mut share_tx = Some(share_tx);
1228 let mut prev_snapshot = LocalSnapshot {
1229 ignores_by_parent_abs_path: Default::default(),
1230 removed_entry_ids: Default::default(),
1231 git_repositories: Default::default(),
1232 snapshot: Snapshot {
1233 id: WorktreeId(worktree_id as usize),
1234 abs_path: Path::new("").into(),
1235 root_name: Default::default(),
1236 root_char_bag: Default::default(),
1237 entries_by_path: Default::default(),
1238 entries_by_id: Default::default(),
1239 repository_entries: Default::default(),
1240 scan_id: 0,
1241 completed_scan_id: 0,
1242 },
1243 };
1244 while let Some(snapshot) = snapshots_rx.recv().await {
1245 #[cfg(any(test, feature = "test-support"))]
1246 const MAX_CHUNK_SIZE: usize = 2;
1247 #[cfg(not(any(test, feature = "test-support")))]
1248 const MAX_CHUNK_SIZE: usize = 256;
1249
1250 let update =
1251 snapshot.build_update(&prev_snapshot, project_id, worktree_id, true);
1252 for update in proto::split_worktree_update(update, MAX_CHUNK_SIZE) {
1253 let _ = resume_updates_rx.try_recv();
1254 while let Err(error) = client.request(update.clone()).await {
1255 log::error!("failed to send worktree update: {}", error);
1256 log::info!("waiting to resume updates");
1257 if resume_updates_rx.next().await.is_none() {
1258 return Ok(());
1259 }
1260 }
1261 }
1262
1263 if let Some(share_tx) = share_tx.take() {
1264 let _ = share_tx.send(());
1265 }
1266
1267 prev_snapshot = snapshot;
1268 }
1269
1270 Ok::<_, anyhow::Error>(())
1271 }
1272 .log_err()
1273 });
1274
1275 self.share = Some(ShareState {
1276 project_id,
1277 snapshots_tx,
1278 resume_updates: resume_updates_tx,
1279 _maintain_remote_snapshot,
1280 });
1281 }
1282
1283 cx.foreground()
1284 .spawn(async move { share_rx.await.map_err(|_| anyhow!("share ended")) })
1285 }
1286
1287 pub fn unshare(&mut self) {
1288 self.share.take();
1289 }
1290
1291 pub fn is_shared(&self) -> bool {
1292 self.share.is_some()
1293 }
1294}
1295
1296impl RemoteWorktree {
1297 fn snapshot(&self) -> Snapshot {
1298 self.snapshot.clone()
1299 }
1300
1301 pub fn disconnected_from_host(&mut self) {
1302 self.updates_tx.take();
1303 self.snapshot_subscriptions.clear();
1304 self.disconnected = true;
1305 }
1306
1307 pub fn save_buffer(
1308 &self,
1309 buffer_handle: ModelHandle<Buffer>,
1310 cx: &mut ModelContext<Worktree>,
1311 ) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
1312 let buffer = buffer_handle.read(cx);
1313 let buffer_id = buffer.remote_id();
1314 let version = buffer.version();
1315 let rpc = self.client.clone();
1316 let project_id = self.project_id;
1317 cx.as_mut().spawn(|mut cx| async move {
1318 let response = rpc
1319 .request(proto::SaveBuffer {
1320 project_id,
1321 buffer_id,
1322 version: serialize_version(&version),
1323 })
1324 .await?;
1325 let version = deserialize_version(&response.version);
1326 let fingerprint = deserialize_fingerprint(&response.fingerprint)?;
1327 let mtime = response
1328 .mtime
1329 .ok_or_else(|| anyhow!("missing mtime"))?
1330 .into();
1331
1332 buffer_handle.update(&mut cx, |buffer, cx| {
1333 buffer.did_save(version.clone(), fingerprint, mtime, cx);
1334 });
1335
1336 Ok((version, fingerprint, mtime))
1337 })
1338 }
1339
1340 pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) {
1341 if let Some(updates_tx) = &self.updates_tx {
1342 updates_tx
1343 .unbounded_send(update)
1344 .expect("consumer runs to completion");
1345 }
1346 }
1347
1348 fn observed_snapshot(&self, scan_id: usize) -> bool {
1349 self.completed_scan_id >= scan_id
1350 }
1351
1352 fn wait_for_snapshot(&mut self, scan_id: usize) -> impl Future<Output = Result<()>> {
1353 let (tx, rx) = oneshot::channel();
1354 if self.observed_snapshot(scan_id) {
1355 let _ = tx.send(());
1356 } else if self.disconnected {
1357 drop(tx);
1358 } else {
1359 match self
1360 .snapshot_subscriptions
1361 .binary_search_by_key(&scan_id, |probe| probe.0)
1362 {
1363 Ok(ix) | Err(ix) => self.snapshot_subscriptions.insert(ix, (scan_id, tx)),
1364 }
1365 }
1366
1367 async move {
1368 rx.await?;
1369 Ok(())
1370 }
1371 }
1372
1373 pub fn update_diagnostic_summary(
1374 &mut self,
1375 path: Arc<Path>,
1376 summary: &proto::DiagnosticSummary,
1377 ) {
1378 let server_id = LanguageServerId(summary.language_server_id as usize);
1379 let summary = DiagnosticSummary {
1380 error_count: summary.error_count as usize,
1381 warning_count: summary.warning_count as usize,
1382 };
1383
1384 if summary.is_empty() {
1385 if let Some(summaries) = self.diagnostic_summaries.get_mut(&path) {
1386 summaries.remove(&server_id);
1387 if summaries.is_empty() {
1388 self.diagnostic_summaries.remove(&path);
1389 }
1390 }
1391 } else {
1392 self.diagnostic_summaries
1393 .entry(path)
1394 .or_default()
1395 .insert(server_id, summary);
1396 }
1397 }
1398
1399 pub fn insert_entry(
1400 &mut self,
1401 entry: proto::Entry,
1402 scan_id: usize,
1403 cx: &mut ModelContext<Worktree>,
1404 ) -> Task<Result<Entry>> {
1405 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1406 cx.spawn(|this, mut cx| async move {
1407 wait_for_snapshot.await?;
1408 this.update(&mut cx, |worktree, _| {
1409 let worktree = worktree.as_remote_mut().unwrap();
1410 let mut snapshot = worktree.background_snapshot.lock();
1411 let entry = snapshot.insert_entry(entry);
1412 worktree.snapshot = snapshot.clone();
1413 entry
1414 })
1415 })
1416 }
1417
1418 pub(crate) fn delete_entry(
1419 &mut self,
1420 id: ProjectEntryId,
1421 scan_id: usize,
1422 cx: &mut ModelContext<Worktree>,
1423 ) -> Task<Result<()>> {
1424 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1425 cx.spawn(|this, mut cx| async move {
1426 wait_for_snapshot.await?;
1427 this.update(&mut cx, |worktree, _| {
1428 let worktree = worktree.as_remote_mut().unwrap();
1429 let mut snapshot = worktree.background_snapshot.lock();
1430 snapshot.delete_entry(id);
1431 worktree.snapshot = snapshot.clone();
1432 });
1433 Ok(())
1434 })
1435 }
1436}
1437
1438impl Snapshot {
1439 pub fn id(&self) -> WorktreeId {
1440 self.id
1441 }
1442
1443 pub fn abs_path(&self) -> &Arc<Path> {
1444 &self.abs_path
1445 }
1446
1447 pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
1448 self.entries_by_id.get(&entry_id, &()).is_some()
1449 }
1450
1451 pub(crate) fn insert_entry(&mut self, entry: proto::Entry) -> Result<Entry> {
1452 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1453 let old_entry = self.entries_by_id.insert_or_replace(
1454 PathEntry {
1455 id: entry.id,
1456 path: entry.path.clone(),
1457 is_ignored: entry.is_ignored,
1458 scan_id: 0,
1459 },
1460 &(),
1461 );
1462 if let Some(old_entry) = old_entry {
1463 self.entries_by_path.remove(&PathKey(old_entry.path), &());
1464 }
1465 self.entries_by_path.insert_or_replace(entry.clone(), &());
1466 Ok(entry)
1467 }
1468
1469 fn delete_entry(&mut self, entry_id: ProjectEntryId) -> Option<Arc<Path>> {
1470 let removed_entry = self.entries_by_id.remove(&entry_id, &())?;
1471 self.entries_by_path = {
1472 let mut cursor = self.entries_by_path.cursor();
1473 let mut new_entries_by_path =
1474 cursor.slice(&TraversalTarget::Path(&removed_entry.path), Bias::Left, &());
1475 while let Some(entry) = cursor.item() {
1476 if entry.path.starts_with(&removed_entry.path) {
1477 self.entries_by_id.remove(&entry.id, &());
1478 cursor.next(&());
1479 } else {
1480 break;
1481 }
1482 }
1483 new_entries_by_path.push_tree(cursor.suffix(&()), &());
1484 new_entries_by_path
1485 };
1486
1487 Some(removed_entry.path)
1488 }
1489
1490 pub(crate) fn apply_remote_update(&mut self, mut update: proto::UpdateWorktree) -> Result<()> {
1491 let mut entries_by_path_edits = Vec::new();
1492 let mut entries_by_id_edits = Vec::new();
1493 for entry_id in update.removed_entries {
1494 if let Some(entry) = self.entry_for_id(ProjectEntryId::from_proto(entry_id)) {
1495 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
1496 entries_by_id_edits.push(Edit::Remove(entry.id));
1497 }
1498 }
1499
1500 for entry in update.updated_entries {
1501 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1502 if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
1503 entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
1504 }
1505 entries_by_id_edits.push(Edit::Insert(PathEntry {
1506 id: entry.id,
1507 path: entry.path.clone(),
1508 is_ignored: entry.is_ignored,
1509 scan_id: 0,
1510 }));
1511 entries_by_path_edits.push(Edit::Insert(entry));
1512 }
1513
1514 self.entries_by_path.edit(entries_by_path_edits, &());
1515 self.entries_by_id.edit(entries_by_id_edits, &());
1516
1517 update.removed_repositories.sort_unstable();
1518 self.repository_entries.retain(|_, entry| {
1519 if let Ok(_) = update
1520 .removed_repositories
1521 .binary_search(&entry.work_directory.to_proto())
1522 {
1523 false
1524 } else {
1525 true
1526 }
1527 });
1528
1529 for repository in update.updated_repositories {
1530 let work_directory_entry: WorkDirectoryEntry =
1531 ProjectEntryId::from_proto(repository.work_directory_id).into();
1532
1533 if let Some(entry) = self.entry_for_id(*work_directory_entry) {
1534 let mut statuses = TreeMap::default();
1535 for status_entry in repository.updated_statuses {
1536 let Some(git_file_status) = read_git_status(status_entry.status) else {
1537 continue;
1538 };
1539
1540 let repo_path = RepoPath::new(status_entry.repo_path.into());
1541 statuses.insert(repo_path, git_file_status);
1542 }
1543
1544 let work_directory = RepositoryWorkDirectory(entry.path.clone());
1545 if self.repository_entries.get(&work_directory).is_some() {
1546 self.repository_entries.update(&work_directory, |repo| {
1547 repo.branch = repository.branch.map(Into::into);
1548 repo.statuses.insert_tree(statuses);
1549
1550 for repo_path in repository.removed_repo_paths {
1551 let repo_path = RepoPath::new(repo_path.into());
1552 repo.statuses.remove(&repo_path);
1553 }
1554 });
1555 } else {
1556 self.repository_entries.insert(
1557 work_directory,
1558 RepositoryEntry {
1559 work_directory: work_directory_entry,
1560 branch: repository.branch.map(Into::into),
1561 statuses,
1562 },
1563 )
1564 }
1565 } else {
1566 log::error!("no work directory entry for repository {:?}", repository)
1567 }
1568 }
1569
1570 self.scan_id = update.scan_id as usize;
1571 if update.is_last_update {
1572 self.completed_scan_id = update.scan_id as usize;
1573 }
1574
1575 Ok(())
1576 }
1577
1578 pub fn file_count(&self) -> usize {
1579 self.entries_by_path.summary().file_count
1580 }
1581
1582 pub fn visible_file_count(&self) -> usize {
1583 self.entries_by_path.summary().visible_file_count
1584 }
1585
1586 fn traverse_from_offset(
1587 &self,
1588 include_dirs: bool,
1589 include_ignored: bool,
1590 start_offset: usize,
1591 ) -> Traversal {
1592 let mut cursor = self.entries_by_path.cursor();
1593 cursor.seek(
1594 &TraversalTarget::Count {
1595 count: start_offset,
1596 include_dirs,
1597 include_ignored,
1598 },
1599 Bias::Right,
1600 &(),
1601 );
1602 Traversal {
1603 cursor,
1604 include_dirs,
1605 include_ignored,
1606 }
1607 }
1608
1609 fn traverse_from_path(
1610 &self,
1611 include_dirs: bool,
1612 include_ignored: bool,
1613 path: &Path,
1614 ) -> Traversal {
1615 let mut cursor = self.entries_by_path.cursor();
1616 cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1617 Traversal {
1618 cursor,
1619 include_dirs,
1620 include_ignored,
1621 }
1622 }
1623
1624 pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1625 self.traverse_from_offset(false, include_ignored, start)
1626 }
1627
1628 pub fn entries(&self, include_ignored: bool) -> Traversal {
1629 self.traverse_from_offset(true, include_ignored, 0)
1630 }
1631
1632 pub fn repositories(&self) -> impl Iterator<Item = (&Arc<Path>, &RepositoryEntry)> {
1633 self.repository_entries
1634 .iter()
1635 .map(|(path, entry)| (&path.0, entry))
1636 }
1637
1638 /// Given an ordered iterator of entries, returns an iterator of those entries,
1639 /// along with their containing git repository.
1640 pub fn entries_with_repos<'a>(
1641 &'a self,
1642 entries: impl 'a + Iterator<Item = &'a Entry>,
1643 ) -> impl 'a + Iterator<Item = (&'a Entry, Option<&'a RepositoryEntry>)> {
1644 let mut containing_repos = Vec::<(&Arc<Path>, &RepositoryEntry)>::new();
1645 let mut repositories = self.repositories().peekable();
1646 entries.map(move |entry| {
1647 while let Some((repo_path, _)) = containing_repos.last() {
1648 if !entry.path.starts_with(repo_path) {
1649 containing_repos.pop();
1650 } else {
1651 break;
1652 }
1653 }
1654 while let Some((repo_path, _)) = repositories.peek() {
1655 if entry.path.starts_with(repo_path) {
1656 containing_repos.push(repositories.next().unwrap());
1657 } else {
1658 break;
1659 }
1660 }
1661 let repo = containing_repos.last().map(|(_, repo)| *repo);
1662 (entry, repo)
1663 })
1664 }
1665
1666 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1667 let empty_path = Path::new("");
1668 self.entries_by_path
1669 .cursor::<()>()
1670 .filter(move |entry| entry.path.as_ref() != empty_path)
1671 .map(|entry| &entry.path)
1672 }
1673
1674 fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
1675 let mut cursor = self.entries_by_path.cursor();
1676 cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
1677 let traversal = Traversal {
1678 cursor,
1679 include_dirs: true,
1680 include_ignored: true,
1681 };
1682 ChildEntriesIter {
1683 traversal,
1684 parent_path,
1685 }
1686 }
1687
1688 fn descendent_entries<'a>(
1689 &'a self,
1690 include_dirs: bool,
1691 include_ignored: bool,
1692 parent_path: &'a Path,
1693 ) -> DescendentEntriesIter<'a> {
1694 let mut cursor = self.entries_by_path.cursor();
1695 cursor.seek(&TraversalTarget::Path(parent_path), Bias::Left, &());
1696 let mut traversal = Traversal {
1697 cursor,
1698 include_dirs,
1699 include_ignored,
1700 };
1701
1702 if traversal.end_offset() == traversal.start_offset() {
1703 traversal.advance();
1704 }
1705
1706 DescendentEntriesIter {
1707 traversal,
1708 parent_path,
1709 }
1710 }
1711
1712 pub fn root_entry(&self) -> Option<&Entry> {
1713 self.entry_for_path("")
1714 }
1715
1716 pub fn root_name(&self) -> &str {
1717 &self.root_name
1718 }
1719
1720 pub fn root_git_entry(&self) -> Option<RepositoryEntry> {
1721 self.repository_entries
1722 .get(&RepositoryWorkDirectory(Path::new("").into()))
1723 .map(|entry| entry.to_owned())
1724 }
1725
1726 pub fn git_entries(&self) -> impl Iterator<Item = &RepositoryEntry> {
1727 self.repository_entries.values()
1728 }
1729
1730 pub fn scan_id(&self) -> usize {
1731 self.scan_id
1732 }
1733
1734 pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
1735 let path = path.as_ref();
1736 self.traverse_from_path(true, true, path)
1737 .entry()
1738 .and_then(|entry| {
1739 if entry.path.as_ref() == path {
1740 Some(entry)
1741 } else {
1742 None
1743 }
1744 })
1745 }
1746
1747 pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
1748 let entry = self.entries_by_id.get(&id, &())?;
1749 self.entry_for_path(&entry.path)
1750 }
1751
1752 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
1753 self.entry_for_path(path.as_ref()).map(|e| e.inode)
1754 }
1755}
1756
1757impl LocalSnapshot {
1758 pub(crate) fn get_local_repo(&self, repo: &RepositoryEntry) -> Option<&LocalRepositoryEntry> {
1759 self.git_repositories.get(&repo.work_directory.0)
1760 }
1761
1762 pub(crate) fn repo_for_metadata(
1763 &self,
1764 path: &Path,
1765 ) -> Option<(&ProjectEntryId, &LocalRepositoryEntry)> {
1766 self.git_repositories
1767 .iter()
1768 .find(|(_, repo)| repo.in_dot_git(path))
1769 }
1770
1771 #[cfg(test)]
1772 pub(crate) fn build_initial_update(&self, project_id: u64) -> proto::UpdateWorktree {
1773 let root_name = self.root_name.clone();
1774 proto::UpdateWorktree {
1775 project_id,
1776 worktree_id: self.id().to_proto(),
1777 abs_path: self.abs_path().to_string_lossy().into(),
1778 root_name,
1779 updated_entries: self.entries_by_path.iter().map(Into::into).collect(),
1780 removed_entries: Default::default(),
1781 scan_id: self.scan_id as u64,
1782 is_last_update: true,
1783 updated_repositories: self.repository_entries.values().map(Into::into).collect(),
1784 removed_repositories: Default::default(),
1785 }
1786 }
1787
1788 pub(crate) fn build_update(
1789 &self,
1790 other: &Self,
1791 project_id: u64,
1792 worktree_id: u64,
1793 include_ignored: bool,
1794 ) -> proto::UpdateWorktree {
1795 let mut updated_entries = Vec::new();
1796 let mut removed_entries = Vec::new();
1797 let mut self_entries = self
1798 .entries_by_id
1799 .cursor::<()>()
1800 .filter(|e| include_ignored || !e.is_ignored)
1801 .peekable();
1802 let mut other_entries = other
1803 .entries_by_id
1804 .cursor::<()>()
1805 .filter(|e| include_ignored || !e.is_ignored)
1806 .peekable();
1807 loop {
1808 match (self_entries.peek(), other_entries.peek()) {
1809 (Some(self_entry), Some(other_entry)) => {
1810 match Ord::cmp(&self_entry.id, &other_entry.id) {
1811 Ordering::Less => {
1812 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1813 updated_entries.push(entry);
1814 self_entries.next();
1815 }
1816 Ordering::Equal => {
1817 if self_entry.scan_id != other_entry.scan_id {
1818 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1819 updated_entries.push(entry);
1820 }
1821
1822 self_entries.next();
1823 other_entries.next();
1824 }
1825 Ordering::Greater => {
1826 removed_entries.push(other_entry.id.to_proto());
1827 other_entries.next();
1828 }
1829 }
1830 }
1831 (Some(self_entry), None) => {
1832 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1833 updated_entries.push(entry);
1834 self_entries.next();
1835 }
1836 (None, Some(other_entry)) => {
1837 removed_entries.push(other_entry.id.to_proto());
1838 other_entries.next();
1839 }
1840 (None, None) => break,
1841 }
1842 }
1843
1844 let mut updated_repositories: Vec<proto::RepositoryEntry> = Vec::new();
1845 let mut removed_repositories = Vec::new();
1846 let mut self_repos = self.snapshot.repository_entries.iter().peekable();
1847 let mut other_repos = other.snapshot.repository_entries.iter().peekable();
1848 loop {
1849 match (self_repos.peek(), other_repos.peek()) {
1850 (Some((self_work_dir, self_repo)), Some((other_work_dir, other_repo))) => {
1851 match Ord::cmp(self_work_dir, other_work_dir) {
1852 Ordering::Less => {
1853 updated_repositories.push((*self_repo).into());
1854 self_repos.next();
1855 }
1856 Ordering::Equal => {
1857 if self_repo != other_repo {
1858 updated_repositories.push(self_repo.build_update(other_repo));
1859 }
1860
1861 self_repos.next();
1862 other_repos.next();
1863 }
1864 Ordering::Greater => {
1865 removed_repositories.push(other_repo.work_directory.to_proto());
1866 other_repos.next();
1867 }
1868 }
1869 }
1870 (Some((_, self_repo)), None) => {
1871 updated_repositories.push((*self_repo).into());
1872 self_repos.next();
1873 }
1874 (None, Some((_, other_repo))) => {
1875 removed_repositories.push(other_repo.work_directory.to_proto());
1876 other_repos.next();
1877 }
1878 (None, None) => break,
1879 }
1880 }
1881
1882 proto::UpdateWorktree {
1883 project_id,
1884 worktree_id,
1885 abs_path: self.abs_path().to_string_lossy().into(),
1886 root_name: self.root_name().to_string(),
1887 updated_entries,
1888 removed_entries,
1889 scan_id: self.scan_id as u64,
1890 is_last_update: self.completed_scan_id == self.scan_id,
1891 updated_repositories,
1892 removed_repositories,
1893 }
1894 }
1895
1896 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
1897 if entry.is_file() && entry.path.file_name() == Some(&GITIGNORE) {
1898 let abs_path = self.abs_path.join(&entry.path);
1899 match smol::block_on(build_gitignore(&abs_path, fs)) {
1900 Ok(ignore) => {
1901 self.ignores_by_parent_abs_path
1902 .insert(abs_path.parent().unwrap().into(), (Arc::new(ignore), true));
1903 }
1904 Err(error) => {
1905 log::error!(
1906 "error loading .gitignore file {:?} - {:?}",
1907 &entry.path,
1908 error
1909 );
1910 }
1911 }
1912 }
1913
1914 self.reuse_entry_id(&mut entry);
1915
1916 if entry.kind == EntryKind::PendingDir {
1917 if let Some(existing_entry) =
1918 self.entries_by_path.get(&PathKey(entry.path.clone()), &())
1919 {
1920 entry.kind = existing_entry.kind;
1921 }
1922 }
1923
1924 let scan_id = self.scan_id;
1925 let removed = self.entries_by_path.insert_or_replace(entry.clone(), &());
1926 if let Some(removed) = removed {
1927 if removed.id != entry.id {
1928 self.entries_by_id.remove(&removed.id, &());
1929 }
1930 }
1931 self.entries_by_id.insert_or_replace(
1932 PathEntry {
1933 id: entry.id,
1934 path: entry.path.clone(),
1935 is_ignored: entry.is_ignored,
1936 scan_id,
1937 },
1938 &(),
1939 );
1940
1941 entry
1942 }
1943
1944 fn populate_dir(
1945 &mut self,
1946 parent_path: Arc<Path>,
1947 entries: impl IntoIterator<Item = Entry>,
1948 ignore: Option<Arc<Gitignore>>,
1949 fs: &dyn Fs,
1950 ) {
1951 let mut parent_entry = if let Some(parent_entry) =
1952 self.entries_by_path.get(&PathKey(parent_path.clone()), &())
1953 {
1954 parent_entry.clone()
1955 } else {
1956 log::warn!(
1957 "populating a directory {:?} that has been removed",
1958 parent_path
1959 );
1960 return;
1961 };
1962
1963 match parent_entry.kind {
1964 EntryKind::PendingDir => {
1965 parent_entry.kind = EntryKind::Dir;
1966 }
1967 EntryKind::Dir => {}
1968 _ => return,
1969 }
1970
1971 if let Some(ignore) = ignore {
1972 self.ignores_by_parent_abs_path
1973 .insert(self.abs_path.join(&parent_path).into(), (ignore, false));
1974 }
1975
1976 if parent_path.file_name() == Some(&DOT_GIT) {
1977 self.build_repo(parent_path, fs);
1978 }
1979
1980 let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
1981 let mut entries_by_id_edits = Vec::new();
1982
1983 for mut entry in entries {
1984 self.reuse_entry_id(&mut entry);
1985 entries_by_id_edits.push(Edit::Insert(PathEntry {
1986 id: entry.id,
1987 path: entry.path.clone(),
1988 is_ignored: entry.is_ignored,
1989 scan_id: self.scan_id,
1990 }));
1991 entries_by_path_edits.push(Edit::Insert(entry));
1992 }
1993
1994 self.entries_by_path.edit(entries_by_path_edits, &());
1995 self.entries_by_id.edit(entries_by_id_edits, &());
1996 }
1997
1998 fn build_repo(&mut self, parent_path: Arc<Path>, fs: &dyn Fs) -> Option<()> {
1999 let abs_path = self.abs_path.join(&parent_path);
2000 let work_dir: Arc<Path> = parent_path.parent().unwrap().into();
2001
2002 // Guard against repositories inside the repository metadata
2003 if work_dir
2004 .components()
2005 .find(|component| component.as_os_str() == *DOT_GIT)
2006 .is_some()
2007 {
2008 return None;
2009 };
2010
2011 let work_dir_id = self
2012 .entry_for_path(work_dir.clone())
2013 .map(|entry| entry.id)?;
2014
2015 if self.git_repositories.get(&work_dir_id).is_none() {
2016 let repo = fs.open_repo(abs_path.as_path())?;
2017 let work_directory = RepositoryWorkDirectory(work_dir.clone());
2018 let scan_id = self.scan_id;
2019
2020 let repo_lock = repo.lock();
2021
2022 self.repository_entries.insert(
2023 work_directory,
2024 RepositoryEntry {
2025 work_directory: work_dir_id.into(),
2026 branch: repo_lock.branch_name().map(Into::into),
2027 statuses: repo_lock.statuses().unwrap_or_default(),
2028 },
2029 );
2030 drop(repo_lock);
2031
2032 self.git_repositories.insert(
2033 work_dir_id,
2034 LocalRepositoryEntry {
2035 scan_id,
2036 full_scan_id: scan_id,
2037 repo_ptr: repo,
2038 git_dir_path: parent_path.clone(),
2039 },
2040 )
2041 }
2042
2043 Some(())
2044 }
2045 fn reuse_entry_id(&mut self, entry: &mut Entry) {
2046 if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
2047 entry.id = removed_entry_id;
2048 } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
2049 entry.id = existing_entry.id;
2050 }
2051 }
2052
2053 fn remove_path(&mut self, path: &Path) {
2054 let mut new_entries;
2055 let removed_entries;
2056 {
2057 let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
2058 new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
2059 removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
2060 new_entries.push_tree(cursor.suffix(&()), &());
2061 }
2062 self.entries_by_path = new_entries;
2063
2064 let mut entries_by_id_edits = Vec::new();
2065 for entry in removed_entries.cursor::<()>() {
2066 let removed_entry_id = self
2067 .removed_entry_ids
2068 .entry(entry.inode)
2069 .or_insert(entry.id);
2070 *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
2071 entries_by_id_edits.push(Edit::Remove(entry.id));
2072 }
2073 self.entries_by_id.edit(entries_by_id_edits, &());
2074
2075 if path.file_name() == Some(&GITIGNORE) {
2076 let abs_parent_path = self.abs_path.join(path.parent().unwrap());
2077 if let Some((_, needs_update)) = self
2078 .ignores_by_parent_abs_path
2079 .get_mut(abs_parent_path.as_path())
2080 {
2081 *needs_update = true;
2082 }
2083 }
2084 }
2085
2086 fn ancestor_inodes_for_path(&self, path: &Path) -> TreeSet<u64> {
2087 let mut inodes = TreeSet::default();
2088 for ancestor in path.ancestors().skip(1) {
2089 if let Some(entry) = self.entry_for_path(ancestor) {
2090 inodes.insert(entry.inode);
2091 }
2092 }
2093 inodes
2094 }
2095
2096 fn ignore_stack_for_abs_path(&self, abs_path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
2097 let mut new_ignores = Vec::new();
2098 for ancestor in abs_path.ancestors().skip(1) {
2099 if let Some((ignore, _)) = self.ignores_by_parent_abs_path.get(ancestor) {
2100 new_ignores.push((ancestor, Some(ignore.clone())));
2101 } else {
2102 new_ignores.push((ancestor, None));
2103 }
2104 }
2105
2106 let mut ignore_stack = IgnoreStack::none();
2107 for (parent_abs_path, ignore) in new_ignores.into_iter().rev() {
2108 if ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
2109 ignore_stack = IgnoreStack::all();
2110 break;
2111 } else if let Some(ignore) = ignore {
2112 ignore_stack = ignore_stack.append(parent_abs_path.into(), ignore);
2113 }
2114 }
2115
2116 if ignore_stack.is_abs_path_ignored(abs_path, is_dir) {
2117 ignore_stack = IgnoreStack::all();
2118 }
2119
2120 ignore_stack
2121 }
2122}
2123
2124async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
2125 let contents = fs.load(abs_path).await?;
2126 let parent = abs_path.parent().unwrap_or_else(|| Path::new("/"));
2127 let mut builder = GitignoreBuilder::new(parent);
2128 for line in contents.lines() {
2129 builder.add_line(Some(abs_path.into()), line)?;
2130 }
2131 Ok(builder.build()?)
2132}
2133
2134impl WorktreeId {
2135 pub fn from_usize(handle_id: usize) -> Self {
2136 Self(handle_id)
2137 }
2138
2139 pub(crate) fn from_proto(id: u64) -> Self {
2140 Self(id as usize)
2141 }
2142
2143 pub fn to_proto(&self) -> u64 {
2144 self.0 as u64
2145 }
2146
2147 pub fn to_usize(&self) -> usize {
2148 self.0
2149 }
2150}
2151
2152impl fmt::Display for WorktreeId {
2153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2154 self.0.fmt(f)
2155 }
2156}
2157
2158impl Deref for Worktree {
2159 type Target = Snapshot;
2160
2161 fn deref(&self) -> &Self::Target {
2162 match self {
2163 Worktree::Local(worktree) => &worktree.snapshot,
2164 Worktree::Remote(worktree) => &worktree.snapshot,
2165 }
2166 }
2167}
2168
2169impl Deref for LocalWorktree {
2170 type Target = LocalSnapshot;
2171
2172 fn deref(&self) -> &Self::Target {
2173 &self.snapshot
2174 }
2175}
2176
2177impl Deref for RemoteWorktree {
2178 type Target = Snapshot;
2179
2180 fn deref(&self) -> &Self::Target {
2181 &self.snapshot
2182 }
2183}
2184
2185impl fmt::Debug for LocalWorktree {
2186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2187 self.snapshot.fmt(f)
2188 }
2189}
2190
2191impl fmt::Debug for Snapshot {
2192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2193 struct EntriesById<'a>(&'a SumTree<PathEntry>);
2194 struct EntriesByPath<'a>(&'a SumTree<Entry>);
2195
2196 impl<'a> fmt::Debug for EntriesByPath<'a> {
2197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2198 f.debug_map()
2199 .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
2200 .finish()
2201 }
2202 }
2203
2204 impl<'a> fmt::Debug for EntriesById<'a> {
2205 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2206 f.debug_list().entries(self.0.iter()).finish()
2207 }
2208 }
2209
2210 f.debug_struct("Snapshot")
2211 .field("id", &self.id)
2212 .field("root_name", &self.root_name)
2213 .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
2214 .field("entries_by_id", &EntriesById(&self.entries_by_id))
2215 .finish()
2216 }
2217}
2218
2219#[derive(Clone, PartialEq)]
2220pub struct File {
2221 pub worktree: ModelHandle<Worktree>,
2222 pub path: Arc<Path>,
2223 pub mtime: SystemTime,
2224 pub(crate) entry_id: ProjectEntryId,
2225 pub(crate) is_local: bool,
2226 pub(crate) is_deleted: bool,
2227}
2228
2229impl language::File for File {
2230 fn as_local(&self) -> Option<&dyn language::LocalFile> {
2231 if self.is_local {
2232 Some(self)
2233 } else {
2234 None
2235 }
2236 }
2237
2238 fn mtime(&self) -> SystemTime {
2239 self.mtime
2240 }
2241
2242 fn path(&self) -> &Arc<Path> {
2243 &self.path
2244 }
2245
2246 fn full_path(&self, cx: &AppContext) -> PathBuf {
2247 let mut full_path = PathBuf::new();
2248 let worktree = self.worktree.read(cx);
2249
2250 if worktree.is_visible() {
2251 full_path.push(worktree.root_name());
2252 } else {
2253 let path = worktree.abs_path();
2254
2255 if worktree.is_local() && path.starts_with(HOME.as_path()) {
2256 full_path.push("~");
2257 full_path.push(path.strip_prefix(HOME.as_path()).unwrap());
2258 } else {
2259 full_path.push(path)
2260 }
2261 }
2262
2263 if self.path.components().next().is_some() {
2264 full_path.push(&self.path);
2265 }
2266
2267 full_path
2268 }
2269
2270 /// Returns the last component of this handle's absolute path. If this handle refers to the root
2271 /// of its worktree, then this method will return the name of the worktree itself.
2272 fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr {
2273 self.path
2274 .file_name()
2275 .unwrap_or_else(|| OsStr::new(&self.worktree.read(cx).root_name))
2276 }
2277
2278 fn is_deleted(&self) -> bool {
2279 self.is_deleted
2280 }
2281
2282 fn as_any(&self) -> &dyn Any {
2283 self
2284 }
2285
2286 fn to_proto(&self) -> rpc::proto::File {
2287 rpc::proto::File {
2288 worktree_id: self.worktree.id() as u64,
2289 entry_id: self.entry_id.to_proto(),
2290 path: self.path.to_string_lossy().into(),
2291 mtime: Some(self.mtime.into()),
2292 is_deleted: self.is_deleted,
2293 }
2294 }
2295}
2296
2297impl language::LocalFile for File {
2298 fn abs_path(&self, cx: &AppContext) -> PathBuf {
2299 self.worktree
2300 .read(cx)
2301 .as_local()
2302 .unwrap()
2303 .abs_path
2304 .join(&self.path)
2305 }
2306
2307 fn load(&self, cx: &AppContext) -> Task<Result<String>> {
2308 let worktree = self.worktree.read(cx).as_local().unwrap();
2309 let abs_path = worktree.absolutize(&self.path);
2310 let fs = worktree.fs.clone();
2311 cx.background()
2312 .spawn(async move { fs.load(&abs_path).await })
2313 }
2314
2315 fn buffer_reloaded(
2316 &self,
2317 buffer_id: u64,
2318 version: &clock::Global,
2319 fingerprint: RopeFingerprint,
2320 line_ending: LineEnding,
2321 mtime: SystemTime,
2322 cx: &mut AppContext,
2323 ) {
2324 let worktree = self.worktree.read(cx).as_local().unwrap();
2325 if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) {
2326 worktree
2327 .client
2328 .send(proto::BufferReloaded {
2329 project_id,
2330 buffer_id,
2331 version: serialize_version(version),
2332 mtime: Some(mtime.into()),
2333 fingerprint: serialize_fingerprint(fingerprint),
2334 line_ending: serialize_line_ending(line_ending) as i32,
2335 })
2336 .log_err();
2337 }
2338 }
2339}
2340
2341impl File {
2342 pub fn from_proto(
2343 proto: rpc::proto::File,
2344 worktree: ModelHandle<Worktree>,
2345 cx: &AppContext,
2346 ) -> Result<Self> {
2347 let worktree_id = worktree
2348 .read(cx)
2349 .as_remote()
2350 .ok_or_else(|| anyhow!("not remote"))?
2351 .id();
2352
2353 if worktree_id.to_proto() != proto.worktree_id {
2354 return Err(anyhow!("worktree id does not match file"));
2355 }
2356
2357 Ok(Self {
2358 worktree,
2359 path: Path::new(&proto.path).into(),
2360 mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(),
2361 entry_id: ProjectEntryId::from_proto(proto.entry_id),
2362 is_local: false,
2363 is_deleted: proto.is_deleted,
2364 })
2365 }
2366
2367 pub fn from_dyn(file: Option<&Arc<dyn language::File>>) -> Option<&Self> {
2368 file.and_then(|f| f.as_any().downcast_ref())
2369 }
2370
2371 pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
2372 self.worktree.read(cx).id()
2373 }
2374
2375 pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
2376 if self.is_deleted {
2377 None
2378 } else {
2379 Some(self.entry_id)
2380 }
2381 }
2382}
2383
2384#[derive(Clone, Debug, PartialEq, Eq)]
2385pub struct Entry {
2386 pub id: ProjectEntryId,
2387 pub kind: EntryKind,
2388 pub path: Arc<Path>,
2389 pub inode: u64,
2390 pub mtime: SystemTime,
2391 pub is_symlink: bool,
2392 pub is_ignored: bool,
2393}
2394
2395#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2396pub enum EntryKind {
2397 PendingDir,
2398 Dir,
2399 File(CharBag),
2400}
2401
2402#[derive(Clone, Copy, Debug)]
2403pub enum PathChange {
2404 Added,
2405 Removed,
2406 Updated,
2407 AddedOrUpdated,
2408}
2409
2410impl Entry {
2411 fn new(
2412 path: Arc<Path>,
2413 metadata: &fs::Metadata,
2414 next_entry_id: &AtomicUsize,
2415 root_char_bag: CharBag,
2416 ) -> Self {
2417 Self {
2418 id: ProjectEntryId::new(next_entry_id),
2419 kind: if metadata.is_dir {
2420 EntryKind::PendingDir
2421 } else {
2422 EntryKind::File(char_bag_for_path(root_char_bag, &path))
2423 },
2424 path,
2425 inode: metadata.inode,
2426 mtime: metadata.mtime,
2427 is_symlink: metadata.is_symlink,
2428 is_ignored: false,
2429 }
2430 }
2431
2432 pub fn is_dir(&self) -> bool {
2433 matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
2434 }
2435
2436 pub fn is_file(&self) -> bool {
2437 matches!(self.kind, EntryKind::File(_))
2438 }
2439}
2440
2441impl sum_tree::Item for Entry {
2442 type Summary = EntrySummary;
2443
2444 fn summary(&self) -> Self::Summary {
2445 let visible_count = if self.is_ignored { 0 } else { 1 };
2446 let file_count;
2447 let visible_file_count;
2448 if self.is_file() {
2449 file_count = 1;
2450 visible_file_count = visible_count;
2451 } else {
2452 file_count = 0;
2453 visible_file_count = 0;
2454 }
2455
2456 EntrySummary {
2457 max_path: self.path.clone(),
2458 count: 1,
2459 visible_count,
2460 file_count,
2461 visible_file_count,
2462 }
2463 }
2464}
2465
2466impl sum_tree::KeyedItem for Entry {
2467 type Key = PathKey;
2468
2469 fn key(&self) -> Self::Key {
2470 PathKey(self.path.clone())
2471 }
2472}
2473
2474#[derive(Clone, Debug)]
2475pub struct EntrySummary {
2476 max_path: Arc<Path>,
2477 count: usize,
2478 visible_count: usize,
2479 file_count: usize,
2480 visible_file_count: usize,
2481}
2482
2483impl Default for EntrySummary {
2484 fn default() -> Self {
2485 Self {
2486 max_path: Arc::from(Path::new("")),
2487 count: 0,
2488 visible_count: 0,
2489 file_count: 0,
2490 visible_file_count: 0,
2491 }
2492 }
2493}
2494
2495impl sum_tree::Summary for EntrySummary {
2496 type Context = ();
2497
2498 fn add_summary(&mut self, rhs: &Self, _: &()) {
2499 self.max_path = rhs.max_path.clone();
2500 self.count += rhs.count;
2501 self.visible_count += rhs.visible_count;
2502 self.file_count += rhs.file_count;
2503 self.visible_file_count += rhs.visible_file_count;
2504 }
2505}
2506
2507#[derive(Clone, Debug)]
2508struct PathEntry {
2509 id: ProjectEntryId,
2510 path: Arc<Path>,
2511 is_ignored: bool,
2512 scan_id: usize,
2513}
2514
2515impl sum_tree::Item for PathEntry {
2516 type Summary = PathEntrySummary;
2517
2518 fn summary(&self) -> Self::Summary {
2519 PathEntrySummary { max_id: self.id }
2520 }
2521}
2522
2523impl sum_tree::KeyedItem for PathEntry {
2524 type Key = ProjectEntryId;
2525
2526 fn key(&self) -> Self::Key {
2527 self.id
2528 }
2529}
2530
2531#[derive(Clone, Debug, Default)]
2532struct PathEntrySummary {
2533 max_id: ProjectEntryId,
2534}
2535
2536impl sum_tree::Summary for PathEntrySummary {
2537 type Context = ();
2538
2539 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
2540 self.max_id = summary.max_id;
2541 }
2542}
2543
2544impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
2545 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
2546 *self = summary.max_id;
2547 }
2548}
2549
2550#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2551pub struct PathKey(Arc<Path>);
2552
2553impl Default for PathKey {
2554 fn default() -> Self {
2555 Self(Path::new("").into())
2556 }
2557}
2558
2559impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
2560 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2561 self.0 = summary.max_path.clone();
2562 }
2563}
2564
2565struct BackgroundScanner {
2566 snapshot: Mutex<LocalSnapshot>,
2567 fs: Arc<dyn Fs>,
2568 status_updates_tx: UnboundedSender<ScanState>,
2569 executor: Arc<executor::Background>,
2570 refresh_requests_rx: channel::Receiver<(Vec<PathBuf>, barrier::Sender)>,
2571 prev_state: Mutex<BackgroundScannerState>,
2572 next_entry_id: Arc<AtomicUsize>,
2573 finished_initial_scan: bool,
2574}
2575
2576struct BackgroundScannerState {
2577 snapshot: Snapshot,
2578 event_paths: Vec<Arc<Path>>,
2579}
2580
2581impl BackgroundScanner {
2582 fn new(
2583 snapshot: LocalSnapshot,
2584 next_entry_id: Arc<AtomicUsize>,
2585 fs: Arc<dyn Fs>,
2586 status_updates_tx: UnboundedSender<ScanState>,
2587 executor: Arc<executor::Background>,
2588 refresh_requests_rx: channel::Receiver<(Vec<PathBuf>, barrier::Sender)>,
2589 ) -> Self {
2590 Self {
2591 fs,
2592 status_updates_tx,
2593 executor,
2594 refresh_requests_rx,
2595 next_entry_id,
2596 prev_state: Mutex::new(BackgroundScannerState {
2597 snapshot: snapshot.snapshot.clone(),
2598 event_paths: Default::default(),
2599 }),
2600 snapshot: Mutex::new(snapshot),
2601 finished_initial_scan: false,
2602 }
2603 }
2604
2605 async fn run(
2606 &mut self,
2607 mut events_rx: Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>>,
2608 ) {
2609 use futures::FutureExt as _;
2610
2611 let (root_abs_path, root_inode) = {
2612 let snapshot = self.snapshot.lock();
2613 (
2614 snapshot.abs_path.clone(),
2615 snapshot.root_entry().map(|e| e.inode),
2616 )
2617 };
2618
2619 // Populate ignores above the root.
2620 let ignore_stack;
2621 for ancestor in root_abs_path.ancestors().skip(1) {
2622 if let Ok(ignore) = build_gitignore(&ancestor.join(&*GITIGNORE), self.fs.as_ref()).await
2623 {
2624 self.snapshot
2625 .lock()
2626 .ignores_by_parent_abs_path
2627 .insert(ancestor.into(), (ignore.into(), false));
2628 }
2629 }
2630 {
2631 let mut snapshot = self.snapshot.lock();
2632 snapshot.scan_id += 1;
2633 ignore_stack = snapshot.ignore_stack_for_abs_path(&root_abs_path, true);
2634 if ignore_stack.is_all() {
2635 if let Some(mut root_entry) = snapshot.root_entry().cloned() {
2636 root_entry.is_ignored = true;
2637 snapshot.insert_entry(root_entry, self.fs.as_ref());
2638 }
2639 }
2640 };
2641
2642 // Perform an initial scan of the directory.
2643 let (scan_job_tx, scan_job_rx) = channel::unbounded();
2644 smol::block_on(scan_job_tx.send(ScanJob {
2645 abs_path: root_abs_path,
2646 path: Arc::from(Path::new("")),
2647 ignore_stack,
2648 ancestor_inodes: TreeSet::from_ordered_entries(root_inode),
2649 scan_queue: scan_job_tx.clone(),
2650 }))
2651 .unwrap();
2652 drop(scan_job_tx);
2653 self.scan_dirs(true, scan_job_rx).await;
2654 {
2655 let mut snapshot = self.snapshot.lock();
2656 snapshot.completed_scan_id = snapshot.scan_id;
2657 }
2658 self.send_status_update(false, None);
2659
2660 // Process any any FS events that occurred while performing the initial scan.
2661 // For these events, update events cannot be as precise, because we didn't
2662 // have the previous state loaded yet.
2663 if let Poll::Ready(Some(events)) = futures::poll!(events_rx.next()) {
2664 let mut paths = events.into_iter().map(|e| e.path).collect::<Vec<_>>();
2665 while let Poll::Ready(Some(more_events)) = futures::poll!(events_rx.next()) {
2666 paths.extend(more_events.into_iter().map(|e| e.path));
2667 }
2668 self.process_events(paths).await;
2669 }
2670
2671 self.finished_initial_scan = true;
2672
2673 // Continue processing events until the worktree is dropped.
2674 loop {
2675 select_biased! {
2676 // Process any path refresh requests from the worktree. Prioritize
2677 // these before handling changes reported by the filesystem.
2678 request = self.refresh_requests_rx.recv().fuse() => {
2679 let Ok((paths, barrier)) = request else { break };
2680 if !self.process_refresh_request(paths.clone(), barrier).await {
2681 return;
2682 }
2683 }
2684
2685 events = events_rx.next().fuse() => {
2686 let Some(events) = events else { break };
2687 let mut paths = events.into_iter().map(|e| e.path).collect::<Vec<_>>();
2688 while let Poll::Ready(Some(more_events)) = futures::poll!(events_rx.next()) {
2689 paths.extend(more_events.into_iter().map(|e| e.path));
2690 }
2691 self.process_events(paths.clone()).await;
2692 }
2693 }
2694 }
2695 }
2696
2697 async fn process_refresh_request(&self, paths: Vec<PathBuf>, barrier: barrier::Sender) -> bool {
2698 if let Some(mut paths) = self.reload_entries_for_paths(paths, None).await {
2699 paths.sort_unstable();
2700 util::extend_sorted(
2701 &mut self.prev_state.lock().event_paths,
2702 paths,
2703 usize::MAX,
2704 Ord::cmp,
2705 );
2706 }
2707 self.send_status_update(false, Some(barrier))
2708 }
2709
2710 async fn process_events(&mut self, paths: Vec<PathBuf>) {
2711 let (scan_job_tx, scan_job_rx) = channel::unbounded();
2712 let paths = self
2713 .reload_entries_for_paths(paths, Some(scan_job_tx.clone()))
2714 .await;
2715 if let Some(paths) = &paths {
2716 util::extend_sorted(
2717 &mut self.prev_state.lock().event_paths,
2718 paths.iter().cloned(),
2719 usize::MAX,
2720 Ord::cmp,
2721 );
2722 }
2723 drop(scan_job_tx);
2724 self.scan_dirs(false, scan_job_rx).await;
2725
2726 self.update_ignore_statuses().await;
2727
2728 let mut snapshot = self.snapshot.lock();
2729
2730 if let Some(paths) = paths {
2731 for path in paths {
2732 self.reload_repo_for_file_path(&path, &mut *snapshot, self.fs.as_ref());
2733 }
2734 }
2735
2736 let mut git_repositories = mem::take(&mut snapshot.git_repositories);
2737 git_repositories.retain(|work_directory_id, _| {
2738 snapshot
2739 .entry_for_id(*work_directory_id)
2740 .map_or(false, |entry| {
2741 snapshot.entry_for_path(entry.path.join(*DOT_GIT)).is_some()
2742 })
2743 });
2744 snapshot.git_repositories = git_repositories;
2745
2746 let mut git_repository_entries = mem::take(&mut snapshot.snapshot.repository_entries);
2747 git_repository_entries.retain(|_, entry| {
2748 snapshot
2749 .git_repositories
2750 .get(&entry.work_directory.0)
2751 .is_some()
2752 });
2753 snapshot.snapshot.repository_entries = git_repository_entries;
2754
2755 snapshot.removed_entry_ids.clear();
2756 snapshot.completed_scan_id = snapshot.scan_id;
2757
2758 drop(snapshot);
2759
2760 self.send_status_update(false, None);
2761 self.prev_state.lock().event_paths.clear();
2762 }
2763
2764 async fn scan_dirs(
2765 &self,
2766 enable_progress_updates: bool,
2767 scan_jobs_rx: channel::Receiver<ScanJob>,
2768 ) {
2769 use futures::FutureExt as _;
2770
2771 if self
2772 .status_updates_tx
2773 .unbounded_send(ScanState::Started)
2774 .is_err()
2775 {
2776 return;
2777 }
2778
2779 let progress_update_count = AtomicUsize::new(0);
2780 self.executor
2781 .scoped(|scope| {
2782 for _ in 0..self.executor.num_cpus() {
2783 scope.spawn(async {
2784 let mut last_progress_update_count = 0;
2785 let progress_update_timer = self.progress_timer(enable_progress_updates).fuse();
2786 futures::pin_mut!(progress_update_timer);
2787
2788 loop {
2789 select_biased! {
2790 // Process any path refresh requests before moving on to process
2791 // the scan queue, so that user operations are prioritized.
2792 request = self.refresh_requests_rx.recv().fuse() => {
2793 let Ok((paths, barrier)) = request else { break };
2794 if !self.process_refresh_request(paths, barrier).await {
2795 return;
2796 }
2797 }
2798
2799 // Send periodic progress updates to the worktree. Use an atomic counter
2800 // to ensure that only one of the workers sends a progress update after
2801 // the update interval elapses.
2802 _ = progress_update_timer => {
2803 match progress_update_count.compare_exchange(
2804 last_progress_update_count,
2805 last_progress_update_count + 1,
2806 SeqCst,
2807 SeqCst
2808 ) {
2809 Ok(_) => {
2810 last_progress_update_count += 1;
2811 self.send_status_update(true, None);
2812 }
2813 Err(count) => {
2814 last_progress_update_count = count;
2815 }
2816 }
2817 progress_update_timer.set(self.progress_timer(enable_progress_updates).fuse());
2818 }
2819
2820 // Recursively load directories from the file system.
2821 job = scan_jobs_rx.recv().fuse() => {
2822 let Ok(job) = job else { break };
2823 if let Err(err) = self.scan_dir(&job).await {
2824 if job.path.as_ref() != Path::new("") {
2825 log::error!("error scanning directory {:?}: {}", job.abs_path, err);
2826 }
2827 }
2828 }
2829 }
2830 }
2831 })
2832 }
2833 })
2834 .await;
2835 }
2836
2837 fn send_status_update(&self, scanning: bool, barrier: Option<barrier::Sender>) -> bool {
2838 let mut prev_state = self.prev_state.lock();
2839 let new_snapshot = self.snapshot.lock().clone();
2840 let old_snapshot = mem::replace(&mut prev_state.snapshot, new_snapshot.snapshot.clone());
2841
2842 let changes = self.build_change_set(
2843 &old_snapshot,
2844 &new_snapshot.snapshot,
2845 &prev_state.event_paths,
2846 );
2847
2848 self.status_updates_tx
2849 .unbounded_send(ScanState::Updated {
2850 snapshot: new_snapshot,
2851 changes,
2852 scanning,
2853 barrier,
2854 })
2855 .is_ok()
2856 }
2857
2858 async fn scan_dir(&self, job: &ScanJob) -> Result<()> {
2859 let mut new_entries: Vec<Entry> = Vec::new();
2860 let mut new_jobs: Vec<Option<ScanJob>> = Vec::new();
2861 let mut ignore_stack = job.ignore_stack.clone();
2862 let mut new_ignore = None;
2863 let (root_abs_path, root_char_bag, next_entry_id) = {
2864 let snapshot = self.snapshot.lock();
2865 (
2866 snapshot.abs_path().clone(),
2867 snapshot.root_char_bag,
2868 self.next_entry_id.clone(),
2869 )
2870 };
2871 let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2872 while let Some(child_abs_path) = child_paths.next().await {
2873 let child_abs_path: Arc<Path> = match child_abs_path {
2874 Ok(child_abs_path) => child_abs_path.into(),
2875 Err(error) => {
2876 log::error!("error processing entry {:?}", error);
2877 continue;
2878 }
2879 };
2880
2881 let child_name = child_abs_path.file_name().unwrap();
2882 let child_path: Arc<Path> = job.path.join(child_name).into();
2883 let child_metadata = match self.fs.metadata(&child_abs_path).await {
2884 Ok(Some(metadata)) => metadata,
2885 Ok(None) => continue,
2886 Err(err) => {
2887 log::error!("error processing {:?}: {:?}", child_abs_path, err);
2888 continue;
2889 }
2890 };
2891
2892 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2893 if child_name == *GITIGNORE {
2894 match build_gitignore(&child_abs_path, self.fs.as_ref()).await {
2895 Ok(ignore) => {
2896 let ignore = Arc::new(ignore);
2897 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
2898 new_ignore = Some(ignore);
2899 }
2900 Err(error) => {
2901 log::error!(
2902 "error loading .gitignore file {:?} - {:?}",
2903 child_name,
2904 error
2905 );
2906 }
2907 }
2908
2909 // Update ignore status of any child entries we've already processed to reflect the
2910 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2911 // there should rarely be too numerous. Update the ignore stack associated with any
2912 // new jobs as well.
2913 let mut new_jobs = new_jobs.iter_mut();
2914 for entry in &mut new_entries {
2915 let entry_abs_path = root_abs_path.join(&entry.path);
2916 entry.is_ignored =
2917 ignore_stack.is_abs_path_ignored(&entry_abs_path, entry.is_dir());
2918
2919 if entry.is_dir() {
2920 if let Some(job) = new_jobs.next().expect("Missing scan job for entry") {
2921 job.ignore_stack = if entry.is_ignored {
2922 IgnoreStack::all()
2923 } else {
2924 ignore_stack.clone()
2925 };
2926 }
2927 }
2928 }
2929 }
2930
2931 let mut child_entry = Entry::new(
2932 child_path.clone(),
2933 &child_metadata,
2934 &next_entry_id,
2935 root_char_bag,
2936 );
2937
2938 if child_entry.is_dir() {
2939 let is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, true);
2940 child_entry.is_ignored = is_ignored;
2941
2942 // Avoid recursing until crash in the case of a recursive symlink
2943 if !job.ancestor_inodes.contains(&child_entry.inode) {
2944 let mut ancestor_inodes = job.ancestor_inodes.clone();
2945 ancestor_inodes.insert(child_entry.inode);
2946
2947 new_jobs.push(Some(ScanJob {
2948 abs_path: child_abs_path,
2949 path: child_path,
2950 ignore_stack: if is_ignored {
2951 IgnoreStack::all()
2952 } else {
2953 ignore_stack.clone()
2954 },
2955 ancestor_inodes,
2956 scan_queue: job.scan_queue.clone(),
2957 }));
2958 } else {
2959 new_jobs.push(None);
2960 }
2961 } else {
2962 child_entry.is_ignored = ignore_stack.is_abs_path_ignored(&child_abs_path, false);
2963 }
2964
2965 new_entries.push(child_entry);
2966 }
2967
2968 self.snapshot.lock().populate_dir(
2969 job.path.clone(),
2970 new_entries,
2971 new_ignore,
2972 self.fs.as_ref(),
2973 );
2974
2975 for new_job in new_jobs {
2976 if let Some(new_job) = new_job {
2977 job.scan_queue.send(new_job).await.unwrap();
2978 }
2979 }
2980
2981 Ok(())
2982 }
2983
2984 async fn reload_entries_for_paths(
2985 &self,
2986 mut abs_paths: Vec<PathBuf>,
2987 scan_queue_tx: Option<Sender<ScanJob>>,
2988 ) -> Option<Vec<Arc<Path>>> {
2989 let doing_recursive_update = scan_queue_tx.is_some();
2990
2991 abs_paths.sort_unstable();
2992 abs_paths.dedup_by(|a, b| a.starts_with(&b));
2993
2994 let root_abs_path = self.snapshot.lock().abs_path.clone();
2995 let root_canonical_path = self.fs.canonicalize(&root_abs_path).await.log_err()?;
2996 let metadata = futures::future::join_all(
2997 abs_paths
2998 .iter()
2999 .map(|abs_path| self.fs.metadata(&abs_path))
3000 .collect::<Vec<_>>(),
3001 )
3002 .await;
3003
3004 let mut snapshot = self.snapshot.lock();
3005 let is_idle = snapshot.completed_scan_id == snapshot.scan_id;
3006 snapshot.scan_id += 1;
3007 if is_idle && !doing_recursive_update {
3008 snapshot.completed_scan_id = snapshot.scan_id;
3009 }
3010
3011 // Remove any entries for paths that no longer exist or are being recursively
3012 // refreshed. Do this before adding any new entries, so that renames can be
3013 // detected regardless of the order of the paths.
3014 let mut event_paths = Vec::<Arc<Path>>::with_capacity(abs_paths.len());
3015 for (abs_path, metadata) in abs_paths.iter().zip(metadata.iter()) {
3016 if let Ok(path) = abs_path.strip_prefix(&root_canonical_path) {
3017 if matches!(metadata, Ok(None)) || doing_recursive_update {
3018 snapshot.remove_path(path);
3019 }
3020 event_paths.push(path.into());
3021 } else {
3022 log::error!(
3023 "unexpected event {:?} for root path {:?}",
3024 abs_path,
3025 root_canonical_path
3026 );
3027 }
3028 }
3029
3030 for (path, metadata) in event_paths.iter().cloned().zip(metadata.into_iter()) {
3031 let abs_path: Arc<Path> = root_abs_path.join(&path).into();
3032
3033 match metadata {
3034 Ok(Some(metadata)) => {
3035 let ignore_stack =
3036 snapshot.ignore_stack_for_abs_path(&abs_path, metadata.is_dir);
3037 let mut fs_entry = Entry::new(
3038 path.clone(),
3039 &metadata,
3040 self.next_entry_id.as_ref(),
3041 snapshot.root_char_bag,
3042 );
3043 fs_entry.is_ignored = ignore_stack.is_all();
3044 snapshot.insert_entry(fs_entry, self.fs.as_ref());
3045
3046 if let Some(scan_queue_tx) = &scan_queue_tx {
3047 let mut ancestor_inodes = snapshot.ancestor_inodes_for_path(&path);
3048 if metadata.is_dir && !ancestor_inodes.contains(&metadata.inode) {
3049 ancestor_inodes.insert(metadata.inode);
3050 smol::block_on(scan_queue_tx.send(ScanJob {
3051 abs_path,
3052 path,
3053 ignore_stack,
3054 ancestor_inodes,
3055 scan_queue: scan_queue_tx.clone(),
3056 }))
3057 .unwrap();
3058 }
3059 }
3060 }
3061 Ok(None) => {
3062 self.remove_repo_path(&path, &mut snapshot);
3063 }
3064 Err(err) => {
3065 // TODO - create a special 'error' entry in the entries tree to mark this
3066 log::error!("error reading file on event {:?}", err);
3067 }
3068 }
3069 }
3070
3071 Some(event_paths)
3072 }
3073
3074 fn remove_repo_path(&self, path: &Path, snapshot: &mut LocalSnapshot) -> Option<()> {
3075 if !path
3076 .components()
3077 .any(|component| component.as_os_str() == *DOT_GIT)
3078 {
3079 let scan_id = snapshot.scan_id;
3080 let repo = snapshot.repo_for(&path)?;
3081
3082 let repo_path = repo.work_directory.relativize(&snapshot, &path)?;
3083
3084 let work_dir = repo.work_directory(snapshot)?;
3085 let work_dir_id = repo.work_directory;
3086
3087 snapshot
3088 .git_repositories
3089 .update(&work_dir_id, |entry| entry.scan_id = scan_id);
3090
3091 snapshot.repository_entries.update(&work_dir, |entry| {
3092 entry
3093 .statuses
3094 .remove_range(&repo_path, &RepoPathDescendants(&repo_path))
3095 });
3096 }
3097
3098 Some(())
3099 }
3100
3101 fn reload_repo_for_file_path(
3102 &self,
3103 path: &Path,
3104 snapshot: &mut LocalSnapshot,
3105 fs: &dyn Fs,
3106 ) -> Option<()> {
3107 let scan_id = snapshot.scan_id;
3108
3109 if path
3110 .components()
3111 .any(|component| component.as_os_str() == *DOT_GIT)
3112 {
3113 let (entry_id, repo_ptr) = {
3114 let Some((entry_id, repo)) = snapshot.repo_for_metadata(&path) else {
3115 let dot_git_dir = path.ancestors()
3116 .skip_while(|ancestor| ancestor.file_name() != Some(&*DOT_GIT))
3117 .next()?;
3118
3119 snapshot.build_repo(dot_git_dir.into(), fs);
3120 return None;
3121 };
3122 if repo.full_scan_id == scan_id {
3123 return None;
3124 }
3125 (*entry_id, repo.repo_ptr.to_owned())
3126 };
3127
3128 let work_dir = snapshot
3129 .entry_for_id(entry_id)
3130 .map(|entry| RepositoryWorkDirectory(entry.path.clone()))?;
3131
3132 let repo = repo_ptr.lock();
3133 repo.reload_index();
3134 let branch = repo.branch_name();
3135 let statuses = repo.statuses().unwrap_or_default();
3136
3137 snapshot.git_repositories.update(&entry_id, |entry| {
3138 entry.scan_id = scan_id;
3139 entry.full_scan_id = scan_id;
3140 });
3141
3142 snapshot.repository_entries.update(&work_dir, |entry| {
3143 entry.branch = branch.map(Into::into);
3144 entry.statuses = statuses;
3145 });
3146 } else {
3147 if snapshot
3148 .entry_for_path(&path)
3149 .map(|entry| entry.is_ignored)
3150 .unwrap_or(false)
3151 {
3152 self.remove_repo_path(&path, snapshot);
3153 return None;
3154 }
3155
3156 let repo = snapshot.repo_for(&path)?;
3157
3158 let work_dir = repo.work_directory(snapshot)?;
3159 let work_dir_id = repo.work_directory.clone();
3160
3161 snapshot
3162 .git_repositories
3163 .update(&work_dir_id, |entry| entry.scan_id = scan_id);
3164
3165 let local_repo = snapshot.get_local_repo(&repo)?.to_owned();
3166
3167 // Short circuit if we've already scanned everything
3168 if local_repo.full_scan_id == scan_id {
3169 return None;
3170 }
3171
3172 let mut repository = snapshot.repository_entries.remove(&work_dir)?;
3173
3174 for entry in snapshot.descendent_entries(false, false, path) {
3175 let Some(repo_path) = repo.work_directory.relativize(snapshot, &entry.path) else {
3176 continue;
3177 };
3178
3179 let status = local_repo.repo_ptr.lock().status(&repo_path);
3180 if let Some(status) = status {
3181 repository.statuses.insert(repo_path.clone(), status);
3182 } else {
3183 repository.statuses.remove(&repo_path);
3184 }
3185 }
3186
3187 snapshot.repository_entries.insert(work_dir, repository)
3188 }
3189
3190 Some(())
3191 }
3192
3193 async fn update_ignore_statuses(&self) {
3194 use futures::FutureExt as _;
3195
3196 let mut snapshot = self.snapshot.lock().clone();
3197 let mut ignores_to_update = Vec::new();
3198 let mut ignores_to_delete = Vec::new();
3199 let abs_path = snapshot.abs_path.clone();
3200 for (parent_abs_path, (_, needs_update)) in &mut snapshot.ignores_by_parent_abs_path {
3201 if let Ok(parent_path) = parent_abs_path.strip_prefix(&abs_path) {
3202 if *needs_update {
3203 *needs_update = false;
3204 if snapshot.snapshot.entry_for_path(parent_path).is_some() {
3205 ignores_to_update.push(parent_abs_path.clone());
3206 }
3207 }
3208
3209 let ignore_path = parent_path.join(&*GITIGNORE);
3210 if snapshot.snapshot.entry_for_path(ignore_path).is_none() {
3211 ignores_to_delete.push(parent_abs_path.clone());
3212 }
3213 }
3214 }
3215
3216 for parent_abs_path in ignores_to_delete {
3217 snapshot.ignores_by_parent_abs_path.remove(&parent_abs_path);
3218 self.snapshot
3219 .lock()
3220 .ignores_by_parent_abs_path
3221 .remove(&parent_abs_path);
3222 }
3223
3224 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
3225 ignores_to_update.sort_unstable();
3226 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
3227 while let Some(parent_abs_path) = ignores_to_update.next() {
3228 while ignores_to_update
3229 .peek()
3230 .map_or(false, |p| p.starts_with(&parent_abs_path))
3231 {
3232 ignores_to_update.next().unwrap();
3233 }
3234
3235 let ignore_stack = snapshot.ignore_stack_for_abs_path(&parent_abs_path, true);
3236 smol::block_on(ignore_queue_tx.send(UpdateIgnoreStatusJob {
3237 abs_path: parent_abs_path,
3238 ignore_stack,
3239 ignore_queue: ignore_queue_tx.clone(),
3240 }))
3241 .unwrap();
3242 }
3243 drop(ignore_queue_tx);
3244
3245 self.executor
3246 .scoped(|scope| {
3247 for _ in 0..self.executor.num_cpus() {
3248 scope.spawn(async {
3249 loop {
3250 select_biased! {
3251 // Process any path refresh requests before moving on to process
3252 // the queue of ignore statuses.
3253 request = self.refresh_requests_rx.recv().fuse() => {
3254 let Ok((paths, barrier)) = request else { break };
3255 if !self.process_refresh_request(paths, barrier).await {
3256 return;
3257 }
3258 }
3259
3260 // Recursively process directories whose ignores have changed.
3261 job = ignore_queue_rx.recv().fuse() => {
3262 let Ok(job) = job else { break };
3263 self.update_ignore_status(job, &snapshot).await;
3264 }
3265 }
3266 }
3267 });
3268 }
3269 })
3270 .await;
3271 }
3272
3273 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
3274 let mut ignore_stack = job.ignore_stack;
3275 if let Some((ignore, _)) = snapshot.ignores_by_parent_abs_path.get(&job.abs_path) {
3276 ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
3277 }
3278
3279 let mut entries_by_id_edits = Vec::new();
3280 let mut entries_by_path_edits = Vec::new();
3281 let path = job.abs_path.strip_prefix(&snapshot.abs_path).unwrap();
3282 for mut entry in snapshot.child_entries(path).cloned() {
3283 let was_ignored = entry.is_ignored;
3284 let abs_path = snapshot.abs_path().join(&entry.path);
3285 entry.is_ignored = ignore_stack.is_abs_path_ignored(&abs_path, entry.is_dir());
3286 if entry.is_dir() {
3287 let child_ignore_stack = if entry.is_ignored {
3288 IgnoreStack::all()
3289 } else {
3290 ignore_stack.clone()
3291 };
3292 job.ignore_queue
3293 .send(UpdateIgnoreStatusJob {
3294 abs_path: abs_path.into(),
3295 ignore_stack: child_ignore_stack,
3296 ignore_queue: job.ignore_queue.clone(),
3297 })
3298 .await
3299 .unwrap();
3300 }
3301
3302 if entry.is_ignored != was_ignored {
3303 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
3304 path_entry.scan_id = snapshot.scan_id;
3305 path_entry.is_ignored = entry.is_ignored;
3306 entries_by_id_edits.push(Edit::Insert(path_entry));
3307 entries_by_path_edits.push(Edit::Insert(entry));
3308 }
3309 }
3310
3311 let mut snapshot = self.snapshot.lock();
3312 snapshot.entries_by_path.edit(entries_by_path_edits, &());
3313 snapshot.entries_by_id.edit(entries_by_id_edits, &());
3314 }
3315
3316 fn build_change_set(
3317 &self,
3318 old_snapshot: &Snapshot,
3319 new_snapshot: &Snapshot,
3320 event_paths: &[Arc<Path>],
3321 ) -> HashMap<(Arc<Path>, ProjectEntryId), PathChange> {
3322 use PathChange::{Added, AddedOrUpdated, Removed, Updated};
3323
3324 let mut changes = HashMap::default();
3325 let mut old_paths = old_snapshot.entries_by_path.cursor::<PathKey>();
3326 let mut new_paths = new_snapshot.entries_by_path.cursor::<PathKey>();
3327 let received_before_initialized = !self.finished_initial_scan;
3328
3329 for path in event_paths {
3330 let path = PathKey(path.clone());
3331 old_paths.seek(&path, Bias::Left, &());
3332 new_paths.seek(&path, Bias::Left, &());
3333
3334 loop {
3335 match (old_paths.item(), new_paths.item()) {
3336 (Some(old_entry), Some(new_entry)) => {
3337 if old_entry.path > path.0
3338 && new_entry.path > path.0
3339 && !old_entry.path.starts_with(&path.0)
3340 && !new_entry.path.starts_with(&path.0)
3341 {
3342 break;
3343 }
3344
3345 match Ord::cmp(&old_entry.path, &new_entry.path) {
3346 Ordering::Less => {
3347 changes.insert((old_entry.path.clone(), old_entry.id), Removed);
3348 old_paths.next(&());
3349 }
3350 Ordering::Equal => {
3351 if received_before_initialized {
3352 // If the worktree was not fully initialized when this event was generated,
3353 // we can't know whether this entry was added during the scan or whether
3354 // it was merely updated.
3355 changes.insert(
3356 (new_entry.path.clone(), new_entry.id),
3357 AddedOrUpdated,
3358 );
3359 } else if old_entry.mtime != new_entry.mtime {
3360 changes.insert((new_entry.path.clone(), new_entry.id), Updated);
3361 }
3362 old_paths.next(&());
3363 new_paths.next(&());
3364 }
3365 Ordering::Greater => {
3366 changes.insert((new_entry.path.clone(), new_entry.id), Added);
3367 new_paths.next(&());
3368 }
3369 }
3370 }
3371 (Some(old_entry), None) => {
3372 changes.insert((old_entry.path.clone(), old_entry.id), Removed);
3373 old_paths.next(&());
3374 }
3375 (None, Some(new_entry)) => {
3376 changes.insert((new_entry.path.clone(), new_entry.id), Added);
3377 new_paths.next(&());
3378 }
3379 (None, None) => break,
3380 }
3381 }
3382 }
3383
3384 changes
3385 }
3386
3387 async fn progress_timer(&self, running: bool) {
3388 if !running {
3389 return futures::future::pending().await;
3390 }
3391
3392 #[cfg(any(test, feature = "test-support"))]
3393 if self.fs.is_fake() {
3394 return self.executor.simulate_random_delay().await;
3395 }
3396
3397 smol::Timer::after(Duration::from_millis(100)).await;
3398 }
3399}
3400
3401fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
3402 let mut result = root_char_bag;
3403 result.extend(
3404 path.to_string_lossy()
3405 .chars()
3406 .map(|c| c.to_ascii_lowercase()),
3407 );
3408 result
3409}
3410
3411struct ScanJob {
3412 abs_path: Arc<Path>,
3413 path: Arc<Path>,
3414 ignore_stack: Arc<IgnoreStack>,
3415 scan_queue: Sender<ScanJob>,
3416 ancestor_inodes: TreeSet<u64>,
3417}
3418
3419struct UpdateIgnoreStatusJob {
3420 abs_path: Arc<Path>,
3421 ignore_stack: Arc<IgnoreStack>,
3422 ignore_queue: Sender<UpdateIgnoreStatusJob>,
3423}
3424
3425pub trait WorktreeHandle {
3426 #[cfg(any(test, feature = "test-support"))]
3427 fn flush_fs_events<'a>(
3428 &self,
3429 cx: &'a gpui::TestAppContext,
3430 ) -> futures::future::LocalBoxFuture<'a, ()>;
3431}
3432
3433impl WorktreeHandle for ModelHandle<Worktree> {
3434 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
3435 // occurred before the worktree was constructed. These events can cause the worktree to perfrom
3436 // extra directory scans, and emit extra scan-state notifications.
3437 //
3438 // This function mutates the worktree's directory and waits for those mutations to be picked up,
3439 // to ensure that all redundant FS events have already been processed.
3440 #[cfg(any(test, feature = "test-support"))]
3441 fn flush_fs_events<'a>(
3442 &self,
3443 cx: &'a gpui::TestAppContext,
3444 ) -> futures::future::LocalBoxFuture<'a, ()> {
3445 use smol::future::FutureExt;
3446
3447 let filename = "fs-event-sentinel";
3448 let tree = self.clone();
3449 let (fs, root_path) = self.read_with(cx, |tree, _| {
3450 let tree = tree.as_local().unwrap();
3451 (tree.fs.clone(), tree.abs_path().clone())
3452 });
3453
3454 async move {
3455 fs.create_file(&root_path.join(filename), Default::default())
3456 .await
3457 .unwrap();
3458 tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_some())
3459 .await;
3460
3461 fs.remove_file(&root_path.join(filename), Default::default())
3462 .await
3463 .unwrap();
3464 tree.condition(cx, |tree, _| tree.entry_for_path(filename).is_none())
3465 .await;
3466
3467 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3468 .await;
3469 }
3470 .boxed_local()
3471 }
3472}
3473
3474#[derive(Clone, Debug)]
3475struct TraversalProgress<'a> {
3476 max_path: &'a Path,
3477 count: usize,
3478 visible_count: usize,
3479 file_count: usize,
3480 visible_file_count: usize,
3481}
3482
3483impl<'a> TraversalProgress<'a> {
3484 fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
3485 match (include_ignored, include_dirs) {
3486 (true, true) => self.count,
3487 (true, false) => self.file_count,
3488 (false, true) => self.visible_count,
3489 (false, false) => self.visible_file_count,
3490 }
3491 }
3492}
3493
3494impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
3495 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
3496 self.max_path = summary.max_path.as_ref();
3497 self.count += summary.count;
3498 self.visible_count += summary.visible_count;
3499 self.file_count += summary.file_count;
3500 self.visible_file_count += summary.visible_file_count;
3501 }
3502}
3503
3504impl<'a> Default for TraversalProgress<'a> {
3505 fn default() -> Self {
3506 Self {
3507 max_path: Path::new(""),
3508 count: 0,
3509 visible_count: 0,
3510 file_count: 0,
3511 visible_file_count: 0,
3512 }
3513 }
3514}
3515
3516pub struct Traversal<'a> {
3517 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
3518 include_ignored: bool,
3519 include_dirs: bool,
3520}
3521
3522impl<'a> Traversal<'a> {
3523 pub fn advance(&mut self) -> bool {
3524 self.cursor.seek_forward(
3525 &TraversalTarget::Count {
3526 count: self.end_offset() + 1,
3527 include_dirs: self.include_dirs,
3528 include_ignored: self.include_ignored,
3529 },
3530 Bias::Left,
3531 &(),
3532 )
3533 }
3534
3535 pub fn advance_to_sibling(&mut self) -> bool {
3536 while let Some(entry) = self.cursor.item() {
3537 self.cursor.seek_forward(
3538 &TraversalTarget::PathSuccessor(&entry.path),
3539 Bias::Left,
3540 &(),
3541 );
3542 if let Some(entry) = self.cursor.item() {
3543 if (self.include_dirs || !entry.is_dir())
3544 && (self.include_ignored || !entry.is_ignored)
3545 {
3546 return true;
3547 }
3548 }
3549 }
3550 false
3551 }
3552
3553 pub fn entry(&self) -> Option<&'a Entry> {
3554 self.cursor.item()
3555 }
3556
3557 pub fn start_offset(&self) -> usize {
3558 self.cursor
3559 .start()
3560 .count(self.include_dirs, self.include_ignored)
3561 }
3562
3563 pub fn end_offset(&self) -> usize {
3564 self.cursor
3565 .end(&())
3566 .count(self.include_dirs, self.include_ignored)
3567 }
3568}
3569
3570impl<'a> Iterator for Traversal<'a> {
3571 type Item = &'a Entry;
3572
3573 fn next(&mut self) -> Option<Self::Item> {
3574 if let Some(item) = self.entry() {
3575 self.advance();
3576 Some(item)
3577 } else {
3578 None
3579 }
3580 }
3581}
3582
3583#[derive(Debug)]
3584enum TraversalTarget<'a> {
3585 Path(&'a Path),
3586 PathSuccessor(&'a Path),
3587 Count {
3588 count: usize,
3589 include_ignored: bool,
3590 include_dirs: bool,
3591 },
3592}
3593
3594impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
3595 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
3596 match self {
3597 TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
3598 TraversalTarget::PathSuccessor(path) => {
3599 if !cursor_location.max_path.starts_with(path) {
3600 Ordering::Equal
3601 } else {
3602 Ordering::Greater
3603 }
3604 }
3605 TraversalTarget::Count {
3606 count,
3607 include_dirs,
3608 include_ignored,
3609 } => Ord::cmp(
3610 count,
3611 &cursor_location.count(*include_dirs, *include_ignored),
3612 ),
3613 }
3614 }
3615}
3616
3617struct ChildEntriesIter<'a> {
3618 parent_path: &'a Path,
3619 traversal: Traversal<'a>,
3620}
3621
3622impl<'a> Iterator for ChildEntriesIter<'a> {
3623 type Item = &'a Entry;
3624
3625 fn next(&mut self) -> Option<Self::Item> {
3626 if let Some(item) = self.traversal.entry() {
3627 if item.path.starts_with(&self.parent_path) {
3628 self.traversal.advance_to_sibling();
3629 return Some(item);
3630 }
3631 }
3632 None
3633 }
3634}
3635
3636struct DescendentEntriesIter<'a> {
3637 parent_path: &'a Path,
3638 traversal: Traversal<'a>,
3639}
3640
3641impl<'a> Iterator for DescendentEntriesIter<'a> {
3642 type Item = &'a Entry;
3643
3644 fn next(&mut self) -> Option<Self::Item> {
3645 if let Some(item) = self.traversal.entry() {
3646 if item.path.starts_with(&self.parent_path) {
3647 self.traversal.advance();
3648 return Some(item);
3649 }
3650 }
3651 None
3652 }
3653}
3654
3655impl<'a> From<&'a Entry> for proto::Entry {
3656 fn from(entry: &'a Entry) -> Self {
3657 Self {
3658 id: entry.id.to_proto(),
3659 is_dir: entry.is_dir(),
3660 path: entry.path.to_string_lossy().into(),
3661 inode: entry.inode,
3662 mtime: Some(entry.mtime.into()),
3663 is_symlink: entry.is_symlink,
3664 is_ignored: entry.is_ignored,
3665 }
3666 }
3667}
3668
3669impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
3670 type Error = anyhow::Error;
3671
3672 fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
3673 if let Some(mtime) = entry.mtime {
3674 let kind = if entry.is_dir {
3675 EntryKind::Dir
3676 } else {
3677 let mut char_bag = *root_char_bag;
3678 char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase()));
3679 EntryKind::File(char_bag)
3680 };
3681 let path: Arc<Path> = PathBuf::from(entry.path).into();
3682 Ok(Entry {
3683 id: ProjectEntryId::from_proto(entry.id),
3684 kind,
3685 path,
3686 inode: entry.inode,
3687 mtime: mtime.into(),
3688 is_symlink: entry.is_symlink,
3689 is_ignored: entry.is_ignored,
3690 })
3691 } else {
3692 Err(anyhow!(
3693 "missing mtime in remote worktree entry {:?}",
3694 entry.path
3695 ))
3696 }
3697 }
3698}
3699
3700#[cfg(test)]
3701mod tests {
3702 use super::*;
3703 use fs::{FakeFs, RealFs};
3704 use gpui::{executor::Deterministic, TestAppContext};
3705 use pretty_assertions::assert_eq;
3706 use rand::prelude::*;
3707 use serde_json::json;
3708 use std::{env, fmt::Write};
3709 use util::{http::FakeHttpClient, test::temp_tree};
3710
3711 #[gpui::test]
3712 async fn test_traversal(cx: &mut TestAppContext) {
3713 let fs = FakeFs::new(cx.background());
3714 fs.insert_tree(
3715 "/root",
3716 json!({
3717 ".gitignore": "a/b\n",
3718 "a": {
3719 "b": "",
3720 "c": "",
3721 }
3722 }),
3723 )
3724 .await;
3725
3726 let http_client = FakeHttpClient::with_404_response();
3727 let client = cx.read(|cx| Client::new(http_client, cx));
3728
3729 let tree = Worktree::local(
3730 client,
3731 Path::new("/root"),
3732 true,
3733 fs,
3734 Default::default(),
3735 &mut cx.to_async(),
3736 )
3737 .await
3738 .unwrap();
3739 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3740 .await;
3741
3742 tree.read_with(cx, |tree, _| {
3743 assert_eq!(
3744 tree.entries(false)
3745 .map(|entry| entry.path.as_ref())
3746 .collect::<Vec<_>>(),
3747 vec![
3748 Path::new(""),
3749 Path::new(".gitignore"),
3750 Path::new("a"),
3751 Path::new("a/c"),
3752 ]
3753 );
3754 assert_eq!(
3755 tree.entries(true)
3756 .map(|entry| entry.path.as_ref())
3757 .collect::<Vec<_>>(),
3758 vec![
3759 Path::new(""),
3760 Path::new(".gitignore"),
3761 Path::new("a"),
3762 Path::new("a/b"),
3763 Path::new("a/c"),
3764 ]
3765 );
3766 })
3767 }
3768
3769 #[gpui::test]
3770 async fn test_descendent_entries(cx: &mut TestAppContext) {
3771 let fs = FakeFs::new(cx.background());
3772 fs.insert_tree(
3773 "/root",
3774 json!({
3775 "a": "",
3776 "b": {
3777 "c": {
3778 "d": ""
3779 },
3780 "e": {}
3781 },
3782 "f": "",
3783 "g": {
3784 "h": {}
3785 },
3786 "i": {
3787 "j": {
3788 "k": ""
3789 },
3790 "l": {
3791
3792 }
3793 },
3794 ".gitignore": "i/j\n",
3795 }),
3796 )
3797 .await;
3798
3799 let http_client = FakeHttpClient::with_404_response();
3800 let client = cx.read(|cx| Client::new(http_client, cx));
3801
3802 let tree = Worktree::local(
3803 client,
3804 Path::new("/root"),
3805 true,
3806 fs,
3807 Default::default(),
3808 &mut cx.to_async(),
3809 )
3810 .await
3811 .unwrap();
3812 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3813 .await;
3814
3815 tree.read_with(cx, |tree, _| {
3816 assert_eq!(
3817 tree.descendent_entries(false, false, Path::new("b"))
3818 .map(|entry| entry.path.as_ref())
3819 .collect::<Vec<_>>(),
3820 vec![Path::new("b/c/d"),]
3821 );
3822 assert_eq!(
3823 tree.descendent_entries(true, false, Path::new("b"))
3824 .map(|entry| entry.path.as_ref())
3825 .collect::<Vec<_>>(),
3826 vec![
3827 Path::new("b"),
3828 Path::new("b/c"),
3829 Path::new("b/c/d"),
3830 Path::new("b/e"),
3831 ]
3832 );
3833
3834 assert_eq!(
3835 tree.descendent_entries(false, false, Path::new("g"))
3836 .map(|entry| entry.path.as_ref())
3837 .collect::<Vec<_>>(),
3838 Vec::<PathBuf>::new()
3839 );
3840 assert_eq!(
3841 tree.descendent_entries(true, false, Path::new("g"))
3842 .map(|entry| entry.path.as_ref())
3843 .collect::<Vec<_>>(),
3844 vec![Path::new("g"), Path::new("g/h"),]
3845 );
3846
3847 assert_eq!(
3848 tree.descendent_entries(false, false, Path::new("i"))
3849 .map(|entry| entry.path.as_ref())
3850 .collect::<Vec<_>>(),
3851 Vec::<PathBuf>::new()
3852 );
3853 assert_eq!(
3854 tree.descendent_entries(false, true, Path::new("i"))
3855 .map(|entry| entry.path.as_ref())
3856 .collect::<Vec<_>>(),
3857 vec![Path::new("i/j/k")]
3858 );
3859 assert_eq!(
3860 tree.descendent_entries(true, false, Path::new("i"))
3861 .map(|entry| entry.path.as_ref())
3862 .collect::<Vec<_>>(),
3863 vec![Path::new("i"), Path::new("i/l"),]
3864 );
3865 })
3866 }
3867
3868 #[gpui::test(iterations = 10)]
3869 async fn test_circular_symlinks(executor: Arc<Deterministic>, cx: &mut TestAppContext) {
3870 let fs = FakeFs::new(cx.background());
3871 fs.insert_tree(
3872 "/root",
3873 json!({
3874 "lib": {
3875 "a": {
3876 "a.txt": ""
3877 },
3878 "b": {
3879 "b.txt": ""
3880 }
3881 }
3882 }),
3883 )
3884 .await;
3885 fs.insert_symlink("/root/lib/a/lib", "..".into()).await;
3886 fs.insert_symlink("/root/lib/b/lib", "..".into()).await;
3887
3888 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3889 let tree = Worktree::local(
3890 client,
3891 Path::new("/root"),
3892 true,
3893 fs.clone(),
3894 Default::default(),
3895 &mut cx.to_async(),
3896 )
3897 .await
3898 .unwrap();
3899
3900 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3901 .await;
3902
3903 tree.read_with(cx, |tree, _| {
3904 assert_eq!(
3905 tree.entries(false)
3906 .map(|entry| entry.path.as_ref())
3907 .collect::<Vec<_>>(),
3908 vec![
3909 Path::new(""),
3910 Path::new("lib"),
3911 Path::new("lib/a"),
3912 Path::new("lib/a/a.txt"),
3913 Path::new("lib/a/lib"),
3914 Path::new("lib/b"),
3915 Path::new("lib/b/b.txt"),
3916 Path::new("lib/b/lib"),
3917 ]
3918 );
3919 });
3920
3921 fs.rename(
3922 Path::new("/root/lib/a/lib"),
3923 Path::new("/root/lib/a/lib-2"),
3924 Default::default(),
3925 )
3926 .await
3927 .unwrap();
3928 executor.run_until_parked();
3929 tree.read_with(cx, |tree, _| {
3930 assert_eq!(
3931 tree.entries(false)
3932 .map(|entry| entry.path.as_ref())
3933 .collect::<Vec<_>>(),
3934 vec![
3935 Path::new(""),
3936 Path::new("lib"),
3937 Path::new("lib/a"),
3938 Path::new("lib/a/a.txt"),
3939 Path::new("lib/a/lib-2"),
3940 Path::new("lib/b"),
3941 Path::new("lib/b/b.txt"),
3942 Path::new("lib/b/lib"),
3943 ]
3944 );
3945 });
3946 }
3947
3948 #[gpui::test]
3949 async fn test_rescan_with_gitignore(cx: &mut TestAppContext) {
3950 let parent_dir = temp_tree(json!({
3951 ".gitignore": "ancestor-ignored-file1\nancestor-ignored-file2\n",
3952 "tree": {
3953 ".git": {},
3954 ".gitignore": "ignored-dir\n",
3955 "tracked-dir": {
3956 "tracked-file1": "",
3957 "ancestor-ignored-file1": "",
3958 },
3959 "ignored-dir": {
3960 "ignored-file1": ""
3961 }
3962 }
3963 }));
3964 let dir = parent_dir.path().join("tree");
3965
3966 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
3967
3968 let tree = Worktree::local(
3969 client,
3970 dir.as_path(),
3971 true,
3972 Arc::new(RealFs),
3973 Default::default(),
3974 &mut cx.to_async(),
3975 )
3976 .await
3977 .unwrap();
3978 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3979 .await;
3980 tree.flush_fs_events(cx).await;
3981 cx.read(|cx| {
3982 let tree = tree.read(cx);
3983 assert!(
3984 !tree
3985 .entry_for_path("tracked-dir/tracked-file1")
3986 .unwrap()
3987 .is_ignored
3988 );
3989 assert!(
3990 tree.entry_for_path("tracked-dir/ancestor-ignored-file1")
3991 .unwrap()
3992 .is_ignored
3993 );
3994 assert!(
3995 tree.entry_for_path("ignored-dir/ignored-file1")
3996 .unwrap()
3997 .is_ignored
3998 );
3999 });
4000
4001 std::fs::write(dir.join("tracked-dir/tracked-file2"), "").unwrap();
4002 std::fs::write(dir.join("tracked-dir/ancestor-ignored-file2"), "").unwrap();
4003 std::fs::write(dir.join("ignored-dir/ignored-file2"), "").unwrap();
4004 tree.flush_fs_events(cx).await;
4005 cx.read(|cx| {
4006 let tree = tree.read(cx);
4007 assert!(
4008 !tree
4009 .entry_for_path("tracked-dir/tracked-file2")
4010 .unwrap()
4011 .is_ignored
4012 );
4013 assert!(
4014 tree.entry_for_path("tracked-dir/ancestor-ignored-file2")
4015 .unwrap()
4016 .is_ignored
4017 );
4018 assert!(
4019 tree.entry_for_path("ignored-dir/ignored-file2")
4020 .unwrap()
4021 .is_ignored
4022 );
4023 assert!(tree.entry_for_path(".git").unwrap().is_ignored);
4024 });
4025 }
4026
4027 #[gpui::test]
4028 async fn test_git_repository_for_path(cx: &mut TestAppContext) {
4029 let root = temp_tree(json!({
4030 "c.txt": "",
4031 "dir1": {
4032 ".git": {},
4033 "deps": {
4034 "dep1": {
4035 ".git": {},
4036 "src": {
4037 "a.txt": ""
4038 }
4039 }
4040 },
4041 "src": {
4042 "b.txt": ""
4043 }
4044 },
4045 }));
4046
4047 let http_client = FakeHttpClient::with_404_response();
4048 let client = cx.read(|cx| Client::new(http_client, cx));
4049 let tree = Worktree::local(
4050 client,
4051 root.path(),
4052 true,
4053 Arc::new(RealFs),
4054 Default::default(),
4055 &mut cx.to_async(),
4056 )
4057 .await
4058 .unwrap();
4059
4060 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
4061 .await;
4062 tree.flush_fs_events(cx).await;
4063
4064 tree.read_with(cx, |tree, _cx| {
4065 let tree = tree.as_local().unwrap();
4066
4067 assert!(tree.repo_for("c.txt".as_ref()).is_none());
4068
4069 let entry = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap();
4070 assert_eq!(
4071 entry
4072 .work_directory(tree)
4073 .map(|directory| directory.as_ref().to_owned()),
4074 Some(Path::new("dir1").to_owned())
4075 );
4076
4077 let entry = tree.repo_for("dir1/deps/dep1/src/a.txt".as_ref()).unwrap();
4078 assert_eq!(
4079 entry
4080 .work_directory(tree)
4081 .map(|directory| directory.as_ref().to_owned()),
4082 Some(Path::new("dir1/deps/dep1").to_owned())
4083 );
4084
4085 let entries = tree.files(false, 0);
4086
4087 let paths_with_repos = tree
4088 .entries_with_repos(entries)
4089 .map(|(entry, repo)| {
4090 (
4091 entry.path.as_ref(),
4092 repo.and_then(|repo| {
4093 repo.work_directory(&tree)
4094 .map(|work_directory| work_directory.0.to_path_buf())
4095 }),
4096 )
4097 })
4098 .collect::<Vec<_>>();
4099
4100 assert_eq!(
4101 paths_with_repos,
4102 &[
4103 (Path::new("c.txt"), None),
4104 (
4105 Path::new("dir1/deps/dep1/src/a.txt"),
4106 Some(Path::new("dir1/deps/dep1").into())
4107 ),
4108 (Path::new("dir1/src/b.txt"), Some(Path::new("dir1").into())),
4109 ]
4110 );
4111 });
4112
4113 let repo_update_events = Arc::new(Mutex::new(vec![]));
4114 tree.update(cx, |_, cx| {
4115 let repo_update_events = repo_update_events.clone();
4116 cx.subscribe(&tree, move |_, _, event, _| {
4117 if let Event::UpdatedGitRepositories(update) = event {
4118 repo_update_events.lock().push(update.clone());
4119 }
4120 })
4121 .detach();
4122 });
4123
4124 std::fs::write(root.path().join("dir1/.git/random_new_file"), "hello").unwrap();
4125 tree.flush_fs_events(cx).await;
4126
4127 assert_eq!(
4128 repo_update_events.lock()[0]
4129 .keys()
4130 .cloned()
4131 .collect::<Vec<Arc<Path>>>(),
4132 vec![Path::new("dir1").into()]
4133 );
4134
4135 std::fs::remove_dir_all(root.path().join("dir1/.git")).unwrap();
4136 tree.flush_fs_events(cx).await;
4137
4138 tree.read_with(cx, |tree, _cx| {
4139 let tree = tree.as_local().unwrap();
4140
4141 assert!(tree.repo_for("dir1/src/b.txt".as_ref()).is_none());
4142 });
4143 }
4144
4145 #[gpui::test]
4146 async fn test_git_status(cx: &mut TestAppContext) {
4147 #[track_caller]
4148 fn git_init(path: &Path) -> git2::Repository {
4149 git2::Repository::init(path).expect("Failed to initialize git repository")
4150 }
4151
4152 #[track_caller]
4153 fn git_add(path: &Path, repo: &git2::Repository) {
4154 let mut index = repo.index().expect("Failed to get index");
4155 index.add_path(path).expect("Failed to add a.txt");
4156 index.write().expect("Failed to write index");
4157 }
4158
4159 #[track_caller]
4160 fn git_remove_index(path: &Path, repo: &git2::Repository) {
4161 let mut index = repo.index().expect("Failed to get index");
4162 index.remove_path(path).expect("Failed to add a.txt");
4163 index.write().expect("Failed to write index");
4164 }
4165
4166 #[track_caller]
4167 fn git_commit(msg: &'static str, repo: &git2::Repository) {
4168 use git2::Signature;
4169
4170 let signature = Signature::now("test", "test@zed.dev").unwrap();
4171 let oid = repo.index().unwrap().write_tree().unwrap();
4172 let tree = repo.find_tree(oid).unwrap();
4173 if let Some(head) = repo.head().ok() {
4174 let parent_obj = head.peel(git2::ObjectType::Commit).unwrap();
4175
4176 let parent_commit = parent_obj.as_commit().unwrap();
4177
4178 repo.commit(
4179 Some("HEAD"),
4180 &signature,
4181 &signature,
4182 msg,
4183 &tree,
4184 &[parent_commit],
4185 )
4186 .expect("Failed to commit with parent");
4187 } else {
4188 repo.commit(Some("HEAD"), &signature, &signature, msg, &tree, &[])
4189 .expect("Failed to commit");
4190 }
4191 }
4192
4193 #[track_caller]
4194 fn git_stash(repo: &mut git2::Repository) {
4195 use git2::Signature;
4196
4197 let signature = Signature::now("test", "test@zed.dev").unwrap();
4198 repo.stash_save(&signature, "N/A", None)
4199 .expect("Failed to stash");
4200 }
4201
4202 #[track_caller]
4203 fn git_reset(offset: usize, repo: &git2::Repository) {
4204 let head = repo.head().expect("Couldn't get repo head");
4205 let object = head.peel(git2::ObjectType::Commit).unwrap();
4206 let commit = object.as_commit().unwrap();
4207 let new_head = commit
4208 .parents()
4209 .inspect(|parnet| {
4210 parnet.message();
4211 })
4212 .skip(offset)
4213 .next()
4214 .expect("Not enough history");
4215 repo.reset(&new_head.as_object(), git2::ResetType::Soft, None)
4216 .expect("Could not reset");
4217 }
4218
4219 #[allow(dead_code)]
4220 #[track_caller]
4221 fn git_status(repo: &git2::Repository) -> HashMap<String, git2::Status> {
4222 repo.statuses(None)
4223 .unwrap()
4224 .iter()
4225 .map(|status| (status.path().unwrap().to_string(), status.status()))
4226 .collect()
4227 }
4228
4229 const IGNORE_RULE: &'static str = "**/target";
4230
4231 let root = temp_tree(json!({
4232 "project": {
4233 "a.txt": "a",
4234 "b.txt": "bb",
4235 "c": {
4236 "d": {
4237 "e.txt": "eee"
4238 }
4239 },
4240 "f.txt": "ffff",
4241 "target": {
4242 "build_file": "???"
4243 },
4244 ".gitignore": IGNORE_RULE
4245 },
4246
4247 }));
4248
4249 let http_client = FakeHttpClient::with_404_response();
4250 let client = cx.read(|cx| Client::new(http_client, cx));
4251 let tree = Worktree::local(
4252 client,
4253 root.path(),
4254 true,
4255 Arc::new(RealFs),
4256 Default::default(),
4257 &mut cx.to_async(),
4258 )
4259 .await
4260 .unwrap();
4261
4262 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
4263 .await;
4264
4265 const A_TXT: &'static str = "a.txt";
4266 const B_TXT: &'static str = "b.txt";
4267 const E_TXT: &'static str = "c/d/e.txt";
4268 const F_TXT: &'static str = "f.txt";
4269 const DOTGITIGNORE: &'static str = ".gitignore";
4270 const BUILD_FILE: &'static str = "target/build_file";
4271
4272 let work_dir = root.path().join("project");
4273 let mut repo = git_init(work_dir.as_path());
4274 repo.add_ignore_rule(IGNORE_RULE).unwrap();
4275 git_add(Path::new(A_TXT), &repo);
4276 git_add(Path::new(E_TXT), &repo);
4277 git_add(Path::new(DOTGITIGNORE), &repo);
4278 git_commit("Initial commit", &repo);
4279
4280 std::fs::write(work_dir.join(A_TXT), "aa").unwrap();
4281
4282 tree.flush_fs_events(cx).await;
4283
4284 // Check that the right git state is observed on startup
4285 tree.read_with(cx, |tree, _cx| {
4286 let snapshot = tree.snapshot();
4287 assert_eq!(snapshot.repository_entries.iter().count(), 1);
4288 let (dir, repo) = snapshot.repository_entries.iter().next().unwrap();
4289 assert_eq!(dir.0.as_ref(), Path::new("project"));
4290
4291 assert_eq!(repo.statuses.iter().count(), 3);
4292 assert_eq!(
4293 repo.statuses.get(&Path::new(A_TXT).into()),
4294 Some(&GitFileStatus::Modified)
4295 );
4296 assert_eq!(
4297 repo.statuses.get(&Path::new(B_TXT).into()),
4298 Some(&GitFileStatus::Added)
4299 );
4300 assert_eq!(
4301 repo.statuses.get(&Path::new(F_TXT).into()),
4302 Some(&GitFileStatus::Added)
4303 );
4304 });
4305
4306 git_add(Path::new(A_TXT), &repo);
4307 git_add(Path::new(B_TXT), &repo);
4308 git_commit("Committing modified and added", &repo);
4309 tree.flush_fs_events(cx).await;
4310
4311 // Check that repo only changes are tracked
4312 tree.read_with(cx, |tree, _cx| {
4313 let snapshot = tree.snapshot();
4314 let (_, repo) = snapshot.repository_entries.iter().next().unwrap();
4315
4316 assert_eq!(repo.statuses.iter().count(), 1);
4317 assert_eq!(
4318 repo.statuses.get(&Path::new(F_TXT).into()),
4319 Some(&GitFileStatus::Added)
4320 );
4321 });
4322
4323 git_reset(0, &repo);
4324 git_remove_index(Path::new(B_TXT), &repo);
4325 git_stash(&mut repo);
4326 std::fs::write(work_dir.join(E_TXT), "eeee").unwrap();
4327 std::fs::write(work_dir.join(BUILD_FILE), "this should be ignored").unwrap();
4328 tree.flush_fs_events(cx).await;
4329
4330 // Check that more complex repo changes are tracked
4331 tree.read_with(cx, |tree, _cx| {
4332 let snapshot = tree.snapshot();
4333 let (_, repo) = snapshot.repository_entries.iter().next().unwrap();
4334
4335 assert_eq!(repo.statuses.iter().count(), 3);
4336 assert_eq!(repo.statuses.get(&Path::new(A_TXT).into()), None);
4337 assert_eq!(
4338 repo.statuses.get(&Path::new(B_TXT).into()),
4339 Some(&GitFileStatus::Added)
4340 );
4341 assert_eq!(
4342 repo.statuses.get(&Path::new(E_TXT).into()),
4343 Some(&GitFileStatus::Modified)
4344 );
4345 assert_eq!(
4346 repo.statuses.get(&Path::new(F_TXT).into()),
4347 Some(&GitFileStatus::Added)
4348 );
4349 });
4350
4351 std::fs::remove_file(work_dir.join(B_TXT)).unwrap();
4352 std::fs::remove_dir_all(work_dir.join("c")).unwrap();
4353 std::fs::write(
4354 work_dir.join(DOTGITIGNORE),
4355 [IGNORE_RULE, "f.txt"].join("\n"),
4356 )
4357 .unwrap();
4358
4359 git_add(Path::new(DOTGITIGNORE), &repo);
4360 git_commit("Committing modified git ignore", &repo);
4361
4362 tree.flush_fs_events(cx).await;
4363
4364 // Check that non-repo behavior is tracked
4365 tree.read_with(cx, |tree, _cx| {
4366 let snapshot = tree.snapshot();
4367 let (_, repo) = snapshot.repository_entries.iter().next().unwrap();
4368
4369 assert_eq!(repo.statuses.iter().count(), 0);
4370 });
4371
4372 let mut renamed_dir_name = "first_directory/second_directory";
4373 const RENAMED_FILE: &'static str = "rf.txt";
4374
4375 std::fs::create_dir_all(work_dir.join(renamed_dir_name)).unwrap();
4376 std::fs::write(
4377 work_dir.join(renamed_dir_name).join(RENAMED_FILE),
4378 "new-contents",
4379 )
4380 .unwrap();
4381
4382 tree.flush_fs_events(cx).await;
4383
4384 tree.read_with(cx, |tree, _cx| {
4385 let snapshot = tree.snapshot();
4386 let (_, repo) = snapshot.repository_entries.iter().next().unwrap();
4387
4388 assert_eq!(repo.statuses.iter().count(), 1);
4389 assert_eq!(
4390 repo.statuses
4391 .get(&Path::new(renamed_dir_name).join(RENAMED_FILE).into()),
4392 Some(&GitFileStatus::Added)
4393 );
4394 });
4395
4396 renamed_dir_name = "new_first_directory/second_directory";
4397
4398 std::fs::rename(
4399 work_dir.join("first_directory"),
4400 work_dir.join("new_first_directory"),
4401 )
4402 .unwrap();
4403
4404 tree.flush_fs_events(cx).await;
4405
4406 tree.read_with(cx, |tree, _cx| {
4407 let snapshot = tree.snapshot();
4408 let (_, repo) = snapshot.repository_entries.iter().next().unwrap();
4409
4410 assert_eq!(repo.statuses.iter().count(), 1);
4411 assert_eq!(
4412 repo.statuses
4413 .get(&Path::new(renamed_dir_name).join(RENAMED_FILE).into()),
4414 Some(&GitFileStatus::Added)
4415 );
4416 });
4417 }
4418
4419 #[gpui::test]
4420 async fn test_write_file(cx: &mut TestAppContext) {
4421 let dir = temp_tree(json!({
4422 ".git": {},
4423 ".gitignore": "ignored-dir\n",
4424 "tracked-dir": {},
4425 "ignored-dir": {}
4426 }));
4427
4428 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
4429
4430 let tree = Worktree::local(
4431 client,
4432 dir.path(),
4433 true,
4434 Arc::new(RealFs),
4435 Default::default(),
4436 &mut cx.to_async(),
4437 )
4438 .await
4439 .unwrap();
4440 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
4441 .await;
4442 tree.flush_fs_events(cx).await;
4443
4444 tree.update(cx, |tree, cx| {
4445 tree.as_local().unwrap().write_file(
4446 Path::new("tracked-dir/file.txt"),
4447 "hello".into(),
4448 Default::default(),
4449 cx,
4450 )
4451 })
4452 .await
4453 .unwrap();
4454 tree.update(cx, |tree, cx| {
4455 tree.as_local().unwrap().write_file(
4456 Path::new("ignored-dir/file.txt"),
4457 "world".into(),
4458 Default::default(),
4459 cx,
4460 )
4461 })
4462 .await
4463 .unwrap();
4464
4465 tree.read_with(cx, |tree, _| {
4466 let tracked = tree.entry_for_path("tracked-dir/file.txt").unwrap();
4467 let ignored = tree.entry_for_path("ignored-dir/file.txt").unwrap();
4468 assert!(!tracked.is_ignored);
4469 assert!(ignored.is_ignored);
4470 });
4471 }
4472
4473 #[gpui::test(iterations = 30)]
4474 async fn test_create_directory_during_initial_scan(cx: &mut TestAppContext) {
4475 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
4476
4477 let fs = FakeFs::new(cx.background());
4478 fs.insert_tree(
4479 "/root",
4480 json!({
4481 "b": {},
4482 "c": {},
4483 "d": {},
4484 }),
4485 )
4486 .await;
4487
4488 let tree = Worktree::local(
4489 client,
4490 "/root".as_ref(),
4491 true,
4492 fs,
4493 Default::default(),
4494 &mut cx.to_async(),
4495 )
4496 .await
4497 .unwrap();
4498
4499 let mut snapshot1 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
4500
4501 let entry = tree
4502 .update(cx, |tree, cx| {
4503 tree.as_local_mut()
4504 .unwrap()
4505 .create_entry("a/e".as_ref(), true, cx)
4506 })
4507 .await
4508 .unwrap();
4509 assert!(entry.is_dir());
4510
4511 cx.foreground().run_until_parked();
4512 tree.read_with(cx, |tree, _| {
4513 assert_eq!(tree.entry_for_path("a/e").unwrap().kind, EntryKind::Dir);
4514 });
4515
4516 let snapshot2 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
4517 let update = snapshot2.build_update(&snapshot1, 0, 0, true);
4518 snapshot1.apply_remote_update(update).unwrap();
4519 assert_eq!(snapshot1.to_vec(true), snapshot2.to_vec(true),);
4520 }
4521
4522 #[gpui::test(iterations = 100)]
4523 async fn test_random_worktree_operations_during_initial_scan(
4524 cx: &mut TestAppContext,
4525 mut rng: StdRng,
4526 ) {
4527 let operations = env::var("OPERATIONS")
4528 .map(|o| o.parse().unwrap())
4529 .unwrap_or(5);
4530 let initial_entries = env::var("INITIAL_ENTRIES")
4531 .map(|o| o.parse().unwrap())
4532 .unwrap_or(20);
4533
4534 let root_dir = Path::new("/test");
4535 let fs = FakeFs::new(cx.background()) as Arc<dyn Fs>;
4536 fs.as_fake().insert_tree(root_dir, json!({})).await;
4537 for _ in 0..initial_entries {
4538 randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
4539 }
4540 log::info!("generated initial tree");
4541
4542 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
4543 let worktree = Worktree::local(
4544 client.clone(),
4545 root_dir,
4546 true,
4547 fs.clone(),
4548 Default::default(),
4549 &mut cx.to_async(),
4550 )
4551 .await
4552 .unwrap();
4553
4554 let mut snapshot = worktree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
4555
4556 for _ in 0..operations {
4557 worktree
4558 .update(cx, |worktree, cx| {
4559 randomly_mutate_worktree(worktree, &mut rng, cx)
4560 })
4561 .await
4562 .log_err();
4563 worktree.read_with(cx, |tree, _| {
4564 tree.as_local().unwrap().snapshot.check_invariants()
4565 });
4566
4567 if rng.gen_bool(0.6) {
4568 let new_snapshot =
4569 worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4570 let update = new_snapshot.build_update(&snapshot, 0, 0, true);
4571 snapshot.apply_remote_update(update.clone()).unwrap();
4572 assert_eq!(
4573 snapshot.to_vec(true),
4574 new_snapshot.to_vec(true),
4575 "incorrect snapshot after update {:?}",
4576 update
4577 );
4578 }
4579 }
4580
4581 worktree
4582 .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
4583 .await;
4584 worktree.read_with(cx, |tree, _| {
4585 tree.as_local().unwrap().snapshot.check_invariants()
4586 });
4587
4588 let new_snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4589 let update = new_snapshot.build_update(&snapshot, 0, 0, true);
4590 snapshot.apply_remote_update(update.clone()).unwrap();
4591 assert_eq!(
4592 snapshot.to_vec(true),
4593 new_snapshot.to_vec(true),
4594 "incorrect snapshot after update {:?}",
4595 update
4596 );
4597 }
4598
4599 #[gpui::test(iterations = 100)]
4600 async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng) {
4601 let operations = env::var("OPERATIONS")
4602 .map(|o| o.parse().unwrap())
4603 .unwrap_or(40);
4604 let initial_entries = env::var("INITIAL_ENTRIES")
4605 .map(|o| o.parse().unwrap())
4606 .unwrap_or(20);
4607
4608 let root_dir = Path::new("/test");
4609 let fs = FakeFs::new(cx.background()) as Arc<dyn Fs>;
4610 fs.as_fake().insert_tree(root_dir, json!({})).await;
4611 for _ in 0..initial_entries {
4612 randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
4613 }
4614 log::info!("generated initial tree");
4615
4616 let client = cx.read(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
4617 let worktree = Worktree::local(
4618 client.clone(),
4619 root_dir,
4620 true,
4621 fs.clone(),
4622 Default::default(),
4623 &mut cx.to_async(),
4624 )
4625 .await
4626 .unwrap();
4627
4628 worktree
4629 .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
4630 .await;
4631
4632 // After the initial scan is complete, the `UpdatedEntries` event can
4633 // be used to follow along with all changes to the worktree's snapshot.
4634 worktree.update(cx, |tree, cx| {
4635 let mut paths = tree
4636 .as_local()
4637 .unwrap()
4638 .paths()
4639 .cloned()
4640 .collect::<Vec<_>>();
4641
4642 cx.subscribe(&worktree, move |tree, _, event, _| {
4643 if let Event::UpdatedEntries(changes) = event {
4644 for ((path, _), change_type) in changes.iter() {
4645 let path = path.clone();
4646 let ix = match paths.binary_search(&path) {
4647 Ok(ix) | Err(ix) => ix,
4648 };
4649 match change_type {
4650 PathChange::Added => {
4651 assert_ne!(paths.get(ix), Some(&path));
4652 paths.insert(ix, path);
4653 }
4654
4655 PathChange::Removed => {
4656 assert_eq!(paths.get(ix), Some(&path));
4657 paths.remove(ix);
4658 }
4659
4660 PathChange::Updated => {
4661 assert_eq!(paths.get(ix), Some(&path));
4662 }
4663
4664 PathChange::AddedOrUpdated => {
4665 if paths[ix] != path {
4666 paths.insert(ix, path);
4667 }
4668 }
4669 }
4670 }
4671
4672 let new_paths = tree.paths().cloned().collect::<Vec<_>>();
4673 assert_eq!(paths, new_paths, "incorrect changes: {:?}", changes);
4674 }
4675 })
4676 .detach();
4677 });
4678
4679 fs.as_fake().pause_events();
4680 let mut snapshots = Vec::new();
4681 let mut mutations_len = operations;
4682 while mutations_len > 1 {
4683 if rng.gen_bool(0.2) {
4684 worktree
4685 .update(cx, |worktree, cx| {
4686 randomly_mutate_worktree(worktree, &mut rng, cx)
4687 })
4688 .await
4689 .log_err();
4690 } else {
4691 randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
4692 }
4693
4694 let buffered_event_count = fs.as_fake().buffered_event_count();
4695 if buffered_event_count > 0 && rng.gen_bool(0.3) {
4696 let len = rng.gen_range(0..=buffered_event_count);
4697 log::info!("flushing {} events", len);
4698 fs.as_fake().flush_events(len);
4699 } else {
4700 randomly_mutate_fs(&fs, root_dir, 0.6, &mut rng).await;
4701 mutations_len -= 1;
4702 }
4703
4704 cx.foreground().run_until_parked();
4705 if rng.gen_bool(0.2) {
4706 log::info!("storing snapshot {}", snapshots.len());
4707 let snapshot =
4708 worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4709 snapshots.push(snapshot);
4710 }
4711 }
4712
4713 log::info!("quiescing");
4714 fs.as_fake().flush_events(usize::MAX);
4715 cx.foreground().run_until_parked();
4716 let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4717 snapshot.check_invariants();
4718
4719 {
4720 let new_worktree = Worktree::local(
4721 client.clone(),
4722 root_dir,
4723 true,
4724 fs.clone(),
4725 Default::default(),
4726 &mut cx.to_async(),
4727 )
4728 .await
4729 .unwrap();
4730 new_worktree
4731 .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
4732 .await;
4733 let new_snapshot =
4734 new_worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
4735 assert_eq!(snapshot.to_vec(true), new_snapshot.to_vec(true));
4736 }
4737
4738 for (i, mut prev_snapshot) in snapshots.into_iter().enumerate() {
4739 let include_ignored = rng.gen::<bool>();
4740 if !include_ignored {
4741 let mut entries_by_path_edits = Vec::new();
4742 let mut entries_by_id_edits = Vec::new();
4743 for entry in prev_snapshot
4744 .entries_by_id
4745 .cursor::<()>()
4746 .filter(|e| e.is_ignored)
4747 {
4748 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
4749 entries_by_id_edits.push(Edit::Remove(entry.id));
4750 }
4751
4752 prev_snapshot
4753 .entries_by_path
4754 .edit(entries_by_path_edits, &());
4755 prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
4756 }
4757
4758 let update = snapshot.build_update(&prev_snapshot, 0, 0, include_ignored);
4759 prev_snapshot.apply_remote_update(update.clone()).unwrap();
4760 assert_eq!(
4761 prev_snapshot.to_vec(include_ignored),
4762 snapshot.to_vec(include_ignored),
4763 "wrong update for snapshot {i}. update: {:?}",
4764 update
4765 );
4766 }
4767 }
4768
4769 fn randomly_mutate_worktree(
4770 worktree: &mut Worktree,
4771 rng: &mut impl Rng,
4772 cx: &mut ModelContext<Worktree>,
4773 ) -> Task<Result<()>> {
4774 log::info!("mutating worktree");
4775 let worktree = worktree.as_local_mut().unwrap();
4776 let snapshot = worktree.snapshot();
4777 let entry = snapshot.entries(false).choose(rng).unwrap();
4778
4779 match rng.gen_range(0_u32..100) {
4780 0..=33 if entry.path.as_ref() != Path::new("") => {
4781 log::info!("deleting entry {:?} ({})", entry.path, entry.id.0);
4782 worktree.delete_entry(entry.id, cx).unwrap()
4783 }
4784 ..=66 if entry.path.as_ref() != Path::new("") => {
4785 let other_entry = snapshot.entries(false).choose(rng).unwrap();
4786 let new_parent_path = if other_entry.is_dir() {
4787 other_entry.path.clone()
4788 } else {
4789 other_entry.path.parent().unwrap().into()
4790 };
4791 let mut new_path = new_parent_path.join(gen_name(rng));
4792 if new_path.starts_with(&entry.path) {
4793 new_path = gen_name(rng).into();
4794 }
4795
4796 log::info!(
4797 "renaming entry {:?} ({}) to {:?}",
4798 entry.path,
4799 entry.id.0,
4800 new_path
4801 );
4802 let task = worktree.rename_entry(entry.id, new_path, cx).unwrap();
4803 cx.foreground().spawn(async move {
4804 task.await?;
4805 Ok(())
4806 })
4807 }
4808 _ => {
4809 let task = if entry.is_dir() {
4810 let child_path = entry.path.join(gen_name(rng));
4811 let is_dir = rng.gen_bool(0.3);
4812 log::info!(
4813 "creating {} at {:?}",
4814 if is_dir { "dir" } else { "file" },
4815 child_path,
4816 );
4817 worktree.create_entry(child_path, is_dir, cx)
4818 } else {
4819 log::info!("overwriting file {:?} ({})", entry.path, entry.id.0);
4820 worktree.write_file(entry.path.clone(), "".into(), Default::default(), cx)
4821 };
4822 cx.foreground().spawn(async move {
4823 task.await?;
4824 Ok(())
4825 })
4826 }
4827 }
4828 }
4829
4830 async fn randomly_mutate_fs(
4831 fs: &Arc<dyn Fs>,
4832 root_path: &Path,
4833 insertion_probability: f64,
4834 rng: &mut impl Rng,
4835 ) {
4836 log::info!("mutating fs");
4837 let mut files = Vec::new();
4838 let mut dirs = Vec::new();
4839 for path in fs.as_fake().paths() {
4840 if path.starts_with(root_path) {
4841 if fs.is_file(&path).await {
4842 files.push(path);
4843 } else {
4844 dirs.push(path);
4845 }
4846 }
4847 }
4848
4849 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
4850 let path = dirs.choose(rng).unwrap();
4851 let new_path = path.join(gen_name(rng));
4852
4853 if rng.gen() {
4854 log::info!(
4855 "creating dir {:?}",
4856 new_path.strip_prefix(root_path).unwrap()
4857 );
4858 fs.create_dir(&new_path).await.unwrap();
4859 } else {
4860 log::info!(
4861 "creating file {:?}",
4862 new_path.strip_prefix(root_path).unwrap()
4863 );
4864 fs.create_file(&new_path, Default::default()).await.unwrap();
4865 }
4866 } else if rng.gen_bool(0.05) {
4867 let ignore_dir_path = dirs.choose(rng).unwrap();
4868 let ignore_path = ignore_dir_path.join(&*GITIGNORE);
4869
4870 let subdirs = dirs
4871 .iter()
4872 .filter(|d| d.starts_with(&ignore_dir_path))
4873 .cloned()
4874 .collect::<Vec<_>>();
4875 let subfiles = files
4876 .iter()
4877 .filter(|d| d.starts_with(&ignore_dir_path))
4878 .cloned()
4879 .collect::<Vec<_>>();
4880 let files_to_ignore = {
4881 let len = rng.gen_range(0..=subfiles.len());
4882 subfiles.choose_multiple(rng, len)
4883 };
4884 let dirs_to_ignore = {
4885 let len = rng.gen_range(0..subdirs.len());
4886 subdirs.choose_multiple(rng, len)
4887 };
4888
4889 let mut ignore_contents = String::new();
4890 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
4891 writeln!(
4892 ignore_contents,
4893 "{}",
4894 path_to_ignore
4895 .strip_prefix(&ignore_dir_path)
4896 .unwrap()
4897 .to_str()
4898 .unwrap()
4899 )
4900 .unwrap();
4901 }
4902 log::info!(
4903 "creating gitignore {:?} with contents:\n{}",
4904 ignore_path.strip_prefix(&root_path).unwrap(),
4905 ignore_contents
4906 );
4907 fs.save(
4908 &ignore_path,
4909 &ignore_contents.as_str().into(),
4910 Default::default(),
4911 )
4912 .await
4913 .unwrap();
4914 } else {
4915 let old_path = {
4916 let file_path = files.choose(rng);
4917 let dir_path = dirs[1..].choose(rng);
4918 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
4919 };
4920
4921 let is_rename = rng.gen();
4922 if is_rename {
4923 let new_path_parent = dirs
4924 .iter()
4925 .filter(|d| !d.starts_with(old_path))
4926 .choose(rng)
4927 .unwrap();
4928
4929 let overwrite_existing_dir =
4930 !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
4931 let new_path = if overwrite_existing_dir {
4932 fs.remove_dir(
4933 &new_path_parent,
4934 RemoveOptions {
4935 recursive: true,
4936 ignore_if_not_exists: true,
4937 },
4938 )
4939 .await
4940 .unwrap();
4941 new_path_parent.to_path_buf()
4942 } else {
4943 new_path_parent.join(gen_name(rng))
4944 };
4945
4946 log::info!(
4947 "renaming {:?} to {}{:?}",
4948 old_path.strip_prefix(&root_path).unwrap(),
4949 if overwrite_existing_dir {
4950 "overwrite "
4951 } else {
4952 ""
4953 },
4954 new_path.strip_prefix(&root_path).unwrap()
4955 );
4956 fs.rename(
4957 &old_path,
4958 &new_path,
4959 fs::RenameOptions {
4960 overwrite: true,
4961 ignore_if_exists: true,
4962 },
4963 )
4964 .await
4965 .unwrap();
4966 } else if fs.is_file(&old_path).await {
4967 log::info!(
4968 "deleting file {:?}",
4969 old_path.strip_prefix(&root_path).unwrap()
4970 );
4971 fs.remove_file(old_path, Default::default()).await.unwrap();
4972 } else {
4973 log::info!(
4974 "deleting dir {:?}",
4975 old_path.strip_prefix(&root_path).unwrap()
4976 );
4977 fs.remove_dir(
4978 &old_path,
4979 RemoveOptions {
4980 recursive: true,
4981 ignore_if_not_exists: true,
4982 },
4983 )
4984 .await
4985 .unwrap();
4986 }
4987 }
4988 }
4989
4990 fn gen_name(rng: &mut impl Rng) -> String {
4991 (0..6)
4992 .map(|_| rng.sample(rand::distributions::Alphanumeric))
4993 .map(char::from)
4994 .collect()
4995 }
4996
4997 impl LocalSnapshot {
4998 fn check_invariants(&self) {
4999 assert_eq!(
5000 self.entries_by_path
5001 .cursor::<()>()
5002 .map(|e| (&e.path, e.id))
5003 .collect::<Vec<_>>(),
5004 self.entries_by_id
5005 .cursor::<()>()
5006 .map(|e| (&e.path, e.id))
5007 .collect::<collections::BTreeSet<_>>()
5008 .into_iter()
5009 .collect::<Vec<_>>(),
5010 "entries_by_path and entries_by_id are inconsistent"
5011 );
5012
5013 let mut files = self.files(true, 0);
5014 let mut visible_files = self.files(false, 0);
5015 for entry in self.entries_by_path.cursor::<()>() {
5016 if entry.is_file() {
5017 assert_eq!(files.next().unwrap().inode, entry.inode);
5018 if !entry.is_ignored {
5019 assert_eq!(visible_files.next().unwrap().inode, entry.inode);
5020 }
5021 }
5022 }
5023
5024 assert!(files.next().is_none());
5025 assert!(visible_files.next().is_none());
5026
5027 let mut bfs_paths = Vec::new();
5028 let mut stack = vec![Path::new("")];
5029 while let Some(path) = stack.pop() {
5030 bfs_paths.push(path);
5031 let ix = stack.len();
5032 for child_entry in self.child_entries(path) {
5033 stack.insert(ix, &child_entry.path);
5034 }
5035 }
5036
5037 let dfs_paths_via_iter = self
5038 .entries_by_path
5039 .cursor::<()>()
5040 .map(|e| e.path.as_ref())
5041 .collect::<Vec<_>>();
5042 assert_eq!(bfs_paths, dfs_paths_via_iter);
5043
5044 let dfs_paths_via_traversal = self
5045 .entries(true)
5046 .map(|e| e.path.as_ref())
5047 .collect::<Vec<_>>();
5048 assert_eq!(dfs_paths_via_traversal, dfs_paths_via_iter);
5049
5050 for ignore_parent_abs_path in self.ignores_by_parent_abs_path.keys() {
5051 let ignore_parent_path =
5052 ignore_parent_abs_path.strip_prefix(&self.abs_path).unwrap();
5053 assert!(self.entry_for_path(&ignore_parent_path).is_some());
5054 assert!(self
5055 .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
5056 .is_some());
5057 }
5058 }
5059
5060 fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
5061 let mut paths = Vec::new();
5062 for entry in self.entries_by_path.cursor::<()>() {
5063 if include_ignored || !entry.is_ignored {
5064 paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
5065 }
5066 }
5067 paths.sort_by(|a, b| a.0.cmp(b.0));
5068 paths
5069 }
5070 }
5071}