worktree_store.rs

   1use std::{
   2    io::{BufRead, BufReader},
   3    path::{Path, PathBuf},
   4    pin::pin,
   5    sync::{atomic::AtomicUsize, Arc},
   6};
   7
   8use anyhow::{anyhow, Context as _, Result};
   9use collections::{HashMap, HashSet};
  10use fs::Fs;
  11use futures::{
  12    future::{BoxFuture, Shared},
  13    FutureExt, SinkExt,
  14};
  15use gpui::{App, AsyncApp, Context, Entity, EntityId, EventEmitter, Task, WeakEntity};
  16use postage::oneshot;
  17use rpc::{
  18    proto::{self, SSH_PROJECT_ID},
  19    AnyProtoClient, ErrorExt, TypedEnvelope,
  20};
  21use smol::{
  22    channel::{Receiver, Sender},
  23    stream::StreamExt,
  24};
  25use text::ReplicaId;
  26use util::{paths::SanitizedPath, ResultExt};
  27use worktree::{Entry, ProjectEntryId, UpdatedEntriesSet, Worktree, WorktreeId, WorktreeSettings};
  28
  29use crate::{search::SearchQuery, ProjectPath};
  30
  31struct MatchingEntry {
  32    worktree_path: Arc<Path>,
  33    path: ProjectPath,
  34    respond: oneshot::Sender<ProjectPath>,
  35}
  36
  37enum WorktreeStoreState {
  38    Local {
  39        fs: Arc<dyn Fs>,
  40    },
  41    Remote {
  42        upstream_client: AnyProtoClient,
  43        upstream_project_id: u64,
  44    },
  45}
  46
  47pub struct WorktreeStore {
  48    next_entry_id: Arc<AtomicUsize>,
  49    downstream_client: Option<(AnyProtoClient, u64)>,
  50    retain_worktrees: bool,
  51    worktrees: Vec<WorktreeHandle>,
  52    worktrees_reordered: bool,
  53    #[allow(clippy::type_complexity)]
  54    loading_worktrees:
  55        HashMap<SanitizedPath, Shared<Task<Result<Entity<Worktree>, Arc<anyhow::Error>>>>>,
  56    state: WorktreeStoreState,
  57}
  58
  59pub enum WorktreeStoreEvent {
  60    WorktreeAdded(Entity<Worktree>),
  61    WorktreeRemoved(EntityId, WorktreeId),
  62    WorktreeReleased(EntityId, WorktreeId),
  63    WorktreeOrderChanged,
  64    WorktreeUpdateSent(Entity<Worktree>),
  65    WorktreeUpdatedEntries(WorktreeId, UpdatedEntriesSet),
  66    WorktreeUpdatedGitRepositories(WorktreeId),
  67    WorktreeDeletedEntry(WorktreeId, ProjectEntryId),
  68}
  69
  70impl EventEmitter<WorktreeStoreEvent> for WorktreeStore {}
  71
  72impl WorktreeStore {
  73    pub fn init(client: &AnyProtoClient) {
  74        client.add_model_request_handler(Self::handle_create_project_entry);
  75        client.add_model_request_handler(Self::handle_copy_project_entry);
  76        client.add_model_request_handler(Self::handle_delete_project_entry);
  77        client.add_model_request_handler(Self::handle_expand_project_entry);
  78        client.add_model_request_handler(Self::handle_git_branches);
  79        client.add_model_request_handler(Self::handle_update_branch);
  80    }
  81
  82    pub fn local(retain_worktrees: bool, fs: Arc<dyn Fs>) -> Self {
  83        Self {
  84            next_entry_id: Default::default(),
  85            loading_worktrees: Default::default(),
  86            downstream_client: None,
  87            worktrees: Vec::new(),
  88            worktrees_reordered: false,
  89            retain_worktrees,
  90            state: WorktreeStoreState::Local { fs },
  91        }
  92    }
  93
  94    pub fn remote(
  95        retain_worktrees: bool,
  96        upstream_client: AnyProtoClient,
  97        upstream_project_id: u64,
  98    ) -> Self {
  99        Self {
 100            next_entry_id: Default::default(),
 101            loading_worktrees: Default::default(),
 102            downstream_client: None,
 103            worktrees: Vec::new(),
 104            worktrees_reordered: false,
 105            retain_worktrees,
 106            state: WorktreeStoreState::Remote {
 107                upstream_client,
 108                upstream_project_id,
 109            },
 110        }
 111    }
 112
 113    /// Iterates through all worktrees, including ones that don't appear in the project panel
 114    pub fn worktrees(&self) -> impl '_ + DoubleEndedIterator<Item = Entity<Worktree>> {
 115        self.worktrees
 116            .iter()
 117            .filter_map(move |worktree| worktree.upgrade())
 118    }
 119
 120    /// Iterates through all user-visible worktrees, the ones that appear in the project panel.
 121    pub fn visible_worktrees<'a>(
 122        &'a self,
 123        cx: &'a App,
 124    ) -> impl 'a + DoubleEndedIterator<Item = Entity<Worktree>> {
 125        self.worktrees()
 126            .filter(|worktree| worktree.read(cx).is_visible())
 127    }
 128
 129    pub fn worktree_for_id(&self, id: WorktreeId, cx: &App) -> Option<Entity<Worktree>> {
 130        self.worktrees()
 131            .find(|worktree| worktree.read(cx).id() == id)
 132    }
 133
 134    pub fn current_branch(&self, repository: ProjectPath, cx: &App) -> Option<Arc<str>> {
 135        self.worktree_for_id(repository.worktree_id, cx)?
 136            .read(cx)
 137            .git_entry(repository.path)?
 138            .branch()
 139    }
 140
 141    pub fn worktree_for_entry(
 142        &self,
 143        entry_id: ProjectEntryId,
 144        cx: &App,
 145    ) -> Option<Entity<Worktree>> {
 146        self.worktrees()
 147            .find(|worktree| worktree.read(cx).contains_entry(entry_id))
 148    }
 149
 150    pub fn find_worktree(
 151        &self,
 152        abs_path: impl Into<SanitizedPath>,
 153        cx: &App,
 154    ) -> Option<(Entity<Worktree>, PathBuf)> {
 155        let abs_path: SanitizedPath = abs_path.into();
 156        for tree in self.worktrees() {
 157            if let Ok(relative_path) = abs_path.as_path().strip_prefix(tree.read(cx).abs_path()) {
 158                return Some((tree.clone(), relative_path.into()));
 159            }
 160        }
 161        None
 162    }
 163
 164    pub fn find_or_create_worktree(
 165        &mut self,
 166        abs_path: impl AsRef<Path>,
 167        visible: bool,
 168        cx: &mut Context<Self>,
 169    ) -> Task<Result<(Entity<Worktree>, PathBuf)>> {
 170        let abs_path = abs_path.as_ref();
 171        if let Some((tree, relative_path)) = self.find_worktree(abs_path, cx) {
 172            Task::ready(Ok((tree, relative_path)))
 173        } else {
 174            let worktree = self.create_worktree(abs_path, visible, cx);
 175            cx.background_executor()
 176                .spawn(async move { Ok((worktree.await?, PathBuf::new())) })
 177        }
 178    }
 179
 180    pub fn entry_for_id<'a>(&'a self, entry_id: ProjectEntryId, cx: &'a App) -> Option<&'a Entry> {
 181        self.worktrees()
 182            .find_map(|worktree| worktree.read(cx).entry_for_id(entry_id))
 183    }
 184
 185    pub fn worktree_and_entry_for_id<'a>(
 186        &'a self,
 187        entry_id: ProjectEntryId,
 188        cx: &'a App,
 189    ) -> Option<(Entity<Worktree>, &'a Entry)> {
 190        self.worktrees().find_map(|worktree| {
 191            worktree
 192                .read(cx)
 193                .entry_for_id(entry_id)
 194                .map(|e| (worktree.clone(), e))
 195        })
 196    }
 197
 198    pub fn entry_for_path(&self, path: &ProjectPath, cx: &App) -> Option<Entry> {
 199        self.worktree_for_id(path.worktree_id, cx)?
 200            .read(cx)
 201            .entry_for_path(&path.path)
 202            .cloned()
 203    }
 204
 205    pub fn create_worktree(
 206        &mut self,
 207        abs_path: impl Into<SanitizedPath>,
 208        visible: bool,
 209        cx: &mut Context<Self>,
 210    ) -> Task<Result<Entity<Worktree>>> {
 211        let abs_path: SanitizedPath = abs_path.into();
 212        if !self.loading_worktrees.contains_key(&abs_path) {
 213            let task = match &self.state {
 214                WorktreeStoreState::Remote {
 215                    upstream_client, ..
 216                } => {
 217                    if upstream_client.is_via_collab() {
 218                        Task::ready(Err(Arc::new(anyhow!("cannot create worktrees via collab"))))
 219                    } else {
 220                        self.create_ssh_worktree(
 221                            upstream_client.clone(),
 222                            abs_path.clone(),
 223                            visible,
 224                            cx,
 225                        )
 226                    }
 227                }
 228                WorktreeStoreState::Local { fs } => {
 229                    self.create_local_worktree(fs.clone(), abs_path.clone(), visible, cx)
 230                }
 231            };
 232
 233            self.loading_worktrees
 234                .insert(abs_path.clone(), task.shared());
 235        }
 236        let task = self.loading_worktrees.get(&abs_path).unwrap().clone();
 237        cx.spawn(|this, mut cx| async move {
 238            let result = task.await;
 239            this.update(&mut cx, |this, _| this.loading_worktrees.remove(&abs_path))
 240                .ok();
 241            match result {
 242                Ok(worktree) => Ok(worktree),
 243                Err(err) => Err((*err).cloned()),
 244            }
 245        })
 246    }
 247
 248    fn create_ssh_worktree(
 249        &mut self,
 250        client: AnyProtoClient,
 251        abs_path: impl Into<SanitizedPath>,
 252        visible: bool,
 253        cx: &mut Context<Self>,
 254    ) -> Task<Result<Entity<Worktree>, Arc<anyhow::Error>>> {
 255        let mut abs_path = Into::<SanitizedPath>::into(abs_path).to_string();
 256        // If we start with `/~` that means the ssh path was something like `ssh://user@host/~/home-dir-folder/`
 257        // in which case want to strip the leading the `/`.
 258        // On the host-side, the `~` will get expanded.
 259        // That's what git does too: https://github.com/libgit2/libgit2/issues/3345#issuecomment-127050850
 260        if abs_path.starts_with("/~") {
 261            abs_path = abs_path[1..].to_string();
 262        }
 263        if abs_path.is_empty() || abs_path == "/" {
 264            abs_path = "~/".to_string();
 265        }
 266        cx.spawn(|this, mut cx| async move {
 267            let this = this.upgrade().context("Dropped worktree store")?;
 268
 269            let response = client
 270                .request(proto::AddWorktree {
 271                    project_id: SSH_PROJECT_ID,
 272                    path: abs_path.clone(),
 273                    visible,
 274                })
 275                .await?;
 276
 277            if let Some(existing_worktree) = this.read_with(&cx, |this, cx| {
 278                this.worktree_for_id(WorktreeId::from_proto(response.worktree_id), cx)
 279            })? {
 280                return Ok(existing_worktree);
 281            }
 282
 283            let root_name = PathBuf::from(&response.canonicalized_path)
 284                .file_name()
 285                .map(|n| n.to_string_lossy().to_string())
 286                .unwrap_or(response.canonicalized_path.to_string());
 287
 288            let worktree = cx.update(|cx| {
 289                Worktree::remote(
 290                    SSH_PROJECT_ID,
 291                    0,
 292                    proto::WorktreeMetadata {
 293                        id: response.worktree_id,
 294                        root_name,
 295                        visible,
 296                        abs_path: response.canonicalized_path,
 297                    },
 298                    client,
 299                    cx,
 300                )
 301            })?;
 302
 303            this.update(&mut cx, |this, cx| {
 304                this.add(&worktree, cx);
 305            })?;
 306            Ok(worktree)
 307        })
 308    }
 309
 310    fn create_local_worktree(
 311        &mut self,
 312        fs: Arc<dyn Fs>,
 313        abs_path: impl Into<SanitizedPath>,
 314        visible: bool,
 315        cx: &mut Context<Self>,
 316    ) -> Task<Result<Entity<Worktree>, Arc<anyhow::Error>>> {
 317        let next_entry_id = self.next_entry_id.clone();
 318        let path: SanitizedPath = abs_path.into();
 319
 320        cx.spawn(move |this, mut cx| async move {
 321            let worktree = Worktree::local(path.clone(), visible, fs, next_entry_id, &mut cx).await;
 322
 323            let worktree = worktree?;
 324
 325            this.update(&mut cx, |this, cx| this.add(&worktree, cx))?;
 326
 327            if visible {
 328                cx.update(|cx| {
 329                    cx.add_recent_document(path.as_path());
 330                })
 331                .log_err();
 332            }
 333
 334            Ok(worktree)
 335        })
 336    }
 337
 338    pub fn add(&mut self, worktree: &Entity<Worktree>, cx: &mut Context<Self>) {
 339        let worktree_id = worktree.read(cx).id();
 340        debug_assert!(self.worktrees().all(|w| w.read(cx).id() != worktree_id));
 341
 342        let push_strong_handle = self.retain_worktrees || worktree.read(cx).is_visible();
 343        let handle = if push_strong_handle {
 344            WorktreeHandle::Strong(worktree.clone())
 345        } else {
 346            WorktreeHandle::Weak(worktree.downgrade())
 347        };
 348        if self.worktrees_reordered {
 349            self.worktrees.push(handle);
 350        } else {
 351            let i = match self
 352                .worktrees
 353                .binary_search_by_key(&Some(worktree.read(cx).abs_path()), |other| {
 354                    other.upgrade().map(|worktree| worktree.read(cx).abs_path())
 355                }) {
 356                Ok(i) | Err(i) => i,
 357            };
 358            self.worktrees.insert(i, handle);
 359        }
 360
 361        cx.emit(WorktreeStoreEvent::WorktreeAdded(worktree.clone()));
 362        self.send_project_updates(cx);
 363
 364        let handle_id = worktree.entity_id();
 365        cx.subscribe(worktree, |_, worktree, event, cx| {
 366            let worktree_id = worktree.update(cx, |worktree, _| worktree.id());
 367            match event {
 368                worktree::Event::UpdatedEntries(changes) => {
 369                    cx.emit(WorktreeStoreEvent::WorktreeUpdatedEntries(
 370                        worktree.read(cx).id(),
 371                        changes.clone(),
 372                    ));
 373                }
 374                worktree::Event::UpdatedGitRepositories(_) => {
 375                    cx.emit(WorktreeStoreEvent::WorktreeUpdatedGitRepositories(
 376                        worktree_id,
 377                    ));
 378                }
 379                worktree::Event::DeletedEntry(id) => {
 380                    cx.emit(WorktreeStoreEvent::WorktreeDeletedEntry(worktree_id, *id))
 381                }
 382            }
 383        })
 384        .detach();
 385        cx.observe_release(worktree, move |this, worktree, cx| {
 386            cx.emit(WorktreeStoreEvent::WorktreeReleased(
 387                handle_id,
 388                worktree.id(),
 389            ));
 390            cx.emit(WorktreeStoreEvent::WorktreeRemoved(
 391                handle_id,
 392                worktree.id(),
 393            ));
 394            this.send_project_updates(cx);
 395        })
 396        .detach();
 397    }
 398
 399    pub fn remove_worktree(&mut self, id_to_remove: WorktreeId, cx: &mut Context<Self>) {
 400        self.worktrees.retain(|worktree| {
 401            if let Some(worktree) = worktree.upgrade() {
 402                if worktree.read(cx).id() == id_to_remove {
 403                    cx.emit(WorktreeStoreEvent::WorktreeRemoved(
 404                        worktree.entity_id(),
 405                        id_to_remove,
 406                    ));
 407                    false
 408                } else {
 409                    true
 410                }
 411            } else {
 412                false
 413            }
 414        });
 415        self.send_project_updates(cx);
 416    }
 417
 418    pub fn set_worktrees_reordered(&mut self, worktrees_reordered: bool) {
 419        self.worktrees_reordered = worktrees_reordered;
 420    }
 421
 422    fn upstream_client(&self) -> Option<(AnyProtoClient, u64)> {
 423        match &self.state {
 424            WorktreeStoreState::Remote {
 425                upstream_client,
 426                upstream_project_id,
 427                ..
 428            } => Some((upstream_client.clone(), *upstream_project_id)),
 429            WorktreeStoreState::Local { .. } => None,
 430        }
 431    }
 432
 433    pub fn set_worktrees_from_proto(
 434        &mut self,
 435        worktrees: Vec<proto::WorktreeMetadata>,
 436        replica_id: ReplicaId,
 437        cx: &mut Context<Self>,
 438    ) -> Result<()> {
 439        let mut old_worktrees_by_id = self
 440            .worktrees
 441            .drain(..)
 442            .filter_map(|worktree| {
 443                let worktree = worktree.upgrade()?;
 444                Some((worktree.read(cx).id(), worktree))
 445            })
 446            .collect::<HashMap<_, _>>();
 447
 448        let (client, project_id) = self
 449            .upstream_client()
 450            .clone()
 451            .ok_or_else(|| anyhow!("invalid project"))?;
 452
 453        for worktree in worktrees {
 454            if let Some(old_worktree) =
 455                old_worktrees_by_id.remove(&WorktreeId::from_proto(worktree.id))
 456            {
 457                let push_strong_handle =
 458                    self.retain_worktrees || old_worktree.read(cx).is_visible();
 459                let handle = if push_strong_handle {
 460                    WorktreeHandle::Strong(old_worktree.clone())
 461                } else {
 462                    WorktreeHandle::Weak(old_worktree.downgrade())
 463                };
 464                self.worktrees.push(handle);
 465            } else {
 466                self.add(
 467                    &Worktree::remote(project_id, replica_id, worktree, client.clone(), cx),
 468                    cx,
 469                );
 470            }
 471        }
 472        self.send_project_updates(cx);
 473
 474        Ok(())
 475    }
 476
 477    pub fn move_worktree(
 478        &mut self,
 479        source: WorktreeId,
 480        destination: WorktreeId,
 481        cx: &mut Context<Self>,
 482    ) -> Result<()> {
 483        if source == destination {
 484            return Ok(());
 485        }
 486
 487        let mut source_index = None;
 488        let mut destination_index = None;
 489        for (i, worktree) in self.worktrees.iter().enumerate() {
 490            if let Some(worktree) = worktree.upgrade() {
 491                let worktree_id = worktree.read(cx).id();
 492                if worktree_id == source {
 493                    source_index = Some(i);
 494                    if destination_index.is_some() {
 495                        break;
 496                    }
 497                } else if worktree_id == destination {
 498                    destination_index = Some(i);
 499                    if source_index.is_some() {
 500                        break;
 501                    }
 502                }
 503            }
 504        }
 505
 506        let source_index =
 507            source_index.with_context(|| format!("Missing worktree for id {source}"))?;
 508        let destination_index =
 509            destination_index.with_context(|| format!("Missing worktree for id {destination}"))?;
 510
 511        if source_index == destination_index {
 512            return Ok(());
 513        }
 514
 515        let worktree_to_move = self.worktrees.remove(source_index);
 516        self.worktrees.insert(destination_index, worktree_to_move);
 517        self.worktrees_reordered = true;
 518        cx.emit(WorktreeStoreEvent::WorktreeOrderChanged);
 519        cx.notify();
 520        Ok(())
 521    }
 522
 523    pub fn disconnected_from_host(&mut self, cx: &mut App) {
 524        for worktree in &self.worktrees {
 525            if let Some(worktree) = worktree.upgrade() {
 526                worktree.update(cx, |worktree, _| {
 527                    if let Some(worktree) = worktree.as_remote_mut() {
 528                        worktree.disconnected_from_host();
 529                    }
 530                });
 531            }
 532        }
 533    }
 534
 535    pub fn send_project_updates(&mut self, cx: &mut Context<Self>) {
 536        let Some((downstream_client, project_id)) = self.downstream_client.clone() else {
 537            return;
 538        };
 539
 540        let update = proto::UpdateProject {
 541            project_id,
 542            worktrees: self.worktree_metadata_protos(cx),
 543        };
 544
 545        // collab has bad concurrency guarantees, so we send requests in serial.
 546        let update_project = if downstream_client.is_via_collab() {
 547            Some(downstream_client.request(update))
 548        } else {
 549            downstream_client.send(update).log_err();
 550            None
 551        };
 552        cx.spawn(|this, mut cx| async move {
 553            if let Some(update_project) = update_project {
 554                update_project.await?;
 555            }
 556
 557            this.update(&mut cx, |this, cx| {
 558                let worktrees = this.worktrees().collect::<Vec<_>>();
 559
 560                for worktree in worktrees {
 561                    worktree.update(cx, |worktree, cx| {
 562                        let client = downstream_client.clone();
 563                        worktree.observe_updates(project_id, cx, {
 564                            move |update| {
 565                                let client = client.clone();
 566                                async move {
 567                                    if client.is_via_collab() {
 568                                        client
 569                                            .request(update)
 570                                            .map(|result| result.log_err().is_some())
 571                                            .await
 572                                    } else {
 573                                        client.send(update).log_err().is_some()
 574                                    }
 575                                }
 576                            }
 577                        });
 578                    });
 579
 580                    cx.emit(WorktreeStoreEvent::WorktreeUpdateSent(worktree.clone()))
 581                }
 582
 583                anyhow::Ok(())
 584            })
 585        })
 586        .detach_and_log_err(cx);
 587    }
 588
 589    pub fn worktree_metadata_protos(&self, cx: &App) -> Vec<proto::WorktreeMetadata> {
 590        self.worktrees()
 591            .map(|worktree| {
 592                let worktree = worktree.read(cx);
 593                proto::WorktreeMetadata {
 594                    id: worktree.id().to_proto(),
 595                    root_name: worktree.root_name().into(),
 596                    visible: worktree.is_visible(),
 597                    abs_path: worktree.abs_path().to_string_lossy().into(),
 598                }
 599            })
 600            .collect()
 601    }
 602
 603    pub fn shared(
 604        &mut self,
 605        remote_id: u64,
 606        downstream_client: AnyProtoClient,
 607        cx: &mut Context<Self>,
 608    ) {
 609        self.retain_worktrees = true;
 610        self.downstream_client = Some((downstream_client, remote_id));
 611
 612        // When shared, retain all worktrees
 613        for worktree_handle in self.worktrees.iter_mut() {
 614            match worktree_handle {
 615                WorktreeHandle::Strong(_) => {}
 616                WorktreeHandle::Weak(worktree) => {
 617                    if let Some(worktree) = worktree.upgrade() {
 618                        *worktree_handle = WorktreeHandle::Strong(worktree);
 619                    }
 620                }
 621            }
 622        }
 623        self.send_project_updates(cx);
 624    }
 625
 626    pub fn unshared(&mut self, cx: &mut Context<Self>) {
 627        self.retain_worktrees = false;
 628        self.downstream_client.take();
 629
 630        // When not shared, only retain the visible worktrees
 631        for worktree_handle in self.worktrees.iter_mut() {
 632            if let WorktreeHandle::Strong(worktree) = worktree_handle {
 633                let is_visible = worktree.update(cx, |worktree, _| {
 634                    worktree.stop_observing_updates();
 635                    worktree.is_visible()
 636                });
 637                if !is_visible {
 638                    *worktree_handle = WorktreeHandle::Weak(worktree.downgrade());
 639                }
 640            }
 641        }
 642    }
 643
 644    /// search over all worktrees and return buffers that *might* match the search.
 645    pub fn find_search_candidates(
 646        &self,
 647        query: SearchQuery,
 648        limit: usize,
 649        open_entries: HashSet<ProjectEntryId>,
 650        fs: Arc<dyn Fs>,
 651        cx: &Context<Self>,
 652    ) -> Receiver<ProjectPath> {
 653        let snapshots = self
 654            .visible_worktrees(cx)
 655            .filter_map(|tree| {
 656                let tree = tree.read(cx);
 657                Some((tree.snapshot(), tree.as_local()?.settings()))
 658            })
 659            .collect::<Vec<_>>();
 660
 661        let executor = cx.background_executor().clone();
 662
 663        // We want to return entries in the order they are in the worktrees, so we have one
 664        // thread that iterates over the worktrees (and ignored directories) as necessary,
 665        // and pushes a oneshot::Receiver to the output channel and a oneshot::Sender to the filter
 666        // channel.
 667        // We spawn a number of workers that take items from the filter channel and check the query
 668        // against the version of the file on disk.
 669        let (filter_tx, filter_rx) = smol::channel::bounded(64);
 670        let (output_tx, output_rx) = smol::channel::bounded(64);
 671        let (matching_paths_tx, matching_paths_rx) = smol::channel::unbounded();
 672
 673        let input = cx.background_executor().spawn({
 674            let fs = fs.clone();
 675            let query = query.clone();
 676            async move {
 677                Self::find_candidate_paths(
 678                    fs,
 679                    snapshots,
 680                    open_entries,
 681                    query,
 682                    filter_tx,
 683                    output_tx,
 684                )
 685                .await
 686                .log_err();
 687            }
 688        });
 689        const MAX_CONCURRENT_FILE_SCANS: usize = 64;
 690        let filters = cx.background_executor().spawn(async move {
 691            let fs = &fs;
 692            let query = &query;
 693            executor
 694                .scoped(move |scope| {
 695                    for _ in 0..MAX_CONCURRENT_FILE_SCANS {
 696                        let filter_rx = filter_rx.clone();
 697                        scope.spawn(async move {
 698                            Self::filter_paths(fs, filter_rx, query)
 699                                .await
 700                                .log_with_level(log::Level::Debug);
 701                        })
 702                    }
 703                })
 704                .await;
 705        });
 706        cx.background_executor()
 707            .spawn(async move {
 708                let mut matched = 0;
 709                while let Ok(mut receiver) = output_rx.recv().await {
 710                    let Some(path) = receiver.next().await else {
 711                        continue;
 712                    };
 713                    let Ok(_) = matching_paths_tx.send(path).await else {
 714                        break;
 715                    };
 716                    matched += 1;
 717                    if matched == limit {
 718                        break;
 719                    }
 720                }
 721                drop(input);
 722                drop(filters);
 723            })
 724            .detach();
 725        matching_paths_rx
 726    }
 727
 728    fn scan_ignored_dir<'a>(
 729        fs: &'a Arc<dyn Fs>,
 730        snapshot: &'a worktree::Snapshot,
 731        path: &'a Path,
 732        query: &'a SearchQuery,
 733        include_root: bool,
 734        filter_tx: &'a Sender<MatchingEntry>,
 735        output_tx: &'a Sender<oneshot::Receiver<ProjectPath>>,
 736    ) -> BoxFuture<'a, Result<()>> {
 737        async move {
 738            let abs_path = snapshot.abs_path().join(path);
 739            let Some(mut files) = fs
 740                .read_dir(&abs_path)
 741                .await
 742                .with_context(|| format!("listing ignored path {abs_path:?}"))
 743                .log_err()
 744            else {
 745                return Ok(());
 746            };
 747
 748            let mut results = Vec::new();
 749
 750            while let Some(Ok(file)) = files.next().await {
 751                let Some(metadata) = fs
 752                    .metadata(&file)
 753                    .await
 754                    .with_context(|| format!("fetching fs metadata for {abs_path:?}"))
 755                    .log_err()
 756                    .flatten()
 757                else {
 758                    continue;
 759                };
 760                if metadata.is_symlink || metadata.is_fifo {
 761                    continue;
 762                }
 763                results.push((
 764                    file.strip_prefix(snapshot.abs_path())?.to_path_buf(),
 765                    !metadata.is_dir,
 766                ))
 767            }
 768            results.sort_by(|(a_path, _), (b_path, _)| a_path.cmp(b_path));
 769            for (path, is_file) in results {
 770                if is_file {
 771                    if query.filters_path() {
 772                        let matched_path = if include_root {
 773                            let mut full_path = PathBuf::from(snapshot.root_name());
 774                            full_path.push(&path);
 775                            query.file_matches(&full_path)
 776                        } else {
 777                            query.file_matches(&path)
 778                        };
 779                        if !matched_path {
 780                            continue;
 781                        }
 782                    }
 783                    let (tx, rx) = oneshot::channel();
 784                    output_tx.send(rx).await?;
 785                    filter_tx
 786                        .send(MatchingEntry {
 787                            respond: tx,
 788                            worktree_path: snapshot.abs_path().clone(),
 789                            path: ProjectPath {
 790                                worktree_id: snapshot.id(),
 791                                path: Arc::from(path),
 792                            },
 793                        })
 794                        .await?;
 795                } else {
 796                    Self::scan_ignored_dir(
 797                        fs,
 798                        snapshot,
 799                        &path,
 800                        query,
 801                        include_root,
 802                        filter_tx,
 803                        output_tx,
 804                    )
 805                    .await?;
 806                }
 807            }
 808            Ok(())
 809        }
 810        .boxed()
 811    }
 812
 813    async fn find_candidate_paths(
 814        fs: Arc<dyn Fs>,
 815        snapshots: Vec<(worktree::Snapshot, WorktreeSettings)>,
 816        open_entries: HashSet<ProjectEntryId>,
 817        query: SearchQuery,
 818        filter_tx: Sender<MatchingEntry>,
 819        output_tx: Sender<oneshot::Receiver<ProjectPath>>,
 820    ) -> Result<()> {
 821        let include_root = snapshots.len() > 1;
 822        for (snapshot, settings) in snapshots {
 823            for entry in snapshot.entries(query.include_ignored(), 0) {
 824                if entry.is_dir() && entry.is_ignored {
 825                    if !settings.is_path_excluded(&entry.path) {
 826                        Self::scan_ignored_dir(
 827                            &fs,
 828                            &snapshot,
 829                            &entry.path,
 830                            &query,
 831                            include_root,
 832                            &filter_tx,
 833                            &output_tx,
 834                        )
 835                        .await?;
 836                    }
 837                    continue;
 838                }
 839
 840                if entry.is_fifo || !entry.is_file() {
 841                    continue;
 842                }
 843
 844                if query.filters_path() {
 845                    let matched_path = if include_root {
 846                        let mut full_path = PathBuf::from(snapshot.root_name());
 847                        full_path.push(&entry.path);
 848                        query.file_matches(&full_path)
 849                    } else {
 850                        query.file_matches(&entry.path)
 851                    };
 852                    if !matched_path {
 853                        continue;
 854                    }
 855                }
 856
 857                let (mut tx, rx) = oneshot::channel();
 858
 859                if open_entries.contains(&entry.id) {
 860                    tx.send(ProjectPath {
 861                        worktree_id: snapshot.id(),
 862                        path: entry.path.clone(),
 863                    })
 864                    .await?;
 865                } else {
 866                    filter_tx
 867                        .send(MatchingEntry {
 868                            respond: tx,
 869                            worktree_path: snapshot.abs_path().clone(),
 870                            path: ProjectPath {
 871                                worktree_id: snapshot.id(),
 872                                path: entry.path.clone(),
 873                            },
 874                        })
 875                        .await?;
 876                }
 877
 878                output_tx.send(rx).await?;
 879            }
 880        }
 881        Ok(())
 882    }
 883
 884    pub fn branches(
 885        &self,
 886        project_path: ProjectPath,
 887        cx: &App,
 888    ) -> Task<Result<Vec<git::repository::Branch>>> {
 889        let Some(worktree) = self.worktree_for_id(project_path.worktree_id, cx) else {
 890            return Task::ready(Err(anyhow!("No worktree found for ProjectPath")));
 891        };
 892
 893        match worktree.read(cx) {
 894            Worktree::Local(local_worktree) => {
 895                let branches = util::maybe!({
 896                    let worktree_error = |error| {
 897                        format!(
 898                            "{} for worktree {}",
 899                            error,
 900                            local_worktree.abs_path().to_string_lossy()
 901                        )
 902                    };
 903
 904                    let entry = local_worktree
 905                        .git_entry(project_path.path)
 906                        .with_context(|| worktree_error("No git entry found"))?;
 907
 908                    let repo = local_worktree
 909                        .get_local_repo(&entry)
 910                        .with_context(|| worktree_error("No repository found"))?
 911                        .repo()
 912                        .clone();
 913
 914                    repo.branches()
 915                });
 916
 917                Task::ready(branches)
 918            }
 919            Worktree::Remote(remote_worktree) => {
 920                let request = remote_worktree.client().request(proto::GitBranches {
 921                    project_id: remote_worktree.project_id(),
 922                    repository: Some(proto::ProjectPath {
 923                        worktree_id: project_path.worktree_id.to_proto(),
 924                        path: project_path.path.to_string_lossy().to_string(), // Root path
 925                    }),
 926                });
 927
 928                cx.background_executor().spawn(async move {
 929                    let response = request.await?;
 930
 931                    let branches = response
 932                        .branches
 933                        .into_iter()
 934                        .map(|proto_branch| git::repository::Branch {
 935                            is_head: proto_branch.is_head,
 936                            name: proto_branch.name.into(),
 937                            unix_timestamp: proto_branch
 938                                .unix_timestamp
 939                                .map(|timestamp| timestamp as i64),
 940                        })
 941                        .collect();
 942
 943                    Ok(branches)
 944                })
 945            }
 946        }
 947    }
 948
 949    pub fn update_or_create_branch(
 950        &self,
 951        repository: ProjectPath,
 952        new_branch: String,
 953        cx: &App,
 954    ) -> Task<Result<()>> {
 955        let Some(worktree) = self.worktree_for_id(repository.worktree_id, cx) else {
 956            return Task::ready(Err(anyhow!("No worktree found for ProjectPath")));
 957        };
 958
 959        match worktree.read(cx) {
 960            Worktree::Local(local_worktree) => {
 961                let result = util::maybe!({
 962                    let worktree_error = |error| {
 963                        format!(
 964                            "{} for worktree {}",
 965                            error,
 966                            local_worktree.abs_path().to_string_lossy()
 967                        )
 968                    };
 969
 970                    let entry = local_worktree
 971                        .git_entry(repository.path)
 972                        .with_context(|| worktree_error("No git entry found"))?;
 973
 974                    let repo = local_worktree
 975                        .get_local_repo(&entry)
 976                        .with_context(|| worktree_error("No repository found"))?
 977                        .repo()
 978                        .clone();
 979
 980                    if !repo.branch_exits(&new_branch)? {
 981                        repo.create_branch(&new_branch)?;
 982                    }
 983
 984                    repo.change_branch(&new_branch)?;
 985                    Ok(())
 986                });
 987
 988                Task::ready(result)
 989            }
 990            Worktree::Remote(remote_worktree) => {
 991                let request = remote_worktree.client().request(proto::UpdateGitBranch {
 992                    project_id: remote_worktree.project_id(),
 993                    repository: Some(proto::ProjectPath {
 994                        worktree_id: repository.worktree_id.to_proto(),
 995                        path: repository.path.to_string_lossy().to_string(), // Root path
 996                    }),
 997                    branch_name: new_branch,
 998                });
 999
1000                cx.background_executor().spawn(async move {
1001                    request.await?;
1002                    Ok(())
1003                })
1004            }
1005        }
1006    }
1007
1008    async fn filter_paths(
1009        fs: &Arc<dyn Fs>,
1010        mut input: Receiver<MatchingEntry>,
1011        query: &SearchQuery,
1012    ) -> Result<()> {
1013        let mut input = pin!(input);
1014        while let Some(mut entry) = input.next().await {
1015            let abs_path = entry.worktree_path.join(&entry.path.path);
1016            let Some(file) = fs.open_sync(&abs_path).await.log_err() else {
1017                continue;
1018            };
1019
1020            let mut file = BufReader::new(file);
1021            let file_start = file.fill_buf()?;
1022
1023            if let Err(Some(starting_position)) =
1024                std::str::from_utf8(file_start).map_err(|e| e.error_len())
1025            {
1026                // Before attempting to match the file content, throw away files that have invalid UTF-8 sequences early on;
1027                // That way we can still match files in a streaming fashion without having look at "obviously binary" files.
1028                return Err(anyhow!(
1029                    "Invalid UTF-8 sequence in file {abs_path:?} at byte position {starting_position}"
1030                ));
1031            }
1032
1033            if query.detect(file).unwrap_or(false) {
1034                entry.respond.send(entry.path).await?
1035            }
1036        }
1037
1038        Ok(())
1039    }
1040
1041    pub async fn handle_create_project_entry(
1042        this: Entity<Self>,
1043        envelope: TypedEnvelope<proto::CreateProjectEntry>,
1044        mut cx: AsyncApp,
1045    ) -> Result<proto::ProjectEntryResponse> {
1046        let worktree = this.update(&mut cx, |this, cx| {
1047            let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
1048            this.worktree_for_id(worktree_id, cx)
1049                .ok_or_else(|| anyhow!("worktree not found"))
1050        })??;
1051        Worktree::handle_create_entry(worktree, envelope.payload, cx).await
1052    }
1053
1054    pub async fn handle_copy_project_entry(
1055        this: Entity<Self>,
1056        envelope: TypedEnvelope<proto::CopyProjectEntry>,
1057        mut cx: AsyncApp,
1058    ) -> Result<proto::ProjectEntryResponse> {
1059        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
1060        let worktree = this.update(&mut cx, |this, cx| {
1061            this.worktree_for_entry(entry_id, cx)
1062                .ok_or_else(|| anyhow!("worktree not found"))
1063        })??;
1064        Worktree::handle_copy_entry(worktree, envelope.payload, cx).await
1065    }
1066
1067    pub async fn handle_delete_project_entry(
1068        this: Entity<Self>,
1069        envelope: TypedEnvelope<proto::DeleteProjectEntry>,
1070        mut cx: AsyncApp,
1071    ) -> Result<proto::ProjectEntryResponse> {
1072        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
1073        let worktree = this.update(&mut cx, |this, cx| {
1074            this.worktree_for_entry(entry_id, cx)
1075                .ok_or_else(|| anyhow!("worktree not found"))
1076        })??;
1077        Worktree::handle_delete_entry(worktree, envelope.payload, cx).await
1078    }
1079
1080    pub async fn handle_expand_project_entry(
1081        this: Entity<Self>,
1082        envelope: TypedEnvelope<proto::ExpandProjectEntry>,
1083        mut cx: AsyncApp,
1084    ) -> Result<proto::ExpandProjectEntryResponse> {
1085        let entry_id = ProjectEntryId::from_proto(envelope.payload.entry_id);
1086        let worktree = this
1087            .update(&mut cx, |this, cx| this.worktree_for_entry(entry_id, cx))?
1088            .ok_or_else(|| anyhow!("invalid request"))?;
1089        Worktree::handle_expand_entry(worktree, envelope.payload, cx).await
1090    }
1091
1092    pub async fn handle_git_branches(
1093        this: Entity<Self>,
1094        branches: TypedEnvelope<proto::GitBranches>,
1095        cx: AsyncApp,
1096    ) -> Result<proto::GitBranchesResponse> {
1097        let project_path = branches
1098            .payload
1099            .repository
1100            .clone()
1101            .context("Invalid GitBranches call")?;
1102        let project_path = ProjectPath {
1103            worktree_id: WorktreeId::from_proto(project_path.worktree_id),
1104            path: Path::new(&project_path.path).into(),
1105        };
1106
1107        let branches = this
1108            .read_with(&cx, |this, cx| this.branches(project_path, cx))?
1109            .await?;
1110
1111        Ok(proto::GitBranchesResponse {
1112            branches: branches
1113                .into_iter()
1114                .map(|branch| proto::Branch {
1115                    is_head: branch.is_head,
1116                    name: branch.name.to_string(),
1117                    unix_timestamp: branch.unix_timestamp.map(|timestamp| timestamp as u64),
1118                })
1119                .collect(),
1120        })
1121    }
1122
1123    pub async fn handle_update_branch(
1124        this: Entity<Self>,
1125        update_branch: TypedEnvelope<proto::UpdateGitBranch>,
1126        cx: AsyncApp,
1127    ) -> Result<proto::Ack> {
1128        let project_path = update_branch
1129            .payload
1130            .repository
1131            .clone()
1132            .context("Invalid GitBranches call")?;
1133        let project_path = ProjectPath {
1134            worktree_id: WorktreeId::from_proto(project_path.worktree_id),
1135            path: Path::new(&project_path.path).into(),
1136        };
1137        let new_branch = update_branch.payload.branch_name;
1138
1139        this.read_with(&cx, |this, cx| {
1140            this.update_or_create_branch(project_path, new_branch, cx)
1141        })?
1142        .await?;
1143
1144        Ok(proto::Ack {})
1145    }
1146}
1147
1148#[derive(Clone, Debug)]
1149enum WorktreeHandle {
1150    Strong(Entity<Worktree>),
1151    Weak(WeakEntity<Worktree>),
1152}
1153
1154impl WorktreeHandle {
1155    fn upgrade(&self) -> Option<Entity<Worktree>> {
1156        match self {
1157            WorktreeHandle::Strong(handle) => Some(handle.clone()),
1158            WorktreeHandle::Weak(handle) => handle.upgrade(),
1159        }
1160    }
1161}