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