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